Skip to content

Commit

Permalink
net: lwm2m: Composite Observation refactor
Browse files Browse the repository at this point in the history
Json library parser modify data so thats why we can't parse same
data again. Now Composite observation handler parse SenML Json or
CBOR resource path which is given to new API composite Read API
which not need any new data parser.

Signed-off-by: Juha Heiskanen <[email protected]>
  • Loading branch information
Juha Heiskanen authored and carlescufi committed Jun 6, 2022
1 parent 2da8df8 commit 7ffc95c
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 20 deletions.
32 changes: 28 additions & 4 deletions subsys/net/lib/lwm2m/lwm2m_engine.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,8 @@ static int do_composite_observe_read_path_op(struct lwm2m_message *msg, uint16_t
sys_slist_t *lwm2m_path_list,
sys_slist_t *lwm2m_path_free_list);
static void lwm2m_engine_free_list(sys_slist_t *path_list, sys_slist_t *free_list);
static int do_composite_read_op_for_parsed_list(struct lwm2m_message *msg, uint16_t content_format,
sys_slist_t *path_list);

/* for debugging: to print IP addresses */
char *lwm2m_sprint_ip_addr(const struct sockaddr *addr)
Expand Down Expand Up @@ -912,7 +914,7 @@ static int engine_add_composite_observer(struct lwm2m_message *msg,
LOG_DBG("OBSERVER Composite DUPLICATE [%s]",
log_strdup(lwm2m_sprint_ip_addr(&msg->ctx->remote_addr)));

return 0;
return do_composite_read_op_for_parsed_list(msg, format, &lwm2m_path_list);
}

ret = engine_observe_attribute_list_get(&lwm2m_path_list, &attrs, msg->ctx->srv_obj_inst);
Expand All @@ -925,7 +927,7 @@ static int engine_add_composite_observer(struct lwm2m_message *msg,
return -ENOMEM;
}
engine_observe_node_init(obs, token, msg->ctx, tkl, format, attrs.pmax);
return 0;
return do_composite_read_op_for_parsed_list(msg, format, &lwm2m_path_list);
}

static void remove_observer_from_list(struct lwm2m_ctx *ctx, sys_snode_t *prev_node,
Expand Down Expand Up @@ -999,7 +1001,8 @@ static int engine_remove_composite_observer(struct lwm2m_message *msg, const uin

LOG_DBG("observer '%s' removed", log_strdup(sprint_token(token, tkl)));

return 0;
return do_composite_read_op_for_parsed_list(msg, format, &lwm2m_path_list);

}

#if defined(CONFIG_LOG)
Expand Down Expand Up @@ -3847,6 +3850,28 @@ static int do_composite_read_op(struct lwm2m_message *msg, uint16_t content_form
}
}

static int do_composite_read_op_for_parsed_list(struct lwm2m_message *msg, uint16_t content_format,
sys_slist_t *path_list)
{
switch (content_format) {

#if defined(CONFIG_LWM2M_RW_SENML_JSON_SUPPORT)
case LWM2M_FORMAT_APP_SEML_JSON:
return do_composite_read_op_for_parsed_list_senml_json(msg, path_list);
#endif

#if defined(CONFIG_LWM2M_RW_SENML_CBOR_SUPPORT)
case LWM2M_FORMAT_APP_SENML_CBOR:
return do_composite_read_op_for_parsed_path_senml_cbor(msg, path_list);
#endif

default:
LOG_ERR("Unsupported content-format: %u", content_format);
return -ENOMSG;

}
}

