Dear TI FAE Engineer
When the CC3235 operates in AP mode as a client and sends a GET request, it encounters a Socket Error Error! code = -111, Description = GETTask: connect failed.We look forward to your prompt response as the project is extremely urgent.Below is the code for httpget.c:
/*
* Copyright (c) 2015-2017, 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 ========
* HTTP Client GET example application
*/
/* BSD support */
#include <stdlib.h>
#include <stdio.h>
#include <stdint.h>
#include <pthread.h>
#include <unistd.h>
#include <mqueue.h>
#include <assert.h>
#include <string.h>
#include "CRC32.h"
#include <ti/display/Display.h>
#include <ti/net/http/httpclient.h>
#include "semaphore.h"
#include <ti/net/slnetutils.h>
#include "user_program.h"
#define HOSTNAME "">http://www.example.com"
#define REQUEST_URI "/"
#define USER_AGENT "HTTPClient (ARM; TI-RTOS)\r\n\r\n"
#define charParams "/resource01/"
#define charParamLens 4
#define HTTP_MIN_RECV (256)
static char simpleProfileChar1[SIMPLEPROFILE_CHAR1_LEN];
static char simpleProfileChar2[SIMPLEPROFILE_CHAR2_LEN];
static char simpleProfileChar3[SIMPLEPROFILE_CHAR3_LEN];
static char simpleProfileChar4[SIMPLEPROFILE_CHAR4_LEN];
static char simpleProfileChar5[SIMPLEPROFILE_CHAR5_LEN];
static char simpleProfileChar6[SIMPLEPROFILE_CHAR6_LEN];
static char simpleProfileChar7[SIMPLEPROFILE_CHAR7_LEN];
static char simpleProfileChar8[SIMPLEPROFILE_CHAR8_LEN];
static char simpleProfileChar9[SIMPLEPROFILE_CHAR9_LEN];
static char simpleProfileChar10[SIMPLEPROFILE_CHAR10_LEN];
static char simpleProfileChar11[SIMPLEPROFILE_CHAR11_LEN];
extern void User_processCharValueChangeEvt(char *state);
extern char gStationIp[];//IP地址
//const char FeedbackData[]="0700000000000000000000000F4D2FE0";
char ClientPostDataRespon[7];
char receiveUpdataUrl[11];
char UpdataUrl[10];
//extern void* SimpleWifiPeripheral_taskFxn(void* pvParameters);
extern void HttpClient_GetParameter(const char *charparam, char *value);
extern Display_Handle display;
extern sem_t ipEventSyncObj;
extern sem_t SyncObj;
extern void printError(char *errString,
int code);
enum RESOURCEx resource_get = resource00;//httpget话题切换标志位
enum RESOURCEx resource_post;//Fxnpost话题切换标志位
#include <ti/drivers/net/wifi/simplelink.h>
// 获取资源枚举值
enum RESOURCEx getResourceEnum(const char *UpdataUrl) {
if (strcmp(UpdataUrl, "resourceFF") == 0) {
return resource00;
} else if (strcmp(UpdataUrl, "resource01") == 0) {
return resource01;
} else if (strcmp(UpdataUrl, "resource02") == 0) {
return resource02;
}else if (strcmp(UpdataUrl, "resource03") == 0) {
return resource03;
}else if (strcmp(UpdataUrl, "resource04") == 0) {
return resource04;
}else if (strcmp(UpdataUrl, "resource05") == 0) {
return resource05;
}else if (strcmp(UpdataUrl, "resource06") == 0) {
return resource06;
}else if (strcmp(UpdataUrl, "resource07") == 0) {
return resource07;
}else if (strcmp(UpdataUrl, "resource08") == 0) {
return resource08;
}else if (strcmp(UpdataUrl, "resource09") == 0) {
return resource09;
}else if (strcmp(UpdataUrl, "resource0A") == 0) {
return resource0A;
}else if (strcmp(UpdataUrl, "resource0B") == 0) {
return resource0B;
}
return -1;
}
//POST相关的变量
extern uint8_t TemBleSoftInfo;
extern char BleSoftInfo;
//发数据至服务器
void HttpClient_SendPostRequest(const char *charParam, uint8_t charParamLen, char *value)
{
int16_t len = 0;
bool moreDataFlag = false;
const char contentType[] = "text/plain; charset=utf-8";
int16_t ret = 0;
HTTPClient_Handle httpClientHandle;
int16_t statusCode;
// // 获取互斥锁(如果被占用就等待)
// Semaphore_pend(httpMutexHandle, OS_WAIT_FOREVER);
Display_printf(display, 0, 0, "POST\n");
httpClientHandle = HTTPClient_create(&statusCode,0);
// Display_printf(display, 0, 0, "POSTClient_create\n");
if(statusCode < 0)
{
printError("POSTTask: creation of http client handle failed",
statusCode);
}
ret = HTTPClient_setHeader(httpClientHandle,
HTTPClient_HFIELD_REQ_CONTENT_TYPE,
contentType,strlen(contentType)+1,
HTTPClient_HFIELD_PERSISTENT);
// Display_printf(display, 0, 0, "setHeader\n");
if(ret < 0)
{
printError("POSTTask: setting request header failed", ret);
}
ret = HTTPClient_connect(httpClientHandle,gStationIp,0,0);//gStationIp作为全局变量用
Display_printf(display, 0, 0, "connected\n");
if(ret < 0)
{
printError("POSTTask: connect failed", ret);
}
ret = HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_POST,charParam,
value,charParamLen, 0);
// Display_printf(display, 0, 0, "SendedPOSTRequest\n");
if(ret < 0)
{
printError("POSTTask: send failed", ret);
}
if(ret != HTTP_SC_OK)
{
printError("POSTTask: cannot get status", ret);
}
//Display_printf(display, 0, 0, "HTTP Response Status Code: %d\n", ret);
len = 0;
do
{
ret = HTTPClient_readResponseBody(httpClientHandle, ClientPostDataRespon, 7,
&moreDataFlag);
if(ret < 0)
{
printError("POSTTask: response body processing failed", ret);
}
Display_printf(display, 0, 0, "Send Msg:%.*s \r\n",ret,ClientPostDataRespon);
len += ret;
}
while(moreDataFlag);
//Display_printf(display, 0, 0, "Received %d bytes of payload\n", len);
ret = HTTPClient_disconnect(httpClientHandle);
if(ret < 0)
{
printError("POSTTask: disconnect failed", ret);
}
ret = HTTPClient_destroy(httpClientHandle);
if(ret < 0)
{
printError("POSTTask: destroy Client failed", ret);
}
Display_printf(display, 0, 0, "POST finish\n");
// // 释放互斥锁
// Semaphore_post(httpMutexHandle);
}
/*********************************************************************
* @fn SimpleProfile_SetParameter
*
* @brief send data.发送数据至服务器
*
* @param charparam, len value
*
* @return esp_gatt_status_t.
*/
void HttpClient_SetParameter(const char *charparam, uint16_t charParamLen, char *value )
{
if(strcmp(charparam, SIMPLEPROFILE_CHAR1) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR1,SIMPLEPROFILE_CHAR1_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR2) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR2,SIMPLEPROFILE_CHAR2_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR3) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR3,SIMPLEPROFILE_CHAR3_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR4) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR4,SIMPLEPROFILE_CHAR4_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR5) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR5,SIMPLEPROFILE_CHAR5_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR6) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR6,SIMPLEPROFILE_CHAR6_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR7) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR7,SIMPLEPROFILE_CHAR7_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR8) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR8,SIMPLEPROFILE_CHAR8_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR9) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR9,SIMPLEPROFILE_CHAR9_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR10) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR10,SIMPLEPROFILE_CHAR10_LEN,value);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR11) == 0)
{
HttpClient_SendPostRequest(SIMPLEPROFILE_CHAR11,SIMPLEPROFILE_CHAR11_LEN,value);
}
}
//从服务器接收数据
void HttpClient_SendGetRequest(const char *charParam, uint8_t charParamLen, char *value)
{
int16_t len = 0;
int connect_counter = 0;
bool moreDataFlag = false;
int16_t statusCode;
const char contentType[] = "text/plain; charset=utf-8";
int16_t ret = 0;
HTTPClient_Handle httpClientHandle;
// // 获取互斥锁(如果被占用就等待)
// Semaphore_pend(httpMutexHandle, OS_WAIT_FOREVER);
Display_printf(display, 0, 0, "GET\n");
httpClientHandle = HTTPClient_create(&statusCode,0);
// Display_printf(display, 0, 0, "Client_created\n");
// Display_printf(display, 0, 0, "statusCode:%d\n",statusCode);
if(statusCode < 0)
{
printError("GETTask: creation of http client handle failed",
statusCode);
}
ret = HTTPClient_setHeader(httpClientHandle,
HTTPClient_HFIELD_REQ_CONTENT_TYPE,
contentType,strlen(contentType)+1,
HTTPClient_HFIELD_PERSISTENT);
// Display_printf(display, 0, 0, "setHeader\n");
// Display_printf(display, 0, 0, "HTTPClient_setHeader:%d\n",ret);
if(ret < 0)
{
printError("GETTask: setting request header failed", ret);
}
// Display_printf(display, 0, 0, "gStationIp='%s'\n",gStationIp);
Display_printf(display, 0, 0, "connecting\n");
ret = HTTPClient_connect(httpClientHandle,gStationIp,0,0);//gStationIp作为全局变量用
// Display_printf(display, 0, 0, "connected\n");
// Display_printf(display, 0, 0, "HTTPClient_connect:%d\n",ret);
if(ret < 0)
{
printError("GETTask: connect failed", ret);
// Display_printf(display, 0, 0, "GETTask: connect failed, try to reconnect\n");
// while(ret<0)//连接失败重复连接5000次
// {
// connect_counter++;
// ret = HTTPClient_connect(httpClientHandle,gStationIp,0,0);//gStationIp作为全局变量用
//
// }
// Display_printf(display, 0, 0, "reconnect:%d times\n",connect_counter);
}
ret = HTTPClient_sendRequest(httpClientHandle,HTTP_METHOD_GET,charParam,
NULL,0,
0);
// Display_printf(display, 0, 0, "SendedGetRequest\r\n");
// Display_printf(display, 0, 0, "HTTPClient_sendRequest:%d\n",ret);
if(ret < 0)
{
printError("GETTask: send failed", ret);
}
if(ret != HTTP_SC_OK)
{
printError("GETTask: cannot get status", ret);
}
//Display_printf(display, 0, 0, "HTTP Response Status Code: %d\n", ret);
len = 0;
do
{
ret = HTTPClient_readResponseBody(httpClientHandle, value+len, charParamLen,
&moreDataFlag);
// Display_printf(display, 0, 0, "HTTPClient_readResponseBody:%d\n",ret);
if(ret < 0)
{
printError("GETTask: response body processing failed", ret);
}
Display_printf(display, 0, 0, "%s \r\n",value+len);
len += ret;
}while(moreDataFlag);
//Display_printf(display, 0, 0, "Received %d bytes of payload\n", len);
ret = HTTPClient_disconnect(httpClientHandle);
// Display_printf(display, 0, 0, "HTTPClient_disconnect:%d\n",ret);
if(ret < 0)
{
printError("GETTask: disconnect failed", ret);
}
ret = HTTPClient_destroy(httpClientHandle);
// Display_printf(display, 0, 0, "HTTPClient_destroy:%d\n",ret);
if(ret < 0)
{
printError("GETTask: destroy Client failed", ret);
}
Display_printf(display, 0, 0, "GET finish\n");
// // 释放互斥锁
// Semaphore_post(httpMutexHandle);
}
//void HttpClient_SendGetRequest(HTTPClient_Handle httpClientHandle,const char *charParam, uint8_t charParamLen, char *value)
/*********************************************************************
* @fn HttpClient_GetRequest
*
* @brief receive data . 从服务器接收数据
*
* @param charparam, value
*
* @return esp_gatt_status_t.
*/
void HttpClient_GetParameter(const char *charparam, char *value)
{
//char *prf_char;
// memcpy(dst, src, SIMPLEPROFILE_CHAR1_LEN);
if(strcmp(charparam, SIMPLEPROFILE_CHAR1) == 0)
{
//HttpClient_SendGetRequest(httpClientHandle,SIMPLEPROFILE_CHAR0, SIMPLEPROFILE_CHAR0_LEN,receiveUpdataUrl);
// HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR1_LEN,value);
memcpy(value, userData.Tem_Attr_Buff, SIMPLEPROFILE_CHAR1_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR2) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR2_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR2_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR3) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR3_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR3_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR4) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR4_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR4_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR5) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR5_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR5_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR6) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR6_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR6_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR7) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR7_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR7_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR8) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR8_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR8_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR9) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR9_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR9_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR10) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR10_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR10_LEN);
}
else if(strcmp(charparam, SIMPLEPROFILE_CHAR11) == 0)
{
HttpClient_SendGetRequest(charparam,SIMPLEPROFILE_CHAR11_LEN,value);
//memcpy(value, prf_char, SIMPLEPROFILE_CHAR11_LEN);
}
}
int SPIcount = 0;
int TIMEcount = 0;
void SimpleWifiPeripheral_processAppMsg(void)
{
//int quecommand;
// Display_printf(display, 0, 0, "mq_receive\n");
mq_receive(commandQueue, (char*)&command, sizeof(command), NULL);
switch (command)
{
case SBP_SPI_TRANSFER_DONE_EVT://SPI传输事件
// Display_printf(display, 0, 0, "SPIcount:%d\n",SPIcount);
usleep(10000);//test
command=0;
SPIcount++;
// get_flag = 0;//该函数执行置一
User_processSPITransferDoneEvt(userData.state);
break;
case SBP_TIMER_OUT_EVT://定时器事件
// Display_printf(display, 0, 0, "TIMEcount:%d\n",TIMEcount);
usleep(10000);//test
userData.state = 0;
command=0;
TIMEcount++;
// post_flag = 1;//该函数执行置一
User_processTimerOutEvt(userData.state);
break;
case SBP_CHAR_CHANGE_EVT://读取话题事件
User_processCharValueChangeEvt(UpdataUrl);
break;
default:
break;
}
// mq_close();
}
/*
* ======== SimpleWifiPeripheral_taskFxn ========
* spi event and timeout event
*/
void* SimpleWifiPeripheral_taskFxn(void* pvParameters)//线程1
{
enum RESOURCEx resource_post;
#if 1
SlWlanConnStatusParam_t connectionParams;
uint16_t Opt = 0;
int32_t retVal = 0;
uint16_t Size = 0;
#endif
#if 1
Size = sizeof(SlWlanConnStatusParam_t);
memset(&connectionParams, 0x7e, sizeof(SlWlanConnStatusParam_t));
retVal = sl_WlanGet(SL_WLAN_CONNECTION_INFO, &Opt, &Size, (uint8_t *)&connectionParams);
Display_printf(display, 0, 0, "connection status %d, mode %d\n",
connectionParams.ConnStatus, connectionParams.Mode);
#endif
if (connectionParams.Mode == ROLE_AP)
{
//sem_wait(&ipEventSyncObj);//接收信号量获得IP值
sem_wait(&SyncObj);
Display_printf(display, 0, 0, "start \n");
}
for (;;)
{
usleep(10000);//延迟10毫秒
SimpleWifiPeripheral_processAppMsg();
}
}
/*
* ======== httpTask ========
* Makes a HTTP GET request
*/
void* httpTask(void* pvParameters)//线程2
{
int32_t status = 0;
uint8_t readCounter = 0;
char compareCounter[1];
char* counter;
// uint8_t ret = 0;
pthread_t SimpleWifiPeripheral = (pthread_t)NULL;
pthread_attr_t pAttrs;
struct sched_param priPar;
static int flag = 1;//get标志位
#if 0
SlNetSock_Addr_t *sa;
SlNetSock_AddrIn_t sAddr;
uint8_t destIP[4];
#endif
#if 1
SlWlanConnStatusParam_t connectionParams;
uint16_t Opt = 0;
int32_t retVal = 0;
uint16_t Size = 0;
#endif
#if 1
Size = sizeof(SlWlanConnStatusParam_t);
memset(&connectionParams, 0x7e, sizeof(SlWlanConnStatusParam_t));
retVal = sl_WlanGet(SL_WLAN_CONNECTION_INFO, &Opt, &Size, (uint8_t *)&connectionParams);
Display_printf(display, 0, 0, "connection status %d, mode %d\n",
connectionParams.ConnStatus, connectionParams.Mode);
#endif
#if 0
if (connectionParams.Mode == ROLE_AP)
{
sAddr.sin_family = SLNETSOCK_AF_INET;
sAddr.sin_port = SlNetUtil_htons((unsigned short)80);
destIP[0] = 10;
destIP[1] = 123;
destIP[2] = 45;
destIP[3] = 3;
sAddr.sin_addr.s_addr = SlNetUtil_htonl((((uint32_t) destIP[0] << 24) | ((uint32_t) destIP[1] << 16) | (destIP[2] << 8) | (destIP[3])));
sa = (SlNetSock_Addr_t*)&sAddr; //HTTPClient_setProxy() expects a SlNetSock_Addr_t, but the input
//is treated like a SlNetSock_AddrIn_t when the socket is created
HTTPClient_setProxy(sa);
}
#endif
if (connectionParams.Mode == ROLE_AP)
{
sem_wait(&ipEventSyncObj);//接收信号量获得IP值
Display_printf(display, 0, 0, "Sending a HTTP GET request to '%s'\n",gStationIp);//打印IP地址
sem_post(&SyncObj);
}
else
{
Display_printf(display, 0, 0, "Sending a HTTP GET request to '%s'\n",HOSTNAME);
}
int conter =0;
while(1)
{
conter++;
HttpClient_SendGetRequest(SIMPLEPROFILE_CHAR0, SIMPLEPROFILE_CHAR0_LEN,receiveUpdataUrl);
Display_printf(display, 0, 0, "%d\n",conter);
}
return(0);
}