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:TI-RTOS 2.16.01.14 HttpCli 库中的 HTTP 超时参数

Guru**** 2524550 points
Other Parts Discussed in Thread: TM4C129ENCPDT

请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

https://e2e.ti.com/support/microcontrollers/arm-based-microcontrollers-group/arm-based-microcontrollers/f/arm-based-microcontrollers-forum/1399120/tm4c129encpdt-http-timeout-param-in-ti-rtos-2-16-01-14-httpcli-library

器件型号:TM4C129ENCPDT

工具与软件:

大家好:
我无法在 Httpsget 示例中设置 http 调用的超时。

HT4.12.4.12.4(&params) Cli_Params_;
params.tls = TLS;

params.timeout = 1;// 1秒。

是否在 任何底层文件的库中设置了超时?

在哪里以及如何在代码中设置 params.timeout statcaly?

此致
线性调频脉冲

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

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

     您如何知道它无法正常工作?

     您是否可以尝试其他值、如2s、3s 或5s?  

     您是否尝试过没有 TLS 的 HTTPGET? 超时是否有效? 我想说的是、问题与 TLS 有关、而不是与 TLS 有关。  

     阅读此帖子、超时应该可行。  https://e2e.ti.com/support/processors-group/processors/f/processors-forum/523790/httpcli_getresponse-timeout/1904709?tisearch=e2e-sitesearch&keymatch=HTTPCli_Params% 252520超时#1904709

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Charles:

    我在 HttpGet 示例中尝试了未使用 TLS 的 HTTPGET、但它在这里甚至不起作用。

    此致

    线性调频脉冲

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    您好!

     能否详细阐述不起作用的问题? 如果将超时值保留为默认值0、会发生什么情况? 当您更改为1、2、3...时会发生什么情况? 它是否完全不超时或没有根据您指定的时间超时?

     请查看这篇文章、看看定义  HTTPCli_LIBTYPE_MIN 是否会有所不同。 请参阅 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% 252520timeout#1803901.

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Charles:

    "如果将超时值保留为默认值等于0、会发生什么情况。 当您更改为1、2、3...时会发生什么情况? 它是否完全不超时或没有根据您指定的时间超时?"

    最初我没有定义 HTTPCli_LIBTYPE_MIN 我的应用程序工作正常,没有定义这个宏,有几个 http 调用需要超过一分钟的时间来获得响应,所以我想增加超时到90秒。 我在 TI RTOS SDK 的 httpcli.h 文件中阅读了该 params.timeout。 我使用了 params.timeout = 1;由于我的应用平均需要2.5秒从我们的服务器获得响应、所以为了测试这个超时、我将超时定义为1秒。 它应该给我超时错误与错误代码-104或类似的错误代码。 只是通过使用这个超时而不定义宏  HTTPCli_LIBTYPE_MIN 没有 在意义上工作,即使在内部定义10秒,它没有做任何区别,它使用从内核内部获取默认超时值,我认为,所以它没有为我的应用训练。 我阅读了  Aashish Bhanawah 的答案,并尝试定义 HTTPCli_LIBTYPE_MIN 宏 ,并在 HttpGet 和 httpsget 示例中使用 params.timeout = 1指向 www.google.com 和我自己的服务器通常需要1.5秒才能得到响应。 我收到-109错误、指出无法获取状态。 当我使用 params.timeout = 0时、它得到200 resp 代码的响应。 这意味着超时设置为默认值。 如何在我的应用中使用该超时东西? 如果它适用于 HttpGet 和 https://focus.ti.com.cn 示例、那么它肯定适用于我的应用程序。 请向我建议一些解决方法。

    此致

    Chiranth

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Charles:

    MCU:Tm4C129ENCPDT
    TIRTOS:2.16.01.14

    XDC 工具:xdctools_3_32_00_06_core

    这是我在 TI RTOS 的 httpcli.h 文件中的代码片段。

    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;

    我在那里看到了 tls_handle *tls;这 httpcli.h 文件是不是它存在的情况下 ,其他人 Aashish Bhanawat 回答。 剂量此 TLS 事件导致错误-109?

    此致
    线性调频脉冲

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。
    我看到那里有 tls_handle *tls;这个 httpcli.h 文件是不是它在其他人的情况下它是不存在的,其中  Aashish Bhanawat 回答。 剂量此 TLS 事件导致错误-109 ?[/报价]

    请参阅  TI-RTOS 安装中的文件:///C/C:/ti/tirtos_tivac_2_16_00_08/products/ns_1_11_00_10/docs/html/httpcli_8h_source.html。 它对各种错误代码进行了说明。 我想知道 超时是否只对 TLS HTTPSGET 有效。 对于非 TLS HTTPGET、可能在没有传递 TLS 句柄的情况下无法工作。 在   Aashish Bhanawat 回答的案例中、更具体地说是针对 Wi-Fi 设备的不同设备。 我不是 HTTPCli 的专家,不确定我是否能进一步帮助。  

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Charles:
    我浏览了这个文档,并知道-109错误是由于调用 HttpCLI_getResponseStatus API ,因为错误指出,我们不应该在异步模式下调用这个 API ,这是它给我们-109错误的原因。
    你能把我的问题转发给 HTPCli epert 你知道吗?

    此致
    Chiranth

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    Chiranth、您好!

     我不理解您的意思、即  无法在异步模式下使用 HttpCLi_getResponseStatus。 请查看以下您可以导入的 Resource Explorer 中的 HTTP GET 示例。 您能否成功运行此示例? 如果运行此示例、我不希望您得到-109。 也很抱歉、虽然我不是 NDK 网络服务(如 HTTP)的专家、但我也找不到专家来评论。   

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    尊敬的 Charles:

    我尝试使用 http 和 https 没有使用宏 HTTPCli_LIBTYPE_min , tiemout 是 wokring with plain http 和不使用 https ,我通过改变超时值从1,2,3等测试. 对此有何建议?

    此致
    线性调频脉冲

  • 请注意,本文内容源自机器翻译,可能存在语法或其它翻译错误,仅供参考。如需获取准确内容,请参阅链接中的英语原文或自行翻译。

    Chiranth、您好!

    使用纯 http 生成的 tiemout 无法使用 https、//quote]

    在前面的响应中、您提到 HTTP 也不能在超时的情况下工作。 现在、您说它适用于 HTTP、但不适用于 HTTPS。 对不起,我真的不知道为什么它适用于 HTTP 而不是 HTTPS。