Ejemplo n.º 1
0
static bool IwconfigSetSsid(SYS_CMD_DEVICE_NODE *pCmdIO, int argc, char **argv)
{
    if (argc < 3)
    {
        SYS_CONSOLE_MESSAGE("Missing value for last parameter\r\n");
        return false;
    }

    if (argc > 3)
    {
        SYS_CONSOLE_MESSAGE("SSID cannot contain space character in this demo\r\n");
        return false;
    }

    if (strlen((const char *)argv[2]) > DRV_WIFI_MAX_SSID_LENGTH)
    {
        SYS_CONSOLE_PRINT("SSID may not contain more than %u characters\r\n", DRV_WIFI_MAX_SSID_LENGTH);
        return false;
    }

    DRV_WIFI_SsidSet((uint8_t *)argv[2], strlen((const char *)argv[2]));

    return true;
}
void AppConfig_NetworkInitialise(void)
{
    bool configDefault = false;
    bool logSettingsDefault = false;
    bool deviceServerConfig = false;

    //Unfortunately defaults to connected
    DRV_WIFI_MGMT_INDICATE_SOFT_AP_EVENT *event = DRV_WIFI_SoftApEventInfoGet();
    if (event)
    {
        event->event = -1;
    }

    ConfigStore_Initialize();
    // TODO - remove debug
//   	CreatorConsole_Printf("DeviceConfig size = %d, LogConfig size = %d\r\n", sizeof(ConfigStruct), sizeof(LoggingSettingsStruct));
//   	CreatorConsole_Printf("LOGGINGSETTINGS_PAGEOFFSET = %d\r\n", LOGGINGSETTINGS_PAGEOFFSET);
//   	CreatorConsole_Printf("DRV_NVM_PAGE_SIZE  = %d\r\n", DRV_NVM_PAGE_SIZE);

    if (!ConfigStore_Config_Read())
        CreatorConsole_Puts("ERROR: Could not read config_store memory.\r\n");

    if (!ConfigStore_Config_IsValid())
    {
        if (!ConfigStore_Config_IsMagicValid())
            CreatorConsole_Puts("\r\nWriting default device configuration for first-time use...");
        else
            CreatorConsole_Puts("\r\nDevice configuration invalid. Re-writing default configuration...");

        configDefault = true;
        if (!ConfigStore_Config_ResetToDefaults())
            CreatorConsole_Puts("ERROR: Could not reset config to defaults\r\n");

        if (!ConfigStore_Config_Write())
            CreatorConsole_Puts("ERROR: Could not write default config\r\n");

        CreatorConsole_Puts(" Done\r\n");
    }

    if (!ConfigStore_LoggingSettings_Read())
        CreatorConsole_Puts("ERROR: Could not read loggingSettings.\r\n");

    if (!ConfigStore_LoggingSettings_IsValid())
    {
        if (!ConfigStore_LoggingSettings_IsMagicValid())
            CreatorConsole_Puts("\r\nWriting default device logging-settings for first-time use...");
        else
            CreatorConsole_Puts("\r\nDevice logging-settings invalid. Re-writing defaults...");

        logSettingsDefault = true;
        if (!ConfigStore_LoggingSettings_ResetToDefaults())
            CreatorConsole_Puts("ERROR: Could not reset config to defaults\r\n");

        if (!ConfigStore_LoggingSettings_Write())
            CreatorConsole_Puts("ERROR: Could not write default config\r\n");

        CreatorConsole_Puts(" Done\r\n");
    }

    CreatorLogLevel level = ConfigStore_GetLoggingLevel();
    CreatorLog_SetLevel(level);
    Client_SetLogLevel(level);

    if (!ConfigStore_DeviceServerConfig_Read())
        CreatorConsole_Puts("ERROR: Could not read device server settings.\r\n");

    if (!ConfigStore_DeviceServerConfig_IsValid())
    {
        if (!ConfigStore_DeviceServerConfig_IsMagicValid())
            CreatorConsole_Puts("\r\nWriting default device server settings for first-time use...");
        else
            CreatorConsole_Puts("\r\nDevice server settings invalid. Re-writing defaults...");

        deviceServerConfig = true;
        if (!ConfigStore_DeviceServerConfig_ResetToDefaults())
            CreatorConsole_Puts("ERROR: Could not reset device server config to defaults\r\n");

        if (!ConfigStore_DeviceServerConfig_Write())
            CreatorConsole_Puts("ERROR: Could not write default device server config\r\n");

        CreatorConsole_Puts(" Done\r\n");
    }

    CreatorTimer_SetTicksPerSecond(1000);

    // Check date/time has valid minimum value
    // TODO - could use NTP to get time (in app mode), or mobile app could set time (in config mode)
    time_t time = Creator_GetTime(NULL);
    if (time < DATETIME_MINIMUM_VALUE)
    {
        Creator_SetTime(DATETIME_MINIMUM_VALUE);
    }
    else
    {
        Creator_SetTime(time);	// Kick start the date/time support
    }
    
    // Add initial activity log entries (Note: must be after date/time and logging settings initialised)
    if (configDefault && logSettingsDefault && deviceServerConfig)
    {
        Creator_Log(CreatorLogLevel_Warning, "Default configuration settings set for first-time use");
    }
    else
    {
        if (configDefault)
            Creator_Log(CreatorLogLevel_Error, "Creator configuration lost, default values set");
        if (logSettingsDefault)
            Creator_Log(CreatorLogLevel_Error, "Logging settings lost, default values set");
        if (deviceServerConfig)
            Creator_Log(CreatorLogLevel_Error, "Device server configuration lost, default values set");
    }

    if ((BSP_SwitchStateGet(BSP_SWITCH_1) == BSP_SWITCH_STATE_PRESSED) && (BSP_SwitchStateGet(BSP_SWITCH_2) == BSP_SWITCH_STATE_PRESSED))
        _RunningInConfigurationMode = true;

    // Only run in application mode if device has valid configuration
    if (!_RunningInConfigurationMode && (!ConfigStore_Config_IsValid() || ConfigStore_StartInConfigMode() || !AppConfig_CheckValidAppConfig(false)))
        _RunningInConfigurationMode = true;

    int numberOfNetworkInterfaces = TCPIP_STACK_NumberOfNetworksGet();
    int index;
    TCPIP_NET_HANDLE networkHandle = 0;
    do
    {
        CreatorThread_SleepMilliseconds(NULL, 500);
        for (index = 0; index < numberOfNetworkInterfaces; index++)
        {
            networkHandle = TCPIP_STACK_IndexToNet(index);
            if (TCPIP_STACK_NetIsUp(networkHandle))
            {
                break;
            }
        }
    } while (index == numberOfNetworkInterfaces);

    CreatorConsole_Puts("Done\r\n");

    TCPIP_DHCP_Disable(networkHandle);
    TCPIP_DNS_Disable(networkHandle, true);
    TCPIP_DHCPS_Disable(networkHandle);
    TCPIP_DNSS_Disable(networkHandle);

    if (!ConfigStore_SoftAPSSIDValid())
    {
        const unsigned int MAC_ADDRESS_UNIQUE_PORTION_LENGTH = 6;
        char *softAPSSID = (char *) ConfigStore_GetSoftAPSSID();
        unsigned int baseStrLen = strlen(WF_DEFAULT_SSID_NAME_PREFIX) * sizeof(char);
        memset(softAPSSID, 0, CONFIG_STORE_DEFAULT_FIELD_LENGTH);
        memcpy(softAPSSID, WF_DEFAULT_SSID_NAME_PREFIX, baseStrLen);
        char macStr[MAC_ADDRESS_LENGTH];
        memset(macStr, 0, sizeof(macStr));
        uint8_t mac[MAC_ADDRESS_LENGTH / 2];
        DRV_WIFI_MacAddressGet(mac);
        unsigned int i = 0;
        for (i = 0; i < MAC_ADDRESS_LENGTH / 2; i++)
            sprintf(macStr + (i * 2 * sizeof(char)), "%02X", mac[i]);
        // Store device MAC Address so it doesn't need to be retrieved more than once
        ConfigStore_SetMacAddress(macStr);
        // Use unique portion of mac address to build SSID (last three bytes)
        strncpy((char*) softAPSSID + baseStrLen, macStr + MAC_ADDRESS_LENGTH - MAC_ADDRESS_UNIQUE_PORTION_LENGTH, MAC_ADDRESS_UNIQUE_PORTION_LENGTH);

        // Set the device's name to the same as the SoftAP SSID if it is still the blank value
        if (strcmp(ConfigStore_GetDeviceName(), CREATOR_BLANK_DEVICE_NAME) == 0)
            ConfigStore_SetDeviceName(softAPSSID);

        // Update the Device's configuration information
        ConfigStore_Config_UpdateCheckbyte();
        if (!ConfigStore_Config_Write())
            CreatorConsole_Puts("ERROR: Could not write default config");
    }

    uint8_t networkType;
    uint8_t securityType = 0;
    uint8_t connectionState;
    DRV_WIFI_NetworkTypeGet(&networkType);

    if (networkType == DRV_WIFI_NETWORK_TYPE_ADHOC)
    {
        do
        {
            CreatorThread_SleepMilliseconds(NULL, 100);
            DRV_WIFI_ConnectionStateGet(&connectionState);
        } while ((connectionState == DRV_WIFI_CSTATE_CONNECTION_IN_PROGRESS) || (connectionState == DRV_WIFI_CSTATE_RECONNECTION_IN_PROGRESS));
    }
    else if (networkType == DRV_WIFI_NETWORK_TYPE_SOFT_AP)
    {
        do
        {
            CreatorThread_SleepMilliseconds(NULL, 100);
            DRV_WIFI_ConnectionStateGet(&connectionState);
        } while ((connectionState != DRV_WIFI_CSTATE_CONNECTION_IN_PROGRESS) && (connectionState == DRV_WIFI_CSTATE_CONNECTION_PERMANENTLY_LOST));
    }

    DRV_WIFI_ReconnectModeSet(0, DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT, 40, DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT);

    DRV_WIFI_Disconnect();

    do
    {
        CreatorThread_SleepMilliseconds(NULL, 100);
        DRV_WIFI_ConnectionStateGet(&connectionState);
    } while (connectionState != DRV_WIFI_CSTATE_NOT_CONNECTED);

    if (_AppInfo)
    {
        CreatorConsole_Puts("\r\n --- ");
        CreatorConsole_Puts(_AppInfo->ApplicationName);
        CreatorConsole_Puts(" v");
        CreatorConsole_Puts(_AppInfo->ApplicationVersion);
        CreatorConsole_Puts(" ---\r\n");
    }
    const char *networkSSID;
    const char *softAPPPassword = NULL;
    if (_RunningInConfigurationMode)
    {
        networkType = DRV_WIFI_NETWORK_TYPE_SOFT_AP;
        CreatorConsole_Puts("              [Configuration Mode]\r\n\r\n\r\n");
        networkSSID = ConfigStore_GetSoftAPSSID();
        const char *mac = ConfigStore_GetSoftAPSSID();
        int networkSSIDLength = strlen(networkSSID);
        DRV_WIFI_SsidSet((uint8_t *) networkSSID, networkSSIDLength);
        softAPPPassword = ConfigStore_GetSoftAPPassword();
        securityType = SetWEPKey(softAPPPassword);
        DRV_WIFI_NetworkTypeSet(DRV_WIFI_NETWORK_TYPE_SOFT_AP);

        while (DRV_WIFI_ContextLoad() == TCPIP_MAC_RES_PENDING)
        {
            CreatorThread_SleepMilliseconds(NULL, 50);
        }
    }
    else
    {
        networkType = DRV_WIFI_NETWORK_TYPE_INFRASTRUCTURE;
        UIControl_SetUIState(AppUIState_AppInitConnectingToNetwork);
        CreatorConsole_Puts("               [Application Mode]\r\n\r\n\r\n");

        uint8_t channelList[] = { };
        DRV_WIFI_ChannelListSet(channelList, sizeof(channelList));

        DRV_WIFI_ReconnectModeSet(DRV_WIFI_RETRY_FOREVER,         // retry forever to connect to Wi-Fi network
                DRV_WIFI_ATTEMPT_TO_RECONNECT,  // reconnect on deauth from AP
                40,                             // beacon timeout is 40 beacon periods
                DRV_WIFI_ATTEMPT_TO_RECONNECT); // reconnect on beacon timeout

        networkSSID = ConfigStore_GetNetworkSSID();
        int networkSSIDLength = strlen(networkSSID);
        DRV_WIFI_SsidSet((uint8_t *) networkSSID, networkSSIDLength);

        WiFiEncryptionType encryptionType = ConfigStore_GetEncryptionType();
        switch (encryptionType) {
            case WiFiEncryptionType_WEP:
            {
                const char *wepKey = ConfigStore_GetNetworkPassword();
                securityType = SetWEPKey(wepKey);
            }
                break;
            case WiFiEncryptionType_Open:
                DRV_WIFI_SecurityOpenSet();
                securityType = DRV_WIFI_SECURITY_OPEN;
                break;
            case WiFiEncryptionType_WPA:
            case WiFiEncryptionType_WPA2:
            default:
            {
                DRV_WIFI_WPA_CONTEXT context;
                if (encryptionType == WiFiEncryptionType_WPA)
                    context.wpaSecurityType = DRV_WIFI_SECURITY_WPA_WITH_KEY; // DRV_WIFI_SECURITY_WPA_WITH_PASS_PHRASE;
                else
                    context.wpaSecurityType = DRV_WIFI_SECURITY_WPA2_WITH_KEY; //DRV_WIFI_SECURITY_WPA2_WITH_PASS_PHRASE;
                const char *passPhrase = ConfigStore_GetNetworkPassword();
                int keyLength = strlen(passPhrase);
                memcpy(context.keyInfo.key, passPhrase, keyLength);
                context.keyInfo.keyLength = keyLength;

                while (TCPIP_MAC_RES_OK != DRV_WIFI_KeyDerive(keyLength, context.keyInfo.key, networkSSIDLength, networkSSID))
                    ;
                context.keyInfo.keyLength = 32;

                DRV_WIFI_SecurityWpaSet(&context);
                securityType = context.wpaSecurityType;
            }
                break;
        }
        DRV_WIFI_NetworkTypeSet(DRV_WIFI_NETWORK_TYPE_INFRASTRUCTURE);
    }

    CreatorConsole_Puts("==========================\r\n");
    CreatorConsole_Puts("*** WiFi Configuration ***\r\n");
    CreatorConsole_Puts("==========================\r\n");
    CreatorConsole_Puts("MAC:\t\t");
    CreatorConsole_Puts(ConfigStore_GetMacAddress());
    CreatorConsole_Puts("\r\nSSID:\t\t");
    CreatorConsole_Puts(networkSSID);
    if (softAPPPassword)
    {
        CreatorConsole_Puts("\r\nPassword:\t");
        CreatorConsole_Puts(softAPPPassword);

    }
    CreatorConsole_Puts("\r\nNetwork Type:\t");

    switch (networkType) {
        case DRV_WIFI_NETWORK_TYPE_INFRASTRUCTURE:
            CreatorConsole_Puts("Infrastructure");
            break;
        case DRV_WIFI_NETWORK_TYPE_ADHOC:
            CreatorConsole_Puts("AdHoc");
            break;
        case DRV_WIFI_NETWORK_TYPE_SOFT_AP:
            CreatorConsole_Puts("SoftAP");
            break;
    }
    CreatorConsole_Puts("\r\nSecurity:\t");

    switch (securityType) {
        case DRV_WIFI_SECURITY_OPEN:
            CreatorConsole_Puts("Open");
            break;
        case DRV_WIFI_SECURITY_WEP_40:
            CreatorConsole_Puts("WEP40");
            break;
        case DRV_WIFI_SECURITY_WEP_104:
            CreatorConsole_Puts("WEP104");
            break;
        case DRV_WIFI_SECURITY_WPA_WITH_KEY:
        case DRV_WIFI_SECURITY_WPA_WITH_PASS_PHRASE:
            CreatorConsole_Puts("WPA");
            break;
        case DRV_WIFI_SECURITY_WPA2_WITH_KEY:
        case DRV_WIFI_SECURITY_WPA2_WITH_PASS_PHRASE:
            CreatorConsole_Puts("WPA2");
            break;
        case DRV_WIFI_SECURITY_WPA_AUTO_WITH_KEY:
        case DRV_WIFI_SECURITY_WPA_AUTO_WITH_PASS_PHRASE:
            CreatorConsole_Puts("WPA AUTO");
            break;
    }

    DRV_WIFI_PsPollDisable();
    DRV_WIFI_Connect();

    CreatorConsole_Puts("\r\n\r\n");

    if (_RunningInConfigurationMode)
    {
        do
        {
            CreatorThread_SleepMilliseconds(NULL, 100);
            DRV_WIFI_ConnectionStateGet(&connectionState);
        } while ((connectionState != DRV_WIFI_CSTATE_CONNECTION_IN_PROGRESS) && (connectionState != DRV_WIFI_CSTATE_RECONNECTION_IN_PROGRESS));
        TCPIP_DHCPS_Enable(networkHandle);
        TCPIP_DNSS_Enable(networkHandle);
        UIControl_SetUIState(AppUIState_SoftApNotConnected);
        IPV4_ADDR ipAdd;
        ipAdd.Val = TCPIP_STACK_NetAddress(networkHandle);
        CreatorConsole_Puts(TCPIP_STACK_NetNameGet(networkHandle));
        CreatorConsole_Puts(" IP Address: ");
        CreatorConsole_Printf("%d.%d.%d.%d \r\n", ipAdd.v[0], ipAdd.v[1], ipAdd.v[2], ipAdd.v[3]);
    }
    else
    {
        CreatorConsole_Printf("\n\rConnecting to WiFi network...\n\r");
        bool displayedConnectionWarningToUser = false;
        do
        {
            CreatorThread_SleepMilliseconds(NULL, 100);
            DRV_WIFI_ConnectionStateGet(&connectionState);

            if (!displayedConnectionWarningToUser)
            {
                SYS_UPTIME uptime;
                AppConfig_Uptime(&uptime);
                if (uptime.Seconds >= 30 && !AppConfig_IsDeviceOnline())
                {
                    CreatorConsole_Printf("\n\r\n\r");
                    CreatorConsole_Printf("\n\r**********************************************************************");
                    CreatorConsole_Printf("\n\r* Your WiFire is taking a long time to connect to your WiFi network. *");
                    CreatorConsole_Printf("\n\r* Please check your network settings are correctly configured.       *");
                    CreatorConsole_Printf("\n\r*                                                                    *");
                    CreatorConsole_Printf("\n\r* Hold BTN1 and BTN2 whilst pressing the RESET button on your WiFire *");
                    CreatorConsole_Printf("\n\r* to restart it in Configuration mode.                               *");
                    CreatorConsole_Printf("\n\r*                                                                    *");
                    CreatorConsole_Printf("\n\r* You can then review and change your settings if required.          *");
                    CreatorConsole_Printf("\n\r**********************************************************************");
                    CreatorConsole_Printf("\n\r\n\r");
                    displayedConnectionWarningToUser = true;
                }
            }

        } while ((connectionState != DRV_WIFI_CSTATE_CONNECTED_INFRASTRUCTURE));
        TCPIP_DHCP_Enable(networkHandle);
        IPV4_ADDR ipAdd;
        ipAdd.Val = TCPIP_STACK_NetAddress(networkHandle);
        while (ipAdd.Val == 0x1901A8C0)
        {
            CreatorThread_SleepMilliseconds(NULL, 500);
            ipAdd.Val = TCPIP_STACK_NetAddress(networkHandle);
        }
        CreatorConsole_Puts(TCPIP_STACK_NetNameGet(networkHandle));
        CreatorConsole_Puts(" IP Address: ");
        CreatorConsole_Printf("%d.%d.%d.%d \r\n", ipAdd.v[0], ipAdd.v[1], ipAdd.v[2], ipAdd.v[3]);
        TCPIP_DNS_Enable(networkHandle, TCPIP_DNS_ENABLE_PREFERRED);
        UIControl_SetUIState(AppUIState_AppInitConnectedToNetwork);
    }
    CreatorConsole_Puts("\r\nConnected\r\n");

    //	IPV4_ADDR ipAdd;
    //	ipAdd.Val = TCPIP_STACK_NetAddress(networkHandle);
    //	CreatorConsole_Puts(TCPIP_STACK_NetNameGet(networkHandle));
    //	CreatorConsole_Puts(" IP Address: ");
    //	CreatorConsole_Printf("%d.%d.%d.%d \r\n", ipAdd.v[0], ipAdd.v[1], ipAdd.v[2], ipAdd.v[3]);

}
Ejemplo n.º 3
0
static int WFEasyConfigProcess(TCPIP_NET_IF* pNetIf)
{
#if 0  // should not be needed
    uint8_t ConnectionState;
#endif
    DRV_WIFI_SECURITY_CONTEXT securityContext;
    
    #if defined (EZ_CONFIG_STALL)
        if (CFGCXT.cfg_state == cfg_stopped)
        {
            /* State machine just started get current time stamp */
            CFGCXT.cfg_state = cfg_stalled;
            CFGCXT.timeStart = SYS_TICK_Get();
            return false;
        }

        /* Wait for stall time to expire */
        if (CFGCXT.cfg_state == cfg_stalled)
        {
            SYS_TICK time = SYS_TICK_Get();
            if ((time - CFGCXT.timeStart) < (WF_EASY_CONFIG_DELAY_TIME * SYS_TICK_TicksPerSecondGet()))
            {
                return false;
            }
        }
    #endif //EZ_CONFIG_STALL

#if 0  // should not be needed
    /* We will re-use the current profile */
    DRV_WIFI_ConnectionStateGet(&ConnectionState);
#endif

    /* Need to disconnect */
    DRV_WIFI_Disconnect();

    /* Set SSID... */
    if (CFGCXT.ssid)
    {
        DRV_WIFI_SsidSet(CFGCXT.ssid, strlen((char*)CFGCXT.ssid));
    }

    /* Now deal with security... */
    switch ((uint8_t)CFGCXT.security)
    {
        case DRV_WIFI_SECURITY_OPEN: /* No security */
            DRV_WIFI_SecurityOpenSet();
            break; 

        case DRV_WIFI_SECURITY_WPA_AUTO_WITH_PASS_PHRASE:
            if (CFGCXT.key)
            {
                securityContext.wpaContext.wpaSecurityType = DRV_WIFI_SECURITY_WPA_AUTO_WITH_PASS_PHRASE;
                securityContext.wpaContext.keyInfo.keyLength = strlen((char *)CFGCXT.key);
                memcpy(securityContext.wpaContext.keyInfo.key, CFGCXT.key, securityContext.wpaContext.keyInfo.keyLength);
                DRV_WIFI_SecurityWpaSet(&securityContext.wpaContext);
            }
            break;

        case DRV_WIFI_SECURITY_WPA_AUTO_WITH_KEY:
            if (CFGCXT.key) 
            {
                securityContext.wpaContext.wpaSecurityType = DRV_WIFI_SECURITY_WPA_AUTO_WITH_KEY;
                securityContext.wpaContext.keyInfo.keyLength = 32;
                memcpy(securityContext.wpaContext.keyInfo.key, CFGCXT.key, 32);
                DRV_WIFI_SecurityWpaSet(&securityContext.wpaContext);
            }
            break;

        case DRV_WIFI_SECURITY_WEP_40:
            if (CFGCXT.key)
            {
                securityContext.wepContext.wepSecurityType = DRV_WIFI_SECURITY_WEP_40;
                securityContext.wepContext.wepKeyLength    = DRV_WIFI_WEP40_KEY_LENGTH;
                memset(CFGCXT.key, 0x00, DRV_WIFI_WEP40_KEY_LENGTH);
                memset(securityContext.wepContext.wepKey, 0x00, DRV_WIFI_WEP40_KEY_LENGTH);
                securityContext.wepContext.wepKeyType      = DRV_WIFI_DEFAULT_WEP_KEY_TYPE;
                DRV_WIFI_SecurityWepSet(&securityContext.wepContext);
            }
            break;

        case DRV_WIFI_SECURITY_WEP_104:
            if (CFGCXT.key)
            {
                securityContext.wepContext.wepSecurityType = DRV_WIFI_SECURITY_WEP_104;
                securityContext.wepContext.wepKeyLength    = DRV_WIFI_WEP104_KEY_LENGTH;
                memset(CFGCXT.key, 0x00, DRV_WIFI_WEP104_KEY_LENGTH);
                memset(securityContext.wepContext.wepKey, 0x00, DRV_WIFI_WEP104_KEY_LENGTH);
                securityContext.wepContext.wepKeyType      = DRV_WIFI_DEFAULT_WEP_KEY_TYPE;
                DRV_WIFI_SecurityWepSet(&securityContext.wepContext);
            }
            break;
    }
 
    #if defined (EZ_CONFIG_STORE) && defined(TCPIP_STACK_USE_STORAGE)
        #if 0
            TCPIP_STORAGE_HANDLE hS;
            hS = TCPIPStorageOpen(0, 1);
            TCPIPStorageSaveIfConfig(hS, "MRF24W", true);
            TCPIPStorageClose(hS);
        #else
            DRV_WIFI_ConfigDataSave();
        #endif
    #endif // defined (EZ_CONFIG_STORE)

    /* Set wlan mode */
    DRV_WIFI_NetworkTypeSet(CFGCXT.type);

#if defined(DISABLE_MODULE_FW_CONNECT_MANAGER_IN_INFRASTRUCTURE)
    DRV_WIFI_ReconnectModeSet(0,                                     // report-only when connection lost (no reconnect)
                        DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT,  // report-only when deauth received (no reconnect)
                        40,                                    // set beacon timeout to 40 beacon periods
                        DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT); // report only when beacon timeout occurs
#endif
    //TCPIP_NET_IF* p_config= (TCPIP_NET_IF*)GetNetworkConfig();
    if (p_wifi_ConfigData->networkType == DRV_WIFI_NETWORK_TYPE_INFRASTRUCTURE)
    {
        DRV_WIFI_ReconnectModeSet(DRV_WIFI_RETRY_FOREVER,   // retry forever to connect to WiFi network
                            DRV_WIFI_ATTEMPT_TO_RECONNECT,  // reconnect on deauth from AP
                            40,                             // beacon timeout is 40 beacon periods
                            DRV_WIFI_ATTEMPT_TO_RECONNECT); // reconnect on beacon timeout
    }

#if WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_SOFT_AP
    // SoftAP: To allow redirection, need to hibernate before changing network type. Module
    //         FW has SoftAP flag and therefore hibernate mode is needed to clear this
    //         indication and allow proper network change. This should work for non-SoftAP,
    //         but these have not been tested yet.
    #if 0
        WF_hibernate.state = DRV_WIFI_HB_ENTER_SLEEP;
        WF_hibernate.wakeup_notice = false;
        //WFConsolePrintRomStr("SoftAP redirection: Put Wi-Fi module into hibernate mode.", true);

        DelayMs(200);

        WF_hibernate.wakeup_notice = true;
        //WFConsolePrintRomStr("Wakeup Wi-Fi module.", true);
    #else
        //extern bool SoftAP_Redirection_Enable;
        //SoftAP_Redirection_Enable = true;

        int ret_init;
        WF_InitForSoftApReDirection_enable();
        do
        {
            ret_init = WF_InitForSoftApReDirection();
        }while(ret_init == TCPIP_MAC_RES_PENDING);

    #endif
#else
    /* Kick off connection now... */
    DRV_WIFI_Connect();
#endif
    /* Change state and return true to show we are done! */
    CFGCXT.cfg_state = cfg_stopped;

    return true;
}
Ejemplo n.º 4
0
static void EasyConfigConnect(void)
{
    DRV_WIFI_Disconnect();    // break the connection supporting the web page

    if (g_easyConfigCtx.ssid)    // if AP has an SSID (not a hidden SSID)
    {
        DRV_WIFI_SsidSet(g_easyConfigCtx.ssid, strlen((char*)g_easyConfigCtx.ssid));
    }

    // Set security supported by designated AP
    EasyConfigSetSecurity();

    #if defined (EZ_CONFIG_STORE) 
        #if 0
            TCPIP_STORAGE_HANDLE hS;
            hS = TCPIPStorageOpen(0, 1);
            TCPIPStorageSaveIfConfig(hS, "MRF24W", true);
            TCPIPStorageClose(hS);
        #else
            DRV_WIFI_ConfigDataSave();
        #endif
    #endif // defined (EZ_CONFIG_STORE)

                /* Set wlan mode */
    DRV_WIFI_NetworkTypeSet(CFGCXT.type);

    #if defined(DISABLE_MODULE_FW_CONNECT_MANAGER_IN_INFRASTRUCTURE)
        DRV_WIFI_ReconnectModeSet(0,                               // report-only when connection lost (no reconnect)
                            DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT,  // report-only when deauth received (no reconnect)
                            40,                                    // set beacon timeout to 40 beacon periods
                            DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT); // report only when beacon timeout occurs
    #else
        //TCPIP_NET_IF* p_config= (TCPIP_NET_IF*)GetNetworkConfig();
        if (p_wifi_ConfigData->networkType == DRV_WIFI_NETWORK_TYPE_INFRASTRUCTURE)
        {
            DRV_WIFI_ReconnectModeSet(DRV_WIFI_RETRY_FOREVER,   // retry forever to connect to WiFi network
                                DRV_WIFI_ATTEMPT_TO_RECONNECT,  // reconnect on deauth from AP
                                40,                             // beacon timeout is 40 beacon periods
                                DRV_WIFI_ATTEMPT_TO_RECONNECT); // reconnect on beacon timeout
        }
    #endif

#if WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_SOFT_AP
    // SoftAP: To allow redirection, need to hibernate before changing network type. Module
    //         FW has SoftAP flag and therefore hibernate mode is needed to clear this
    //         indication and allow proper network change. This should work for non-SoftAP,
    //         but these have not been tested yet.
    #if 0
        WF_hibernate.state = DRV_WIFI_HB_ENTER_SLEEP;
        WF_hibernate.wakeup_notice = false;
        //WFConsolePrintRomStr("SoftAP redirection: Put Wi-Fi module into hibernate mode.", true);

        DelayMs(200);

        WF_hibernate.wakeup_notice = true;
        //WFConsolePrintRomStr("Wakeup Wi-Fi module.", true);
    #else
        //extern bool SoftAP_Redirection_Enable;
        //SoftAP_Redirection_Enable = true;
        int ret_init;
        WF_InitForSoftApReDirection_enable();
        do
        {
            ret_init = WF_InitForSoftApReDirection();
        }while(ret_init == TCPIP_MAC_RES_PENDING);
    #endif
#else
    /* Kick off connection now... */
    DRV_WIFI_Connect();
#endif

}
Ejemplo n.º 5
0
/*****************************************************************************
 * FUNCTION: Demo_Wifi_Connect
 *
 * RETURNS:  TCPIP_MAC_RES
 *
 * PARAMS:   None
 *
 *  NOTES:   Connects to an 802.11 network.
 *****************************************************************************/
 TCPIP_MAC_RES Demo_Wifi_Connect(void)
{
    typedef enum {
        demo_Wifi_Connect_SM_1=0,
        demo_Wifi_Connect_SM_2,
        demo_Wifi_Connect_SM_3,
        demo_Wifi_Connect_SM_done
    }enum_demoWifiConnect_SM;
    static enum_demoWifiConnect_SM demoWifiConnect_SM = demo_Wifi_Connect_SM_1;
    TCPIP_MAC_RES RetCode = TCPIP_MAC_RES_PENDING;
	

    switch(demoWifiConnect_SM)
    {
        case demo_Wifi_Connect_SM_1:
        {
            uint8_t channelList[] = WF_DEFAULT_CHANNEL_LIST;
            DRV_WIFI_SCAN_CONTEXT scanContext;

#if (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_SOFT_AP)
            uint8_t channelList_postscan[] = WF_DEFAULT_CHANNEL_LIST_POSTSCAN;
#endif

            DRV_WIFI_SsidSet(p_wifi_ConfigData->netSSID, p_wifi_ConfigData->SsidLength);

#if (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_ADHOC) || (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_SOFT_AP)
            DRV_WIFI_NetworkTypeSet(p_wifi_ConfigData->networkType);
#else
            DRV_WIFI_NetworkTypeSet(WF_DEFAULT_NETWORK_TYPE);
#endif

#ifndef WF_DEFAULT_SCAN_TYPE
            scanContext.scanType = DRV_WIFI_ACTIVE_SCAN;    
#else
            scanContext.scanType = WF_DEFAULT_SCAN_TYPE;
#endif

            scanContext.minChannelTime = DRV_WIFI_DEFAULT_SCAN_MIN_CHANNEL_TIME;
            scanContext.maxChannelTime = DRV_WIFI_DEFAULT_SCAN_MAX_CHANNEL_TIME;
            scanContext.probeDelay     = DRV_WIFI_DEFAULT_SCAN_PROBE_DELAY;
            scanContext.scanCount      = DRV_WIFI_DEFAULT_SCAN_COUNT;
            DRV_WIFI_ScanContextSet(&scanContext);

#if (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_SOFT_AP)
            if (((CFGCXT.type != DRV_WIFI_NETWORK_TYPE_SOFT_AP) && (CFGCXT.prevWLAN == DRV_WIFI_NETWORK_TYPE_SOFT_AP)) ||
                (p_wifi_ConfigData->networkType != DRV_WIFI_NETWORK_TYPE_SOFT_AP))
            {
                DRV_WIFI_ChannelListSet(channelList_postscan, sizeof(channelList_postscan));
            }
            else
            {
                DRV_WIFI_ChannelListSet(channelList, sizeof(channelList));
            }
#else // not network type not DRV_WIFI_NETWORK_TYPE_SOFT_AP
            DRV_WIFI_ChannelListSet(channelList, sizeof(channelList));
#endif // (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_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 DRV_WIFI_RETRY_FOREVER in the AdHoc mode, an AdHoc network
            // would never be established.
#if (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_ADHOC) || (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_SOFT_AP)
            if(p_wifi_ConfigData->networkType == DRV_WIFI_NETWORK_TYPE_INFRASTRUCTURE)
            {
                DRV_WIFI_ReconnectModeSet(DRV_WIFI_RETRY_FOREVER,   // retry forever to connect to WiFi network
                                    DRV_WIFI_ATTEMPT_TO_RECONNECT,  // reconnect on deauth from AP
                                    40,                             // beacon timeout is 40 beacon periods
                                    DRV_WIFI_ATTEMPT_TO_RECONNECT); // reconnect on beacon timeout
            }
            else if((p_wifi_ConfigData->networkType == DRV_WIFI_NETWORK_TYPE_ADHOC) || (p_wifi_ConfigData->networkType == DRV_WIFI_NETWORK_TYPE_SOFT_AP))
            {
                DRV_WIFI_ReconnectModeSet(WF_DEFAULT_LIST_RETRY_COUNT,    // retry N times to start or join AdHoc network
                                    DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT, // do not attempt to reconnect on deauth from station
                                    40,                                   // beacon timeout is 40 beacon periods
                                    DRV_WIFI_ATTEMPT_TO_RECONNECT);             // reconnect on beacon timeout
            }
            else
            {
                SYS_CONSOLE_MESSAGE("Please compile with correct XX_RETRY_COUNT\r\n");
                while(1);
            }
#else // network type not DRV_WIFI_NETWORK_TYPE_ADHOC or DRV_WIFI_NETWORK_TYPE_SOFT_AP
            DRV_WIFI_ReconnectModeSet(WF_DEFAULT_LIST_RETRY_COUNT, // retry N times to start or join AdHoc network
                                DRV_WIFI_ATTEMPT_TO_RECONNECT,     // reconnect on deauth from AP
                                40,                                // beacon timeout is 40 beacon periods
                                DRV_WIFI_ATTEMPT_TO_RECONNECT);    // reconnect on beacon timeout

#endif // (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_ADHOC) || (WF_DEFAULT_NETWORK_TYPE == DRV_WIFI_NETWORK_TYPE_SOFT_AP)


            // Set Tx Mode
            DRV_WIFI_TxModeSet(WF_DEFAULT_TX_MODE); // WF_TXMODE_G_RATES, WF_TXMODE_B_RATES, WF_TXMODE_LEGACY_RATES

            // Error check items specific to WPS Push Button mode
#if (WF_DEFAULT_WIFI_SECURITY_MODE==DRV_WIFI_SECURITY_WPS_PUSH_BUTTON)
                //    SYS_ASSERT(strlen(p_wifi_ConfigData->netSSID) == 0, "");  // SSID must be empty when using WPS
                // To Do: fix this to work with different Domain and also empty channelList
                // SYS_ASSERT(sizeof(channelList)==11, "");      // must scan all channels for WPS
#endif
        
        }
        demoWifiConnect_SM++;
        break;

        case demo_Wifi_Connect_SM_2:
        {
            TCPIP_MAC_RES tmp = TCPIP_MAC_RES_OK;
            if (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_OPEN)
            {
                DRV_WIFI_SecurityOpenSet();
            }
            else if ((p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WEP_40) || (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WEP_104))
            {
                SetWepSecurity();
            }
            else if ((p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPA_AUTO_WITH_KEY)         ||
                     (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPA_WITH_KEY)              ||
                     (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPA_AUTO_WITH_PASS_PHRASE) ||
                     (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPA_WITH_PASS_PHRASE)      ||
                     (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPA2_WITH_KEY)             ||
                     (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPA2_WITH_PASS_PHRASE))
            {
                tmp = SetWpaSecurity();
            }
            else if ((p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPS_PIN) ||
                    (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_WPS_PUSH_BUTTON))
            {
                SetWpsSecurity();
            }
            else if (p_wifi_ConfigData->SecurityMode == DRV_WIFI_SECURITY_EAP)
            {
                SetEapSecurity();
            }

            if(TCPIP_MAC_RES_OK == tmp ) demoWifiConnect_SM++;
        }
            
            break;
        
        case demo_Wifi_Connect_SM_3:
        {
#if WF_DEFAULT_PS_POLL == DRV_WIFI_ENABLED
            {
                DRV_WIFI_PS_POLL_CONTEXT psPollContext;
                psPollContext.dtimInterval   = DRV_WIFI_DEFAULT_PS_DTIM_INTERVAL;
                psPollContext.listenInterval = DRV_WIFI_DEFAULT_PS_LISTEN_INTERVAL;
                psPollContext.useDtim        = true;
                DRV_WIFI_PsPollEnable(&psPollContext);
            }
 #else    /* WF_DEFAULT_PS_POLL == DRV_WIFI_DISABLED */
            DRV_WIFI_PsPollDisable();
#endif    /* WF_DEFAULT_PS_POLL == DRV_WIFI_ENABLED */

#if defined(TCPIP_STACK_USE_GRATUITOUS_ARP)
            DRV_WIFI_GratuitousArpStart(5);  // output a gratuitous arp every 5 seconds (after connection)
#endif

 #if defined(SYS_CONSOLE_ENABLE)
             OutputDemoHeader();
#endif

        // override reconnect mode if connection manager disabled
#if defined(DISABLE_MODULE_FW_CONNECT_MANAGER_IN_INFRASTRUCTURE)
            DRV_WIFI_ReconnectModeSet(0,                               // report-only when connection lost (no reconnect)
                                DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT,  // report-only when deauth received (no reconnect)
                                40,                                    // set beacon timeout to 40 beacon periods
                                DRV_WIFI_DO_NOT_ATTEMPT_TO_RECONNECT); // report only when beacon timeout occurs
#endif

            WF_EnableDebugPrint(ENABLE_WPS_PRINTS);
            SYS_CONSOLE_MESSAGE("\r\nStart WiFi Connect . . .\r\n");

#if defined(TCPIP_STACK_USE_DHCP_CLIENT)
            if( DhcpHandle_Mrf24w == NULL)
            {    DhcpHandle_Mrf24w =  TCPIP_DHCP_HandlerRegister(TCPIP_STACK_NetHandleGet("MRF24W"), DhcpEventHandler, NULL);
            }
#endif  // defined(TCPIP_STACK_USE_DHCP_CLIENT)

            // start the WiFi connection process
            DRV_WIFI_Connect();

            WifiAsyncSetEventPending(ASYNC_DHCP_CONFIG_PENDING);  // ken test
        }
            demoWifiConnect_SM++;
            RetCode = TCPIP_MAC_RES_OK;
            //break;   //not break, will continue to demo_Wifi_Connect_SM_done
            
        case demo_Wifi_Connect_SM_done:
            demoWifiConnect_SM = demo_Wifi_Connect_SM_1;
            break;
    }
 
	return RetCode;
}