coap_pdu_t * coap_new_pdu(coap_transport_type transport, unsigned int size) { coap_pdu_t *pdu; #ifndef WITH_CONTIKI pdu = coap_pdu_init(0, 0, ntohs(COAP_INVALID_TID), #ifndef WITH_TCP COAP_MAX_PDU_SIZE, #else size, #endif transport); #else /* WITH_CONTIKI */ pdu = coap_pdu_init(0, 0, uip_ntohs(COAP_INVALID_TID), #ifndef WITH_TCP COAP_MAX_PDU_SIZE, #else size, #endif transport); #endif /* WITH_CONTIKI */ #ifndef NDEBUG if (!pdu) coap_log(LOG_CRIT, "coap_new_pdu: cannot allocate memory for new PDU\n"); #endif return pdu; }
coap_pdu_t * coap_new_pdu(void) { coap_pdu_t *pdu; #ifndef WITH_CONTIKI pdu = coap_pdu_init(0, 0, ntohs((unsigned short)COAP_INVALID_TID), COAP_MAX_PDU_SIZE); #else /* WITH_CONTIKI */ pdu = coap_pdu_init(0, 0, uip_ntohs(COAP_INVALID_TID), COAP_MAX_PDU_SIZE); #endif /* WITH_CONTIKI */ #ifndef NDEBUG if (!pdu) coap_log(LOG_CRIT, "coap_new_pdu: cannot allocate memory for new PDU\n"); #endif return pdu; }
/* * 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 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 coap_pdu_t * make_pdu( unsigned int value ) { coap_pdu_t *pdu; unsigned char enc; static unsigned char buf[20]; int len, ls; if (!(pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU))) return NULL; pdu->type = COAP_MESSAGE_NON; pdu->code = COAP_REQUEST_POST; pdu->tid = id++; enc = COAP_PSEUDOFP_ENCODE_8_4_DOWN(value,ls); len = sprintf((char *)buf, "%c%u", enc, COAP_PSEUDOFP_DECODE_8_4(enc)); coap_add_data( pdu, len, buf ); return pdu; }
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; }
/* * 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; }
int t_error_response_tests_create(void) { pdu = coap_pdu_init(0, 0, 0, COAP_MAX_PDU_SIZE); return pdu == NULL; }
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 }
static int t_error_response_tests_create(void) { pdu = coap_pdu_init(0, 0, 0, COAP_DEFAULT_MTU); return pdu == NULL; }