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