void WiFiTask(void)
{
    #if defined (WF_USE_POWER_SAVE_FUNCTIONS) 
    //--------------------------------------------------------------------------
    // if not waiting for a mgmt response and the application wants PS-Poll Mode
    //--------------------------------------------------------------------------
    if ((!g_WaitingForMgmtResponse) && (GetAppPowerSaveMode() == TRUE))
    {
        // else if changed from connected to disconnected, or vice-versa
        if (g_WiFiConnectionChanged == TRUE)
        {
            g_WiFiConnectionChanged = FALSE;

            // if lost connection
            if (g_WiFiConnection == FALSE)
            {
               WFConfigureLowPowerMode(WF_LOW_POWER_MODE_OFF);
            }
            // else connected (or reconnected)  
            else
            {
                // if not using DHCP
                if (AppConfig.Flags.bIsDHCPEnabled == FALSE)
                {
                    WF_PsPollEnable(g_rxDtim);
                    WFConfigureLowPowerMode(WF_LOW_POWER_MODE_ON);
                }    
                // note: if using DHCP, another case will reenable PS-Poll mode
            } 
        }            
        // else if app is using DHCP and we just got an IP address via DHCP
        else if ((AppConfig.Flags.bIsDHCPEnabled == TRUE) && (g_DhcpSuccessful == TRUE))
        {
           g_DhcpSuccessful = FALSE; 
           WF_PsPollEnable(g_rxDtim);
           WFConfigureLowPowerMode(WF_LOW_POWER_MODE_ON);
        }    
        // if application wants PS-Poll, but the driver disabled it to send a message (and not waiting for DHCP)
        else if ( g_WiFiConnection == TRUE && isSleepNeeded() && !isDhcpInProgress() )
        {
            ClearSleepNeeded();
            WFConfigureLowPowerMode(WF_LOW_POWER_MODE_ON);
        }  
    }    
    #endif /* WF_USE_POWER_SAVE_FUNCTIONS */

} 
Exemple #2
0
/*******************************************************************************
  Function:
	  BOOL iwconfigSetPower(void)

  Summary:
	 Enables or disables PS Poll mode.

  Description:
	 Enables or disables PS Poll mode.
	 reenable / all - Enables all power saving features (PS_POLL) of the MRF24W. MRF24W
	                        will wake up to check for all types of traffic (unicast, multicast, and broadcast)
        disable          - Disables any power savings features.
        unicast          - MRF24W will be in its deepest sleep state, only waking up at periodic intervals
                               to check for unicast data. MRF24W will not wake up on the DTIM period for
                               broadcast or multicast traffic.

  Parameters:
       reenable / disable / unicast /all

  Returns:
	TRUE or FALSE

  Remarks:
       WF_USE_POWER_SAVE_FUNCTIONS must be defined to use PS Poll mode.
 *****************************************************************************/
static BOOL iwconfigSetPower(void)
{
    if (ARGC < 3u)
    {
        WFConsolePrintRomStr("Missing value for last parameter", TRUE);
        return FALSE;
    }

    if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "reenable") == 0) )
    {   // reenable power saving
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
        WF_PsPollEnable(TRUE);
#endif
    }
    else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "disable") == 0) )
    {   // disable power saving
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
        WF_PsPollDisable();
#endif
    }
    else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "unicast") == 0) )
    {   // enable power saving but don't poll for DTIM
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
        WF_PsPollEnable(FALSE);
#endif
    }
    else if ( (3u <= ARGC) && (strcmppgm2ram((char*)ARGV[2], "all") == 0) )
    {   // enable power saving and poll for DTIM
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
        WF_PsPollEnable(TRUE);
#endif
    }
    else
    {
        WFConsolePrintRomStr("Unknown parameter", TRUE);
        return FALSE;
    }

    return TRUE;
}
Exemple #3
0
static BOOL iwconfigSetConnect(void)
{   // IWCONFIG CONNECT [ssid] [channel] [power-mode]   //[security-mode] [WEP-key/passphrase] [retry-attempt]
    UINT8 networkType;
    if(ARGC < 3u)
    {
        putsUART("Wrong command, correct command is:IWCONFIG CONNECT [ssid] [bssid] [channel] [power-mode]\r\n");
        return FALSE;
    }
    networkType = SetMode_idle();
    if(ARGC >= 3u) // ssid
    {
        WF_CPSetSsid(iwconfigCb.cpId, (UINT8 *)ARGV[2], strlen((char*)ARGV[2]));
    }
    if(ARGC >= 4u) //channel
    {
        int int_channel;
        sscanf((const char *)ARGV[3], (const char *)"%d",&int_channel);
        if((int_channel>=1)&&(int_channel<=14))
        {
            WF_CASetChannelList((UINT8 *)&int_channel, 1);
        }
        else
        {
            WFConsolePrintRomStr("channel err (1~14): Unknown parameter", TRUE);
            return FALSE;
        }
    }
    if(ARGC >= 5u) //channel
    {
        int int_channel;
        UINT8 channel;
        sscanf((const char *)ARGV[4], (const char *)"%d",&int_channel);
        if((int_channel>=1)&&(int_channel<=14))
        {
            channel = int_channel;
            //{char buf_t[20];sprintf(buf_t,"channel=%d\r\n",int_channel);putsUART(buf_t);}
            WF_CASetChannelList(&channel, 1);
            DelayMs(100);
        }
        else
        {
            WFConsolePrintRomStr("channel err (1~14): Unknown parameter", TRUE);
            return FALSE;
        }
    }
    if(ARGC >= 6u) // power-mode
    {
        if (strcmppgm2ram((char*)ARGV[5], "reenable") == 0)
        {   // reenable power saving
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
            WF_PsPollEnable(TRUE);
#endif
        }
        else if  (strcmppgm2ram((char*)ARGV[5], "disable") == 0)
        {   // disable power saving
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
            WF_PsPollDisable();
#endif
        }
        else if  (strcmppgm2ram((char*)ARGV[5], "unicast") == 0)
        {   // enable power saving but don't poll for DTIM
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
            WF_PsPollEnable(FALSE);
#endif
        }
        else if  (strcmppgm2ram((char*)ARGV[5], "all") == 0)
        {   // enable power saving and poll for DTIM
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
            WF_PsPollEnable(TRUE);
#endif
        }
        else
        {
            WFConsolePrintRomStr("Unknown parameter", TRUE);
            return FALSE;
        }
    }
    if(ARGC >= 7u) // [security-mode]
    {

    }
    SetMode_NotIdle(networkType);
    return TRUE;
}
/*****************************************************************************
 * FUNCTION: WF_ProcessEvent
 *
 * RETURNS:  None
 *
 * PARAMS:   event      -- event that occurred
 *           eventInfo  -- additional information about the event.  Not all events
 *                         have associated info, in which case this value will be
 *                         set to WF_NO_ADDITIONAL_INFO (0xff)
 *           extraInfo - more additional information about the event
 *
 *  NOTES:   The Host application must NOT directly call this function.  This
 *           function is called by the WiFi Driver code when an event occurs
 *           that may need processing by the Host CPU.
 *
 *           No other WiFi Driver function should be called from this function, with the
 *           exception of WF_ASSERT.  It is recommended that if the application wishes to be
 *           notified of an event that it simply set a flag and let application code in the
 *           main loop handle the event.
 *
 *           WFSetFuncState must be called when entering and leaving this function.
 *           When WF_DEBUG is enabled this allows a runtime check if any illegal WF functions
 *           are called from within this function.
 *
 *           For events that the application is not interested in simply leave the
 *           case statement empty.
  *
 *           Customize this function as needed for your application.
 *****************************************************************************/
