Skip to content

Commit e071555

Browse files
ahedin25cfriedt
authored andcommitted
net: lwm2m: Add IPSO current sensor object
Add support for the current sensor object used by the MG100 and BT610 LwM2M demo. Signed-off-by: Andrew Hedin <[email protected]>
1 parent 2cab877 commit e071555

File tree

5 files changed

+270
-0
lines changed

5 files changed

+270
-0
lines changed

include/net/lwm2m.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@
5757
#define IPSO_OBJECT_HUMIDITY_SENSOR_ID 3304
5858
#define IPSO_OBJECT_LIGHT_CONTROL_ID 3311
5959
#define IPSO_OBJECT_ACCELEROMETER_ID 3313
60+
#define IPSO_OBJECT_CURRENT_SENSOR_ID 3317
6061
#define IPSO_OBJECT_PRESSURE_ID 3323
6162
#define IPSO_OBJECT_BUZZER_ID 3338
6263
#define IPSO_OBJECT_TIMER_ID 3340

subsys/net/lib/lwm2m/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,5 +70,8 @@ zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_ONOFF_SWITCH
7070
zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_PUSH_BUTTON
7171
ipso_push_button.c
7272
)
73+
zephyr_library_sources_ifdef(CONFIG_LWM2M_IPSO_CURRENT_SENSOR
74+
ipso_current_sensor.c
75+
)
7376

7477
zephyr_library_link_libraries_ifdef(CONFIG_MBEDTLS mbedTLS)

subsys/net/lib/lwm2m/Kconfig.ipso

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,4 +285,37 @@ config LWM2M_IPSO_PUSH_BUTTON_VERSION_1_1
285285

286286
endchoice
287287

288+
config LWM2M_IPSO_CURRENT_SENSOR
289+
bool "IPSO Current Sensor Support"
290+
help
291+
This IPSO object should be used with a current sensor to
292+
report a current measurement. It also provides resources for
293+
minimum/maximum measured values and the minimum/maximum range
294+
that can be measured by the sensor.
295+
296+
config LWM2M_IPSO_CURRENT_SENSOR_INSTANCE_COUNT
297+
int "Maximum # of IPSO Current Sensor object instances"
298+
default 1
299+
depends on LWM2M_IPSO_CURRENT_SENSOR
300+
help
301+
This setting establishes the total count of IPSO Current
302+
Sensor instances available to the LWM2M client.
303+
304+
choice
305+
prompt "IPSO Current object version"
306+
default LWM2M_IPSO_CURRENT_SENSOR_VERSION_1_0
307+
depends on LWM2M_IPSO_CURRENT_SENSOR
308+
help
309+
Select which version of the IPSO Current object should be used.
310+
311+
config LWM2M_IPSO_CURRENT_SENSOR_VERSION_1_0
312+
bool "IPSO Current object version 1.0"
313+
314+
config LWM2M_IPSO_CURRENT_SENSOR_VERSION_1_1
315+
bool "IPSO Current object version 1.1"
316+
help
317+
Adds timestamp and measurement quality
318+
319+
endchoice
320+
288321
endif # LWM2M_LWM2M_IPSO_SUPPORT
Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
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 <logging/log.h>
16+
LOG_MODULE_REGISTER(LOG_MODULE_NAME);
17+
18+
#include <stdint.h>
19+
#include <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+
83+
static void update_min_measured(uint16_t obj_inst_id, int index)
84+
{
85+
min_measured_value[index] = sensor_value[index];
86+
NOTIFY_OBSERVER(IPSO_OBJECT_ID, obj_inst_id, MIN_MEASURED_VALUE_RID);
87+
}
88+
89+
static void update_max_measured(uint16_t obj_inst_id, int index)
90+
{
91+
max_measured_value[index] = sensor_value[index];
92+
NOTIFY_OBSERVER(IPSO_OBJECT_ID, obj_inst_id, MAX_MEASURED_VALUE_RID);
93+
}
94+
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+
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+
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(SENSOR_UNITS_RID, res[index], i, res_inst[index], j,
181+
units[index], UNIT_STR_MAX_SIZE);
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(APPLICATION_TYPE_RID, res[index], i, res_inst[index],
198+
j, app_type[index], APP_TYPE_STR_MAX_SIZE);
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+
216+
static int ipso_current_sensor_init(const struct device *dev)
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+
SYS_INIT(ipso_current_sensor_init, APPLICATION,
232+
CONFIG_KERNEL_INIT_PRIORITY_DEFAULT);

tests/net/all/prj.conf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,7 @@ CONFIG_LWM2M_IPSO_BUZZER=y
300300
CONFIG_LWM2M_IPSO_TIMER=y
301301
CONFIG_LWM2M_IPSO_ONOFF_SWITCH=y
302302
CONFIG_LWM2M_IPSO_PUSH_BUTTON=y
303+
CONFIG_LWM2M_IPSO_CURRENT_SENSOR=y
303304

304305
# VLAN
305306
CONFIG_NET_VLAN=y

0 commit comments

Comments
 (0)