net: lwm2m: fix logic for lwm2m_next_engine_obj_inst()

The object instance list isn't sorted by object instance id.  Let's
simplify this and fix the logic in lwm2m_next_engine_obj_inst() to make
sure that we always get the NEXT object instance by value of
obj_inst_id, not just the next object instance in the list.

NOTE: This change removes the "last" object instance pointer from the
parameters of lwm2m_next_engine_obj_inst().  Some of the logic to return
a NULL value for the end of the list has to be moved back into
do_read_op().

Signed-off-by: Michael Scott <mike@foundries.io>
This commit is contained in:
Michael Scott 2018-08-27 17:00:17 -07:00 committed by Anas Nashif
commit 280f159b67

View file

@ -683,18 +683,20 @@ static struct lwm2m_engine_obj_inst *get_engine_obj_inst(int obj_id,
} }
static struct lwm2m_engine_obj_inst * static struct lwm2m_engine_obj_inst *
next_engine_obj_inst(struct lwm2m_engine_obj_inst *last, next_engine_obj_inst(int obj_id, int obj_inst_id)
int obj_id, int obj_inst_id)
{ {
while (last) { struct lwm2m_engine_obj_inst *obj_inst, *next = NULL;
last = SYS_SLIST_PEEK_NEXT_CONTAINER(last, node);
if (last && last->obj->obj_id == obj_id && SYS_SLIST_FOR_EACH_CONTAINER(&engine_obj_inst_list, obj_inst,
last->obj_inst_id == obj_inst_id) { node) {
return last; if (obj_inst->obj->obj_id == obj_id &&
obj_inst->obj_inst_id > obj_inst_id &&
(!next || next->obj_inst_id > obj_inst->obj_inst_id)) {
next = obj_inst;
} }
} }
return NULL; return next;
} }
int lwm2m_create_obj_inst(u16_t obj_id, u16_t obj_inst_id, int lwm2m_create_obj_inst(u16_t obj_id, u16_t obj_inst_id,
@ -2738,7 +2740,7 @@ static int do_read_op(struct lwm2m_engine_obj *obj,
while (obj_inst) { while (obj_inst) {
if (!obj_inst->resources || obj_inst->resource_count == 0) { if (!obj_inst->resources || obj_inst->resource_count == 0) {
continue; goto move_forward;
} }
/* save path's res_id because we may need to change it below */ /* save path's res_id because we may need to change it below */
@ -2804,9 +2806,14 @@ static int do_read_op(struct lwm2m_engine_obj *obj,
engine_put_end(out, path); engine_put_end(out, path);
} }
/* advance to the next object instance */ move_forward:
obj_inst = next_engine_obj_inst(obj_inst, path->obj_id, if (path->level <= 1) {
path->obj_inst_id); /* advance to the next object instance */
obj_inst = next_engine_obj_inst(path->obj_id,
obj_inst->obj_inst_id);
} else {
obj_inst = NULL;
}
} }
/* did not read anything even if we should have - on single item */ /* did not read anything even if we should have - on single item */