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 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; }