Skip to content

Commit 96c8d16

Browse files
DaveLacertecarlescufi
authored andcommitted
net: lwm2m: Add IPSO voltage sensor object
Add support fot the IPSO voltage sensor object Signed-off-by: Dave Lacerte <[email protected]>
1 parent 654d217 commit 96c8d16

File tree

5 files changed

+271
-0
lines changed

5 files changed

+271
-0
lines changed

include/zephyr/net/lwm2m.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
#define IPSO_OBJECT_HUMIDITY_SENSOR_ID 3304
6565
#define IPSO_OBJECT_LIGHT_CONTROL_ID 3311
6666
#define IPSO_OBJECT_ACCELEROMETER_ID 3313
67+
#define IPSO_OBJECT_VOLTAGE_SENSOR_ID 3316
6768
#define IPSO_OBJECT_CURRENT_SENSOR_ID 3317
6869
#define IPSO_OBJECT_PRESSURE_ID 3323
6970
#define IPSO_OBJECT_BUZZER_ID 3338

subsys/net/lib/lwm2m/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,9 @@ zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_PUSH_BUTTON
111111
zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_CURRENT_SENSOR
112112
ipso_current_sensor.c
113113
)
114+
zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR
115+
ipso_voltage_sensor.c
116+
)
114117
zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_FILLING_SENSOR
115118
ipso_filling_sensor.c
116119
)

subsys/net/lib/lwm2m/Kconfig.ipso

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,39 @@ config LWM2M_IPSO_CURRENT_SENSOR_VERSION_1_1
318318

319319
endchoice
320320