void WF_ProcessEvent(uint8_t event, uint16_t eventInfo, uint8_t *extraInfo)
{
    #if defined(STACK_USE_UART)
    char buf[8];
    #endif
    tMgmtIndicateSoftAPEvent *softAPEvent;

    /* this function tells the WF driver that we are in this function */
    WFSetFuncState(WF_PROCESS_EVENT_FUNC, WF_ENTERING_FUNCTION);

    switch (event)
    {
        /*--------------------------------------*/
        case WF_EVENT_CONNECTION_SUCCESSFUL:
        /*--------------------------------------*/
            #if defined(STACK_USE_UART)
            putrsUART("Event: Connection Successful\r\n");

            #if defined(EZ_CONFIG_STORE)
                AppConfig.saveSecurityInfo = true;
            #endif

            #endif
            break;

        /*--------------------------------------*/
        case WF_EVENT_CONNECTION_FAILED:
        case WF_EVENT_CONNECTION_TEMPORARILY_LOST:
        case WF_EVENT_CONNECTION_PERMANENTLY_LOST:
        /*--------------------------------------*/
            #if defined(STACK_USE_UART)
            WF_OutputConnectionDebugMsg(event, eventInfo);
            #endif
            break;

        /*--------------------------------------*/
        case WF_EVENT_CONNECTION_REESTABLISHED:
        /*--------------------------------------*/
            #if defined(STACK_USE_UART)
            putrsUART("Event: Connection Reestablished\r\n");
            #endif
#if 0  //This code will cause assert, so I mask them  -- Jian Wan
            #if defined(WF_USE_POWER_SAVE_FUNCTIONS)
            {
                bool PsPollEnabled;

                PsPollEnabled = (MY_DEFAULT_PS_POLL == WF_ENABLED);
                if (!PsPollEnabled)
                {
                    /* disable low power (PS-Poll) mode */
                    #if defined(STACK_USE_UART)
                    putrsUART("Disable PS-Poll\r\n");
                    #endif
                    WF_PsPollDisable();
                }
                else
                {
                    /* Enable low power (PS-Poll) mode */
                    #if defined(STACK_USE_UART)
                    putrsUART("Enable PS-Poll\r\n");
                    #endif
                    WF_PsPollEnable(true);
                }
            }
            #endif
#endif
            break;

        /*--------------------------------------*/
        case WF_EVENT_SCAN_RESULTS_READY:
        /*--------------------------------------*/
            #if defined(STACK_USE_UART)
            putrsUART("Event: Scan Results Ready,");
            sprintf(buf, " %d", eventInfo);
            putsUART(buf);
            putrsUART("results\r\n");
            #endif /* STACK_USE_UART */

            #if defined ( EZ_CONFIG_SCAN ) && !defined(__XC8)
            WFScanEventHandler(eventInfo);
            #endif /* EZ_CONFIG_SCAN */

            #if MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP
            g_scan_done = 1;    // WF_PRESCAN
            #endif

            #if defined(WF_PRE_SCAN_IN_ADHOC)
            g_prescan_adhoc_done = 1;
            #endif
            break;

        case WF_EVENT_SOFT_AP_EVENT:
            softAPEvent = (tMgmtIndicateSoftAPEvent *)extraInfo;
            #if defined(STACK_USE_UART)
            {
                char str[96];
                char *result = "None";
                char *reason = "None";
                uint8_t *addr = softAPEvent->address;
                putrsUART("Event: SoftAP, ");
                if (softAPEvent->event == SOFTAP_EVENT_CONNECTED) {
                    result = "Connected";
                } else if (softAPEvent->event == SOFTAP_EVENT_DISCONNECTED) {
                    result = "Disconnected";
                    if (softAPEvent->reason == SOFTAP_EVENT_LINK_LOST)
                        reason = "LinkLost";
                    else if (softAPEvent->reason == SOFTAP_EVENT_RECEIVED_DEAUTH)
                        reason = "ReceivedDeauth";
                }
                sprintf(str, "%s, %s, %x:%x:%x:%x:%x:%x", result, reason, addr[0], addr[1], addr[2],
                    addr[3], addr[4], addr[5]);
                putsUART(str);
                putrsUART("\r\n");
            }
            #endif /* STACK_USE_UART */
            break;

        default:
            WF_ASSERT(false);  /* unknown event */
            break;
    }

    /* Informs the WF driver that we are leaving this function */
    WFSetFuncState(WF_PROCESS_EVENT_FUNC, WF_LEAVING_FUNCTION);
}
/*****************************************************************************
 * FUNCTION: void WF_Connect(void)
 *
 * RETURNS:  None
 *
 * PARAMS:   none
 *
 * NOTES:    This routine creates a Connection Profile Entry, Initializes it
 *                then creates the connection algorithm and establishes the WiFi
 *                connection according to the Profile settings.
 *                Customize this function as needed for your application.
 *
 *                Wifi Direct, ensure following parameters are set up properly
 *                  - AppConfig.MySSID =  "DIRECT-"
 *                  - sizeof(channelList) == 3)
 *                  - channelList[0] == 1
 *                  - channelList[1] == 6
 *                  - channelList[2] == 11
 *
 *****************************************************************************/
