Skip to content

Commit 1bfd2ab

Browse files
rlubosdanieldegrasse
authored andcommitted
net: lwm2m: Update value of resources in notification registry
Update the value stored in the notification registry whenever notification is sent (for those resources that have gt/lt/st attributes assigned). Signed-off-by: Robert Lubos <[email protected]>
1 parent 1ee6943 commit 1bfd2ab

File tree

3 files changed

+207
-0
lines changed

3 files changed

+207
-0
lines changed

subsys/net/lib/lwm2m/lwm2m_message_handling.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3200,6 +3200,7 @@ int generate_notify_message(struct lwm2m_ctx *ctx, struct observe_node *obs, voi
32003200
obs->active_notify = msg;
32013201
obs->resource_update = false;
32023202
lwm2m_information_interface_send(msg);
3203+
lwm2m_engine_observer_refresh_notified_values(obs);
32033204
#if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT)
32043205
msg->cache_info = NULL;
32053206
#endif

subsys/net/lib/lwm2m/lwm2m_observation.c

Lines changed: 204 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -484,6 +484,71 @@ int engine_observe_attribute_list_get(sys_slist_t *path_list, struct notificatio
484484
return 0;
485485
}
486486

487+
static int resource_value_as_double(const struct lwm2m_obj_path *path,
488+
double *value)
489+
{
490+
struct lwm2m_engine_obj_field *obj_field;
491+
union {
492+
int64_t s64;
493+
int32_t s32;
494+
int16_t s16;
495+
int8_t s8;
496+
uint32_t u32;
497+
uint16_t u16;
498+
uint8_t u8;
499+
} temp_buf = { 0 };
500+
int ret;
501+
502+
lwm2m_registry_lock();
503+
504+
ret = path_to_objs(path, NULL, &obj_field, NULL, NULL);
505+
if (ret < 0 || obj_field == NULL) {
506+
lwm2m_registry_unlock();
507+
return -ENOENT;
508+
}
509+
510+
switch (obj_field->data_type) {
511+
case LWM2M_RES_TYPE_U32:
512+
ret = lwm2m_get_u32(path, &temp_buf.u32);
513+
*value = (double)temp_buf.u32;
514+
break;
515+
case LWM2M_RES_TYPE_U16:
516+
ret = lwm2m_get_u16(path, &temp_buf.u16);
517+
*value = (double)temp_buf.u16;
518+
break;
519+
case LWM2M_RES_TYPE_U8:
520+
ret = lwm2m_get_u8(path, &temp_buf.u8);
521+
*value = (double)temp_buf.u8;
522+
break;
523+
case LWM2M_RES_TYPE_S64:
524+
ret = lwm2m_get_s64(path, &temp_buf.s64);
525+
*value = (double)temp_buf.s64;
526+
break;
527+
case LWM2M_RES_TYPE_S32:
528+
ret = lwm2m_get_s32(path, &temp_buf.s32);
529+
*value = (double)temp_buf.s32;
530+
break;
531+
case LWM2M_RES_TYPE_S16:
532+
ret = lwm2m_get_s16(path, &temp_buf.s16);
533+
*value = (double)temp_buf.s16;
534+
break;
535+
case LWM2M_RES_TYPE_S8:
536+
ret = lwm2m_get_s8(path, &temp_buf.s8);
537+
*value = (double)temp_buf.s8;
538+
break;
539+
case LWM2M_RES_TYPE_FLOAT:
540+
ret = lwm2m_get_f64(path, value);
541+
break;
542+
default:
543+
ret = -ENOTSUP;
544+
break;
545+
}
546+
547+
lwm2m_registry_unlock();
548+
549+
return ret;
550+
}
551+
487552
int lwm2m_notify_observer_path(const struct lwm2m_obj_path *path)
488553
{
489554
struct observe_node *obs;
@@ -757,6 +822,8 @@ static int engine_add_observer(struct lwm2m_message *msg, const uint8_t *token,
757822
msg->path.obj_inst_id, msg->path.res_id, msg->path.level,
758823
lwm2m_sprint_ip_addr(&msg->ctx->remote_addr));
759824

825+
lwm2m_engine_observer_refresh_notified_values(obs);
826+
760827
return 0;
761828
}
762829

@@ -772,6 +839,8 @@ static int engine_add_observer(struct lwm2m_message *msg, const uint8_t *token,
772839
}
773840

774841
engine_observe_node_init(obs, token, msg->ctx, tkl, format, attrs.pmax);
842+
lwm2m_engine_observer_refresh_notified_values(obs);
843+
775844
return 0;
776845
}
777846

@@ -845,6 +914,8 @@ static int engine_add_composite_observer(struct lwm2m_message *msg, const uint8_
845914
LOG_DBG("OBSERVER Composite DUPLICATE [%s]",
846915
lwm2m_sprint_ip_addr(&msg->ctx->remote_addr));
847916

917+
lwm2m_engine_observer_refresh_notified_values(obs);
918+
848919
return do_composite_read_op_for_parsed_list(msg, format, &lwm2m_path_list);
849920
}
850921

@@ -858,6 +929,8 @@ static int engine_add_composite_observer(struct lwm2m_message *msg, const uint8_
858929
return -ENOMEM;
859930
}
860931
engine_observe_node_init(obs, token, msg->ctx, tkl, format, attrs.pmax);
932+
lwm2m_engine_observer_refresh_notified_values(obs);
933+
861934
return do_composite_read_op_for_parsed_list(msg, format, &lwm2m_path_list);
862935
}
863936

