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 <zephyr/logging/log.h>
13 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
14 
15 #include <stdint.h>
16 #include <zephyr/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 double sensor_value[MAX_INSTANCE_COUNT];
49 static char units[MAX_INSTANCE_COUNT][UNIT_STR_MAX_SIZE];
50 static double min_measured_value[MAX_INSTANCE_COUNT];
51 static double max_measured_value[MAX_INSTANCE_COUNT];
52 static double min_range_value[MAX_INSTANCE_COUNT];
53 static double 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] = sensor_value[index];
82 	lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MIN_MEASURED_VALUE_RID);
83 }
84 
update_max_measured(uint16_t obj_inst_id,int index)85 static void update_max_measured(uint16_t obj_inst_id, int index)
86 {
87 	max_measured_value[index] = sensor_value[index];
88 	lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
89 }
90 
reset_min_max_measured_values_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)91 static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
92 					    uint8_t *args, uint16_t args_len)
93 {
94 	int i;
95 
96 	LOG_DBG("RESET MIN/MAX %d", obj_inst_id);
97 	for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
98 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
99 			update_min_measured(obj_inst_id, i);
100 			update_max_measured(obj_inst_id, i);
101 			return 0;
102 		}
103 	}
104 
105 	return -ENOENT;
106 }
107 
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,size_t offset)108 static int sensor_value_write_cb(uint16_t obj_inst_id, uint16_t res_id,
109 				 uint16_t res_inst_id, uint8_t *data,
110 				 uint16_t data_len, bool last_block,
111 				 size_t total_size, size_t offset)
112 {
113 	int i;
114 
115 	for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
116 		if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
117 			/* update min / max */
118 			if (sensor_value[i] < min_measured_value[i]) {
119 				update_min_measured(obj_inst_id, i);
120 			}
121 
122 			if (sensor_value[i] > max_measured_value[i]) {
123 				update_max_measured(obj_inst_id, i);
124 			}
125 		}
126 	}
127 
128 	return 0;
129 }
130 
131 static struct lwm2m_engine_obj_inst *
pressure_sensor_create(uint16_t obj_inst_id)132 pressure_sensor_create(uint16_t obj_inst_id)
133 {
134 	int index, i = 0, j = 0;
135 
136 	/* Check that there is no other instance with this ID */
137 	for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
138 		if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
139 			LOG_ERR("Can not create instance - "
140 				"already existing: %u",
141 				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, res_inst[index], j, 1,
172 		     false, true, &sensor_value[index], sizeof(*sensor_value),
173 		     NULL, NULL, NULL, sensor_value_write_cb, NULL);
174 	INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
175 			  units[index], UNIT_STR_MAX_SIZE, 0);
176 	INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i,
177 			  res_inst[index], j, &min_measured_value[index],
178 			  sizeof(*min_measured_value));
179 	INIT_OBJ_RES_DATA(MAX_MEASURED_VALUE_RID, res[index], i,
180 			  res_inst[index], j, &max_measured_value[index],
181 			  sizeof(*max_measured_value));
182 	INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[index], i, res_inst[index],
183 			  j, &min_range_value[index], sizeof(*min_range_value));
184 	INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[index], i, res_inst[index],
185 			  j, &max_range_value[index], sizeof(*max_range_value));
186 	INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID, res[index], i,
187 			     reset_min_max_measured_values_cb);
188 	INIT_OBJ_RES_OPTDATA(CURRENT_CALIBRATION_RID, res[index], i,
189 			     res_inst[index], j);
190 	INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[index], i,
191 			     res_inst[index], j);
192 #if defined(CONFIG_LWM2M_IPSO_PRESSURE_SENSOR_VERSION_1_1)
193 	INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j);
194 	INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[index], i,
195 			     res_inst[index], j);
196 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[index],
197 			     i, res_inst[index], j);
198 	INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[index], i,
199 			     res_inst[index], j);
200 #endif
201 
202 	inst[index].resources = res[index];
203 	inst[index].resource_count = i;
204 	LOG_DBG("Created IPSO %s Sensor instance: %d", SENSOR_NAME,
205 		obj_inst_id);
206 	return &inst[index];
207 }
208 
ipso_pressure_sensor_init(void)209 static int ipso_pressure_sensor_init(void)
210 {
211 	sensor.obj_id = IPSO_OBJECT_ID;
212 	sensor.version_major = PRESSURE_VERSION_MAJOR;
213 	sensor.version_minor = PRESSURE_VERSION_MINOR;
214 	sensor.is_core = false;
215 	sensor.fields = fields;
216 	sensor.field_count = ARRAY_SIZE(fields);
217 	sensor.max_instance_count = MAX_INSTANCE_COUNT;
218 	sensor.create_cb = pressure_sensor_create;
219 	lwm2m_register_obj(&sensor);
220 
221 	return 0;
222 }
223 
224 LWM2M_OBJ_INIT(ipso_pressure_sensor_init);
225