1 /*
2 * Copyright (c) 2021 Laird Connectivity
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 /*
8 * Filling sensor
9 * https://github.com/OpenMobileAlliance/lwm2m-registry/blob/prod/3435.xml
10 */
11
12 #define LOG_MODULE_NAME net_ipso_filling_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 #include "ipso_filling_sensor.h"
25
26 #define FILLING_VERSION_MAJOR 1
27 #define FILLING_VERSION_MINOR 0
28
29 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_FILLING_SENSOR_INSTANCE_COUNT
30
31 #define IPSO_OBJECT_ID IPSO_OBJECT_FILLING_LEVEL_SENSOR_ID
32
33 #define NUMBER_OF_OBJ_FIELDS 13
34
35 /*
36 * Calculate resource instances as follows:
37 * start with NUMBER_OF_OBJ_FIELDS
38 * subtract EXEC resources (1)
39 */
40 #define RESOURCE_INSTANCE_COUNT (NUMBER_OF_OBJ_FIELDS - 1)
41
42 /* resource state variables */
43 static uint32_t container_height[MAX_INSTANCE_COUNT]; /* cm */
44 static double actual_fill_percentage[MAX_INSTANCE_COUNT];
45 static uint32_t actual_fill_level[MAX_INSTANCE_COUNT]; /* cm */
46 static double high_threshold[MAX_INSTANCE_COUNT];
47 static bool container_full[MAX_INSTANCE_COUNT];
48 static double low_threshold[MAX_INSTANCE_COUNT];
49 static bool container_empty[MAX_INSTANCE_COUNT];
50 static double average_fill_speed[MAX_INSTANCE_COUNT];
51 static time_t forecast_full_date[MAX_INSTANCE_COUNT];
52 static time_t forecast_empty_date[MAX_INSTANCE_COUNT];
53 static bool container_out_of_location[MAX_INSTANCE_COUNT];
54 static bool container_out_of_position[MAX_INSTANCE_COUNT];
55
56 static struct lwm2m_engine_obj fill_sensor;
57 static struct lwm2m_engine_obj_field fields[] = {
58 OBJ_FIELD_DATA(CONTAINER_HEIGHT_FILLING_SENSOR_RID, RW, U32),
59 OBJ_FIELD_DATA(ACTUAL_FILL_PERCENTAGE_FILLING_SENSOR_RID, R_OPT,
60 FLOAT),
61 OBJ_FIELD_DATA(ACTUAL_FILL_LEVEL_FILLING_SENSOR_RID, R_OPT, U32),
62 OBJ_FIELD_DATA(HIGH_THRESHOLD_PERCENTAGE_FILLING_SENSOR_RID, RW_OPT,
63 FLOAT),
64 OBJ_FIELD_DATA(CONTAINER_FULL_FILLING_SENSOR_RID, R, BOOL),
65 OBJ_FIELD_DATA(LOW_THRESHOLD_PERCENTAGE_FILLING_SENSOR_RID, RW_OPT,
66 FLOAT),
67 OBJ_FIELD_DATA(CONTAINER_EMPTY_FILLING_SENSOR_RID, R, BOOL),
68 OBJ_FIELD_DATA(AVERAGE_FILL_SPEED_FILLING_SENSOR_RID, R_OPT, FLOAT),
69 OBJ_FIELD_EXECUTE_OPT(RESET_AVERAGE_FILL_SPEED_FILLING_SENSOR_RID),
70 OBJ_FIELD_DATA(FORECAST_FULL_DATE_FILLING_SENSOR_RID, R_OPT, TIME),
71 OBJ_FIELD_DATA(FORECAST_EMPTY_DATE_FILLING_SENSOR_RID, R_OPT, TIME),
72 OBJ_FIELD_DATA(CONTAINER_OUT_OF_LOCATION_FILLING_SENSOR_RID, R_OPT,
73 BOOL),
74 OBJ_FIELD_DATA(CONTAINER_OUT_OF_POSITION_FILLING_SENSOR_RID, R_OPT,
75 BOOL),
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
reset_average_fill_speed_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)83 static int reset_average_fill_speed_cb(uint16_t obj_inst_id, uint8_t *args,
84 uint16_t args_len)
85 {
86 int i;
87
88 LOG_DBG("Reset Average Fill Speed %d", obj_inst_id);
89 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
90 if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
91 average_fill_speed[i] = 0;
92 return 0;
93 }
94 }
95
96 return -ENOENT;
97 }
98
99 /* Update empty/full when fill percentage or thresholds change.
100 * If value changes, then notify.
101 */
update(uint16_t obj_inst_id,uint16_t res_id,int index)102 static void update(uint16_t obj_inst_id, uint16_t res_id, int index)
103 {
104 bool full;
105 bool empty;
106
107 full = actual_fill_percentage[index] > high_threshold[index];
108 if (container_full[index] != full) {
109 container_full[index] = full;
110 lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id,
111 CONTAINER_FULL_FILLING_SENSOR_RID);
112 }
113
114 empty = actual_fill_percentage[index] < low_threshold[index];
115 if (container_empty[index] != empty) {
116 container_empty[index] = empty;
117 lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id,
118 CONTAINER_EMPTY_FILLING_SENSOR_RID);
119 }
120 }
121
update_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)122 static int update_cb(uint16_t obj_inst_id, uint16_t res_id,
123 uint16_t res_inst_id, uint8_t *data, uint16_t data_len,
124 bool last_block, 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(obj_inst_id, res_id, i);
131 break;
132 }
133 }
134 return 0;
135 }
136
filling_sensor_create(uint16_t obj_inst_id)137 static struct lwm2m_engine_obj_inst *filling_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 container_height[index] = 0;
165 actual_fill_percentage[index] = 0;
166 actual_fill_level[index] = 0;
167 high_threshold[index] = 0;
168 container_full[index] = false;
169 low_threshold[index] = 0;
170 container_empty[index] = false;
171 average_fill_speed[index] = 0;
172 forecast_full_date[index] = 0;
173 forecast_empty_date[index] = 0;
174 container_out_of_location[index] = false;
175 container_out_of_position[index] = false;
176
177 (void)memset(res[index], 0,
178 sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
179 init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
180
181 /* initialize instance resource data */
182 INIT_OBJ_RES(CONTAINER_HEIGHT_FILLING_SENSOR_RID, res[index], i,
183 res_inst[index], j, 1, false, true,
184 &container_height[index], sizeof(*container_height), NULL,
185 NULL, NULL, update_cb, NULL);
186 INIT_OBJ_RES(ACTUAL_FILL_PERCENTAGE_FILLING_SENSOR_RID, res[index], i,
187 res_inst[index], j, 1, false, true,
188 &actual_fill_percentage[index], sizeof(*actual_fill_percentage),
189 NULL, NULL, NULL, update_cb, NULL);
190 INIT_OBJ_RES_DATA(ACTUAL_FILL_LEVEL_FILLING_SENSOR_RID, res[index],
191 i, res_inst[index], j, &actual_fill_level[index],
192 sizeof(*actual_fill_level));
193 INIT_OBJ_RES(HIGH_THRESHOLD_PERCENTAGE_FILLING_SENSOR_RID, res[index],
194 i, res_inst[index], j, 1, false, true,
195 &high_threshold[index], sizeof(*high_threshold), NULL,
196 NULL, NULL, update_cb, NULL);
197 INIT_OBJ_RES_DATA(CONTAINER_FULL_FILLING_SENSOR_RID, res[index], i,
198 res_inst[index], j, &container_full[index],
199 sizeof(*container_full));
200 INIT_OBJ_RES(LOW_THRESHOLD_PERCENTAGE_FILLING_SENSOR_RID, res[index], i,
201 res_inst[index], j, 1, false, true, &low_threshold[index],
202 sizeof(*low_threshold), NULL, NULL, NULL, update_cb, NULL);
203 INIT_OBJ_RES_DATA(CONTAINER_EMPTY_FILLING_SENSOR_RID, res[index], i,
204 res_inst[index], j, &container_empty[index],
205 sizeof(*container_empty));
206 INIT_OBJ_RES_DATA(AVERAGE_FILL_SPEED_FILLING_SENSOR_RID, res[index], i,
207 res_inst[index], j, &average_fill_speed[index],
208 sizeof(*average_fill_speed));
209 INIT_OBJ_RES_EXECUTE(RESET_AVERAGE_FILL_SPEED_FILLING_SENSOR_RID,
210 res[index], i, reset_average_fill_speed_cb);
211 INIT_OBJ_RES_DATA(FORECAST_FULL_DATE_FILLING_SENSOR_RID, res[index], i,
212 res_inst[index], j, &forecast_full_date[index],
213 sizeof(*forecast_full_date));
214 INIT_OBJ_RES_DATA(FORECAST_EMPTY_DATE_FILLING_SENSOR_RID, res[index], i,
215 res_inst[index], j, &forecast_empty_date[index],
216 sizeof(*forecast_empty_date));
217 INIT_OBJ_RES_DATA(CONTAINER_OUT_OF_LOCATION_FILLING_SENSOR_RID,
218 res[index], i, res_inst[index], j,
219 &container_out_of_location[index],
220 sizeof(*container_out_of_location));
221 INIT_OBJ_RES_DATA(CONTAINER_OUT_OF_POSITION_FILLING_SENSOR_RID,
222 res[index], i, res_inst[index], j,
223 &container_out_of_position[index],
224 sizeof(*container_out_of_position));
225 inst[index].resources = res[index];
226 inst[index].resource_count = i;
227 LOG_DBG("Created IPSO Filling Sensor instance: %d", obj_inst_id);
228 return &inst[index];
229 }
230
fill_sensor_init(void)231 static int fill_sensor_init(void)
232 {
233 fill_sensor.obj_id = IPSO_OBJECT_ID;
234 fill_sensor.version_major = FILLING_VERSION_MAJOR;
235 fill_sensor.version_minor = FILLING_VERSION_MINOR;
236 fill_sensor.is_core = false;
237 fill_sensor.fields = fields;
238 fill_sensor.field_count = ARRAY_SIZE(fields);
239 fill_sensor.max_instance_count = MAX_INSTANCE_COUNT;
240 fill_sensor.create_cb = filling_sensor_create;
241 lwm2m_register_obj(&fill_sensor);
242
243 return 0;
244 }
245
246 LWM2M_OBJ_INIT(fill_sensor_init);
247