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 /*
10 * Generic sensor is based off temperature sensor.
11 * It is a template used to create other sensors.
12 */
13
14 #define LOG_MODULE_NAME net_ipso_generic_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 GENERIC_VERSION_MAJOR 1
28
29 #if defined(CONFIG_LWM2M_IPSO_GENERIC_SENSOR_VERSION_1_1)
30 #define GENERIC_VERSION_MINOR 1
31 #define NUMBER_OF_OBJ_FIELDS 13
32 #else
33 #define GENERIC_VERSION_MINOR 0
34 #define NUMBER_OF_OBJ_FIELDS 9
35 #endif /* defined(CONFIG_LWM2M_IPSO_GENERIC_SENSOR_VERSION_1_1) */
36
37 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_GENERIC_SENSOR_INSTANCE_COUNT
38
39 #define IPSO_OBJECT_ID IPSO_OBJECT_GENERIC_SENSOR_ID
40
41 #define SENSOR_NAME CONFIG_LWM2M_IPSO_GENERIC_SENSOR_NAME
42
43 #define UNIT_STR_MAX_SIZE 8
44 #define APP_TYPE_STR_MAX_SIZE 32
45 #define SENSOR_TYPE_STR_MAX_SIZE 32
46
47 BUILD_ASSERT(SENSOR_TYPE_STR_MAX_SIZE >=
48 sizeof(CONFIG_LWM2M_IPSO_GENERIC_SENSOR_TYPE),
49 "CONFIG_LWM2M_IPSO_GENERIC_SENSOR_TYPE is too long.");
50
51 /*
52 * Calculate resource instances as follows:
53 * start with NUMBER_OF_OBJ_FIELDS
54 * subtract EXEC resources (1)
55 */
56 #define RESOURCE_INSTANCE_COUNT (NUMBER_OF_OBJ_FIELDS - 1)
57
58 /* resource state variables */
59 static double sensor_value[MAX_INSTANCE_COUNT];
60 static char units[MAX_INSTANCE_COUNT][UNIT_STR_MAX_SIZE];
61 static double min_measured_value[MAX_INSTANCE_COUNT];
62 static double max_measured_value[MAX_INSTANCE_COUNT];
63 static double min_range_value[MAX_INSTANCE_COUNT];
64 static double max_range_value[MAX_INSTANCE_COUNT];
65 static char app_type[MAX_INSTANCE_COUNT][APP_TYPE_STR_MAX_SIZE];
66 static char sensor_type[MAX_INSTANCE_COUNT][SENSOR_TYPE_STR_MAX_SIZE];
67
68 static struct lwm2m_engine_obj sensor;
69 static struct lwm2m_engine_obj_field fields[] = {
70 OBJ_FIELD_DATA(SENSOR_VALUE_RID, R, FLOAT),
71 OBJ_FIELD_DATA(SENSOR_UNITS_RID, R_OPT, STRING),
72 OBJ_FIELD_DATA(MIN_MEASURED_VALUE_RID, R_OPT, FLOAT),
73 OBJ_FIELD_DATA(MAX_MEASURED_VALUE_RID, R_OPT, FLOAT),
74 OBJ_FIELD_DATA(MIN_RANGE_VALUE_RID, R_OPT, FLOAT),
75 OBJ_FIELD_DATA(MAX_RANGE_VALUE_RID, R_OPT, FLOAT),
76 OBJ_FIELD_EXECUTE_OPT(RESET_MIN_MAX_MEASURED_VALUES_RID),
77 OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
78 OBJ_FIELD_DATA(SENSOR_TYPE_RID, R_OPT, STRING),
79 #if defined(CONFIG_LWM2M_IPSO_GENERIC_SENSOR_VERSION_1_1)
80 OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
81 OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
82 OBJ_FIELD_DATA(MEASUREMENT_QUALITY_INDICATOR_RID, R_OPT, U8),
83 OBJ_FIELD_DATA(MEASUREMENT_QUALITY_LEVEL_RID, R_OPT, U8),
84 #endif
85 };
86
87 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
88 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][NUMBER_OF_OBJ_FIELDS];
89 static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT]
90 [RESOURCE_INSTANCE_COUNT];
91
update_min_measured(uint16_t obj_inst_id,int index)92 static void update_min_measured(uint16_t obj_inst_id, int index)
93 {
94 min_measured_value[index] = sensor_value[index];
95 lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MIN_MEASURED_VALUE_RID);
96 }
97
update_max_measured(uint16_t obj_inst_id,int index)98 static void update_max_measured(uint16_t obj_inst_id, int index)
99 {
100 max_measured_value[index] = sensor_value[index];
101 lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
102 }
103
reset_min_max_measured_values_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)104 static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
105 uint8_t *args, uint16_t args_len)
106 {
107 int i;
108
109 LOG_DBG("RESET MIN/MAX %d", obj_inst_id);
110 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
111 if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
112 update_min_measured(obj_inst_id, i);
113 update_max_measured(obj_inst_id, i);
114 return 0;
115 }
116 }
117
118 return -ENOENT;
119 }
120
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)121 static int sensor_value_write_cb(uint16_t obj_inst_id, uint16_t res_id,
122 uint16_t res_inst_id, uint8_t *data,
123 uint16_t data_len, bool last_block,
124 size_t total_size)
125 {
126 int i;
127
128 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
129 if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
130 /* update min / max */
131 if (sensor_value[i] < min_measured_value[i]) {
132 update_min_measured(obj_inst_id, i);
133 }
134
135 if (sensor_value[i] > max_measured_value[i]) {
136 update_max_measured(obj_inst_id, i);
137 }
138 }
139 }
140
141 return 0;
142 }
143
generic_sensor_create(uint16_t obj_inst_id)144 static struct lwm2m_engine_obj_inst *generic_sensor_create(uint16_t obj_inst_id)
145 {
146 int index, i = 0, j = 0;
147
148 /* Check that there is no other instance with this ID */
149 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
150 if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
151 LOG_ERR("Can not create instance - "
152 "already existing: %u",
153 obj_inst_id);
154 return NULL;
155 }
156 }
157
158 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
159 if (!inst[index].obj) {
160 break;
161 }
162 }
163
164 if (index >= MAX_INSTANCE_COUNT) {
165 LOG_ERR("Can not create instance - no more room: %u",
166 obj_inst_id);
167 return NULL;
168 }
169
170 /* Set default values */
171 sensor_value[index] = 0;
172 units[index][0] = '\0';
173 min_measured_value[index] = INT32_MAX;
174 max_measured_value[index] = -INT32_MAX;
175 min_range_value[index] = 0;
176 max_range_value[index] = 0;
177 app_type[index][0] = '\0';
178 strncpy(sensor_type[index], CONFIG_LWM2M_IPSO_GENERIC_SENSOR_TYPE,
179 SENSOR_TYPE_STR_MAX_SIZE);
180
181 (void)memset(res[index], 0,
182 sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
183 init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
184
185 /* initialize instance resource data */
186 INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1,
187 false, true, &sensor_value[index], sizeof(*sensor_value),
188 NULL, NULL, NULL, sensor_value_write_cb, NULL);
189 INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
190 units[index], UNIT_STR_MAX_SIZE);
191 INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i,
192 res_inst[index], j, &min_measured_value[index],
193 sizeof(*min_measured_value));
194 INIT_OBJ_RES_DATA(MAX_MEASURED_VALUE_RID, res[index], i,
195 res_inst[index], j, &max_measured_value[index],
196 sizeof(*max_measured_value));
197 INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[index], i, res_inst[index],
198 j, &min_range_value[index], sizeof(*min_range_value));
199 INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[index], i, res_inst[index],
200 j, &max_range_value[index], sizeof(*max_range_value));
201 INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID, res[index], i,
202 reset_min_max_measured_values_cb);
203 INIT_OBJ_RES_DATA(APPLICATION_TYPE_RID, res[index], i, res_inst[index],
204 j, app_type[index], APP_TYPE_STR_MAX_SIZE);
205 INIT_OBJ_RES_DATA(SENSOR_TYPE_RID, res[index], i, res_inst[index], j,
206 sensor_type[index], SENSOR_TYPE_STR_MAX_SIZE);
207
208 #if defined(CONFIG_LWM2M_IPSO_GENERIC_SENSOR_VERSION_1_1)
209 INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j);
210 INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[index], i,
211 res_inst[index], j);
212 INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[index], i,
213 res_inst[index], j);
214 INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[index], i,
215 res_inst[index], j);
216 #endif
217
218 inst[index].resources = res[index];
219 inst[index].resource_count = i;
220 LOG_DBG("Created IPSO %s Sensor instance: %d", SENSOR_NAME,
221 obj_inst_id);
222 return &inst[index];
223 }
224
ipso_generic_sensor_init(void)225 static int ipso_generic_sensor_init(void)
226 {
227 sensor.obj_id = IPSO_OBJECT_ID;
228 sensor.version_major = GENERIC_VERSION_MAJOR;
229 sensor.version_minor = GENERIC_VERSION_MINOR;
230 sensor.is_core = false;
231 sensor.fields = fields;
232 sensor.field_count = ARRAY_SIZE(fields);
233 sensor.max_instance_count = MAX_INSTANCE_COUNT;
234 sensor.create_cb = generic_sensor_create;
235 lwm2m_register_obj(&sensor);
236
237 return 0;
238 }
239
240 SYS_INIT(ipso_generic_sensor_init, APPLICATION,
241 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
242