void WF_Connect(void)
{
    uint8_t ConnectionProfileID;
    uint8_t channelList[] = MY_DEFAULT_CHANNEL_LIST;
    uint8_t channelList_postscan[] = MY_DEFAULT_CHANNEL_LIST_POSTSCAN;

    /* create a Connection Profile */
    WF_CPCreate(&ConnectionProfileID);

    AppConfig.passPhraseToKeyFlag = 0;

    // Enables or disables the MRF24W Regional Domain with RF module FW version 0x3106 or earlier
    // With RF module FW version 0x3107 and future releases, this function is NOT supported due to changes
    // in FCC requirements, which does not allow programming of the regional domain.
    WF_SetRegionalDomain(MY_DEFAULT_DOMAIN);

    if ((AppConfig.networkType == WF_SOFT_AP) || (AppConfig.networkType == WF_ADHOC)) {
        /**********************************************************************/
        /* Append Last 4 digits to MAC address to SSID - Creating unique SSID */
        /* Wifi comm demo SSID : MCHP_xxxx                                    */
        /* Wifi G demo SSID : MCHP_G_xxxx                                     */
        /**********************************************************************/
        sprintf((char *) AppConfig.MySSID, "MCHP_G_%02x%02x", AppConfig.MyMACAddr.v[4], AppConfig.MyMACAddr.v[5]);
        sprintf((char *) AppConfig.NetBIOSName, "%s%02x%02x", MY_DEFAULT_HOST_NAME, AppConfig.MyMACAddr.v[4], AppConfig.MyMACAddr.v[5]);

        size_t i = 0;
        // in the following while loop, all lower case letters have been changed to upper case ones
        // because browser automatically translates lower case letters to upper case
        while (i < sizeof (AppConfig.NetBIOSName)) {
            if (((*((char *) AppConfig.NetBIOSName + i)) >= 'a') & ((*((char *) AppConfig.NetBIOSName + i)) <= 'z')) {
                *((char *) AppConfig.NetBIOSName + i) = *((char *) AppConfig.NetBIOSName + i) - 32;
            } else if (*((char *) AppConfig.NetBIOSName + i) == '\0')
                break;
            i++;
        }
        FormatNetBIOSName(AppConfig.NetBIOSName);
    }

    AppConfig.SsidLength = strlen((char *) (AppConfig.MySSID));

    WF_CPSetSsid(ConnectionProfileID, AppConfig.MySSID, AppConfig.SsidLength);

    WF_CPSetNetworkType(ConnectionProfileID, AppConfig.networkType);
    if (AppConfig.networkType == WF_ADHOC) {
        WF_CPSetAdHocBehavior(ConnectionProfileID, WF_ADHOC_CONNECT_THEN_START);
    }

#if !defined(MRF24WG)
    Delay10us(10); /* required for MRF24WB */
#endif

#if WF_HOST_DERIVE_KEY_FROM_PASSPHRASE == WF_ENABLED
    if (AppConfig.SecurityMode == WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE) {
        WF_ConvPassphraseToKey(AppConfig.SecurityKeyLength, AppConfig.SecurityKey,
                AppConfig.SsidLength, AppConfig.MySSID);
        AppConfig.SecurityMode--;
        AppConfig.SecurityKeyLength = 32;
        AppConfig.passPhraseToKeyFlag = 1;
    }
#endif /* #if WF_HOST_DERIVE_KEY_FROM_PASSPHRASE == WF_ENABLED */

    switch (AppConfig.SecurityMode) {
    case WF_SECURITY_OPEN:
        WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_OPEN, 0, NULL, 0);
        break;
    case WF_SECURITY_WEP_40:
        // assume key 0
        WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 5);
        break;
    case WF_SECURITY_WEP_104:
        // assume key 0
        WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 13);
        break;
    case WF_SECURITY_WPA_AUTO_WITH_KEY:
        WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_WPA_AUTO_WITH_KEY,
                0, AppConfig.SecurityKey, 32);
        break;
    default:
        WF_ASSERT(false);
        break;
    }

    WF_CASetScanType(MY_DEFAULT_SCAN_TYPE);

    if (((CFGCXT.type != WF_SOFT_AP)&&(CFGCXT.prevWLAN == WF_SOFT_AP)) || (AppConfig.networkType != WF_SOFT_AP)) {
        WF_CASetChannelList(channelList_postscan, sizeof (channelList_postscan));
    } else {
        WF_CASetChannelList(channelList, sizeof (channelList));
    }

    // The Retry Count parameter tells the WiFi Connection manager how many attempts to make when trying
    // to connect to an existing network.  In the Infrastructure case, the default is to retry forever so that
    // if the AP is turned off or out of range, the radio will continue to attempt a connection until the
    // AP is eventually back on or in range.  In the Adhoc case, the default is to retry 3 times since the
    // purpose of attempting to establish a network in the Adhoc case is only to verify that one does not
    // initially exist.  If the retry count was set to WF_RETRY_FOREVER in the AdHoc mode, an AdHoc network
    // would never be established.  The constants MY_DEFAULT_LIST_RETRY_COUNT_ADHOC and
    // MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE have been created specifically for the June 2011 MAL release.
    if ((AppConfig.networkType == CFG_WF_ADHOC) || (AppConfig.networkType == CFG_WF_SOFT_AP)) {
        WF_CASetListRetryCount(ADHOC_RETRY_COUNT);
    } else /* AppConfig.networkType == CFG_WF_INFRASTRUCTURE */ {
        WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE);
    }

    WF_CASetEventNotificationAction(MY_DEFAULT_EVENT_NOTIFICATION_LIST);

