CoAP Server
Antwort
24.05.19 14:58

Hello everyone,

I'm trying to implement a CoAP server on a XDK device. I think that Everything is set up properly but, when I try to make a request I have no response at all... Can someone help me, please?

 

Kind regards,

Pedro Silva

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

/* own header files */
#include "AppController.h"

/* system header files */
#include <stdio.h>

/* additional interface header files */
#include "BCDS_CmdProcessor.h"
#include "XDK_Utils.h"
#include "FreeRTOS.h"
#include "task.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"
#include "BCDS_NetworkConfig.h"

/* constant definitions ***************************************************** */
#define  XDK_APP_DELAY      UINT32_C(1000)
/* local variables ********************************************************** */

static CmdProcessor_T * AppCmdProcessor;/**< Handle to store the main Command processor handle to be used by run-time event driven threads */
static CmdProcessor_T CmdProcessorHandleServalPAL;
static xTaskHandle AppControllerHandle = NULL;/**< OS thread handle for Application controller to be used by run-time blocking threads */

#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)

static Retcode_T ServalPalSetup(void);
void networkSetup(void);
Retcode_T coapReceiveCallback(Msg_T *msg_ptr, Retcode_T status);
void parseCoapRequest(Msg_T *msg_ptr, uint8_t *code);
Retcode_T sendingCallback(Callable_T *callable_ptr, Retcode_T status);
void sendCoapResponse(Msg_T *message, char const* payloard_ptr);
void createCoapResponse(Msg_T *msg_ptr, char const *payload_ptr, uint8_t responseCode);



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);
    /* serval pal common init */
    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) "-------";
    WlanConnect_PassPhrase_T connectPassPhrase =
            (WlanConnect_PassPhrase_T) "--------";
    WlanConnect_Init();
    NetworkConfig_SetIpDhcp(0);
    WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);

    ServalPalSetup();
}



Retcode_T coapReceiveCallback(Msg_T *msg_ptr, Retcode_T status){
  uint8_t code = 0;
  parseCoapRequest(msg_ptr, &code);
  if(code == Coap_Codes[COAP_POST])
  {
    sendCoapResponse(msg_ptr, "POST received");
  }
  else if(code == Coap_Codes[COAP_GET])
  {
    sendCoapResponse(msg_ptr, "GET received");
  }
  return status;
}

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 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 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);
  // call this after all options have been serialized
  CoapSerializer_setEndOfOptions(&serializer, msg_ptr);
  // serialize the payload
  uint8_t resource[30] = {"Teste 1"};
  uint8_t resourceLength = strlen(payload_ptr);
  memcpy(resource, payload_ptr, resourceLength + 1);
  CoapSerializer_serializePayload(&serializer, msg_ptr, resource, resourceLength);
}


static void AppControllerFire(void* pvParameters)
{
    BCDS_UNUSED(pvParameters);

    /* A function that implements a task must not exit or attempt to return to
     its caller function as there is nothing to return to. */
    while (1)
    {
        /* code to implement application control flow */
        vTaskDelay(XDK_APP_DELAY);
    }
}

/**
 * @brief To enable the necessary modules for the application
 *
 * @param [in] param1
 * A generic pointer to any context data structure which will be passed to the function when it is invoked by the command processor.
 *
 * @param [in] param2
 * A generic 32 bit value  which will be passed to the function when it is invoked by the command processor..
 */
static void AppControllerEnable(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    /* @todo - Enable necessary modules for the application and check their return values */
    if (RETCODE_OK == retcode)
    {
        if (pdPASS != xTaskCreate(AppControllerFire, (const char * const ) "AppController", TASK_STACK_SIZE_APP_CONTROLLER, NULL, TASK_PRIO_APP_CONTROLLER, &AppControllerHandle))
        {
            retcode = RETCODE(RETCODE_SEVERITY_ERROR, RETCODE_OUT_OF_RESOURCES);
        }
    }
    if (RETCODE_OK != retcode)
    {
        printf("AppControllerEnable : Failed \r\n");
        Retcode_RaiseError(retcode);
        assert(0); /* To provide LED indication for the user */
    }

    vTaskDelay(5000);
       Ip_Port_T serverPort = Ip_convertIntToPort((uint16_t)5683);
       // Port must be inserted via Ip_convertIntToPort(). Otherwise the port is not
       // recognized correctly.
       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)));

    Utils_PrintResetCause();
}

/**
 * @brief To setup the necessary modules for the application
 *
 * @param [in] param1
 * A generic pointer to any context data structure which will be passed to the function when it is invoked by the command processor.
 *
 * @param [in] param2
 * A generic 32 bit value  which will be passed to the function when it is invoked by the command processor..
 */
static void AppControllerSetup(void * param1, uint32_t param2)
{
    BCDS_UNUSED(param1);
    BCDS_UNUSED(param2);
    Retcode_T retcode = RETCODE_OK;

    /* @todo - 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 */
    }
}

/* global functions ********************************************************* */

/** Refer interface header for description */
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 Stimmen)
RE: CoAP Server
Antwort
03.06.19 11:34 als Antwort auf Pedro Silva.

Hello Pedro,

in the link there is a detailed description with sample code that you can use.

https://developer.bosch.com/web/xdk/coap#6-2

regards

Saeid

+1 (1 Stimme)
RE: CoAP Server
Antwort
13.06.19 15:56 als Antwort auf Saeid Kajlar.

Hello Saeid,

The code that I used to implement the server is the example you mentioned, and it's not working.

Can you take a close look, please?

Thank you.

 

Regards

Pedro Silva

0 (0 Stimmen)