net: lib: lwm2m: Deprecate string based enable cache

Deprecate the old API and replace with new one which uses
the lwm2m_obj_path struct instead of a string.

Signed-off-by: Jarno Lämsä <jarno.lamsa@nordicsemi.no>
This commit is contained in:
Jarno Lämsä 2023-01-17 13:20:03 +02:00 committed by Carles Cufí
commit 3b3463ecba
2 changed files with 50 additions and 18 deletions

View file

@ -2127,18 +2127,37 @@ struct lwm2m_ctx *lwm2m_rd_client_ctx(void);
/**  /** 
* @brief Enable data cache for a resource. * @brief Enable data cache for a resource.
* *
* @deprecated Use lwm2m_enable_cache instead
*
* Application may enable caching of resource data by allocating buffer for LwM2M engine to use. * Application may enable caching of resource data by allocating buffer for LwM2M engine to use.
* Buffer must be size of struct @ref lwm2m_time_series_elem times cache_len * Buffer must be size of struct @ref lwm2m_time_series_elem times cache_len
* *
* @param resource_path LwM2M resourcepath string "obj/obj-inst/res(/res-inst)" * @param resource_path LwM2M resourcepath string "obj/obj-inst/res(/res-inst)"
* @param data_cache Pointer to Data cache array * @param data_cache Pointer to Data cache array
* @param cache_len number of cached entries * @param cache_len number of cached entries
*  *
* @return 0 for success or negative in case of error. * @return 0 for success or negative in case of error.
* *
*/ */
__deprecated
int lwm2m_engine_enable_cache(char const *resource_path, struct lwm2m_time_series_elem *data_cache, int lwm2m_engine_enable_cache(char const *resource_path, struct lwm2m_time_series_elem *data_cache,
size_t cache_len); size_t cache_len);
/** 
* @brief Enable data cache for a resource.
*
* Application may enable caching of resource data by allocating buffer for LwM2M engine to use.
* Buffer must be size of struct @ref lwm2m_time_series_elem times cache_len
*
* @param path LwM2M path to resource as a struct
* @param data_cache Pointer to Data cache array
* @param cache_len number of cached entries
*
* @return 0 for success or negative in case of error.
*
*/
int lwm2m_enable_cache(struct lwm2m_obj_path *path, struct lwm2m_time_series_elem *data_cache,
size_t cache_len);
#endif /* ZEPHYR_INCLUDE_NET_LWM2M_H_ */ #endif /* ZEPHYR_INCLUDE_NET_LWM2M_H_ */
/**@} */ /**@} */

View file

@ -2073,11 +2073,10 @@ struct lwm2m_time_series_resource *lwm2m_cache_entry_get_by_object(struct lwm2m_
} }
int lwm2m_engine_enable_cache(char const *resource_path, struct lwm2m_time_series_elem *data_cache, int lwm2m_enable_cache(struct lwm2m_obj_path *path, struct lwm2m_time_series_elem *data_cache,
size_t cache_len) size_t cache_len)
{ {
#if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT) #if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT)
struct lwm2m_obj_path path;
struct lwm2m_engine_obj_inst *obj_inst; struct lwm2m_engine_obj_inst *obj_inst;
struct lwm2m_engine_obj_field *obj_field; struct lwm2m_engine_obj_field *obj_field;
struct lwm2m_engine_res_inst *res_inst = NULL; struct lwm2m_engine_res_inst *res_inst = NULL;
@ -2085,25 +2084,14 @@ int lwm2m_engine_enable_cache(char const *resource_path, struct lwm2m_time_serie
int ret = 0; int ret = 0;
size_t cache_entry_size = sizeof(struct lwm2m_time_series_elem); size_t cache_entry_size = sizeof(struct lwm2m_time_series_elem);
/* translate path -> path_obj */
ret = lwm2m_string_to_path(resource_path, &path, '/');
if (ret < 0) {
return ret;
}
if (path.level < LWM2M_PATH_LEVEL_RESOURCE) {
LOG_ERR("path must have at least 3 parts");
return -EINVAL;
}
/* look up resource obj */ /* look up resource obj */
ret = path_to_objs(&path, &obj_inst, &obj_field, NULL, &res_inst); ret = path_to_objs(path, &obj_inst, &obj_field, NULL, &res_inst);
if (ret < 0) { if (ret < 0) {
return ret; return ret;
} }
if (!res_inst) { if (!res_inst) {
LOG_ERR("res instance %d not found", path.res_inst_id); LOG_ERR("res instance %d not found", path->res_inst_id);
return -ENOENT; return -ENOENT;
} }
@ -2119,7 +2107,7 @@ int lwm2m_engine_enable_cache(char const *resource_path, struct lwm2m_time_serie
case LWM2M_RES_TYPE_BOOL: case LWM2M_RES_TYPE_BOOL:
case LWM2M_RES_TYPE_FLOAT: case LWM2M_RES_TYPE_FLOAT:
/* Support only fixed width resource types */ /* Support only fixed width resource types */
cache_entry = lwm2m_cache_entry_allocate(&path); cache_entry = lwm2m_cache_entry_allocate(path);
break; break;
default: default:
cache_entry = NULL; cache_entry = NULL;
@ -2140,6 +2128,31 @@ int lwm2m_engine_enable_cache(char const *resource_path, struct lwm2m_time_serie
#endif /* CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT */ #endif /* CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT */
} }
int lwm2m_engine_enable_cache(char const *resource_path, struct lwm2m_time_series_elem *data_cache,
size_t cache_len)
{
#if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT)
struct lwm2m_obj_path path;
/* translate path -> path_obj */
ret = lwm2m_string_to_path(resource_path, &path, '/');
if (ret < 0) {
return ret;
}
if (path.level < LWM2M_PATH_LEVEL_RESOURCE) {
LOG_ERR("path must have at least 3 parts");
return -EINVAL;
}
return lwm2m_enable_cache(&path, data_cache, cache_len);
#else
LOG_ERR("LwM2M resource cache is only supported for "
"CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT");
return -ENOTSUP;
#endif /* CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT */
}
int lwm2m_engine_data_cache_init(void) int lwm2m_engine_data_cache_init(void)
{ {
#if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT) #if defined(CONFIG_LWM2M_RESOURCE_DATA_CACHE_SUPPORT)