CoAP Server Example is not working
Answer
6/13/19 9:28 AM

Dear Community,

I'm implementing a CoAP Rest Server in my XDK for the project that I'm developing. I based my code on the example provided in the developers page ("https://developer.bosch.com/web/xdk/coap") and after I clean, build and flash it in my XDK it runs with no errors, but when I make a Client request the server doesn't respond. I made a manual debug with simple printf and I discovered that the "coapReceiveCallback" function is not being ran. When I try to implement a Client on XDK it works perfectly, so the Client request isn't the problem, because if I make the same request to another server I have a response.

I will leave my code down below and my application.mk file. I need to solve this problem ASAP. Can someone help me with this? Please take a look at my code.

Thank you.

Best Regards

Pedro S.

#include "XdkAppInfo.h"
#undef BCDS_MODULE_ID  /* Module ID define before including Basics package*/
#define BCDS_MODULE_ID XDK_APP_MODULE_ID_APP_CONTROLLER

#include <stdio.h>

#include "BCDS_CmdProcessor.h"
#include "FreeRTOS.h"

#include "BCDS_WlanConnect.h"
#include "BCDS_NetworkConfig.h"
#include "BCDS_ServalPal.h"
#include "BCDS_ServalPalWiFi.h"

#include "Serval_Coap.h"
#include "Serval_CoapServer.h"

/* --------------------------------------------------------------------------- |
 * HANDLES ******************************************************************* |
 * -------------------------------------------------------------------------- */

static CmdProcessor_T * AppCmdProcessor;
static CmdProcessor_T CmdProcessorHandleServalPAL;

/* --------------------------------------------------------------------------- |
 * VARIABLES ***************************************************************** |
 * -------------------------------------------------------------------------- */

#define TASK_PRIORITY_SERVALPAL_CMD_PROC            UINT32_C(3)
#define TASK_STACK_SIZE_SERVALPAL_CMD_PROC          UINT32_C(600)
#define TASK_QUEUE_LEN_SERVALPAL_CMD_PROC           UINT32_C(10)

/* --------------------------------------------------------------------------- |
 * EXECUTING FUNCTIONS ******************************************************* |
 * -------------------------------------------------------------------------- */

static Retcode_T ServalPalSetup(void)
{
    Retcode_T returnValue = RETCODE_OK;
    returnValue = CmdProcessor_Initialize(&CmdProcessorHandleServalPAL, (char *)"Serval PAL", TASK_PRIORITY_SERVALPAL_CMD_PROC, TASK_STACK_SIZE_SERVALPAL_CMD_PROC, TASK_QUEUE_LEN_SERVALPAL_CMD_PROC);

    if (RETCODE_OK == returnValue)
    {
        returnValue = ServalPal_Initialize(&CmdProcessorHandleServalPAL);
    }
    if (RETCODE_OK == returnValue)
    {
        returnValue = ServalPalWiFi_Init();
    }
    if (RETCODE_OK == returnValue)
    {
        ServalPalWiFi_StateChangeInfo_T stateChangeInfo = { SERVALPALWIFI_OPEN, 0 };
        returnValue = ServalPalWiFi_NotifyWiFiEvent(SERVALPALWIFI_STATE_CHANGE, &stateChangeInfo);
    }
    return returnValue;
}

void networkSetup(void) {

    WlanConnect_SSID_T connectSSID = (WlanConnect_SSID_T) "OvrR4g002_2.4G";
    WlanConnect_PassPhrase_T connectPassPhrase =
            (WlanConnect_PassPhrase_T) "Welcome@OvrP";
    WlanConnect_Init();
    NetworkConfig_SetIpDhcp(0);
    WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);

    ServalPalSetup();
}

