diff --git a/src/coap_block.c b/src/coap_block.c index f36d8bb840..fad031a4e9 100644 --- a/src/coap_block.c +++ b/src/coap_block.c @@ -2283,9 +2283,27 @@ coap_block_new_lg_crcv(coap_session_t *session, coap_pdu_t *pdu, uint64_t state_token = STATE_TOKEN_FULL(++session->tx_token, 1); size_t token_options = pdu->data ? (size_t)(pdu->data - pdu->token) : pdu->used_size; - size_t data_len = lg_xmit ? lg_xmit->length : - pdu->data ? - pdu->used_size - (pdu->data - pdu->token) : 0; + size_t data_len; + int observe_action = -1; + + if (lg_xmit) { + coap_opt_iterator_t opt_iter; + coap_opt_t *opt; + + opt = coap_check_option(pdu, COAP_OPTION_OBSERVE, &opt_iter); + + if (opt) { + observe_action = coap_decode_var_bytes(coap_opt_value(opt), + coap_opt_length(opt)); + } + } + if (observe_action == COAP_OBSERVE_ESTABLISH) { + data_len = lg_xmit->length; + } else { + lg_xmit = NULL; + data_len = pdu->data ? + pdu->used_size - (pdu->data - pdu->token) : 0; + } lg_crcv = coap_malloc_type(COAP_LG_CRCV, sizeof(coap_lg_crcv_t)); diff --git a/src/coap_net.c b/src/coap_net.c index 816b65c111..18de2e2c39 100644 --- a/src/coap_net.c +++ b/src/coap_net.c @@ -3569,7 +3569,7 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu if (session->block_mode & COAP_BLOCK_USE_LIBCOAP) { uint32_t block_mode = session->block_mode; - if (pdu->code == COAP_REQUEST_CODE_FETCH || + if (observe || resource->flags & COAP_RESOURCE_FLAGS_FORCE_SINGLE_BODY) session->block_mode |= COAP_BLOCK_SINGLE_BODY; if (coap_handle_request_put_block(context, session, pdu, response, @@ -3617,10 +3617,10 @@ handle_request(coap_context_t *context, coap_session_t *session, coap_pdu_t *pdu uint8_t buf[4]; coap_touch_observer(context, session, &pdu->actual_token); - coap_add_option_internal(response, COAP_OPTION_OBSERVE, - coap_encode_var_safe(buf, sizeof(buf), - resource->observe), - buf); + coap_insert_option(response, COAP_OPTION_OBSERVE, + coap_encode_var_safe(buf, sizeof(buf), + resource->observe), + buf); } } else if (observe_action == COAP_OBSERVE_CANCEL) { coap_delete_observer_request(resource, session, &pdu->actual_token, pdu);