From 7eb25df5269022a31586c52bc136e854c76f6ed0 Mon Sep 17 00:00:00 2001 From: Henning Fleddermann Date: Tue, 8 Feb 2022 01:48:32 +0100 Subject: [PATCH] net: lib: lwm2m: add lwm2m_engine_is_observed function add helper function to check wether a specific lwm2m path is observed Signed-off-by: Henning Fleddermann --- include/net/lwm2m.h | 13 +++++++++++++ subsys/net/lib/lwm2m/lwm2m_engine.c | 29 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/include/net/lwm2m.h b/include/net/lwm2m.h index d174589f77b..7b362be16d9 100644 --- a/include/net/lwm2m.h +++ b/include/net/lwm2m.h @@ -1062,6 +1062,19 @@ int lwm2m_engine_update_service_period(k_work_handler_t service, uint32_t period */ int lwm2m_update_device_service_period(uint32_t period_ms); +/** + * @brief Check whether a path is observed + * + * @param[in] pathstr LwM2M path string to check, e.g. "3/0/1" + * + * @return true when there exists an observation of the same level + * or lower as the given path, false if it doesn't or path is not a + * valid LwM2M-path. + * E.g. true if path refers to a resource and the parent object has an + * observation, false for the inverse. + */ +bool lwm2m_engine_path_is_observed(const char *pathstr); + /** * @brief Start the LwM2M engine * diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index f01afa4b0dc..0f004ade750 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -2105,6 +2105,35 @@ int lwm2m_engine_delete_res_inst(const char *pathstr) return 0; } +bool lwm2m_engine_path_is_observed(const char *pathstr) +{ + struct observe_node *obs; + struct lwm2m_obj_path path; + int ret; + int i; + + ret = string_to_path(pathstr, &path, '/'); + if (ret < 0) { + return false; + } + + for (i = 0; i < sock_nfds; ++i) { + SYS_SLIST_FOR_EACH_CONTAINER(&sock_ctx[i]->observer, obs, node) { + if (obs->path.level <= path.level && + (obs->path.obj_id == path.obj_id && + (obs->path.level < LWM2M_PATH_LEVEL_OBJECT_INST || + (obs->path.obj_inst_id == path.obj_inst_id && + (obs->path.level < LWM2M_PATH_LEVEL_RESOURCE || + (obs->path.res_id == path.res_id && + (obs->path.level < LWM2M_PATH_LEVEL_RESOURCE_INST || + obs->path.res_inst_id == path.res_inst_id))))))) { + return true; + } + } + } + return false; +} + int lwm2m_engine_register_read_callback(const char *pathstr, lwm2m_engine_get_data_cb_t cb) {