static void t_error_response3(void) { const uint8_t code = COAP_RESPONSE_CODE(402); uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0x90, 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', 'n' }; coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (const uint8_t *)"token"); /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time"); */ /* unknown critical option 9 */ coap_add_option(pdu, 9, 0, NULL); coap_option_filter_clear(opts); coap_option_setb(opts, 9); response = coap_new_error_response(pdu, code, opts); CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); CU_ASSERT(response->token_length == 5); CU_ASSERT(response->code == code); CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); }
static void t_error_response2(void) { uint8_t teststr[] = { 0x55, 0x84, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0xff, 'N', 'o', 't', ' ', 'F', 'o', 'u', 'n', 'd' }; coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); pdu->type = COAP_MESSAGE_NON; pdu->tid = 0x1234; coap_add_token(pdu, 5, (const uint8_t *)"token"); coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (const uint8_t *)"time"); coap_option_filter_clear(opts); response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), opts); CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_NON); CU_ASSERT(response->token_length == 5); CU_ASSERT(response->code == 0x84); CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); }
void t_encode_pdu8(void) { /* PDU with token and data */ char teststr[] = { 0x42, 0x43, 0x12, 0x34, 0x00, 0x01, 0xff, 0x00 }; int result; coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ pdu->hdr->type = COAP_MESSAGE_CON; pdu->hdr->code = COAP_RESPONSE_CODE(203); pdu->hdr->id = htons(0x1234); CU_ASSERT(pdu->length == 4); result = coap_add_token(pdu, 2, (unsigned char *) "\x00\x01"); CU_ASSERT(result > 0); result = coap_add_data(pdu, 1, (unsigned char *) "\0"); CU_ASSERT(result > 0); CU_ASSERT(pdu->length == 8); CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 7); CU_ASSERT(pdu->length == sizeof(teststr)); CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); }
void t_error_response2(void) { unsigned char teststr[] = { 0x55, 0x84, 0x12, 0x34, 't', 'o', 'k', 'e', 'n', 0xff, 'N', 'o', 't', ' ', 'F', 'o', 'u', 'n', 'd' }; coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); pdu->hdr->type = COAP_MESSAGE_NON; pdu->hdr->id = htons(0x1234); coap_add_token(pdu, 5, (unsigned char *)"token"); coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); coap_option_filter_clear(opts); response = coap_new_error_response(pdu, COAP_RESPONSE_CODE(404), opts); CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->length == sizeof(teststr)); CU_ASSERT(response->hdr->version == 1); CU_ASSERT(response->hdr->type == COAP_MESSAGE_NON); CU_ASSERT(response->hdr->token_length == 5); CU_ASSERT(response->hdr->code == 0x84); CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); }
/* * Function: _wilddog_coap_ackSend. * Description: response an ack . * Input: recv_type: recv type conn or non-con . * ack_type : respond an ack or reset. * mid : recv coap's message id. * tokenLen: recv coap's token len. * recv_token: recv coap's token. * Output: N/A. * Return: N/A. */ STATIC int WD_SYSTEM _wilddog_coap_ackSend ( u32 recv_type, u32 ack_type, u32 mid, u32 tokenLen, u32 recv_token ) { int returnCode= 0; unsigned int id = mid; size_t tkl = tokenLen; unsigned char* p_tk = (unsigned char*)&recv_token; coap_pdu_t *toSend = NULL; /* only con request need tobe respond.*/ if(recv_type != COAP_MESSAGE_CON) return WILDDOG_ERR_NOERR; toSend = coap_pdu_init(ack_type, 0, id,WILDDOG_PROTO_MAXSIZE); coap_add_token(toSend,tkl,p_tk); if (toSend == NULL) { wilddog_debug_level(WD_DEBUG_ERROR,"coap_addToken error"); return WILDDOG_ERR_NULL; } returnCode = _wilddog_sec_send(toSend->hdr,toSend->length); coap_delete_pdu(toSend); return returnCode; }
void t_error_response3(void) { const unsigned char code = COAP_RESPONSE_CODE(402); unsigned char teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0x90, 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', 'n' }; coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); pdu->hdr->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* coap_add_option(pdu, COAP_OPTION_URI_HOST, 4, (unsigned char *)"time"); */ /* unknown critical option 9 */ coap_add_option(pdu, 9, 0, NULL); coap_option_filter_clear(opts); coap_option_setb(opts, 9); response = coap_new_error_response(pdu, code, opts); CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->length == sizeof(teststr)); CU_ASSERT(response->hdr->version == 1); CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); CU_ASSERT(response->hdr->token_length == 5); CU_ASSERT(response->hdr->code == code); CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); }
void t_encode_pdu3(void) { int result; result = coap_add_token(pdu, 9, (unsigned char *) "123456789"); CU_ASSERT(result == 0); }
coap_pdu_t* CACreatePDUforRequestWithPayload(const code_t code, coap_list_t *options, const char* payload) { OIC_LOG(DEBUG, TAG, "CACreatePDUforRequestWithPayload"); coap_pdu_t *pdu; coap_list_t *opt; unsigned char _token_data[8]; str the_token = { 0, _token_data }; if (!(pdu = coap_new_pdu())) return NULL; /* initialize message id */ unsigned short message_id; prng((unsigned char *)&message_id, sizeof(unsigned short)); pdu->hdr->type = msgtype; pdu->hdr->id = htons(++message_id); pdu->hdr->code = code; pdu->hdr->token_length = the_token.length; if (!coap_add_token(pdu, the_token.length, the_token.s)) { OIC_LOG(DEBUG, TAG,"cannot add token to request"); } for (opt = options; opt; opt = opt->next) { coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), COAP_OPTION_LENGTH(*(coap_option *)opt->data), COAP_OPTION_DATA(*(coap_option *)opt->data)); } if (NULL != payload) { uint32_t len = strlen(payload); if ((flags & CA_FLAGS_BLOCK) == 0) { OIC_LOG_V(DEBUG, TAG, "coap_add_data, payload: %s", payload); coap_add_data(pdu, len, payload); } else { OIC_LOG_V(DEBUG, TAG, "coap_add_block, payload: %s", payload); coap_add_block(pdu, len, payload, block.num, block.szx); } } return pdu; }
void t_encode_pdu2(void) { size_t old_max = pdu->max_size; int result; coap_pdu_clear(pdu, 7); /* set very small PDU size */ pdu->hdr->type = COAP_MESSAGE_CON; pdu->hdr->code = COAP_REQUEST_GET; pdu->hdr->id = htons(0x1234); result = coap_add_token(pdu, 5, (unsigned char *) "token"); CU_ASSERT(result == 0); coap_pdu_clear(pdu, old_max); /* restore PDU size */ }
void t_error_response8(void) { const unsigned char code = COAP_RESPONSE_CODE(503); unsigned char teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0xe0, 0x02, 0xdc, 0xd0, 0x00, 0xff, 'S', 'e', 'r', 'v', 'i', 'c', 'e', ' ', 'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e' }; coap_pdu_t *response; if (COAP_MAX_OPT < 2000) { CU_PASS("WARNING: bypassed due to COAP_MAX_OPT < 2000"); return; } coap_pdu_clear(pdu, pdu->max_size); pdu->hdr->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* known option 1000 */ coap_add_option(pdu, 1000, 0, NULL); /* unknown options 1001 and 1014 */ coap_add_option(pdu, 1001, 0, NULL); coap_add_option(pdu, 1014, 0, NULL); /* known option 2000 */ coap_add_option(pdu, 2000, 0, NULL); coap_option_filter_clear(opts); coap_option_setb(opts, 1001); coap_option_setb(opts, 1014); response = coap_new_error_response(pdu, code, opts); CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->length == sizeof(teststr)); CU_ASSERT(response->hdr->version == 1); CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); CU_ASSERT(response->hdr->token_length == 5); CU_ASSERT(response->hdr->code == code); CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); }
void t_encode_pdu1(void) { char teststr[] = { 0x45, 0x01, 0x12, 0x34, 't', 'o', 'k', 'e', 'n' }; int result; coap_pdu_clear(pdu, pdu->max_size); pdu->hdr->type = COAP_MESSAGE_CON; pdu->hdr->code = COAP_REQUEST_GET; pdu->hdr->id = htons(0x1234); result = coap_add_token(pdu, 5, (unsigned char *) "token"); CU_ASSERT(result == 1); CU_ASSERT(pdu->length = sizeof(teststr)); CU_ASSERT_PTR_NULL(pdu->data); CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); }
void check_async(coap_context_t *ctx, coap_tick_t now) { coap_pdu_t *response; coap_async_state_t *tmp; unsigned char buf[2]; size_t size = sizeof(coap_hdr_t) + 8; if (!async || now < async->created + (unsigned long)async->appdata) return; size += async->tokenlen; response = coap_pdu_init(async->flags & COAP_ASYNC_CONFIRM ? COAP_MESSAGE_CON : COAP_MESSAGE_NON, COAP_RESPONSE_CODE(205), 0, size); if (!response) { debug("check_async: insufficient memory, we'll try later\n"); async->appdata = (void *)((unsigned long)async->appdata + 15 * COAP_TICKS_PER_SECOND); return; } response->hdr->id = coap_new_message_id(ctx); if (async->tokenlen) coap_add_token(response, async->tokenlen, async->token); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); coap_add_data(response, 4, (unsigned char *)"done"); if (coap_send(ctx, &async->peer, response) == COAP_INVALID_TID) { debug("check_async: cannot send response for message %d\n", response->hdr->id); } coap_delete_pdu(response); coap_remove_async(ctx, async->id, &tmp); coap_free_async(async); async = NULL; }
static void t_error_response7(void) { const unsigned char code = COAP_RESPONSE_CODE(402); unsigned char optval[] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12 }; uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0xdd, 0x0a, 0x06, 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0xff, 'B', 'a', 'd', ' ', 'O', 'p', 't', 'i', 'o', 'n' }; coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); pdu->hdr->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (unsigned char *)"token"); /* known option 11 */ coap_add_option(pdu, 11, 4, (unsigned char *)"time"); /* unknown critical option 23 */ coap_add_option(pdu, 23, sizeof(optval), optval); coap_option_filter_clear(opts); coap_option_setb(opts, 23); response = coap_new_error_response(pdu, code, opts); CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->length == sizeof(teststr)); CU_ASSERT(response->hdr->version == 1); CU_ASSERT(response->hdr->type == COAP_MESSAGE_ACK); CU_ASSERT(response->hdr->token_length == 5); CU_ASSERT(response->hdr->code == code); CU_ASSERT(memcmp(response->hdr, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); }
static void t_error_response8(void) { const uint8_t code = COAP_RESPONSE_CODE(503); uint8_t teststr[] = { 0x65, code, 0x00, 0x00, 't', 'o', 'k', 'e', 'n', 0xe0, 0x02, 0xdc, 0xd0, 0x00, 0xff, 'S', 'e', 'r', 'v', 'i', 'c', 'e', ' ', 'U', 'n', 'a', 'v', 'a', 'i', 'l', 'a', 'b', 'l', 'e' }; coap_pdu_t *response; coap_pdu_clear(pdu, pdu->max_size); pdu->type = COAP_MESSAGE_CON; coap_add_token(pdu, 5, (const uint8_t *)"token"); /* known option 1000 */ coap_add_option(pdu, 1000, 0, NULL); /* unknown options 1001 and 1014 */ coap_add_option(pdu, 1001, 0, NULL); coap_add_option(pdu, 1014, 0, NULL); /* known option 2000 */ coap_add_option(pdu, 2000, 0, NULL); coap_option_filter_clear(opts); coap_option_setb(opts, 1001); coap_option_setb(opts, 1014); response = coap_new_error_response(pdu, code, opts); CU_ASSERT_PTR_NOT_NULL(response); CU_ASSERT(response->used_size == sizeof(teststr) - 4); CU_ASSERT(response->type == COAP_MESSAGE_ACK); CU_ASSERT(response->token_length == 5); CU_ASSERT(response->code == code); CU_ASSERT(coap_pdu_encode_header(response, COAP_PROTO_UDP) == 4); CU_ASSERT(memcmp(response->token - 4, teststr, sizeof(teststr)) == 0); coap_delete_pdu(response); }
static void send_async_response(coap_context_t *ctx, const coap_endpoint_t *local_if) { coap_pdu_t *response; unsigned char buf[3]; const char* response_data = "Hello World!"; size_t size = sizeof(coap_hdr_t) + 20; response = coap_pdu_init(async->flags & COAP_MESSAGE_CON, COAP_RESPONSE_CODE(205), 0, size); response->hdr->id = coap_new_message_id(ctx); if (async->tokenlen) coap_add_token(response, async->tokenlen, async->token); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); coap_add_data (response, strlen(response_data), (unsigned char *)response_data); if (coap_send(ctx, local_if, &async->peer, response) == COAP_INVALID_TID) { } coap_delete_pdu(response); coap_async_state_t *tmp; coap_remove_async(ctx, async->id, &tmp); coap_free_async(async); async = NULL; }
void t_encode_pdu5(void) { /* PDU with token and options */ char teststr[] = { 0x68, 0x84, 0x12, 0x34, '1', '2', '3', '4', '5', '6', '7', '8', 0x18, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0xd1, 0x03, 0x12 }; int result; coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ pdu->hdr->type = COAP_MESSAGE_ACK; pdu->hdr->code = COAP_RESPONSE_CODE(404); pdu->hdr->id = htons(0x1234); CU_ASSERT(pdu->length == 4); result = coap_add_token(pdu, 8, (unsigned char *) "12345678"); CU_ASSERT(pdu->length == 12); result = coap_add_option(pdu, COAP_OPTION_IF_MATCH, 8, (unsigned char *) "ABCDEFGH"); CU_ASSERT(result == 9); CU_ASSERT(pdu->max_delta == 1); CU_ASSERT(pdu->length == 21); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_ACCEPT, 1, (unsigned char *) "\x12"); CU_ASSERT(result == 3); CU_ASSERT(pdu->max_delta == 17); CU_ASSERT(pdu->length == 24); CU_ASSERT_PTR_NULL(pdu->data); CU_ASSERT(pdu->length == sizeof(teststr)); CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); }
/* * Function: _wilddog_coap_creat * Description: creat an coap package with no option and payload. * Input: p_arg: cmd/message/token/package len. * Output: N/A * Return: coap pointer. */ STATIC size_t WD_SYSTEM _wilddog_coap_creat ( Protocol_Arg_Creat_T *p_arg, int flag ) { coap_pdu_t *p_pdu = NULL; u8 type=0,code = 0; if(p_arg == NULL) return 0; /* get coap type and code.*/ if(_wilddog_coap_cmd2Typecode(p_arg->cmd,&type,&code) < 0) return 0; /*creat an coap package.*/ p_pdu = coap_pdu_init(type,code,p_arg->d_index,p_arg->d_packageLen); if(p_pdu == NULL) return 0; /* add token option.*/ coap_add_token(p_pdu, COAP_TOKENLEN, (u8*)&(p_arg->d_token)); wilddog_debug_level(WD_DEBUG_LOG,"\tcc\tcreat coap pakge :%p :",p_pdu); return ( size_t )p_pdu; }
coap_pdu_t* CACreatePDUforRequest(const code_t code, coap_list_t *options) { OIC_LOG(DEBUG, TAG, "CACreatePDUforRequest"); coap_pdu_t *pdu; coap_list_t *opt; unsigned char _token_data[8]; str the_token = { 0, _token_data }; if (!(pdu = coap_new_pdu())) return NULL; /* initialize message id */ unsigned short message_id; prng((unsigned char *)&message_id, sizeof(unsigned short)); pdu->hdr->type = msgtype; pdu->hdr->id = htons(++message_id); pdu->hdr->code = code; pdu->hdr->token_length = the_token.length; if (!coap_add_token(pdu, the_token.length, the_token.s)) { OIC_LOG(DEBUG, TAG, "cannot add token to request"); } for (opt = options; opt; opt = opt->next) { coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *)opt->data), COAP_OPTION_LENGTH(*(coap_option *)opt->data), COAP_OPTION_DATA(*(coap_option *)opt->data)); } return pdu; }
coap_pdu_t *CAGeneratePDUImpl(code_t code, coap_list_t *options, const CAInfo_t *info, const CAEndpoint_t *endpoint) { OIC_LOG(DEBUG, TAG, "IN"); VERIFY_NON_NULL_RET(info, TAG, "info", NULL); VERIFY_NON_NULL_RET(endpoint, TAG, "endpoint", NULL); coap_pdu_t *pdu = coap_new_pdu(); if (NULL == pdu) { OIC_LOG(ERROR, TAG, "malloc failed"); return NULL; } OIC_LOG_V(DEBUG, TAG, "msgID is %d", info->messageId); uint16_t message_id; if (0 == info->messageId) { /* initialize message id */ prng((uint8_t * ) &message_id, sizeof(message_id)); OIC_LOG_V(DEBUG, TAG, "gen msg id=%d", message_id); } else { /* use saved message id */ message_id = info->messageId; } pdu->hdr->id = message_id; OIC_LOG_V(DEBUG, TAG, "messageId in pdu is %d, %d", message_id, pdu->hdr->id); pdu->hdr->type = info->type; pdu->hdr->code = COAP_RESPONSE_CODE(code); if (info->token && CA_EMPTY != code) { uint32_t tokenLength = info->tokenLength; OIC_LOG_V(DEBUG, TAG, "token info token length: %d, token :", tokenLength); OIC_LOG_BUFFER(DEBUG, TAG, (const uint8_t *)info->token, tokenLength); int32_t ret = coap_add_token(pdu, tokenLength, (unsigned char *)info->token); if (0 == ret) { OIC_LOG(ERROR, TAG, "can't add token"); } } if (options) { for (coap_list_t *opt = options; opt; opt = opt->next) { OIC_LOG_V(DEBUG, TAG, "[%s] opt will be added.", COAP_OPTION_DATA(*(coap_option *) opt->data)); coap_add_option(pdu, COAP_OPTION_KEY(*(coap_option *) opt->data), COAP_OPTION_LENGTH(*(coap_option *) opt->data), COAP_OPTION_DATA(*(coap_option *) opt->data)); } } bool enabledPayload = false; #ifndef WITH_BWT enabledPayload = true; #endif if (enabledPayload || CA_ADAPTER_GATT_BTLE == endpoint->adapter) { if (NULL != info->payload && 0 < info->payloadSize) { OIC_LOG(DEBUG, TAG, "payload is added"); coap_add_data(pdu, info->payloadSize, (const unsigned char *) info->payload); } } OIC_LOG(DEBUG, TAG, "OUT"); return pdu; }
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 }
void t_encode_pdu10(void) { /* PDU with token, options and data */ char teststr[] = { 0x62, 0x44, 0x12, 0x34, 0x00, 0x00, 0x8d, 0xf2, 'c', 'o', 'a', 'p', ':', '/', '/', 'e', 'x', 'a', 'm', 'p', 'l', 'e', '.', 'c', 'o', 'm', '/', '1', '2', '3', '4', '5', '/', '%', '3', 'F', 'x', 'y', 'z', '/', '3', '0', '4', '8', '2', '3', '4', '2', '3', '4', '/', '2', '3', '4', '0', '2', '3', '4', '8', '2', '3', '4', '/', '2', '3', '9', '0', '8', '4', '2', '3', '4', '-', '2', '3', '/', '%', 'A', 'B', '%', '3', '0', '%', 'a', 'f', '/', '+', '1', '2', '3', '/', 'h', 'f', 'k', 's', 'd', 'h', '/', '2', '3', '4', '8', '0', '-', '2', '3', '4', '-', '9', '8', '2', '3', '5', '/', '1', '2', '0', '4', '/', '2', '4', '3', '5', '4', '6', '3', '4', '5', '3', '4', '5', '2', '4', '3', '/', '0', '1', '9', '8', 's', 'd', 'n', '3', '-', 'a', '-', '3', '/', '/', '/', 'a', 'f', 'f', '0', '9', '3', '4', '/', '9', '7', 'u', '2', '1', '4', '1', '/', '0', '0', '0', '2', '/', '3', '9', '3', '2', '4', '2', '3', '5', '3', '2', '/', '5', '6', '2', '3', '4', '0', '2', '3', '/', '-', '-', '-', '-', '/', '=', '1', '2', '3', '4', '=', '/', '0', '9', '8', '1', '4', '1', '-', '9', '5', '6', '4', '6', '4', '3', '/', '2', '1', '9', '7', '0', '-', '-', '-', '-', '-', '/', '8', '2', '3', '6', '4', '9', '2', '3', '4', '7', '2', 'w', 'e', 'r', 'e', 'r', 'e', 'w', 'r', '0', '-', '9', '2', '1', '-', '3', '9', '1', '2', '3', '-', '3', '4', '/', 0x0d, 0x01, '/', '/', '4', '9', '2', '4', '0', '3', '-', '-', '0', '9', '8', '/', 0xc1, '*', 0xff, 'd', 'a', 't', 'a' }; int result; coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ pdu->hdr->type = COAP_MESSAGE_ACK; pdu->hdr->code = COAP_RESPONSE_CODE(204); pdu->hdr->id = htons(0x1234); CU_ASSERT(pdu->length == 4); result = coap_add_token(pdu, 2, (unsigned char *) "\0\0"); CU_ASSERT(result > 0); result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 255, (unsigned char *) "coap://example.com/12345/%3Fxyz/3048234234/23402348234/239084234-23/%AB%30%af/+123/hfksdh/23480-234-98235/1204/243546345345243/0198sdn3-a-3///aff0934/97u2141/0002/3932423532/56234023/----/=1234=/098141-9564643/21970-----/82364923472wererewr0-921-39123-34/"); CU_ASSERT(result == 257); CU_ASSERT(pdu->max_delta == 8); CU_ASSERT(pdu->length == 263); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_LOCATION_PATH, 14, (unsigned char *) "//492403--098/"); CU_ASSERT(result == 16); CU_ASSERT(pdu->max_delta == 8); CU_ASSERT(pdu->length == 279); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_LOCATION_QUERY, 1, (unsigned char *) "*"); CU_ASSERT(result == 2); CU_ASSERT(pdu->max_delta == 20); CU_ASSERT(pdu->length == 281); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_data(pdu, 4, (unsigned char *) "data"); CU_ASSERT(result > 0); CU_ASSERT(pdu->length == 286); CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 282); CU_ASSERT(pdu->length == sizeof(teststr)); CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); }