net: lwm2m: replace coap_next_block() w/ coap_update_from_block()

We should call coap_update_from_block() which will determine the minimum
size of the BLOCK1 SIZE between server/client and update the current
offset and total size(if available) accordingly.

Signed-off-by: Robert Chou <robert.ch.chou@acer.com>
This commit is contained in:
Robert Chou 2017-10-23 10:10:08 +08:00 committed by Jukka Rissanen
commit 44995e2de0

View file

@ -2393,8 +2393,8 @@ static int handle_request(struct coap_packet *request,
int observe = -1; /* default to -1, 0 = ENABLE, 1 = DISABLE */ int observe = -1; /* default to -1, 0 = ENABLE, 1 = DISABLE */
bool discover = false; bool discover = false;
struct block_context *block_ctx = NULL; struct block_context *block_ctx = NULL;
size_t block_offset = 0;
enum coap_block_size block_size; enum coap_block_size block_size;
bool last_block = false;
/* setup engine context */ /* setup engine context */
memset(&context, 0, sizeof(struct lwm2m_engine_context)); memset(&context, 0, sizeof(struct lwm2m_engine_context));
@ -2535,9 +2535,11 @@ static int handle_request(struct coap_packet *request,
/* Check for block transfer */ /* Check for block transfer */
r = get_option_int(in.in_cpkt, COAP_OPTION_BLOCK1); r = get_option_int(in.in_cpkt, COAP_OPTION_BLOCK1);
if (r > 0) { if (r > 0) {
last_block = !GET_MORE(r);
/* RFC7252: 4.6. Message Size */ /* RFC7252: 4.6. Message Size */
block_size = GET_BLOCK_SIZE(r); block_size = GET_BLOCK_SIZE(r);
if (GET_MORE(r) && if (!last_block &&
coap_block_size_to_bytes(block_size) > in.payload_len) { coap_block_size_to_bytes(block_size) > in.payload_len) {
SYS_LOG_DBG("Trailing payload is discarded!"); SYS_LOG_DBG("Trailing payload is discarded!");
r = -EFBIG; r = -EFBIG;
@ -2554,13 +2556,14 @@ static int handle_request(struct coap_packet *request,
goto error; goto error;
} }
/* 0 will be returned if it's the last block */ r = coap_update_from_block(in.in_cpkt, &block_ctx->ctx);
block_offset = coap_next_block(in.in_cpkt, &block_ctx->ctx); if (r < 0) {
} SYS_LOG_ERR("Error from block update: %d", r);
goto error;
}
/* Handle blockwise 1 (Part 1): Set response code / free context */ /* Handle blockwise 1 (Part 1): Set response code */
if (block_ctx) { if (!last_block) {
if (block_offset > 0) {
msg->code = COAP_RESPONSE_CODE_CONTINUE; msg->code = COAP_RESPONSE_CODE_CONTINUE;
} }
} }
@ -2635,9 +2638,9 @@ static int handle_request(struct coap_packet *request,
goto error; goto error;
} }
/* Handle blockwise 1 (Part 2): Append BLOCK1 option */ /* Handle blockwise 1 (Part 2): Append BLOCK1 option / free context */
if (block_ctx) { if (block_ctx) {
if (block_offset > 0) { if (!last_block) {
/* More to come, ack with correspond block # */ /* More to come, ack with correspond block # */
r = coap_append_block1_option(out.out_cpkt, r = coap_append_block1_option(out.out_cpkt,
&block_ctx->ctx); &block_ctx->ctx);