HTTP Tutorial Post Problem
응답
19. 7. 16 오전 10:48

Hello Community,

 

I'm trying to run the HTTP-Tutorial from the YT-channel of bosch.

https://www.youtube.com/watch?v=_Vf4A-XEYes

The Get request is working without any bigger problems. 

When I try to run the Post request the xdk lost connection to the pc after flashing the file on it.

The xdk only reconnects when I push the button 1 for 2 seconds but the programm is still not running.

 

Informations of the Device Console:

 INFO | Connecting to XDK device 'XDK Device 1' in port 'COM3'...
 INFO | Connection to port 'COM3' established
 INFO | Creating Firmware Over the Air (FOTA) Container
 INFO | Binary File: C:\Users\f.kuehn\XDK-Workspace_11.07\httptutorial2\debug\httptutorial.bin
 INFO | Header Version: 0100
 INFO | Product Class: 0010
 INFO | Product Variant: 0000
 INFO | Firmware Version: 00000000
 INFO | Flashing file 'C:/Users/f.kuehn/XDK-Workspace_11.07/httptutorial2/debug/httptutorial.bin'...
 INFO | XDK DEVICE 1: Ready
 INFO | XDK DEVICE 1: C
 INFO | XDK DEVICE 1:
 INFO | XDK DEVICE 1: XMODEM Download Success
 INFO | XDK DEVICE 1: c
 INFO | XDK DEVICE 1:  CRC of application area
 INFO | XDK DEVICE 1:  CRC0000F97E
 INFO | Application checksum 'f97e' successfully verified.
 INFO | Transmission successfully completed!
 INFO | Booting application...
 INFO | XDK DEVICE 1: b
 INFO | XDK DEVICE 1:  Jumping to application
 INFO | Port 'COM3' has been disconnected

 

My Programm code:

/* own header files */
#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>
#include <Serval_HttpClient.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 "PAL_Initialize_ih.h"
#include "PAL_socketMonitor_ih.h"
#include "PIp.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 xTaskHandle AppControllerHandle = NULL;/**< OS thread handle for Application controller to be used by run-time blocking threads */

/* global variables ********************************************************* */

/* inline functions ********************************************************* */

/* local functions ********************************************************** */


static retcode_t onHTTPRequestSent(Callable_T *callfunc, retcode_t status)
{
 (void) (callfunc);
      if (status != RC_OK) {
     printf("Failed to send HTTP request!\r\n");
 }
return(RC_OK);
}


static retcode_t onHTTPResponseReceived(HttpSession_T *httpSession, Msg_T *msg_ptr, retcode_t status)
{
  (void) (httpSession);

  if (status == RC_OK && msg_ptr != NULL) {
    Http_StatusCode_T statusCode = HttpMsg_getStatusCode(msg_ptr);
    char const *contentType = HttpMsg_getContentType(msg_ptr);

    char const *content_ptr;
    unsigned int contentLength = 0;
    HttpMsg_getContent(msg_ptr, &content_ptr, &contentLength);
    char content[contentLength+1];
    strncpy(content, content_ptr, contentLength);
    content[contentLength] = 0;

    printf("HTTP RESPONSE: %d [%s]\r\n", statusCode, contentType);
    printf("%s\r\n", content);
  } else {
    printf("Failed to receive HTTP response!\r\n");
  }
  return(RC_OK);
}

retcode_t writeNextPartToBuffer(OutMsgSerializationHandover_T* handover)
{
const char* payload = "Hello";

uint16_t payloadLength = (uint16_t) strlen(payload);
uint16_t alreadySerialized = handover->offset;
 uint16_t remainingLength = payloadLength - alreadySerialized;
uint16_t bytesToCopy;
retcode_t rc;
if ( remainingLength <= handover->bufLen ) {
 bytesToCopy = remainingLength;
 rc = RC_OK;
 } else {
 bytesToCopy = handover->bufLen;
 rc = RC_MSG_FACTORY_INCOMPLETE;
 }

memcpy(handover->buf_ptr, payload + alreadySerialized, bytesToCopy);
 handover->offset = alreadySerialized + bytesToCopy;
 handover->len = bytesToCopy;

return rc;
}

