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 /*
9  * Source material for IPSO Temperature Sensor object (3303):
10  * https://github.com/IPSO-Alliance/pub/blob/master/docs/IPSO-Smart-Objects.pdf
11  * Section: "10. IPSO Object: Temperature"
12  */
13 
14 #define LOG_MODULE_NAME net_ipso_temp_sensor
15 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
16 
17 #include <zephyr/logging/log.h>
18 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
19 
20 #include <stdint.h>
21 #include <zephyr/init.h>
22 
23 #include "lwm2m_object.h"
24 #include "lwm2m_engine.h"
25 #include "lwm2m_resource_ids.h"
26 
27 #define TEMP_VERSION_MAJOR 1
28 
29 #if defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1)
30 #define TEMP_VERSION_MINOR 1
31 #define TEMP_MAX_ID 12
32 #else
33 #define TEMP_VERSION_MINOR 0
34 #define TEMP_MAX_ID 7
35 #endif /* defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1) */
36 
37 #define MAX_INSTANCE_COUNT	CONFIG_LWM2M_IPSO_TEMP_SENSOR_INSTANCE_COUNT
38 
39 #define UNIT_STR_MAX_SIZE	8
40 
41 /*
42  * Calculate resource instances as follows:
43  * start with TEMP_MAX_ID
44  * subtract EXEC resources (1)
45  */
46 #define RESOURCE_INSTANCE_COUNT	(TEMP_MAX_ID - 1)
47 
48 /* resource state variables */
49 static double sensor_value[MAX_INSTANCE_COUNT];
50 static char units[MAX_INSTANCE_COUNT][UNIT_STR_MAX_SIZE];
51 static double min_measured_value[MAX_INSTANCE_COUNT];
52 static double max_measured_value[MAX_INSTANCE_COUNT];
53 static double min_range_value[MAX_INSTANCE_COUNT];
54 static double max_range_value[MAX_INSTANCE_COUNT];
55 
56 static struct lwm2m_engine_obj temp_sensor;
57 static struct lwm2m_engine_obj_field fields[] = {
58 	OBJ_FIELD_DATA(SENSOR_VALUE_RID, R, FLOAT),
59 	OBJ_FIELD_DATA(SENSOR_UNITS_RID, R_OPT, STRING),
60 	OBJ_FIELD_DATA(MIN_MEASURED_VALUE_RID, R_OPT, FLOAT),
61 	OBJ_FIELD_DATA(MAX_MEASURED_VALUE_RID, R_OPT, FLOAT),
62 	OBJ_FIELD_DATA(MIN_RANGE_VALUE_RID, R_OPT, FLOAT),
63 	OBJ_FIELD_DATA(MAX_RANGE_VALUE_RID, R_OPT, FLOAT),
64 	OBJ_FIELD_EXECUTE_OPT(RESET_MIN_MAX_MEASURED_VALUES_RID),
65 #if defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1)
66 	OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
67 	OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
68 	OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
69 	OBJ_FIELD_DATA(MEASUREMENT_QUALITY_INDICATOR_RID, R_OPT, U8),
70 	OBJ_FIELD_DATA(MEASUREMENT_QUALITY_LEVEL_RID, R_OPT, U8),
71 #endif
72 };
73 
74 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
75 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][TEMP_MAX_ID];
76 static struct lwm2m_engine_res_inst
77 		res_inst[MAX_INSTANCE_COUNT][RESOURCE_INSTANCE_COUNT];
78 
update_min_measured(uint16_t obj_inst_id,int index)79 static void update_min_measured(uint16_t obj_inst_id, int index)
80 {
81 	min_measured_value[index] = sensor_value[index];
82 	lwm2m_notify_observer(IPSO_OBJECT_TEMP_SENSOR_ID, obj_inst_id,
83 			MIN_MEASURED_VALUE_RID);
84 }
85 
update_max_measured(uint16_t obj_inst_id,int index)86 static void update_max_measured(uint16_t obj_inst_id, int index)
87 {
88 	max_measured_value[index] = sensor_value[index];
89 	lwm2m_notify_observer(IPSO_OBJECT_TEMP_SENSOR_ID, obj_inst_id,
90 			MAX_MEASURED_VALUE_RID);
91 }
92 
reset_min_max_measured_values_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)93 static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
94 					    uint8_t *args, uint16_t args_len)
95 {
96 	int i;
97 
98 	LOG_DBG("RESET MIN/MAX %d", obj_inst_id);
99 	for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
100 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
101 			update_min_measured(obj_inst_id, i);
102 			update_max_measured(obj_inst_id, i);
103 			return 0;
104 		}
105 	}
106 
107 	return -ENOENT;
108 }
109 
sensor_value_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)110 static int sensor_value_write_cb(uint16_t obj_inst_id,
111 				 uint16_t res_id, uint16_t res_inst_id,
112 				 uint8_t *data, uint16_t data_len,
113 				 bool last_block, size_t total_size)
114 {
115 	int i;
116 
117 	for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
118 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
119 			/* update min / max */
120 			if (sensor_value[i] < min_measured_value[i]) {
121 				update_min_measured(obj_inst_id, i);
122 			}
123 
124 			if (sensor_value[i] > max_measured_value[i]) {
125 				update_max_measured(obj_inst_id, i);
126 			}
127 		}
128 	}
129 
130 	return 0;
131 }
132 
temp_sensor_create(uint16_t obj_inst_id)133 static struct lwm2m_engine_obj_inst *temp_sensor_create(uint16_t obj_inst_id)
134 {
135 	int index, i = 0, j = 0;
136 
137 	/* Check that there is no other instance with this ID */
138 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
139 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
140 			LOG_ERR("Can not create instance - "
141 				"already existing: %u", obj_inst_id);
142 			return NULL;
143 		}
144 	}
145 
146 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
147 		if (!inst[index].obj) {
148 			break;
149 		}
150 	}
151 
152 	if (index >= MAX_INSTANCE_COUNT) {
153 		LOG_ERR("Can not create instance - no more room: %u",
154 			obj_inst_id);
155 		return NULL;
156 	}
157 
158 	/* Set default values */
159 	sensor_value[index] = 0;
160 	units[index][0] = '\0';
161 	min_measured_value[index] = INT32_MAX;
162 	max_measured_value[index] = -INT32_MAX;
163 	min_range_value[index] = 0;
164 	max_range_value[index] = 0;
165 
166 	(void)memset(res[index], 0,
167 		     sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
168 	init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
169 
170 	/* initialize instance resource data */
171 	INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i,
172 		     res_inst[index], j, 1, false, true,
173 		     &sensor_value[index], sizeof(*sensor_value),
174 		     NULL, NULL, NULL, sensor_value_write_cb, NULL);
175 	INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
176 			  units[index], UNIT_STR_MAX_SIZE, 0);
177 	INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i,
178 			  res_inst[index], j, &min_measured_value[index],
179 			  sizeof(*min_measured_value));
180 	INIT_OBJ_RES_DATA(MAX_MEASURED_VALUE_RID, res[index], i,
181 			  res_inst[index], j, &max_measured_value[index],
182 			  sizeof(*max_measured_value));
183 	INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[index], i,
184 			  res_inst[index], j, &min_range_value[index],
185 			  sizeof(*min_range_value));
186 	INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[index], i,
187 			  res_inst[index], j, &max_range_value[index],
188 			  sizeof(*max_range_value));
189 	INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID,
190 			     res[index], i, reset_min_max_measured_values_cb);
191 #if defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1)
192 	INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[index], i,
193 			     res_inst[index], j);
194 	INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j);
195 	INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[index], i,
196 			     res_inst[index], j);
197 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[index],
198 			     i, res_inst[index], j);
199 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[index], i,
200 			     res_inst[index], j);
201 #endif
202 
203 	inst[index].resources = res[index];
204 	inst[index].resource_count = i;
205 	LOG_DBG("Create IPSO Temperature Sensor instance: %d", obj_inst_id);
206 	return &inst[index];
207 }
208 
ipso_temp_sensor_init(void)209 static int ipso_temp_sensor_init(void)
210 {
211 	temp_sensor.obj_id = IPSO_OBJECT_TEMP_SENSOR_ID;
212 	temp_sensor.version_major = TEMP_VERSION_MAJOR;
213 	temp_sensor.version_minor = TEMP_VERSION_MINOR;
214 	temp_sensor.is_core = false;
215 	temp_sensor.fields = fields;
216 	temp_sensor.field_count = ARRAY_SIZE(fields);
217 	temp_sensor.max_instance_count = MAX_INSTANCE_COUNT;
218 	temp_sensor.create_cb = temp_sensor_create;
219 	lwm2m_register_obj(&temp_sensor);
220 
221 	return 0;
222 }
223 
224 LWM2M_OBJ_INIT(ipso_temp_sensor_init);
225