net: lib: lwm2m: Deprecate object and resource API

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ä <jarno.lamsa@nordicsemi.no>
This commit is contained in:
Jarno Lämsä 2023-01-04 13:30:43 +02:00 committed by Carles Cufí
commit 5c80be9379
2 changed files with 121 additions and 33 deletions

View file

@ -644,6 +644,8 @@ int lwm2m_engine_update_observer_max_period(struct lwm2m_ctx *client_ctx, const
/** /**
* @brief Create an LwM2M object instance. * @brief Create an LwM2M object instance.
* *
* @deprecated Use lwm2m_create_obj_inst() instead.
*
* LwM2M clients use this function to create non-default LwM2M objects: * LwM2M clients use this function to create non-default LwM2M objects:
* Example to create first temperature sensor object: * Example to create first temperature sensor object:
* lwm2m_engine_create_obj_inst("3303/0"); * 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. * @return 0 for success or negative in case of error.
*/ */
__deprecated
int lwm2m_engine_create_obj_inst(const char *pathstr); 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. * @brief Delete an LwM2M object instance.
* *
* @deprecated Use lwm2m_delete_obj_inst() instead.
*
* LwM2M clients use this function to delete LwM2M objects. * LwM2M clients use this function to delete LwM2M objects.
* *
* @param[in] pathstr LwM2M path string "obj/obj-inst" * @param[in] pathstr LwM2M path string "obj/obj-inst"
* *
* @return 0 for success or negative in case of error. * @return 0 for success or negative in case of error.
*/ */
__deprecated
int lwm2m_engine_delete_obj_inst(const char *pathstr); 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. * @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 * @brief Create a resource instance
* *
* @deprecated Use lwm2m_create_res_inst() instead.
*
* LwM2M clients use this function to create multi-resource instances: * LwM2M clients use this function to create multi-resource instances:
* Example to create 0 instance of device available power sources: * Example to create 0 instance of device available power sources:
* lwm2m_engine_create_res_inst("3/0/6/0"); * 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. * @return 0 for success or negative in case of error.
*/ */
__deprecated
int lwm2m_engine_create_res_inst(const char *pathstr); 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 * @brief Delete a resource instance
* *
* @deprecated Use lwm2m_delete_res_inst() instead.
*
* Use this function to remove an existing resource instance * Use this function to remove an existing resource instance
* *
* @param[in] pathstr LwM2M path string "obj/obj-inst/res/res-inst" * @param[in] pathstr LwM2M path string "obj/obj-inst/res/res-inst"
* *
* @return 0 for success or negative in case of error. * @return 0 for success or negative in case of error.
*/ */
__deprecated
int lwm2m_engine_delete_res_inst(const char *pathstr); 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. * @brief Update the period of a given service.
* *

View file

@ -282,10 +282,29 @@ int lwm2m_delete_obj_inst(uint16_t obj_id, uint16_t obj_inst_id)
return ret; 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) int lwm2m_engine_create_obj_inst(const char *pathstr)
{ {
struct lwm2m_obj_path path; struct lwm2m_obj_path path;
struct lwm2m_engine_obj_inst *obj_inst;
int ret = 0; int ret = 0;
LOG_DBG("path:%s", pathstr); LOG_DBG("path:%s", pathstr);
@ -296,12 +315,19 @@ int lwm2m_engine_create_obj_inst(const char *pathstr)
return ret; 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"); LOG_ERR("path must have 2 parts");
return -EINVAL; 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) { if (ret < 0) {
return ret; return ret;
} }
@ -324,19 +350,7 @@ int lwm2m_engine_delete_obj_inst(const char *pathstr)
return ret; return ret;
} }
if (path.level != 2U) { return lwm2m_delete_object_inst(&path);
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;
} }
struct lwm2m_engine_obj_inst *lwm2m_engine_get_obj_inst(const struct lwm2m_obj_path *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; return 0;
} }
int lwm2m_engine_create_res_inst(const char *pathstr) int lwm2m_create_res_inst(const struct lwm2m_obj_path *path)
{ {
int ret; int ret;
struct lwm2m_engine_res *res = NULL; struct lwm2m_engine_res *res = NULL;
struct lwm2m_engine_res_inst *res_inst = NULL; struct lwm2m_engine_res_inst *res_inst = NULL;
struct lwm2m_obj_path path;
ret = lwm2m_string_to_path(pathstr, &path, '/'); if (path->level < LWM2M_PATH_LEVEL_RESOURCE_INST) {
if (ret < 0) {
return ret;
}
if (path.level < LWM2M_PATH_LEVEL_RESOURCE_INST) {
LOG_ERR("path must have 4 parts"); LOG_ERR("path must have 4 parts");
return -EINVAL; return -EINVAL;
} }
k_mutex_lock(&registry_lock, K_FOREVER); k_mutex_lock(&registry_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) { if (ret < 0) {
k_mutex_unlock(&registry_lock); k_mutex_unlock(&registry_lock);
return ret; return ret;
} }
if (!res) { if (!res) {
LOG_ERR("resource %u not found", path.res_id); LOG_ERR("resource %u not found", path->res_id);
k_mutex_unlock(&registry_lock); k_mutex_unlock(&registry_lock);
return -ENOENT; return -ENOENT;
} }
if (res_inst && res_inst->res_inst_id != RES_INSTANCE_NOT_CREATED) { 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(&registry_lock); k_mutex_unlock(&registry_lock);
return -EINVAL; return -EINVAL;
} }
k_mutex_unlock(&registry_lock); k_mutex_unlock(&registry_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; int ret;
struct lwm2m_engine_res_inst *res_inst = NULL;
struct lwm2m_obj_path path; struct lwm2m_obj_path path;
ret = lwm2m_string_to_path(pathstr, &path, '/'); ret = lwm2m_string_to_path(pathstr, &path, '/');
@ -1574,19 +1581,27 @@ int lwm2m_engine_delete_res_inst(const char *pathstr)
return ret; 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"); LOG_ERR("path must have 4 parts");
return -EINVAL; return -EINVAL;
} }
k_mutex_lock(&registry_lock, K_FOREVER); k_mutex_lock(&registry_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) { if (ret < 0) {
k_mutex_unlock(&registry_lock); k_mutex_unlock(&registry_lock);
return ret; return ret;
} }
if (!res_inst) { 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(&registry_lock); k_mutex_unlock(&registry_lock);
return -ENOENT; return -ENOENT;
} }
@ -1598,6 +1613,19 @@ int lwm2m_engine_delete_res_inst(const char *pathstr)
k_mutex_unlock(&registry_lock); k_mutex_unlock(&registry_lock);
return 0; 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 */ /* Register callbacks */
int lwm2m_engine_register_read_callback(const char *pathstr, lwm2m_engine_get_data_cb_t cb) int lwm2m_engine_register_read_callback(const char *pathstr, lwm2m_engine_get_data_cb_t cb)