static void hnd_espressif_put(coap_context_t *ctx, coap_resource_t *resource, coap_session_t *session, coap_pdu_t *request, coap_binary_t *token, coap_string_t *query, coap_pdu_t *response) { size_t size; unsigned char *data; coap_resource_notify_observers(resource, NULL); if (strcmp (espressif_data, "no data") == 0) { response->code = COAP_RESPONSE_CODE(201); } else { response->code = COAP_RESPONSE_CODE(204); } /* coap_get_data() sets size to 0 on error */ (void)coap_get_data(request, &size, &data); if (size == 0) { /* re-init */ snprintf(espressif_data, sizeof(espressif_data), "no data"); espressif_data_len = strlen(espressif_data); } else { espressif_data_len = size > sizeof (espressif_data) ? sizeof (espressif_data) : size; memcpy (espressif_data, data, espressif_data_len); } }
rd_t * make_rd(coap_address_t *peer, coap_pdu_t *pdu) { rd_t *rd; unsigned char *data; coap_opt_iterator_t opt_iter; coap_opt_t *etag; rd = rd_new(); if (!rd) { debug("hnd_get_rd: cannot allocate storage for rd\n"); return NULL; } if (coap_get_data(pdu, &rd->data.length, &data)) { rd->data.s = (unsigned char *)coap_malloc(rd->data.length); if (!rd->data.s) { debug("hnd_get_rd: cannot allocate storage for rd->data\n"); rd_delete(rd); return NULL; } memcpy(rd->data.s, data, rd->data.length); } etag = coap_check_option(pdu, COAP_OPTION_ETAG, &opt_iter); if (etag) { rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag)); memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len); } return rd; }
void hnd_put_time(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { coap_tick_t t; size_t size; unsigned char *data; /* FIXME: re-set my_clock_base to clock_offset if my_clock_base == 0 * and request is empty. When not empty, set to value in request payload * (insist on query ?ticks). Return Created or Ok. */ /* if my_clock_base was deleted, we pretend to have no such resource */ response->hdr->code = my_clock_base ? COAP_RESPONSE_CODE(204) : COAP_RESPONSE_CODE(201); resource->dirty = 1; coap_get_data(request, &size, &data); if (size == 0) /* re-init */ my_clock_base = clock_offset; else { my_clock_base = 0; coap_ticks(&t); while(size--) my_clock_base = my_clock_base * 10 + *data++; my_clock_base -= t / COAP_TICKS_PER_SECOND; } }
/* * Function: _wilddog_recv_dispatch. * Description: recv coap packet and handle it. * * Input: p_recvPdu : point to the recv coap packet. * p_cm_recvArg: recv arg that will transmit to cm port. * Output: N/A. * Return: N/A. */ Wilddog_Return_T WD_SYSTEM _wilddog_recv_dispatch ( coap_pdu_t *p_recvPdu, Protocol_recvArg_T *p_cm_recvArg ) { size_t playloadLen =0; u8 *p_payload = NULL; /* get err*/ wilddog_debug_level(WD_DEBUG_LOG," coap err : %d",p_recvPdu->hdr->code); p_cm_recvArg->err = _wilddog_coap_code2Http( \ (unsigned int)_GET_COAP_CODE(p_recvPdu->hdr->code)); wilddog_debug_level(WD_DEBUG_LOG,"coap http error %ld",p_cm_recvArg->err); /* get token option.*/ if(p_recvPdu->hdr->token_length != COAP_TOKENLEN) return WILDDOG_ERR_INVALID; memcpy(&p_cm_recvArg->d_token,p_recvPdu->hdr->token,COAP_TOKENLEN); /* get observer index.*/ p_cm_recvArg->d_isObserver = _wilddog_recv_getOptionValue(p_recvPdu, COAP_OPTION_OBSERVE, (u8*)&p_cm_recvArg->d_observerIndx, sizeof(p_cm_recvArg->d_observerIndx)); wilddog_debug_level(WD_DEBUG_LOG, \ "coap get observerIndex : %lu ", \ p_cm_recvArg->d_observerIndx); /* get maxage.*/ _wilddog_recv_getOptionValue(p_recvPdu, COAP_OPTION_MAXAGE, \ (u8*)&(p_cm_recvArg->d_maxAge), \ sizeof( p_cm_recvArg->d_maxAge)); wilddog_debug_level(WD_DEBUG_LOG, \ "coap get max-age : %lu ", \ p_cm_recvArg->d_maxAge); /* get blockNum.*/ /*get payload data */ coap_get_data(p_recvPdu,&playloadLen,&p_payload); /* clean buf .*/ memset(p_cm_recvArg->p_recvData,0,(size_t)p_cm_recvArg->d_recvDataLen); if( playloadLen > p_cm_recvArg->d_recvDataLen ) return WILDDOG_ERR_INVALID; memcpy( p_cm_recvArg->p_recvData,p_payload,playloadLen); p_cm_recvArg->d_recvDataLen = playloadLen; wilddog_debug_level(WD_DEBUG_LOG, \ "coap recv data :%s", \ p_cm_recvArg->p_recvData); return WILDDOG_ERR_NOERR; }
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 CoAPWrapper::get_payload(CoAPCallback &callback, std::string &payload) { str load= {0, NULL}; coap_pdu_t *pdu = (coap_pdu_t *) (callback.request_); if (coap_get_data(pdu, &load.length, &load.s)) { payload.assign((char*)load.s, load.length); } }
// When we receive the CoAP response we build and send the HTTP response void coap_response_handler(struct coap_context_t *ctx, const coap_endpoint_t *local_interface, const coap_address_t *remote, coap_pdu_t *sent, coap_pdu_t *received, const coap_tid_t id) { printf("COAP %13s:%-5u -> ", inet_ntoa((&remote->addr.sin)->sin_addr), ntohs((&remote->addr.sin)->sin_port)); coap_show_pdu(received); for(int i = 0; i < MAX_HTTP_CONNECTIONS; i++) { if(http_coap_pairs[i].message_id == received->hdr->id) { struct MHD_Connection *connection = http_coap_pairs[i].connection; size_t len = 0; unsigned char *databuf = NULL; struct MHD_Response *response = MHD_create_response_from_buffer(len, databuf, MHD_RESPMEM_MUST_COPY); int read_result = coap_get_data(received, &len, &databuf); if(received->hdr->code == COAP_RESPONSE_CODE(205) && read_result == 0) { coap_abort_to_http(connection, "coap_get_data: cannot read CoAP response data\n"); } static char tid_str[8]; snprintf(tid_str, sizeof(tid_str), "%u", ntohs(received->hdr->id)); MHD_add_response_header(response, "X-CoAP-Message-Id", tid_str); MHD_add_response_header(response, "X-CoAP-Response-Code", msg_code_string(received->hdr->code)); // HTTP Content-Type const char *http_content_type; int coap_content_format; coap_opt_iterator_t opt_iter; coap_opt_t *option; coap_option_iterator_init(received, &opt_iter, COAP_OPT_ALL); while((option = coap_option_next(&opt_iter))) { switch(opt_iter.type) { case COAP_OPTION_CONTENT_FORMAT: coap_content_format = (int)coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option)); break; default: continue; } } switch(coap_content_format) { case COAP_MEDIATYPE_TEXT_PLAIN: http_content_type = "text/plain"; break; case COAP_MEDIATYPE_APPLICATION_LINK_FORMAT: http_content_type = "application/link-format"; break; case COAP_MEDIATYPE_APPLICATION_XML: http_content_type = "application/xml"; break; case COAP_MEDIATYPE_APPLICATION_OCTET_STREAM: http_content_type = "application/octet-stream"; break; case COAP_MEDIATYPE_APPLICATION_EXI: http_content_type = "application/exi"; break; case COAP_MEDIATYPE_APPLICATION_JSON: http_content_type = "application/json"; break; case COAP_MEDIATYPE_APPLICATION_CBOR: http_content_type = "application/cbor"; break; default: http_content_type = "unknown"; break; } MHD_add_response_header(response, MHD_HTTP_HEADER_CONTENT_TYPE, http_content_type); // HTTP Code unsigned int http_code; switch(received->hdr->code) { case COAP_RESPONSE_200: http_code = MHD_HTTP_NO_CONTENT; break; /* 2.00 OK */ case COAP_RESPONSE_201: http_code = MHD_HTTP_CREATED; break; /* 2.01 Created */ case COAP_RESPONSE_CODE(205): http_code = MHD_HTTP_OK; break; case COAP_RESPONSE_304: http_code = MHD_HTTP_ACCEPTED; break; /* 2.03 Valid */ case COAP_RESPONSE_400: http_code = MHD_HTTP_BAD_REQUEST; break; /* 4.00 Bad Request */ case COAP_RESPONSE_404: http_code = MHD_HTTP_NOT_FOUND; break; /* 4.04 Not Found */ case COAP_RESPONSE_405: http_code = MHD_HTTP_NOT_ACCEPTABLE; break; /* 4.05 Method Not Allowed */ case COAP_RESPONSE_415: http_code = MHD_HTTP_UNSUPPORTED_MEDIA_TYPE;break; /* 4.15 Unsupported Media Type */ case COAP_RESPONSE_500: http_code = MHD_HTTP_INTERNAL_SERVER_ERROR; break; /* 5.00 Internal Server Error */ case COAP_RESPONSE_501: http_code = MHD_HTTP_NOT_IMPLEMENTED; break; /* 5.01 Not Implemented */ case COAP_RESPONSE_503: http_code = MHD_HTTP_SERVICE_UNAVAILABLE; break; /* 5.03 Service Unavailable */ case COAP_RESPONSE_504: http_code = MHD_HTTP_GATEWAY_TIMEOUT; break; /* 5.04 Gateway Timeout */ default: http_code = MHD_HTTP_INTERNAL_SERVER_ERROR; break; } // Send the response MHD_queue_response(connection, http_code, response); MHD_destroy_response(response); const struct sockaddr_in *client_addr = (const struct sockaddr_in *) MHD_get_connection_info(connection, MHD_CONNECTION_INFO_CLIENT_ADDRESS)->client_addr; printf("HTTP %13s:%-5u <- %u %s [ %s, %zu bytes, \"%s\" ]\n", inet_ntoa(client_addr->sin_addr), ntohs(client_addr->sin_port), http_code, http_reason_phrase_for(http_code), http_content_type, len, (databuf != NULL) ? (char *)databuf : ""); } // clear the association http_coap_pairs[i].connection = NULL; return; } }
CAResult_t CAGetInfoFromPDU(const coap_pdu_t *pdu, uint32_t *outCode, CAInfo_t *outInfo) { OIC_LOG(DEBUG, TAG, "IN"); if (!pdu || !outCode || !outInfo) { OIC_LOG(ERROR, TAG, "NULL pointer param"); return CA_STATUS_INVALID_PARAM; } coap_opt_iterator_t opt_iter; coap_option_iterator_init((coap_pdu_t *) pdu, &opt_iter, COAP_OPT_ALL); if (outCode) { (*outCode) = (uint32_t) CA_RESPONSE_CODE(pdu->hdr->code); } // init HeaderOption list uint32_t count = CAGetOptionCount(opt_iter); memset(outInfo, 0, sizeof(*outInfo)); outInfo->numOptions = count; // set type outInfo->type = pdu->hdr->type; // set message id outInfo->messageId = pdu->hdr->id; outInfo->payloadFormat = CA_FORMAT_UNDEFINED; if (count > 0) { outInfo->options = (CAHeaderOption_t *) OICCalloc(count, sizeof(CAHeaderOption_t)); if (NULL == outInfo->options) { OIC_LOG(ERROR, TAG, "Out of memory"); return CA_MEMORY_ALLOC_FAILED; } } coap_opt_t *option; char optionResult[CA_MAX_URI_LENGTH] = {0}; uint32_t idx = 0; uint32_t optionLength = 0; bool isfirstsetflag = false; bool isQueryBeingProcessed = false; while ((option = coap_option_next(&opt_iter))) { char buf[COAP_MAX_PDU_SIZE] = {0}; uint32_t bufLength = CAGetOptionData(opt_iter.type, (uint8_t *)(COAP_OPT_VALUE(option)), COAP_OPT_LENGTH(option), (uint8_t *)buf, sizeof(buf)); if (bufLength) { OIC_LOG_V(DEBUG, TAG, "COAP URI element : %s", buf); if (COAP_OPTION_URI_PATH == opt_iter.type || COAP_OPTION_URI_QUERY == opt_iter.type) { if (false == isfirstsetflag) { isfirstsetflag = true; optionResult[optionLength] = '/'; optionLength++; // Make sure there is enough room in the optionResult buffer if ((optionLength + bufLength) < sizeof(optionResult)) { memcpy(&optionResult[optionLength], buf, bufLength); optionLength += bufLength; } else { goto exit; } } else { if (COAP_OPTION_URI_PATH == opt_iter.type) { // Make sure there is enough room in the optionResult buffer if (optionLength < sizeof(optionResult)) { optionResult[optionLength] = '/'; optionLength++; } else { goto exit; } } else if (COAP_OPTION_URI_QUERY == opt_iter.type) { if (false == isQueryBeingProcessed) { // Make sure there is enough room in the optionResult buffer if (optionLength < sizeof(optionResult)) { optionResult[optionLength] = '?'; optionLength++; isQueryBeingProcessed = true; } else { goto exit; } } else { // Make sure there is enough room in the optionResult buffer if (optionLength < sizeof(optionResult)) { optionResult[optionLength] = ';'; optionLength++; } else { goto exit; } } } // Make sure there is enough room in the optionResult buffer if ((optionLength + bufLength) < sizeof(optionResult)) { memcpy(&optionResult[optionLength], buf, bufLength); optionLength += bufLength; } else { goto exit; } } } else if (COAP_OPTION_BLOCK1 == opt_iter.type || COAP_OPTION_BLOCK2 == opt_iter.type || COAP_OPTION_SIZE1 == opt_iter.type || COAP_OPTION_SIZE2 == opt_iter.type) { OIC_LOG_V(DEBUG, TAG, "option[%d] will be filtering", opt_iter.type); } else if (COAP_OPTION_CONTENT_FORMAT == opt_iter.type) { if (1 == COAP_OPT_LENGTH(option) && COAP_MEDIATYPE_APPLICATION_CBOR == buf[0]) { outInfo->payloadFormat = CA_FORMAT_CBOR; } else { outInfo->payloadFormat = CA_FORMAT_UNSUPPORTED; } OIC_LOG_V(DEBUG, TAG, "option[%d] has format [%d]", opt_iter.type, (uint8_t)buf[0]); } else { if (idx < count) { uint32_t length = bufLength; if (length <= CA_MAX_HEADER_OPTION_DATA_LENGTH) { outInfo->options[idx].optionID = opt_iter.type; outInfo->options[idx].optionLength = length; outInfo->options[idx].protocolID = CA_COAP_ID; memcpy(outInfo->options[idx].optionData, buf, length); idx++; } } } } } // set token data if (pdu->hdr->token_length > 0) { OIC_LOG_V(DEBUG, TAG, "inside token length : %d", pdu->hdr->token_length); outInfo->token = (char *) OICMalloc(pdu->hdr->token_length); if (NULL == outInfo->token) { OIC_LOG(ERROR, TAG, "Out of memory"); OICFree(outInfo->options); return CA_MEMORY_ALLOC_FAILED; } memcpy(outInfo->token, pdu->hdr->token, pdu->hdr->token_length); } outInfo->tokenLength = pdu->hdr->token_length; // set payload data size_t dataSize; uint8_t *data; if (coap_get_data(pdu, &dataSize, &data)) { OIC_LOG(DEBUG, TAG, "inside pdu->data"); outInfo->payload = (uint8_t *) OICMalloc(dataSize); if (NULL == outInfo->payload) { OIC_LOG(ERROR, TAG, "Out of memory"); OICFree(outInfo->options); OICFree(outInfo->token); return CA_MEMORY_ALLOC_FAILED; } memcpy(outInfo->payload, pdu->data, dataSize); outInfo->payloadSize = dataSize; } if (optionResult[0] != '\0') { OIC_LOG_V(DEBUG, TAG, "URL length:%d", strlen(optionResult)); outInfo->resourceUri = OICStrdup(optionResult); if (!outInfo->resourceUri) { OIC_LOG(ERROR, TAG, "Out of memory"); OICFree(outInfo->options); OICFree(outInfo->token); return CA_MEMORY_ALLOC_FAILED; } } OIC_LOG(DEBUG, TAG, "OUT"); return CA_STATUS_OK; exit: OIC_LOG(ERROR, TAG, "buffer too small"); OICFree(outInfo->options); return CA_STATUS_FAILED; }
void hnd_post_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 *test_payload; size_t len; size_t l = 6 + sizeof(void *); coap_dynamic_uri_t *uri; unsigned char *data; #define BUFSIZE 20 int res; unsigned char _buf[BUFSIZE]; unsigned char *buf = _buf; size_t buflen = BUFSIZE; (void)resource; (void)peer; (void)token; coap_get_data(request, &len, &data); /* allocate storage for resource and to hold URI */ test_payload = coap_new_payload(len); uri = (coap_dynamic_uri_t *)coap_malloc(sizeof(coap_dynamic_uri_t) + l); if (!(test_payload && uri)) { coap_log(LOG_CRIT, "cannot allocate new resource under /test"); response->hdr->code = COAP_RESPONSE_CODE(500); coap_free(test_payload); coap_free(uri); } else { coap_resource_t *r; memset(uri, 0, sizeof(coap_dynamic_uri_t)); uri->length = min(l, (size_t)snprintf((char *)uri->data, l, "test/%p", (void*)test_payload)); test_payload->length = len; memcpy(test_payload->data, data, len); r = coap_resource_init(uri->data, uri->length, 0); coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource); coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource); /* set media_type if available */ option = coap_check_option(request, COAP_OPTION_CONTENT_TYPE, &opt_iter); if (option) { test_payload->media_type = coap_decode_var_bytes(COAP_OPT_VALUE(option), COAP_OPT_LENGTH(option)); } coap_add_resource(ctx, r); coap_add_payload(r->key, test_payload, uri); /* add Location-Path */ res = coap_split_path(uri->data, uri->length, buf, &buflen); while (res--) { coap_add_option(response, COAP_OPTION_LOCATION_PATH, COAP_OPT_LENGTH(buf), COAP_OPT_VALUE(buf)); buf += COAP_OPT_SIZE(buf); } response->hdr->code = COAP_RESPONSE_CODE(201); } }
void hnd_post_rd(coap_context_t *ctx, struct coap_resource_t *resource, const coap_endpoint_t *local_interface, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { size_t len=0; unsigned char *databuf; unsigned char strBuf[100]; unsigned char s[100]; coap_print_addr(peer, s, 100); if (coap_get_data(request, &len, &databuf)){ memcpy(strBuf, databuf, len); strBuf[len]='\0'; fprintf(stdout, "%s %s\n", s, strBuf); }else{ fprintf(stdout, "%s\n", s); } fflush(stdout); coap_resource_t *r; coap_opt_iterator_t opt_iter; coap_opt_t *query; #define LOCSIZE 68 unsigned char *loc; size_t loc_size; str h = {0, NULL}, ins = {0, NULL}, rt = {0, NULL}, lt = {0, NULL}; /* store query parameters */ unsigned char *buf; loc = (unsigned char *)coap_malloc(LOCSIZE); if (!loc) { response->hdr->code = COAP_RESPONSE_CODE(500); return; } memcpy(loc, RD_ROOT_STR, RD_ROOT_SIZE); loc_size = RD_ROOT_SIZE; loc[loc_size++] = '/'; /* store query parameters for later use */ query = coap_check_option(request, COAP_OPTION_URI_QUERY, &opt_iter); if (query) { parse_param((unsigned char *)"h", 1, COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &h); parse_param((unsigned char *)"ins", 3, COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &ins); parse_param((unsigned char *)"lt", 2, COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), <); parse_param((unsigned char *)"rt", 2, COAP_OPT_VALUE(query), COAP_OPT_LENGTH(query), &rt); } if (h.length) { /* client has specified a node name */ memcpy(loc + loc_size, h.s, min(h.length, LOCSIZE - loc_size - 1)); loc_size += min(h.length, LOCSIZE - loc_size - 1); if (ins.length && loc_size > 1) { loc[loc_size++] = '-'; memcpy((char *)(loc + loc_size), ins.s, min(ins.length, LOCSIZE - loc_size - 1)); loc_size += min(ins.length, LOCSIZE - loc_size - 1); } } else { /* generate node identifier */ loc_size += snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, "%x", request->hdr->id); if (loc_size > 1) { if (ins.length) { loc[loc_size++] = '-'; memcpy((char *)(loc + loc_size), ins.s, min(ins.length, LOCSIZE - loc_size - 1)); loc_size += min(ins.length, LOCSIZE - loc_size - 1); } else { coap_tick_t now; coap_ticks(&now); loc_size += snprintf((char *)(loc + loc_size), LOCSIZE - loc_size - 1, "-%x", now); } } } /* TODO: * - use lt to check expiration */ r = coap_resource_init(loc, loc_size, COAP_RESOURCE_FLAGS_RELEASE_URI); coap_register_handler(r, COAP_REQUEST_GET, hnd_get_resource); coap_register_handler(r, COAP_REQUEST_PUT, hnd_put_resource); coap_register_handler(r, COAP_REQUEST_DELETE, hnd_delete_resource); if (ins.s) { buf = (unsigned char *)coap_malloc(ins.length + 2); if (buf) { /* add missing quotes */ buf[0] = '"'; memcpy(buf + 1, ins.s, ins.length); buf[ins.length + 1] = '"'; coap_add_attr(r, (unsigned char *)"ins", 3, buf, ins.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE); } } if (rt.s) { buf = (unsigned char *)coap_malloc(rt.length + 2); if (buf) { /* add missing quotes */ buf[0] = '"'; memcpy(buf + 1, rt.s, rt.length); buf[rt.length + 1] = '"'; coap_add_attr(r, (unsigned char *)"rt", 2, buf, rt.length + 2, COAP_ATTR_FLAGS_RELEASE_VALUE); } } add_source_address(r, peer); { rd_t *rd; rd = make_rd(peer, request); if (rd) { coap_hash_path(loc, loc_size, rd->key); HASH_ADD(hh, resources, key, sizeof(coap_key_t), rd); } else { /* FIXME: send error response and delete r */ } } coap_add_resource(ctx, r); /* create response */ response->hdr->code = COAP_RESPONSE_CODE(201); { /* split path into segments and add Location-Path options */ unsigned char _b[LOCSIZE]; unsigned char *b = _b; size_t buflen = sizeof(_b); int nseg; nseg = coap_split_path(loc, loc_size, b, &buflen); while (nseg--) { coap_add_option(response, COAP_OPTION_LOCATION_PATH, COAP_OPT_LENGTH(b), COAP_OPT_VALUE(b)); b += COAP_OPT_SIZE(b); } } }
void hnd_put_resource(coap_context_t *ctx, struct coap_resource_t *resource, const coap_endpoint_t *local_interface, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { #if 1 response->hdr->code = COAP_RESPONSE_CODE(501); #else /* FIXME */ coap_opt_iterator_t opt_iter; coap_opt_t *token, *etag; coap_pdu_t *response; size_t size = sizeof(coap_hdr_t); int type = (request->hdr->type == COAP_MESSAGE_CON) ? COAP_MESSAGE_ACK : COAP_MESSAGE_NON; rd_t *rd = NULL; unsigned char code; /* result code */ unsigned char *data; str tmp; HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd); if (rd) { /* found resource object, now check Etag */ etag = coap_check_option(request, COAP_OPTION_ETAG, &opt_iter); if (!etag || (COAP_OPT_LENGTH(etag) != rd->etag_len) || memcmp(COAP_OPT_VALUE(etag), rd->etag, rd->etag_len) != 0) { if (coap_get_data(request, &tmp.length, &data)) { tmp.s = (unsigned char *)coap_malloc(tmp.length); if (!tmp.s) { debug("hnd_put_rd: cannot allocate storage for new rd\n"); code = COAP_RESPONSE_CODE(503); goto finish; } coap_free(rd->data.s); rd->data.s = tmp.s; rd->data.length = tmp.length; memcpy(rd->data.s, data, rd->data.length); } } if (etag) { rd->etag_len = min(COAP_OPT_LENGTH(etag), sizeof(rd->etag)); memcpy(rd->etag, COAP_OPT_VALUE(etag), rd->etag_len); } code = COAP_RESPONSE_CODE(204); /* FIXME: update lifetime */ } else { code = COAP_RESPONSE_CODE(503); } finish: /* FIXME: do not create a new response but use the old one instead */ response = coap_pdu_init(type, code, request->hdr->id, size); if (!response) { debug("cannot create response for message %d\n", request->hdr->id); return; } if (request->hdr->token_length) coap_add_token(response, request->hdr->token_length, request->hdr->token); if (coap_send(ctx, peer, response) == COAP_INVALID_TID) { debug("hnd_get_rd: cannot send response for message %d\n", request->hdr->id); } coap_delete_pdu(response); #endif }