C - Post

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

/* additional interface header files */
#include "FreeRTOS.h"
#include "timers.h"
#include "BCDS_CmdProcessor.h"
#include "BCDS_Assert.h"

#include "BCDS_WlanConnect.h"
#include "PAL_initialize_ih.h"
#include "PAL_socketMonitor_ih.h"
#include "PIp.h"
#include 

/* local functions */

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;
}

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);
}

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);
}

/* global functions */

void createAndSendPostMessage(void){
    // assemble the request message
    Ip_Address_T destAddr;
    Ip_convertOctetsToAddr(23, 22, 14, 18, &destAddr);
    Ip_Port_T port = Ip_convertIntToPort(80);

    Msg_T* msg_ptr;
    HttpClient_initRequest(&destAddr, port, &msg_ptr);
    HttpMsg_setReqMethod(msg_ptr, Http_Method_Post);
    HttpMsg_setReqUrl(msg_ptr, "/post");

    Msg_prependPartFactory(msg_ptr, &writeNextPartToBuffer);

    // send the request
    static Callable_T sentCallable;
    Callable_assign(&sentCallable, &onHTTPRequestSent);
    HttpClient_pushRequest(msg_ptr, &sentCallable, &onHTTPResponseReceived);
}

void networkSetup(void){
    WlanConnect_SSID_T connectSSID = (WlanConnect_SSID_T) "yourNetworkSSID";
    WlanConnect_PassPhrase_T connectPassPhrase = (WlanConnect_PassPhrase_T) "yourNetworkPW";
    WlanConnect_Init();
    WlanConnect_WPA(connectSSID, connectPassPhrase, NULL);
    PAL_initialize();
    PAL_socketMonitorInit();
}

/**
 * @brief This is a template function where the user can write his custom application.
 *
 */
void appInitSystem(void * CmdProcessorHandle, uint32_t param2)
{
    if (CmdProcessorHandle == NULL)
    {
        printf("Command processor handle is null \n\r");
        assert(false);
    }
    BCDS_UNUSED(param2);
    networkSetup();
    HttpClient_initialize();
    createAndSendPostMessage();
}

Last updated

Was this helpful?