#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
#if (MY_DEFAULT_PS_POLL == WF_ENABLED)
    WF_PsPollEnable(true);
#else
    WF_PsPollDisable();
#endif
#endif

    WF_CASetBeaconTimeout(MY_DEFAULT_BEACON_TIMEOUT);

#if defined(STACK_USE_UART)
    WF_OutputConnectionInfo(&AppConfig);
#endif

    if (AppConfig.networkType == CFG_WF_SOFT_AP) {
#if (WF_SOFTAP_SEND_KEEP_ALIVE == WF_ENABLED)
        WF_SetLinkDownThreshold(WF_SOFTAP_LINK_LOST_THRESHOLD);
#endif
    } else { // AppConfig.networkType != CFG_WF_SOFT_AP
#if (WF_CHECK_LINK_STATUS == WF_ENABLED)
        WF_SetLinkDownThreshold(WF_LINK_LOST_THRESHOLD);
#endif
    }

    // Initiates connection to BSS
    WF_CMConnect(ConnectionProfileID);
}
Exemple #6
0
/*****************************************************************************
 * FUNCTION: WF_Connect
 *
 * RETURNS:  None
 *
 * PARAMS:   None
 *
 *  NOTES:   Connects to an 802.11 network.  Customize this function as needed 
 *           for your application.
 *****************************************************************************/
static void WF_Connect(void)
{
    UINT8 ConnectionProfileID;
    UINT8 channelList[] = MY_DEFAULT_CHANNEL_LIST;
    #if defined(WF_USE_POWER_SAVE_FUNCTIONS)
    BOOL  PsPollEnabled;
    #endif
    
    /* create a Connection Profile */
    WF_CPCreate(&ConnectionProfileID);

    #if defined(STACK_USE_UART)
    putrsUART("Set SSID (");
    putsUART(AppConfig.MySSID);
    putrsUART(")\r\n");
    #endif
    WF_CPSetSsid(ConnectionProfileID, 
                 AppConfig.MySSID, 
                 AppConfig.SsidLength);

    #if defined(STACK_USE_UART)
    putrsUART("Set Network Type\r\n");
	#endif
    WF_CPSetNetworkType(ConnectionProfileID, AppConfig.networkType);

    if (AppConfig.networkType == WF_ADHOC)
    {
        WF_CPSetAdHocBehavior(ConnectionProfileID, WF_ADHOC_CONNECT_THEN_START);
    }
    #if defined(STACK_USE_UART)
    putrsUART("Set Security\r\n");
    #endif
    switch(AppConfig.SecurityMode) {
        case WF_SECURITY_OPEN:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_OPEN, 0, NULL, 0);
            break;
        case WF_SECURITY_WEP_40:
            // assume key 0
            WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 5);
            break;
        case WF_SECURITY_WEP_104:
            // assume key 0
            WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 13);
            break;
        case WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE, 
                             0, AppConfig.SecurityKey, strlen((char*)AppConfig.SecurityKey));
            break;
        case WF_SECURITY_WPA_AUTO_WITH_KEY:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_WPA_AUTO_WITH_KEY,
                             0, AppConfig.SecurityKey, 32);
            break;
        default:
        {
	    } 
        	//#if defined(STACK_USE_UART)
		//	putrsUART("\r\n\r\nCaptain this should NOT happen.\r\n\r\n");
			//#endif

    }
        
	#if defined(STACK_USE_UART)
	putrsUART("Set Scan Type\r\n");
	#endif
    WF_CASetScanType(MY_DEFAULT_SCAN_TYPE);
    
    #if defined(STACK_USE_UART)
    putrsUART("Set Channel List\r\n");
    #endif    
    WF_CASetChannelList(channelList, sizeof(channelList));
    
    #if defined(STACK_USE_UART)
    putrsUART("Set list retry count\r\n");
    #endif
    WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT);

    #if defined(STACK_USE_UART)        
    putrsUART("Set Event Notify\r\n");    
    #endif
    WF_CASetEventNotificationAction(MY_DEFAULT_EVENT_NOTIFICATION_LIST);
    
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
    PsPollEnabled = (MY_DEFAULT_PS_POLL == WF_ENABLED);
    if (!PsPollEnabled)
    {    
        /* disable low power (PS-Poll) mode */
        #if defined(STACK_USE_UART)
        putrsUART("Disable PS-Poll\r\n");        
        #endif
        WF_PsPollDisable();
    }    
    else
    {
        /* Enable low power (PS-Poll) mode */
        #if defined(STACK_USE_UART)
        putrsUART("Enable PS-Poll\r\n");        
        #endif
        WF_PsPollEnable(TRUE);
    }    