static int do_composite_observe_read_path_op(struct lwm2m_message *msg, uint16_t content_format,
sys_slist_t *lwm2m_path_list,
sys_slist_t *lwm2m_path_free_list)
Expand Down Expand Up @@ -4890,7 +4915,6 @@ static int handle_request(struct coap_packet *request,
if (r < 0) {
goto error;
}
r = do_composite_read_op(msg, accept);
}
} else {
if ((code & COAP_REQUEST_MASK) == COAP_METHOD_GET) {
Expand Down
23 changes: 15 additions & 8 deletions subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.c
Original file line number Diff line number Diff line change
Expand Up @@ -784,10 +784,23 @@ static uint8_t parse_composite_read_paths(struct lwm2m_message *msg,
return paths;
}

int do_composite_read_op_for_parsed_path_senml_cbor(struct lwm2m_message *msg,
sys_slist_t *lwm_path_list)
{
int ret;

setup_out_fmt_data(msg);

ret = lwm2m_perform_composite_read_op(msg, LWM2M_FORMAT_APP_SENML_CBOR, lwm_path_list);

clear_out_fmt_data(msg);

return ret;
}


int do_composite_read_op_senml_cbor(struct lwm2m_message *msg)
{
int ret;
struct lwm2m_obj_path_list lwm2m_path_list_buf[CONFIG_LWM2M_COMPOSITE_PATH_LIST_SIZE];
sys_slist_t lwm_path_list;
sys_slist_t lwm_path_free_list;
Expand All @@ -807,13 +820,7 @@ int do_composite_read_op_senml_cbor(struct lwm2m_message *msg)

lwm2m_engine_clear_duplicate_path(&lwm_path_list, &lwm_path_free_list);

setup_out_fmt_data(msg);

ret = lwm2m_perform_composite_read_op(msg, LWM2M_FORMAT_APP_SENML_CBOR, &lwm_path_list);

clear_out_fmt_data(msg);

return ret;
return do_composite_read_op_for_parsed_path_senml_cbor(msg, &lwm_path_free_list);
}


Expand Down
2 changes: 2 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_rw_senml_cbor.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ extern const struct lwm2m_reader senml_cbor_reader;

int do_read_op_senml_cbor(struct lwm2m_message *msg);
int do_composite_read_op_senml_cbor(struct lwm2m_message *msg);
int do_composite_read_op_for_parsed_path_senml_cbor(struct lwm2m_message *msg,
sys_slist_t *lwm_path_list);
int do_write_op_senml_cbor(struct lwm2m_message *msg);

int do_composite_observe_parse_path_senml_cbor(struct lwm2m_message *msg,
Expand Down
23 changes: 15 additions & 8 deletions subsys/net/lib/lwm2m/lwm2m_rw_senml_json.c
Original file line number Diff line number Diff line change
Expand Up @@ -1414,10 +1414,23 @@ static uint8_t json_parse_composite_read_paths(struct lwm2m_message *msg,
return valid_path_cnt;
}

int do_composite_read_op_senml_json(struct lwm2m_message *msg)
int do_composite_read_op_for_parsed_list_senml_json(struct lwm2m_message *msg,
sys_slist_t *path_list)
{
int ret;
struct json_out_formatter_data fd;

(void)memset(&fd, 0, sizeof(fd));
engine_set_out_user_data(&msg->out, &fd);

ret = lwm2m_perform_composite_read_op(msg, LWM2M_FORMAT_APP_SEML_JSON, path_list);
engine_clear_out_user_data(&msg->out);

return ret;
}

int do_composite_read_op_senml_json(struct lwm2m_message *msg)
{
struct lwm2m_obj_path_list path_list_buf[CONFIG_LWM2M_COMPOSITE_PATH_LIST_SIZE];
sys_slist_t path_list;
sys_slist_t free_list;
Expand All @@ -1437,13 +1450,7 @@ int do_composite_read_op_senml_json(struct lwm2m_message *msg)
/* Clear path which are part are part of recursive path /1 will include /1/0/1 */
lwm2m_engine_clear_duplicate_path(&path_list, &free_list);

(void)memset(&fd, 0, sizeof(fd));
engine_set_out_user_data(&msg->out, &fd);

ret = lwm2m_perform_composite_read_op(msg, LWM2M_FORMAT_APP_SEML_JSON, &path_list);
engine_clear_out_user_data(&msg->out);

return ret;
return do_composite_read_op_for_parsed_list_senml_json(msg, &path_list);
}

int do_send_op_senml_json(struct lwm2m_message *msg, sys_slist_t *lwm2m_path_list)
Expand Down
3 changes: 3 additions & 0 deletions subsys/net/lib/lwm2m/lwm2m_rw_senml_json.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ int do_write_op_senml_json(struct lwm2m_message *msg);
int do_send_op_senml_json(struct lwm2m_message *msg, sys_slist_t *lwm2m_path_list);
/* API for call composite READ from engine */
int do_composite_read_op_senml_json(struct lwm2m_message *msg);
/* API for call composite READ for parsed path list from engine */
int do_composite_read_op_for_parsed_list_senml_json(struct lwm2m_message *msg,
sys_slist_t *path_list);
/* API for call composite READ path list from engine */
int do_composite_observe_parse_path_senml_json(struct lwm2m_message *msg,
sys_slist_t *lwm2m_path_list,
Expand Down

0 comments on commit 7ffc95c

Please sign in to comment.