This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

CoAP, CoAPS and UDP

Hi,

I use example projects  Thread: CoAP Client and  Thread: CoAP Server . I can send and receive data over CoAP. My purpose is to use CoAP, CoAPS and UDP tohether. If client send UDP message, server can reply with UDP payload or client send CoAPs, server reply same manner. How can I accomplish to send CoAPs and UDP data?

  • I'm not sure, what you exactly mean. Let me try to explain, how coap and coaps is handled in scenarios I know. coaps uses dtls the create encrypted coap-message as udp messages, while coap send the coap-message unencrypted directly over udp. This includes, that the client uses different "coap-endpoint", setup to sent the coap-message direct or encrpyted. Such a client-coap-endpoint is bound to a local port. The results in two coap-endpoints with different ports on the client. The server usually does the same, it offers on 5683 plain coap and on 5684 coaps (coap-dtls-udp). Some implementations (e.g. Eclipse/Californium) offer a (client-side) endpoint manager for that. Once setup, the client may just use either coap or coaps as scheme and the the coap-client will do the magic for you.

    So in short: use different coap-endpoints with different ports and different configurations.

  • Thanks, but it is not my question. 

    My question is that: How can I modify  Thread: CoAP Client and  Thread: CoAP Server examples? There is no written code such as coaps.c or udp.c.  I can just use coap codes. I can send and receive message with just by using CoAP.

    There is a constant for secure

    #define OPENTHREAD_CONFIG_COAP_SECURE_API_ENABLE 0

    I must set it to 1 but after that ? How to modify code? What is the simplest way? Some codes are written in C++. But I just know C.

  • Hi,

    Support for CoAP, CoAPS, and UDP is all part of the prebuilt OpenThread libraries.

    You can find the API reference in our infocenter:

    All these APIs can be used in the application by including the corresponding header file, e.g:

    • #include <openthread/coap.h>
    • #include <openthread/coaps.h>
    • #include <openthread/udp.h>

    You can have a look at the use of the CoAP API usage in "examples\thread\app_utils\thread_coap_utils.c / thread_coap_utils.h"

    Best regards,
    Jørgen

  • Hey,

    i did something very similar. I modified the CoAP examples, so i can used it for plain UDP transport.

    Sending is working... fine.

    If i send a message to my device my connected receiver callback is triggered, also fine. The otMessageInfo  shows the correct sender and receiver IPv6 addresses.

    But the message-pointer is pointing anywhere.

    static bool UDPReceiverCallback(void *aContext, otMessage* aMessage, const otMessageInfo *aMessageInfo) {
      volatile otMessage* receivedMessage = aMessage;
      uint8_t buffer[64];  
      volatile uint32_t x =  aMessage->mNext;
      memcpy(&buffer, aMessage->mNext, 64);
      volatile otMessageInfo receivedMessageInfo = *(aMessageInfo);
     
      dk_set_led_on(DONGLE_GREEN_MULTI_LED);
    }

    I just send a simple string "Hello".



    How do i handle the message? Is there any example?

    I just want to reconstruct the "Hello" string

    regards,

    Sören

  • Hi,

    UDP is implemented in the CLI example. You can have a look at the implementation in OpenThread to see how received messages are parsed.

    Best regards,
    Jørgen

Related