Sign In

  • Username:
  • Password:

Upload File

  •  

Create COAP Request in libcoap_1

int coap_send_request(unsigned int method, uint8_t dst_short, unsigned char * uri_path, uint8_t payload_len, unsigned char * payload)
{
    coap_address_t dst;
    coap_address_init(&dst);

    uint8_t addr_u8[16] = {0x20, 0x01, 0x0d, 0xb8, 0x12, 0x34, 0xff, 0xff, 0x00, 0x00, 0x00, 0xff, 0xfe, 0x00, 0x00, dst_short};
    uint16_t addr_u16[8] = {0x0120, 0xb80d, 0x3412, 0xffff, 0x0000, 0xff00, 0x00fe, (uint16_t)dst_short<<8};
    memcpy(dst.addr.u8, addr_u8, sizeof(addr_u8));
    memcpy(dst.addr.u16, addr_u16, sizeof(addr_u16));
    dst.port = UIP_HTONS(COAP_DEFAULT_PORT);

    char uri_host[40];
    sprintf(uri_host, "2001:0db8:1234:ffff:0000:00ff:fe00:%x", dst_short);

    coap_pdu_t * req;
    req = coap_new_pdu();
    req->hdr->type = COAP_MESSAGE_NON;
    req->hdr->id = coap_new_message_id(coap_context);
    req->hdr->code = method;

    coap_add_option(req, COAP_OPTION_URI_HOST, strlen(uri_host), (unsigned char *)uri_host);
    coap_add_option(req, COAP_OPTION_URI_PATH, strlen((const char *)uri_path), uri_path);
    coap_add_data(req, payload_len, (const unsigned char *)payload);
    int res = coap_send(coap_context, &dst, req);
    coap_delete_pdu(req);
    return res;
}