1 /*
2  * Copyright (c) 2019 Foundries.io
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * Source material for IPSO Timer object (3340):
9  * http://www.openmobilealliance.org/tech/profiles/lwm2m/3340.xml
10  */
11 
12 #define LOG_MODULE_NAME net_ipso_timer
13 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
14 
15 #include <zephyr/logging/log.h>
16 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
17 
18 #include <stdint.h>
19 #include <zephyr/init.h>
20 
21 #include "lwm2m_object.h"
22 #include "lwm2m_engine.h"
23 #include "lwm2m_resource_ids.h"
24 
25 #define TIMER_VERSION_MAJOR 1
26 #define TIMER_VERSION_MINOR 0
27 
28 #define TIMER_MAX_ID		11
29 
30 #define MAX_INSTANCE_COUNT	CONFIG_LWM2M_IPSO_TIMER_INSTANCE_COUNT
31 
32 /*
33  * Calculate resource instances as follows:
34  * start with TIMER_MAX_ID
35  * subtract EXEC resources (1)
36  */
37 #define RESOURCE_INSTANCE_COUNT	(TIMER_MAX_ID - 1)
38 
39 enum ipso_timer_mode {
40 	TIMER_MODE_OFF = 0,
41 	TIMER_MODE_ONE_SHOT,
42 	TIMER_MODE_INTERVAL,		/* TODO */
43 	TIMER_MODE_DELAY_ON_PICKUP,	/* TODO */
44 	TIMER_MODE_DELAY_ON_DROPOUT,	/* TODO */
45 };
46 
47 /* resource state */
48 struct ipso_timer_data {
49 	double delay_duration;
50 	double remaining_time;
51 	double min_off_time;
52 	double cumulative_time;
53 
54 	uint64_t trigger_offset;
55 	uint32_t trigger_counter;
56 	uint32_t cumulative_time_ms;
57 
58 	struct k_work_delayable timer_work;
59 
60 	uint16_t obj_inst_id;
61 	uint8_t timer_mode;
62 	bool enabled;
63 	bool active;
64 };
65 
66 static struct ipso_timer_data timer_data[MAX_INSTANCE_COUNT];
67 
68 static struct lwm2m_engine_obj ipso_timer;
69 static struct lwm2m_engine_obj_field fields[] = {
70 	OBJ_FIELD_DATA(DELAY_DURATION_RID, RW, FLOAT),
71 	OBJ_FIELD_DATA(REMAINING_TIME_RID, R_OPT, FLOAT),
72 	OBJ_FIELD_DATA(MINIMUM_OFF_TIME_RID, RW_OPT, FLOAT),
73 	OBJ_FIELD_EXECUTE_OPT(TRIGGER_RID),
74 	OBJ_FIELD_DATA(ON_OFF_RID, RW_OPT, BOOL),
75 	OBJ_FIELD_DATA(DIGITAL_INPUT_COUNTER_RID, RW_OPT, U32), /* TODO */
76 	OBJ_FIELD_DATA(CUMULATIVE_TIME_RID, RW_OPT, FLOAT),
77 	OBJ_FIELD_DATA(DIGITAL_STATE_RID, R_OPT, BOOL),
78 	OBJ_FIELD_DATA(COUNTER_RID, R_OPT, U32),
79 	OBJ_FIELD_DATA(TIMER_MODE_RID, RW_OPT, U8),
80 	OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
81 };
82 
83 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
84 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][TIMER_MAX_ID];
85 static struct lwm2m_engine_res_inst
86 		res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
87 
get_timer_index(uint16_t obj_inst_id)88 static int get_timer_index(uint16_t obj_inst_id)
89 {
90 	int i, ret = -ENOENT;
91 
92 	for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
93 		if (!inst[i].obj || inst[i].obj_inst_id != obj_inst_id) {
94 			continue;
95 		}
96 
97 		ret = i;
98 		break;
99 	}
100 
101 	return ret;
102 }
103 
start_timer(struct ipso_timer_data * timer)104 static int start_timer(struct ipso_timer_data *timer)
105 {
106 	uint32_t temp = 0U;
107 	struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_TIMER_ID, timer->obj_inst_id,
108 					       DIGITAL_STATE_RID);
109 
110 	/* make sure timer is enabled and not already active */
111 	if (timer->timer_mode == TIMER_MODE_OFF || timer->active ||
112 	    !timer->enabled) {
113 		return -EINVAL;
114 	}
115 
116 	/* check min off time from last trigger_offset */
117 	temp = timer->min_off_time * MSEC_PER_SEC;
118 	if (k_uptime_get() < timer->trigger_offset + temp) {
119 		return -EINVAL;
120 	}
121 
122 	/* TODO: check delay_duration > 0 ? other modes can it be 0? */
123 
124 	timer->trigger_offset = k_uptime_get();
125 	timer->trigger_counter += 1U;
126 
127 	lwm2m_set_bool(&path, true);
128 
129 	temp = timer->delay_duration * MSEC_PER_SEC;
130 	k_work_reschedule(&timer->timer_work, K_MSEC(temp));
131 
132 	return 0;
133 }
134 
stop_timer(struct ipso_timer_data * timer,bool cancel)135 static int stop_timer(struct ipso_timer_data *timer, bool cancel)
136 {
137 	struct lwm2m_obj_path path = LWM2M_OBJ(IPSO_OBJECT_TIMER_ID, timer->obj_inst_id,
138 					       DIGITAL_STATE_RID);
139 
140 	/* make sure timer is active */
141 	if (!timer->active) {
142 		return -EINVAL;
143 	}
144 
145 	timer->cumulative_time_ms += k_uptime_get() - timer->trigger_offset;
146 	lwm2m_set_bool(&path, false);
147 
148 	if (cancel) {
149 		k_work_cancel_delayable(&timer->timer_work);
150 	}
151 
152 	return 0;
153 }
154 
remaining_time_read_cb(uint16_t obj_inst_id,uint16_t res_id,uint16_t res_inst_id,size_t * data_len)155 static void *remaining_time_read_cb(uint16_t obj_inst_id,
156 				    uint16_t res_id, uint16_t res_inst_id,
157 				    size_t *data_len)
158 {
159 	uint32_t temp = 0U;
160 	int i;
161 
162 	i = get_timer_index(obj_inst_id);
163 	if (i < 0) {
164 		return NULL;
165 	}
166 
167 	if (timer_data[i].active) {
168 		temp = timer_data[i].delay_duration * MSEC_PER_SEC;
169 		temp -= (k_uptime_get() - timer_data[i].trigger_offset);
170 		timer_data[i].remaining_time = (double)temp / MSEC_PER_SEC;
171 	} else {
172 		timer_data[i].remaining_time = 0;
173 	}
174 
175 	*data_len = sizeof(timer_data[i].remaining_time);
176 	return &timer_data[i].remaining_time;
177 }
178 
cumulative_time_read_cb(uint16_t obj_inst_id,uint16_t res_id,uint16_t res_inst_id,size_t * data_len)179 static void *cumulative_time_read_cb(uint16_t obj_inst_id,
180 				     uint16_t res_id, uint16_t res_inst_id,
181 				     size_t *data_len)
182 {
183 	int i;
184 	uint32_t temp;
185 
186 	i = get_timer_index(obj_inst_id);
187 	if (i < 0) {
188 		return NULL;
189 	}
190 
191 	temp = timer_data[i].cumulative_time_ms;
192 	if (timer_data[i].active) {
193 		temp += k_uptime_get() - timer_data[i].trigger_offset;
194 	}
195 
196 	timer_data[i].cumulative_time = (double)temp / MSEC_PER_SEC;
197 
198 	*data_len = sizeof(timer_data[i].cumulative_time);
199 	return &timer_data[i].cumulative_time;
200 }
201 
cumulative_time_post_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,size_t offset)202 static int cumulative_time_post_write_cb(uint16_t obj_inst_id,
203 					 uint16_t res_id,
204 					 uint16_t res_inst_id, uint8_t *data,
205 					 uint16_t data_len, bool last_block,
206 					 size_t total_size, size_t offset)
207 {
208 	int i;
209 
210 	i = get_timer_index(obj_inst_id);
211 	if (i < 0) {
212 		return i;
213 	}
214 
215 	timer_data[i].cumulative_time_ms = 0U;
216 	return 0;
217 }
218 
enabled_post_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,size_t offset)219 static int enabled_post_write_cb(uint16_t obj_inst_id, uint16_t res_id,
220 				 uint16_t res_inst_id, uint8_t *data,
221 				 uint16_t data_len, bool last_block,
222 				 size_t total_size, size_t offset)
223 {
224 	int i;
225 
226 	i = get_timer_index(obj_inst_id);
227 	if (i < 0) {
228 		return i;
229 	}
230 
231 	/* check if timer is active and move to disabled state */
232 	if (!timer_data[i].enabled && timer_data[i].active) {
233 		return stop_timer(&timer_data[i], true);
234 	}
235 
236 	return 0;
237 }
238 
trigger_counter_post_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,size_t offset)239 static int trigger_counter_post_write_cb(uint16_t obj_inst_id,
240 					 uint16_t res_id,
241 					 uint16_t res_inst_id, uint8_t *data,
242 					 uint16_t data_len, bool last_block,
243 					 size_t total_size, size_t offset)
244 {
245 	int i;
246 
247 	i = get_timer_index(obj_inst_id);
248 	if (i < 0) {
249 		return i;
250 	}
251 
252 	timer_data[i].trigger_counter = 0U;
253 	return 0;
254 }
255 
timer_work_cb(struct k_work * work)256 static void timer_work_cb(struct k_work *work)
257 {
258 	struct k_work_delayable *dwork = k_work_delayable_from_work(work);
259 	struct ipso_timer_data *timer = CONTAINER_OF(dwork,
260 						     struct ipso_timer_data,
261 						     timer_work);
262 	stop_timer(timer, false);
263 }
264 
timer_trigger_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)265 static int timer_trigger_cb(uint16_t obj_inst_id,
266 			    uint8_t *args, uint16_t args_len)
267 {
268 	int i;
269 
270 	i = get_timer_index(obj_inst_id);
271 	if (i < 0) {
272 		return i;
273 	}
274 
275 	return start_timer(&timer_data[i]);
276 }
277 
timer_inst_create(uint16_t obj_inst_id)278 static struct lwm2m_engine_obj_inst *timer_inst_create(uint16_t obj_inst_id)
279 {
280 	int index, avail = -1, i = 0, j = 0;
281 
282 	/* Check that there is no other instance with this ID */
283 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
284 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
285 			LOG_ERR("Can not create instance - "
286 				"already existing: %u", obj_inst_id);
287 			return NULL;
288 		}
289 
290 		/* Save first available slot index */
291 		if (avail < 0 && !inst[index].obj) {
292 			avail = index;
293 		}
294 	}
295 
296 	if (avail < 0) {
297 		LOG_ERR("Can not create instance - no more room: %u",
298 			obj_inst_id);
299 		return NULL;
300 	}
301 
302 	/* Set default values */
303 	(void)memset(&timer_data[avail], 0, sizeof(timer_data[avail]));
304 	k_work_init_delayable(&timer_data[avail].timer_work, timer_work_cb);
305 	timer_data[avail].delay_duration = 5; /* 5 seconds */
306 	timer_data[avail].enabled = true;
307 	timer_data[avail].timer_mode = TIMER_MODE_ONE_SHOT;
308 	timer_data[avail].obj_inst_id = obj_inst_id;
309 
310 	(void)memset(res[avail], 0,
311 		     sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
312 	init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
313 
314 	/* initialize instance resource data */
315 	INIT_OBJ_RES_DATA(DELAY_DURATION_RID, res[avail], i,
316 			  res_inst[avail], j, &timer_data[avail].delay_duration,
317 			  sizeof(timer_data[avail].delay_duration));
318 	INIT_OBJ_RES(REMAINING_TIME_RID, res[avail], i, res_inst[avail], j, 1,
319 		     false, true, &timer_data[avail].remaining_time,
320 		     sizeof(timer_data[avail].remaining_time),
321 		     remaining_time_read_cb, NULL, NULL, NULL, NULL);
322 	INIT_OBJ_RES_DATA(MINIMUM_OFF_TIME_RID, res[avail], i,
323 			  res_inst[avail], j, &timer_data[avail].min_off_time,
324 			  sizeof(timer_data[avail].min_off_time));
325 	INIT_OBJ_RES_EXECUTE(TRIGGER_RID, res[avail], i, timer_trigger_cb);
326 	INIT_OBJ_RES(ON_OFF_RID, res[avail], i, res_inst[avail], j, 1, false,
327 		     true, &timer_data[avail].enabled,
328 		     sizeof(timer_data[avail].enabled),
329 		     NULL, NULL, NULL, enabled_post_write_cb, NULL);
330 	INIT_OBJ_RES(CUMULATIVE_TIME_RID, res[avail], i, res_inst[avail], j, 1,
331 		     false, true, &timer_data[avail].cumulative_time,
332 		     sizeof(timer_data[avail].cumulative_time),
333 		     cumulative_time_read_cb, NULL, NULL,
334 		     cumulative_time_post_write_cb, NULL);
335 	INIT_OBJ_RES_DATA(DIGITAL_STATE_RID, res[avail], i,
336 			  res_inst[avail], j, &timer_data[avail].active,
337 			  sizeof(timer_data[avail].active));
338 	INIT_OBJ_RES(COUNTER_RID, res[avail], i, res_inst[avail], j, 1, false,
339 		     true, &timer_data[avail].trigger_counter,
340 		     sizeof(timer_data[avail].trigger_counter),
341 		     NULL, NULL, NULL, trigger_counter_post_write_cb, NULL);
342 	INIT_OBJ_RES_DATA(TIMER_MODE_RID, res[avail], i, res_inst[avail], j,
343 			  &timer_data[avail].timer_mode,
344 			  sizeof(timer_data[avail].timer_mode));
345 	INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i,
346 			     res_inst[avail], j);
347 
348 	inst[avail].resources = res[avail];
349 	inst[avail].resource_count = i;
350 
351 	LOG_DBG("Create IPSO Timer instance: %d", obj_inst_id);
352 
353 	return &inst[avail];
354 }
355 
ipso_timer_init(void)356 static int ipso_timer_init(void)
357 {
358 	ipso_timer.obj_id = IPSO_OBJECT_TIMER_ID;
359 	ipso_timer.version_major = TIMER_VERSION_MAJOR;
360 	ipso_timer.version_minor = TIMER_VERSION_MINOR;
361 	ipso_timer.is_core = false;
362 	ipso_timer.fields = fields;
363 	ipso_timer.field_count = ARRAY_SIZE(fields);
364 	ipso_timer.max_instance_count = MAX_INSTANCE_COUNT;
365 	ipso_timer.create_cb = timer_inst_create;
366 	lwm2m_register_obj(&ipso_timer);
367 
368 	return 0;
369 }
370 
371 LWM2M_OBJ_INIT(ipso_timer_init);
372