void NetStop() { unsigned long lRetVal; // // Disconnect from the AP // lRetVal = sl_WlanDisconnect(); if(0 == lRetVal) { // Wait while(IS_CONNECTED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } // // Stop the simplelink host // sl_Stop(SL_STOP_TIMEOUT); // // Reset all variables // g_ulStatus = 0; g_ulGatewayIP = 0; g_ucSimplelinkstarted = 0; g_ulIpAddr = 0; }
//***************************************************************************** // //! Disconnect Disconnects from an Access Point //! //! \param none //! //! \return 0 disconnected done, other already disconnected // //***************************************************************************** long Network_IF_DisconnectFromAP() { long lRetVal = 0; if (IS_CONNECTED(g_ulStatus)) { lRetVal = sl_WlanDisconnect(); if(0 == lRetVal) { // Wait while(IS_CONNECTED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #else osi_Sleep(1); #endif } return lRetVal; } else { return lRetVal; } } else { return lRetVal; } }
//***************************************************************************** // //! Disconnect Disconnects from an Access Point //! //! \param none //! //! \return none // //***************************************************************************** void Network_IF_DisconnectFromAP() { if (IS_CONNECTED(g_ulStatus)) sl_WlanDisconnect(); while(IS_CONNECTED(g_ulStatus)); }
int32_t ResetSimpleLink() { int32_t i32Mode = -1, i32RetVal = -1; uint8_t ui8Val = 1; i32Mode = sl_Start(0, 0, 0); i32RetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0); if (i32RetVal != 0) { return -1; } i32RetVal = sl_WlanProfileDel(0xFF); if (i32RetVal != 0) { return -1; } if (ROLE_STA != i32Mode) { if (ROLE_AP == i32Mode) { while (!STATUS_GET(g_sSLCon.Status, STATUS_BIT_IP_ACQUIRED)) { } } sl_WlanSetMode(ROLE_STA); sl_Stop(0); g_sSLCon.Status = 0; i32RetVal = sl_Start(0, 0, 0); if (ROLE_STA != i32RetVal) { return -1; } } i32RetVal = sl_WlanDisconnect(); if (i32RetVal == 0) { while (STATUS_GET(g_sSLCon.Status, STATUS_BIT_CONNECTED)) { } } sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, 1, 1, &ui8Val); sl_WlanPolicySet(SL_POLICY_SCAN, SL_SCAN_POLICY(0), NULL, NULL); ui8Val = 0; sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *) &ui8Val); sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0); sl_NetAppMDNSUnRegisterService(0, 0); sl_Stop(0); InitVariables(); return 0; }
//--tested, working--// bool WiFiClass::init() { // //only initialize once // if (_initialized) { return true; } // //Initialize the UDMA // UDMAInit(); // //start the SimpleLink driver (no callback) // int iRet = sl_Start(NULL, NULL, NULL); // //check if sl_start failed // if (iRet==ROLE_STA_ERR || iRet==ROLE_AP_ERR || iRet==ROLE_P2P_ERR) { return false; } // //set the mode to station if it's not already in station mode // if (iRet != ROLE_STA) { sl_WlanSetMode(ROLE_STA); sl_Stop(0); sl_Start(NULL, NULL, NULL); } // //Delete all profiles$ // sl_WlanProfileDel(0xff); // //disconnect from anything if for some reason it's connected // sl_WlanDisconnect(); sl_NetAppMDNSUnRegisterService(0, 0); _initialized = true; // // Start collecting statistics // sl_WlanRxStatStart(); return true; }
//***************************************************************************** // //! \brief disconnection from the WLAN Accesspoint //! //! \param[in] None //! //! \return None //! //! \note //! //! \warning If the WLAN disconnection fails, We will be stuck in this //! function forever. // //***************************************************************************** void WlanDisconnect() { unsigned char ucQueueMsg = 0; while(ucQueueMsg != (unsigned char)EVENT_DISCONNECTION) { sl_WlanDisconnect(); osi_MsgQRead(&g_tConnection, &ucQueueMsg, OSI_WAIT_FOREVER); } return; }
int WiFiClass::disconnect(void) { if (!_initialized) { init(); } // //disconnect from the wlan and return the current wlan_status // sl_WlanDisconnect(); return WiFi_status; }
STATIC void wlan_sl_disconnect (void) { // Device in station-mode. Disconnect previous connection if any // The function returns 0 if 'Disconnected done', negative number if already // disconnected Wait for 'disconnection' event if 0 is returned, Ignore // other return-codes if (0 == sl_WlanDisconnect()) { while (IS_CONNECTED(wlan_obj.status)) { mp_hal_delay_ms(MODWLAN_CONNECTION_WAIT_MS); wlan_update(); } } }
//***************************************************************************** // //! DeInitDriver //! The function de-initializes a CC3200 device //! //! \param None //! //! \return none // //***************************************************************************** void DeInitDriver(void) { // // Disconnect from the AP // sl_WlanDisconnect(); // // Stop the simplelink host // sl_Stop(10); }
bool wifi_setup_sta(const char *ssid, const char *pass) { SlSecParams_t sp; LOG(LL_INFO, ("WiFi: connecting to %s", ssid)); if (sl_WlanSetMode(ROLE_STA) != 0) return false; sl_Stop(0); sl_Start(NULL, NULL, NULL); sl_WlanDisconnect(); sp.Key = (_i8 *) pass; sp.KeyLen = strlen(pass); sp.Type = sp.KeyLen ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN; if (sl_WlanConnect((const _i8 *) ssid, strlen(ssid), 0, &sp, 0) != 0) { return false; } return true; }
void wlan_configure() { sl_Start(NULL, NULL, NULL); // // reset all network policies // unsigned char ucpolicyVal; int ret; ret = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), &ucpolicyVal, 1 /*PolicyValLen*/); if(ret < 0) { LOOP_FOREVER(); } sl_WlanSetMode(ROLE_STA); sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0); sl_WlanProfileDel(0xFF); sl_WlanDisconnect(); _WlanRxFilterOperationCommandBuff_t RxFilterIdMask;// = {0}; memset(&RxFilterIdMask, 0, sizeof(RxFilterIdMask)); unsigned char ucVal = 0; unsigned char ucConfigOpt = 0; // Enable DHCP client sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal); // Disable scan ucConfigOpt = SL_SCAN_POLICY(0); sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0); // Set Tx power level for station mode // Number between 0-15, as dB offset from max power - 0 will set max power unsigned char ucPower = 0; sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower); // Set PM policy to normal sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); // Unregister mDNS services sl_NetAppMDNSUnRegisterService(0, 0); // Remove all 64 filters (8*8) memset(RxFilterIdMask.FilterIdMask, 0xFF, 8); sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t)); sl_Stop(SL_STOP_TIMEOUT); }
void CC3200Helpers_HibernateNowFor(unsigned long dwSeconds, char flStopSL) { unsigned long long qwTicks = dwSeconds; qwTicks *= SLOW_CLK_FREQ; MAP_PRCMHibernateIntervalSet(qwTicks); MAP_PRCMHibernateWakeupSourceEnable(PRCM_HIB_SLOW_CLK_CTR); MAP_UtilsDelay(80000); if ( flStopSL ) { sl_WlanDisconnect(); sl_Stop(0); } MAP_SPICSDisable(SSPI_BASE); MAP_SPICSEnable(SSPI_BASE); MAP_SPIDataPut(SSPI_BASE, 0xB9); // deep power down MAP_SPICSDisable(SSPI_BASE); MAP_PRCMHibernateEnter(); }
//***************************************************************************** // //! \brief Disconnecting from a WLAN Accesspoint //! //! \param[in] None //! //! \return 0 : Success, -ve : failure // //***************************************************************************** long WlanDisconnect() { long lRetVal = -1; lRetVal = sl_WlanDisconnect(); if(0 == lRetVal) { // Wait while(IS_CONNECTED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } return lRetVal; }
/*! \brief This function puts the device in its default state. It: - Set the mode to STATION - Configures connection policy to Auto and AutoSmartConfig - Deletes all the stored profiles - Enables DHCP - Disables Scan policy - Sets Tx power to maximum - Sets power policy to normal - Unregister mDNS services - Remove all filters \param[in] none \return On success, zero is returned. On error, negative is returned */ static _i32 configureSimpleLinkToDefaultState(_i8 *pConfig) { SlVersionFull ver = {0}; _WlanRxFilterOperationCommandBuff_t RxFilterIdMask = {0}; _u8 val = 1; _u8 configOpt = 0; _u8 configLen = 0; _u8 power = 0; _i32 retVal = -1; _i32 mode = -1; mode = sl_Start(0, pConfig, 0); ASSERT_ON_ERROR(mode); /* If the device is not in station-mode, try configuring it in station-mode */ if (ROLE_STA != mode) { if (ROLE_AP == mode) { /* If the device is in AP mode, we need to wait for this event before doing anything */ while(!IS_IP_ACQUIRED(g_Status)); } /* Switch to STA role and restart */ retVal = sl_WlanSetMode(ROLE_STA); ASSERT_ON_ERROR(retVal); retVal = sl_Stop(SL_STOP_TIMEOUT); ASSERT_ON_ERROR(retVal); retVal = sl_Start(0, pConfig, 0); ASSERT_ON_ERROR(retVal); /* Check if the device is in station again */ if (ROLE_STA != retVal) { /* We don't want to proceed if the device is not coming up in station-mode */ ASSERT_ON_ERROR(DEVICE_NOT_IN_STATION_MODE); } } /* Get the device's version-information */ configOpt = SL_DEVICE_GENERAL_VERSION; configLen = sizeof(ver); retVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &configOpt, &configLen, (_u8 *)(&ver)); ASSERT_ON_ERROR(retVal); printf("Host Driver Version: %s\n",SL_DRIVER_VERSION); printf("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n", ver.NwpVersion[0],ver.NwpVersion[1],ver.NwpVersion[2],ver.NwpVersion[3], ver.ChipFwAndPhyVersion.FwVersion[0],ver.ChipFwAndPhyVersion.FwVersion[1], ver.ChipFwAndPhyVersion.FwVersion[2],ver.ChipFwAndPhyVersion.FwVersion[3], ver.ChipFwAndPhyVersion.PhyVersion[0],ver.ChipFwAndPhyVersion.PhyVersion[1], ver.ChipFwAndPhyVersion.PhyVersion[2],ver.ChipFwAndPhyVersion.PhyVersion[3]); /* Set connection policy to Auto + SmartConfig (Device's default connection policy) */ retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0); ASSERT_ON_ERROR(retVal); /* Remove all profiles */ retVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(retVal); /* * Device in station-mode. Disconnect previous connection if any * The function returns 0 if 'Disconnected done', negative number if already disconnected * Wait for 'disconnection' event if 0 is returned, Ignore other return-codes */ retVal = sl_WlanDisconnect(); if(0 == retVal) { /* Wait */ while(IS_CONNECTED(g_Status)); } /* Enable DHCP client*/ retVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&val); ASSERT_ON_ERROR(retVal); /* Disable scan */ configOpt = SL_SCAN_POLICY(0); retVal = sl_WlanPolicySet(SL_POLICY_SCAN , configOpt, NULL, 0); ASSERT_ON_ERROR(retVal); /* Set Tx power level for station mode Number between 0-15, as dB offset from max power - 0 will set maximum power */ power = 0; retVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (_u8 *)&power); ASSERT_ON_ERROR(retVal); /* Set PM policy to normal */ retVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); ASSERT_ON_ERROR(retVal); /* Unregister mDNS services */ retVal = sl_NetAppMDNSUnRegisterService(0, 0); ASSERT_ON_ERROR(retVal); /* Remove all 64 filters (8*8) */ memset(RxFilterIdMask.FilterIdMask, 0xFF, 8); retVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t)); ASSERT_ON_ERROR(retVal); retVal = sl_Stop(SL_STOP_TIMEOUT); ASSERT_ON_ERROR(retVal); retVal = initializeAppVariables(); ASSERT_ON_ERROR(retVal); return retVal; /* Success */ }
//***************************************************************************** //! \brief This function puts the device in its default state. It: //! - Set the mode to STATION //! - Configures connection policy to Auto and AutoSmartConfig //! - Deletes all the stored profiles //! - Enables DHCP //! - Disables Scan policy //! - Sets Tx power to maximum //! - Sets power policy to normal //! - Unregister mDNS services //! - Remove all filters //! //! \param none //! \return On success, zero is returned. On error, negative is returned //***************************************************************************** static long ConfigureSimpleLinkToDefaultState() { SlVersionFull ver = {0}; _WlanRxFilterOperationCommandBuff_t RxFilterIdMask = {0}; unsigned char ucVal = 1; unsigned char ucConfigOpt = 0; unsigned char ucConfigLen = 0; unsigned char ucPower = 0; long lRetVal = -1; long lMode = -1; lMode = sl_Start(0, 0, 0); ASSERT_ON_ERROR(lMode); // If the device is not in station-mode, try configuring it in station-mode if (ROLE_STA != lMode) { if (ROLE_AP == lMode) { // If the device is in AP mode, we need to wait for this event // before doing anything while(!IS_IP_ACQUIRED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } // Switch to STA role and restart lRetVal = sl_WlanSetMode(ROLE_STA); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(0xFF); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Start(0, 0, 0); ASSERT_ON_ERROR(lRetVal); // Check if the device is in station again if (ROLE_STA != lRetVal) { // We don't want to proceed if the device is not coming up in STA-mode return DEVICE_NOT_IN_STATION_MODE; } } // Get the device's version-information ucConfigOpt = SL_DEVICE_GENERAL_VERSION; ucConfigLen = sizeof(ver); lRetVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &ucConfigOpt, &ucConfigLen, (unsigned char *)(&ver)); ASSERT_ON_ERROR(lRetVal); UART_PRINT("Host Driver Version: %s\n\r",SL_DRIVER_VERSION); UART_PRINT("Build Version %d.%d.%d.%d.31.%d.%d.%d.%d.%d.%d.%d.%d\n\r", ver.NwpVersion[0],ver.NwpVersion[1],ver.NwpVersion[2],ver.NwpVersion[3], ver.ChipFwAndPhyVersion.FwVersion[0],ver.ChipFwAndPhyVersion.FwVersion[1], ver.ChipFwAndPhyVersion.FwVersion[2],ver.ChipFwAndPhyVersion.FwVersion[3], ver.ChipFwAndPhyVersion.PhyVersion[0],ver.ChipFwAndPhyVersion.PhyVersion[1], ver.ChipFwAndPhyVersion.PhyVersion[2],ver.ChipFwAndPhyVersion.PhyVersion[3]); // Set connection policy to Auto + SmartConfig // (Device's default connection policy) lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0); ASSERT_ON_ERROR(lRetVal); // Remove all profiles lRetVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(lRetVal); // // Device in station-mode. Disconnect previous connection if any // The function returns 0 if 'Disconnected done', negative number if already // disconnected Wait for 'disconnection' event if 0 is returned, Ignore // other return-codes // lRetVal = sl_WlanDisconnect(); if(0 == lRetVal) { // Wait while(IS_CONNECTED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } // Enable DHCP client lRetVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&ucVal); ASSERT_ON_ERROR(lRetVal); // Disable scan ucConfigOpt = SL_SCAN_POLICY(0); lRetVal = sl_WlanPolicySet(SL_POLICY_SCAN , ucConfigOpt, NULL, 0); ASSERT_ON_ERROR(lRetVal); // Set Tx power level for station mode // Number between 0-15, as dB offset from max power - 0 will set max power ucPower = 0; lRetVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&ucPower); ASSERT_ON_ERROR(lRetVal); // Set PM policy to normal lRetVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); ASSERT_ON_ERROR(lRetVal); // Unregister mDNS services lRetVal = sl_NetAppMDNSUnRegisterService(0, 0); ASSERT_ON_ERROR(lRetVal); // Remove all 64 filters (8*8) memset(RxFilterIdMask.FilterIdMask, 0xFF, 8); lRetVal = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (_u8 *)&RxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t)); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(SL_STOP_TIMEOUT); ASSERT_ON_ERROR(lRetVal); InitializeAppVariables(); return lRetVal; // Success }
int miot_wifi_disconnect(void) { return (sl_WlanDisconnect() == 0); }
int sj_wifi_disconnect() { return (sl_WlanDisconnect() == 0); }
//**************************************************************************** // //! Task function implements the Antenna Selection functionality //! //! \param none //! //! This function //! 1. Starts Device in STA Mode //! 2. Scans, Sort and Stores all the AP with Antenna 1 //! 3. Scans, Sort and Stores all the AP with Antenna 2 //! 4. Switch to AP Mode and Wait for AP Configuration from Browser //! 5. Switch to STA Mode and Connect to Configured AP with Selected Antenna //! //! \return None. // //**************************************************************************** void AntennaSelection(void* pTaskParams) { int iDeviceMode = 0; unsigned char ucCountSSID; unsigned char ucCountSSIDAnt2; long lRetVal = -1; InitializeAppVariables(); // // Following function configure the device to default state by cleaning // the persistent settings stored in NVMEM (viz. connection profiles & // policies, power policy etc) // // Applications may choose to skip this step if the developer is sure // that the device is in its default state at start of applicaton // // Note that all profiles and persistent settings that were done on the // device will be lost // lRetVal = ConfigureSimpleLinkToDefaultState(); if(lRetVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == lRetVal) { UART_PRINT("Failed to configure the device in its default state\n\r"); } LOOP_FOREVER(__LINE__); } UART_PRINT("Device is configured in default state \n\r"); // // Assumption is that the device is configured in station mode already // and it is in its default state // lRetVal = sl_Start(0, 0, 0); if (lRetVal < 0 || ROLE_STA != lRetVal) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(__LINE__); } UART_PRINT("Device started as STATION \n\r"); // // Start the driver // iDeviceMode = InitDriver(); if (iDeviceMode == ROLE_AP) { //Device in AP Mode, Wait for Initialization to Complete while (g_ucIpObtained == 0) { MAP_UtilsDelay(100); } } sl_WlanSetMode(ROLE_STA); if (iDeviceMode == ROLE_AP) DeInitDriverAP(); else DeInitDriver(); g_ucIpObtained = 0; g_ucConnectionStatus = 0; InitDriver(); //Select Antenna 1 AntennaSelect(1); //Get Scan Result ucCountSSID = GetScanResult(&g_netEntries[0]); //Select Antenna 2 AntennaSelect(2); //Get Scan Result ucCountSSIDAnt2 = GetScanResult(&g_netEntriesAnt2[0]); //Sort Scan Result SortByRSSI(&g_netEntries[0],ucCountSSID); SortByRSSI(&g_netEntriesAnt2[0],ucCountSSIDAnt2); while(!g_ucAntSelectDone) { //Switch to AP Mode sl_WlanSetMode(ROLE_AP); DeInitDriver(); g_ucIpObtained = 0; g_ucConnectionStatus = 0; //Initialize the SLHost Driver InitDriver(); //Wait for Ip Acquired Event in AP Mode while (g_ucIpObtained == 0) { MAP_UtilsDelay(100); } // // Wait for AP Configuraiton, Open Browser and Configure AP // while (g_ucProfileAdded && !g_ucAntSelectDone) { MAP_UtilsDelay(100); } g_ucProfileAdded = 1; //Switch to STA Mode sl_WlanSetMode(ROLE_STA); //AP Configured, Restart in STA Mode DeInitDriverAP(); g_ucIpObtained = 0; g_ucConnectionStatus = 0; //MAP_UtilsDelay(10000000); InitDriver(); // // Connect to the Configured Access Point // WlanConnect(); g_ucConnectedToConfAP = g_ucConnectionStatus; sl_WlanDisconnect(); } while (1) { } }
/*! \brief This function puts the device in its default state. It: - Set the mode to STATION - Configures connection policy to Auto and AutoSmartConfig - Deletes all the stored profiles - Enables DHCP - Disables Scan policy - Sets Tx power to maximum - Sets power policy to normal - Unregister mDNS services \param[in] none \return On success, zero is returned. On error, negative is returned */ static int32_t configureSimpleLinkToDefaultState(char *pConfig){ SlVersionFull ver = {0}; UINT8 val = 1; UINT8 configOpt = 0; UINT8 configLen = 0; UINT8 power = 0; INT32 retVal = -1; INT32 mode = -1; mode = sl_Start(0, pConfig, 0); /* If the device is not in station-mode, try putting it in station-mode */ if (ROLE_STA != mode){ if (ROLE_AP == mode){ /* If the device is in AP mode, we need to wait for this event before doing anything */ while(!IS_IP_AQUIRED(g_Status)); } /* Switch to STA role and restart */ retVal = sl_WlanSetMode(ROLE_STA); retVal = sl_Stop(0xFF); retVal = sl_Start(0, pConfig, 0); /* Check if the device is in station again */ if (ROLE_STA != retVal){ /* We don't want to proceed if the device is not coming up in station-mode */ return DEVICE_NOT_IN_STATION_MODE; } } /* Get the device's version-information */ configOpt = SL_DEVICE_GENERAL_VERSION; configLen = sizeof(ver); retVal = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &configOpt, &configLen, (unsigned char *)(&ver)); /* Set connection policy to Auto + SmartConfig (Device's default connection policy) */ retVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 1), NULL, 0); /* Remove all profiles */ retVal = sl_WlanProfileDel(0xFF); /* * Device in station-mode. Disconnect previous connection if any * The function returns 0 if 'Disconnected done', negative number if already disconnected * Wait for 'disconnection' event if 0 is returned, Ignore other return-codes */ retVal = sl_WlanDisconnect(); if(0 == retVal){ /* Wait */ while(IS_CONNECTED(g_Status)); } /* Enable DHCP client*/ retVal = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&val); /* Disable scan */ configOpt = SL_SCAN_POLICY(0); retVal = sl_WlanPolicySet(SL_POLICY_SCAN , configOpt, NULL, 0); /* Set Tx power level for station mode Number between 0-15, as dB offset from max power - 0 will set maximum power */ power = 0; retVal = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&power); /* Set PM policy to normal */ retVal = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); /* TBD - Unregister mDNS services */ retVal = sl_NetAppMDNSUnRegisterService(0, 0); retVal = sl_Stop(0xFF); g_Status = 0; memset(&Recvbuff,0,MAX_RECV_BUFF_SIZE); memset(&SendBuff,0,MAX_SEND_BUFF_SIZE); memset(&HostName,0,MAX_HOSTNAME_SIZE); DestinationIP = 0;; SockID = 0; return retVal; /* Success */ }
void initNetwork(signed char *ssid, SlSecParams_t *keyParams) { memset(g_ConnectionSSID, 0, sizeof(g_ConnectionSSID)); memset(g_ConnectionBSSID, 0, sizeof(g_ConnectionBSSID)); short status = sl_Start(0, 0, 0); if (status >= 0) { printVersionInfo(); // disable scan unsigned char configOpt = SL_SCAN_POLICY(0); sl_WlanPolicySet(SL_POLICY_SCAN, configOpt, NULL, 0); // set tx power to maximum unsigned char txPower = 0; sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&txPower); // set power policy to normal sl_WlanPolicySet(SL_POLICY_PM, SL_NORMAL_POLICY, NULL, 0); // remove all rx filters _WlanRxFilterOperationCommandBuff_t rxFilterIdMask; memset(rxFilterIdMask.FilterIdMask, 0xFF, 8); sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (unsigned char *)&rxFilterIdMask, sizeof(_WlanRxFilterOperationCommandBuff_t)); status = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 0), NULL, 0); } if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } sl_WlanDisconnect(); status = sl_WlanSetMode(ROLE_STA); if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } UART_PRINT("\r\n"); UART_PRINT("[QuickStart] Network init\r\n"); status = sl_WlanConnect(ssid, strlen((char *)ssid), NULL, keyParams, NULL); if (status < 0) { sl_Stop(SL_STOP_TIMEOUT); ERR_PRINT(status); LOOP_FOREVER(); } while (!IS_IP_ACQUIRED(g_Status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #else osi_Sleep(100); #endif } sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); }
//**************************************************************************** // //! \brief Configuring the Connection Policy //! //! This function configures different Connection Policy such as FAST,AUTO,OPEN //! //! \param[in] None //! //! \return 0 on success else error code //! //! \note // //**************************************************************************** static long SetConnectionPolicy() { unsigned char policyVal; long lRetVal = -1; /* Clear all stored profiles and reset the policies */ lRetVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), 0, 0); ASSERT_ON_ERROR(lRetVal); //Add Profile /* user needs to change SSID_NAME = "<Secured AP>" SECURITY_TYPE = SL_SEC_TYPE_WPA SECURITY_KEY = "<password>" and set the priority as per requirement to connect with a secured AP */ SlSecParams_t secParams; secParams.Key = SECURITY_KEY; secParams.KeyLen = strlen(SECURITY_KEY); secParams.Type = SECURITY_TYPE; lRetVal = sl_WlanProfileAdd(SSID_NAME,strlen(SSID_NAME),0,&secParams,0,1,0); ASSERT_ON_ERROR(lRetVal); //set AUTO policy lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1,0,0,0,0), &policyVal, 1 /*PolicyValLen*/); ASSERT_ON_ERROR(lRetVal); //wait until IP is acquired while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION); CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED); // // ****** Put breakpoint here ****** // If control comes here- means device connected to AP in auto mode // // Disconnect from AP lRetVal = sl_WlanDisconnect(); if(0 == lRetVal) { // Wait while(IS_CONNECTED(g_ulStatus)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //delete profiles lRetVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(lRetVal); //set Auto SmartConfig policy lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1,0,0,0,1), &policyVal,0); ASSERT_ON_ERROR(lRetVal); // reset the NWP lRetVal = ResetNwp(); ASSERT_ON_ERROR(lRetVal); // wait for smart config to complete and device to connect to an AP //wait until IP is acquired while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION); CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED); // // ****** Put breakpoint here ****** // If control comes here- means device connected to AP in smartconfig mode // /* Delete all profiles (0xFF) stored */ lRetVal = sl_WlanProfileDel(0xFF); ASSERT_ON_ERROR(lRetVal); // reset the NWP lRetVal = ResetNwp(); ASSERT_ON_ERROR(lRetVal); /* Set connection policy to Fast, Device will connect to last connected AP. * This feature can be used to reconnect to AP */ lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION , \ SL_CONNECTION_POLICY(1,1,0,0,0), 0, 0); ASSERT_ON_ERROR(lRetVal); /* Connect to the open AP */ lRetVal = sl_WlanConnect(SSID_NAME, strlen(SSID_NAME), 0, 0, 0); ASSERT_ON_ERROR(lRetVal); //wait until IP is acquired while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_CONNECTION); CLR_STATUS_BIT(g_ulStatus, STATUS_BIT_IP_AQUIRED); // Add unsecured AP profile with priority 6 (7 is highest) // Because of a limitation in SDK-0.5 which prevents the automatic addition // of the profile (fast), the application is required to explicitly add it. // The limitation shall be addressed in subsequent SDK release, and the below // lines for adding the profile can be removed then...! secParams.Key = ""; secParams.KeyLen = 0; secParams.Type = SL_SEC_TYPE_OPEN; lRetVal = sl_WlanProfileAdd((signed char*)SSID_NAME, strlen(SSID_NAME), 0, &secParams, 0, 6, 0); ASSERT_ON_ERROR(lRetVal); // reset the NWP lRetVal = ResetNwp(); ASSERT_ON_ERROR(lRetVal); /* Wait for the connection to be established */ while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { _SlNonOsMainLoopTask(); } // // ****** Put breakpoint here ****** // If control comes here- means device connected to AP in FAST mode // //remove all policies lRetVal = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(0,0,0,0,0), &policyVal,0); ASSERT_ON_ERROR(lRetVal); return SUCCESS; }
static int WifiDefaultSettings(void) { long retval = -1; unsigned char val = 1; unsigned char config, config_len; //filters _WlanRxFilterOperationCommandBuff_t filter_mask = { .Padding = {0} }; //initialize the SimpleLink API retval = sl_Start(0,0,0); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL start fail"); } //set device in station mode if(retval != ROLE_STA) { if(retval == ROLE_AP) { //we need to wait for an event before doing anything while(!IS_IP_ACQUIRED(wifi_state.status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //change mode to Station retval = sl_WlanSetMode(ROLE_STA); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "WLAN mode fail"); } //restart retval = sl_Stop(0xFF); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL stop fail"); } retval = sl_Start(0,0,0); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL start fail"); } if(retval != ROLE_STA) { RETURN_ERROR(ERROR_UNKNOWN, "WLAN mode fail"); } } //get SimpleLink version config = SL_DEVICE_GENERAL_VERSION; config_len = sizeof(SlVersionFull); retval = sl_DevGet(SL_DEVICE_GENERAL_CONFIGURATION, &config, &config_len, (unsigned char*)&wifi_state.version); if(retval<0) { RETURN_ERROR(retval, "WIFI conf fail"); } //default connection policy retval = sl_WlanPolicySet(SL_POLICY_CONNECTION, SL_CONNECTION_POLICY(1, 0, 0, 0, 0), NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } //disconnect retval = sl_WlanDisconnect(); if(retval == 0) { //not yet disconnected while(IS_CONNECTED(wifi_state.status)) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } } //Enable DHCP client retval = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE,1,1,&val); if(retval<0) { RETURN_ERROR(retval, "WIFI conf fail"); } //Disable scan policy config = SL_SCAN_POLICY(0); retval = sl_WlanPolicySet(SL_POLICY_SCAN, config, NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } //Set Tx power level for station mode //Number between 0-15, as dB offset from max power - 0 will set max power val = 0; retval = sl_WlanSet(SL_WLAN_CFG_GENERAL_PARAM_ID, WLAN_GENERAL_PARAM_OPT_STA_TX_POWER, 1, (unsigned char *)&val); if(retval<0) { RETURN_ERROR(retval, "WIFI set fail"); } // Set PM policy to normal retval = sl_WlanPolicySet(SL_POLICY_PM , SL_NORMAL_POLICY, NULL, 0); if(retval<0) { RETURN_ERROR(retval, "WIFI policy fail"); } // Unregister mDNS services retval = sl_NetAppMDNSUnRegisterService(0, 0); if(retval<0) { RETURN_ERROR(retval, "mDNS fail"); } //Set mDNS device hostname char hostname[64]; char macstring[20]; unsigned char maclen = SL_MAC_ADDR_LEN; retval = sl_NetCfgGet(SL_MAC_ADDRESS_GET, NULL, &maclen, wifi_state.mac); if(retval < 0) { RETURN_ERROR(retval, "WIFI conf fail"); } snprintf(macstring, 20, "%02X%02X%02X%02X%02X%02X", wifi_state.mac[0], wifi_state.mac[1], wifi_state.mac[2], wifi_state.mac[3], wifi_state.mac[4], wifi_state.mac[5]); snprintf(hostname, 64, "LeashDebugger%s", macstring); retval = sl_NetAppSet (SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, strlen((const char *)hostname), (unsigned char *) hostname); // Remove all 64 filters (8*8) memset(filter_mask.FilterIdMask, 0xFF, 8); retval = sl_WlanRxFilterSet(SL_REMOVE_RX_FILTER, (uint8_t*)&filter_mask, sizeof(_WlanRxFilterOperationCommandBuff_t)); if(retval<0) { RETURN_ERROR(retval, "WIFI filter fail"); } retval = sl_Stop(SL_STOP_TIMEOUT); if(retval < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL stop fail"); } wifi_state.status = 0; return RET_SUCCESS; }