From 228bf05af6a863b6da10212ca858f9d91e71994e Mon Sep 17 00:00:00 2001 From: Robert Chou Date: Mon, 5 Feb 2018 16:05:51 +0800 Subject: [PATCH] net: lwm2m: update internal API select_writer() and select_reader() As per review of PR #5893, this is a follow up patch to update select_writer() API to match the behavior of select_reader() API. Quote from OMA-TS-LightweightM2M-V1_0_1-20170704-A. 8.2.5 "An Object Instance or Resource is Read by sending a CoAP GET to the corresponding path. The response includes the value in the corresponding Plain Text, Opaque, TLV or JSON format according to the specified Content-Format (see section 6.4).The request MAY specify an Accept option containing the preferred Content-Format to receive. When the specified Content-Format is not supported by the LwM2M Client, the request MUST be rejected." Therefore, we do not attempt to assign a content-format when the requested one is not supported. Instead, we return an error code. 0 is returned when reader or writer has been successfully selected by select_reader() or select_writer() Signed-off-by: Robert Chou --- subsys/net/lib/lwm2m/lwm2m_engine.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/subsys/net/lib/lwm2m/lwm2m_engine.c b/subsys/net/lib/lwm2m/lwm2m_engine.c index 7b636e28fbf..920c7c265c2 100644 --- a/subsys/net/lib/lwm2m/lwm2m_engine.c +++ b/subsys/net/lib/lwm2m/lwm2m_engine.c @@ -1133,7 +1133,7 @@ u16_t lwm2m_get_rd_data(u8_t *client_data, u16_t size) /* input / output selection */ -static u16_t select_writer(struct lwm2m_output_context *out, u16_t accept) +static int select_writer(struct lwm2m_output_context *out, u16_t accept) { switch (accept) { @@ -1159,15 +1159,12 @@ static u16_t select_writer(struct lwm2m_output_context *out, u16_t accept) #endif default: - SYS_LOG_ERR("Unknown Accept type %u, using LWM2M plain text", - accept); - out->writer = &plain_text_writer; - accept = LWM2M_FORMAT_PLAIN_TEXT; - break; + SYS_LOG_WRN("Unknown content type %u", accept); + return -ENOMSG; } - return accept; + return 0; } static int select_reader(struct lwm2m_input_context *in, u16_t format) @@ -1190,7 +1187,7 @@ static int select_reader(struct lwm2m_input_context *in, u16_t format) return -ENOMSG; } - return format; + return 0; } /* user data setter functions */ @@ -3045,7 +3042,7 @@ static int handle_request(struct coap_packet *request, } } - /* read Content Format */ + /* read Content Format / setup in.reader */ r = coap_find_options(in.in_cpkt, COAP_OPTION_CONTENT_FORMAT, options, 1); if (r > 0) { @@ -3056,7 +3053,7 @@ static int handle_request(struct coap_packet *request, } } - /* read Accept */ + /* read Accept / setup out.writer */ r = coap_find_options(in.in_cpkt, COAP_OPTION_ACCEPT, options, 1); if (r > 0) { accept = coap_option_value_to_int(&options[0]); @@ -3065,6 +3062,11 @@ static int handle_request(struct coap_packet *request, accept = LWM2M_FORMAT_OMA_TLV; } + r = select_writer(&out, accept); + if (r < 0) { + goto error; + } + if (!well_known) { /* find registered obj */ obj = get_engine_obj(path.obj_id); @@ -3075,8 +3077,6 @@ static int handle_request(struct coap_packet *request, } } - accept = select_writer(&out, accept); - /* set the operation */ switch (code & COAP_REQUEST_MASK) {