This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

TM4C129ENCPDT: HTTP timeout param in TI-RTOS 2.16.01.14 HttpCli library

Part Number: TM4C129ENCPDT

Tool/software:

hi all ,
i am not able to set the timeout for http calls , in Httpsget example.

HTTPCli_Params_init(&params);
params.tls = tls;

params.timeout = 1; // 1 second.

is there anywhere the timeout is set  in library in any underlaying file?

where and how can i set the params.timeout staticaly in code ?

Regards
chiranth

/*
 * Copyright (c) 2015-2016, Texas Instruments Incorporated
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * *  Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *
 * *  Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * *  Neither the name of Texas Instruments Incorporated nor the names of
 *    its contributors may be used to endorse or promote products derived
 *    from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

/*
 *  ======== httpsget.c ========
 *  HTTPS Client GET example application
 */
#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         "www.example.com:443"
#define REQUEST_URI      "/"
#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[] =
    "<--- 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);
    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));
}

/*
 *  ======== httpsTask ========
 *  Makes an HTTP GET request
 */
Void httpsTask(UArg arg0, UArg arg1)
{
    bool moreFlag = false;
    char data[64];
    int ret;
    int len;
    struct sockaddr_in addr;

    TLS_Params tlsParams;
    TLS_Handle tls;

    HTTPCli_Params params;
    HTTPCli_Struct cli;
    HTTPCli_Field fields[3] = {
        { HTTPStd_FIELD_NAME_HOST, HOSTNAME },
        { HTTPStd_FIELD_NAME_USER_AGENT, USER_AGENT },
        { 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);
    }

    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(&params);
    params.tls = tls;
    params.timeout = 1;
    ret = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0, &params);
    if (ret < 0) {
        printError("httpsTask: connect failed", ret);
    }

    ret = HTTPCli_sendRequest(&cli, HTTPStd_GET, REQUEST_URI, false);
    if (ret < 0) {
        printError("httpsTask: send failed", ret);
    }

    ret = HTTPCli_getResponseStatus(&cli);
    if (ret != HTTPStd_OK) {
        printError("httpsTask: cannot get status", ret);
    }

    System_printf("HTTP Response Status Code: %d\n", ret);

    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)httpsTask, &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);
}

  • Hi,

      How do you know it is not working?

      Can you tried other values like 2s, 3s or 5s? 

      Have you tried the HTTPGET without the TLS? Does the timeout work? I just wanted to isolate the problem is the problem is related to TLS or not. 

      Reading this post, the timeout should work. https://e2e.ti.com/support/processors-group/processors/f/processors-forum/523790/httpcli_getresponse-timeout/1904709?tisearch=e2e-sitesearch&keymatch=HTTPCli_Params%252520timeout#1904709

  • hi charles,

    i tried HTTPGET without TLS in httpget example provided even its not working here.

    regards

    chiranth

  • Hi,

      Can you elaborate what is not working in detail? What happens if you leave the timeout value as the default equal to 0. What happens when you change to 1, 2, 3...? Do it not timeout at all or it does not timeout according to the time you specify?

      Please look at this post and see if defining HTTPCli_LIBTYPE_MIN will make a difference. Refer to the answer by Aashish Bhanawah. 

    https://e2e.ti.com/support/wireless-connectivity/wi-fi-group/wifi/f/wi-fi-forum/496037/timeout-doesn-t-work-when-doing-httpcli_readresponsebody/1803901?tisearch=e2e-sitesearch&keymatch=HTTPCli_Params%25252520timeout#1803901

  • Hi Charles,

    "What happens if you leave the timeout value as the default equal to 0. What happens when you change to 1, 2, 3...? Do it not timeout at all or it does not timeout according to the time you specify?"

    Initially i didnt define HTTPCli_LIBTYPE_MIN  my application works fine without defining this macro , there few http calls takes more than a minute to get response , so i want to increase the timeout to 90 seconds. so i read about this params.timeout in httpcli.h file in TI RTOS sdk. i used params.timeout = 1; as my application takes 2.5 seconds to get response from our server in average so that to test this timeout i defined timeout to 1 second. its should give me timeout error with error code -104 or similar error code. just by using this timeout without defining a macro  HTTPCli_LIBTYPE_MIN  wasn't working in the sense even if define 10 seconds internally it didnt make any difference it use to take default timeout value internally from kernel i think so it didn't workout for my application. i read  Aashish Bhanawah answer and tried defining HTTPCli_LIBTYPE_MIN macro and using params.timeout = 1  in httpget and httpsget example pointing to www.google.com and my own server usually it takes 1.5 seconds to get respopnse . i got -109 error saying cannot get status. when i use params.timeout = 0 ; it get the response with 200 resp code. that means the timeout is set to default value. how do i use this timeout thing in my application? if it works for httpget and httpsget example it will definitely work for my application. Please suggest me some workaround.

    Regards

    Chiranth

  • Hi Charles,

    MCU: Tm4C129ENCPDT
    TIRTOS: 2.16.01.14

    XDC tools :xdctools_3_32_00_06_core

    here is the code snippet i have in httpcli.h file from TI RTOS.

    typedef struct HTTPCli_Params {
    
        TLS_Handle *tls;
    #ifndef HTTPCli_LIBTYPE_MIN
        HTTPCli_StatusHandler *shandle;
        HTTPCli_ContentHandler *chandle;
        HTTPCli_RedirectCallback rhandle;
    #ifndef __linux__
        unsigned int stackSize; /*!< Async thread stack size. 0 for default */
        unsigned int priority;  /*!< Async thread priority. 0 for default */
    #endif
    #endif
    
    #ifdef NET_SLP
        HTTPCli_Notify rnotify;   /*!< Async read notify handle (6LoWPAN) */
        HTTPCli_Notify wnotify;   /*!< Async write notify handle (6LoWPAN) */
        HTTPCli_Notify enotify;   /*!< Async exception notify handle (6LowPAN)*/
    #endif
    
    #ifndef NET_SLP
        int timeout;
        /*!< Timeout value (in seconds) for socket. Set 0 for default value */
    #endif
    
    } HTTPCli_Params;

    I see there TLS_Handle *tls; is this httpcli.h file wheras its not present in case of others where Aashish Bhanawat answer. dose this TLS thing is causing error -109 ?

    Regards
    chiranth

  • I see there TLS_Handle *tls; is this httpcli.h file wheras its not present in case of others where Aashish Bhanawat answer. dose this TLS thing is causing error -109 ?

    Please refer to file:///C:/ti/tirtos_tivac_2_16_00_08/products/ns_1_11_00_10/docs/html/httpcli_8h_source.html in your TI-RTOS installation. It has the explanation on the various error codes. I wonder if timeout will only work for the TLS HTTPSGET. For the non TLS HTTPGET, maybe it does not work without passing the tls handle. In the case where  Aashish Bhanawat answered, it was for a different device more specifically about a Wifi device. I'm not an expert in HTTPCli and not sure if I can help any further. 

  • Hi Charles,
    i went through this doc and came to know that -109 error is due to calling HttpCLi_getResponseStatus API , as the error states that we shouldn't call this API in async mode its the reason why its giving us -109 error.
    can you forward my issue to  HTTPCli epert you know ?

    Regards
    Chiranth

  • Hi Chiranth,

      I don't understand what you mean that HttpCLi_getResponseStatus cannot be used in async mode. Please look at the below HTTP GET example from Resource Explorer that you can import. Can you successfully run this example? I don't expect you will get -109 if you run this example. Also sorry, while I'm not an expert in the NDK Network Services such as HTTP, I can't find experts to comment either.  

  • Hi Charles,

    i tried with http and https without using that macro HTTPCli_LIBTYPE_MIN , the tiemout is wokring with plain http and not working with https, i tested by changing the timeout value from 1,2,3 and so on . any suggestion on this ?

    regards
    chiranth

  • Hi Chiranth,

    the tiemout is wokring with plain http and not working with https,

    Earlier in your response, you said HTTP does not work  with timeout either. Now you said it is working for HTTP but not HTTPS. Sorry, I really don't know why it is working for HTTP but not HTTPS.