A Port of TI's Webserver for the CC3000

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers evnt_handler.cpp Source File

evnt_handler.cpp

00001 /*****************************************************************************
00002 *
00003 *  evnt_handler.c  - CC3000 Host Driver Implementation.
00004 *  Copyright (C) 2011 Texas Instruments Incorporated - http://www.ti.com/
00005 *
00006 *  Redistribution and use in source and binary forms, with or without
00007 *  modification, are permitted provided that the following conditions
00008 *  are met:
00009 *
00010 *    Redistributions of source code must retain the above copyright
00011 *    notice, this list of conditions and the following disclaimer.
00012 *
00013 *    Redistributions in binary form must reproduce the above copyright
00014 *    notice, this list of conditions and the following disclaimer in the
00015 *    documentation and/or other materials provided with the   
00016 *    distribution.
00017 *
00018 *    Neither the name of Texas Instruments Incorporated nor the names of
00019 *    its contributors may be used to endorse or promote products derived
00020 *    from this software without specific prior written permission.
00021 *
00022 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 
00023 *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 
00024 *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00025 *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 
00026 *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
00027 *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 
00028 *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00029 *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00030 *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 
00031 *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
00032 *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00033 *
00034 *****************************************************************************/
00035 //*****************************************************************************
00036 //
00037 //! \addtogroup evnt_handler_api
00038 //! @{
00039 //
00040 //******************************************************************************
00041 
00042 //******************************************************************************
00043 //                  INCLUDE FILES
00044 //******************************************************************************
00045 
00046 #include "cc3000_common.h"
00047 #include "string.h"
00048 #include "hci.h"
00049 #include "evnt_handler.h"
00050 #include "wlan.h"
00051 #include "socket.h"
00052 #include "netapp.h"
00053 #include "spi.h"
00054 
00055  
00056 
00057 //*****************************************************************************
00058 //                  COMMON DEFINES
00059 //*****************************************************************************
00060 
00061 #define FLOW_CONTROL_EVENT_HANDLE_OFFSET        (0)
00062 #define FLOW_CONTROL_EVENT_BLOCK_MODE_OFFSET    (1)
00063 #define FLOW_CONTROL_EVENT_FREE_BUFFS_OFFSET    (2)
00064 #define FLOW_CONTROL_EVENT_SIZE                 (4)
00065 
00066 #define BSD_RSP_PARAMS_SOCKET_OFFSET        (0)
00067 #define BSD_RSP_PARAMS_STATUS_OFFSET        (4)
00068 
00069 #define GET_HOST_BY_NAME_RETVAL_OFFSET  (0)
00070 #define GET_HOST_BY_NAME_ADDR_OFFSET    (4)
00071 
00072 #define ACCEPT_SD_OFFSET            (0)
00073 #define ACCEPT_RETURN_STATUS_OFFSET (4)
00074 #define ACCEPT_ADDRESS__OFFSET      (8)
00075 
00076 #define SL_RECEIVE_SD_OFFSET            (0)
00077 #define SL_RECEIVE_NUM_BYTES_OFFSET     (4)
00078 #define SL_RECEIVE__FLAGS__OFFSET       (8)
00079 
00080 
00081 #define SELECT_STATUS_OFFSET            (0)
00082 #define SELECT_READFD_OFFSET            (4)
00083 #define SELECT_WRITEFD_OFFSET           (8)
00084 #define SELECT_EXFD_OFFSET              (12)
00085 
00086 
00087 #define NETAPP_IPCONFIG_IP_OFFSET               (0)
00088 #define NETAPP_IPCONFIG_SUBNET_OFFSET           (4)
00089 #define NETAPP_IPCONFIG_GW_OFFSET               (8)
00090 #define NETAPP_IPCONFIG_DHCP_OFFSET             (12)
00091 #define NETAPP_IPCONFIG_DNS_OFFSET              (16)
00092 #define NETAPP_IPCONFIG_MAC_OFFSET              (20)
00093 #define NETAPP_IPCONFIG_SSID_OFFSET             (26)
00094 
00095 #define NETAPP_IPCONFIG_IP_LENGTH               (4)
00096 #define NETAPP_IPCONFIG_MAC_LENGTH              (6)
00097 #define NETAPP_IPCONFIG_SSID_LENGTH             (32)
00098 
00099 
00100 #define NETAPP_PING_PACKETS_SENT_OFFSET         (0)
00101 #define NETAPP_PING_PACKETS_RCVD_OFFSET         (4)
00102 #define NETAPP_PING_MIN_RTT_OFFSET              (8)
00103 #define NETAPP_PING_MAX_RTT_OFFSET              (12)
00104 #define NETAPP_PING_AVG_RTT_OFFSET              (16)
00105 
00106 #define GET_SCAN_RESULTS_TABlE_COUNT_OFFSET             (0)
00107 #define GET_SCAN_RESULTS_SCANRESULT_STATUS_OFFSET       (4)
00108 #define GET_SCAN_RESULTS_ISVALID_TO_SSIDLEN_OFFSET      (8)
00109 #define GET_SCAN_RESULTS_FRAME_TIME_OFFSET              (10)
00110 #define GET_SCAN_RESULTS_SSID_MAC_LENGTH                (38)
00111 
00112 
00113 
00114 //*****************************************************************************
00115 //                  GLOBAL VARAIABLES
00116 //*****************************************************************************
00117 
00118 unsigned long socket_active_status = SOCKET_STATUS_INIT_VAL; 
00119 
00120 
00121 //*****************************************************************************
00122 //            Prototypes for the static functions
00123 //*****************************************************************************
00124 
00125 static long hci_event_unsol_flowcontrol_handler(char *pEvent);
00126 
00127 static void update_socket_active_status(char *resp_params);
00128 
00129 
00130 //*****************************************************************************
00131 //
00132 //!  hci_unsol_handle_patch_request
00133 //!
00134 //!  @param  event_hdr  event header
00135 //!
00136 //!  @return none
00137 //!
00138 //!  @brief   Handle unsolicited event from type patch request
00139 //
00140 //*****************************************************************************
00141 void hci_unsol_handle_patch_request(char *event_hdr)
00142 {
00143     char *params = (char *)(event_hdr) + HCI_EVENT_HEADER_SIZE;
00144     unsigned long ucLength = 0;
00145     char *patch;
00146     
00147     switch (*params)
00148     {
00149     case HCI_EVENT_PATCHES_DRV_REQ:
00150         
00151         if (tSLInformation.sDriverPatches)
00152         {
00153             patch = tSLInformation.sDriverPatches(&ucLength);
00154             
00155             if (patch)
00156             {
00157                 hci_patch_send(HCI_EVENT_PATCHES_DRV_REQ, 
00158                                              tSLInformation.pucTxCommandBuffer, patch, ucLength);
00159                 return;
00160             }
00161         }
00162         
00163         // Send 0 length Patches response event
00164         hci_patch_send(HCI_EVENT_PATCHES_DRV_REQ, 
00165                                      tSLInformation.pucTxCommandBuffer, 0, 0);
00166         break;
00167         
00168     case HCI_EVENT_PATCHES_FW_REQ:
00169         
00170         if (tSLInformation.sFWPatches)
00171         {
00172             patch = tSLInformation.sFWPatches(&ucLength);
00173             
00174             // Build and send a patch
00175             if (patch)
00176             {
00177                 hci_patch_send(HCI_EVENT_PATCHES_FW_REQ, 
00178                                              tSLInformation.pucTxCommandBuffer, patch, ucLength);
00179                 return;
00180             }
00181         }
00182         
00183         // Send 0 length Patches response event
00184         hci_patch_send(HCI_EVENT_PATCHES_FW_REQ, 
00185                                      tSLInformation.pucTxCommandBuffer, 0, 0);
00186         break;
00187         
00188     case HCI_EVENT_PATCHES_BOOTLOAD_REQ:
00189         
00190         if (tSLInformation.sBootLoaderPatches)
00191         {
00192             patch = tSLInformation.sBootLoaderPatches(&ucLength);
00193             
00194             if (patch)
00195             {
00196                 hci_patch_send(HCI_EVENT_PATCHES_BOOTLOAD_REQ,  
00197                                              tSLInformation.pucTxCommandBuffer, patch, ucLength);
00198                 return;
00199             }
00200         }
00201         
00202         // Send 0 length Patches response event
00203         hci_patch_send(HCI_EVENT_PATCHES_BOOTLOAD_REQ, 
00204                                      tSLInformation.pucTxCommandBuffer, 0, 0);
00205         break;
00206     }
00207 }
00208 
00209 
00210 
00211 //*****************************************************************************
00212 //
00213 //!  hci_event_handler
00214 //!
00215 //!  @param  pRetParams     incoming data buffer
00216 //!  @param  from           from information (in case of data received)
00217 //!  @param  fromlen        from information length (in case of data received)
00218 //!
00219 //!  @return         none
00220 //!
00221 //!  @brief          Parse the incoming events packets and issues corresponding
00222 //!                  event handler from global array of handlers pointers
00223 //
00224 //*****************************************************************************
00225 
00226     
00227 unsigned char *
00228 hci_event_handler(void *pRetParams, unsigned char *from, unsigned char *fromlen)
00229 {
00230     unsigned char *pucReceivedData, ucArgsize;
00231     unsigned short usLength;
00232     unsigned char *pucReceivedParams;
00233     unsigned short usReceivedEventOpcode = 0;
00234     unsigned long retValue32;
00235   unsigned char * RecvParams;
00236   unsigned char *RetParams;
00237     
00238     
00239     while (1)
00240     {
00241         //printf("Looping...\r\n");
00242         if (tSLInformation.usEventOrDataReceived != 0)
00243         {               
00244             pucReceivedData = (tSLInformation.pucReceivedData);
00245 
00246             if (*pucReceivedData == HCI_TYPE_EVNT)
00247             {
00248                 // Event Received
00249                 STREAM_TO_UINT16((char *)pucReceivedData, HCI_EVENT_OPCODE_OFFSET,
00250                                                  usReceivedEventOpcode);
00251                 pucReceivedParams = pucReceivedData + HCI_EVENT_HEADER_SIZE;        
00252                 RecvParams = pucReceivedParams;
00253                 RetParams = (unsigned char *)pRetParams;
00254                 
00255                 // In case unsolicited event received - here the handling finished
00256                 if (hci_unsol_event_handler((char *)pucReceivedData) == 0)
00257                 {
00258                     STREAM_TO_UINT8(pucReceivedData, HCI_DATA_LENGTH_OFFSET, usLength);
00259                     
00260                     switch(usReceivedEventOpcode)
00261                     {       
00262                     case HCI_CMND_READ_BUFFER_SIZE:
00263                         {
00264                             STREAM_TO_UINT8((char *)pucReceivedParams, 0, 
00265                                                             tSLInformation.usNumberOfFreeBuffers);
00266                             STREAM_TO_UINT16((char *)pucReceivedParams, 1, 
00267                                                              tSLInformation.usSlBufferLength);
00268                         }
00269                         break;
00270                         
00271                     case HCI_CMND_WLAN_CONFIGURE_PATCH:
00272                     case HCI_NETAPP_DHCP:
00273                     case HCI_NETAPP_PING_SEND:
00274                     case HCI_NETAPP_PING_STOP:
00275                     case HCI_NETAPP_ARP_FLUSH:
00276                     case HCI_NETAPP_SET_DEBUG_LEVEL:
00277                     case HCI_NETAPP_SET_TIMERS:
00278                     case HCI_EVNT_NVMEM_READ:
00279                     case HCI_EVNT_NVMEM_CREATE_ENTRY:
00280                     case HCI_CMND_NVMEM_WRITE_PATCH:
00281                     case HCI_NETAPP_PING_REPORT:
00282                     case HCI_EVNT_MDNS_ADVERTISE:
00283                         
00284                         STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET
00285                                                         ,*(unsigned char *)pRetParams);
00286                         break;
00287                         
00288                     case HCI_CMND_SETSOCKOPT:
00289                     case HCI_CMND_WLAN_CONNECT:
00290                     case HCI_CMND_WLAN_IOCTL_STATUSGET:
00291                     case HCI_EVNT_WLAN_IOCTL_ADD_PROFILE:
00292                     case HCI_CMND_WLAN_IOCTL_DEL_PROFILE:
00293                     case HCI_CMND_WLAN_IOCTL_SET_CONNECTION_POLICY:
00294                     case HCI_CMND_WLAN_IOCTL_SET_SCANPARAM:
00295                     case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_START:
00296                     case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_STOP:
00297                     case HCI_CMND_WLAN_IOCTL_SIMPLE_CONFIG_SET_PREFIX:
00298                     case HCI_CMND_EVENT_MASK:
00299                     case HCI_EVNT_WLAN_DISCONNECT:
00300                     case HCI_EVNT_SOCKET:
00301                     case HCI_EVNT_BIND:
00302                     case HCI_CMND_LISTEN:
00303                     case HCI_EVNT_CLOSE_SOCKET:
00304                     case HCI_EVNT_CONNECT:
00305                     case HCI_EVNT_NVMEM_WRITE:
00306                         
00307                         STREAM_TO_UINT32((char *)pucReceivedParams,0
00308                                                          ,*(unsigned long *)pRetParams);
00309                         break;
00310                         
00311                     case HCI_EVNT_READ_SP_VERSION:
00312                         
00313                         STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET
00314                                                         ,*(unsigned char *)pRetParams);
00315                         pRetParams = ((char *)pRetParams) + 1;
00316                         STREAM_TO_UINT32((char *)pucReceivedParams, 0, retValue32);
00317                         UINT32_TO_STREAM((unsigned char *)pRetParams, retValue32);              
00318                         break;
00319                         
00320                     case HCI_EVNT_BSD_GETHOSTBYNAME:
00321                         
00322                         STREAM_TO_UINT32((char *)pucReceivedParams
00323                               ,GET_HOST_BY_NAME_RETVAL_OFFSET,*(unsigned long *)pRetParams);
00324                         pRetParams = ((char *)pRetParams) + 4;
00325                         STREAM_TO_UINT32((char *)pucReceivedParams
00326                                     ,GET_HOST_BY_NAME_ADDR_OFFSET,*(unsigned long *)pRetParams);                    
00327                         break;
00328                         
00329                     case HCI_EVNT_ACCEPT:
00330                         {
00331                             STREAM_TO_UINT32((char *)pucReceivedParams,ACCEPT_SD_OFFSET
00332                                                              ,*(unsigned long *)pRetParams);
00333                             pRetParams = ((char *)pRetParams) + 4;
00334                             STREAM_TO_UINT32((char *)pucReceivedParams
00335                                         ,ACCEPT_RETURN_STATUS_OFFSET,*(unsigned long *)pRetParams);
00336               pRetParams = ((char *)pRetParams) + 4; 
00337                             
00338                             //This argument returns in network order
00339                             memcpy((unsigned char *)pRetParams, 
00340                                   pucReceivedParams + ACCEPT_ADDRESS__OFFSET, sizeof(sockaddr));    
00341                             break;
00342                         }
00343                         
00344                     case HCI_EVNT_RECV:
00345                     case HCI_EVNT_RECVFROM:
00346                         {
00347                             STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(unsigned long *)pRetParams);
00348                             pRetParams = ((char *)pRetParams) + 4;
00349                             STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(unsigned long *)pRetParams);
00350                             pRetParams = ((char *)pRetParams) + 4;
00351                             STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE__FLAGS__OFFSET,*(unsigned long *)pRetParams);
00352                             
00353                             if(((tBsdReadReturnParams *)pRetParams)->iNumberOfBytes == ERROR_SOCKET_INACTIVE)
00354                             {
00355                                 set_socket_active_status(((tBsdReadReturnParams *)pRetParams)->iSocketDescriptor,SOCKET_STATUS_INACTIVE);
00356                             }
00357                             break;
00358                         }
00359                                                 
00360                                         case HCI_EVNT_SEND:
00361                     case HCI_EVNT_SENDTO:
00362                         {
00363                             STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_SD_OFFSET ,*(unsigned long *)pRetParams);
00364                             pRetParams = ((char *)pRetParams) + 4;
00365                             STREAM_TO_UINT32((char *)pucReceivedParams,SL_RECEIVE_NUM_BYTES_OFFSET,*(unsigned long *)pRetParams);
00366                             pRetParams = ((char *)pRetParams) + 4;
00367                             
00368                             break;
00369                         }
00370                         
00371                     case HCI_EVNT_SELECT:
00372                         { 
00373                             STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_STATUS_OFFSET,*(unsigned long *)pRetParams);
00374                             pRetParams = ((char *)pRetParams) + 4;
00375                             STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_READFD_OFFSET,*(unsigned long *)pRetParams);
00376                             pRetParams = ((char *)pRetParams) + 4;
00377                             STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_WRITEFD_OFFSET,*(unsigned long *)pRetParams);
00378                             pRetParams = ((char *)pRetParams) + 4;
00379                             STREAM_TO_UINT32((char *)pucReceivedParams,SELECT_EXFD_OFFSET,*(unsigned long *)pRetParams);            
00380                             break;
00381                         }
00382                         
00383                     case HCI_CMND_GETSOCKOPT:
00384                         
00385                         STREAM_TO_UINT8(pucReceivedData, HCI_EVENT_STATUS_OFFSET,((tBsdGetSockOptReturnParams *)pRetParams)->iStatus);
00386                         //This argument returns in network order
00387                         memcpy((unsigned char *)pRetParams, pucReceivedParams, 4);
00388                         break;
00389                         
00390                     case HCI_CMND_WLAN_IOCTL_GET_SCAN_RESULTS:
00391                         
00392                         STREAM_TO_UINT32((char *)pucReceivedParams,GET_SCAN_RESULTS_TABlE_COUNT_OFFSET,*(unsigned long *)pRetParams);
00393                         pRetParams = ((char *)pRetParams) + 4;                      
00394                         STREAM_TO_UINT32((char *)pucReceivedParams,GET_SCAN_RESULTS_SCANRESULT_STATUS_OFFSET,*(unsigned long *)pRetParams);
00395                         pRetParams = ((char *)pRetParams) + 4;                                                                          
00396                         STREAM_TO_UINT16((char *)pucReceivedParams,GET_SCAN_RESULTS_ISVALID_TO_SSIDLEN_OFFSET,*(unsigned long *)pRetParams);
00397                         pRetParams = ((char *)pRetParams) + 2;                      
00398                         STREAM_TO_UINT16((char *)pucReceivedParams,GET_SCAN_RESULTS_FRAME_TIME_OFFSET,*(unsigned long *)pRetParams);
00399                         pRetParams = ((char *)pRetParams) + 2;  
00400                         memcpy((unsigned char *)pRetParams, (char *)(pucReceivedParams + GET_SCAN_RESULTS_FRAME_TIME_OFFSET + 2), GET_SCAN_RESULTS_SSID_MAC_LENGTH);    
00401                         break;
00402                         
00403                     case HCI_CMND_SIMPLE_LINK_START:
00404                         break;
00405                         
00406                     case HCI_NETAPP_IPCONFIG:
00407                         
00408                         //Read IP address
00409                         STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
00410                         RecvParams += 4;
00411                         
00412                         //Read subnet
00413                         STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
00414                         RecvParams += 4;
00415                         
00416                         //Read default GW
00417                         STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
00418                         RecvParams += 4;
00419                         
00420                         //Read DHCP server                                              
00421                         STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
00422                         RecvParams += 4;
00423                         
00424                         //Read DNS server                                           
00425                         STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_IP_LENGTH);
00426                         RecvParams += 4;
00427                         
00428                         //Read Mac address                              
00429                         STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_MAC_LENGTH);
00430                         RecvParams += 6;
00431                         
00432                         //Read SSID
00433                         STREAM_TO_STREAM(RecvParams,RetParams,NETAPP_IPCONFIG_SSID_LENGTH);
00434     
00435                     }
00436                 }
00437                 
00438                 if (usReceivedEventOpcode == tSLInformation.usRxEventOpcode)
00439                 {
00440                     tSLInformation.usRxEventOpcode = 0;
00441                 }
00442             }
00443             else
00444             {               
00445                 pucReceivedParams = pucReceivedData;
00446                 STREAM_TO_UINT8((char *)pucReceivedData, HCI_PACKET_ARGSIZE_OFFSET, ucArgsize);
00447                 
00448                 STREAM_TO_UINT16((char *)pucReceivedData, HCI_PACKET_LENGTH_OFFSET, usLength);
00449 
00450                 // Data received: note that the only case where from and from length 
00451                 // are not null is in recv from, so fill the args accordingly
00452                 if (from)
00453                 {
00454                     STREAM_TO_UINT32((char *)(pucReceivedData + HCI_DATA_HEADER_SIZE), BSD_RECV_FROM_FROMLEN_OFFSET, *(unsigned long *)fromlen);
00455                     memcpy(from, (pucReceivedData + HCI_DATA_HEADER_SIZE + BSD_RECV_FROM_FROM_OFFSET) ,*fromlen);
00456                 }
00457                 
00458                 memcpy(pRetParams, pucReceivedParams + HCI_DATA_HEADER_SIZE + ucArgsize,
00459                              usLength - ucArgsize);
00460                 
00461                 tSLInformation.usRxDataPending = 0;
00462             }
00463         
00464             tSLInformation.usEventOrDataReceived = 0;
00465             
00466             SpiResumeSpi();
00467             
00468             // Since we are going to TX - we need to handle this event after the 
00469             // ResumeSPi since we need interrupts
00470             if ((*pucReceivedData == HCI_TYPE_EVNT) &&
00471                     (usReceivedEventOpcode == HCI_EVNT_PATCHES_REQ))
00472             {
00473                 hci_unsol_handle_patch_request((char *)pucReceivedData);
00474             }
00475             
00476             if ((tSLInformation.usRxEventOpcode == 0) && (tSLInformation.usRxDataPending == 0))
00477             {
00478                 return NULL;
00479             }   
00480         }
00481     }
00482 
00483 }
00484 
00485 //*****************************************************************************
00486 //
00487 //!  hci_unsol_event_handler
00488 //!
00489 //!  @param  event_hdr   event header
00490 //!
00491 //!  @return             1 if event supported and handled
00492 //!                      0 if event is not supported
00493 //!
00494 //!  @brief              Handle unsolicited events
00495 //
00496 //*****************************************************************************
00497 long
00498 hci_unsol_event_handler(char *event_hdr)
00499 {
00500     char * data = NULL;
00501     long event_type;
00502     unsigned long NumberOfReleasedPackets;
00503     unsigned long NumberOfSentPackets;
00504     
00505     STREAM_TO_UINT16(event_hdr, HCI_EVENT_OPCODE_OFFSET,event_type);
00506     
00507     if (event_type & HCI_EVNT_UNSOL_BASE)
00508     {
00509         switch(event_type)
00510         {
00511     
00512         case HCI_EVNT_DATA_UNSOL_FREE_BUFF:
00513             {
00514                 hci_event_unsol_flowcontrol_handler(event_hdr);
00515                 
00516                 NumberOfReleasedPackets = tSLInformation.NumberOfReleasedPackets;
00517                 NumberOfSentPackets = tSLInformation.NumberOfSentPackets;
00518                                 
00519                 if (NumberOfReleasedPackets == NumberOfSentPackets)
00520                 {
00521                     if (tSLInformation.InformHostOnTxComplete)
00522                     {
00523                         tSLInformation.sWlanCB(HCI_EVENT_CC3000_CAN_SHUT_DOWN, NULL, 0);
00524                     }
00525                 }               
00526                 return 1;
00527                 
00528             }
00529         }
00530     }
00531     
00532     if(event_type & HCI_EVNT_WLAN_UNSOL_BASE)
00533     {           
00534         switch(event_type)
00535         {
00536         case HCI_EVNT_WLAN_KEEPALIVE:
00537         case HCI_EVNT_WLAN_UNSOL_CONNECT:
00538         case HCI_EVNT_WLAN_UNSOL_DISCONNECT:
00539         case HCI_EVNT_WLAN_UNSOL_INIT:
00540         case HCI_EVNT_WLAN_ASYNC_SIMPLE_CONFIG_DONE:
00541             
00542             if( tSLInformation.sWlanCB )
00543             {
00544                 tSLInformation.sWlanCB(event_type, 0, 0);
00545             }
00546             break;
00547             
00548         case HCI_EVNT_WLAN_UNSOL_DHCP:
00549             {
00550                 unsigned char   params[NETAPP_IPCONFIG_MAC_OFFSET + 1]; // extra byte is for the status
00551                 unsigned char *recParams = params;
00552                 
00553                 data = (char*)(event_hdr) + HCI_EVENT_HEADER_SIZE;
00554                 
00555                 //Read IP address
00556                 STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH);
00557                 data += 4;
00558                 //Read subnet
00559                 STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH);
00560                 data += 4;
00561                 //Read default GW
00562                 STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH); 
00563                 data += 4;
00564                 //Read DHCP server  
00565                 STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH);     
00566                 data += 4;
00567                 //Read DNS server  
00568                 STREAM_TO_STREAM(data,recParams,NETAPP_IPCONFIG_IP_LENGTH); 
00569                 // read the status
00570                 STREAM_TO_UINT8(event_hdr, HCI_EVENT_STATUS_OFFSET, *recParams);
00571 
00572 
00573                 if( tSLInformation.sWlanCB )
00574                 {
00575                     tSLInformation.sWlanCB(event_type, (char *)params, sizeof(params));
00576                 }
00577             }
00578             break;
00579             
00580         case HCI_EVNT_WLAN_ASYNC_PING_REPORT:
00581             {
00582                 netapp_pingreport_args_t params;            
00583                 data = (char*)(event_hdr) + HCI_EVENT_HEADER_SIZE;          
00584                 STREAM_TO_UINT32(data, NETAPP_PING_PACKETS_SENT_OFFSET, params.packets_sent);           
00585                 STREAM_TO_UINT32(data, NETAPP_PING_PACKETS_RCVD_OFFSET, params.packets_received);           
00586                 STREAM_TO_UINT32(data, NETAPP_PING_MIN_RTT_OFFSET, params.min_round_time);      
00587                 STREAM_TO_UINT32(data, NETAPP_PING_MAX_RTT_OFFSET, params.max_round_time);  
00588                 STREAM_TO_UINT32(data, NETAPP_PING_AVG_RTT_OFFSET, params.avg_round_time);
00589                 
00590                 if( tSLInformation.sWlanCB )
00591                 {
00592                     tSLInformation.sWlanCB(event_type, (char *)&params, sizeof(params));
00593                 }
00594             }
00595             break;
00596         case HCI_EVNT_BSD_TCP_CLOSE_WAIT:
00597             {
00598                 data = (char *)(event_hdr) + HCI_EVENT_HEADER_SIZE;
00599                 if( tSLInformation.sWlanCB )
00600                 {
00601                     tSLInformation.sWlanCB(event_type, data, 0);
00602                 }
00603             }
00604             break;
00605             
00606         //'default' case which means "event not supported"  
00607         default: 
00608             return (0);
00609         }
00610         return(1);
00611     }
00612     
00613     if ((event_type == HCI_EVNT_SEND) || (event_type == HCI_EVNT_SENDTO)
00614             || (event_type == HCI_EVNT_WRITE))
00615     {
00616                 char *pArg;
00617                 long status;
00618                 
00619                 pArg = M_BSD_RESP_PARAMS_OFFSET(event_hdr);
00620                 STREAM_TO_UINT32(pArg, BSD_RSP_PARAMS_STATUS_OFFSET,status);
00621                 
00622                 if (ERROR_SOCKET_INACTIVE == status)
00623                 {
00624                     // The only synchronous event that can come from SL device in form of 
00625                     // command complete is "Command Complete" on data sent, in case SL device 
00626                     // was unable to transmit
00627                     STREAM_TO_UINT8(event_hdr, HCI_EVENT_STATUS_OFFSET, tSLInformation.slTransmitDataError);
00628                     update_socket_active_status(M_BSD_RESP_PARAMS_OFFSET(event_hdr));
00629                     
00630                     return (1);
00631                 }
00632                 else
00633                     return (0);
00634     }
00635     
00636     return(0);
00637 }
00638 
00639 //*****************************************************************************
00640 //
00641 //!  hci_unsolicited_event_handler
00642 //!
00643 //!  @param None
00644 //!
00645 //!  @return         ESUCCESS if successful, EFAIL if an error occurred
00646 //!
00647 //!  @brief          Parse the incoming unsolicited event packets and issues 
00648 //!                  corresponding event handler.
00649 //
00650 //*****************************************************************************
00651 long
00652 hci_unsolicited_event_handler(void)
00653 {
00654     unsigned long   res = 0;
00655     unsigned char *pucReceivedData;
00656     
00657     if (tSLInformation.usEventOrDataReceived != 0)
00658     {
00659         pucReceivedData = (tSLInformation.pucReceivedData);
00660         
00661         if (*pucReceivedData == HCI_TYPE_EVNT)
00662         {           
00663             
00664             // In case unsolicited event received - here the handling finished
00665             if (hci_unsol_event_handler((char *)pucReceivedData) == 1)
00666             {
00667                 
00668                 // There was an unsolicited event received - we can release the buffer
00669                 // and clean the event received 
00670                 tSLInformation.usEventOrDataReceived = 0;
00671                 
00672                 res = 1;
00673                 SpiResumeSpi();
00674             }
00675         }
00676     }
00677     
00678     return res;
00679 }
00680 
00681 //*****************************************************************************
00682 //
00683 //!  set_socket_active_status
00684 //!
00685 //!  @param Sd
00686 //!  @param Status
00687 //!  @return         none
00688 //!
00689 //!  @brief          Check if the socket ID and status are valid and set 
00690 //!                  accordingly  the global socket status
00691 //
00692 //*****************************************************************************
00693 void set_socket_active_status(long Sd, long Status)
00694 {
00695     if(M_IS_VALID_SD(Sd) && M_IS_VALID_STATUS(Status))
00696     {
00697         socket_active_status &= ~(1 << Sd);      /* clean socket's mask */
00698         socket_active_status |= (Status << Sd); /* set new socket's mask */
00699     }
00700 }
00701 
00702 
00703 //*****************************************************************************
00704 //
00705 //!  hci_event_unsol_flowcontrol_handler
00706 //!
00707 //!  @param  pEvent  pointer to the string contains parameters for IPERF
00708 //!  @return         ESUCCESS if successful, EFAIL if an error occurred
00709 //!
00710 //!  @brief  Called in case unsolicited event from type
00711 //!          HCI_EVNT_DATA_UNSOL_FREE_BUFF has received.
00712 //!                Keep track on the number of packets transmitted and update the
00713 //!                  number of free buffer in the SL device.
00714 //
00715 //*****************************************************************************
00716 long
00717 hci_event_unsol_flowcontrol_handler(char *pEvent)
00718 {
00719     
00720     long temp, value;
00721     unsigned short i;
00722     unsigned short  pusNumberOfHandles=0;
00723     char *pReadPayload;
00724     
00725     STREAM_TO_UINT16((char *)pEvent,HCI_EVENT_HEADER_SIZE,pusNumberOfHandles);
00726     pReadPayload = ((char *)pEvent +
00727                                     HCI_EVENT_HEADER_SIZE + sizeof(pusNumberOfHandles));    
00728     temp = 0;
00729     
00730     for(i = 0; i < pusNumberOfHandles ; i++)
00731     {
00732         STREAM_TO_UINT16(pReadPayload, FLOW_CONTROL_EVENT_FREE_BUFFS_OFFSET, value);
00733         temp += value;
00734         pReadPayload += FLOW_CONTROL_EVENT_SIZE;  
00735     }
00736     
00737     tSLInformation.usNumberOfFreeBuffers += temp;
00738     tSLInformation.NumberOfReleasedPackets += temp;
00739     
00740     return(ESUCCESS);
00741 }
00742 
00743 //*****************************************************************************
00744 //
00745 //!  get_socket_active_status
00746 //!
00747 //!  @param  Sd  Socket IS
00748 //!  @return     Current status of the socket.   
00749 //!
00750 //!  @brief  Retrieve socket status
00751 //
00752 //*****************************************************************************
00753 
00754 long
00755 get_socket_active_status(long Sd)
00756 {
00757     if(M_IS_VALID_SD(Sd))
00758     {
00759         return (socket_active_status & (1 << Sd)) ? SOCKET_STATUS_INACTIVE : SOCKET_STATUS_ACTIVE;
00760     }
00761     return SOCKET_STATUS_INACTIVE;
00762 }
00763 
00764 //*****************************************************************************
00765 //
00766 //!  update_socket_active_status
00767 //!
00768 //!  @param  resp_params  Socket IS
00769 //!  @return     Current status of the socket.   
00770 //!
00771 //!  @brief  Retrieve socket status
00772 //
00773 //*****************************************************************************
00774 void
00775 update_socket_active_status(char *resp_params)
00776 {
00777     long status, sd;
00778     
00779     STREAM_TO_UINT32(resp_params, BSD_RSP_PARAMS_SOCKET_OFFSET,sd);
00780     STREAM_TO_UINT32(resp_params, BSD_RSP_PARAMS_STATUS_OFFSET,status);
00781     
00782     if(ERROR_SOCKET_INACTIVE == status)
00783     {
00784         set_socket_active_status(sd, SOCKET_STATUS_INACTIVE);
00785     }
00786 }
00787 
00788 
00789 //*****************************************************************************
00790 //
00791 //!  SimpleLinkWaitEvent
00792 //!
00793 //!  @param  usOpcode      command operation code
00794 //!  @param  pRetParams    command return parameters
00795 //!
00796 //!  @return               none
00797 //!
00798 //!  @brief                Wait for event, pass it to the hci_event_handler and
00799 //!                        update the event opcode in a global variable.
00800 //
00801 //*****************************************************************************
00802 
00803 void 
00804 SimpleLinkWaitEvent(unsigned short usOpcode, void *pRetParams)
00805 {
00806     // In the blocking implementation the control to caller will be returned only 
00807     // after the end of current transaction
00808     tSLInformation.usRxEventOpcode = usOpcode;
00809     hci_event_handler(pRetParams, 0, 0);
00810 }
00811 
00812 //*****************************************************************************
00813 //
00814 //!  SimpleLinkWaitData
00815 //!
00816 //!  @param  pBuf       data buffer
00817 //!  @param  from       from information
00818 //!  @param  fromlen    from information length
00819 //!
00820 //!  @return               none
00821 //!
00822 //!  @brief                Wait for data, pass it to the hci_event_handler
00823 //!                        and update in a global variable that there is 
00824 //!                        data to read.
00825 //
00826 //*****************************************************************************
00827 
00828 void 
00829 SimpleLinkWaitData(unsigned char *pBuf, unsigned char *from, 
00830                                      unsigned char *fromlen)
00831 {
00832     // In the blocking implementation the control to caller will be returned only 
00833     // after the end of current transaction, i.e. only after data will be received
00834     tSLInformation.usRxDataPending = 1;
00835     hci_event_handler(pBuf, from, fromlen);
00836 }
00837 
00838 //*****************************************************************************
00839 //
00840 // Close the Doxygen group.
00841 //! @}
00842 //
00843 //*****************************************************************************
00844