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 <robert.ch.chou@acer.com>
This commit is contained in:
Robert Chou 2018-02-05 16:05:51 +08:00 committed by Anas Nashif
commit 228bf05af6

View file

@ -1133,7 +1133,7 @@ u16_t lwm2m_get_rd_data(u8_t *client_data, u16_t size)
/* input / output selection */ /* 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) { switch (accept) {
@ -1159,15 +1159,12 @@ static u16_t select_writer(struct lwm2m_output_context *out, u16_t accept)
#endif #endif
default: default:
SYS_LOG_ERR("Unknown Accept type %u, using LWM2M plain text", SYS_LOG_WRN("Unknown content type %u", accept);
accept); return -ENOMSG;
out->writer = &plain_text_writer;
accept = LWM2M_FORMAT_PLAIN_TEXT;
break;
} }
return accept; return 0;
} }
static int select_reader(struct lwm2m_input_context *in, u16_t format) 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 -ENOMSG;
} }
return format; return 0;
} }
/* user data setter functions */ /* 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, r = coap_find_options(in.in_cpkt, COAP_OPTION_CONTENT_FORMAT,
options, 1); options, 1);
if (r > 0) { 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); r = coap_find_options(in.in_cpkt, COAP_OPTION_ACCEPT, options, 1);
if (r > 0) { if (r > 0) {
accept = coap_option_value_to_int(&options[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; accept = LWM2M_FORMAT_OMA_TLV;
} }
r = select_writer(&out, accept);
if (r < 0) {
goto error;
}
if (!well_known) { if (!well_known) {
/* find registered obj */ /* find registered obj */
obj = get_engine_obj(path.obj_id); 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 */ /* set the operation */
switch (code & COAP_REQUEST_MASK) { switch (code & COAP_REQUEST_MASK) {