From 5c80be93793a8162b57dec80b52681925656d940 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jarno=20L=C3=A4ms=C3=A4?= Date: Wed, 4 Jan 2023 13:30:43 +0200 Subject: [PATCH] net: lib: lwm2m: Deprecate object and resource API MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Deprecate old API and offer a new API for object and resource creation and deletion. The new API uses path struct instead of using a string as a reference to a path. Signed-off-by: Jarno Lämsä --- include/zephyr/net/lwm2m.h | 60 +++++++++++++++++ subsys/net/lib/lwm2m/lwm2m_registry.c | 94 +++++++++++++++++---------- 2 files changed, 121 insertions(+), 33 deletions(-) diff --git a/include/zephyr/net/lwm2m.h b/include/zephyr/net/lwm2m.h index 0de08404cbd..c667876f966 100644 --- a/include/zephyr/net/lwm2m.h +++ b/include/zephyr/net/lwm2m.h @@ -644,6 +644,8 @@ int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const /** * @brief Create an LwM2M object instance. * + * @deprecated Use lwm2m_create_obj_inst() instead. + * * LwM2M clients use this function to create non-default LwM2M objects: * Example to create first temperature sensor object: * lwm2m_engine_create_obj_inst("3303/0"); @@ -652,19 +654,47 @@ int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const * * @return 0 for success or negative in case of error. */ +__deprecated int lwm2m_engine_create_obj_inst(const char *pathstr); +/** + * @brief Create an LwM2M object instance. + * + * LwM2M clients use this function to create non-default LwM2M objects: + * Example to create first temperature sensor object: + * lwm2m_create_obj_inst(&LWM2M_OBJ(3303, 0)); + * + * @param[in] path LwM2M path as a struct + * + * @return 0 for success or negative in case of error. + */ +int lwm2m_create_object_inst(const struct lwm2m_obj_path *path); + /** * @brief Delete an LwM2M object instance. * + * @deprecated Use lwm2m_delete_obj_inst() instead. + * * LwM2M clients use this function to delete LwM2M objects. * * @param[in] pathstr LwM2M path string "obj/obj-inst" * * @return 0 for success or negative in case of error. */ +__deprecated int lwm2m_engine_delete_obj_inst(const char *pathstr); +/** + * @brief Delete an LwM2M object instance. + * + * LwM2M clients use this function to delete LwM2M objects. + * + * @param[in] path LwM2M path as a struct + * + * @return 0 for success or negative in case of error. + */ +int lwm2m_delete_object_inst(const struct lwm2m_obj_path *path); + /** * @brief Locks the registry for this thread. * @@ -1553,6 +1583,8 @@ int lwm2m_engine_get_res_data(const char *pathstr, void **data_ptr, uint16_t *da /** * @brief Create a resource instance * + * @deprecated Use lwm2m_create_res_inst() instead. + * * LwM2M clients use this function to create multi-resource instances: * Example to create 0 instance of device available power sources: * lwm2m_engine_create_res_inst("3/0/6/0"); @@ -1561,19 +1593,47 @@ int lwm2m_engine_get_res_data(const char *pathstr, void **data_ptr, uint16_t *da * * @return 0 for success or negative in case of error. */ +__deprecated int lwm2m_engine_create_res_inst(const char *pathstr); +/** + * @brief Create a resource instance + * + * LwM2M clients use this function to create multi-resource instances: + * Example to create 0 instance of device available power sources: + * lwm2m_create_res_inst(&LWM2M_OBJ(3, 0, 6, 0)); + * + * @param[in] path LwM2M path as a struct + * + * @return 0 for success or negative in case of error. + */ +int lwm2m_create_res_inst(const struct lwm2m_obj_path *path); + /** * @brief Delete a resource instance * + * @deprecated Use lwm2m_delete_res_inst() instead. + * * Use this function to remove an existing resource instance * * @param[in] pathstr LwM2M path string "obj/obj-inst/res/res-inst" * * @return 0 for success or negative in case of error. */ +__deprecated int lwm2m_engine_delete_res_inst(const char *pathstr); +/** + * @brief Delete a resource instance + * + * Use this function to remove an existing resource instance + * + * @param[in] path LwM2M path as a struct + * + * @return 0 for success or negative in case of error. + */ +int lwm2m_delete_res_inst(const struct lwm2m_obj_path *path); + /** * @brief Update the period of a given service. * diff --git a/subsys/net/lib/lwm2m/lwm2m_registry.c b/subsys/net/lib/lwm2m/lwm2m_registry.c index fd434ba0183..2ef0e12c741 100644 --- a/subsys/net/lib/lwm2m/lwm2m_registry.c +++ b/subsys/net/lib/lwm2m/lwm2m_registry.c @@ -282,10 +282,29 @@ int lwm2m_delete_obj_inst(uint16_t obj_id, uint16_t obj_inst_id) return ret; } +int lwm2m_create_object_inst(const struct lwm2m_obj_path *path) +{ + struct lwm2m_engine_obj_inst *obj_inst; + int ret = 0; + + if (path->level != LWM2M_PATH_LEVEL_OBJECT_INST) { + LOG_ERR("path must have 2 parts"); + return -EINVAL; + } + + ret = lwm2m_create_obj_inst(path->obj_id, path->obj_inst_id, &obj_inst); + if (ret < 0) { + return ret; + } + + engine_trigger_update(true); + + return 0; +} + int lwm2m_engine_create_obj_inst(const char *pathstr) { struct lwm2m_obj_path path; - struct lwm2m_engine_obj_inst *obj_inst; int ret = 0; LOG_DBG("path:%s", pathstr); @@ -296,12 +315,19 @@ int lwm2m_engine_create_obj_inst(const char *pathstr) return ret; } - if (path.level != 2U) { + return lwm2m_create_object_inst(&path); +} + +int lwm2m_delete_object_inst(const struct lwm2m_obj_path *path) +{ + int ret = 0; + + if (path->level != LWM2M_PATH_LEVEL_OBJECT_INST) { LOG_ERR("path must have 2 parts"); return -EINVAL; } - ret = lwm2m_create_obj_inst(path.obj_id, path.obj_inst_id, &obj_inst); + ret = lwm2m_delete_obj_inst(path->obj_id, path->obj_inst_id); if (ret < 0) { return ret; } @@ -324,19 +350,7 @@ int lwm2m_engine_delete_obj_inst(const char *pathstr) return ret; } - if (path.level != 2U) { - LOG_ERR("path must have 2 parts"); - return -EINVAL; - } - - ret = lwm2m_delete_obj_inst(path.obj_id, path.obj_inst_id); - if (ret < 0) { - return ret; - } - - engine_trigger_update(true); - - return 0; + return lwm2m_delete_object_inst(&path); } struct lwm2m_engine_obj_inst *lwm2m_engine_get_obj_inst(const struct lwm2m_obj_path *path) @@ -1525,48 +1539,41 @@ int lwm2m_engine_get_create_res_inst(struct lwm2m_obj_path *path, struct lwm2m_e return 0; } -int lwm2m_engine_create_res_inst(const char *pathstr) +int lwm2m_create_res_inst(const struct lwm2m_obj_path *path) { int ret; struct lwm2m_engine_res *res = NULL; struct lwm2m_engine_res_inst *res_inst = NULL; - struct lwm2m_obj_path path; - ret = lwm2m_string_to_path(pathstr, &path, '/'); - if (ret < 0) { - return ret; - } - - if (path.level < LWM2M_PATH_LEVEL_RESOURCE_INST) { + if (path->level < LWM2M_PATH_LEVEL_RESOURCE_INST) { LOG_ERR("path must have 4 parts"); return -EINVAL; } k_mutex_lock(®istry_lock, K_FOREVER); - ret = path_to_objs(&path, NULL, NULL, &res, &res_inst); + ret = path_to_objs(path, NULL, NULL, &res, &res_inst); if (ret < 0) { k_mutex_unlock(®istry_lock); return ret; } if (!res) { - LOG_ERR("resource %u not found", path.res_id); + LOG_ERR("resource %u not found", path->res_id); k_mutex_unlock(®istry_lock); return -ENOENT; } if (res_inst && res_inst->res_inst_id != RES_INSTANCE_NOT_CREATED) { - LOG_ERR("res instance %u already exists", path.res_inst_id); + LOG_ERR("res instance %u already exists", path->res_inst_id); k_mutex_unlock(®istry_lock); return -EINVAL; } k_mutex_unlock(®istry_lock); - return lwm2m_engine_allocate_resource_instance(res, &res_inst, path.res_inst_id); + return lwm2m_engine_allocate_resource_instance(res, &res_inst, path->res_inst_id); } -int lwm2m_engine_delete_res_inst(const char *pathstr) +int lwm2m_engine_create_res_inst(const char *pathstr) { int ret; - struct lwm2m_engine_res_inst *res_inst = NULL; struct lwm2m_obj_path path; ret = lwm2m_string_to_path(pathstr, &path, '/'); @@ -1574,19 +1581,27 @@ int lwm2m_engine_delete_res_inst(const char *pathstr) return ret; } - if (path.level < LWM2M_PATH_LEVEL_RESOURCE_INST) { + return lwm2m_create_res_inst(&path); +} + +int lwm2m_delete_res_inst(const struct lwm2m_obj_path *path) +{ + int ret; + struct lwm2m_engine_res_inst *res_inst = NULL; + + if (path->level < LWM2M_PATH_LEVEL_RESOURCE_INST) { LOG_ERR("path must have 4 parts"); return -EINVAL; } k_mutex_lock(®istry_lock, K_FOREVER); - ret = path_to_objs(&path, NULL, NULL, NULL, &res_inst); + ret = path_to_objs(path, NULL, NULL, NULL, &res_inst); if (ret < 0) { k_mutex_unlock(®istry_lock); return ret; } if (!res_inst) { - LOG_ERR("res instance %u not found", path.res_inst_id); + LOG_ERR("res instance %u not found", path->res_inst_id); k_mutex_unlock(®istry_lock); return -ENOENT; } @@ -1598,6 +1613,19 @@ int lwm2m_engine_delete_res_inst(const char *pathstr) k_mutex_unlock(®istry_lock); return 0; } + +int lwm2m_engine_delete_res_inst(const char *pathstr) +{ + int ret; + struct lwm2m_obj_path path; + + ret = lwm2m_string_to_path(pathstr, &path, '/'); + if (ret < 0) { + return ret; + } + + return lwm2m_delete_res_inst(&path); +} /* Register callbacks */ int lwm2m_engine_register_read_callback(const char *pathstr, lwm2m_engine_get_data_cb_t cb)