net: lwm2m: separate write operation from write-attributes op

Content-format is used to determine the type of the PUT/POST
request. Therefore, it's incorrect to assign default when the
caller does not include one in the request.

Define LWM2M_FORMAT_NONE=65535 to indicate the format is missing.
The 65000~65535 is reserved for experiments and should be safe for
the purpose. Check content-type at PUT method to setup
write/write-attrs operation accordingly.

Also, add reporting write-attrs as not implemented to the caller.

Signed-off-by: Robert Chou <robert.ch.chou@acer.com>
This commit is contained in:
Robert Chou 2017-11-21 18:16:52 +08:00 committed by Anas Nashif
commit 685db067d5
2 changed files with 16 additions and 9 deletions

View file

@ -2057,7 +2057,7 @@ int lwm2m_write_handler(struct lwm2m_engine_obj_inst *obj_inst,
} }
static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj, static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
struct lwm2m_engine_context *context) struct lwm2m_engine_context *context)
{ {
if (!obj || !context) { if (!obj || !context) {
return -EINVAL; return -EINVAL;
@ -2065,7 +2065,7 @@ static int lwm2m_write_attr_handler(struct lwm2m_engine_obj *obj,
/* TODO: set parameters on resource for notification */ /* TODO: set parameters on resource for notification */
return 0; return -ENOTSUP;
} }
static int lwm2m_exec_handler(struct lwm2m_engine_obj *obj, static int lwm2m_exec_handler(struct lwm2m_engine_obj *obj,
@ -2449,7 +2449,7 @@ static int handle_request(struct coap_packet *request,
struct lwm2m_engine_obj *obj = NULL; struct lwm2m_engine_obj *obj = NULL;
u8_t token[8]; u8_t token[8];
u8_t tkl = 0; u8_t tkl = 0;
u16_t format, accept; u16_t format = LWM2M_FORMAT_NONE, accept;
struct lwm2m_input_context in; struct lwm2m_input_context in;
struct lwm2m_output_context out; struct lwm2m_output_context out;
struct lwm2m_obj_path path; struct lwm2m_obj_path path;
@ -2521,10 +2521,8 @@ static int handle_request(struct coap_packet *request,
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) {
format = coap_option_value_to_int(&options[0]); format = select_reader(
} else { &in, coap_option_value_to_int(&options[0]));
SYS_LOG_DBG("No content-format given. Assume text plain.");
format = LWM2M_FORMAT_PLAIN_TEXT;
} }
/* read Accept */ /* read Accept */
@ -2546,7 +2544,6 @@ static int handle_request(struct coap_packet *request,
} }
} }
format = select_reader(&in, format);
accept = select_writer(&out, accept); accept = select_writer(&out, accept);
/* set the operation */ /* set the operation */
@ -2586,7 +2583,13 @@ static int handle_request(struct coap_packet *request,
break; break;
case COAP_METHOD_PUT: case COAP_METHOD_PUT:
context.operation = LWM2M_OP_WRITE; /* write attributes if content-format is absent */
if (format == LWM2M_FORMAT_NONE) {
context.operation = LWM2M_OP_WRITE_ATTR;
} else {
context.operation = LWM2M_OP_WRITE;
}
msg->code = COAP_RESPONSE_CODE_CHANGED; msg->code = COAP_RESPONSE_CODE_CHANGED;
break; break;
@ -2741,6 +2744,8 @@ error:
msg->code = COAP_RESPONSE_CODE_INCOMPLETE; msg->code = COAP_RESPONSE_CODE_INCOMPLETE;
} else if (r == -EFBIG) { } else if (r == -EFBIG) {
msg->code = COAP_RESPONSE_CODE_REQUEST_TOO_LARGE; msg->code = COAP_RESPONSE_CODE_REQUEST_TOO_LARGE;
} else if (r == -ENOTSUP) {
msg->code = COAP_RESPONSE_CODE_NOT_IMPLEMENTED;
} else { } else {
/* Failed to handle the request */ /* Failed to handle the request */
msg->code = COAP_RESPONSE_CODE_INTERNAL_ERROR; msg->code = COAP_RESPONSE_CODE_INTERNAL_ERROR;

View file

@ -23,6 +23,8 @@
#define LWM2M_FORMAT_OMA_OLD_OPAQUE 1544 #define LWM2M_FORMAT_OMA_OLD_OPAQUE 1544
#define LWM2M_FORMAT_OMA_TLV 11542 #define LWM2M_FORMAT_OMA_TLV 11542
#define LWM2M_FORMAT_OMA_JSON 11543 #define LWM2M_FORMAT_OMA_JSON 11543
/* 65000 ~ 65535 inclusive are reserved for experiments */
#define LWM2M_FORMAT_NONE 65535
#define COAP_RESPONSE_CODE_CLASS(x) (x >> 5) #define COAP_RESPONSE_CODE_CLASS(x) (x >> 5)