C - Rest
/* 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 "Serval_HttpClient.h"
/* local functions */
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 createAndSendGetMessage(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_Get);
HttpMsg_setReqUrl(msg_ptr, "/ip");
// 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();
}
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();
createAndSendGetMessage();
}
Last updated
Was this helpful?