Hello,
I am having a weird problem with the NDK stack, when I try to make a http get request, I get from time to time an error 101 (on httpcli_connect), and in between the value are well received.
Before, I was having this kind of error after disconnecting and reconnecting the ethernet physical link but now it's happening even without disconnection of physical socket...
Here the source code based on the HTTP Get example for TM4C1294XL board.
/* * Copyright (c) 2015, 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. */ /* * ======== httpget.c ======== * An HTTP Client example application that GETs weather info. */ #include <string.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/knl/Task.h> #include <ti/drivers/GPIO.h> #include <ti/net/http/httpcli.h> #include <sys/socket.h> /* Example/Board Header file */ #include "Board.h" #define IP "54.175.219.8"
#define PORT 80
#define HOSTNAME "httpbin.org"
#define REQUEST_URI "/ip"
#define HTTPTASKSTACKSIZE 2048 /* * ======== printError ======== */ void printError(char *errString, int code) { System_printf("Error! code = %d, desc = %s\n", code, errString); System_flush(); BIOS_exit(code); } /* * ======== httpTask ======== * Makes an HTTP GET request */ Void httpTask(UArg arg0, UArg arg1) { bool moreFlag = false; char db_write_ok[20]={0x00,0x00,0x00,0x00,'\0'}; int ret; char header[100]={}; struct sockaddr_in addr; HTTPCli_Struct cli; HTTPCli_Field fields[2] = { { HTTPStd_FIELD_NAME_HOST, HOSTNAME }, { NULL, NULL } }; while(1) { // Set port and IP address of the database addr.sin_family = AF_INET; addr.sin_port = htons(PORT); inet_pton(AF_INET, IP, &addr.sin_addr); HTTPCli_construct(&cli); HTTPCli_setRequestFields(&cli, fields); ret = HTTPCli_connect(&cli, (struct sockaddr *)&addr, 0, NULL); if (ret < 0) { System_printf("httpTask: connect failed : %d\n", ret);System_flush(); HTTPCli_disconnect(&cli); HTTPCli_destruct(&cli); } ret = HTTPCli_sendRequest(&cli, HTTPStd_GET, REQUEST_URI, false); if (ret < 0) { System_printf("httpTask: send failed : %d\n", ret);System_flush(); HTTPCli_disconnect(&cli); HTTPCli_destruct(&cli); } ret = HTTPCli_getResponseStatus(&cli); if (ret != HTTPStd_OK) { System_printf("httpTask: cannot get status : %d\n", ret);System_flush(); HTTPCli_disconnect(&cli); HTTPCli_destruct(&cli); } ret = HTTPCli_getResponseField(&cli, header, sizeof(header), &moreFlag); if (ret != HTTPCli_FIELD_ID_END) { System_printf("httpTask: response field processing failed : %d\n", ret);System_flush(); HTTPCli_disconnect(&cli); HTTPCli_destruct(&cli); } ret = HTTPCli_readResponseBody(&cli, db_write_ok, sizeof(db_write_ok), &moreFlag); if (ret < 0) { System_printf("httpTask: response body processing failed : %d\n", ret);System_flush(); HTTPCli_disconnect(&cli); HTTPCli_destruct(&cli); } System_printf("Rcv : %s\n",db_write_ok);System_flush(); HTTPCli_disconnect(&cli); HTTPCli_destruct(&cli); Task_sleep(3000); } } /* * ======== 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)httpTask, &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 HTTP 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); }
The TI-RTOS version is 2.14.00.10.
Thank you in advance for your help.