#endif

    #if defined(STACK_USE_UART)
    putrsUART("Set Beacon Timeout\r\n");
    #endif
    WF_CASetBeaconTimeout(40);


    #if defined(STACK_USE_UART)                     
    putrsUART("Start WiFi Connect\r\n");        
    #endif
    WF_CMConnect(ConnectionProfileID);
}   
Exemple #7
0
/*****************************************************************************
 * FUNCTION: WF_Connect
 *
 * RETURNS:  None
 *
 * PARAMS:   None
 *
 *  NOTES:   Connects to an 802.11 network.  Customize this function as needed 
 *           for your application.
 *****************************************************************************/
static void WF_Connect(void)
{
    UINT8 ConnectionProfileID;
    UINT8 channelList[] = MY_DEFAULT_CHANNEL_LIST;
    #if defined(WF_USE_POWER_SAVE_FUNCTIONS)
    BOOL  PsPollEnabled;
    #endif
    
    /* create a Connection Profile */
    WF_CPCreate(&ConnectionProfileID);

    #if defined(STACK_USE_UART)
    putrsUART("Set SSID (");
    putsUART(AppConfig.MySSID);
    putrsUART(")\r\n");
    #endif
    WF_CPSetSsid(ConnectionProfileID, 
                 AppConfig.MySSID, 
                 AppConfig.SsidLength);

    #if defined(STACK_USE_UART)
    putrsUART("Set Network Type\r\n");
	#endif
    WF_CPSetNetworkType(ConnectionProfileID, MY_DEFAULT_NETWORK_TYPE);
    
	#if defined(STACK_USE_UART)
	putrsUART("Set Scan Type\r\n");
	#endif
    WF_CASetScanType(MY_DEFAULT_SCAN_TYPE);
    
    #if defined(STACK_USE_UART)
    putrsUART("Set Channel List\r\n");
    #endif    
    WF_CASetChannelList(channelList, sizeof(channelList));
    
    #if defined(STACK_USE_UART)
    putrsUART("Set list retry count\r\n");
    #endif
    // The Retry Count parameter tells the WiFi Connection manager how many attempts to make when trying
    // to connect to an existing network.  In the Infrastructure case, the default is to retry forever so that
    // if the AP is turned off or out of range, the radio will continue to attempt a connection until the
    // AP is eventually back on or in range.  In the Adhoc case, the default is to retry 3 times since the 
    // purpose of attempting to establish a network in the Adhoc case is only to verify that one does not
    // initially exist.  If the retry count was set to WF_RETRY_FOREVER in the AdHoc mode, an AdHoc network
    // would never be established.  The constants MY_DEFAULT_LIST_RETRY_COUNT_ADHOC and 
    // MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE have been created specifically for the June 2011 MAL release.
    #if defined(EZ_CONFIG_STORE)
        if (AppConfig.networkType == WF_ADHOC)
            WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_ADHOC);
        else
            WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE);
    #else
        #if (MY_DEFAULT_NETWORK_TYPE == WF_ADHOC)
            WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_ADHOC);
        #else
            WF_CASetListRetryCount(MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE);
        #endif
    #endif

    #if defined(STACK_USE_UART)        
    putrsUART("Set Event Notify\r\n");    
    #endif
    WF_CASetEventNotificationAction(MY_DEFAULT_EVENT_NOTIFICATION_LIST);
    
#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
    PsPollEnabled = (MY_DEFAULT_PS_POLL == WF_ENABLED);
    if (!PsPollEnabled)
    {    
        /* disable low power (PS-Poll) mode */
        #if defined(STACK_USE_UART)
        putrsUART("Disable PS-Poll\r\n");        
        #endif
        WF_PsPollDisable();
    }    
    else
    {
        /* Enable low power (PS-Poll) mode */
        #if defined(STACK_USE_UART)
        putrsUART("Enable PS-Poll\r\n");        
        #endif
        WF_PsPollEnable(TRUE);
    }    
#endif

    #if defined(STACK_USE_UART)
    putrsUART("Set Beacon Timeout\r\n");
    #endif
    WF_CASetBeaconTimeout(40);
    
    /* Set Security */
    #if (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_OPEN)
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (Open)\r\n");
        #endif
    #elif (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WEP_40)
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WEP40)\r\n");
        #endif
    #elif (MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WEP_104)
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WEP104)\r\n");
        #endif
    #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_WITH_KEY 
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WPA with key)\r\n");
        #endif
    #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA2_WITH_KEY 
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WPA2 with key)\r\n");
        #endif
    #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_WITH_PASS_PHRASE
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WPA with pass phrase)\r\n");
        #endif
    #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA2_WITH_PASS_PHRASE
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WPA2 with pass phrase)\r\n");    
        #endif
    #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_AUTO_WITH_KEY
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WPA with key, auto-select)\r\n");
        #endif
    #elif MY_DEFAULT_WIFI_SECURITY_MODE == WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE
        #if defined(STACK_USE_UART)
        putrsUART("Set Security (WPA with pass phrase, auto-select)\r\n");
        #endif
    #endif /* MY_DEFAULT_WIFI_SECURITY_MODE */

    WF_CPSetSecurity(ConnectionProfileID,
                     AppConfig.SecurityMode,
                     AppConfig.WepKeyIndex,   /* only used if WEP enabled */
                     AppConfig.SecurityKey,
                     AppConfig.SecurityKeyLength);
    #if defined(STACK_USE_UART)                     
    putrsUART("Start WiFi Connect\r\n");        
    #endif
    WF_CMConnect(ConnectionProfileID);
}   
Exemple #8
0
/*****************************************************************************
 * FUNCTION: WF_Connect
 *
 * RETURNS:  None
 *
 * PARAMS:   None
 *
 *  NOTES:   Connects to an 802.11 network.  Customize this function as needed 
 *           for your application.
 *****************************************************************************/
