Developer Portal Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    SOLVED

    CoAP Server Example is not working

    Highlighted
    Member

    CoAP Server Example is not working

    Dear Developers,

    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 */
        }
    }
    12 REPLIES 12
    Highlighted
    Community Manager

    Re: CoAP Server Example is not working

    Hi Pedro! Thanks for providing your code and also for your question!

    Our developers team will have a look into your question and we will get back to you as soon as possible!

    I will keep you posted!

    Happy Friday!

     

    Highlighted
    Member

    Re: CoAP Server Example is not working

    Hi Grocamora,

    Thank you for your reply. I will wait for your feedback!

    Kind regards,

    Pedro S.

    Highlighted
    Community Manager
    Community Manager

    Re: CoAP Server Example is not working

    Hi Pedro,

    Do you know what is the behavior when this setting below is either 1, or its previous setting, instead of 0?

    Thanks!

    -D SERVAL_ENABLE_COAP_COMBINED_SERVER_AND_CLIENT=0\

    Highlighted
    Member

    Re: CoAP Server Example is not working

    Hello pdo,

    I've already tried to set it to 1\ and the behavior is exately the same, by default is set to 1\! The problem is not solved yet...

    Thanks!

    Regards,

    Pedro Silva

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist