void hnd_get_rd(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response, void *userdata) { unsigned char buf[3]; response->hdr->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf); coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf); }
static void make_bad_request(CoAPCallback &callback, int code) { unsigned char buf[3]; coap_pdu_t *response = (coap_pdu_t*)callback.response_; response->hdr->code = COAP_RESPONSE_CODE(code); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf); coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf); }
void hnd_get_index(coap_context_t *ctx, struct coap_resource_t *resource, coap_address_t *peer, coap_pdu_t *request, str *token, coap_pdu_t *response) { unsigned char buf[3]; response->hdr->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); coap_add_option(response, COAP_OPTION_MAXAGE, coap_encode_var_bytes(buf, 0x2ffff), buf); coap_add_data(response, strlen(INDEX), (unsigned char *)INDEX); }
void t_encode_pdu4(void) { /* PDU with options */ char teststr[] = { 0x60, 0x99, 0x12, 0x34, 0x3d, 0x05, 0x66, 0x61, 0x6e, 0x63, 0x79, 0x70, 0x72, 0x6f, 0x78, 0x79, 0x2e, 0x63, 0x6f, 0x61, 0x70, 0x2e, 0x6d, 0x65, 0x84, 0x70, 0x61, 0x74, 0x68, 0x00, 0xe8, 0x1e, 0x28, 0x66, 0x61, 0x6e, 0x63, 0x79, 0x6f, 0x70, 0x74 }; int result; coap_pdu_clear(pdu, pdu->max_size); /* clear PDU */ pdu->hdr->type = COAP_MESSAGE_ACK; pdu->hdr->code = 0x99; pdu->hdr->id = htons(0x1234); CU_ASSERT(pdu->length == 4); result = coap_add_option(pdu, COAP_OPTION_URI_HOST, 18, (unsigned char *) "fancyproxy.coap.me"); CU_ASSERT(result == 20); CU_ASSERT(pdu->max_delta == 3); CU_ASSERT(pdu->length == 24); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 4, (unsigned char *) "path"); CU_ASSERT(result == 5); CU_ASSERT(pdu->max_delta == 11); CU_ASSERT(pdu->length == 29); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_URI_PATH, 0, NULL); CU_ASSERT(result == 1); CU_ASSERT(pdu->max_delta == 11); CU_ASSERT(pdu->length == 30); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, 8000, 8, (unsigned char *) "fancyopt"); CU_ASSERT(result == 11); CU_ASSERT(pdu->max_delta == 8000); CU_ASSERT(pdu->length == 41); CU_ASSERT_PTR_NULL(pdu->data); CU_ASSERT(pdu->length == sizeof(teststr)); CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); }
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 CoAP_RD_Resource::response_ok(CoAPResource *node, CoAPCallback &callback) { #define LOCSIZE 68 coap_pdu_t *response = (coap_pdu_t*)callback.response_; /* 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((unsigned char*)node->uri().c_str(), node->uri().length(), 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 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); }
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); }
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); }
int coap_write_block_opt(coap_block_t *block, unsigned short type, coap_pdu_t *pdu, size_t data_length) { size_t start, want, avail; unsigned char buf[3]; assert(pdu); /* Block2 */ if (type != COAP_OPTION_BLOCK2) { warn("coap_write_block_opt: skipped unknown option\n"); return -1; } start = block->num << (block->szx + 4); if (data_length <= start) { debug("illegal block requested\n"); return -2; } avail = pdu->max_size - pdu->length - 4; want = 1 << (block->szx + 4); /* check if entire block fits in message */ if (want <= avail) { block->m = want < data_length - start; } else { /* Sender has requested a block that is larger than the remaining * space in pdu. This is ok if the remaining data fits into the pdu * anyway. The block size needs to be adjusted only if there is more * data left that cannot be delivered in this message. */ if (data_length - start <= avail) { /* it's the final block and everything fits in the message */ block->m = 0; } else { unsigned char szx; /* we need to decrease the block size */ if (avail < 16) { /* bad luck, this is the smallest block size */ debug("not enough space, even the smallest block does not fit"); return -3; } debug("decrease block size for %d to %d\n", avail, coap_fls(avail) - 5); szx = block->szx; block->szx = coap_fls(avail) - 5; block->m = 1; block->num <<= szx - block->szx; } } /* to re-encode the block option */ coap_add_option(pdu, type, coap_encode_var_bytes(buf, ((block->num << 4) | (block->m << 3) | block->szx)), buf); return 1; }
void t_encode_pdu9(void) { /* PDU with options and data */ char teststr[] = { 0x60, 0x44, 0x12, 0x34, 0x48, 's', 'o', 'm', 'e', 'e', 't', 'a', 'g', 0x10, 0xdd, 0x11, 0x04, 's', 'o', 'm', 'e', 'r', 'a', 't', 'h', 'e', 'r', 'l', 'o', 'n', 'g', 'u', 'r', 'i', 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_option(pdu, COAP_OPTION_ETAG, 8, (unsigned char *) "someetag"); CU_ASSERT(result == 9); CU_ASSERT(pdu->max_delta == 4); CU_ASSERT(pdu->length == 13); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_IF_NONE_MATCH, 0, NULL); CU_ASSERT(result == 1); CU_ASSERT(pdu->max_delta == 5); CU_ASSERT(pdu->length == 14); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_option(pdu, COAP_OPTION_PROXY_URI, 17, (unsigned char *) "someratherlonguri"); CU_ASSERT(result == 20); CU_ASSERT(pdu->max_delta == 35); CU_ASSERT(pdu->length == 34); CU_ASSERT_PTR_NULL(pdu->data); result = coap_add_data(pdu, 4, (unsigned char *) "data"); CU_ASSERT(result > 0); CU_ASSERT(pdu->length == 39); CU_ASSERT(pdu->data == (unsigned char *) pdu->hdr + 35); CU_ASSERT(pdu->length == sizeof(teststr)); CU_ASSERT(memcmp(pdu->hdr, teststr, sizeof(teststr)) == 0); }
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); }
// Put ordered list into pdu void coap_add_options(coap_pdu_t *pdu) { coap_list_t *option; for (option = optlist; option; option = option->next ) { coap_add_option ( pdu, COAP_OPTION_KEY(*(coap_option *)option->data), COAP_OPTION_LENGTH(*(coap_option *)option->data), COAP_OPTION_DATA(*(coap_option *)option->data) ); } }
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); }
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, void *userdata) { rd_t *rd = NULL; unsigned char buf[3]; HASH_FIND(hh, resources, resource->key, sizeof(coap_key_t), rd); response->hdr->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_APPLICATION_LINK_FORMAT), buf); if (rd && rd->etag_len) coap_add_option(response, COAP_OPTION_ETAG, rd->etag_len, rd->etag); if (rd && rd->data.s) coap_add_data(response, rd->data.length, rd->data.s); }
/* * Function: _wilddog_coap_addPath * Description: add host to coap packages. * Input: p_arg: p_coap/p_options. * Output: N/A * Return: WILDDOG_ERR_NOERR or WILDDOG_ERR_NULL. */ STATIC Wilddog_Return_T WD_SYSTEM _wilddog_coap_addPath ( Protocol_Arg_Option_T *p_arg, int flag ) { u8 *p_subpath_h = NULL; u8 *p_subpath_end = NULL; if( p_arg == NULL || p_arg->p_pkg == NULL || p_arg->p_options == NULL) return WILDDOG_ERR_NULL; /* @add path */ for(p_subpath_h= p_arg->p_options;p_subpath_h;) { p_subpath_h = (u8*)_wilddog_strchar(( char *)p_subpath_h,'/'); if(!p_subpath_h) break; p_subpath_h++; p_subpath_end = (u8*)_wilddog_strchar((char *)p_subpath_h,'/'); if( !p_subpath_end ) { coap_add_option((coap_pdu_t*) p_arg->p_pkg, \ COAP_OPTION_URI_PATH, \ strlen((const char *)p_subpath_h), \ p_subpath_h); break; } else coap_add_option((coap_pdu_t*) p_arg->p_pkg, \ COAP_OPTION_URI_PATH, \ p_subpath_end - p_subpath_h, \ p_subpath_h); } return WILDDOG_ERR_NOERR; }
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); }
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_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_addQuery * Description: add query. * Input: p_arg: p_coap/p_options. * Output: N/A * Return: WILDDOG_ERR_NOERR or WILDDOG_ERR_NULL. */ STATIC Wilddog_Return_T WD_SYSTEM _wilddog_coap_addQuery ( Protocol_Arg_Option_T *p_arg, int flag ) { if( p_arg == NULL || p_arg->p_pkg == NULL || p_arg->p_options == NULL) return WILDDOG_ERR_NULL; coap_add_option((coap_pdu_t*)p_arg->p_pkg, \ COAP_OPTION_URI_QUERY, \ strlen((const char *)p_arg->p_options), \ p_arg->p_options); return WILDDOG_ERR_NOERR; }
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; }
/* * Function: _wilddog_coap_addObserver * Description: add query. * Input: p_arg: p_coap/p_options. * Output: N/A * Return: WILDDOG_ERR_NOERR or WILDDOG_ERR_NULL. */ STATIC Wilddog_Return_T WD_SYSTEM _wilddog_coap_addObserver ( Protocol_Arg_Option_T *p_arg, int flag ) { if( p_arg == NULL || p_arg->p_pkg == NULL || p_arg->p_options == NULL) { return WILDDOG_ERR_NULL; } coap_add_option((coap_pdu_t*)p_arg->p_pkg,\ COAP_OPTION_OBSERVE, \ 1, \ p_arg->p_options); return WILDDOG_ERR_NOERR; }
void hnd_get_query(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_filter_t f; coap_opt_t *q; size_t len, L; unsigned char buf[70]; (void)ctx; (void)resource; (void)peer; (void)token; response->hdr->code = COAP_RESPONSE_CODE(205); coap_add_option(response, COAP_OPTION_CONTENT_TYPE, coap_encode_var_bytes(buf, COAP_MEDIATYPE_TEXT_PLAIN), buf); coap_option_filter_clear(f); coap_option_setb(f, COAP_OPTION_URI_QUERY); coap_option_iterator_init(request, &opt_iter, f); len = 0; while ((len < sizeof(buf)) && (q = coap_option_next(&opt_iter))) { L = min(sizeof(buf) - len, 11); memcpy(buf + len, "Uri-Query: ", L); len += L; L = min(sizeof(buf) - len, COAP_OPT_LENGTH(q)); memcpy(buf + len, COAP_OPT_VALUE(q), L); len += L; if (len < sizeof(buf)) buf[len++] = '\n'; } coap_add_data(response, len, buf); }
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; }
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; }
int main(void) { char alias[] = "temp"; char cik[] = "a32c85ba9dda45823be416246cf8b433baa068d7"; char host[] = "coap.exosite.com"; char port[] = "5683"; srand(time(NULL)); // CoAP Message Setup #define MSG_BUF_LEN 64 uint8_t msg_send_buf[MSG_BUF_LEN]; coap_pdu msg_send = {msg_send_buf, 0, 64}; uint8_t msg_recv_buf[MSG_BUF_LEN]; coap_pdu msg_recv = {msg_recv_buf, 0, 64}; uint16_t message_id_counter = rand(); // Socket to Exosite int remotesock; size_t bytes_sent; ssize_t bytes_recv; int rv; struct addrinfo exohints, *servinfo, *q; memset(&exohints, 0, sizeof exohints); exohints.ai_family = AF_UNSPEC; exohints.ai_socktype = SOCK_DGRAM; exohints.ai_flags = AI_PASSIVE; if ((rv = getaddrinfo(host, port, &exohints, &servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } // loop through all the results and make a socket for(q = servinfo; q != NULL; q = q->ai_next) { if ((remotesock = socket(q->ai_family, q->ai_socktype, q->ai_protocol)) == -1) { perror("bad socket"); continue; } break; } if (q == NULL) { fprintf(stderr, "Failed to Bind Socket\n"); return 2; } for (;;) { printf("--------------------------------------------------------------------------------\n"); // Build Message coap_init_pdu(&msg_send); //memset(msg_send, 0, msg_send_len); coap_set_version(&msg_send, COAP_V1); coap_set_type(&msg_send, CT_CON); coap_set_code(&msg_send, CC_GET); // or POST to write coap_set_mid(&msg_send, message_id_counter++); coap_set_token(&msg_send, rand(), 2); coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)"1a", 2); coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)alias, strlen(alias)); coap_add_option(&msg_send, CON_URI_QUERY, (uint8_t*)cik, strlen(cik)); // to write, set payload: //coap_set_payload(msg_send, &msg_send_len, MSG_BUF_LEN, (uint8_t*)"99", 2); // Send Message if ((bytes_sent = sendto(remotesock, msg_send.buf, msg_send.len, 0, q->ai_addr, q->ai_addrlen)) == -1){ fprintf(stderr, "Failed to Send Message\n"); return 2; } printf("Sent.\n"); coap_pretty_print(&msg_send); // Wait for Response bytes_recv = recvfrom(remotesock, (void *)msg_recv.buf, msg_recv.max, 0, q->ai_addr, &q->ai_addrlen); if (bytes_recv < 0) { fprintf(stderr, "%s\n", strerror(errno)); exit(EXIT_FAILURE); } msg_recv.len = bytes_recv; if(coap_validate_pkt(&msg_recv) == CE_NONE) { printf("Got Valid CoAP Packet\n"); if(coap_get_mid(&msg_recv) == coap_get_mid(&msg_send) && coap_get_token(&msg_recv) == coap_get_token(&msg_send)) { printf("Is Response to Last Message\n"); coap_pretty_print(&msg_recv); } }else{ printf("Received %zi Bytes, Not Valid CoAP\n", msg_recv.len); hex_dump(msg_recv.buf, msg_recv.len); } sleep(1); // One Second } }
void notify(coap_context_t *context, coap_resource_t *res, coap_subscription_t *sub, unsigned int duration, int code) { coap_pdu_t *pdu; int ls, finished=0; unsigned char ct, d; unsigned int length; #ifndef NDEBUG char addr[INET6_ADDRSTRLEN]; #endif if ( !context || !res || !sub || !(pdu = coap_new_pdu()) ) return; pdu->hdr->type = COAP_MESSAGE_CON; pdu->hdr->id = rand(); /* use a random transaction id */ pdu->hdr->code = code; /* FIXME: content-type and data (how about block?) */ if (res->uri->na.length) coap_add_option (pdu, COAP_OPTION_URI_AUTHORITY, res->uri->na.length, res->uri->na.s ); if (res->uri->path.length) coap_add_option (pdu, COAP_OPTION_URI_PATH, res->uri->path.length, res->uri->path.s); d = COAP_PSEUDOFP_ENCODE_8_4_DOWN(duration, ls); coap_add_option ( pdu, COAP_OPTION_SUBSCRIPTION, 1, &d ); if (sub->token.length) { coap_add_option (pdu, COAP_OPTION_TOKEN, sub->token.length, sub->token.s); } if (res->uri->query.length) coap_add_option (pdu, COAP_OPTION_URI_QUERY, res->uri->query.length, res->uri->query.s ); if (res->data) { length = (unsigned char *)pdu->hdr + COAP_MAX_PDU_SIZE - pdu->data; ct = res->mediatype; res->data(res->uri, &ct, 0, pdu->data, &length, &finished); pdu->length += length; /* TODO: add block option if not finished */ /* TODO: add mediatype */ } #ifndef NDEBUG if ( inet_ntop(AF_INET6, &(sub->subscriber.sin6_addr), addr, INET6_ADDRSTRLEN) ) { debug("*** notify for %s to [%s]:%d\n", res->uri->path.s, addr, ntohs(sub->subscriber.sin6_port)); } #endif if ( pdu && coap_send_confirmed(context, &sub->subscriber, pdu ) == COAP_INVALID_TID ) { debug("coap_check_resource_list: error sending notification\n"); coap_delete_pdu(pdu); } }
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 ICACHE_FLASH_ATTR coap_send(void *pvParameters) //int coap_send(void) { char *url = "coap.me"; int port = 5683; uint16_t message_id_counter = rand(); printf("%d,%d",message_id_counter, port); int sock; int bytes_sent; int bytes_recv; struct hostent *host; struct sockaddr_in server_addr; /* 通过函数入口参数url获得host地址*/ host= (struct hostent *) gethostbyname(url); printf("here"); /* 创建一个socket,类型是SOCK_DGRAM,UDP类型 */ if ((sock = socket(AF_INET, SOCK_DGRAM, 0)) == -1) { printf("Socket Error\n"); } printf("here2"); /* 初始化预连接的服务端地址 */ server_addr.sin_family = AF_INET; server_addr.sin_port = htons(port); server_addr.sin_addr = *((struct in_addr *)host->h_addr); memset(&(server_addr.sin_zero), 0, sizeof(server_addr.sin_zero)); printf("--------------------\r\n"); // Build Message coap_init_pdu(&msg_send); //memset(msg_send, 0, msg_send_len); coap_set_version(&msg_send, COAP_V1); coap_set_type(&msg_send, CT_CON); coap_set_code(&msg_send, CC_GET); // or POST to write coap_set_mid(&msg_send, message_id_counter++); // coap_set_token(&msg_send, rand(), 2); coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)"hello", strlen("hello")); // coap_add_option(&msg_send, CON_URI_PATH, (uint8_t*)alias, strlen(alias)); // coap_add_option(&msg_send, CON_URI_QUERY, (uint8_t*)cik, strlen(cik)); // to write, set payload: //coap_set_payload(msg_send, &msg_send_len, MSG_BUF_LEN, (uint8_t*)"99", 2); // Send Message if ((bytes_sent = sendto(sock, msg_send.buf, msg_send.len, 0, (struct sockaddr *)&server_addr, sizeof(struct sockaddr))) == -1) { printf("Failed to Send Message\r\n"); } else{ printf("bytes %d\n",bytes_sent); } socklen_t len = sizeof(server_addr); bytes_recv = recvfrom(sock, (void *)msg_recv.buf, msg_recv.max, 0, (struct sockaddr *)&server_addr,&len); if (bytes_recv < 0) { printf("game over baby\n"); } else{ printf("yuhu\n"); } msg_recv.len = bytes_recv; if(coap_validate_pkt(&msg_recv) == CE_NONE) { printf("Got Valid CoAP Packet\n"); if(coap_get_mid(&msg_recv) == coap_get_mid(&msg_send) && coap_get_token(&msg_recv) == coap_get_token(&msg_send)) { printf("Is Response to Last Message\n"); coap_pretty_print(&msg_recv); } }else{ printf("Received %zi Bytes, Not Valid CoAP\n", msg_recv.len); } vTaskDelete(NULL); }
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; }