This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

Assign both CoAP client and server roles to a node in nRF_Connect_SDK

Hi,

In nRF_Connect_SDK's OpenThread's Coap_Client sample, I want to implement the role of both client and server on a single node.

Also instead of sending messages to CoAP_Server, I want to send messages to my own CoAP server, whose address I have specified in the application. Following is the application code...

#define BORDER_ROUTER_IP "2001:db8:1:ffff::a00:1"
#define COMMON_RESOURCE "proxy/zenedge"
#define EUID "u=64088dce2d8f7568"
#define PACKET_VERSION "pv=1"
#define BOOTSTRAP "pt=b"

#define BOOTSTRAP_MESSAGE_FORMAT "o=2&i=1&t=1&2=10&o=2&i=2&t=2&2=11&o=2&i=5&t=4&2=11&o=2&i=6&t=5&2=7&o=3&i=4&t=7&2=5&o=3&i=7&t=8&2=12&o=2&i=8&t=3&2=11&o=2&i=11&t=22&2=5&o=2&i=12&t=31&2=11"

/**
 * @brief Bootstrap callback
 * @param aContext Not used
 * @param aMessage coap message
 * @param aMessageInfo Not used
 * @param aError  Error happened during receiving of coap message
 */
void bootStrapCallBack(
        void *aContext,
        otMessage *aMessage,
        const otMessageInfo *aMessageInfo,
        otError aError) {
    uint32_t length = otMessageGetLength(aMessage) - otMessageGetOffset(aMessage);
    uint8_t buff_callback[256];

    if (aError != OT_ERROR_NONE) {
        LOG_INF("(bootstrap call_back) Error receiving coap response message: Error %d: %s\r\n", aError,
                otThreadErrorToString(aError));
    } else {
        if (otCoapMessageGetCode(aMessage) == OT_COAP_CODE_CONTENT) {

            LOG_INF("(bootstrap call_back) Response received successfully\n\r");
            if (length < 256) {
                otMessageRead(aMessage, otMessageGetOffset(aMessage), buff_callback, length);
                LOG_INF("%s\n\r", buff_callback);
            } else {
                LOG_INF("Response length too lengthy\n");
            }
        } else {
            LOG_INF("Response code is %d,  %s\r\n", otCoapMessageGetCode(aMessage),
                    otThreadErrorToString(otCoapMessageGetCode(aMessage)));
        }
    }
    OT_UNUSED_VARIABLE(aContext);
    OT_UNUSED_VARIABLE(aMessageInfo);
}

/**
 * @brief send coap packet for bootstraping
 */
void coap_send_bootstrap_message() {

    char buff[512];
    char *bootstrap_message_payload = NULL;
    sprintf(buff, BOOTSTRAP_MESSAGE_FORMAT);
    bootstrap_message_payload = (char *)malloc(512);
    strcpy(bootstrap_message_payload, buff);

    otMessageInfo message_info;
    otMessage *p_message;
    otError error = OT_ERROR_NONE;
    do {
        p_message = otCoapNewMessage(openthread_get_default_instance(), NULL);

        otCoapMessageInit(p_message, OT_COAP_TYPE_CONFIRMABLE, OT_COAP_CODE_POST);
        otCoapMessageGenerateToken(p_message, 2);
        otCoapMessageAppendUriPathOptions(p_message, COMMON_RESOURCE);
        otCoapMessageAppendUriQueryOption(p_message, EUID);
        otCoapMessageAppendUriQueryOption(p_message, PACKET_VERSION);
        otCoapMessageAppendUriQueryOption(p_message, BOOTSTRAP);
        otCoapMessageSetPayloadMarker(p_message);
        if (p_message == NULL) {
            LOG_INF("Failed to allocate config message for CoAP Request\r\n");
            break;
        }
        error = otMessageAppend(p_message, bootstrap_message_payload, strlen(bootstrap_message_payload));
        if (error != OT_ERROR_NONE) {
            break;
        }
        memset(&message_info, 0, sizeof(message_info));
        message_info.mPeerPort = COAP_PORT;
        otIp6AddressFromString(BORDER_ROUTER_IP, &message_info.mPeerAddr);

        error = otCoapSendRequest(openthread_get_default_instance(), p_message, &message_info, bootStrapCallBack, NULL);

    } while (false);

    if (error != OT_ERROR_NONE && p_message != NULL) {
        LOG_INF("Failed to send config CoAP Request: %d\r\n", error);
        otMessageFree(p_message);
    } else {
        LOG_INF("Packet sent successfully");
    }
    return error;
}