void WF_Connect(void)
{
    //UINT8 ConnectionProfileID;
    UINT8 channelList[] = MY_DEFAULT_CHANNEL_LIST;
    #if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)
    UINT8 channelList_postscan[] = MY_DEFAULT_CHANNEL_LIST_POSTSCAN;
    #endif

    /* create a Connection Profile */
    WF_CPCreate(&ConnectionProfileID);
    
    WF_SetRegionalDomain(MY_DEFAULT_DOMAIN);  

    WF_CPSetSsid(ConnectionProfileID, 
                 AppConfig.MySSID, 
                 AppConfig.SsidLength);
     
    WF_CPSetNetworkType(ConnectionProfileID, AppConfig.networkType);
    if (AppConfig.networkType == WF_ADHOC)
    {
        WF_CPSetAdHocBehavior(ConnectionProfileID, WF_ADHOC_CONNECT_THEN_START);
    }
    
    #if !defined(MRF24WG)	
     //   Delay10us(10);  //If necessary, give time to Roadrunner to clean message buffer, because Security message is a big data package
    #endif
    
    switch(AppConfig.SecurityMode) {
        case WF_SECURITY_OPEN:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_OPEN, 0, NULL, 0);
            break;
        case WF_SECURITY_WEP_40:
            // assume key 0
            WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 5);
            break;
        case WF_SECURITY_WEP_104:
            // assume key 0
            WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 13);
            break;
        case WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE, 
                             0, AppConfig.SecurityKey, strlen((char*)AppConfig.SecurityKey));
            break;
        case WF_SECURITY_WPA_AUTO_WITH_KEY:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_WPA_AUTO_WITH_KEY,
                             0, AppConfig.SecurityKey, 32);
            break;
        default:
            putrsUART("\r\n\r\nCaptain this should NOT happen.\r\n\r\n");

    }

    WF_CASetScanType(MY_DEFAULT_SCAN_TYPE);
    
    #if (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)
    if (((CFGCXT.type!=WF_SOFT_AP)&&(CFGCXT.prevWLAN==WF_SOFT_AP)) || (AppConfig.networkType!=WF_SOFT_AP))
    {
        // putrsUART("\r\n\r\nWF_Connect: Channel list update when transitioning from SoftAP to non-SoftAP or NOT in SoftAP..\r\n\r\n");
        WF_CASetChannelList(channelList_postscan, sizeof(channelList_postscan));
    }
    else
    {
    WF_CASetChannelList(channelList, sizeof(channelList));
    }
    #else
    WF_CASetChannelList(channelList, sizeof(channelList));
    #endif // (MY_DEFAULT_NETWORK_TYPE == WF_SOFT_AP)
    
    // The Retry Count parameter tells the WiFi Connection manager how many attempts to make when trying
    // to connect to an existing network.  In the Infrastructure case, the default is to retry forever so that
    // if the AP is turned off or out of range, the radio will continue to attempt a connection until the
    // AP is eventually back on or in range.  In the Adhoc case, the default is to retry 3 times since the 
    // purpose of attempting to establish a network in the Adhoc case is only to verify that one does not
    // initially exist.  If the retry count was set to WF_RETRY_FOREVER in the AdHoc mode, an AdHoc network
    // would never be established.  The constants MY_DEFAULT_LIST_RETRY_COUNT_ADHOC and 
    // MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE have been created specifically for the June 2011 MAL release.

    WF_CASetListRetryCount(ADHOC_RETRY_COUNT);

    WF_CASetEventNotificationAction(MY_DEFAULT_EVENT_NOTIFICATION_LIST);
    
    #if defined(WF_USE_POWER_SAVE_FUNCTIONS)
        #if (MY_DEFAULT_PS_POLL == WF_ENABLED)
            WF_PsPollEnable(TRUE);
        #else
            WF_PsPollDisable();    
        #endif
    #endif

    WF_CASetBeaconTimeout(MY_DEFAULT_BEACON_TIMEOUT);

    #if defined(STACK_USE_UART)   
        WF_OutputConnectionInfo(&AppConfig);
    #endif

    #if (SOFTAP_CHECK_LINK_STATUS == WF_ENABLED)
        WF_SetLinkDownThreshold(SOFTAP_LINK_FAILURE_THRESHOLD);
    #endif

    WF_CMConnect(ConnectionProfileID);
}   
Exemple #9
0
/*****************************************************************************
 * FUNCTION: WF_Connect
 *
 * RETURNS:  None
 *
 * PARAMS:   None
 *
 *  NOTES:   Connects to an 802.11 network.  Customize this function as needed 
 *           for your application.
 *****************************************************************************/
