1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2018-2019 Foundries.io
4  *
5  * SPDX-License-Identifier: Apache-2.0
6  */
7 
8 #define LOG_MODULE_NAME net_lwm2m_obj_server
9 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
10 
11 #include <zephyr/logging/log.h>
12 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
13 
14 #include <stdint.h>
15 #include <zephyr/init.h>
16 
17 #include "lwm2m_object.h"
18 #include "lwm2m_obj_server.h"
19 #include "lwm2m_rd_client.h"
20 #include "lwm2m_registry.h"
21 #include "lwm2m_engine.h"
22 
23 #define SERVER_VERSION_MAJOR 1
24 #if defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)
25 #define SERVER_VERSION_MINOR 1
26 #define SERVER_MAX_ID		 24
27 #else
28 #define SERVER_VERSION_MINOR 0
29 #define SERVER_MAX_ID		 9
30 #endif /* defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1) */
31 
32 /* Server flags */
33 #define SERVER_FLAG_STORE_NOTIFY	2
34 
35 #define MAX_INSTANCE_COUNT		CONFIG_LWM2M_SERVER_INSTANCE_COUNT
36 
37 #define TRANSPORT_BINDING_LEN		4
38 
39 /*
40  * Calculate resource instances as follows:
41  * start with SERVER_MAX_ID
42  * subtract EXEC resources (2)
43  */
44 #define RESOURCE_INSTANCE_COUNT	(SERVER_MAX_ID - 2)
45 
46 /* resource state variables */
47 static uint16_t server_id[MAX_INSTANCE_COUNT];
48 static uint32_t lifetime[MAX_INSTANCE_COUNT];
49 static uint32_t default_min_period[MAX_INSTANCE_COUNT];
50 static uint32_t default_max_period[MAX_INSTANCE_COUNT];
51 static k_timepoint_t disabled_until[MAX_INSTANCE_COUNT];
52 static uint32_t disabled_timeout[MAX_INSTANCE_COUNT];
53 static uint8_t  server_flag_store_notify[MAX_INSTANCE_COUNT];
54 static char  transport_binding[MAX_INSTANCE_COUNT][TRANSPORT_BINDING_LEN];
55 /* Server object version 1.1 */
56 static uint8_t priority[MAX_INSTANCE_COUNT];
57 static bool mute_send[MAX_INSTANCE_COUNT];
58 static bool boostrap_on_fail[MAX_INSTANCE_COUNT];
59 
60 static struct lwm2m_engine_obj server;
61 static struct lwm2m_engine_obj_field fields[] = {
62 	OBJ_FIELD_DATA(SERVER_SHORT_SERVER_ID, R, U16),
63 	OBJ_FIELD_DATA(SERVER_LIFETIME_ID, RW, U32),
64 	OBJ_FIELD_DATA(SERVER_DEFAULT_MIN_PERIOD_ID, RW_OPT, U32),
65 	OBJ_FIELD_DATA(SERVER_DEFAULT_MAX_PERIOD_ID, RW_OPT, U32),
66 	OBJ_FIELD_EXECUTE_OPT(SERVER_DISABLE_ID),
67 	OBJ_FIELD_DATA(SERVER_DISABLE_TIMEOUT_ID, RW_OPT, U32),
68 	OBJ_FIELD_DATA(SERVER_STORE_NOTIFY_ID, RW, BOOL),
69 	/* Mark Transport Binding is RO but BOOTSTRAP needs to write it */
70 	OBJ_FIELD_DATA(SERVER_TRANSPORT_BINDING_ID, RW, STRING),
71 	OBJ_FIELD_EXECUTE(SERVER_REG_UPDATE_TRIGGER_ID),
72 #if defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)
73 	OBJ_FIELD_EXECUTE(SERVER_BOOTSTRAP_UPDATE_TRIGGER_ID),
74 	OBJ_FIELD_DATA(SERVER_APN_LINK_ID, RW_OPT, OBJLNK),
75 	OBJ_FIELD_DATA(SERVER_TLS_DTLS_ALERT_CODE_ID, R_OPT, U8),
76 	OBJ_FIELD_DATA(SERVER_LAST_BOOTSTRAPPED_ID, R_OPT, TIME),
77 	OBJ_FIELD_DATA(SERVER_REGISTRATION_PRIORITY_ORDER_ID, RW_OPT, U8),
78 	OBJ_FIELD_DATA(SERVER_INITIAL_REGISTRATION_DELAY_TIMER_ID, W_OPT, U16),
79 	OBJ_FIELD_DATA(SERVER_REGISTRATION_FAILURE_BLOCK_ID, W_OPT, BOOL),
80 	OBJ_FIELD_DATA(SERVER_BOOTSTRAP_ON_REGISTRATION_FAILURE_ID, RW_OPT, BOOL),
81 	OBJ_FIELD_DATA(SERVER_COMMUNICATION_RETRY_COUNT_ID, W_OPT, U16),
82 	OBJ_FIELD_DATA(SERVER_COMMUNICATION_RETRY_TIMER_ID, W_OPT, U16),
83 	OBJ_FIELD_DATA(SERVER_COMMUNICATION_SEQUENCE_DELAY_TIMER_ID, W_OPT, U16),
84 	OBJ_FIELD_DATA(SERVER_COMMUNICATION_SEQUENCE_RETRY_TIMER_ID, W_OPT, U16),
85 	OBJ_FIELD_DATA(SERVER_SMS_TRIGGER_ID, RW_OPT, BOOL),
86 	OBJ_FIELD_DATA(SERVER_PREFERRED_TRANSPORT_ID, RW_OPT, STRING),
87 	OBJ_FIELD_DATA(SERVER_MUTE_SEND_ID, RW_OPT, BOOL),
88 #endif /* defined(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1) */
89 
90 };
91 
92 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
93 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][SERVER_MAX_ID];
94 static struct lwm2m_engine_res_inst
95 			res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
96 
disable_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)97 static int disable_cb(uint16_t obj_inst_id, uint8_t *args, uint16_t args_len)
98 {
99 	ARG_UNUSED(args);
100 	ARG_UNUSED(args_len);
101 
102 	int ret;
103 
104 	for (int i = 0; i < MAX_INSTANCE_COUNT; i++) {
105 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
106 			LOG_DBG("DISABLE %d", obj_inst_id);
107 			ret = lwm2m_rd_client_server_disabled(obj_inst_id);
108 			if (ret == 0) {
109 				disabled_until[i] =
110 					sys_timepoint_calc(K_SECONDS(disabled_timeout[i]));
111 				return 0;
112 			}
113 			return ret;
114 		}
115 	}
116 
117 	return -ENOENT;
118 }
119 
update_trigger_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)120 static int update_trigger_cb(uint16_t obj_inst_id,
121 			     uint8_t *args, uint16_t args_len)
122 {
123 	engine_trigger_update(false);
124 	return 0;
125 }
126 
bootstrap_trigger_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)127 static int bootstrap_trigger_cb(uint16_t obj_inst_id,
128 			     uint8_t *args, uint16_t args_len)
129 {
130 	return engine_trigger_bootstrap();
131 }
132 
lwm2m_server_get_mute_send(uint16_t obj_inst_id)133 bool lwm2m_server_get_mute_send(uint16_t obj_inst_id)
134 {
135 	int i;
136 
137 	for (i = 0; i < ARRAY_SIZE(inst); i++) {
138 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
139 			return mute_send[i];
140 		}
141 	}
142 	return false;
143 }
144 
145 
lifetime_write_cb(uint16_t obj_inst_id,uint16_t res_id,uint16_t res_inst_id,uint8_t * data,uint16_t data_len,bool last_block,size_t total_size)146 static int lifetime_write_cb(uint16_t obj_inst_id, uint16_t res_id,
147 			     uint16_t res_inst_id, uint8_t *data,
148 			     uint16_t data_len, bool last_block,
149 			     size_t total_size)
150 {
151 	ARG_UNUSED(obj_inst_id);
152 	ARG_UNUSED(res_id);
153 	ARG_UNUSED(res_inst_id);
154 	ARG_UNUSED(data);
155 	ARG_UNUSED(data_len);
156 	ARG_UNUSED(last_block);
157 	ARG_UNUSED(total_size);
158 
159 	engine_trigger_update(false);
160 	return 0;
161 }
162 
server_get_instance_s32(uint16_t obj_inst_id,int32_t * data,int32_t default_value)163 static int32_t server_get_instance_s32(uint16_t obj_inst_id, int32_t *data,
164 				     int32_t default_value)
165 {
166 	int i;
167 
168 	for (i = 0; i < ARRAY_SIZE(inst); i++) {
169 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
170 			return data[i];
171 		}
172 	}
173 
174 	return default_value;
175 }
176 
lwm2m_server_get_pmin(uint16_t obj_inst_id)177 int32_t lwm2m_server_get_pmin(uint16_t obj_inst_id)
178 {
179 	return server_get_instance_s32(obj_inst_id, default_min_period,
180 				       CONFIG_LWM2M_SERVER_DEFAULT_PMIN);
181 }
182 
lwm2m_server_get_pmax(uint16_t obj_inst_id)183 int32_t lwm2m_server_get_pmax(uint16_t obj_inst_id)
184 {
185 	return server_get_instance_s32(obj_inst_id, default_max_period,
186 				       CONFIG_LWM2M_SERVER_DEFAULT_PMAX);
187 }
188 
lwm2m_server_get_ssid(uint16_t obj_inst_id)189 int lwm2m_server_get_ssid(uint16_t obj_inst_id)
190 {
191 	int i;
192 
193 	for (i = 0; i < ARRAY_SIZE(inst); i++) {
194 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
195 			return server_id[i];
196 		}
197 	}
198 
199 	return -ENOENT;
200 }
201 
lwm2m_server_short_id_to_inst(uint16_t short_id)202 int lwm2m_server_short_id_to_inst(uint16_t short_id)
203 {
204 	int i;
205 
206 	for (i = 0; i < ARRAY_SIZE(inst); i++) {
207 		if (inst[i].obj && server_id[i] == short_id) {
208 			return inst[i].obj_inst_id;
209 		}
210 	}
211 
212 	return -ENOENT;
213 }
214 
lwm2m_server_inst_id_to_index(uint16_t obj_inst_id)215 static int lwm2m_server_inst_id_to_index(uint16_t obj_inst_id)
216 {
217 	for (int i = 0; i < ARRAY_SIZE(inst); i++) {
218 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
219 			return i;
220 		}
221 	}
222 	return -1;
223 }
224 
lwm2m_server_is_enabled(uint16_t obj_inst_id)225 bool lwm2m_server_is_enabled(uint16_t obj_inst_id)
226 {
227 	int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
228 
229 	if (idx < 0) {
230 		return false;
231 	}
232 	return sys_timepoint_expired(disabled_until[idx]);
233 }
234 
lwm2m_server_disable(uint16_t obj_inst_id,k_timeout_t timeout)235 int lwm2m_server_disable(uint16_t obj_inst_id, k_timeout_t timeout)
236 {
237 	int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
238 
239 	if (idx < 0) {
240 		return -ENOENT;
241 	}
242 	disabled_until[idx] = sys_timepoint_calc(timeout);
243 	return 0;
244 }
245 
lwm2m_server_get_disabled_time(uint16_t obj_inst_id)246 k_timepoint_t lwm2m_server_get_disabled_time(uint16_t obj_inst_id)
247 {
248 	int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
249 
250 	if (idx < 0) {
251 		return sys_timepoint_calc(K_FOREVER);
252 	}
253 	return disabled_until[idx];
254 }
255 
lwm2m_server_reset_timestamps(void)256 void lwm2m_server_reset_timestamps(void)
257 {
258 	for (int i = 0; i < ARRAY_SIZE(inst); i++) {
259 		disabled_until[i] = sys_timepoint_calc(K_NO_WAIT);
260 	}
261 }
262 
lwm2m_server_select(uint16_t * obj_inst_id)263 bool lwm2m_server_select(uint16_t *obj_inst_id)
264 {
265 	uint8_t min = UINT8_MAX;
266 	uint8_t max = 0;
267 
268 	/* Find priority boundaries */
269 	if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
270 		for (int i = 0; i < ARRAY_SIZE(inst); i++) {
271 			if (min > priority[i]) {
272 				min = priority[i];
273 			}
274 			if (max < priority[i]) {
275 				max = priority[i];
276 			}
277 		}
278 	} else  {
279 		min = max = 0;
280 	}
281 
282 	for (uint8_t prio = min; prio <= max; prio++) {
283 		for (int i = 0; i < ARRAY_SIZE(inst); i++) {
284 			/* Disabled for a period */
285 			if (!lwm2m_server_is_enabled(inst[i].obj_inst_id)) {
286 				continue;
287 			}
288 
289 			/* Invalid short IDs */
290 			if (server_id[i] == 0 || server_id[i] == UINT16_MAX) {
291 				continue;
292 			}
293 
294 			/* Check priority */
295 			if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
296 				if (priority[i] > prio) {
297 					continue;
298 				}
299 			}
300 			if (obj_inst_id) {
301 				*obj_inst_id = inst[i].obj_inst_id;
302 			}
303 			return true;
304 		}
305 	}
306 
307 	LOG_ERR("No server candidate found");
308 	return false;
309 }
310 
lwm2m_server_get_prio(uint16_t obj_inst_id)311 uint8_t lwm2m_server_get_prio(uint16_t obj_inst_id)
312 {
313 	if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
314 		int idx = lwm2m_server_inst_id_to_index(obj_inst_id);
315 
316 		if (idx < 0) {
317 			return UINT8_MAX;
318 		}
319 		return priority[idx];
320 	}
321 
322 	return (uint8_t)obj_inst_id % UINT8_MAX;
323 }
324 
server_create(uint16_t obj_inst_id)325 static struct lwm2m_engine_obj_inst *server_create(uint16_t obj_inst_id)
326 {
327 	int index, i = 0, j = 0;
328 
329 	/* Check that there is no other instance with this ID */
330 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
331 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
332 			LOG_ERR("Can not create instance - "
333 				"already existing: %u", obj_inst_id);
334 			return NULL;
335 		}
336 	}
337 
338 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
339 		if (!inst[index].obj) {
340 			break;
341 		}
342 	}
343 
344 	if (index >= MAX_INSTANCE_COUNT) {
345 		LOG_ERR("Can not create instance - "
346 			"no more room: %u", obj_inst_id);
347 		return NULL;
348 	}
349 
350 	/* Set default values */
351 	disabled_until[i] = sys_timepoint_calc(K_NO_WAIT);
352 	server_flag_store_notify[index] = 0U;
353 	server_id[index] = index + 1;
354 	lifetime[index] = CONFIG_LWM2M_ENGINE_DEFAULT_LIFETIME;
355 	default_min_period[index] = CONFIG_LWM2M_SERVER_DEFAULT_PMIN;
356 	default_max_period[index] = CONFIG_LWM2M_SERVER_DEFAULT_PMAX;
357 	disabled_timeout[index] = 86400U;
358 	boostrap_on_fail[index] = true;
359 
360 	lwm2m_engine_get_binding(transport_binding[index]);
361 
362 	(void)memset(res[index], 0,
363 		     sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
364 	init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
365 
366 	/* initialize instance resource data */
367 	INIT_OBJ_RES_DATA(SERVER_SHORT_SERVER_ID, res[index], i,
368 			  res_inst[index], j,
369 			  &server_id[index], sizeof(*server_id));
370 	INIT_OBJ_RES(SERVER_LIFETIME_ID, res[index], i, res_inst[index], j,
371 		     1U, false, true, &lifetime[index], sizeof(*lifetime),
372 		     NULL, NULL, NULL, lifetime_write_cb, NULL);
373 	INIT_OBJ_RES_DATA(SERVER_DEFAULT_MIN_PERIOD_ID, res[index], i,
374 			  res_inst[index], j,
375 			  &default_min_period[index],
376 			  sizeof(*default_min_period));
377 	INIT_OBJ_RES_DATA(SERVER_DEFAULT_MAX_PERIOD_ID, res[index], i,
378 			  res_inst[index], j,
379 			  &default_max_period[index],
380 			  sizeof(*default_max_period));
381 	INIT_OBJ_RES_EXECUTE(SERVER_DISABLE_ID, res[index], i, disable_cb);
382 	INIT_OBJ_RES_DATA(SERVER_DISABLE_TIMEOUT_ID, res[index], i,
383 			  res_inst[index], j,
384 			  &disabled_timeout[index],
385 			  sizeof(*disabled_timeout));
386 	INIT_OBJ_RES_DATA(SERVER_STORE_NOTIFY_ID, res[index], i,
387 			  res_inst[index], j,
388 			  &server_flag_store_notify[index],
389 			  sizeof(*server_flag_store_notify));
390 	/* Mark Transport Binding RO as we only support UDP atm */
391 	INIT_OBJ_RES_DATA_LEN(SERVER_TRANSPORT_BINDING_ID, res[index], i, res_inst[index], j,
392 			      transport_binding[index], TRANSPORT_BINDING_LEN,
393 			      strlen(transport_binding[index]) + 1);
394 	INIT_OBJ_RES_EXECUTE(SERVER_REG_UPDATE_TRIGGER_ID, res[index], i, update_trigger_cb);
395 
396 	if (IS_ENABLED(CONFIG_LWM2M_SERVER_OBJECT_VERSION_1_1)) {
397 		mute_send[index] = false;
398 		priority[index] = 0;
399 		INIT_OBJ_RES_EXECUTE(SERVER_BOOTSTRAP_UPDATE_TRIGGER_ID, res[index], i,
400 				     bootstrap_trigger_cb);
401 		INIT_OBJ_RES_OPTDATA(SERVER_APN_LINK_ID, res[index], i, res_inst[index], j);
402 		INIT_OBJ_RES_OPTDATA(SERVER_TLS_DTLS_ALERT_CODE_ID, res[index], i, res_inst[index],
403 				     j);
404 		INIT_OBJ_RES_OPTDATA(SERVER_LAST_BOOTSTRAPPED_ID, res[index], i, res_inst[index],
405 				     j);
406 		INIT_OBJ_RES_DATA(SERVER_REGISTRATION_PRIORITY_ORDER_ID, res[index], i,
407 				  res_inst[index], j, &priority[index], sizeof(uint8_t));
408 		INIT_OBJ_RES_OPTDATA(SERVER_INITIAL_REGISTRATION_DELAY_TIMER_ID, res[index], i,
409 				     res_inst[index], j);
410 		INIT_OBJ_RES_OPTDATA(SERVER_REGISTRATION_FAILURE_BLOCK_ID, res[index], i,
411 				     res_inst[index], j);
412 		INIT_OBJ_RES_DATA(SERVER_BOOTSTRAP_ON_REGISTRATION_FAILURE_ID, res[index], i,
413 				  res_inst[index], j, &boostrap_on_fail[index], sizeof(bool));
414 		INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_RETRY_COUNT_ID, res[index], i,
415 				     res_inst[index], j);
416 		INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_RETRY_TIMER_ID, res[index], i,
417 				     res_inst[index], j);
418 		INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_SEQUENCE_DELAY_TIMER_ID, res[index], i,
419 				     res_inst[index], j);
420 		INIT_OBJ_RES_OPTDATA(SERVER_COMMUNICATION_SEQUENCE_RETRY_TIMER_ID, res[index], i,
421 				     res_inst[index], j);
422 		INIT_OBJ_RES_OPTDATA(SERVER_SMS_TRIGGER_ID, res[index], i, res_inst[index], j);
423 		INIT_OBJ_RES_OPTDATA(SERVER_PREFERRED_TRANSPORT_ID, res[index], i, res_inst[index],
424 				     j);
425 		INIT_OBJ_RES_DATA(SERVER_MUTE_SEND_ID, res[index], i, res_inst[index], j,
426 				  &mute_send[index], sizeof(bool));
427 	}
428 
429 	inst[index].resources = res[index];
430 	inst[index].resource_count = i;
431 	LOG_DBG("Create LWM2M server instance: %d", obj_inst_id);
432 	return &inst[index];
433 }
434 
lwm2m_server_init(void)435 static int lwm2m_server_init(void)
436 {
437 	int ret = 0;
438 
439 	server.obj_id = LWM2M_OBJECT_SERVER_ID;
440 	server.version_major = SERVER_VERSION_MAJOR;
441 	server.version_minor = SERVER_VERSION_MINOR;
442 	server.is_core = true;
443 	server.fields = fields;
444 	server.field_count = ARRAY_SIZE(fields);
445 	server.max_instance_count = MAX_INSTANCE_COUNT;
446 	server.create_cb = server_create;
447 	lwm2m_register_obj(&server);
448 
449 	/* don't create automatically when using bootstrap */
450 	if (!IS_ENABLED(CONFIG_LWM2M_RD_CLIENT_SUPPORT_BOOTSTRAP)) {
451 		struct lwm2m_engine_obj_inst *obj_inst = NULL;
452 
453 		ret = lwm2m_create_obj_inst(LWM2M_OBJECT_SERVER_ID, 0, &obj_inst);
454 		if (ret < 0) {
455 			LOG_ERR("Create LWM2M server instance 0 error: %d", ret);
456 		}
457 	}
458 
459 	return ret;
460 }
461 
462 LWM2M_CORE_INIT(lwm2m_server_init);
463