Example #1
0
/*******************************************************************************
  Function:
    void DRV_WIFI_ScanGetResult(uint8_t listIndex, t_wfScanResult  *p_scanResult)

  Summary:
    Read selected scan results back from MRF24W.

  Description:
    After a scan has completed this function is used to read one scan result at
    a time from the MRF24WG.

  Parameters:
    listIndex    - Index (0-based list) of the scan entry to retrieve.
    p_scanResult - Pointer to where scan result is written.  See DRV_WIFI_SCAN_RESULT
                   structure.

  Returns:
    None.
  *****************************************************************************/
void DRV_WIFI_ScanGetResult(uint8_t          listIndex, 
                      DRV_WIFI_SCAN_RESULT  *p_scanResult)
{
    uint8_t   hdr[4];
    
    hdr[0] = WF_MGMT_REQUEST_TYPE;
    hdr[1] = WF_SCAN_GET_RESULTS_SUBTYPE; 
    hdr[2] = listIndex;        /* scan result index to read from */
    hdr[3] = 1;                /* number of results to read      */
    
    SendMgmtMsg(hdr,             /* header           */
                sizeof(hdr),     /* size of header   */
                NULL,            /* no data          */
                0);              /* no data          */

    /* index 4 contains number of scan results returned, index 5 is first byte of first scan result */
    WaitForMgmtResponseAndReadData(WF_SCAN_GET_RESULTS_SUBTYPE,         /* expected subtype               */ 
                                   sizeof(DRV_WIFI_SCAN_RESULT),              /* num data bytes to read         */
                                   5,                                   /* starting at this index         */
                                  (uint8_t *)p_scanResult);             /* write the response data here   */
                                  

    /* fix up endianness on the two 16-bit values in the scan results */
    p_scanResult->beaconPeriod = TCPIP_Helper_ntohs(p_scanResult->beaconPeriod);
    p_scanResult->atimWindow   = TCPIP_Helper_ntohs(p_scanResult->atimWindow);
}
/*******************************************************************************
  Function:
    void DRV_WIFI_WPSCredentialsGet(DRV_WIFI_WPS_CREDENTIAL *p_cred);

  Summary:
    Gets the WPS credentials

  Description:
    This function gets the WPS credentials from the MRF24WG

  Parameters:
    p_cred -- pointer to where WPS credentials will be written.

  Returns:
    None.
  *****************************************************************************/
void DRV_WIFI_WPSCredentialsGet(DRV_WIFI_WPS_CREDENTIAL *p_cred)
{
    LowLevel_CPGetElement(WF_CP_ELEMENT_READ_WPS_CRED,   /* Element ID                        */
                          (uint8_t *)p_cred,             /* pointer to element data           */
                          sizeof(*p_cred),               /* number of element data bytes      */
                          true);                         /* read data, free buffer after read */

    // fix 16-bit endianness
    p_cred->encType  = TCPIP_Helper_ntohs(p_cred->encType);
    p_cred->authType = TCPIP_Helper_ntohs(p_cred->authType);
}   
/*****************************************************************************
 * FUNCTION: WFProcessMgmtIndicateMsg
 *
 * RETURNS:  error code
 *
 * PARAMS:   None
 *
 *  NOTES:   Processes a management indicate message
 *****************************************************************************/