As per logs, my packet is being sent, but I don't receive any packet on my CoAP Server interface.

can you suggest if I need to enable any specific configuration in prj.conf or anywhere in the code.

Parents
  • Hi

    Then there are be multiple possible issues.
    Let us try to debug these two first:

    1. Something could be wrong with the server. Try to use another CoAP client to send a message to your server. For example https://coap.me/.
    2. Maybe the  Border Router does not connect the OpenThread network to the internet. Use the client in one of your OpenThread samples to do the following commands:
      ot ipaddr
      ot ping 64:ff9b::0808:0808

    Regards,
    Sigurd Hellesvik

  • Also, now I am receiving the following error in my logs...

    Please explain the meaning of the following two crashes in the code:

    1.

     

    [00:00:11.100,555] <inf> coap_client_utils: Send bootstrap message
    [00:00:11.100,799] <err> os: ***** BUS FAULT *****
    [00:00:11.100,799] <err> os:   Precise data bus error
    [00:00:11.100,799] <err> os:   BFAR Address: 0x786f7270
    [00:00:11.100,830] <err> os: r0/a1:  0x786f7270  r1/a2:  0x2000dde9  r2/a3:  0x786f7270
    [00:00:11.100,830] <err> os: r3/a4:  0x786f7270 r12/ip:  0x000000e6 r14/lr:  0x00016259
    [00:00:11.100,830] <err> os:  xpsr:  0x21000000
    [00:00:11.100,830] <err> os: s[ 0]:  0x00000000  s[ 1]:  0x00000000  s[ 2]:  0x00000000  s[ 3]:  0x00000000
    [00:00:11.100,860] <err> os: s[ 4]:  0x00000000  s[ 5]:  0x00000000  s[ 6]:  0x00000000  s[ 7]:  0xffffffff
    [00:00:11.100,891] <err> os: s[ 8]:  0x00000000  s[ 9]:  0x00000000  s[10]:  0x00000000  s[11]:  0x00000000
    [00:00:11.100,891] <err> os: s[12]:  0x00000000  s[13]:  0x00000000  s[14]:  0x00000000  s[15]:  0x00000000
    [00:00:11.100,891] <err> os: fpscr:  0x20016e80
    [00:00:11.100,921] <err> os: Faulting instruction address (r15/pc): 0x00004142
    [00:00:11.100,921] <err> os: >>> ZEPHYR FATAL ERROR 0: CPU exception on CPU 0
    [00:00:11.100,921] <err> os: Current thread: 0x20002280 (sysworkq)
    [00:00:11.716,156] <err> fatal_error: Resetting system

    2.

    [00:00:21.605,407] <inf> coap_client_utils: role :
    [00:01:48.871,276] <inf> coap_client_utils: Send multicast mesh 'light' request
    [00:01:48.871,795] <err> os: ***** MPU FAULT *****
    [00:01:48.871,795] <err> os:   Stacking error (context area might be not valid)
    [00:01:48.871,826] <err> os: r0/a1:  0x2000d144  r1/a2:  0x00000000  r2/a3:  0x00000008
    [00:01:48.871,826] <err> os: r3/a4:  0x0001db71 r12/ip:  0x0000cd7c r14/lr:  0x61000000
    [00:01:48.871,826] <err> os:  xpsr:  0x00000000
    [00:01:48.871,856] <err> os: s[ 0]:  0x00000000  s[ 1]:  0x00000000  s[ 2]:  0x00000000  s[ 3]:  0x00000000
    [00:01:48.871,856] <err> os: s[ 4]:  0x00000000  s[ 5]:  0xffffffff  s[ 6]:  0x00000000  s[ 7]:  0x00000000
    [00:01:48.871,856] <err> os: s[ 8]:  0x00000000  s[ 9]:  0x00000000  s[10]:  0x00000000  s[11]:  0x00000000
    [00:01:48.871,887] <err> os: s[12]:  0x00000000  s[13]:  0x00000000  s[14]:  0x00000000  s[15]:  0x00000000
    [00:01:48.871,917] <err> os: fpscr:  0x2000d144
    [00:01:48.871,917] <err> os: Faulting instruction address (r15/pc): 0x00000001
    [00:01:48.871,917] <err> os: >>> ZEPHYR FATAL ERROR 2: Stack overflow on CPU 0
    [00:01:48.871,948] <err> os: Current thread: 0x20002280 (sysworkq)
    [00:01:49.436,157] <err> fatal_error: Resetting system

  • Hi

    I do not know what cause the error messages now. But let us have a look at the internet connection first:

    The response from the pings:

    1 packets transmitted, 0 packets received. Packet loss = 100.0%.

    This means that all packets are lost.
    The address you try to ping is an IPv4 address for Google through a IPv6 "bridge".
    Since all packets were lost, this could indicate that you do not have any internet connectivity.

    This will be a problem if the server you intend to communicate with devices outside your thread network.
    Do you need to connect to the internet with your thread devices? (For example to reach an external coap server)
    If you do, we will need to fix the border router.

    Regards,
    Sigurd Hellesvik

  • No, I don't need to connect to the internet for reaching my CoAP server. It is on my border router which is part of my thread network. 

  • It looks like you are using the IP "2001:db8:1:ffff::a00:1" for your CoAP server?

    This will be the public IP of the Border Router (BR), which will face the internet, and your Thread devices will not be able to find it.

    If you do "ip a" (on the Raspberry Pi) you will get a list of the other IP addresses of the BR.
    If you find the address here which face the Thread network, try to use that instead.
    Likely it will be one of the ones you listed:

    > ipaddr
    fdfe:ee41:832:1c64:0:ff:fe00:fc11
    fdfe:ee41:832:1c64:0:ff:fe00:fc10
    fdfe:ee41:832:1c64:0:ff:fe00:fc38
    fdf3:cf88:3e4e:ad4d:70e1:dc2a:eb65:e787
    fdfe:ee41:832:1c64:0:ff:fe00:7c00
    fdfe:ee41:832:1c64:2104:5e7:bb29:898a
    fe80:0:0:0:b458:3788:3659:13e2
    Done

    Regards,
    Sigurd Hellesvik

Reply
  • It looks like you are using the IP "2001:db8:1:ffff::a00:1" for your CoAP server?

    This will be the public IP of the Border Router (BR), which will face the internet, and your Thread devices will not be able to find it.

    If you do "ip a" (on the Raspberry Pi) you will get a list of the other IP addresses of the BR.
    If you find the address here which face the Thread network, try to use that instead.
    Likely it will be one of the ones you listed:

    > ipaddr
    fdfe:ee41:832:1c64:0:ff:fe00:fc11
    fdfe:ee41:832:1c64:0:ff:fe00:fc10
    fdfe:ee41:832:1c64:0:ff:fe00:fc38
    fdf3:cf88:3e4e:ad4d:70e1:dc2a:eb65:e787
    fdfe:ee41:832:1c64:0:ff:fe00:7c00
    fdfe:ee41:832:1c64:2104:5e7:bb29:898a
    fe80:0:0:0:b458:3788:3659:13e2
    Done

    Regards,
    Sigurd Hellesvik

Children
Related