diff --git a/include/zephyr/net/lwm2m.h b/include/zephyr/net/lwm2m.h index c667876f966..81af75ca95b 100644 --- a/include/zephyr/net/lwm2m.h +++ b/include/zephyr/net/lwm2m.h @@ -610,6 +610,8 @@ struct lwm2m_objlnk { /** * @brief Change an observer's pmin value. * + * @deprecated Use lwm2m_update_observer_min_period() instead. + * * LwM2M clients use this function to modify the pmin attribute * for an observation being made. * Example to update the pmin of a temperature sensor value being observed: @@ -621,12 +623,32 @@ struct lwm2m_objlnk { * * @return 0 for success or negative in case of error. */ +__deprecated int lwm2m_engine_update_observer_min_period(struct lwm2m_ctx *client_ctx, const char *pathstr, uint32_t period_s); +/** + * @brief Change an observer's pmin value. + * + * LwM2M clients use this function to modify the pmin attribute + * for an observation being made. + * Example to update the pmin of a temperature sensor value being observed: + * lwm2m_update_observer_min_period(client_ctx, &LWM2M_OBJ(3303, 0, 5700), 5); + * + * @param[in] client_ctx LwM2M context + * @param[in] path LwM2M path as a struct + * @param[in] period_s Value of pmin to be given (in seconds). + * + * @return 0 for success or negative in case of error. + */ +int lwm2m_update_observer_min_period(struct lwm2m_ctx *client_ctx, + const struct lwm2m_obj_path *path, uint32_t period_s); + /** * @brief Change an observer's pmax value. * + * @deprecated Use lwm2m_update_observer_max_period() instead. + * * LwM2M clients use this function to modify the pmax attribute * for an observation being made. * Example to update the pmax of a temperature sensor value being observed: @@ -638,9 +660,27 @@ int lwm2m_engine_update_observer_min_period(struct lwm2m_ctx *client_ctx, const * * @return 0 for success or negative in case of error. */ +__deprecated int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const char *pathstr, uint32_t period_s); +/** + * @brief Change an observer's pmax value. + * + * LwM2M clients use this function to modify the pmax attribute + * for an observation being made. + * Example to update the pmax of a temperature sensor value being observed: + * lwm2m__update_observer_max_period(client_ctx, &LWM2M_OBJ(3303, 0, 5700), 5); + * + * @param[in] client_ctx LwM2M context + * @param[in] path LwM2M path as a struct + * @param[in] period_s Value of pmax to be given (in seconds). + * + * @return 0 for success or negative in case of error. + */ +int lwm2m_update_observer_max_period(struct lwm2m_ctx *client_ctx, + const struct lwm2m_obj_path *path, uint32_t period_s); + /** * @brief Create an LwM2M object instance. * @@ -1664,6 +1704,8 @@ int lwm2m_update_device_service_period(uint32_t period_ms); /** * @brief Check whether a path is observed * + * @deprecated Use lwm2m_path_is_observed() instead. + * * @param[in] pathstr LwM2M path string to check, e.g. "3/0/1" * * @return true when there exists an observation of the same level @@ -1672,8 +1714,22 @@ int lwm2m_update_device_service_period(uint32_t period_ms); * E.g. true if path refers to a resource and the parent object has an * observation, false for the inverse. */ +__deprecated bool lwm2m_engine_path_is_observed(const char *pathstr); +/** + * @brief Check whether a path is observed + * + * @param[in] path LwM2M path as a struct to check + * + * @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_path_is_observed(const struct lwm2m_obj_path *path); + /** * @brief Stop the LwM2M engine * diff --git a/subsys/net/lib/lwm2m/lwm2m_observation.c b/subsys/net/lib/lwm2m/lwm2m_observation.c index db85a4ab633..74ef9d2c99a 100644 --- a/subsys/net/lib/lwm2m/lwm2m_observation.c +++ b/subsys/net/lib/lwm2m/lwm2m_observation.c @@ -199,7 +199,7 @@ int lwm2m_notify_observer(uint16_t obj_id, uint16_t obj_inst_id, uint16_t res_id return lwm2m_notify_observer_path(&path); } -static int engine_observe_get_attributes(struct lwm2m_obj_path *path, +static int engine_observe_get_attributes(const struct lwm2m_obj_path *path, struct notification_attrs *attrs, uint16_t srv_obj_inst) { struct lwm2m_engine_obj *obj; @@ -905,7 +905,7 @@ const char *lwm2m_engine_get_attr_name(const struct lwm2m_attr *attr) } static int lwm2m_engine_observer_timestamp_update(sys_slist_t *observer, - struct lwm2m_obj_path *path, + const struct lwm2m_obj_path *path, uint16_t srv_obj_inst) { struct observe_node *obs; @@ -947,7 +947,7 @@ static int lwm2m_engine_observer_timestamp_update(sys_slist_t *observer, /* input / output selection */ -int lwm2m_get_path_reference_ptr(struct lwm2m_engine_obj *obj, struct lwm2m_obj_path *path, +int lwm2m_get_path_reference_ptr(struct lwm2m_engine_obj *obj, const struct lwm2m_obj_path *path, void **ref) { struct lwm2m_engine_obj_inst *obj_inst; @@ -997,22 +997,16 @@ int lwm2m_get_path_reference_ptr(struct lwm2m_engine_obj *obj, struct lwm2m_obj_ return 0; } -int lwm2m_engine_update_observer_min_period(struct lwm2m_ctx *client_ctx, const char *pathstr, - uint32_t period_s) +int lwm2m_update_observer_min_period(struct lwm2m_ctx *client_ctx, + const struct lwm2m_obj_path *path, uint32_t period_s) { int ret; - struct lwm2m_obj_path path; struct notification_attrs nattrs = {0}; struct notification_attrs attrs = {0}; void *ref; - ret = lwm2m_string_to_path(pathstr, &path, '/'); - if (ret < 0) { - return ret; - } - /* Read Reference pointer to attribute */ - ret = lwm2m_get_path_reference_ptr(NULL, &path, &ref); + ret = lwm2m_get_path_reference_ptr(NULL, path, &ref); if (ret < 0) { return ret; } @@ -1028,7 +1022,7 @@ int lwm2m_engine_update_observer_min_period(struct lwm2m_ctx *client_ctx, const } /* Read Pmin & Pmax values for path */ - ret = engine_observe_get_attributes(&path, &attrs, client_ctx->srv_obj_inst); + ret = engine_observe_get_attributes(path, &attrs, client_ctx->srv_obj_inst); if (ret < 0) { return ret; } @@ -1041,22 +1035,30 @@ int lwm2m_engine_update_observer_min_period(struct lwm2m_ctx *client_ctx, const return lwm2m_update_or_allocate_attribute(ref, LWM2M_ATTR_PMIN, &period_s); } -int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const char *pathstr, +int lwm2m_engine_update_observer_min_period(struct lwm2m_ctx *client_ctx, const char *pathstr, uint32_t period_s) { int ret; struct lwm2m_obj_path path; - void *ref; - struct notification_attrs nattrs = {0}; - struct notification_attrs attrs = {0}; ret = lwm2m_string_to_path(pathstr, &path, '/'); if (ret < 0) { return ret; } + return lwm2m_update_observer_min_period(client_ctx, &path, period_s); +} + +int lwm2m_update_observer_max_period(struct lwm2m_ctx *client_ctx, + const struct lwm2m_obj_path *path, uint32_t period_s) +{ + int ret; + void *ref; + struct notification_attrs nattrs = {0}; + struct notification_attrs attrs = {0}; + /* Read Reference pointer to attribute */ - ret = lwm2m_get_path_reference_ptr(NULL, &path, &ref); + ret = lwm2m_get_path_reference_ptr(NULL, path, &ref); if (ret < 0) { return ret; } @@ -1072,7 +1074,7 @@ int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const } /* Read Pmin & Pmax values for path */ - ret = engine_observe_get_attributes(&path, &attrs, client_ctx->srv_obj_inst); + ret = engine_observe_get_attributes(path, &attrs, client_ctx->srv_obj_inst); if (ret < 0) { return ret; } @@ -1089,10 +1091,24 @@ int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const } /* Update Observer timestamp */ - return lwm2m_engine_observer_timestamp_update(&client_ctx->observer, &path, + return lwm2m_engine_observer_timestamp_update(&client_ctx->observer, path, client_ctx->srv_obj_inst); } +int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const char *pathstr, + uint32_t period_s) +{ + int ret; + struct lwm2m_obj_path path; + + ret = lwm2m_string_to_path(pathstr, &path, '/'); + if (ret < 0) { + return ret; + } + + return lwm2m_update_observer_max_period(client_ctx, &path, period_s); +} + struct lwm2m_attr *lwm2m_engine_get_next_attr(const void *ref, struct lwm2m_attr *prev) { struct lwm2m_attr *iter = (prev == NULL) ? write_attr_pool : prev + 1; @@ -1348,28 +1364,34 @@ int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, struct lwm2m_message return 0; } +bool lwm2m_path_is_observed(const struct lwm2m_obj_path *path) +{ + int i; + struct observe_node *obs; + struct lwm2m_ctx **sock_ctx = lwm2m_sock_ctx(); + + for (i = 0; i < lwm2m_sock_nfds(); ++i) { + SYS_SLIST_FOR_EACH_CONTAINER(&sock_ctx[i]->observer, obs, node) { + + if (lwm2m_notify_observer_list(&obs->path_list, path)) { + return true; + } + } + } + return false; +} + bool lwm2m_engine_path_is_observed(const char *pathstr) { - struct observe_node *obs; - struct lwm2m_obj_path path; int ret; - int i; - struct lwm2m_ctx **sock_ctx = lwm2m_sock_ctx(); + struct lwm2m_obj_path path; ret = lwm2m_string_to_path(pathstr, &path, '/'); if (ret < 0) { return false; } - for (i = 0; i < lwm2m_sock_nfds(); ++i) { - SYS_SLIST_FOR_EACH_CONTAINER(&sock_ctx[i]->observer, obs, node) { - - if (lwm2m_notify_observer_list(&obs->path_list, &path)) { - return true; - } - } - } - return false; + return lwm2m_path_is_observed(&path); } int lwm2m_engine_observation_handler(struct lwm2m_message *msg, int observe, uint16_t accept, diff --git a/subsys/net/lib/lwm2m/lwm2m_observation.h b/subsys/net/lib/lwm2m/lwm2m_observation.h index d90890cc65c..04e0db67101 100644 --- a/subsys/net/lib/lwm2m/lwm2m_observation.h +++ b/subsys/net/lib/lwm2m/lwm2m_observation.h @@ -72,7 +72,7 @@ void lwm2m_engine_path_list_init(sys_slist_t *lwm2m_path_list, sys_slist_t *lwm2 int lwm2m_engine_add_path_to_list(sys_slist_t *lwm2m_path_list, sys_slist_t *lwm2m_free_list, struct lwm2m_obj_path *path); -int lwm2m_get_path_reference_ptr(struct lwm2m_engine_obj *obj, struct lwm2m_obj_path *path, +int lwm2m_get_path_reference_ptr(struct lwm2m_engine_obj *obj, const struct lwm2m_obj_path *path, void **ref); /**