void createCoapResponse(Msg_T *msg_ptr, char const *payload_ptr, uint8_t responseCode){
    CoapSerializer_T serializer;
    CoapSerializer_setup(&serializer, msg_ptr, RESPONSE);
    CoapSerializer_setCode(&serializer, msg_ptr, responseCode);
    CoapSerializer_setConfirmable(msg_ptr, false);
    CoapSerializer_reuseToken(&serializer, msg_ptr);

    CoapOption_T uriOption;
    uriOption.OptionNumber = Coap_Options[COAP_URI_PATH];
    uriOption.value = (uint8_t*) "test";
    uriOption.length = 4;
    CoapSerializer_serializeOption(&serializer, msg_ptr, &uriOption);

    CoapSerializer_setEndOfOptions(&serializer, msg_ptr);
    // serialize the payload
    uint8_t resource[30] = {0};
    uint8_t resourceLength = strlen(payload_ptr);
    memcpy(resource, payload_ptr, resourceLength + 1);
    CoapSerializer_serializePayload(&serializer, msg_ptr, resource, resourceLength);
}

Retcode_T sendingCallback(Callable_T *callable_ptr, Retcode_T status){
    (void) callable_ptr;
    (void) status;
    return RC_OK;
}

void sendCoapResponse(Msg_T *message, char const* payloard_ptr){
    createCoapResponse(message, payloard_ptr, Coap_Codes[COAP_CONTENT]);
    Callable_T *alpCallable_ptr = Msg_defineCallback(message,
            (CallableFunc_T) sendingCallback);
    CoapServer_respond(message, alpCallable_ptr);
}

void parseCoapRequest(Msg_T *msg_ptr, uint8_t *code){
    CoapParser_T parser;
    CoapParser_setup(&parser, msg_ptr);
    *code = CoapParser_getCode(msg_ptr);
    const uint8_t* payload;
    uint8_t payloadlen;
    CoapParser_getPayload(&parser,&payload,&payloadlen);
    printf("Incoming Coap request: %s \n\r",payload);
}

Retcode_T coapReceiveCallback(Msg_T *msg_ptr, Retcode_T status){

	printf("Request received!\n");  //Printf to ensure that the callback function is called

	uint8_t code = 0;
    parseCoapRequest(msg_ptr, &code);
    if(code == Coap_Codes[COAP_POST])
    {
        sendCoapResponse(msg_ptr, "Hello Client, POST received");
    }
    else if(code == Coap_Codes[COAP_GET])
    {
        sendCoapResponse(msg_ptr, "Hello Client, GET Received!");
    }

    return status;
}

/* --------------------------------------------------------------------------- |
 * BOOTING- AND SETUP FUNCTIONS ********************************************** |
 * -------------------------------------------------------------------------- */

static void AppControllerEnable(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);

    vTaskDelay(5000);
    Ip_Port_T serverPort = Ip_convertIntToPort((uint16_t)5683);

    CoapServer_startInstance(serverPort,(CoapAppReqCallback_T) &coapReceiveCallback);

    NetworkConfig_IpSettings_T myIp;
    NetworkConfig_GetIpSettings(&myIp);
    vTaskDelay(5000);
    printf("The IP was retrieved: %u.%u.%u.%u \n\r",
            (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 3)),
            (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 2)),
            (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 1)),
            (unsigned int) (NetworkConfig_Ipv4Byte(myIp.ipV4, 0)));
}

static void AppControllerSetup(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    /* Setup the necessary modules required for the application */
    networkSetup();
    CoapServer_initialize();

    retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerEnable, NULL, UINT32_C(0));
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerSetup : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0); /* To provide LED indication for the user */
    }
}

void AppController_Init(void * cmdProcessorHandle, uint32_t param2)
{
    BCDS_UNUSED(param2);

    Retcode_T retcode = RETCODE_OK;

    if (cmdProcessorHandle == NULL)
    {
        printf("AppController_Init : Command processor handle is NULL \r\n");
        retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_NULL_POINTER);
    }
    else
    {
        AppCmdProcessor = (CmdProcessor_T *) cmdProcessorHandle;
        retcode = CmdProcessor_Enqueue(AppCmdProcessor, AppControllerSetup, NULL, UINT32_C(0));
    }

    if (RETCODE_OK != retcode)
    {
        Retcode_RaiseError(retcode);
        assert(0); /* To provide LED indication for the user */
    }
}

 

0 (0 Votes)