void hnd_put_test(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { coap_opt_iterator_t opt_iter; coap_opt_t *option; coap_payload_t *payload; size_t len; unsigned char *data; (void)ctx; (void)peer; (void)token; response->hdr->code = COAP_RESPONSE_CODE(204); coap_get_data(request, &len, &data); payload = coap_find_payload(resource->key); if (payload && payload->max_data < len) { /* need more storage */ coap_delete_payload(payload); payload = NULL; /* bug: when subsequent coap_new_payload() fails, our old contents is gone */ } if (!payload) { /* create new payload */ payload = coap_new_payload(len); if (!payload) goto error; coap_add_payload(resource->key, payload, NULL); } payload->length = len; memcpy(payload->data, data, len); option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter); if (option) { /* set media type given in request */ payload->media_type = coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option)); } else { /* set default value */ payload->media_type = COAP_MEDIATYPE_TEXT_PLAIN; } /* FIXME: need to change attribute ct of resource. To do so, we need dynamic management of the attribute value */ return; error: warn("cannot modify resource\n"); response->hdr->code = COAP_RESPONSE_CODE(500); }
void hnd_delete_test(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { /* the ETSI validation tool does not like empty resources... */ #if 0 coap_payload_t *payload; payload = coap_find_payload(resource->key); if (payload) payload->length = 0; #endif response->hdr->code = COAP_RESPONSE_CODE(202); }
/* DELETE handler for dynamic resources created by POST /test */ void hnd_delete_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_payload_t *payload; payload = coap_find_payload(resource->key); if (payload) coap_delete_payload(payload); coap_delete_resource(ctx, resource->key); response->hdr->code = COAP_RESPONSE_CODE(202); }
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)); }