重新建立连接时,C应用程序HTTP失败

时间:2016-09-07 11:35:39

标签: c http microcontroller

我正在开发一个可在bosch设备上运行的应用程序 - XDK - http://xdk.bosch-connectivity.com/

此时,我有一个实现http客户端并将POST请求发送到服务器的任务。但是,我的应用程序的主要目的是能够在具有网络覆盖/无覆盖的区域之间移动。这意味着我不断地暂停/重新启动实现http客户端的任务。

所以我在以下场景中遇到了http客户端的问题:我在有覆盖的区域启动应用程序,然后移动到没有覆盖的区域,最后再回到覆盖范围的区域。在这里,我遇到了问题。从那一刻起,我无法再与http客户端连接。

我会在这里发布我的应用程序的代码片段,以便您可以建议我进行一些更改来解决问题。 所有函数,如HttpClient_initRequest,HttpMsg_setReqUrl,Msg_prependPartFactory,......,都属于为XDK提供的API。

void appInitSystem(xTimerHandle xTimer) {
    (void) (xTimer);

    return_t retValue = FAILURE;
    retcode_t wlanRet = RC_PLATFORM_ERROR;

    wlanRet = wlan_init();
    if (wlanRet != RC_OK){
        printf("Network init/connection failed %i \r\n", wlanRet);
        assert(0);
    }

    wlanRet = PAL_initialize();
    if (wlanRet != RC_OK){
        printf("PAL and network initialize %i \r\n", wlanRet);
        assert(0);
    }

    PAL_socketMonitorInit();

    /* start http client */
    wlanRet = HttpClient_initialize();
    if (wlanRet != RC_OK){
        printf("Failed to initialize http client\r\n ");
        assert(0);
    }

    if (RC_OK != PAL_getIpaddress((uint8_t*) "52.58.121.5", &destAddr))
       return;

    retValue = createTasks();
    if (retValue == FAILURE)
        assert(0);
}

retcode_t wlan_init(void) {
    WLI_connectStatus_t retconnection;
    NCI_ipSettings_t myIpSettings;
    char ipAddress[PAL_IP_ADDRESS_SIZE] = { 0 };
    Ip_Address_T* IpaddressHex = Ip_getMyIpAddr();
    WLI_connectSSID_t connectSSID;
    WLI_connectPassPhrase_t connectPassPhrase;
    NCI_return_t ReturnValue = NCI_FAILURE;
    int32_t Result = INT32_C(-1);
    if (WLI_SUCCESS != WLI_init())
        return (RC_PLATFORM_ERROR);

    connectSSID = (WLI_connectSSID_t) WLAN_CONNECT_WPA_SSID;
    connectPassPhrase = (WLI_connectPassPhrase_t) WLAN_CONNECT_WPA_PASS;

    /*DHCP */
    ReturnValue = NCI_setIpDhcp(NULL);
    if (ReturnValue != NCI_SUCCESS){
        printf("Error in setting IP to DHCP\n\r");
        return (RC_PLATFORM_ERROR);
    }

    if (WLI_SUCCESS == WLI_connectWPA(connectSSID, connectPassPhrase, NULL){

        ReturnValue = NCI_getIpSettings(&myIpSettings);
        if (NCI_SUCCESS == ReturnValue){
            *IpaddressHex = Basics_htonl(myIpSettings.ipV4);
            Result = Ip_convertAddrToString(IpaddressHex, ipAddress);
            if (Result < 0){
                printf("Couldn't convert the IP address to string format \r\n ");
                return (RC_PLATFORM_ERROR);
            }
            printf("Connected to WPA network successfully \r\n ");
            printf(" Ip address of the device %s \r\n ", ipAddress);
            return (RC_OK);
        }
        else{
            printf("Error in getting IP settings\n\r");
            return (RC_PLATFORM_ERROR);
        }

   }
   else
      return (RC_PLATFORM_ERROR);

   return RC_OK;
}


/* Task which calls the nttp client */
void dataOffload(void* handle) {
    (void) handle;

     for (;;){
         ....
         http_connServer(NULL);
         ...
         vTaskDelay(1000/portTICK_RATE_MS);
     }
 }

void http_connServer(void * pvParameters){

    retcode_t rc = RC_OK;
    Msg_T* msg_ptr;
    Ip_Port_T destPort = (Ip_Port_T) DEST_PORT_NUMBER;
    static Callable_T SentCallable;
    char const *url_ptr = "/";  
    Callable_T * Callable_pointer;
    Callable_pointer = Callable_assign(&SentCallable, http_callbackOnSent);
    if (Callable_pointer == NULL)
        return;

    rc = HttpClient_initRequest(&destAddr, Ip_convertIntToPort(destPort), &msg_ptr);
    if (rc != RC_OK || msg_ptr == NULL)
        return;

    rc = Msg_prependPartFactory( msg_ptr, &http_setContent);
    if (rc != RC_OK) 
      return;

    rc = HttpMsg_setReqUrl(msg_ptr, url_ptr);
    if (rc != RC_OK) 
        return;

    HttpMsg_setReqMethod(msg_ptr, Http_Method_Post);

    rc = HttpClient_pushRequest(msg_ptr, &SentCallable,
        http_clientResponseCallback);
    if (rc != RC_OK){
        printf("Failed HttpClient_pushRequest \r\n  ");
        return;
    }
}

0 个答案:

没有答案