void WF_Connect(void)
{
    //UINT8 ConnectionProfileID;
    UINT8 channelList[] = MY_DEFAULT_CHANNEL_LIST;
	 
    /* create a Connection Profile */
    WF_CPCreate(&ConnectionProfileID);
    
    WF_SetRegionalDomain(MY_DEFAULT_DOMAIN);  

    WF_CPSetSsid(ConnectionProfileID, 
                 AppConfig.MySSID, 
                 AppConfig.SsidLength);
    
    #if defined(WF_USE_HIDDEN_SSID)
   	    WF_CPSetSsidType(ConnectionProfileID, FALSE);
    #endif
 
    WF_CPSetNetworkType(ConnectionProfileID, AppConfig.networkType);
    if (AppConfig.networkType == WF_ADHOC)
    {
        WF_CPSetAdHocBehavior(ConnectionProfileID, WF_ADHOC_CONNECT_THEN_START);
    }
	
    switch(AppConfig.SecurityMode) {
        case WF_SECURITY_OPEN:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_OPEN, 0, NULL, 0);
            break;
        case WF_SECURITY_WEP_40:
            // assume key 0
            WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 5);
            break;
        case WF_SECURITY_WEP_104:
            // assume key 0
            WF_CPSetSecurity(ConnectionProfileID, AppConfig.SecurityMode, 0, AppConfig.SecurityKey, 13);
            break;
        case WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_WPA_AUTO_WITH_PASS_PHRASE, 
                             0, AppConfig.SecurityKey, strlen((char*)AppConfig.SecurityKey));
            break;
        case WF_SECURITY_WPA_AUTO_WITH_KEY:
            WF_CPSetSecurity(ConnectionProfileID, WF_SECURITY_WPA_AUTO_WITH_KEY,
                             0, AppConfig.SecurityKey, 32);
            break;
        default:
			putrsUART("\r\n\r\nCaptain this should NOT happen.\r\n\r\n");

    }

        
    WF_CASetScanType(MY_DEFAULT_SCAN_TYPE);
    
    WF_CASetChannelList(channelList, sizeof(channelList));
    
    // The Retry Count parameter tells the WiFi Connection manager how many attempts to make when trying
    // to connect to an existing network.  In the Infrastructure case, the default is to retry forever so that
    // if the AP is turned off or out of range, the radio will continue to attempt a connection until the
    // AP is eventually back on or in range.  In the Adhoc case, the default is to retry 3 times since the 
    // purpose of attempting to establish a network in the Adhoc case is only to verify that one does not
    // initially exist.  If the retry count was set to WF_RETRY_FOREVER in the AdHoc mode, an AdHoc network
    // would never be established.  The constants MY_DEFAULT_LIST_RETRY_COUNT_ADHOC and 
    // MY_DEFAULT_LIST_RETRY_COUNT_INFRASTRUCTURE have been created specifically for the June 2011 MAL release.

    WF_CASetListRetryCount(ADHOC_RETRY_COUNT);
	
    WF_CASetEventNotificationAction(MY_DEFAULT_EVENT_NOTIFICATION_LIST);
    
    #if defined(WF_USE_POWER_SAVE_FUNCTIONS)
        #if (MY_DEFAULT_PS_POLL == WF_ENABLED)
            WF_PsPollEnable(TRUE);
        #else
            WF_PsPollDisable();    
        #endif
    #endif

    WF_CASetBeaconTimeout(MY_DEFAULT_BEACON_TIMEOUT);
	
    #if defined(STACK_USE_UART)   
        WF_OutputConnectionInfo(&AppConfig);
    #endif

    WF_CMConnect(ConnectionProfileID);
}   
int main(void)
#endif
{
	static DWORD t = 0;
	static DWORD dwLastIP = 0;
	#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
	BOOL  PsPollEnabled;
	BOOL  psConfDone = FALSE;
	#endif

	// Initialize application specific hardware
	InitializeBoard();

	#if defined(USE_LCD)
	// Initialize and display the stack version on the LCD
	LCDInit();
	DelayMs(100);
	strcpypgm2ram((char*)LCDText, "TCPStack " TCPIP_STACK_VERSION "  "
		"                ");
	LCDUpdate();
	#endif

	// Initialize stack-related hardware components that may be 
	// required by the UART configuration routines
    TickInit();
	#if defined(STACK_USE_MPFS2)
	MPFSInit();
	#endif

	// Initialize Stack and application related NV variables into AppConfig.
	InitAppConfig();

    // Initiates board setup process if button is depressed 
	// on startup
    if(BUTTON0_IO == 0u)
    {
		#if defined(EEPROM_CS_TRIS) || defined(SPIFLASH_CS_TRIS)
		// Invalidate the EEPROM contents if BUTTON0 is held down for more than 4 seconds
		DWORD StartTime = TickGet();
		LED_PUT(0x00);
				
		while(BUTTON0_IO == 0u)
		{
			if(TickGet() - StartTime > 4*TICK_SECOND)
			{
				#if defined(EEPROM_CS_TRIS)
			    XEEBeginWrite(0x0000);
			    XEEWrite(0xFF);
			    XEEWrite(0xFF);
			    XEEEndWrite();
			    #elif defined(SPIFLASH_CS_TRIS)
			    SPIFlashBeginWrite(0x0000);
			    SPIFlashWrite(0xFF);
			    SPIFlashWrite(0xFF);
			    #endif
			    
				#if defined(STACK_USE_UART)
				putrsUART("\r\n\r\nBUTTON0 held for more than 4 seconds.  Default settings restored.\r\n\r\n");
				#endif

				LED_PUT(0x0F);
				while((LONG)(TickGet() - StartTime) <= (LONG)(9*TICK_SECOND/2));
				LED_PUT(0x00);
				while(BUTTON0_IO == 0u);
				Reset();
				break;
			}
		}
		#endif

		#if defined(STACK_USE_UART)
        DoUARTConfig();
		#endif
    }

	// Initialize core stack layers (MAC, ARP, TCP, UDP) and
	// application modules (HTTP, SNMP, etc.)
    StackInit();

    #if defined(WF_CS_TRIS)
    WF_Connect();
    #endif

	// Initialize any application-specific modules or functions/
	// For this demo application, this only includes the
	// UART 2 TCP Bridge
	#if defined(STACK_USE_UART2TCP_BRIDGE)
	UART2TCPBridgeInit();
	#endif

	#if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
    ZeroconfLLInitialize();
	#endif

	#if defined(STACK_USE_ZEROCONF_MDNS_SD)
	mDNSInitialize(MY_DEFAULT_HOST_NAME);
	mDNSServiceRegister(
		(const char *) "DemoWebServer",	// base name of the service
		"_http._tcp.local",			    // type of the service
		80,				                // TCP or UDP port, at which this service is available
		((const BYTE *)"path=/index.htm"),	// TXT info
		1,								    // auto rename the service when if needed
		NULL,							    // no callback function
		NULL							    // no application context
		);

    mDNSMulticastFilterRegister();			
	#endif

	// Now that all items are initialized, begin the co-operative
	// multitasking loop.  This infinite loop will continuously 
	// execute all stack-related tasks, as well as your own
	// application's functions.  Custom functions should be added
	// at the end of this loop.
    // Note that this is a "co-operative mult-tasking" mechanism
    // where every task performs its tasks (whether all in one shot
    // or part of it) and returns so that other tasks can do their
    // job.
    // If a task needs very long time to do its job, it must be broken
    // down into smaller pieces so that other tasks can have CPU time.
    while(1)
    {

//while (1)
/*{
if(BUTTON0_IO == 0u && LED0_IO == 0)
  {
   LED0_IO	=1;
  }
if(BUTTON0_IO == 0u && LED0_IO ==1)
    {
  LED0_IO	=0;
    }
}*/
	#if defined(WF_USE_POWER_SAVE_FUNCTIONS)
		if (!psConfDone && WFisConnected()) {	
			PsPollEnabled = (MY_DEFAULT_PS_POLL == WF_ENABLED);
			if (!PsPollEnabled) {	 
				/* disable low power (PS-Poll) mode */
				#if defined(STACK_USE_UART)
				putrsUART("Disable PS-Poll\r\n");		 
				#endif
				WF_PsPollDisable();
			} else {
				/* Enable low power (PS-Poll) mode */
				#if defined(STACK_USE_UART)
				putrsUART("Enable PS-Poll\r\n");		
				#endif
				WF_PsPollEnable(TRUE);
			}	
			psConfDone = TRUE;
		}
	#endif
        // Blink LED0 (right most one) every second.
//        if(TickGet() - t >= TICK_SECOND/2ul)
//        {
//            t = TickGet();
//            LED0_IO ^= 1;
//        }

        // This task performs normal stack task including checking
        // for incoming packet, type of packet and calling
        // appropriate stack entity to process it.
        StackTask();

        // This tasks invokes each of the core stack application tasks
        StackApplications();

        #if defined(STACK_USE_ZEROCONF_LINK_LOCAL)
		ZeroconfLLProcess();
        #endif

        #if defined(STACK_USE_ZEROCONF_MDNS_SD)
        mDNSProcess();
		// Use this function to exercise service update function
		// HTTPUpdateRecord();
        #endif

		// Process application specific tasks here.
		// For this demo app, this will include the Generic TCP 
		// client and servers, and the SNMP, Ping, and SNMP Trap
		// demos.  Following that, we will process any IO from
		// the inputs on the board itself.
		// Any custom modules or processing you need to do should
		// go here.
		#if defined(STACK_USE_GENERIC_TCP_CLIENT_EXAMPLE)
		GenericTCPClient();
		#endif
		
		#if defined(STACK_USE_GENERIC_TCP_SERVER_EXAMPLE)
		GenericTCPServer();
		#endif
		
		#if defined(STACK_USE_SMTP_CLIENT)
		SMTPDemo();
		#endif
		
		#if defined(STACK_USE_ICMP_CLIENT)
		PingDemo();
		#endif
		
		#if defined(STACK_USE_SNMP_SERVER) && !defined(SNMP_TRAP_DISABLED)
		//User should use one of the following SNMP demo
		// This routine demonstrates V1 or V2 trap formats with one variable binding.
		SNMPTrapDemo();
		
		#if defined(SNMP_STACK_USE_V2_TRAP) || defined(SNMP_V1_V2_TRAP_WITH_SNMPV3)
		//This routine provides V2 format notifications with multiple (3) variable bindings
		//User should modify this routine to send v2 trap format notifications with the required varbinds.
		//SNMPV2TrapDemo();
		#endif 
		if(gSendTrapFlag)
			SNMPSendTrap();
		#endif
		
		#if defined(STACK_USE_BERKELEY_API)
		BerkeleyTCPClientDemo();
		BerkeleyTCPServerDemo();
		BerkeleyUDPClientDemo();
		#endif

		ProcessIO();

        // If the local IP address has changed (ex: due to DHCP lease change)
        // write the new IP address to the LCD display, UART, and Announce 
        // service
		if(dwLastIP != AppConfig.MyIPAddr.Val)
		{
			dwLastIP = AppConfig.MyIPAddr.Val;
			
			#if defined(STACK_USE_UART)
				putrsUART((ROM char*)"\r\nNew IP Address: ");
			#endif

			DisplayIPValue(AppConfig.MyIPAddr);

			#if defined(STACK_USE_UART)
				putrsUART((ROM char*)"\r\n");
			#endif


			#if defined(STACK_USE_ANNOUNCE)
				AnnounceIP();
			#endif

            #if defined(STACK_USE_ZEROCONF_MDNS_SD)
				mDNSFillHostRecord();
			#endif
		}
	}
}