//**************************************************************************** // //! read the current configuration and displays the same. //! //! \param iMode is the current mode of the device //! This function //! 1. display the current networking mode and also dislays the ssid name //! in cas eof AP mode. //! //! \return none. // //**************************************************************************** void DisplayCurrentConfig(int iMode) { char pcSsidName[32]; unsigned short len; unsigned short config_opt; if(iMode == ROLE_STA) { UART_PRINT("Device is in STA mode\n\r"); } else if(iMode == ROLE_AP) { UART_PRINT("Device is in AP mode\n\r"); while(!g_usIpObtained) { _SlNonOsMainLoopTask(); } len = 32; config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char*) pcSsidName); UART_PRINT("AP name: %s\n\r",pcSsidName); } else if(iMode == ROLE_P2P) { UART_PRINT("Device is in P2P mode\n\r"); } else { UART_PRINT("Could not retreive mode\n\r"); } }
//***************************************************************************** //! getSsidName //! //! Returns the SSID Name for the device when in Access Point Mode //! //**************************************************************************** char * getSsidName() { static char ssidName[32]; unsigned short len = 32; unsigned short config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char *)ssidName); return (char *)ssidName; }
//--tested, working--// char* WiFiClass::SSID() { if (!_initialized) { init(); } // //connected_ssid maintained by wlan event handler (SimpleLinkCallbacks.cpp) //when in station mode. For AP mode use sl_WlanGet to obtain the SSID. // if(role == ROLE_STA) return (char*)WiFiClass::connected_ssid; char ssid[32]; unsigned short len = 32; unsigned short config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char*)ssid); strcpy(string_output_buffer, ssid); return string_output_buffer; }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return None // //**************************************************************************** void ConnectToNetwork() { char ucAPSSID[32]; unsigned short len, config_opt; // staring simplelink g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); unsigned char macAddressVal[SL_MAC_ADDR_LEN]; unsigned char macAddressLen = SL_MAC_ADDR_LEN; sl_NetCfgGet(SL_MAC_ADDRESS_GET,NULL,&macAddressLen,(unsigned char *)macAddressVal); Report("CC3200 LaunchPad MAC Address: %.2x:%.2x:%.2x:%.2x:%.2x:%.2x\n\r", macAddressVal[0],macAddressVal[1],macAddressVal[2],macAddressVal[3],macAddressVal[4],macAddressVal[5]); // Device is in AP Mode and Force AP Jumper is not Connected if(((g_uiSimplelinkRole == ROLE_AP) || (g_uiSimplelinkRole == ROLE_P2P)) && g_uiDeviceModeConfig == ROLE_STA ) { //Switch to STA Mode sl_WlanSetMode(ROLE_STA); sl_Stop(SL_STOP_TIMEOUT); g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //Device is in STA Mode and Force AP Jumper is Connected if(((g_uiSimplelinkRole == ROLE_STA) || (g_uiSimplelinkRole == ROLE_P2P)) && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode sl_WlanSetMode(ROLE_AP); sl_Stop(SL_STOP_TIMEOUT); g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //No Mode Change Required if(g_uiSimplelinkRole == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server while (!(g_usMCNetworkUstate & MCU_IP_ALLOC)) { } char iCount=0; //Read the AP SSID memset(ucAPSSID,'\0',AP_SSID_LEN_MAX); len = AP_SSID_LEN_MAX; config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char*) ucAPSSID); //Blink LED 3 times to Indicate AP Mode for(iCount=0;iCount<3;iCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } } else { //waiting for the device to Auto Connect while (((!(g_usMCNetworkUstate & MCU_AP_ASSOC)) || !(g_usMCNetworkUstate & MCU_IP_ALLOC))&& g_ucConnectTimeout < AUTO_CONNECTION_TIMEOUT_COUNT) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); g_ucConnectTimeout++; } //Couldn't connect Using Auto Profile if(g_ucConnectTimeout == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); g_ucConnectTimeout &= ~MCU_AP_ASSOC; g_ucConnectTimeout &= ~MCU_IP_ALLOC; //Connect Using Smart Config SmartConfigConnect(); //Waiting for the device to Auto Connect while (!(g_usMCNetworkUstate & MCU_AP_ASSOC) || !(g_usMCNetworkUstate & MCU_IP_ALLOC)) { MAP_UtilsDelay(500); } } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); g_iInternetAccess = ConnectionTest(); if (g_iInternetAccess == 0) { Report("Successful connection to the Internet\r\n"); osi_SyncObjSignal(&semaphore_Connected); //signal "Connected" semaphore so mqtt task can continue } else { Report("Could not obtain connection to the Internet\r\n"); } } }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return 0 - Success //! -1 - Failure // //**************************************************************************** long ConnectToNetwork() { long lRetVal = -1; unsigned int uiConnectTimeoutCnt =0; // staring simplelink lRetVal = sl_Start(NULL,NULL,NULL); ASSERT_ON_ERROR( lRetVal); // Device is in AP Mode and Force AP Jumper is not Connected if(ROLE_STA != lRetVal && g_uiDeviceModeConfig == ROLE_STA ) { if (ROLE_AP == lRetVal) { // 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 Mode lRetVal = ConfigureMode(ROLE_STA); ASSERT_ON_ERROR( lRetVal); } //Device is in STA Mode and Force AP Jumper is Connected if(ROLE_AP != lRetVal && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode lRetVal = ConfigureMode(ROLE_AP); ASSERT_ON_ERROR( lRetVal); } //No Mode Change Required if(lRetVal == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server // 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 } //Stop Internal HTTP Server lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); char cCount=0; //Blink LED 3 times to Indicate AP Mode for(cCount=0;cCount<3;cCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } char ssid[32]; unsigned short len = 32; unsigned short config_opt = WLAN_AP_OPT_SSID; sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char* )ssid); UART_PRINT("\n\r Connect to : \'%s\'\n\r\n\r",ssid); } else { //Stop Internal HTTP Server lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //waiting for the device to Auto Connect while(uiConnectTimeoutCnt<AUTO_CONNECTION_TIMEOUT_COUNT && ((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus)))) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); uiConnectTimeoutCnt++; } //Couldn't connect Using Auto Profile if(uiConnectTimeoutCnt == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); CLR_STATUS_BIT_ALL(g_ulStatus); //Connect Using Smart Config lRetVal = SmartConfigConnect(); ASSERT_ON_ERROR(lRetVal); //Waiting for the device to Auto Connect while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { MAP_UtilsDelay(500); } } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); g_iInternetAccess = ConnectionTest(); } return SUCCESS; }
//**************************************************************************** // //! \brief Connects to the Network in AP or STA Mode - If ForceAP Jumper is //! Placed, Force it to AP mode //! //! \return 0 on success else error code // //**************************************************************************** long ConnectToNetwork() { char ucAPSSID[32]; unsigned short len, config_opt; long lRetVal = -1; // staring simplelink g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); // Device is not in STA mode and Force AP Jumper is not Connected //- Switch to STA mode if(g_uiSimplelinkRole != ROLE_STA && g_uiDeviceModeConfig == ROLE_STA ) { //Switch to STA Mode lRetVal = sl_WlanSetMode(ROLE_STA); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(SL_STOP_TIMEOUT); g_usMCNetworkUstate = 0; g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); UART_PRINT("started in STA mode\n\r"); } //Device is not in AP mode and Force AP Jumper is Connected - //Switch to AP mode if(g_uiSimplelinkRole != ROLE_AP && g_uiDeviceModeConfig == ROLE_AP ) { //Switch to AP Mode lRetVal = sl_WlanSetMode(ROLE_AP); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(SL_STOP_TIMEOUT); g_usMCNetworkUstate = 0; g_uiSimplelinkRole = sl_Start(NULL,NULL,NULL); } //No Mode Change Required if(g_uiSimplelinkRole == ROLE_AP) { //waiting for the AP to acquire IP address from Internal DHCP Server while(!IS_IP_ACQUIRED(g_ulStatus)) { } //Stop Internal HTTP Server lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); char iCount=0; //Read the AP SSID memset(ucAPSSID,'\0',AP_SSID_LEN_MAX); len = AP_SSID_LEN_MAX; config_opt = WLAN_AP_OPT_SSID; lRetVal = sl_WlanGet(SL_WLAN_CFG_AP_ID, &config_opt , &len, (unsigned char*) ucAPSSID); ASSERT_ON_ERROR(lRetVal); Report("\n\rDevice is in AP Mode, Please Connect to AP [%s] and" "type [mysimplelink.net] in the browser \n\r",ucAPSSID); //Blink LED 3 times to Indicate AP Mode for(iCount=0;iCount<3;iCount++) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(400); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(400); } } else { //Stop Internal HTTP Server lRetVal = sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); //Start Internal HTTP Server lRetVal = sl_NetAppStart(SL_NET_APP_HTTP_SERVER_ID); ASSERT_ON_ERROR( lRetVal); /*//waiting for the device to Auto Connect while ( (!IS_IP_ACQUIRED(g_ulStatus))&& g_ucConnectTimeout < AUTO_CONNECTION_TIMEOUT_COUNT) { //Turn RED LED On GPIO_IF_LedOn(MCU_RED_LED_GPIO); osi_Sleep(50); //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); osi_Sleep(50); g_ucConnectTimeout++; } //Couldn't connect Using Auto Profile if(g_ucConnectTimeout == AUTO_CONNECTION_TIMEOUT_COUNT) { //Blink Red LED to Indicate Connection Error GPIO_IF_LedOn(MCU_RED_LED_GPIO); CLR_STATUS_BIT_ALL(g_ulStatus); Report("Use Smart Config Application to configure the device.\n\r"); //Connect Using Smart Config lRetVal = SmartConfigConnect(); ASSERT_ON_ERROR(lRetVal); //Waiting for the device to Auto Connect while(!IS_IP_ACQUIRED(g_ulStatus)) { MAP_UtilsDelay(500); }*/ SlSecParams_t secParams = {0}; secParams.Key = (signed char *)SECURITY_KEY; secParams.KeyLen = strlen(SECURITY_KEY); secParams.Type = SECURITY_TYPE; lRetVal = sl_WlanConnect((signed char *)SSID_NAME, strlen((const char *)SSID_NAME), 0, &secParams, 0); ASSERT_ON_ERROR(lRetVal); // Wait for WLAN Event while((!IS_CONNECTED(g_ulStatus)) || (!IS_IP_ACQUIRED(g_ulStatus))) { //Turn Green LED On GPIO_IF_LedOn(MCU_GREEN_LED_GPIO); osi_Sleep(50); //Turn Green LED Off GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); osi_Sleep(50); } //Turn Green LED Off GPIO_IF_LedOff(MCU_GREEN_LED_GPIO); UART_PRINT("connected\n\r"); } //Turn RED LED Off GPIO_IF_LedOff(MCU_RED_LED_GPIO); UART_PRINT("\n\rDevice is in STA Mode, Connect to the AP[%s] and type" "IP address [%d.%d.%d.%d] in the browser \n\r",g_ucConnectionSSID, SL_IPV4_BYTE(g_uiIpAddress,3),SL_IPV4_BYTE(g_uiIpAddress,2), SL_IPV4_BYTE(g_uiIpAddress,1),SL_IPV4_BYTE(g_uiIpAddress,0)); return SUCCESS; }