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.

LAUNCHXL-CC3235S: cc3235s go to error

Part Number: LAUNCHXL-CC3235S
Other Parts Discussed in Thread: CC3235S

hi:

my board is LAUNCHXL-CC3235S, when the code is receiving data from server for about 10 minutes,mycode will into following:

void SimpleLinkFatalErrorEventHandler(SlDeviceFatal_t *slFatalErrorEvent) ,the print infomation is " FATAL ERROR: Abort NWP event detected,AbortType  = 3".

i can send you my project if you need it 

    thank you !

  • hi, Vince

        I am very sorry, I was sick some time ago and I lost some time. Here is my log, and I found that when I send a lot of data to CC3235 through the serial port (CC3235 forwards the data to sl_Send()), it will enter the error state. If I simulate a thread to send data directly through sl_Send, then Will not enter the error state

    ReceivedTofile-COM2-2019_10_10_15-24-19.DAT

    Best Regards,
    edison

  • Hi User,

    From the logs, it looks like you are constantly sending scan results get command in your application. Is this intentional?

    Can you share a snippet of your code example? Are you using a example from the SDK?

    Best Regards,
    Vince 

  • hi Vince :

             following two files is my code using network_terminal_CC3235S_LAUNCHXL_freertos_ccs.

             Called the function GetRssi() (constantly sending scan results get command )on line 971 of network_terminal.c,  

    #define MAIN_GLOBALS 1
    /* Standard includes */
    #include <luckcome_doppler.h>
    #include <stdlib.h>
    #include <stdint.h>
    
    /* TI-DRIVERS Header files */
    #include <ti/drivers/net/wifi/simplelink.h>
    
    /* Example/Board Header files */
    #include "cmd_parser.h"
    #include "wlan_cmd.h"
    #include "netapp_cmd.h"
    #include "socket_cmd.h"
    #include "transceiver_cmd.h"
    #include "userInfo.h"
    
    /* Application defines */
    #define SIX_BYTES_SIZE_MAC_ADDRESS  (17)
    
    /****************************************************************************
                          LOCAL FUNCTION PROTOTYPES
    ****************************************************************************/
    int32_t showAvailableCmd();
    int32_t cmd_prompt(void *arg);
    int32_t cmdClearcallback(void *arg);
    int32_t printClearUsage(void *arg);
    int32_t cmdHelpCallback(void *arg);
    int32_t printHelpUsage(void *arg);
    int32_t initAppVariables();
    int32_t sem_wait_timeout(sem_t *sem, uint32_t Timeout);
    
    
    int32_t            wifiStatus = 0;
    int32_t            rssi = 1;
    SlDeviceVersion_t ver = {0};
    uint8_t            macAddress[SL_MAC_ADDR_LEN];
    uint16_t           macAddressLen = SL_MAC_ADDR_LEN;
    int32_t            demoTest = 0;
    
    pthread_t           gSpawn_thread = (pthread_t)NULL;
    appControlBlock     app_CB;
    
    
    /*!
        \brief          SimpleLinkWlanEventHandler
    
        This handler gets called whenever a WLAN event is reported
        by the host driver / NWP. Here user can implement he's own logic
        for any of these events. This handler is used by 'network_terminal'
        application to show case the following scenarios:
    
        1. Handling connection / Disconnection.
        2. Handling Addition of station / removal.
        3. RX filter match handler.
        4. P2P connection establishment.
    
        \param         pWlanEvent       -   pointer to Wlan event data.
    
        \return        void
    
        \note          For more information, please refer to: user.h in the porting
                       folder of the host driver and the 
                       CC31xx/CC32xx NWP programmer's guide (SWRU455)
    
        \sa            cmdWlanConnectCallback, cmdEnableFilterCallback,
                       cmdWlanDisconnectCallback, cmdP2PModecallback.
    
    */
    void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent)
    {
        if(!pWlanEvent)
        {
            return;
        }
    
        switch(pWlanEvent->Id)
        {
            case SL_WLAN_EVENT_CONNECT:
            {
                SET_STATUS_BIT(app_CB.Status, STATUS_BIT_CONNECTION);
    
                /* Copy new connection SSID and BSSID to global parameters */
                memcpy(app_CB.CON_CB.ConnectionSSID, pWlanEvent->Data.Connect.SsidName,
                       pWlanEvent->Data.Connect.SsidLen);
                memcpy(app_CB.CON_CB.ConnectionBSSID, pWlanEvent->Data.Connect.Bssid,
                       SL_WLAN_BSSID_LENGTH);
    
                UART_PRINT(
                    "\n\r[WLAN EVENT] STA Connected to the AP: %s , "
                    "BSSID: %x:%x:%x:%x:%x:%x\n\r",
                    app_CB.CON_CB.ConnectionSSID,
                    app_CB.CON_CB.ConnectionBSSID[0],
                    app_CB.CON_CB.ConnectionBSSID[1],
                    app_CB.CON_CB.ConnectionBSSID[2],
                    app_CB.CON_CB.ConnectionBSSID[3],
                    app_CB.CON_CB.ConnectionBSSID[4],
                    app_CB.CON_CB.ConnectionBSSID[5]);
    
                sem_post(&app_CB.CON_CB.connectEventSyncObj);
            }
            break;
            case SL_WLAN_EVENT_DISCONNECT:
            {
                SlWlanEventDisconnect_t  *pEventData = NULL;
    
                CLR_STATUS_BIT(app_CB.Status, STATUS_BIT_CONNECTION);
                CLR_STATUS_BIT(app_CB.Status, STATUS_BIT_IP_ACQUIRED);
                CLR_STATUS_BIT(app_CB.Status, STATUS_BIT_IPV6_ACQUIRED);
    
                /* If ping operation is running, release it. */
                if(IS_PING_RUNNING(app_CB.Status))
                {
                    sem_post(&app_CB.CON_CB.eventCompletedSyncObj);
                    UART_PRINT(
                        "\n\rPing failed, since device is no longer connected.\n\r");
                }
    
                pEventData = &pWlanEvent->Data.Disconnect;
    
                /* If the user has initiated 'Disconnect' request,
                   'reason_code' is SL_WLAN_DISCONNECT_USER_INITIATED */
                if(SL_WLAN_DISCONNECT_USER_INITIATED == pEventData->ReasonCode)
                {
                    UART_PRINT(
                        "\n\r[WLAN EVENT] Device disconnected from the AP: %s,\n\r"
                        "BSSID: %x:%x:%x:%x:%x:%x on application's request \n\r",
                        app_CB.CON_CB.ConnectionSSID,
                        app_CB.CON_CB.ConnectionBSSID[0],
                        app_CB.CON_CB.ConnectionBSSID[1],
                        app_CB.CON_CB.ConnectionBSSID[2],
                        app_CB.CON_CB.ConnectionBSSID[3],
                        app_CB.CON_CB.ConnectionBSSID[4],
                        app_CB.CON_CB.ConnectionBSSID[5]);
                }
                else
                {
                    UART_PRINT(
                        "\n\r[WLAN ERROR] Device disconnected from the AP: %s,\n\r"
                        "BSSID: %x:%x:%x:%x:%x:%x\n\r",
                        app_CB.CON_CB.ConnectionSSID,
                        app_CB.CON_CB.ConnectionBSSID[0],
                        app_CB.CON_CB.ConnectionBSSID[1],
                        app_CB.CON_CB.ConnectionBSSID[2],
                        app_CB.CON_CB.ConnectionBSSID[3],
                        app_CB.CON_CB.ConnectionBSSID[4],
                        app_CB.CON_CB.ConnectionBSSID[5]);
                }
                memset(&(app_CB.CON_CB.ConnectionSSID), 0x0,
                       sizeof(app_CB.CON_CB.ConnectionSSID));
                memset(&(app_CB.CON_CB.ConnectionBSSID), 0x0,
                       sizeof(app_CB.CON_CB.ConnectionBSSID));
                wifiStatus = 0;
            }
            break;
            case SL_WLAN_EVENT_PROVISIONING_STATUS:
            {
                /* Do nothing, this suppress provisioning event is because
                    simplelink is configured to default state. */
            }
            break;
            case SL_WLAN_EVENT_STA_ADDED:
            {
                memcpy(&(app_CB.CON_CB.ConnectionBSSID), pWlanEvent->Data.STAAdded.Mac,
                       SL_WLAN_BSSID_LENGTH);
                UART_PRINT(
                    "\n\r[WLAN EVENT] STA was added to AP:"
                    " BSSID: %x:%x:%x:%x:%x:%x\n\r",
                    app_CB.CON_CB.ConnectionBSSID[0],
                    app_CB.CON_CB.ConnectionBSSID[1],
                    app_CB.CON_CB.ConnectionBSSID[2],
                    app_CB.CON_CB.ConnectionBSSID[3],
                    app_CB.CON_CB.ConnectionBSSID[4],
                    app_CB.CON_CB.ConnectionBSSID[5]);
            }
            break;
            case SL_WLAN_EVENT_STA_REMOVED:
            {
                memcpy(&(app_CB.CON_CB.ConnectionBSSID), pWlanEvent->Data.STAAdded.Mac,
                       SL_WLAN_BSSID_LENGTH);
                UART_PRINT(
                    "\n\r[WLAN EVENT] STA was removed "
                    "from AP: BSSID: %x:%x:%x:%x:%x:%x\n\r",
                    app_CB.CON_CB.ConnectionBSSID[0],
                    app_CB.CON_CB.ConnectionBSSID[1],
                    app_CB.CON_CB.ConnectionBSSID[2],
                    app_CB.CON_CB.ConnectionBSSID[3],
                    app_CB.CON_CB.ConnectionBSSID[4],
                    app_CB.CON_CB.ConnectionBSSID[5]);
    
                memset(&(app_CB.CON_CB.ConnectionBSSID), 0x0,
                       sizeof(app_CB.CON_CB.ConnectionBSSID));
                wifiStatus = 0;
            }
            break;
            case SL_WLAN_EVENT_RXFILTER:
            {
                SlWlanEventRxFilterInfo_t  *triggred_filter = NULL;
    
                triggred_filter = &(pWlanEvent->Data.RxFilterInfo);
    
                UART_PRINT(
                    "\n\r[WLAN EVENT] Rx filter match triggered. Set filters in"
                    "filter bitmap :0x%x.\n\r",
                    triggred_filter->UserActionIdBitmap);
    
                /*
                 *  User can write he's / her's rx filter match handler here.
                 *  Be advised, you can use the 'triggred_filter'
                 *    structure info to determine which filter
                 *  has received a match.
                 * (Bit X is set if user action id X was
                 *  passed to a filter that matched a packet.)
                 */
            }
            break;
            case SL_WLAN_EVENT_P2P_DEVFOUND:
            {
                UART_PRINT("\n\r[WLAN EVENT] P2P Remote device found\n\r");
                sem_post(&(app_CB.P2P_CB.DeviceFound));
            }
            break;
            case SL_WLAN_EVENT_P2P_REQUEST:
            {
                UART_PRINT("\n\r[WLAN EVENT] P2P Negotiation request received\n\r");
                /* This information is needed to create connection*/
                memset(&(app_CB.P2P_CB.p2pPeerDeviceName), '\0',
                       sizeof(app_CB.P2P_CB.p2pPeerDeviceName));
                memcpy(&app_CB.P2P_CB.p2pPeerDeviceName,
                       pWlanEvent->Data.P2PRequest.GoDeviceName,
                       pWlanEvent->Data.P2PRequest.GoDeviceNameLen);
    
                sem_post(&app_CB.P2P_CB.RcvNegReq);
            }
            break;
            case SL_WLAN_EVENT_P2P_CONNECT:
            {
                UART_PRINT(
                    "n\r[WLAN EVENT] P2P connection was successfully completed as "
                    "CLIENT\n\r");
                UART_PRINT("n\rBSSID is %02x:%02x:%02x:%02x:%02x:%02x\n\r",
                           pWlanEvent->Data.STAAdded.Mac[0],
                           pWlanEvent->Data.STAAdded.Mac[1],
                           pWlanEvent->Data.STAAdded.Mac[2],
                           pWlanEvent->Data.STAAdded.Mac[3],
                           pWlanEvent->Data.STAAdded.Mac[4],
                           pWlanEvent->Data.STAAdded.Mac[5]);
    
                sem_post(&app_CB.P2P_CB.RcvConReq);
            }
            break;
            case SL_WLAN_EVENT_P2P_CLIENT_ADDED:
            {
                UART_PRINT(
                    "n\r[WLAN EVENT] P2P connection was successfully completed "
                    "as GO\n\r");
                UART_PRINT("n\rBSSID is %02x:%02x:%02x:%02x:%02x:%02x\n\r",
                           pWlanEvent->Data.P2PClientAdded.Mac[0],
                           pWlanEvent->Data.P2PClientAdded.Mac[1],
                           pWlanEvent->Data.P2PClientAdded.Mac[2],
                           pWlanEvent->Data.P2PClientAdded.Mac[3],
                           pWlanEvent->Data.P2PClientAdded.Mac[4],
                           pWlanEvent->Data.P2PClientAdded.Mac[5]);
    
                sem_post(&app_CB.P2P_CB.RcvConReq);
            }
            break;
            case SL_WLAN_EVENT_P2P_DISCONNECT:
            {
                UART_PRINT("\n\r[WLAN EVENT] STA disconnected from device.\n\r");
                CLR_STATUS_BIT(app_CB.Status,STATUS_BIT_CONNECTION);
            }
            break;
            case SL_WLAN_EVENT_LINK_QUALITY_TRIGGER:
            {
                int Rssi = (int)((signed char)pWlanEvent->Data.LinkQualityTrigger.Data);
                UART_PRINT((char*)"\n\r[WLAN EVENT] Link Quality Async Event,TriggerId"
                " =%d Rssi detected =%d.\n\r ",
                pWlanEvent->Data.LinkQualityTrigger.TriggerId, Rssi);
                if (pWlanEvent->Data.LinkQualityTrigger.TriggerId == 0)
                {
                    UART_PRINT((char*)"\n\rThis trigger is host app trigger.\n\r ");
                }
                else
                {
                    UART_PRINT((char*)"\n\r[SoftRoaming] One-shot background scan "
                    "is started, search for AP with RSSI upper than %d .\n\r", Rssi);
                }
            }
            break;
            default:
            {
                UART_PRINT("\n\r[WLAN EVENT] Unexpected event [0x%x]\n\r",
                           pWlanEvent->Id);
            }
            break;
        }
        UART_PRINT(cmdPromptStr);
    }
    
    /*!
        \brief          SimpleLinkNetAppEventHandler
    
        This handler gets called whenever a Netapp event is reported
        by the host driver / NWP. Here user can implement he's own logic
        for any of these events. This handler is used by 'network_terminal'
        application to show case the following scenarios:
    
        1. Handling IPv4 / IPv6 IP address acquisition.
        2. Handling IPv4 / IPv6 IP address Dropping.
    
        \param          pNetAppEvent     -   pointer to Netapp event data.
    
        \return         void
    
        \note           For more information, please refer to:
                        user.h in the porting
                        folder of the host driver and the 
                        CC31xx/CC32xx NWP programmer's
                        guide (SWRU455).
    
    */
    int32_t ipv4_lost = -1;
    void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent)
    {
        if(!pNetAppEvent)
        {
            return;
        }
    
        switch(pNetAppEvent->Id)
        {
            case SL_NETAPP_EVENT_IPV4_ACQUIRED:
            {
                SlIpV4AcquiredAsync_t *pEventData = NULL;
    
                SET_STATUS_BIT(app_CB.Status, STATUS_BIT_IP_ACQUIRED);
    
                /* Ip Acquired Event Data */
                pEventData = &pNetAppEvent->Data.IpAcquiredV4;
                app_CB.CON_CB.IpAddr = pEventData->Ip ;
    
                /* Gateway IP address */
                app_CB.CON_CB.GatewayIP = pEventData->Gateway;
    
                UART_PRINT("\n\r[NETAPP EVENT] IP set to: IPv4=%d.%d.%d.%d , "
                        "Gateway=%d.%d.%d.%d\n\r",
    
                SL_IPV4_BYTE(app_CB.CON_CB.IpAddr,3),
                SL_IPV4_BYTE(app_CB.CON_CB.IpAddr,2),
                SL_IPV4_BYTE(app_CB.CON_CB.IpAddr,1),
                SL_IPV4_BYTE(app_CB.CON_CB.IpAddr,0),
    
                SL_IPV4_BYTE(app_CB.CON_CB.GatewayIP,3),
                SL_IPV4_BYTE(app_CB.CON_CB.GatewayIP,2),
                SL_IPV4_BYTE(app_CB.CON_CB.GatewayIP,1),
                SL_IPV4_BYTE(app_CB.CON_CB.GatewayIP,0));
                ipv4_lost = 1;
                sem_post(&(app_CB.CON_CB.ip4acquireEventSyncObj));
            }
            break;
    
            case SL_NETAPP_EVENT_IPV6_ACQUIRED:
            {
                uint32_t i = 0;
    
                SET_STATUS_BIT(app_CB.Status, STATUS_BIT_IPV6_ACQUIRED);
    
                for(i = 0 ; i < 4 ; i++)
                {
                    app_CB.CON_CB.Ipv6Addr[i] = 
                    pNetAppEvent->Data.IpAcquiredV6.Ip[i];
                }
    
                UART_PRINT("\n\r[NETAPP EVENT] IP Acquired: IPv6=");
    
                for(i = 0; i < 3; i++)
                {
                    UART_PRINT("%04x:%04x:",
                               ((app_CB.CON_CB.Ipv6Addr[i] >> 16) & 0xffff),
                               app_CB.CON_CB.Ipv6Addr[i] & 0xffff);
                }
    
                UART_PRINT("%04x:%04x", ((app_CB.CON_CB.Ipv6Addr[3] >> 16) & 0xffff),
                           app_CB.CON_CB.Ipv6Addr[3] & 0xffff);
                UART_PRINT(lineBreak);
                ipv4_lost = 1;
                sem_post(&app_CB.CON_CB.ip6acquireEventSyncObj);
    
            }
            break;
    
            case SL_NETAPP_EVENT_DHCPV4_LEASED:
            {
                SET_STATUS_BIT(app_CB.Status, STATUS_BIT_IP_LEASED);
                SET_STATUS_BIT(app_CB.Status, STATUS_BIT_IP_ACQUIRED);
    
                app_CB.CON_CB.StaIp = pNetAppEvent->Data.IpLeased.IpAddress;
                UART_PRINT(
                    "\n\r[NETAPP EVENT] IP Leased to Client: IP=%d.%d.%d.%d \n\r",
                    SL_IPV4_BYTE(app_CB.CON_CB.StaIp,
                                 3), SL_IPV4_BYTE(app_CB.CON_CB.StaIp,2),
                    SL_IPV4_BYTE(app_CB.CON_CB.StaIp,
                                 1), SL_IPV4_BYTE(app_CB.CON_CB.StaIp,0));
    
                sem_post(&(app_CB.CON_CB.ip4acquireEventSyncObj));
            }
            break;
    
            case SL_NETAPP_EVENT_DHCPV4_RELEASED:
            {
                UART_PRINT("\n\r[NETAPP EVENT] IP is released.\n\r");
            }
            break;
            case SL_NETAPP_EVENT_IPV4_LOST:
                UART_PRINT("\n\r[NETAPP EVENT] Unexpected event [0x%x] \n\r",
                           pNetAppEvent->Id);
                ipv4_lost = -1;
            break;
            default:
                {
                UART_PRINT("\n\r[NETAPP EVENT] Unexpected event [0x%x] \n\r",
                           pNetAppEvent->Id);
                }
                break;
            }
            UART_PRINT(cmdPromptStr);
    }
    
    /*!
        \brief          SimpleLinkHttpServerEventHandler
    
        This handler gets called whenever a HTTP event is reported
        by the NWP internal HTTP server.
    
        \param          pHttpEvent       -   pointer to http event data.
    
        \param          pHttpEvent       -   pointer to http response.
    
        \return         void
    
        \note           For more information, please refer to: user.h in the porting
                        folder of the host driver and the  CC31xx/CC32xx NWP 
                        programmer's guide (SWRU455).
    
     */
    void SimpleLinkHttpServerEventHandler(
        SlNetAppHttpServerEvent_t *pHttpEvent,
        SlNetAppHttpServerResponse_t *
        pHttpResponse)
    {
        /* Unused in this application */
    }
    
    /*!
        \brief          SimpleLinkGeneralEventHandler
    
        This handler gets called whenever a general error is reported
        by the NWP / Host driver. Since these errors are not fatal,
        application can handle them.
    
        \param          pDevEvent    -   pointer to device error event.
    
        \return         void
    
        \note           For more information, please refer to:
                        user.h in the porting
                        folder of the host driver and the 
                        CC31xx/CC32xx NWP programmer's
                        guide (SWRU455).
    
    */
    void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent)
    {
        if(!pDevEvent)
        {
            return;
        }
        /*
          Most of the general errors are not FATAL are are to be handled
          appropriately by the application
        */
        UART_PRINT("\n\r[GENERAL EVENT] - ID=[%d] Sender=[%d]\n\n",
                   pDevEvent->Data.Error.Code,
                   pDevEvent->Data.Error.Source);
    }
    
    /*!
        \brief          SimpleLinkSockEventHandler
    
        This handler gets called whenever a socket event is reported
        by the NWP / Host driver.
    
        \param          SlSockEvent_t    -   pointer to socket event data.
    
        \return         void
    
        \note           For more information, please refer to:
                        user.h in the porting
                        folder of the host driver and the 
                        CC31xx/CC32xx NWP programmer's
                        guide (SWRU455).
    
    */
    void SimpleLinkSockEventHandler(SlSockEvent_t *pSock)
    {
        /* Unused in this application */
    //    UART_PRINT("SimpleLinkSockEventHandler event:%d type:%d val:%d",\
    //    pSock->Event,pSock->SocketAsyncEvent.SockAsyncData.Type,pSock->SocketAsyncEvent.SockAsyncData.Val);
    
        char *CAname;
        if(SL_SOCKET_ASYNC_EVENT == pSock->Event)
        {
            /* debug print "an event received on socket %d\n",pSlSockEvent-
            >SocketAsyncEvent.SockAsyncData.Sd */
            switch(pSock->SocketAsyncEvent.SockAsyncData.Type)
            {
                case SL_SSL_NOTIFICATION_CONNECTED_SECURED:
                    UART_PRINT("\n\r ++SL_SOCKET_ASYNC_EVENT->SL_SSL_NOTIFICATION_CONNECTED_SECURED \n\r");
                    break;
                case SL_SSL_NOTIFICATION_HANDSHAKE_FAILED:
                    UART_PRINT("\n\r ++SL_SOCKET_ASYNC_EVENT->SL_SSL_NOTIFICATION_HANDSHAKE_FAILED \n\r");
                    break;
                case SL_SSL_ACCEPT:
                    UART_PRINT("\n\r ++SL_SOCKET_ASYNC_EVENT->SL_SSL_ACCEPT \n\r");
                    break;
                case SL_OTHER_SIDE_CLOSE_SSL_DATA_NOT_ENCRYPTED:
                    UART_PRINT("\n\r ++SL_SOCKET_ASYNC_EVENT->SL_OTHER_SIDE_CLOSE_SSL_DATA_NOT_ENCRYPTED \n\r");
                    break;
                case SL_SSL_NOTIFICATION_WRONG_ROOT_CA:
                    UART_PRINT("\n\r ++SL_SOCKET_ASYNC_EVENT->SL_SSL_NOTIFICATION_WRONG_ROOT_CA \n\r");
                    break;
                default:
                    break;
            }
        }
        else if(SL_SOCKET_TX_FAILED_EVENT == pSock->Event){ //see SWRU455 p116 table 6-6
            UART_PRINT("\n\r ++SL_SOCKET_TX_FAILED_EVENT-> - Status=[%d] \n\n",
                         pSock->SocketAsyncEvent.SockTxFailData.Status);
    
            switch(pSock->SocketAsyncEvent.SockTxFailData.Status)
            {
                case SL_ERROR_BSD_EAGAIN:
                case SL_ERROR_BSD_ECLOSE:
                case SL_ERROR_BSD_ETIMEDOUT:
                case SL_ERROR_BSD_ECONNREFUSED:
                case SL_ERROR_BSD_EALREADY:
                case SL_ERROR_BSD_ENOTCONN:
                    UART_PRINT("\n\r ++SL_SOCKET_TX_FAILED_EVENT->SL_ERROR_BSD_ENOTCONN \n\r");
                    break;
                default:
                    break;
            }
        }
    
    }
    
    /*!
        \brief          SimpleLinkFatalErrorEventHandler
    
        This handler gets called whenever a socket event is reported
        by the NWP / Host driver. After this routine is called, the user's
        application must restart the device in order to recover.
    
        \param          slFatalErrorEvent    -   pointer to fatal error event.
    
        \return         void
    
        \note           For more information, please refer to:
                        user.h in the porting
                        folder of the host driver and the
                        CC31xx/CC32xx NWP programmer's
                        guide (SWRU455).
        Ϊʲô���ŷ��ž��������ˣ�Ҫ����!!!!!
    */
    void SimpleLinkFatalErrorEventHandler(SlDeviceFatal_t *slFatalErrorEvent)
    {
    
        switch (slFatalErrorEvent->Id)
        {
            case SL_DEVICE_EVENT_FATAL_DEVICE_ABORT:
            {
                UART_PRINT("\n\r[ERROR] - FATAL ERROR: Abort NWP event detected: "
                            "AbortType=%d, AbortData=0x%x\n\r",
                            slFatalErrorEvent->Data.DeviceAssert.Code,
                            slFatalErrorEvent->Data.DeviceAssert.Value);
            }
            break;
    
            case SL_DEVICE_EVENT_FATAL_DRIVER_ABORT:
            {
                UART_PRINT("\n\r[ERROR] - FATAL ERROR: Driver Abort detected. \n\r");
            }
            break;
    
            case SL_DEVICE_EVENT_FATAL_NO_CMD_ACK:
            {
                UART_PRINT("\n\r[ERROR] - FATAL ERROR: No Cmd Ack detected "
                            "[cmd opcode = 0x%x] \n\r",
                                            slFatalErrorEvent->Data.NoCmdAck.Code);
            }
            break;
    
            case SL_DEVICE_EVENT_FATAL_SYNC_LOSS:
            {
                UART_PRINT("\n\r[ERROR] - FATAL ERROR: Sync loss detected n\r");
            }
            break;
    
            case SL_DEVICE_EVENT_FATAL_CMD_TIMEOUT:
            {
                UART_PRINT("\n\r[ERROR] - FATAL ERROR: Async event timeout detected "
                            "[event opcode =0x%x]  \n\r",
                                        slFatalErrorEvent->Data.CmdTimeout.Code);
            }
            break;
    
        default:
            UART_PRINT("\n\r[ERROR] - FATAL ERROR:"
            " Unspecified error detected \n\r");
            break;
        }
    }
    
    /*!
        \brief          SimpleLinkNetAppRequestEventHandler
    
        This handler gets called whenever a NetApp event is reported
        by the NWP / Host driver. User can write he's logic to handle
        the event here.
    
        \param          pNetAppRequest     -   Pointer to NetApp request structure.
    
        \param          pNetAppResponse    -   Pointer to NetApp request Response.
    
        \note           For more information, please refer to: 
                        user.h in the porting
                        folder of the host driver and the
                        CC31xx/CC32xx NWP programmer's
                        guide (SWRU455).
    
        \return         void
    
     */
    void SimpleLinkNetAppRequestEventHandler(SlNetAppRequest_t *pNetAppRequest,
                                             SlNetAppResponse_t *pNetAppResponse)
    {
        /* Unused in this application */
    }
    
    /*!
        \brief          SimpleLinkNetAppRequestMemFreeEventHandler
    
        This handler gets called whenever the NWP is done handling with
        the buffer used in a NetApp request. This allows the use of
        dynamic memory with these requests.
    
        \param          pNetAppRequest     -   Pointer to NetApp request structure.
    
        \param          pNetAppResponse    -   Pointer to NetApp request Response.
    
        \note           For more information, please refer to: 
                        user.h in the porting
                        folder of the host driver and the
                        CC31xx/CC32xx NWP programmer's
                        guide (SWRU455).
    
        \return         void
    
    */
    void SimpleLinkNetAppRequestMemFreeEventHandler(uint8_t *buffer)
    {
        /* Unused in this application */
    }
    
    
    
    int32_t cmd_prompt(void *arg)
    {
        return(0);
    }
    
    
    int32_t ipv4AddressParse(char *str,
                             uint32_t *ipv4ip)
    {
        volatile int32_t i = 0;
        uint32_t n;
        uint32_t ipv4Address = 0;
        char             *token;
    
        token = strtok(str, ".");
        if(token)
        {
            n = (int)strtoul(token, 0, 10);
        }
        else
        {
            return(-1);
        }
    
        while(i < 4)
        {
           /* Check Whether IP is valid */
           if((token != NULL) && (n < 256))
           {
               ipv4Address |= n;
               if(i < 3)
               {
                   ipv4Address = ipv4Address << 8;
               }
               token=strtok(NULL,".");
               if (token)
               {
                   n = (int)strtoul(token, 0, 10);
               }
               i++;
           }
           else
           {
               return -1;
           }
        }
    
        *ipv4ip = ipv4Address;
    
        return(0);
    }
    
    /*!
        \brief          GPIO button function callback
        This routine gets called whenever board button (SW3 for CC32xx
        or S2 for MSP-432) gets pressed. It posts the sleep semaphore,
        in order to wake the device from LPDS.
        \param          index    -   Contains the board GPIO index who
                                     triggered this function.
        \return         void
        \sa             cmdEnableWoWLANCallback
    */
    void gpioButtonFxn1(uint8_t index)
    {
        UART_PRINT("\n\rWoke up from GPIO button .. \n\r");
        sem_post(&app_CB.WowlanSleepSem);
        return;
    }
    
    int32_t GetAppBanner(void)
    {
         int32_t            ret = 0;
    
         uint16_t           ConfigSize = 0;
         uint8_t            ConfigOpt = SL_DEVICE_GENERAL_VERSION;
    
    
         ConfigSize = sizeof(SlDeviceVersion_t);
    
        /* Print device version info. */
        ret =
            sl_DeviceGet(SL_DEVICE_GENERAL, &ConfigOpt, &ConfigSize,
                         (uint8_t*)(&ver));
        ASSERT_ON_ERROR(ret, DEVICE_ERROR);
    
        /* Print device Mac address */
        ret = sl_NetCfgGet(SL_NETCFG_MAC_ADDRESS_GET, 0, &macAddressLen,
                           &macAddress[0]);
        ASSERT_ON_ERROR(ret, WLAN_ERROR);
    
    
        return(ret);
    }
    
    
    void DisplayAppBanner(char* appName, char* appVersion)
    {
        UART_PRINT("\t aaaaaaaaaaa\n");
        UART_PRINT("\t MAC1 address: %02x:%02x:%02x:%02x:%02x:%02x\n",
                   macAddress[0],
                   macAddress[1],
                   macAddress[2],
                   macAddress[3],
                   macAddress[4],
                   macAddress[5]);
        UART_PRINT("AP: %s\n",wifiLocalPara.ssid);
        UART_PRINT("pwd: %s\n",wifiLocalPara.passwd);
        UART_PRINT("serverIp: %d%d%d%d\n",wifiLocalPara.serveripAddr[0],
                   wifiLocalPara.serveripAddr[1],
                   wifiLocalPara.serveripAddr[2],
                   wifiLocalPara.serveripAddr[3]);
        UART_PRINT("serverIp: %d\n",wifiLocalPara.serverPort);
        UART_PRINT("\n\n\n");
    
    
    }
    
    int32_t    initAppVariables(void)
    {
        int32_t ret = 0;
    
        app_CB.Status = 0 ;
        app_CB.Role = ROLE_RESERVED;
        app_CB.Exit = FALSE;
    
        memset(&app_CB.CmdBuffer, 0x0, CMD_BUFFER_LEN);
        memset(&app_CB.gDataBuffer, 0x0, sizeof(app_CB.gDataBuffer));
        memset(&app_CB.CON_CB, 0x0, sizeof(app_CB.CON_CB));
    
        //wlan�����ź�����ʼ��
        ret = sem_init(&app_CB.CON_CB.connectEventSyncObj,    0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        ret = sem_init(&app_CB.CON_CB.eventCompletedSyncObj,  0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        //IP��ȡ�ź���
        ret = sem_init(&app_CB.CON_CB.ip4acquireEventSyncObj, 0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        ret = sem_init(&app_CB.CON_CB.ip6acquireEventSyncObj, 0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        memset(&app_CB.P2P_CB, 0x0, sizeof(app_CB.P2P_CB));
    
        ret = sem_init(&app_CB.P2P_CB.DeviceFound, 0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        ret = sem_init(&app_CB.P2P_CB.RcvConReq, 0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        ret = sem_init(&app_CB.P2P_CB.RcvNegReq, 0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        ret = sem_init(&app_CB.WowlanSleepSem, 0, 0);
        if(ret != 0)
        {
            SHOW_WARNING(ret, OS_ERROR);
            return(-1);
        }
    
        return(ret);
    }
    
    
    int32_t sem_wait_timeout(sem_t *sem,
                             uint32_t Timeout)
    {
        struct timespec abstime;
        abstime.tv_nsec = 0;
        abstime.tv_sec = 0;
    
        /* Since POSIX timeout are relative and not absolute,
         * take the current timestamp. */
        clock_gettime(CLOCK_REALTIME, &abstime);
        if(abstime.tv_nsec < 0)
        {
            abstime.tv_sec = Timeout;
            return (sem_timedwait(sem, &abstime));
        }
    
        /* Add the amount of time to wait */
        abstime.tv_sec += Timeout / 1000;
        abstime.tv_nsec += (Timeout % 1000) * 1000000;
    
        abstime.tv_sec += (abstime.tv_nsec / 1000000000);
        abstime.tv_nsec = abstime.tv_nsec % 1000000000;
    
        /* Call the semaphore wait API */
        return(sem_timedwait(sem, &abstime));
    }
    
    
    void GetRssi(void)
    {
        SlTransceiverRxOverHead_t *transHeader;
        _i8 needNewSocket = 0,i = 0;
        SlWlanNetworkEntry_t netEntries[30];
        int32_t rssi_now = 1;
    
        _i16 resultsCount = sl_WlanGetNetworkList(0,30,&netEntries[0]);
        for(i=0; i< resultsCount; i++)
        {
            if (strcmp(netEntries[i].Ssid, wifiLocalPara.ssid) == 0) {
    //                printf("SSID: %.32s        ",netEntries[i].Ssid);
    //                printf("Channel: %d    ",netEntries[i].Channel);
    //                printf("RSSI: %d \n",netEntries[i].Rssi);
                rssi_now = netEntries[i].Rssi;
            }
        }
        rssi = rssi_now;
    }
    
    void * ConnectProcess(void *pvParameters)
    {
        int32_t tcpConnectCount = 0;
    //    GetUserParam();
        UserInfoDefault();
        DisplayAppBanner(APPLICATION_NAME, APPLICATION_VERSION);
    //    ShowFileInfo();
    
        while(1)
        {
            int32_t ret = 0;
    
            switch(wifiStatus)
            {
                case 0:
                    UART_PRINT("\n\r ------------WlanConnect -> 1\n\r");
                    ret = WlanConnect();
                    UART_PRINT("\n\r ------------WlanConnect -> 2\n\r");
    
                    if(-1 == ret)
                        wifiStatus = 0;
                    else
                        wifiStatus = 1;
                    tcpConnectCount = 0;
                    break;
                case 1:
                    UART_PRINT("\n\r -----------TcpConnect -> 1\n\r");
                    ret = TcpConnect();
    
                    if(-1 == ret){
                      UART_PRINT("\n\r -----------TcpConnect -> 2\n\r");
                      if(++tcpConnectCount > 2){
                          UART_PRINT("\n\r -----------TcpConnect -> sl_WlanDisconnect()\n\r");
                          sl_WlanDisconnect();
                          tcpConnectCount = 0;
                          wifiStatus = 0;
                      }
                    }
                    else{
                      UART_PRINT("\n\r -----------TcpConnect -> 3\n\r");
                      wifiStatus = 2;
                      tcpConnectCount = 0;
                    }
                    break;
                case 2:
    
                    break;
                default:
                    break;
            }
    //        if(wifiStatus > 0)
    //            GetRssi();
            sleep(1);
        }
    }
    
    
    void * TcpProcess(void *pvParameters)
    {
        int32_t ret;
        while(1)
        {
            ReceiveTcp();
            usleep(10000);
        }
    }
    
    
    void * UartProcess(void *pvParameters)
    {
        int32_t ret;
    
        while(1)
        {
    #if 0
    #ifdef USE_UART_IRQ
            ReadUart();
            usleep(5000);
    #else
            Receive2ParseDopplerUart();
    #endif
    #else
            DemoSendToTcp();
            usleep(100000);
    #endif
        }
    }
    
    
    
    void * mainThread(void *arg)
    {
        int32_t             RetVal ;
         pthread_attr_t      pAttrs_spawn;
         pthread_attr_t      pAttrs;
         struct sched_param  priParam;
         pthread_t eventThread = (pthread_t)NULL;
         pthread_t connectThread = (pthread_t)NULL;
         pthread_t lowPriThread = (pthread_t)NULL;
         struct timespec     ts = {0};
         int status;
    
         SPI_init();
         GPIO_init();
    #ifdef USE_UART_IRQ
         InitRecfifo();
    #endif
         RetVal = initAppVariables();
         InitTerm();
    
         /* initialize the realtime clock */
         clock_settime(CLOCK_REALTIME, &ts);
    
         /* Switch off all LEDs on boards */
         GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_OFF);
         GPIO_write(Board_GPIO_LED1, Board_GPIO_LED_OFF);
         GPIO_write(Board_GPIO_LED2, Board_GPIO_LED_OFF);
    
         sl_Stop(0);//add by edison
         /*���� spawn �߳� */
         /* ���̱߳����� Sl_start()����ǰ����ʼ��*/
         /* ���̵߳�Ŀ���Ǵ����NWP���͵��첽�¼��� ÿ���¼��������࣬�Ժ����������������¼���������� */
         pthread_attr_init(&pAttrs_spawn);
         priParam.sched_priority = SPAWN_TASK_PRIORITY;
         RetVal = pthread_attr_setschedparam(&pAttrs_spawn, &priParam);
         RetVal |= pthread_attr_setstacksize(&pAttrs_spawn, TASK_STACK_SIZE);
         RetVal = pthread_create(&gSpawn_thread,
                                 &pAttrs_spawn,
                                 sl_Task,
                                 NULL);
         if(RetVal != 0)
         {
             UART_PRINT("Network Terminal - Unable to create spawn thread \n");
             return(NULL);
         }
    
         /* Before turning on the NWP on, reset any previously configured parameters */
          /*
               IMPORTANT NOTE - This is an example reset function,
               user must update this function to match the application settings.
           */
          RetVal = sl_WifiConfig();
          if(RetVal < 0)
          {
              UART_PRINT("Network Terminal - Couldn't configure Network Processor - %d\n",RetVal);
              return(NULL);
          }
          /* Turn NWP on */
          RetVal = sl_Start(NULL, NULL, NULL);
          if(RetVal < 0)
          {
              UART_PRINT("sl_start failed - %d\n",RetVal);
              return(NULL);
          }
          /* sl_Start returns on success the role that device started on */
          app_CB.Role = RetVal;
          /* disable the soft-roaming */
          cmdSoftRoamingDisablecallback(NULL);
          /* Unregister mDNS services */
          RetVal = sl_NetAppMDNSUnRegisterService(0, 0, 0);
          if(RetVal < 0)
          {
              UART_PRINT("sl_NetAppMDNSUnRegisterService failed - %d\n",RetVal);
              return(NULL);
          }
          RetVal = GetAppBanner();
          if(RetVal < 0)
          {
              UART_PRINT("Network Terminal - Unable to retrieve device information \n");
              return(NULL);
          }
    
          /*���� wifi �����߳�*/
          pthread_attr_init(&pAttrs);
          priParam.sched_priority = CONNECT_TASK_PRIORITY;
          pthread_attr_setschedparam(&pAttrs, &priParam);
          pthread_attr_setstacksize(&pAttrs, TASK_STACK_SIZE);
          pthread_attr_setdetachstate(&pAttrs,PTHREAD_CREATE_DETACHED);
          status = pthread_create(&connectThread,
                                  &pAttrs,
                                  ConnectProcess,
                                  NULL);
          if(status != 0)
              return(NULL);
    #if 0
         /*���� TCP���ݴ����߳�*/
         pthread_attr_init(&pAttrs);
         priParam.sched_priority = TCPRECEIVE_TASK_PRIORITY;
         pthread_attr_setschedparam(&pAttrs, &priParam);
         pthread_attr_setstacksize(&pAttrs, TASK_STACK_SIZE);
         status = pthread_create(&eventThread,
                                 &pAttrs,
                                 TcpProcess,
                                 NULL);
         if(status != 0)
             UART_PRINT("could not create task\n\r");
    #endif
    #if 0
         /*���� UART ���ݴ����߳�*/
         pthread_attr_init(&pAttrs);
         priParam.sched_priority = UART_TASK_PRIORITY;
         pthread_attr_setschedparam(&pAttrs, &priParam);
         pthread_attr_setstacksize(&pAttrs, TASK_STACK_SIZE);
         status = pthread_create(&eventThread,
                                 &pAttrs,
                                 UartProcess,
                                 NULL);
         if(status != 0)
             UART_PRINT("could not create task\n\r");
    #endif
         return(0);
    }
    
    
    #include <stdlib.h>
    
    /* TI-DRIVERS Header files */
    #include <ti/drivers/net/wifi/simplelink.h>
    #ifdef CC32XX
    #include <ti/drivers/Power.h>
    #include <ti/drivers/power/PowerCC32XX.h>
    #endif
    /* Example Header files */
    #include "cmd_parser.h"
    #include <luckcome_doppler.h>
    #include "wlan_cmd.h"
    #include "transceiver_cmd.h"
    #include "userInfo.h"
    
    /* Application defines */
    #define WLAN_EVENT_TOUT             (6000)
    #define MAX_SCAN_TRAILS             (10)
    #define P2P_CONNECT_PRIORITY        (SPAWN_TASK_PRIORITY - 1)
    #define P2P_STACK_SIZE              (2048)
    #define P2P_REMOTE_DEVICE           ("StartScan")
    #define P2P_DEVICE_TYPE             ("1-0050F204-1")
    #define P2P_DEVICE_NAME             ("cc32xx_p2p_device")
    #define LISTEN_CHANNEL              (11)
    #define LISTEN_REGULATORY_CLASS     (81)
    #define OPRA_CHANNEL                (6)
    #define OPRA_REGULATORY_CLASS       (81)
    #define TIMEOUT_SEM                 (-1)
    #define LPDS_WAKEUP_SW              (1)
    #define MGMT                        (0)
    #define CTRL                        (1)
    #define DATA                        (2)
    #define CC3x35_BIT                  (0x100000)
    
    #ifdef SECURE_SOCKET
    #define TCP_PROTOCOL_FLAGS    SL_SEC_SOCKET
    #define ROOT_CA_CERT_FILE     "dummy-root-ca-cert"
    #define PRIVATE_KEY_FILE      "dummy-trusted-cert-key"
    #define TRUSTED_CERT_FILE     "dummy-trusted-cert"
    #define TRUSTED_CERT_CHAIN    "trusted-chain.pem"
    
    #define DEVICE_YEAR                 (2017)
    #define DEVICE_MONTH                (4)
    #define DEVICE_DATE                 (5)
    
    #define BUF_LEN                (MAX_BUF_SIZE - 20)
    #else
    #define TCP_PROTOCOL_FLAGS      0
    #define BUF_LEN                (MAX_BUF_SIZE)
    #endif
    #define SERIALWIFI_NET_BUFFER_SIZE          (1024)
    
    typedef union
    {
        SlSockAddrIn6_t in6;       /* Socket info for Ipv6 */
        SlSockAddrIn_t in4;        /* Socket info for Ipv4 */
    }sockAddr_t;
    
    
    int32_t setStaticIPConfig(uint8_t* pIP,
                              uint8_t* pGw,
                              uint8_t* pDns);
    int32_t sendConnectionReq(void);
    int32_t setP2Pparams(void);
    void printScanResults(uint32_t res_num);
    void printExtScanResults(uint32_t res_num);
    void *p2pConnect(void *arg);
    
    
    int32_t sock = -1;
    extern  wifiStatus;
    uint8_t sendErrorcount = 0;
    unsigned char netReceiveBuffer[SERIALWIFI_NET_BUFFER_SIZE];
    
    int32_t SendTcp(uint8_t* pData,uint16_t len, uint8_t nb)
    {
        int32_t status = 0;
    
        if(2 == wifiStatus)
        {
            status = sl_Send(sock, pData, len, 0);
            if(status < 0)
            {
                UART_PRINT("[line:%d, error:%d] %s\n\r", __LINE__, status,SL_SOCKET_ERROR);
                UART_PRINT("SendTcp()->sendErrorcount: %d\n\r", sendErrorcount);
                sendErrorcount++;
                if(sendErrorcount > 0)//����5����������ʧ�ܵĻ���������WIFI����
                {
                    sendErrorcount = 0;
                    wifiStatus = 1;
                }
                return(-1);
            }else{
                sendErrorcount = 0;
            }
        }
        return 0;
    }
    
    int32_t ReceiveTcp(void)
    {
        int32_t bytesRx = 0;
        if(2 == wifiStatus)
        {
            bytesRx = sl_Recv(sock,
                              netReceiveBuffer,
                              SERIALWIFI_NET_BUFFER_SIZE,
                              0);
            GetTcpPackage(netReceiveBuffer,bytesRx);
        }
    }
    
    
    
    
    signed short shocketNum = 0;
    extern int32_t ipv4_lost;
    int32_t TCPClient(uint8_t nb,
                      uint16_t portNumber,
                      ip_t ipAddress,
                      uint8_t ipv6,
                      uint32_t numberOfPackets,
                      uint8_t tx)
    {
    
        int32_t status;
        uint32_t i = 0;
        int32_t nonBlocking;
        SlSockAddr_t        *sa;
        int32_t addrSize;
        sockAddr_t sAddr;
    
        /* clear the global data buffer */
        memset(app_CB.gDataBuffer.nwData, 0x0, MAX_BUF_SIZE);
    
        /* filling the buffer with data */
        for(i = 0; i < MAX_BUF_SIZE; i++)
        {
            app_CB.gDataBuffer.nwData[i] = (char)(i % 10);
        }
    
        if(ipv6)
        {
            sAddr.in6.sin6_family = SL_AF_INET6;
            sAddr.in6.sin6_port = sl_Htons(portNumber);
            sAddr.in6.sin6_flowinfo = 0;
    
            sAddr.in6.sin6_addr._S6_un._S6_u32[0] =
                ((unsigned long*)ipAddress.ipv6)[0];
            sAddr.in6.sin6_addr._S6_un._S6_u32[1] =
                ((unsigned long*)ipAddress.ipv6)[1];
            sAddr.in6.sin6_addr._S6_un._S6_u32[2] =
                ((unsigned long*)ipAddress.ipv6)[2];
            sAddr.in6.sin6_addr._S6_un._S6_u32[3] =
                ((unsigned long*)ipAddress.ipv6)[3];
            sa = (SlSockAddr_t*)&sAddr.in6;
            addrSize = sizeof(SlSockAddrIn6_t);
        }
        else
        {
            /* filling the TCP server socket address */
            sAddr.in4.sin_family = SL_AF_INET;
    
            /* Since this is the client's side,
             * we must know beforehand the IP address
             * and the port of the server wer'e trying to connect.
             */
            sAddr.in4.sin_port = sl_Htons((unsigned short)portNumber);
            sAddr.in4.sin_addr.s_addr = sl_Htonl((unsigned int)ipAddress.ipv4);
    
            sa = (SlSockAddr_t*)&sAddr.in4;
            addrSize = sizeof(SlSockAddrIn6_t);
        }
    
        if(sock >= 0){
            shocketNum = sl_Close(sock);
        }
        UART_PRINT("\t old sock:  %d",sock);
        sock = sl_Socket(sa->sa_family, SL_SOCK_STREAM, TCP_PROTOCOL_FLAGS);
        UART_PRINT("\t new sock:  %d",sock);
    
        ASSERT_ON_ERROR(sock, SL_SOCKET_ERROR);
    
        /* Set socket as non-blocking socket (if needed):
         * Non-blocking sockets allows user to handle
         * other tasks rather than block
         * on socket API calls.
         * If an API call using the Non-blocking socket descriptor
         * returns 'SL_ERROR_BSD_EAGAIN' -
         * this indicate that the user should try the API again later.
         */
        if(TRUE == nb)
        {
            nonBlocking = TRUE;
            status =
                sl_SetSockOpt(sock, SL_SOL_SOCKET, SL_SO_NONBLOCKING, &nonBlocking,
                              sizeof(nonBlocking));
            UART_PRINT("\t sl_SetSockOpt:  %d",status);
            if(status < 0)
            {
                UART_PRINT("[line:%d, error:%d] %s\n\r", __LINE__, status,
                           SL_SOCKET_ERROR);
                sl_Close(sock);
                return(-1);
            }
        }
        status = -1;
        while(status < 0)
        {
            /* Calling 'sl_Connect' followed by server's
             * 'sl_Accept' would start session with
             * the TCP server. */
            if(ipv4_lost == -1)
            {
                UART_PRINT("\t ipv4_lost:  %d",ipv4_lost);
                sleep(1);
                continue;
            }
            status = sl_Connect(sock, sa, addrSize);
            UART_PRINT("\t sl_Connect:  %d",status);
            if((status == SL_ERROR_BSD_EALREADY)&& (TRUE == nb))
            {
                sleep(1);
                continue;
            }
            else if(status < 0)
            {
                UART_PRINT("[line:%d, error:%d] %s\n\r", __LINE__, status,
                           SL_SOCKET_ERROR);
                sl_Close(sock);
                return(-1);
            }
            break;
        }
        UART_PRINT("\t sl_Connect ok");
        return(0);
    }
    
    
    
    int32_t WlanConnect(void)
    {
        int32_t ret = 0;
        ConnectCmd_t ConnectParams;
    
     /*add by edison*/
        sl_WlanDisconnect();
        app_CB.Status = 0 ;
     /*end add by edison**/
    
        memset(&ConnectParams, 0x0, sizeof(ConnectCmd_t));
    
        ConnectParams.ip = NULL;
        ConnectParams.dns = NULL;
        ConnectParams.gw = NULL;
        ConnectParams.ssid = wifiLocalPara.ssid;
        ConnectParams.secParams.Type = SL_WLAN_SEC_TYPE_WPA_WPA2;
        ConnectParams.secParams.Key = wifiLocalPara.passwd;
        ConnectParams.secParams.KeyLen = strlen(ConnectParams.secParams.Key);
    
        if(app_CB.Role != ROLE_STA)
        {
            ret = sl_WlanSetMode(ROLE_STA);
            ret = sl_Stop(SL_STOP_TIMEOUT);
            ret = sl_Start(0, 0, 0);
    
    
            /* Here we are in STA mode */
            app_CB.Role = ret;
        }
        cmdSetPolicyCallback();
        /* Connect to AP */
        ret = sl_WlanConnect((const signed char *)(ConnectParams.ssid),
                           strlen((const char *)(ConnectParams.ssid)),
                           0,
                           &ConnectParams.secParams,
                           0);
    
    
        /* Wait for connection events:
         * In order to verify that connection was successful,
         * we pend on two incoming events: Connected and Ip acquired.
         * The semaphores below are pend by this (Main) context.
         * They will be signaled once an asynchronous event
         * Indicating that the NWP has connected and acquired IP address is raised.
         * For further information, see this application read me file.
         */
        if(!IS_CONNECTED(app_CB.Status))
        {
            ret = sem_wait_timeout(&app_CB.CON_CB.connectEventSyncObj,
                                   WLAN_EVENT_TOUT);
            if(ret == TIMEOUT_SEM)
            {
                UART_PRINT("\n\r[wlanconnect] : Failed to connect to AP: %s\n\r",
                           ConnectParams.ssid);
                return(-1);
            }
        }
        if(!IS_IP_ACQUIRED(app_CB.Status))
        {
            ret = sem_wait_timeout(&app_CB.CON_CB.ip4acquireEventSyncObj,
                                   WLAN_EVENT_TOUT);
            if(ret == TIMEOUT_SEM)
            {
                /* In next step try to get IPv6,may be router/AP doesn't support IPv4 */
                UART_PRINT("\n\r[wlanconnect] : Failed to acquire IPv4 address.\n\r");
                return(-1);
            }
        }
    /*
        if(!IS_IPV6G_ACQUIRED(app_CB.Status))
        {
            ret = sem_wait_timeout(&app_CB.CON_CB.ip6acquireEventSyncObj,
                                   WLAN_EVENT_TOUT);
            if(ret == TIMEOUT_SEM)
            {
                UART_PRINT(
                    "\n\r[wlanconnect] : Failed to acquire IPv6 address.\n\r");
            }
        }
    
        if(!IS_IPV6G_ACQUIRED(app_CB.Status) &&
           !IS_IPV6L_ACQUIRED(app_CB.Status) && !IS_IP_ACQUIRED(app_CB.Status))
        {
            UART_PRINT("\n\r[line:%d, error:%d] %s\n\r", __LINE__, -1,
                       "Network Error");
        }
    */
        return(0);
    }
    
    int32_t TcpConnect(void)
    {
        int32_t ret = 0;
        SendCmd_t SendCmdParams;
    
        memset(&SendCmdParams, 0x0, sizeof(SendCmd_t));
        SendCmdParams.nb = 1,//1��ʾ������
        SendCmdParams.portNumber = wifiLocalPara.serverPort,
        SendCmdParams.ipAddr.ipv4 = (wifiLocalPara.serveripAddr[0]<<24)|
                                        (wifiLocalPara.serveripAddr[1]<<16)|
                                        (wifiLocalPara.serveripAddr[2]<<8)|
                                        (wifiLocalPara.serveripAddr[3]<<0);
        SendCmdParams.ipv6 = 0,
        SendCmdParams.numberOfPackets = 100;
    
        if(ret < 0)
        {
            return(-1);
        }
    
        ret = TCPClient(SendCmdParams.nb, SendCmdParams.portNumber,
                        SendCmdParams.ipAddr, SendCmdParams.ipv6,
                        SendCmdParams.numberOfPackets,
                        TRUE);
    
        return(ret);
    }
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    
    

  • Hi,

    I'm closing this thread because we are working on this issue on another thread over here.

    https://e2e.ti.com/support/wireless-connectivity/wifi/f/968/t/846830

    Best,

    Ben M