1 /*
2  * Copyright (c) 2019 Foundries.io
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 /*
8  * Source material for IPSO On/Off Switch object (3342):
9  * http://www.openmobilealliance.org/tech/profiles/lwm2m/3342.xml
10  */
11 
12 #define LOG_MODULE_NAME net_ipso_onoff_switch
13 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
14 
15 #include <logging/log.h>
16 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
17 
18 #include <stdint.h>
19 #include <init.h>
20 
21 #include "lwm2m_object.h"
22 #include "lwm2m_engine.h"
23 #include "lwm2m_resource_ids.h"
24 
25 #define SWITCH_VERSION_MAJOR 1
26 
27 #if defined(CONFIG_LWM2M_IPSO_ONOFF_SWITCH_VERSION_1_1)
28 #define SWITCH_VERSION_MINOR 1
29 #define SWITCH_MAX_ID 7
30 #else
31 #define SWITCH_VERSION_MINOR 0
32 #define SWITCH_MAX_ID 5
33 #endif /* defined(CONFIG_LWM2M_IPSO_ONOFF_SWITCH_VERSION_1_1) */
34 
35 #define MAX_INSTANCE_COUNT	CONFIG_LWM2M_IPSO_ONOFF_SWITCH_INSTANCE_COUNT
36 
37 /*
38  * Calculate resource instances as follows:
39  * start with SWITCH_MAX_ID
40  */
41 #define RESOURCE_INSTANCE_COUNT        (SWITCH_MAX_ID)
42 
43 /* resource state */
44 struct ipso_switch_data {
45 	uint64_t trigger_offset;
46 	int64_t on_time_sec;
47 	int64_t off_time_sec;
48 	int64_t counter;
49 	uint16_t obj_inst_id;
50 	bool last_state;
51 	bool state;
52 };
53 
54 static struct ipso_switch_data switch_data[MAX_INSTANCE_COUNT];
55 
56 static struct lwm2m_engine_obj onoff_switch;
57 static struct lwm2m_engine_obj_field fields[] = {
58 	OBJ_FIELD_DATA(DIGITAL_INPUT_STATE_RID, R, BOOL),
59 	OBJ_FIELD_DATA(DIGITAL_INPUT_COUNTER_RID, R_OPT, S64),
60 	OBJ_FIELD_DATA(ON_TIME_RID, RW_OPT, S64),
61 	OBJ_FIELD_DATA(OFF_TIME_RID, RW_OPT, S64),
62 	OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
63 #if defined(CONFIG_LWM2M_IPSO_ONOFF_SWITCH_VERSION_1_1)
64 	OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
65 	OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
66 #endif
67 };
68 
69 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
70 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][SWITCH_MAX_ID];
71 static struct lwm2m_engine_res_inst
72 		res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
73 
get_switch_index(uint16_t obj_inst_id)74 static int get_switch_index(uint16_t obj_inst_id)
75 {
76 	int i, ret = -ENOENT;
77 
78 	for (i = 0; i < ARRAY_SIZE(inst); i++) {
79 		if (!inst[i].obj || inst[i].obj_inst_id != obj_inst_id) {
80 			continue;
81 		}
82 
83 		ret = i;
84 		break;
85 	}
86 
87 	return ret;
88 }
89 
state_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)90 static int state_post_write_cb(uint16_t obj_inst_id,
91 			       uint16_t res_id, uint16_t res_inst_id,
92 			       uint8_t *data, uint16_t data_len,
93 			       bool last_block, size_t total_size)
94 {
95 	int i;
96 
97 	i = get_switch_index(obj_inst_id);
98 	if (i < 0) {
99 		return i;
100 	}
101 
102 	if (switch_data[i].state) {
103 		/* reset off time */
104 		switch_data[i].off_time_sec = 0;
105 		if (!switch_data[i].last_state) {
106 			/* off to on transition */
107 			switch_data[i].counter++;
108 			if (switch_data[i].counter < 0) {
109 				switch_data[i].counter = 0;
110 			}
111 		}
112 	} else {
113 		/* reset on time */
114 		switch_data[i].on_time_sec = 0;
115 	}
116 
117 	switch_data[i].last_state = switch_data[i].state;
118 	switch_data[i].trigger_offset = k_uptime_get();
119 	return 0;
120 }
121 
on_time_read_cb(uint16_t obj_inst_id,uint16_t res_id,uint16_t res_inst_id,size_t * data_len)122 static void *on_time_read_cb(uint16_t obj_inst_id,
123 			     uint16_t res_id, uint16_t res_inst_id,
124 			     size_t *data_len)
125 {
126 	int i = get_switch_index(obj_inst_id);
127 
128 	if (i < 0) {
129 		return NULL;
130 	}
131 
132 	if (switch_data[i].state) {
133 		switch_data[i].on_time_sec =
134 			(int64_t)((k_uptime_get() - switch_data[i].trigger_offset) / 1000);
135 	}
136 
137 	*data_len = sizeof(switch_data[i].on_time_sec);
138 	return &switch_data[i].on_time_sec;
139 }
140 
off_time_read_cb(uint16_t obj_inst_id,uint16_t res_id,uint16_t res_inst_id,size_t * data_len)141 static void *off_time_read_cb(uint16_t obj_inst_id,
142 			      uint16_t res_id, uint16_t res_inst_id,
143 			      size_t *data_len)
144 {
145 	int i = get_switch_index(obj_inst_id);
146 
147 	if (i < 0) {
148 		return NULL;
149 	}
150 
151 	if (!switch_data[i].state) {
152 		switch_data[i].off_time_sec =
153 			(int64_t)((k_uptime_get() - switch_data[i].trigger_offset) / 1000);
154 	}
155 
156 	*data_len = sizeof(switch_data[i].off_time_sec);
157 	return &switch_data[i].off_time_sec;
158 }
159 
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)160 static int time_post_write_cb(uint16_t obj_inst_id,
161 			      uint16_t res_id, uint16_t res_inst_id,
162 			      uint8_t *data, uint16_t data_len,
163 			      bool last_block, size_t total_size)
164 {
165 	int i = get_switch_index(obj_inst_id);
166 
167 	if (i < 0) {
168 		return i;
169 	}
170 
171 	switch_data[i].trigger_offset = 0U;
172 	return 0;
173 }
174 
switch_create(uint16_t obj_inst_id)175 static struct lwm2m_engine_obj_inst *switch_create(uint16_t obj_inst_id)
176 {
177 	int index, avail = -1, i = 0, j = 0;
178 
179 	/* Check that there is no other instance with this ID */
180 	for (index = 0; index < ARRAY_SIZE(inst); index++) {
181 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
182 			LOG_ERR("Can not create instance - "
183 				"already existing: %u", obj_inst_id);
184 			return NULL;
185 		}
186 
187 		/* Save first available slot index */
188 		if (avail < 0 && !inst[index].obj) {
189 			avail = index;
190 		}
191 	}
192 
193 	if (avail < 0) {
194 		LOG_ERR("Can not create instance - no more room: %u",
195 			obj_inst_id);
196 		return NULL;
197 	}
198 
199 	/* Set default values */
200 	(void)memset(&switch_data[avail], 0, sizeof(switch_data[avail]));
201 	switch_data[avail].obj_inst_id = obj_inst_id;
202 
203 	(void)memset(res[avail], 0,
204 		     sizeof(res[avail][0]) * ARRAY_SIZE(res[avail]));
205 	init_res_instance(res_inst[avail], ARRAY_SIZE(res_inst[avail]));
206 
207 	/* initialize instance resource data */
208 	INIT_OBJ_RES(DIGITAL_INPUT_STATE_RID, res[avail], i, res_inst[avail],
209 		     j, 1, false, true, &switch_data[avail].state,
210 		     sizeof(switch_data[avail].state),
211 		     NULL, NULL, NULL, state_post_write_cb, NULL);
212 	INIT_OBJ_RES_DATA(DIGITAL_INPUT_COUNTER_RID, res[avail], i,
213 			  res_inst[avail], j, &switch_data[avail].counter,
214 			  sizeof(switch_data[avail].counter));
215 	INIT_OBJ_RES_OPT(ON_TIME_RID, res[avail], i,
216 			 res_inst[avail], j, 1, false, true,
217 			 on_time_read_cb, NULL, NULL, time_post_write_cb, NULL);
218 	INIT_OBJ_RES_OPT(OFF_TIME_RID, res[avail], i,
219 			 res_inst[avail], j, 1, false, true,
220 			 off_time_read_cb, NULL, NULL, time_post_write_cb, NULL);
221 	INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[avail], i,
222 			     res_inst[avail], j);
223 #if defined(CONFIG_LWM2M_IPSO_ONOFF_SWITCH_VERSION_1_1)
224 	INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[avail], i, res_inst[avail], j);
225 	INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[avail], i,
226 			     res_inst[avail], j);
227 #endif
228 
229 	inst[avail].resources = res[avail];
230 	inst[avail].resource_count = i;
231 
232 	LOG_DBG("Create IPSO On/Off Switch instance: %d", obj_inst_id);
233 
234 	return &inst[avail];
235 }
236 
ipso_switch_init(const struct device * dev)237 static int ipso_switch_init(const struct device *dev)
238 {
239 	onoff_switch.obj_id = IPSO_OBJECT_ONOFF_SWITCH_ID;
240 	onoff_switch.version_major = SWITCH_VERSION_MAJOR;
241 	onoff_switch.version_minor = SWITCH_VERSION_MINOR;
242 	onoff_switch.is_core = false;
243 	onoff_switch.fields = fields;
244 	onoff_switch.field_count = ARRAY_SIZE(fields);
245 	onoff_switch.max_instance_count = ARRAY_SIZE(inst);
246 	onoff_switch.create_cb = switch_create;
247 	lwm2m_register_obj(&onoff_switch);
248 
249 	return 0;
250 }
251 
252 SYS_INIT(ipso_switch_init, APPLICATION, CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
253