Friendly LWM2M client
Lwm2mServer.cpp
Go to the documentation of this file.
1 /*
2  * Lwm2mServer
3  * Generated on: 2023-11-02 16:41:01
4  * Created by: Sinai RnD
5  */
6 
8 
9 #include "Resource.h"
10 #include "ItemOp.h"
11 #include "WppTypes.h"
12 #include "WppLogs.h"
13 #include "WppClient.h"
14 
15 /* --------------- Code_cpp block 0 start --------------- */
16 #if RES_1_11
17 #define TLS_DTLS_ALERT_CODE_MIN 0
18 #define TLS_DTLS_ALERT_CODE_MAX 255
19 #endif
20 /* --------------- Code_cpp block 0 end --------------- */
21 
22 #define TAG "Lwm2mServer"
23 
24 namespace wpp {
25 
26 Lwm2mServer::Lwm2mServer(lwm2m_context_t &context, const OBJ_LINK_T &id): Instance(context, id) {
27 
28  /* --------------- Code_cpp block 1 start --------------- */
29  _requestBootstrapTaskId = WPP_ERR_TASK_ID;
30  _requestDeregistrationTaskId = WPP_ERR_TASK_ID;
31  /* --------------- Code_cpp block 1 end --------------- */
32 
33  resourcesCreate();
34  resourcesInit();
35 
36  /* --------------- Code_cpp block 2 start --------------- */
37  /* --------------- Code_cpp block 2 end --------------- */
38 }
39 
41  /* --------------- Code_cpp block 3 start --------------- */
42  WppTaskQueue::requestToRemoveTask(_requestBootstrapTaskId);
43  WppTaskQueue::requestToRemoveTask(_requestDeregistrationTaskId);
44 
45  /* --------------- Code_cpp block 3 end --------------- */
46 }
47 
49  return ctx.registry().lwm2mServer();
50 }
51 
53  Instance *inst = ctx.registry().lwm2mServer().instance(instId);
54  if (!inst) return NULL;
55  return static_cast<Lwm2mServer*>(inst);
56 }
57 
59  Instance *inst = ctx.registry().lwm2mServer().createInstance(instId);
60  if (!inst) return NULL;
61  return static_cast<Lwm2mServer*>(inst);
62 }
63 
65  return ctx.registry().lwm2mServer().remove(instId);
66 }
67 
68 void Lwm2mServer::serverOperationNotifier(Instance *securityInst, ItemOp::TYPE type, const ResLink &resLink) {
69  /* --------------- Code_cpp block 4 start --------------- */
70  if (type == ItemOp::WRITE && resLink.resId == LIFETIME_1) {
71  INT_T serverId = resource(SHORT_SERVER_ID_0)->get<INT_T>();
72  lwm2m_update_registration(&getContext(), serverId, true, false);
73  }
74  /* --------------- Code_cpp block 4 end --------------- */
75 
76  operationNotify(*this, resLink, type);
77 
78  /* --------------- Code_cpp block 5 start --------------- */
79  /* --------------- Code_cpp block 5 end --------------- */
80 }
81 
83  if (type == ItemOp::WRITE || type == ItemOp::DELETE) notifyResChanged(resLink.resId, resLink.resInstId);
84 
85  /* --------------- Code_cpp block 6 start --------------- */
86  if (type == ItemOp::WRITE && resLink.resId == LIFETIME_1 && getContext().state == STATE_READY){
87  INT_T serverId = resource(SHORT_SERVER_ID_0)->get<INT_T>();
88  INT_T lifetime = resource(LIFETIME_1)->get<INT_T>();
89  lwm2m_update_server_lifetime(&getContext(), serverId, lifetime);
90  }
91  #if defined(LWM2M_SUPPORT_SENML_JSON) && RES_1_23
92  if (type == ItemOp::WRITE && resLink.resId == MUTE_SEND_23) {
93  INT_T serverId = resource(SHORT_SERVER_ID_0)->get<INT_T>();
95  lwm2m_update_server_mute(&getContext(), serverId, mute);
96  }
97  #endif
98  #if RES_1_5
99  if (type == ItemOp::WRITE && resLink.resId == DISABLE_TIMEOUT_5) {
100  INT_T serverId = resource(SHORT_SERVER_ID_0)->get<INT_T>();
101  INT_T timeout = resource(DISABLE_TIMEOUT_5)->get<INT_T>();
102  lwm2m_update_server_disable_timeout(&getContext(), serverId, timeout);
103  }
104  #endif
105  /* --------------- Code_cpp block 6 end --------------- */
106 }
107 
108 void Lwm2mServer::resourcesCreate() {
109  std::vector<Resource> resources = {
112  #if RES_1_2
113  {DEFAULT_MINIMUM_PERIOD_2, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
114  #endif
115  #if RES_1_3
116  {DEFAULT_MAXIMUM_PERIOD_3, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::INT },
117  #endif
118  #if RES_1_4
120  #endif
121  #if RES_1_5
123  #endif
127  #if RES_1_9
129  #endif
130  #if RES_1_10
132  #endif
133  #if RES_1_11
134  {TLS_DTLS_ALERT_CODE_11, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::UINT },
135  #endif
136  #if RES_1_12
137  {LAST_BOOTSTRAPPED_12, ItemOp(ItemOp::READ), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::TIME },
138  #endif
139  #if RES_1_13
140  {REGISTRATION_PRIORITY_ORDER_13, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::UINT },
141  #endif
142  #if RES_1_14
143  {INITIAL_REGISTRATION_DELAY_TIMER_14, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::UINT },
144  #endif
145  #if RES_1_15
146  {REGISTRATION_FAILURE_BLOCK_15, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::BOOL },
147  #endif
148  #if RES_1_16
149  {BOOTSTRAP_ON_REGISTRATION_FAILURE_16, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::BOOL },
150  #endif
151  #if RES_1_17
152  {COMMUNICATION_RETRY_COUNT_17, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::UINT },
153  #endif
154  #if RES_1_18
155  {COMMUNICATION_RETRY_TIMER_18, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::UINT },
156  #endif
157  #if RES_1_19
158  {COMMUNICATION_SEQUENCE_DELAY_TIMER_19, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::UINT },
159  #endif
160  #if RES_1_20
161  {COMMUNICATION_SEQUENCE_RETRY_COUNT_20, ItemOp(ItemOp::READ|ItemOp::WRITE), IS_SINGLE::SINGLE, IS_MANDATORY::OPTIONAL, TYPE_ID::UINT },
162  #endif
163  #if RES_1_21
165  #endif
166  #if RES_1_22
168  #endif
169  #if RES_1_23
171  #endif
172  };
173  setupResources(std::move(resources));
174 }
175 
176 void Lwm2mServer::resourcesInit() {
177  /* --------------- Code_cpp block 7 start --------------- */
178  // TODO: The most part of the server resources logic must be implemented
179  // on wakaama core level, because the Server is only a state holder and
180  // at this level, it does not have the required information for doing
181  // sings described in the documentation.
182 
184  resource(SHORT_SERVER_ID_0)->setDataVerifier((VERIFY_INT_T)[](const INT_T& value) { return SINGLE_INSTANCE_ID < value && value < ID_T_MAX_VAL; });
185 
187 
188  #if RES_1_2
189  resource(DEFAULT_MINIMUM_PERIOD_2)->set<INT_T>(0);
190  #endif
191 
192  #if RES_1_3
193  resource(DEFAULT_MAXIMUM_PERIOD_3)->set<INT_T>(0);
194  #endif
195 
196  #if RES_1_4
197  // Resource starts the separated task to deregistration from all currently registered servers.
198  // The registration proccess performs after deregistration immediatelly. If the object delete
199  // before task execution, this task will be deleted at destructor.
200  resource(DISABLE_4)->set<EXECUTE_T>([this](Instance& inst, ID_T resId, const OPAQUE_T& data) {
201  #ifdef LWM2M_BOOTSTRAP
202  if (!WppTaskQueue::isTaskExist(_requestDeregistrationTaskId)) {
203  WPP_LOGI(TAG, "Deregistration Request Trigger: Deregistration is started");
204  _requestDeregistrationTaskId = WppTaskQueue::addTask(WPP_TASK_MIN_DELAY_S, [this](WppClient &client, void *ctx) -> bool {
205  lwm2m_deregister(&getContext());
206  return true;
207  });
208  } else {
209  WPP_LOGI(TAG, "Deregistration Request Trigger: Deregistration is already in the progress");
210  }
211  #else
212  (void)this;
213  #endif
214  return true;
215  });
216  #endif
217 
218  #if RES_1_5
220  #endif
221 
222  // TODO: Notification Storing (Res id 6) must be implemented by wakaama core
224 
226  resource(BINDING_7)->setDataVerifier((VERIFY_STRING_T)[](const STRING_T& value) { return wppBindingValidate(value); });
227 
228  // TODO: Registration Update (Res id 8) must be implemented by wakaama core
229  resource(REGISTRATION_UPDATE_TRIGGER_8)->set<EXECUTE_T>([this](Instance& inst, ID_T resId, const OPAQUE_T& data) {
230  INT_T serverId = resource(SHORT_SERVER_ID_0)->get<INT_T>();
231  WPP_LOGI(TAG, "Registration Update Trigger: serverId -> %d", serverId);
232  lwm2m_update_registration(&getContext(), serverId, false, true);
233  return true;
234  });
235 
236  // TODO: Bootstrap Request (Res id 9) must be implemented by wakaama core or WppClient
237  #if RES_1_9
238  resource(BOOTSTRAP_REQUEST_TRIGGER_9)->set<EXECUTE_T>([this](Instance& inst, ID_T resId, const OPAQUE_T& data) {
239  if (!WppTaskQueue::isTaskExist(_requestBootstrapTaskId)) {
240  WPP_LOGI(TAG, "Bootstrap Request Trigger: Bootstrap request is started");
241  _requestBootstrapTaskId = WppTaskQueue::addTask(WPP_TASK_MIN_DELAY_S, [this](WppClient &client, void *ctx) -> bool {
242  getContext().state = STATE_BOOTSTRAP_REQUIRED;
243  return true;
244  });
245  } else {
246  WPP_LOGI(TAG, "Bootstrap Request Trigger: Bootstrap request is already in progress");
247  }
248  return true;
249  });
250  #endif
251 
252  #if RES_1_10
253  resource(APN_LINK_10)->set<OBJ_LINK_T>({});
254  #endif
255 
256  #if RES_1_11
257  resource(TLS_DTLS_ALERT_CODE_11)->set<UINT_T>(TLS_DTLS_ALERT_CODE_MIN);
258  resource(TLS_DTLS_ALERT_CODE_11)->setDataVerifier((VERIFY_UINT_T)[](const UINT_T& value) { return TLS_DTLS_ALERT_CODE_MIN <= value && value <= TLS_DTLS_ALERT_CODE_MAX; });
259  #endif
260 
261  #if RES_1_12
262  resource(LAST_BOOTSTRAPPED_12)->set<TIME_T>(0);
263  #endif
264 
265  #if RES_1_13
266  resource(REGISTRATION_PRIORITY_ORDER_13)->set<UINT_T>(0);
267  #endif
268 
269  #if RES_1_14
270  resource(INITIAL_REGISTRATION_DELAY_TIMER_14)->set<UINT_T>(0);
271  #endif
272 
273  #if RES_1_15
274  resource(REGISTRATION_FAILURE_BLOCK_15)->set<BOOL_T>(false);
275  #endif
276 
277  #if RES_1_16
278  resource(BOOTSTRAP_ON_REGISTRATION_FAILURE_16)->set<BOOL_T>(false);
279  #endif
280 
281  #if RES_1_17
282  resource(COMMUNICATION_RETRY_COUNT_17)->set<UINT_T>(0);
283  #endif
284 
285  #if RES_1_18
286  resource(COMMUNICATION_RETRY_TIMER_18)->set<UINT_T>(0);
287  #endif
288 
289  #if RES_1_19
290  resource(COMMUNICATION_SEQUENCE_DELAY_TIMER_19)->set<UINT_T>(0);
291  #endif
292 
293  #if RES_1_20
294  resource(COMMUNICATION_SEQUENCE_RETRY_COUNT_20)->set<UINT_T>(0);
295  #endif
296 
297  #if RES_1_21
298  resource(TRIGGER_21)->set<BOOL_T>(false);
299  #endif
300 
301  #if RES_1_22
302  resource(PREFERRED_TRANSPORT_22)->set<STRING_T>("");
303  #endif
304 
305  #if RES_1_23
306  resource(MUTE_SEND_23)->set<BOOL_T>(true);
307  #endif
308  /* --------------- Code_cpp block 7 end --------------- */
309 }
310 
311 /* --------------- Code_cpp block 8 start --------------- */
312 /* --------------- Code_cpp block 8 end --------------- */
313 
314 } /* namespace wpp */
#define TAG
Definition: Lwm2mServer.cpp:22
#define WPP_LOGI(TAG, FMT,...)
Definition: WppLogs.h:37
#define WPP_TASK_MIN_DELAY_S
Definition: WppTaskQueue.h:17
#define WPP_ERR_TASK_ID
Definition: WppTaskQueue.h:22
#define SINGLE_INSTANCE_ID
Definition: WppTypes.h:13
#define ID_T_MAX_VAL
Definition: WppTypes.h:16
void operationNotify(Instance &inst, const ResLink &resLink, ItemOp::TYPE type)
Notifies the observers about an operation on an instance resource.
Definition: InstSubject.h:78
Instance is interface class that implements manipulation with derived class resources....
Definition: Instance.h:40
lwm2m_context_t & getContext()
Return context that can be used by derived class.
Definition: Instance.cpp:50
void notifyResChanged(ID_T resId, ID_T resInstId=ID_T_MAX_VAL)
Notify server about resource value change.
Definition: Instance.cpp:17
Instance(lwm2m_context_t &context, const OBJ_LINK_T &id)
Definition: Instance.h:42
static bool removeInst(WppClient &ctx, ID_T instId)
Removes an instance of the object.
Definition: Lwm2mServer.cpp:64
void serverOperationNotifier(Instance *securityInst, ItemOp::TYPE type, const ResLink &resLink) override
This method must be implemented by the derived class, and handle information about resource operation...
Definition: Lwm2mServer.cpp:68
Lwm2mServer(lwm2m_context_t &context, const OBJ_LINK_T &id)
Definition: Lwm2mServer.cpp:26
void userOperationNotifier(ItemOp::TYPE type, const ResLink &resLink) override
This method must be implemented by the derived class, and handle information about resource operation...
Definition: Lwm2mServer.cpp:82
static Object & object(WppClient &ctx)
Gets the Object reference.
Definition: Lwm2mServer.cpp:48
static Lwm2mServer * instance(WppClient &ctx, ID_T instId=ID_T_MAX_VAL)
Gets an instance of the object.
Definition: Lwm2mServer.cpp:52
@ NOTIFICATION_STORING_WHEN_DISABLED_OR_OFFLINE_6
Definition: Lwm2mServer.h:45
@ REGISTRATION_UPDATE_TRIGGER_8
Definition: Lwm2mServer.h:47
@ BOOTSTRAP_REQUEST_TRIGGER_9
Definition: Lwm2mServer.h:49
static Lwm2mServer * createInst(WppClient &ctx, ID_T instId=ID_T_MAX_VAL)
Creates an instance of the object.
Definition: Lwm2mServer.cpp:58
The Object class implements manipulation with Instance interface class and its inheritors.
Definition: Object.h:32
Instance * instance(ID_T instanceID=ID_T_MAX_VAL)
Gets an instance of the object.
Definition: Object.cpp:102
virtual Instance * createInstance(ID_T instanceID=ID_T_MAX_VAL)=0
Creates an instance of the object.
bool remove(ID_T instanceID)
Removes an instance of the object.
Definition: Object.cpp:82
void setupResources(const std::vector< Resource > &resources)
This methods setup resources list.
Resource * resource(ID_T resId)
This method return resource ptr if it exists. If resources does not exist then return NULL.
std::vector< Resource > & resources()
This method return list with all resources that has been defined.
bool set(const T &value, ID_T resInstId=SINGLE_INSTANCE_ID)
Set data value by copy for the resource (instance)
Definition: Resource.h:253
bool setDataVerifier(const DATA_VERIFIER_T &verifier)
Set data verifier for the resource.
Definition: Resource.cpp:164
const T & get(ID_T resInstId=SINGLE_INSTANCE_ID)
Definition: Resource.h:290
Represents a client interface for Wpp library.
Definition: WppClient.h:37
WppRegistry & registry()
Gets the WppRegistry associated with the WppClient.
Definition: WppClient.cpp:93
Object & lwm2mServer()
Definition: WppRegistry.cpp:81
static bool isTaskExist(task_id_t id)
Returns true if task exists in the queue.
static task_id_t addTask(time_t delaySec, task_t task)
Add task to queue, ctx that passed to task equals to NULL.
static void requestToRemoveTask(task_id_t id)
This function does not immediately delete the task, it only marks it as one that should be deleted at...
The WppConnection class represents a connection interface for the Wpp library.
Definition: WppClient.cpp:14
bool wppBindingValidate(const STRING_T &binding)
Definition: WppBindings.cpp:6
bool BOOL_T
Wpp data types bindings.
Definition: WppTypes.h:40
std::function< bool(Instance &, ID_T, const OPAQUE_T &)> EXECUTE_T
Definition: WppTypes.h:72
int64_t INT_T
Definition: WppTypes.h:41
uint16_t ID_T
Definition: WppTypes.h:15
std::function< bool(const STRING_T &)> VERIFY_STRING_T
Definition: WppTypes.h:100
std::function< bool(const INT_T &)> VERIFY_INT_T
Data validation function types.
Definition: WppTypes.h:94
uint64_t UINT_T
Definition: WppTypes.h:42
std::string STRING_T
Definition: WppTypes.h:45
INT_T TIME_T
Definition: WppTypes.h:44
std::function< bool(const UINT_T &)> VERIFY_UINT_T
Definition: WppTypes.h:95
std::vector< uint8_t > OPAQUE_T
Opaque - represent buffer or string as lwm2m_data_t.value.asBuffer.
Definition: WppTypes.h:49
The ItemOp struct represents the operations that can be performed on a instance/resource.
Definition: ItemOp.h:24
TYPE
Enum representing the different types of operations.
Definition: ItemOp.h:29
@ DELETE
Definition: ItemOp.h:36
@ EXECUTE
Definition: ItemOp.h:33