@@ -1822,3 +1895,134 @@ void lwm2m_engine_clear_duplicate_path(sys_slist_t *lwm2m_path_list, sys_slist_t
18221895
}
18231896
}
18241897
}
1898+
1899+
static void update_resource_instance_notified_value(
1900+
const struct lwm2m_obj_path *path,
1901+
const struct lwm2m_engine_res_inst *ref)
1902+
{
1903+
struct lwm2m_notify_value_register *last_notified;
1904+
double new_value;
1905+
int ret;
1906+
1907+
last_notified = notify_value_reg_get(ref);
1908+
if (last_notified == NULL) {
1909+
return;
1910+
}
1911+
1912+
ret = resource_value_as_double(path, &new_value);
1913+
if (ret < 0) {
1914+
return;
1915+
}
1916+
1917+
last_notified->value = new_value;
1918+
last_notified->notified = true;
1919+
}
1920+
1921+
static void update_resource_notified_value(const struct lwm2m_obj_path *path,
1922+
const struct lwm2m_engine_res *res)
1923+
{
1924+
struct lwm2m_obj_path res_inst_path = {
1925+
.level = LWM2M_PATH_LEVEL_RESOURCE_INST,
1926+
.obj_id = path->obj_id,
1927+
.obj_inst_id = path->obj_inst_id,
1928+
.res_id = path->res_id,
1929+
};
1930+
struct lwm2m_engine_res_inst *res_inst;
1931+
1932+
if (!is_resource_numerical(path)) {
1933+
return;
1934+
}
1935+
1936+
for (int i = 0; i < res->res_inst_count; i++) {
1937+
res_inst = &res->res_instances[i];
1938+
res_inst_path.res_inst_id = res_inst->res_inst_id;
1939+
1940+
if (res_inst->res_inst_id == RES_INSTANCE_NOT_CREATED) {
1941+
continue;
1942+
}
1943+
1944+
update_resource_instance_notified_value(&res_inst_path, res_inst);
1945+
}
1946+
}
1947+
1948+
static void update_object_instance_notified_values(
1949+
const struct lwm2m_obj_path *path,
1950+
const struct lwm2m_engine_obj_inst *obj_inst)
1951+
{
1952+
struct lwm2m_obj_path res_path = {
1953+
.level = LWM2M_PATH_LEVEL_RESOURCE,
1954+
.obj_id = path->obj_id,
1955+
.obj_inst_id = path->obj_inst_id,
1956+
};
1957+
struct lwm2m_engine_res *res;
1958+
1959+
for (int i = 0; i < obj_inst->resource_count; i++) {
1960+
res = &obj_inst->resources[i];
1961+
res_path.res_id = res->res_id;
1962+
1963+
update_resource_notified_value(&res_path, res);
1964+
}
1965+
}
1966+
1967+
static void update_object_notified_values(const struct lwm2m_obj_path *path,
1968+
const struct lwm2m_engine_obj *obj)
1969+
{
1970+
struct lwm2m_engine_obj_inst *obj_inst = next_engine_obj_inst(obj->obj_id, -1);
1971+
struct lwm2m_obj_path obj_inst_path = {
1972+
.level = LWM2M_PATH_LEVEL_OBJECT_INST,
1973+
.obj_id = path->obj_id,
1974+
};
1975+
1976+
if (obj_inst == NULL) {
1977+
return;
1978+
}
1979+
1980+
for (int i = 0; i < obj->instance_count; i++) {
1981+
obj_inst_path.obj_inst_id = obj_inst->obj_inst_id;
1982+
1983+
update_object_instance_notified_values(&obj_inst_path, obj_inst);
1984+
1985+
obj_inst = next_engine_obj_inst(obj->obj_id, obj_inst->obj_inst_id);
1986+
if (obj_inst == NULL) {
1987+
break;
1988+
}
1989+
}
1990+
}
1991+
1992+
void lwm2m_engine_observer_refresh_notified_values(struct observe_node *obs)
1993+
{
1994+
struct lwm2m_obj_path_list *tmp;
1995+
struct lwm2m_obj_path *obs_path;
1996+
void *ref;
1997+
int ret;
1998+
1999+
lwm2m_registry_lock();
2000+
2001+
SYS_SLIST_FOR_EACH_CONTAINER(&obs->path_list, tmp, node) {
2002+
obs_path = &tmp->path;
2003+
2004+
ret = lwm2m_get_path_reference_ptr(NULL, &tmp->path, &ref);
2005+
if (ret < 0) {
2006+
continue;
2007+
}
2008+
2009+
switch (obs_path->level) {
2010+
case LWM2M_PATH_LEVEL_RESOURCE_INST:
2011+
update_resource_instance_notified_value(obs_path, ref);
2012+
break;
2013+
case LWM2M_PATH_LEVEL_RESOURCE:
2014+
update_resource_notified_value(obs_path, ref);
2015+
break;
2016+
case LWM2M_PATH_LEVEL_OBJECT_INST:
2017+
update_object_instance_notified_values(obs_path, ref);
2018+
break;
2019+
case LWM2M_PATH_LEVEL_OBJECT:
2020+
update_object_notified_values(obs_path, ref);
2021+
break;
2022+
default:
2023+
break;
2024+
}
2025+
}
2026+
2027+
lwm2m_registry_unlock();
2028+
}

subsys/net/lib/lwm2m/lwm2m_observation.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,8 @@ int lwm2m_get_path_reference_ptr(struct lwm2m_engine_obj *obj, const struct lwm2
8888
*/
8989
void lwm2m_engine_clear_duplicate_path(sys_slist_t *lwm2m_path_list, sys_slist_t *lwm2m_free_list);
9090

91+
void lwm2m_engine_observer_refresh_notified_values(struct observe_node *obs);
92+
9193
/* Resources */
9294
sys_slist_t *lwm2m_obs_obj_path_list(void);
9395
#endif /* LWM2M_OBSERVATION_H */

0 commit comments

Comments
 (0)