/**
 * @brief Responsible for controlling application control flow.
 * Any application logic which is blocking in nature or fixed time dependent
 * can be placed here.
 *
 * @param[in] pvParameters
 * FreeRTOS task handle. Could be used if more than one thread is using this function block.
 */
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 */
    }
    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 */

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

    //Connect device to Wifi

    WlanConnect_SSID_T connectSSID = (WlanConnect_SSID_T) "$$$$$$$$$$";
    WlanConnect_PassPhrase_T connectPassPhrase = (WlanConnect_PassPhrase_T) "$$$$$$$$$$";
    WlanConnect_Init();
    Retcode_T retScanStatus;
            WlanConnect_ScanInterval_T scanInterval = 5;
            WlanConnect_ScanList_T scanList;
            portTickType delay500msec = 500;
            retScanStatus = WlanConnect_ScanNetworks(scanInterval, &scanList);
            if (retScanStatus == RETCODE_OK) {
             // Print SSID networks
             for (int i = 0; i < scanList.NumOfScanEntries; i++) {
             if (0 != scanList.ScanData[i].SsidLength) {
             printf("Found SSID number %d is : %s\n\r", i,
             scanList.ScanData[i].Ssid);

             vTaskDelay( delay500msec / portTICK_RATE_MS);
             }
             }
            }
            else if (retScanStatus == RETCODE_NO_NW_AVAILABLE) { /* No networks found */ }
            else {
             printf("Scan failed\n");
            }
    NetworkConfig_SetIpDhcp(0);
    WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);
    NetworkConfig_IpSettings_T myIpGet;
        Retcode_T retStatusGetIp;
        retStatusGetIp = NetworkConfig_GetIpSettings(&myIpGet);
        if (retStatusGetIp == RETCODE_OK) {
         printf("The static IP was retrieved : %u.%u.%u.%u \n\r",
         (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 3)),
         (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 2)),
         (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 1)),
         (unsigned int) (NetworkConfig_Ipv4Byte(myIpGet.ipV4, 0)));
        }
        else {
         // Get IP settings failed
        }
   PAL_initialize();
   PAL_socketMonitorInit();
   // ... initialize chosen HTTP module
   // ... start timers to perform network requests

    //Initialize HTTP Client

    HttpClient_initialize();

    // Set IP address and port

    Ip_Address_T destAddr;
    PAL_getIpaddress((uint8_t*) "posttestserver.com", &destAddr);
    Ip_Port_T port = Ip_convertIntToPort(80);

    // Set up message structure with all information required to create http request

    Msg_T* msg_ptr;
    HttpClient_initRequest(&destAddr, port, &msg_ptr);
    HttpMsg_setReqMethod(msg_ptr, Http_Method_Get);
    HttpMsg_setReqUrl(msg_ptr, "/post");
    HttpMsg_setHost(msg_ptr, "posttestserver.com");
    Msg_prependPartFactory(msg_ptr, &writeNextPartToBuffer);

    // send the request


    static Callable_T sentCallable;
    Callable_assign(&sentCallable, &onHTTPRequestSent);

    // insert additional headers here, if needed (see chapter 4.4)

    HttpClient_pushRequest(msg_ptr, &sentCallable, &onHTTPResponseReceived);

 


}

/**@} */
/** ************************************************************************* */
 

I hope someone recognize the reason,

kind regards 

 

0 (0 투표)
RE: HTTP Tutorial Post Problem
응답
19. 7. 22 오전 8:10 as a reply to Felix Kühn.

Hi Felix

You can take reference from SDK/xdk110/Apps/HttpExampleClient as it works

Thanks

Priyashi

 

0 (0 투표)