net: lwm2m: refactor engine_get_obj to be more useful

Eliminate several similar code-blocks by replacing
engine_get_resource() with a more useful function called:
path_to_objs()

By supplying an lwm2m_obj_path object, it will find and set
the related values for:
struct lwm2m_engine_obj_inst
struct lwm2m_engine_obj_field
struct lwm2m_engine_res_inst

NOTE: NULLs can be supplied where the returned value is not
important.

Signed-off-by: Michael Scott <michael@opensourcefoundries.com>
This commit is contained in:
Michael Scott 2018-04-30 15:02:04 -07:00 committed by Jukka Rissanen
commit 3e16be3c3c

View file

@ -1257,6 +1257,65 @@ static int string_to_path(char *pathstr, struct lwm2m_obj_path *path,
return 0;
}
static int path_to_objs(const struct lwm2m_obj_path *path,
struct lwm2m_engine_obj_inst **obj_inst,
struct lwm2m_engine_obj_field **obj_field,
struct lwm2m_engine_res_inst **res)
{
struct lwm2m_engine_obj_inst *oi;
struct lwm2m_engine_obj_field *of;
struct lwm2m_engine_res_inst *r = NULL;
int i;
if (!path) {
return -EINVAL;
}
oi = get_engine_obj_inst(path->obj_id, path->obj_inst_id);
if (!oi) {
SYS_LOG_ERR("obj instance %d/%d not found",
path->obj_id, path->obj_inst_id);
return -ENOENT;
}
if (!oi->resources || oi->resource_count == 0) {
SYS_LOG_ERR("obj instance has no resources");
return -EINVAL;
}
of = lwm2m_get_engine_obj_field(oi->obj, path->res_id);
if (!of) {
SYS_LOG_ERR("obj field %d not found", path->res_id);
return -ENOENT;
}
for (i = 0; i < oi->resource_count; i++) {
if (oi->resources[i].res_id == path->res_id) {
r = &oi->resources[i];
break;
}
}
if (!r) {
SYS_LOG_ERR("res instance %d not found", path->res_id);
return -ENOENT;
}
if (obj_inst) {
*obj_inst = oi;
}
if (obj_field) {
*obj_field = of;
}
if (res) {
*res = r;
}
return 0;
}
int lwm2m_engine_create_obj_inst(char *pathstr)
{
struct lwm2m_obj_path path;
@ -1282,14 +1341,14 @@ int lwm2m_engine_create_obj_inst(char *pathstr)
static int lwm2m_engine_set(char *pathstr, void *value, u16_t len)
{
int ret = 0, i;
struct lwm2m_obj_path path;
struct lwm2m_engine_obj_inst *obj_inst;
struct lwm2m_engine_obj_field *obj_field;
struct lwm2m_engine_res_inst *res = NULL;
bool changed = false;
void *data_ptr = NULL;
size_t data_len = 0;
int ret = 0;
bool changed = false;
SYS_LOG_DBG("path:%s, value:%p, len:%d", pathstr, value, len);
@ -1305,30 +1364,10 @@ static int lwm2m_engine_set(char *pathstr, void *value, u16_t len)
return -EINVAL;
}
/* find obj_inst/res_id */
obj_inst = get_engine_obj_inst(path.obj_id, path.obj_inst_id);
if (!obj_inst) {
SYS_LOG_ERR("obj instance %d/%d not found",
path.obj_id, path.obj_inst_id);
return -ENOENT;
}
if (!obj_inst->resources || obj_inst->resource_count == 0) {
SYS_LOG_ERR("obj instance has no resources");
return -EINVAL;
}
obj_field = lwm2m_get_engine_obj_field(obj_inst->obj, path.res_id);
if (!obj_field) {
SYS_LOG_ERR("obj field %d not found", path.res_id);
return -ENOENT;
}
for (i = 0; i < obj_inst->resource_count; i++) {
if (obj_inst->resources[i].res_id == path.res_id) {
res = &obj_inst->resources[i];
break;
}
/* look up resource obj */
ret = path_to_objs(&path, &obj_inst, &obj_field, &res);
if (ret < 0) {
return ret;
}
if (!res) {
@ -1514,7 +1553,7 @@ int lwm2m_engine_set_float64(char *pathstr, float64_value_t *value)
static int lwm2m_engine_get(char *pathstr, void *buf, u16_t buflen)
{
int ret = 0, i;
int ret = 0;
struct lwm2m_obj_path path;
struct lwm2m_engine_obj_inst *obj_inst;
struct lwm2m_engine_obj_field *obj_field;
@ -1536,30 +1575,10 @@ static int lwm2m_engine_get(char *pathstr, void *buf, u16_t buflen)
return -EINVAL;
}
/* find obj_inst/res_id */
obj_inst = get_engine_obj_inst(path.obj_id, path.obj_inst_id);
if (!obj_inst) {
SYS_LOG_ERR("obj instance %d/%d not found",
path.obj_id, path.obj_inst_id);
return -ENOENT;
}
if (!obj_inst->resources || obj_inst->resource_count == 0) {
SYS_LOG_ERR("obj instance has no resources");
return -EINVAL;
}
obj_field = lwm2m_get_engine_obj_field(obj_inst->obj, path.res_id);
if (!obj_field) {
SYS_LOG_ERR("obj field %d not found", path.res_id);
return -ENOENT;
}
for (i = 0; i < obj_inst->resource_count; i++) {
if (obj_inst->resources[i].res_id == path.res_id) {
res = &obj_inst->resources[i];
break;
}
/* look up resource obj */
ret = path_to_objs(&path, &obj_inst, &obj_field, &res);
if (ret < 0) {
return ret;
}
if (!res) {
@ -1744,45 +1763,6 @@ int lwm2m_engine_get_float64(char *pathstr, float64_value_t *buf)
return lwm2m_engine_get(pathstr, buf, sizeof(float64_value_t));
}
/* user callback functions */
static int engine_get_resource(struct lwm2m_obj_path *path,
struct lwm2m_engine_res_inst **res)
{
int i;
struct lwm2m_engine_obj_inst *obj_inst;
if (!path) {
return -EINVAL;
}
/* find obj_inst/res_id */
obj_inst = get_engine_obj_inst(path->obj_id, path->obj_inst_id);
if (!obj_inst) {
SYS_LOG_ERR("obj instance %d/%d not found",
path->obj_id, path->obj_inst_id);
return -ENOENT;
}
if (!obj_inst->resources || obj_inst->resource_count == 0) {
SYS_LOG_ERR("obj instance has no resources");
return -EINVAL;
}
for (i = 0; i < obj_inst->resource_count; i++) {
if (obj_inst->resources[i].res_id == path->res_id) {
*res = &obj_inst->resources[i];
break;
}
}
if (!*res) {
SYS_LOG_ERR("res instance %d not found", path->res_id);
return -ENOENT;
}
return 0;
}
int lwm2m_engine_get_resource(char *pathstr, struct lwm2m_engine_res_inst **res)
{
int ret;
@ -1799,7 +1779,7 @@ int lwm2m_engine_get_resource(char *pathstr, struct lwm2m_engine_res_inst **res)
return -EINVAL;
}
return engine_get_resource(&path, res);
return path_to_objs(&path, NULL, NULL, res);
}
int lwm2m_engine_register_read_callback(char *pathstr,
@ -2259,7 +2239,7 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
/* get lwm2m_attr slist */
if (path->level == 3) {
ret = engine_get_resource(path, &res);
ret = path_to_objs(path, NULL, NULL, &res);
if (ret < 0) {
return ret;
}
@ -2517,7 +2497,8 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
}
if (!res || res->res_id != obs->path.res_id) {
ret = engine_get_resource(&obs->path, &res);
ret = path_to_objs(&obs->path, NULL, NULL,
&res);
if (ret < 0) {
return ret;
}
@ -2553,12 +2534,7 @@ static int lwm2m_exec_handler(struct lwm2m_engine_obj *obj,
path = context->path;
obj_inst = get_engine_obj_inst(path->obj_id, path->obj_inst_id);
if (!obj_inst) {
return -ENOENT;
}
ret = engine_get_resource(path, &res);
ret = path_to_objs(path, &obj_inst, NULL, &res);
if (ret < 0) {
return ret;
}