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_humidity_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 HUMIDITY_VERSION_MAJOR 1
23
24 #if defined(CONFIG_LWM2M_IPSO_HUMIDITY_SENSOR_VERSION_1_1)
25 #define HUMIDITY_VERSION_MINOR 1
26 #define NUMBER_OF_OBJ_FIELDS 12
27 #else
28 #define HUMIDITY_VERSION_MINOR 0
29 #define NUMBER_OF_OBJ_FIELDS 7
30 #endif /* defined(CONFIG_LWM2M_IPSO_HUMIDITY_SENSOR_VERSION_1_1) */
31
32 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_HUMIDITY_SENSOR_INSTANCE_COUNT
33
34 #define IPSO_OBJECT_ID IPSO_OBJECT_HUMIDITY_SENSOR_ID
35
36 #define SENSOR_NAME "Humidity"
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 #if defined(CONFIG_LWM2M_IPSO_HUMIDITY_SENSOR_VERSION_1_1)
65 OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
66 OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
67 OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
68 OBJ_FIELD_DATA(MEASUREMENT_QUALITY_INDICATOR_RID, R_OPT, U8),
69 OBJ_FIELD_DATA(MEASUREMENT_QUALITY_LEVEL_RID, R_OPT, U8),
70 #endif
71 };
72
73 static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
74 static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][NUMBER_OF_OBJ_FIELDS];
75 static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT]
76 [RESOURCE_INSTANCE_COUNT];
77
update_min_measured(uint16_t obj_inst_id,int index)78 static void update_min_measured(uint16_t obj_inst_id, int index)
79 {
80 min_measured_value[index] = sensor_value[index];
81 lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MIN_MEASURED_VALUE_RID);
82 }
83
update_max_measured(uint16_t obj_inst_id,int index)84 static void update_max_measured(uint16_t obj_inst_id, int index)
85 {
86 max_measured_value[index] = sensor_value[index];
87 lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
88 }
89
reset_min_max_measured_values_cb(uint16_t obj_inst_id,uint8_t * args,uint16_t args_len)90 static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
91 uint8_t *args, uint16_t args_len)
92 {
93 int i;
94
95 LOG_DBG("RESET MIN/MAX %d", obj_inst_id);
96 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
97 if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
98 update_min_measured(obj_inst_id, i);
99 update_max_measured(obj_inst_id, i);
100 return 0;
101 }
102 }
103
104 return -ENOENT;
105 }
106
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)107 static int sensor_value_write_cb(uint16_t obj_inst_id, uint16_t res_id,
108 uint16_t res_inst_id, uint8_t *data,
109 uint16_t data_len, bool last_block,
110 size_t total_size)
111 {
112 int i;
113
114 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
115 if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
116 /* update min / max */
117 if (sensor_value[i] < min_measured_value[i]) {
118 update_min_measured(obj_inst_id, i);
119 }
120
121 if (sensor_value[i] > max_measured_value[i]) {
122 update_max_measured(obj_inst_id, i);
123 }
124 }
125 }
126
127 return 0;
128 }
129
130 static struct lwm2m_engine_obj_inst *
humidity_sensor_create(uint16_t obj_inst_id)131 humidity_sensor_create(uint16_t obj_inst_id)
132 {
133 int index, i = 0, j = 0;
134
135 /* Check that there is no other instance with this ID */
136 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
137 if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
138 LOG_ERR("Can not create instance - "
139 "already existing: %u",
140 obj_inst_id);
141 return NULL;
142 }
143 }
144
145 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
146 if (!inst[index].obj) {
147 break;
148 }
149 }
150
151 if (index >= MAX_INSTANCE_COUNT) {
152 LOG_ERR("Can not create instance - no more room: %u",
153 obj_inst_id);
154 return NULL;
155 }
156
157 /* Set default values */
158 sensor_value[index] = 0;
159 units[index][0] = '\0';
160 min_measured_value[index] = INT32_MAX;
161 max_measured_value[index] = -INT32_MAX;
162 min_range_value[index] = 0;
163 max_range_value[index] = 0;
164
165 (void)memset(res[index], 0,
166 sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
167 init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
168
169 /* initialize instance resource data */
170 INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1,
171 false, true, &sensor_value[index], sizeof(*sensor_value),
172 NULL, NULL, NULL, sensor_value_write_cb, NULL);
173 INIT_OBJ_RES_DATA_LEN(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
174 units[index], UNIT_STR_MAX_SIZE, 0);
175 INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i,
176 res_inst[index], j, &min_measured_value[index],
177 sizeof(*min_measured_value));
178 INIT_OBJ_RES_DATA(MAX_MEASURED_VALUE_RID, res[index], i,
179 res_inst[index], j, &max_measured_value[index],
180 sizeof(*max_measured_value));
181 INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[index], i, res_inst[index],
182 j, &min_range_value[index], sizeof(*min_range_value));
183 INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[index], i, res_inst[index],
184 j, &max_range_value[index], sizeof(*max_range_value));
185 INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID, res[index], i,
186 reset_min_max_measured_values_cb);
187 #if defined(CONFIG_LWM2M_IPSO_HUMIDITY_SENSOR_VERSION_1_1)
188 INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[index], i,
189 res_inst[index], j);
190 INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j);
191 INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[index], i,
192 res_inst[index], j);
193 INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[index],
194 i, res_inst[index], j);
195 INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[index], i,
196 res_inst[index], j);
197 #endif
198
199 inst[index].resources = res[index];
200 inst[index].resource_count = i;
201 LOG_DBG("Created IPSO %s Sensor instance: %d", SENSOR_NAME,
202 obj_inst_id);
203 return &inst[index];
204 }
205
ipso_humidity_sensor_init(void)206 static int ipso_humidity_sensor_init(void)
207 {
208 sensor.obj_id = IPSO_OBJECT_ID;
209 sensor.version_major = HUMIDITY_VERSION_MAJOR;
210 sensor.version_minor = HUMIDITY_VERSION_MINOR;
211 sensor.is_core = false;
212 sensor.fields = fields;
213 sensor.field_count = ARRAY_SIZE(fields);
214 sensor.max_instance_count = MAX_INSTANCE_COUNT;
215 sensor.create_cb = humidity_sensor_create;
216 lwm2m_register_obj(&sensor);
217
218 return 0;
219 }
220
221 LWM2M_OBJ_INIT(ipso_humidity_sensor_init);
222