1 /*
2  * Copyright (c) 2017 Linaro Limited
3  * Copyright (c) 2018-2019 Foundries.io
4  * Copyright (c) 2020 Laird Connectivity
5  *
6  * SPDX-License-Identifier: Apache-2.0
7  */
8 
9 #define LOG_MODULE_NAME net_ipso_pressure_sensor
10 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
11 
12 #include <logging/log.h>
13 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
14 
15 #include <stdint.h>
16 #include <init.h>
17 
18 #include "lwm2m_object.h"
19 #include "lwm2m_engine.h"
20 #include "lwm2m_resource_ids.h"
21 
22 #define PRESSURE_VERSION_MAJOR 1
23 
24 #if defined(CONFIG_LWM2M_IPSO_PRESSURE_SENSOR_VERSION_1_1)
25 #define PRESSURE_VERSION_MINOR 1
26 #define NUMBER_OF_OBJ_FIELDS 13
27 #else
28 #define PRESSURE_VERSION_MINOR 0
29 #define NUMBER_OF_OBJ_FIELDS 9
30 #endif /* defined(CONFIG_LWM2M_IPSO_PRESSURE_SENSOR_VERSION_1_1) */
31 
32 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_PRESSURE_SENSOR_INSTANCE_COUNT
33 
34 #define IPSO_OBJECT_ID IPSO_OBJECT_PRESSURE_ID
35 
36 #define SENSOR_NAME "Pressure"
37 
38 #define UNIT_STR_MAX_SIZE 8
39 
40 /*
41  * Calculate resource instances as follows:
42  * start with NUMBER_OF_OBJ_FIELDS
43  * subtract EXEC resources (1)
44  */
45 #define RESOURCE_INSTANCE_COUNT (NUMBER_OF_OBJ_FIELDS - 1)
46 
47 /* resource state variables */
48 static float32_value_t sensor_value[MAX_INSTANCE_COUNT];
49 static char units[MAX_INSTANCE_COUNT][UNIT_STR_MAX_SIZE];
50 static float32_value_t min_measured_value[MAX_INSTANCE_COUNT];
51 static float32_value_t max_measured_value[MAX_INSTANCE_COUNT];
52 static float32_value_t min_range_value[MAX_INSTANCE_COUNT];
53 static float32_value_t max_range_value[MAX_INSTANCE_COUNT];
54 
55 static struct lwm2m_engine_obj sensor;
56 static struct lwm2m_engine_obj_field fields[] = {
57 	OBJ_FIELD_DATA(SENSOR_VALUE_RID, R, FLOAT),
58 	OBJ_FIELD_DATA(SENSOR_UNITS_RID, R_OPT, STRING),
59 	OBJ_FIELD_DATA(MIN_MEASURED_VALUE_RID, R_OPT, FLOAT),
60 	OBJ_FIELD_DATA(MAX_MEASURED_VALUE_RID, R_OPT, FLOAT),
61 	OBJ_FIELD_DATA(MIN_RANGE_VALUE_RID, R_OPT, FLOAT),
62 	OBJ_FIELD_DATA(MAX_RANGE_VALUE_RID, R_OPT, FLOAT),
63 	OBJ_FIELD_EXECUTE_OPT(RESET_MIN_MAX_MEASURED_VALUES_RID),
64 	OBJ_FIELD_DATA(CURRENT_CALIBRATION_RID, R_OPT, FLOAT),
65 	OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
66 #if defined(CONFIG_LWM2M_IPSO_PRESSURE_SENSOR_VERSION_1_1)
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][NUMBER_OF_OBJ_FIELDS];
76 static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT]
77 					    [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].val1 = sensor_value[index].val1;
82 	min_measured_value[index].val2 = sensor_value[index].val2;
83 	NOTIFY_OBSERVER(IPSO_OBJECT_ID, obj_inst_id, 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].val1 = sensor_value[index].val1;
89 	max_measured_value[index].val2 = sensor_value[index].val2;
90 	NOTIFY_OBSERVER(IPSO_OBJECT_ID, obj_inst_id, 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, uint16_t res_id,
111 				 uint16_t res_inst_id, uint8_t *data,
112 				 uint16_t data_len, bool last_block,
113 				 size_t total_size)
114 {
115 	int i;
116 	bool update_min = false;
117 	bool update_max = false;
118 
119 	for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
120 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
121 			/* update min / max */
122 			if (sensor_value[i].val1 < min_measured_value[i].val1) {
123 				update_min = true;
124 			} else if (sensor_value[i].val1 ==
125 					   min_measured_value[i].val1 &&
126 				   sensor_value[i].val2 <
127 					   min_measured_value[i].val2) {
128 				update_min = true;
129 			}
130 
131 			if (sensor_value[i].val1 > max_measured_value[i].val1) {
132 				update_max = true;
133 			} else if (sensor_value[i].val1 ==
134 					   max_measured_value[i].val1 &&
135 				   sensor_value[i].val2 >
136 					   max_measured_value[i].val2) {
137 				update_max = true;
138 			}
139 
140 			if (update_min) {
141 				update_min_measured(obj_inst_id, i);
142 			}
143 
144 			if (update_max) {
145 				update_max_measured(obj_inst_id, i);
146 			}
147 		}
148 	}
149 
150 	return 0;
151 }
152 
153 static struct lwm2m_engine_obj_inst *
pressure_sensor_create(uint16_t obj_inst_id)154 pressure_sensor_create(uint16_t obj_inst_id)
155 {
156 	int index, i = 0, j = 0;
157 
158 	/* Check that there is no other instance with this ID */
159 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
160 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
161 			LOG_ERR("Can not create instance - "
162 				"already existing: %u",
163 				obj_inst_id);
164 			return NULL;
165 		}
166 	}
167 
168 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
169 		if (!inst[index].obj) {
170 			break;
171 		}
172 	}
173 
174 	if (index >= MAX_INSTANCE_COUNT) {
175 		LOG_ERR("Can not create instance - no more room: %u",
176 			obj_inst_id);
177 		return NULL;
178 	}
179 
180 	/* Set default values */
181 	sensor_value[index].val1 = 0;
182 	sensor_value[index].val2 = 0;
183 	units[index][0] = '\0';
184 	min_measured_value[index].val1 = INT32_MAX;
185 	min_measured_value[index].val2 = 0;
186 	max_measured_value[index].val1 = -INT32_MAX;
187 	max_measured_value[index].val2 = 0;
188 	min_range_value[index].val1 = 0;
189 	min_range_value[index].val2 = 0;
190 	max_range_value[index].val1 = 0;
191 	max_range_value[index].val2 = 0;
192 
193 	(void)memset(res[index], 0,
194 		     sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
195 	init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
196 
197 	/* initialize instance resource data */
198 	INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1,
199 		     false, true, &sensor_value[index], sizeof(*sensor_value),
200 		     NULL, NULL, NULL, sensor_value_write_cb, NULL);
201 	INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
202 			  units[index], UNIT_STR_MAX_SIZE);
203 	INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i,
204 			  res_inst[index], j, &min_measured_value[index],
205 			  sizeof(*min_measured_value));
206 	INIT_OBJ_RES_DATA(MAX_MEASURED_VALUE_RID, res[index], i,
207 			  res_inst[index], j, &max_measured_value[index],
208 			  sizeof(*max_measured_value));
209 	INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[index], i, res_inst[index],
210 			  j, &min_range_value[index], sizeof(*min_range_value));
211 	INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[index], i, res_inst[index],
212 			  j, &max_range_value[index], sizeof(*max_range_value));
213 	INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID, res[index], i,
214 			     reset_min_max_measured_values_cb);
215 	INIT_OBJ_RES_OPTDATA(CURRENT_CALIBRATION_RID, res[index], i,
216 			     res_inst[index], j);
217 	INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[index], i,
218 			     res_inst[index], j);
219 #if defined(CONFIG_LWM2M_IPSO_PRESSURE_SENSOR_VERSION_1_1)
220 	INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j);
221 	INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[index], i,
222 			     res_inst[index], j);
223 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[index],
224 			     i, res_inst[index], j);
225 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[index], i,
226 			     res_inst[index], j);
227 #endif
228 
229 	inst[index].resources = res[index];
230 	inst[index].resource_count = i;
231 	LOG_DBG("Created IPSO %s Sensor instance: %d", SENSOR_NAME,
232 		obj_inst_id);
233 	return &inst[index];
234 }
235 
ipso_pressure_sensor_init(const struct device * dev)236 static int ipso_pressure_sensor_init(const struct device *dev)
237 {
238 	sensor.obj_id = IPSO_OBJECT_ID;
239 	sensor.version_major = PRESSURE_VERSION_MAJOR;
240 	sensor.version_minor = PRESSURE_VERSION_MINOR;
241 	sensor.is_core = false;
242 	sensor.fields = fields;
243 	sensor.field_count = ARRAY_SIZE(fields);
244 	sensor.max_instance_count = MAX_INSTANCE_COUNT;
245 	sensor.create_cb = pressure_sensor_create;
246 	lwm2m_register_obj(&sensor);
247 
248 	return 0;
249 }
250 
251 SYS_INIT(ipso_pressure_sensor_init, APPLICATION,
252 	 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
253