int send_data_by_block(coap_pdu_t *response, coap_block_t *block_opt, unsigned char *data, unsigned int len) { if (1 == coap_write_block_opt(block_opt, COAP_OPTION_BLOCK2, response, len)) { if (1== coap_add_block(response, len, data, block_opt->num, block_opt->szx)) { return 1; } else { ACE_DEBUG((LM_DEBUG, "Failed to send data by block-wise\n")); } } else { ACE_DEBUG((LM_DEBUG, "Failed to set block opt into repsonse\n")); } return 0; }
void hnd_get_resource(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { coap_key_t etag; unsigned char buf[2]; coap_payload_t *test_payload; coap_block_t block; (void)ctx; (void)peer; (void)token; test_payload = coap_find_payload(resource->key); if (!test_payload) { response->hdr->code = COAP_RESPONSE_CODE(500); return; } response->hdr->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, test_payload->media_type), buf); /* add etag for the resource */ if (test_payload->flags & REQUIRE_ETAG) { memset(etag, 0, sizeof(etag)); coap_hash(test_payload->data, test_payload->length, etag); coap_add_option(response, COAP_OPTION_ETAG, sizeof(etag), etag); } if (request) { int res; if (coap_get_block(request, COAP_OPTION_BLOCK2, &block)) { res = coap_write_block_opt(&block, COAP_OPTION_BLOCK2, response, test_payload->length); switch (res) { case -2: /* illegal block */ response->hdr->code = COAP_RESPONSE_CODE(400); goto error; case -1: /* should really not happen */ assert(0); /* fall through if assert is a no-op */ case -3: /* cannot handle request */ response->hdr->code = COAP_RESPONSE_CODE(500); goto error; default: /* everything is good */ ; } coap_add_block(response, test_payload->length, test_payload->data, block.num, block.szx); } else { if (!coap_add_data(response, test_payload->length, test_payload->data)) { /* set initial block size, will be lowered by * coap_write_block_opt) automatically */ block.szx = 6; coap_write_block_opt(&block, COAP_OPTION_BLOCK2, response, test_payload->length); coap_add_block(response, test_payload->length, test_payload->data, block.num, block.szx); } } } else { /* this is a notification, block is 0 */ /* FIXME: need to store block size with subscription */ } return; error: coap_add_data(response, strlen(coap_response_phrase(response->hdr->code)), (unsigned char *)coap_response_phrase(response->hdr->code)); }