From 280f159b67bc0f6d8b2823fab6597052be9eb603 Mon Sep 17 00:00:00 2001 From: Michael Scott Date: Mon, 27 Aug 2018 17:00:17 -0700 Subject: [PATCH] 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 --- subsys/net/lib/lwm2m/lwm2m_engine.c | 31 ++++++++++++++++++----------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 28743d0efd7..e953ea51437 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -683,18 +683,20 @@ static struct lwm2m_engine_obj_inst *get_engine_obj_inst(int obj_id, } static struct lwm2m_engine_obj_inst * -next_engine_obj_inst(struct lwm2m_engine_obj_inst *last, - int obj_id, int obj_inst_id) +next_engine_obj_inst(int obj_id, int obj_inst_id) { - while (last) { - last = SYS_SLIST_PEEK_NEXT_CONTAINER(last, node); - if (last && last->obj->obj_id == obj_id && - last->obj_inst_id == obj_inst_id) { - return last; + struct lwm2m_engine_obj_inst *obj_inst, *next = NULL; + + SYS_SLIST_FOR_EACH_CONTAINER(&engine_obj_inst_list, obj_inst, + node) { + 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, @@ -2738,7 +2740,7 @@ static int do_read_op(struct lwm2m_engine_obj *obj, while (obj_inst) { 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 */ @@ -2804,9 +2806,14 @@ static int do_read_op(struct lwm2m_engine_obj *obj, engine_put_end(out, path); } - /* advance to the next object instance */ - obj_inst = next_engine_obj_inst(obj_inst, path->obj_id, - path->obj_inst_id); +move_forward: + if (path->level <= 1) { + /* 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 */