321+
config LWM2M_IPSO_VOLTAGE_SENSOR
322+
bool "IPSO Voltage Sensor Support"
323+
help
324+
This IPSO object should be used with a voltage sensor to
325+
report a voltage measurement. It also provides resources for
326+
minimum/maximum measured values and the minimum/maximum range
327+
that can be measured by the sensor.
328+
329+
config LWM2M_IPSO_VOLTAGE_SENSOR_INSTANCE_COUNT
330+
int "Maximum # of IPSO Voltage Sensor object instances"
331+
default 1
332+
depends on LWM2M_IPSO_VOLTAGE_SENSOR
333+
help
334+
This setting establishes the total count of IPSO Voltage
335+
Sensor instances available to the LWM2M client.
336+
337+
choice LWM2M_IPSO_VOLTAGE_SENSOR_VERSION
338+
prompt "IPSO Voltage object version"
339+
default LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_0
340+
depends on LWM2M_IPSO_VOLTAGE_SENSOR
341+
help
342+
Select which version of the IPSO Voltage object should be used.
343+
344+
config LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_0
345+
bool "IPSO Voltage object version 1.0"
346+
347+
config LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_1
348+
bool "IPSO Voltage object version 1.1"
349+
help
350+
Adds timestamp and measurement quality
351+
352+
endchoice
353+
321354
config LWM2M_IPSO_FILLING_SENSOR
322355
bool "IPSO Filling sensor Support"
323356
help
Lines changed: 233 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,233 @@
1+
/*
2+
* Copyright (c) 2021 Laird Connectivity
3+
* Copyright (c) 2023 Hydro-Quebec
4+
*
5+
* SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
/*
9+
* Voltage sensor
10+
* https://github.com/OpenMobileAlliance/lwm2m-registry/blob/prod/3316.xml
11+
*/
12+
13+
#define LOG_MODULE_NAME net_ipso_voltage_sensor
14+
#define LOG_LEVEL CONFIG_LWM2M_LOG_LEVEL
15+
16+
#include <zephyr/logging/log.h>
17+
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
18+
19+
#include <stdint.h>
20+
#include <zephyr/init.h>
21+
22+
#include "lwm2m_object.h"
23+
#include "lwm2m_engine.h"
24+
#include "lwm2m_resource_ids.h"
25+
26+
#define VOLTAGE_VERSION_MAJOR 1
27+
28+
#if defined(CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_1)
29+
#define VOLTAGE_VERSION_MINOR 1
30+
#define NUMBER_OF_OBJ_FIELDS 13
31+
#else
32+
#define VOLTAGE_VERSION_MINOR 0
33+
#define NUMBER_OF_OBJ_FIELDS 9
34+
#endif /* defined(CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_1) */
35+
36+
#define MAX_INSTANCE_COUNT CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR_INSTANCE_COUNT
37+
38+
#define IPSO_OBJECT_ID IPSO_OBJECT_VOLTAGE_SENSOR_ID
39+
40+
#define UNIT_STR_MAX_SIZE 8
41+
#define APP_TYPE_STR_MAX_SIZE 32
42+
43+
/*
44+
* Calculate resource instances as follows:
45+
* start with NUMBER_OF_OBJ_FIELDS
46+
* subtract EXEC resources (1)
47+
*/
48+
#define RESOURCE_INSTANCE_COUNT (NUMBER_OF_OBJ_FIELDS - 1)
49+
50+
/* resource state variables */
51+
static double sensor_value[MAX_INSTANCE_COUNT];
52+
static char units[MAX_INSTANCE_COUNT][UNIT_STR_MAX_SIZE];
53+
static double min_measured_value[MAX_INSTANCE_COUNT];
54+
static double max_measured_value[MAX_INSTANCE_COUNT];
55+
static double min_range_value[MAX_INSTANCE_COUNT];
56+
static double max_range_value[MAX_INSTANCE_COUNT];
57+
static double calibration_coefficient[MAX_INSTANCE_COUNT];
58+
static char app_type[MAX_INSTANCE_COUNT][APP_TYPE_STR_MAX_SIZE];
59+
60+
static struct lwm2m_engine_obj sensor;
61+
static struct lwm2m_engine_obj_field fields[] = {
62+
OBJ_FIELD_DATA(SENSOR_VALUE_RID, R, FLOAT),
63+
OBJ_FIELD_DATA(SENSOR_UNITS_RID, R_OPT, STRING),
64+
OBJ_FIELD_DATA(MIN_MEASURED_VALUE_RID, R_OPT, FLOAT),
65+
OBJ_FIELD_DATA(MAX_MEASURED_VALUE_RID, R_OPT, FLOAT),
66+
OBJ_FIELD_DATA(MIN_RANGE_VALUE_RID, R_OPT, FLOAT),
67+
OBJ_FIELD_DATA(MAX_RANGE_VALUE_RID, R_OPT, FLOAT),
68+
OBJ_FIELD_EXECUTE_OPT(RESET_MIN_MAX_MEASURED_VALUES_RID),
69+
OBJ_FIELD_DATA(APPLICATION_TYPE_RID, RW_OPT, STRING),
70+
OBJ_FIELD_DATA(CURRENT_CALIBRATION_RID, R_OPT, FLOAT),
71+
#if defined(CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_1)
72+
OBJ_FIELD_DATA(TIMESTAMP_RID, R_OPT, TIME),
73+
OBJ_FIELD_DATA(FRACTIONAL_TIMESTAMP_RID, R_OPT, FLOAT),
74+
OBJ_FIELD_DATA(MEASUREMENT_QUALITY_INDICATOR_RID, R_OPT, U8),
75+
OBJ_FIELD_DATA(MEASUREMENT_QUALITY_LEVEL_RID, R_OPT, U8),
76+
#endif
77+
};
78+
79+
static struct lwm2m_engine_obj_inst inst[MAX_INSTANCE_COUNT];
80+
static struct lwm2m_engine_res res[MAX_INSTANCE_COUNT][NUMBER_OF_OBJ_FIELDS];
81+
static struct lwm2m_engine_res_inst res_inst[MAX_INSTANCE_COUNT]
82+
[RESOURCE_INSTANCE_COUNT];
83+
84+
static void update_min_measured(uint16_t obj_inst_id, int index)
85+
{
86+
min_measured_value[index] = sensor_value[index];
87+
lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MIN_MEASURED_VALUE_RID);
88+
}
89+
90+
static void update_max_measured(uint16_t obj_inst_id, int index)
91+
{
92+
max_measured_value[index] = sensor_value[index];
93+
lwm2m_notify_observer(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
94+
}
95+
96+
static int reset_min_max_measured_values_cb(uint16_t obj_inst_id,
97+
uint8_t *args, uint16_t args_len)
98+
{
99+
int i;
100+
101+
LOG_DBG("RESET MIN/MAX %d", obj_inst_id);
102+
for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
103+
if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
104+
update_min_measured(obj_inst_id, i);
105+
update_max_measured(obj_inst_id, i);
106+
return 0;
107+
}
108+
}
109+
110+
return -ENOENT;
111+
}
112+
113+
static int sensor_value_write_cb(uint16_t obj_inst_id, uint16_t res_id,
114+
uint16_t res_inst_id, uint8_t *data,
115+
uint16_t data_len, bool last_block,
116+
size_t total_size)
117+
{
118+
int i;
119+
120+
for (i = 0; i < MAX_INSTANCE_COUNT; i++) {
121+
if (inst[i].obj && inst[i].obj_inst_id == obj_inst_id) {
122+
/* update min / max */
123+
if (sensor_value[i] < min_measured_value[i]) {
124+
update_min_measured(obj_inst_id, i);
125+
}
126+
127+
if (sensor_value[i] > max_measured_value[i]) {
128+
update_max_measured(obj_inst_id, i);
129+
}
130+
131+
break;
132+
}
133+
}
134+
135+
return 0;
136+
}
137+
138+
static struct lwm2m_engine_obj_inst *voltage_sensor_create(uint16_t obj_inst_id)
139+
{
140+
int index, i = 0, j = 0;
141+
142+
/* Check that there is no other instance with this ID */
143+
for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
144+
if (inst[index].obj && inst[index].obj_inst_id == obj_inst_id) {
145+
LOG_ERR("Can not create instance - "
146+
"already existing: %u",
147+
obj_inst_id);
148+
return NULL;
149+
}
150+
}
151+
152+
for (index = 0; index < MAX_INSTANCE_COUNT; index++) {
153+
if (!inst[index].obj) {
154+
break;
155+
}
156+
}
157+
158+
if (index >= MAX_INSTANCE_COUNT) {
159+
LOG_ERR("Can not create instance - no more room: %u",
160+
obj_inst_id);
161+
return NULL;
162+
}
163+
164+
/* Set default values (objects can be removed/recreated during runtime) */
165+
units[index][0] = '\0';
166+
min_measured_value[index] = INT32_MAX;
167+
max_measured_value[index] = -INT32_MAX;
168+
min_range_value[index] = 0;
169+
max_range_value[index] = 0;
170+
calibration_coefficient[index] = 1;
171+
app_type[index][0] = '\0';
172+
173+
(void)memset(res[index], 0,
174+
sizeof(res[index][0]) * ARRAY_SIZE(res[index]));
175+
init_res_instance(res_inst[index], ARRAY_SIZE(res_inst[index]));
176+
177+
/* initialize instance resource data */
178+
INIT_OBJ_RES(SENSOR_VALUE_RID, res[index], i, res_inst[index], j, 1,
179+
false, true, &sensor_value[index], sizeof(*sensor_value),
180+
NULL, NULL, NULL, sensor_value_write_cb, NULL);
181+
INIT_OBJ_RES_DATA(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
182+
units[index], UNIT_STR_MAX_SIZE);
183+
INIT_OBJ_RES_DATA(MIN_MEASURED_VALUE_RID, res[index], i,
184+
res_inst[index], j, &min_measured_value[index],
185+
sizeof(*min_measured_value));
186+
INIT_OBJ_RES_DATA(MAX_MEASURED_VALUE_RID, res[index], i,
187+
res_inst[index], j, &max_measured_value[index],
188+
sizeof(*max_measured_value));
189+
INIT_OBJ_RES_DATA(MIN_RANGE_VALUE_RID, res[index], i, res_inst[index],
190+
j, &min_range_value[index], sizeof(*min_range_value));
191+
INIT_OBJ_RES_DATA(MAX_RANGE_VALUE_RID, res[index], i, res_inst[index],
192+
j, &max_range_value[index], sizeof(*max_range_value));
193+
INIT_OBJ_RES_EXECUTE(RESET_MIN_MAX_MEASURED_VALUES_RID, res[index], i,
194+
reset_min_max_measured_values_cb);
195+
INIT_OBJ_RES_DATA(CURRENT_CALIBRATION_RID, res[index], i,
196+
res_inst[index], j, &calibration_coefficient[index],
197+
sizeof(*calibration_coefficient));
198+
INIT_OBJ_RES_DATA(APPLICATION_TYPE_RID, res[index], i, res_inst[index],
199+
j, app_type[index], APP_TYPE_STR_MAX_SIZE);
200+
201+
#if defined(CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR_VERSION_1_1)
202+
INIT_OBJ_RES_OPTDATA(TIMESTAMP_RID, res[index], i, res_inst[index], j);
203+
INIT_OBJ_RES_OPTDATA(FRACTIONAL_TIMESTAMP_RID, res[index], i,
204+
res_inst[index], j);
205+
INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_INDICATOR_RID, res[index], i,
206+
res_inst[index], j);
207+
INIT_OBJ_RES_OPTDATA(MEASUREMENT_QUALITY_LEVEL_RID, res[index], i,
208+
res_inst[index], j);
209+
#endif
210+
211+
inst[index].resources = res[index];
212+
inst[index].resource_count = i;
213+
LOG_DBG("Created IPSO Voltage Sensor instance: %d", obj_inst_id);
214+
return &inst[index];
215+
}
216+
217+
static int ipso_voltage_sensor_init(const struct device *dev)
218+
{
219+
sensor.obj_id = IPSO_OBJECT_ID;
220+
sensor.version_major = VOLTAGE_VERSION_MAJOR;
221+
sensor.version_minor = VOLTAGE_VERSION_MINOR;
222+
sensor.is_core = false;
223+
sensor.fields = fields;
224+
sensor.field_count = ARRAY_SIZE(fields);
225+
sensor.max_instance_count = MAX_INSTANCE_COUNT;
226+
sensor.create_cb = voltage_sensor_create;
227+
lwm2m_register_obj(&sensor);
228+
229+
return 0;
230+
}
231+
232+
SYS_INIT(ipso_voltage_sensor_init, APPLICATION,
233+
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

tests/net/all/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@ CONFIG_LWM2M_IPSO_TIMER=y
299299
CONFIG_LWM2M_IPSO_ONOFF_SWITCH=y
300300
CONFIG_LWM2M_IPSO_PUSH_BUTTON=y
301301
CONFIG_LWM2M_IPSO_CURRENT_SENSOR=y
302+
CONFIG_LWM2M_IPSO_VOLTAGE_SENSOR=y
302303
CONFIG_LWM2M_IPSO_FILLING_SENSOR=y
303304
CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT=y
304305
CONFIG_LWM2M_UCIFI_SUPPORT=y

0 commit comments

Comments
 (0)