void WFProcessMgmtIndicateMsg()
{
    tMgmtIndicateHdr  hdr;
    uint8_t buf[6];
    uint8_t event = 0xff;
    uint16_t eventInfo;

    // read mgmt indicate header (2 bytes)
    RawRead(RAW_SCRATCH_ID, MGMT_INDICATE_BASE, sizeof(tMgmtIndicateHdr), (uint8_t *)&hdr);

    /* if not a management indicate then fatal error */
    SYS_ASSERT((hdr.type == WF_MGMT_INDICATE_TYPE), "Invalid Indicate Header" );
        
    /* Determine which event occurred and handle it */
    switch (hdr.subType)
    {
        /*-----------------------------------------------------------------*/        
        case WF_EVENT_CONNECTION_ATTEMPT_STATUS_SUBTYPE:
        /*-----------------------------------------------------------------*/
            RawReadRelative(RAW_SCRATCH_ID, 2, buf); /* read first 2 bytes after header */
            /* if connection attempt successful */
            if (buf[0] == CONNECTION_ATTEMPT_SUCCESSFUL)
            {
                event     = DRV_WIFI_EVENT_CONNECTION_SUCCESSFUL;
                eventInfo = DRV_WIFI_NO_ADDITIONAL_INFO;
                SignalWiFiConnectionChanged(true);
                #if defined(TCPIP_STACK_USE_IPV6)
                    WF_Initialize_IPV6_Multicast_Filter();
                #endif
                #if defined (TCPIP_STACK_USE_DHCP_CLIENT)
                    RenewDhcp();
                #endif
                SetLogicalConnectionState(true);
            }
            /* else connection attempt failed */
            else
            {
                event     = DRV_WIFI_EVENT_CONNECTION_FAILED;
                eventInfo = (uint16_t)(buf[0] << 8 | buf[1]);   /* contains connection failure code */
                SetLogicalConnectionState(false);
            }
            break;
            
        /*-----------------------------------------------------------------*/
        case WF_EVENT_CONNECTION_LOST_SUBTYPE:
        /*-----------------------------------------------------------------*/ 
            /* read next two data bytes in message
               buf[0] -- 1: Connection temporarily lost  2: Connection permanently lost 3: Connection Reestablished 
               buf[1] -- 0: Beacon Timeout  1: Deauth from AP  */
            RawReadRelative(RAW_SCRATCH_ID, 2, buf);

            if (buf[0] == CONNECTION_TEMPORARILY_LOST)
            {
                event     = DRV_WIFI_EVENT_CONNECTION_TEMPORARILY_LOST;
                eventInfo = (uint16_t)buf[1];    /* lost due to beacon timeout or deauth */
                SignalWiFiConnectionChanged(false);
                
                SetLogicalConnectionState(false);
            }    
            else if (buf[0] == CONNECTION_PERMANENTLY_LOST)
            {
                event     = DRV_WIFI_EVENT_CONNECTION_PERMANENTLY_LOST;
                eventInfo = (uint16_t)buf[1];   /* lost due to beacon timeout or deauth */
                SetLogicalConnectionState(false);
                SignalWiFiConnectionChanged(false);                
            }
            else if (buf[0] == CONNECTION_REESTABLISHED)
            {
                event     = DRV_WIFI_EVENT_CONNECTION_REESTABLISHED;
                eventInfo = (uint16_t)buf[1];    /* originally lost due to beacon timeout or deauth */
                #if defined(TCPIP_STACK_USE_DHCP_CLIENT)
                RenewDhcp();
                #endif
                SignalWiFiConnectionChanged(true);  
                
                SetLogicalConnectionState(true);
            }    
            else
            {
                /* invalid parameter in message */
                SYS_ASSERT(false, "");
            }        
            break;
        
        /*-----------------------------------------------------------------*/                    
        case WF_EVENT_SCAN_RESULTS_READY_SUBTYPE:        
        /*-----------------------------------------------------------------*/
            RawReadRelative(RAW_SCRATCH_ID, 1, buf);
            event = DRV_WIFI_EVENT_SCAN_RESULTS_READY;
            eventInfo = (uint16_t)buf[0];          /* number of scan results */
            break;
            
        /*-----------------------------------------------------------------*/
        case WF_EVENT_SCAN_IE_RESULTS_READY_SUBTYPE:
        /*-----------------------------------------------------------------*/
            event = DRV_WIFI_EVENT_IE_RESULTS_READY;
            /* read indexes 2 and 3 containing the 16-bit value of IE bytes */
            RawReadRelative(RAW_SCRATCH_ID, 2, (uint8_t *)&eventInfo);
            eventInfo = TCPIP_Helper_ntohs(eventInfo);     /* fix endianess of 16-bit value */
            break;    
        
        /*-----------------------------------------------------------------*/
        case WF_EVENT_KEY_CALCULATION_REQUEST_SUBTYPE:
        /*-----------------------------------------------------------------*/
            event = DRV_WIFI_EVENT_KEY_CALCULATION_REQUEST;
            RawReadRelative(RAW_SCRATCH_ID, sizeof(tMgmtIndicatePassphraseReady), (uint8_t *)&passphraseReady);
            WifiAsyncSetEventPending(ASYNC_WPABUTTON_CONNECT);
            break;

        /*-----------------------------------------------------------------*/
        case WF_EVENT_SOFT_AP_EVENT_SUBTYPE:    /* Valid only with 3108 or the later module FW version */
        /*-----------------------------------------------------------------*/
            event = DRV_WIFI_EVENT_SOFT_AP;
            RawReadRelative(RAW_SCRATCH_ID, sizeof(DRV_WIFI_MGMT_INDICATE_SOFT_AP_EVENT), (uint8_t *)&g_softAPEvent);
            break;
            
        /*-----------------------------------------------------------------*/
        case WF_EVENT_DISCONNECT_DONE_SUBTYPE:
        /*-----------------------------------------------------------------*/
            event =  DRV_WIFI_EVENT_DISCONNECT_DONE;
            /* set state to no connection */
            SetLogicalConnectionState(false);
            break;
            
        /*-----------------------------------------------------------------*/
        default:
        /*-----------------------------------------------------------------*/
            SYS_ASSERT(false, "");
            break;        
    }

    /* if the application wants to be notified of the event */
    WF_UserEventsSet(event, eventInfo, 1);
}