Other Parts Discussed in Thread: SYSBIOS
I am using TM4C129ENCPDTI in CCS Version 12.2.0,
TI-RTOS for TivaC 2.16.01.14,
XDCtools v 3.32.0.06_core and
Compiler ti-cgt-arm_20.2.7.LTS
wolfssl-3.9.10-stable.
I am trying post data to postman-echo.com, but I'm getting below error on calling HTTPCli_getResponseStatus() after HTTPCli_sendRequestBody()..
Error Log:
System provider is set to SysMin. Halt the target to view any SysMin contents in ROV.
Service Status: DHCPC : Enabled : : 000
Service Status: DHCPC : Enabled : Running : 000
Network Added: If-1:192.168.2.74
Service Status: DHCPC : Enabled : Running : 017
n(int): 11
CONTENT_LENGTH: 11
Current time: Fri Jun 9 09:41:43 2023
Sending a HTTPS POST request to 'https://postman-echo.com'
361: out of memory: handle=0x20021ea4, size=5550
Data sent successfully
Error! code = -112, desc = httpsTask: cannot get status
Below is my Https Task:
#include <string.h>
#include <time.h>
/* XDCtools Header files */
#include <xdc/runtime/Error.h>
#include <xdc/runtime/System.h>
/* TI-RTOS Header files */
#include <ti/sysbios/BIOS.h>
#include <ti/sysbios/hal/Seconds.h>
#include <ti/sysbios/knl/Task.h>
#include <ti/sysbios/knl/Semaphore.h>
#include <ti/drivers/GPIO.h>
#include <ti/net/http/httpcli.h>
#include <ti/net/sntp/sntp.h>
/* Example/Board Header file */
#include "Board.h"
#include <sys/socket.h>
#define HOSTNAME "">https://postman-echo.com"
#define REQUEST_URI "/post"
#define CONTENT_TYPE "application/json"
#define Authorization "Basic"
#define USER_AGENT "HTTPCli (ARM; TI-RTOS)"
#define NTP_HOSTNAME "pool.ntp.org"
#define NTP_PORT "123"
#define NTP_SERVERS 3
#define NTP_SERVERS_SIZE (NTP_SERVERS * sizeof(struct sockaddr_in))
#define HTTPTASKSTACKSIZE 32768
/*
* USER STEP: Copy the lines in the root CA certificate between
* -----BEGIN CERTIFICATE-----
* ...
* -----END CERTIFICATE-----
*/
uint8_t ca[] ="MIIEDzCCAvegAwIBAgIBADANBgkqhkiG9w0BAQUFADBoMQswC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iDh6GX70k1PncGQVhiv45YuApnP+yz3SFmH8lU+nLMPUxA2IGvd56D\
eruix/U0F47ZEUD0/CwqTRV/p2JdLiXTAAsgGh1o+Re49L2L7ShZ3U0WixeDyLJl\
xy16paq8U4Zt3VekyvggQQto8PT7dL5WXXp59fkdheMtlb71cZBDzI0fmgAKhynp\
VSJYACPq4xJDKVtHCN2MQWplBqjlIapBtJUhlbl90TSrE9atvNziPTnNvT51cKEY\
WQPJIrSPnNVeKtelttQKbfi3QBFGmh95DmK/D5fs4C8fF5Q=";
// "<--- add root certificate with blackslash at end of each new line -->";
uint32_t calen = sizeof(ca);
unsigned char ntpServers[NTP_SERVERS_SIZE];
static Semaphore_Handle semHandle = NULL;
/*
* ======== printError ========
*/
void printError(char *errString, int code)
{
System_printf("Error! code = %d, desc = %s\n", code, errString);
// TaskSleep(100);
BIOS_exit(code);
}
/*
* ======== timeUpdateHook ========
* Called after NTP time sync
*/
void timeUpdateHook(void *p)
{
Semaphore_post(semHandle);
}
/*
* ======== startNTP ========
*/
void startNTP(void)
{
int ret;
int currPos;
time_t ts;
struct sockaddr_in ntpAddr;
struct addrinfo hints;
struct addrinfo *addrs;
struct addrinfo *currAddr;
Semaphore_Params semParams;
memset(&hints, 0, sizeof(struct addrinfo));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_DGRAM;
ret = getaddrinfo(NTP_HOSTNAME, NTP_PORT, NULL, &addrs);
if (ret != 0) {
printError("startNTP: NTP host cannot be resolved!", ret);
}
currPos = 0;
for (currAddr = addrs; currAddr != NULL; currAddr = currAddr->ai_next) {
if (currPos < NTP_SERVERS_SIZE) {
ntpAddr = *(struct sockaddr_in *)(currAddr->ai_addr);
memcpy(ntpServers + currPos, &ntpAddr, sizeof(struct sockaddr_in));
currPos += sizeof(struct sockaddr_in);
}
else {
break;
}
}
freeaddrinfo(addrs);
ret = SNTP_start(Seconds_get, Seconds_set, timeUpdateHook,
(struct sockaddr *)&ntpServers, NTP_SERVERS, 0);
if (ret == 0) {
printError("startNTP: SNTP cannot be started!", -1);
}
Semaphore_Params_init(&semParams);
semParams.mode = Semaphore_Mode_BINARY;
semHandle = Semaphore_create(0, &semParams, NULL);
if (semHandle == NULL) {
printError("startNTP: Cannot create semaphore!", -1);
}
SNTP_forceTimeSync();
Semaphore_pend(semHandle, BIOS_WAIT_FOREVER);
ts = time(NULL);
System_printf("Current time: %s\n", ctime(&ts));
}
Void httpsPostTask(UArg arg0, UArg arg1)
{
bool moreFlag = false;
char data[64];
char buf[1024];
int ret;
int len;
char CONTENT_LENGTH[3];
struct sockaddr_in addr;
TLS_Params tlsParams;
TLS_Handle tls;
//Data to be sent
strcpy(data, "{\"val\": 10}");
len = strlen(data);
sprintf(CONTENT_LENGTH, "%d", len);
System_printf("\nData: %s\n", data);
System_printf("len(int): %d\n", len);
System_printf("CONTENT_LENGTH: %s\n", CONTENT_LENGTH);
HTTPCli_Params params;
HTTPCli_Struct cli;
HTTPCli_Field fields[5] = {
{ HTTPStd_FIELD_NAME_HOST, HOSTNAME },
{ HTTPStd_FIELD_NAME_AUTHORIZATION, Authorization },
{ HTTPStd_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE },
{ NULL, NULL }
};
startNTP();
System_printf("Sending a HTTPS GET request to '%s'\n", HOSTNAME);
System_flush();
TLS_Params_init(&tlsParams);
tlsParams.ca = ca;
tlsParams.calen = calen;
tls = TLS_create(TLS_METHOD_CLIENT_TLSV1_2, &tlsParams, NULL);
if (!tls) {
printError("httpsTask: TLS create failed", -1);
}
//GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_2, 0);
//GPIOPinWrite(GPIO_PORTH_BASE, GPIO_PIN_3, GPIO_PIN_3);
HTTPCli_construct(&cli);
HTTPCli_setRequestFields(&cli, fields);
ret = HTTPCli_initSockAddr((struct sockaddr *)&addr, HOSTNAME, 0);
if (ret < 0) {
printError("httpsTask: address resolution failed", ret);
}
HTTPCli_Params_init(¶ms);
params.tls = tls;
ret = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0, ¶ms);
if (ret < 0) {
printError("httpsTask: connect failed", ret);
}
ret = HTTPCli_sendRequest(&cli, HTTPStd_POST, REQUEST_URI, true);
if (ret < 0) {
printError("httpsTask: send failed", ret);
}
else
{
System_printf("sendRequest successful\n");
}
ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_LENGTH, CONTENT_LENGTH, false);
if (ret < 0) {
printError("httpTask: send failed", ret);
}
else {
System_printf("sendField successful\n");
}
ret = HTTPCli_sendField(&cli, HTTPStd_FIELD_NAME_CONTENT_TYPE, CONTENT_TYPE, true);
if (ret < 0) {
printError("httpTask: send failed", ret);
}
else {
System_printf("sendField successful\n");
}
ret = HTTPCli_sendRequestBody(&cli, data, strlen(data));
if (ret < 0) {
printError("httpTask: Variable data couldn't be sent", ret);
}
else {
System_printf("Data sent successfully\n");
}
ret = HTTPCli_getResponseStatus(&cli);
if (ret != HTTPStd_OK) {
printError("httpsTask: cannot get status", ret);
}
System_printf("HTTP Response Status Code: %d\n", ret);
// System_flush();
ret = HTTPCli_getResponseField(&cli, data, sizeof(data), &moreFlag);
if (ret != HTTPCli_FIELD_ID_END) {
printError("httpsTask: response field processing failed", ret);
}
len = 0;
do {
ret = HTTPCli_readResponseBody(&cli, data, sizeof(data), &moreFlag);
if (ret < 0) {
printError("httpsTask: response body processing failed", ret);
}
len += ret;
} while (moreFlag);
System_printf("Recieved %d bytes of payload\n", len);
System_flush();
HTTPCli_disconnect(&cli);
HTTPCli_destruct(&cli);
TLS_delete(&tls);
}
/*
* ======== netIPAddrHook ========
* This function is called when IP Addr is added/deleted
*/
void netIPAddrHook(unsigned int IPAddr, unsigned int IfIdx, unsigned int fAdd)
{
static Task_Handle taskHandle;
Task_Params taskParams;
Error_Block eb;
/* Create a HTTP task when the IP address is added */
if (fAdd && !taskHandle) {
Error_init(&eb);
Task_Params_init(&taskParams);
taskParams.stackSize = HTTPTASKSTACKSIZE;
taskParams.priority = 1;
taskHandle = Task_create((Task_FuncPtr)httpsPostTask, &taskParams, &eb);
if (taskHandle == NULL) {
printError("netIPAddrHook: Failed to create HTTP Task\n", -1);
}
}
}
/*
* ======== main ========
*/
int main(void)
{
/* Call board init functions */
Board_initGeneral();
Board_initGPIO();
Board_initEMAC();
/* Turn on user LED */
GPIO_write(Board_LED0, Board_LED_ON);
System_printf("Starting the HTTPS GET example\nSystem provider is set to "
"SysMin. Halt the target to view any SysMin contents in ROV.\n");
/* SysMin will only print to the console when you call flush or exit */
System_flush();
/* Start BIOS */
BIOS_start();
return (0);
}