1 /*
2 * Copyright (c) 2017 Linaro Limited
3 * Copyright (c) 2018-2019 Foundries.io
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 */
7
8 /*
9 * Source material for IPSO Temperature Sensor object (3303):
10 * https://github.com/IPSO-Alliance/pub/blob/master/docs/IPSO-Smart-Objects.pdf
11 * Section: "10. IPSO Object: Temperature"
12 */
13
14 #define LOG_MODULE_NAME net_ipso_temp_sensor
15 #define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
16
17 #include <logging/log.h>
18 LOG_MODULE_REGISTER(LOG_MODULE_NAME);
19
20 #include <stdint.h>
21 #include <init.h>
22
23 #include "lwm2m_object.h"
24 #include "lwm2m_engine.h"
25 #include "lwm2m_resource_ids.h"
26
27 #define TEMP_VERSION_MAJOR 1
28
29 #if defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1)
30 #define TEMP_VERSION_MINOR 1
31 #define TEMP_MAX_ID 12
32 #else
33 #define TEMP_VERSION_MINOR 0
34 #define TEMP_MAX_ID 7
35 #endif /* defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1) */
36
37 #define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_TEMP_SENSOR_INSTANCE_COUNT
38
39 #define UNIT_STR_MAX_SIZE 8
40
41 /*
42 * Calculate resource instances as follows:
43 * start with TEMP_MAX_ID
44 * subtract EXEC resources (1)
45 */
46 #define RESOURCE_INSTANCE_COUNT (TEMP_MAX_ID - 1)
47
48 /* resource state variables */
49 static float32_value_t sensor_value[MAX_INSTANCE_COUNT];
50 static char units[MAX_INSTANCE_COUNT][UNIT_STR_MAX_SIZE];
51 static float32_value_t min_measured_value[MAX_INSTANCE_COUNT];
52 static float32_value_t max_measured_value[MAX_INSTANCE_COUNT];
53 static float32_value_t min_range_value[MAX_INSTANCE_COUNT];
54 static float32_value_t max_range_value[MAX_INSTANCE_COUNT];
55
56 static struct lwm2m_engine_obj temp_sensor;
57 static struct lwm2m_engine_obj_field fields[] = {
58 OBJ_FIELD_DATA(SENSOR_VALUE_RID, R, FLOAT),
59 OBJ_FIELD_DATA(SENSOR_UNITS_RID, R_OPT, STRING),
60 OBJ_FIELD_DATA(MIN_MEASURED_VALUE_RID, R_OPT, FLOAT),
61 OBJ_FIELD_DATA(MAX_MEASURED_VALUE_RID, R_OPT, FLOAT),
62 OBJ_FIELD_DATA(MIN_RANGE_VALUE_RID, R_OPT, FLOAT),
63 OBJ_FIELD_DATA(MAX_RANGE_VALUE_RID, R_OPT, FLOAT),
64 OBJ_FIELD_EXECUTE_OPT(RESET_MIN_MAX_MEASURED_VALUES_RID),
65 #if defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1)
66 OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
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][TEMP_MAX_ID];
76 static struct lwm2m_engine_res_inst
77 res_inst[MAX_INSTANCE_COUNT][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].val1 = sensor_value[index].val1;
82 min_measured_value[index].val2 = sensor_value[index].val2;
83 NOTIFY_OBSERVER(IPSO_OBJECT_TEMP_SENSOR_ID, obj_inst_id,
84 MIN_MEASURED_VALUE_RID);
85 }
86
update_max_measured(uint16_t obj_inst_id,int index)87 static void update_max_measured(uint16_t obj_inst_id, int index)
88 {
89 max_measured_value[index].val1 = sensor_value[index].val1;
90 max_measured_value[index].val2 = sensor_value[index].val2;
91 NOTIFY_OBSERVER(IPSO_OBJECT_TEMP_SENSOR_ID, obj_inst_id,
92 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,
113 uint16_t res_id, uint16_t res_inst_id,
114 uint8_t *data, uint16_t data_len,
115 bool last_block, size_t total_size)
116 {
117 int i;
118 bool update_min = false;
119 bool update_max = false;
120
121 for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
122 if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
123 /* update min / max */
124 if (sensor_value[i].val1 < min_measured_value[i].val1) {
125 update_min = true;
126 } else if (sensor_value[i].val1 ==
127 min_measured_value[i].val1 &&
128 sensor_value[i].val2 <
129 min_measured_value[i].val2) {
130 update_min = true;
131 }
132
133 if (sensor_value[i].val1 > max_measured_value[i].val1) {
134 update_max = true;
135 } else if (sensor_value[i].val1 ==
136 max_measured_value[i].val1 &&
137 sensor_value[i].val2 >
138 max_measured_value[i].val2) {
139 update_max = true;
140 }
141
142 if (update_min) {
143 update_min_measured(obj_inst_id, i);
144 }
145
146 if (update_max) {
147 update_max_measured(obj_inst_id, i);
148 }
149 }
150 }
151
152 return 0;
153 }
154
temp_sensor_create(uint16_t obj_inst_id)155 static struct lwm2m_engine_obj_inst *temp_sensor_create(uint16_t obj_inst_id)
156 {
157 int index, i = 0, j = 0;
158
159 /* Check that there is no other instance with this ID */
160 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
161 if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
162 LOG_ERR("Can not create instance - "
163 "already existing: %u", obj_inst_id);
164 return NULL;
165 }
166 }
167
168 for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
169 if (!inst[index].obj) {
170 break;
171 }
172 }
173
174 if (index >= MAX_INSTANCE_COUNT) {
175 LOG_ERR("Can not create instance - no more room: %u",
176 obj_inst_id);
177 return NULL;
178 }
179
180 /* Set default values */
181 sensor_value[index].val1 = 0;
182 sensor_value[index].val2 = 0;
183 units[index][0] = '\0';
184 min_measured_value[index].val1 = INT32_MAX;
185 min_measured_value[index].val2 = 0;
186 max_measured_value[index].val1 = -INT32_MAX;
187 max_measured_value[index].val2 = 0;
188 min_range_value[index].val1 = 0;
189 min_range_value[index].val2 = 0;
190 max_range_value[index].val1 = 0;
191 max_range_value[index].val2 = 0;
192
193 (void)memset(res[index], 0,
194 sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
195 init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
196
197 /* initialize instance resource data */
198 INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i,
199 res_inst[index], j, 1, false, true,
200 &sensor_value[index], sizeof(*sensor_value),
201 NULL, NULL, NULL, sensor_value_write_cb, NULL);
202 INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
203 units[index], UNIT_STR_MAX_SIZE);
204 INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i,
205 res_inst[index], j, &min_measured_value[index],
206 sizeof(*min_measured_value));
207 INIT_OBJ_RES_DATA(MAX_MEASURED_VALUE_RID, res[index], i,
208 res_inst[index], j, &max_measured_value[index],
209 sizeof(*max_measured_value));
210 INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[index], i,
211 res_inst[index], j, &min_range_value[index],
212 sizeof(*min_range_value));
213 INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[index], i,
214 res_inst[index], j, &max_range_value[index],
215 sizeof(*max_range_value));
216 INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID,
217 res[index], i, reset_min_max_measured_values_cb);
218 #if defined(CONFIG_LWM2M_IPSO_TEMP_SENSOR_VERSION_1_1)
219 INIT_OBJ_RES_OPTDATA(APPLICATION_TYPE_RID, res[index], i,
220 res_inst[index], j);
221 INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j);
222 INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[index], i,
223 res_inst[index], j);
224 INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[index],
225 i, res_inst[index], j);
226 INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[index], i,
227 res_inst[index], j);
228 #endif
229
230 inst[index].resources = res[index];
231 inst[index].resource_count = i;
232 LOG_DBG("Create IPSO Temperature Sensor instance: %d", obj_inst_id);
233 return &inst[index];
234 }
235
ipso_temp_sensor_init(const struct device * dev)236 static int ipso_temp_sensor_init(const struct device *dev)
237 {
238 temp_sensor.obj_id = IPSO_OBJECT_TEMP_SENSOR_ID;
239 temp_sensor.version_major = TEMP_VERSION_MAJOR;
240 temp_sensor.version_minor = TEMP_VERSION_MINOR;
241 temp_sensor.is_core = false;
242 temp_sensor.fields = fields;
243 temp_sensor.field_count = ARRAY_SIZE(fields);
244 temp_sensor.max_instance_count = MAX_INSTANCE_COUNT;
245 temp_sensor.create_cb = temp_sensor_create;
246 lwm2m_register_obj(&temp_sensor);
247
248 return 0;
249 }
250
251 SYS_INIT(ipso_temp_sensor_init, APPLICATION,
252 CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);
253