//**************************************************************************** // //! Confgiures the mode in which the device will work //! //! \param iMode is the current mode of the device //! //! This function //! 1. prompt user for desired configuration and accordingly configure the //! networking mode(STA or AP). //! 2. also give the user the option to configure the ssid name in case of //! AP mode. //! //! \return 0: success, -ve: failure. // //**************************************************************************** long ConfigureMode(int iMode) { char cCharacter = 'a'; char pcSsidName[33]; unsigned short len; long lRetVal = -1; while(cCharacter != '1' && cCharacter != '2' && cCharacter != '3') { UART_PRINT("Do you Want to Switch Mode\n\r1. STA mode\n\r2. AP Mode\n\r" "3. P2P Mode :"); cCharacter = MAP_UARTCharGet(CONSOLE); MAP_UARTCharPut(CONSOLE,cCharacter); MAP_UARTCharPut(CONSOLE,'\n'); MAP_UARTCharPut(CONSOLE,'\r'); } if(cCharacter == '1') { lRetVal = sl_WlanSetMode(ROLE_STA); ASSERT_ON_ERROR(lRetVal); UART_PRINT("mode configured\n\r"); } else if(cCharacter == '2') { UART_PRINT("Enter the SSID name: "); GetSsidName(pcSsidName,33); _SlNonOsMainLoopTask(); lRetVal = sl_WlanSetMode(ROLE_AP); ASSERT_ON_ERROR(lRetVal); len = strlen(pcSsidName); lRetVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, len, (unsigned char*) pcSsidName); ASSERT_ON_ERROR(lRetVal); UART_PRINT("mode configured\n\r"); } else if(cCharacter == '3') { lRetVal = sl_WlanSetMode(ROLE_P2P); ASSERT_ON_ERROR(lRetVal); UART_PRINT("mode configured\n\r"); } else { UART_PRINT("Wrong input\n\r"); } return 0; }
//**************************************************************************** // //! Confgiures the mode in which the device will work //! //! \param iMode is the current mode of the device //! //! This function //! 1. prompt user for desired configuration and accordingly configure the //! networking mode(STA or AP). //! 2. also give the user the option to configure the ssid name in case of //! AP mode. //! //! \return sl_start return value(int). // //**************************************************************************** static int ConfigureMode(int iMode) { char pcSsidName[33]; long retVal = -1; UART_PRINT("Enter the AP SSID name: "); GetSsidName(pcSsidName,33); retVal = sl_WlanSetMode(ROLE_AP); ASSERT_ON_ERROR(__LINE__, retVal); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(pcSsidName), (unsigned char*)pcSsidName); ASSERT_ON_ERROR(__LINE__, retVal); UART_PRINT("Device is configured in AP mode\n\r"); /* Restart Network processor */ retVal = sl_Stop(SL_STOP_TIMEOUT); ASSERT_ON_ERROR(__LINE__, retVal); // reset status bits CLR_STATUS_BIT_ALL(g_ulStatus); return sl_Start(NULL,NULL,NULL); }
int WiFiClass::beginNetwork(char *ssid, char *passphrase) { long retVal = -1; int i; if (!_initialized) { init(); } // Initialize the AP-mode Connected Device array WiFiClass::_connectedDeviceCount = 0; _latestConnect = 0; for (i = 0; i < MAX_AP_DEVICE_REGISTRY; i++) { _connectedDevices[i].in_use = false; memset((uint8_t *)_connectedDevices[i].ipAddress, 0, 4); memset((uint8_t *)_connectedDevices[i].mac, 0, 6); } retVal = sl_WlanSetMode(ROLE_AP); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid), (unsigned char *)ssid); unsigned char val = SL_SEC_TYPE_WPA; retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (unsigned char *)&val); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(passphrase), (unsigned char *)passphrase); /* Restart Network processor */ retVal = sl_Stop(30); role = ROLE_AP; return (retVal == 0 ? sl_Start(NULL, NULL, NULL) : retVal); }
int WiFiClass::beginNetwork(char *ssid, char *passphrase) { long retVal = -1; if (!_initialized) { init(); } retVal = sl_WlanSetMode(ROLE_AP); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid), (unsigned char *)ssid); unsigned char val = SL_SEC_TYPE_WPA; retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (unsigned char *)&val); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(passphrase), (unsigned char *)passphrase); /* Restart Network processor */ retVal = sl_Stop(30); role = ROLE_AP; return sl_Start(NULL,NULL,NULL); }
//***************************************************************************** // //! Check the device mode and switch to P2P mode //! restart the NWP to activate P2P mode //! //! \param None //! //! \return status code - Success:0, Failure:-ve // //***************************************************************************** long StartDeviceInP2P() { long lRetVal = -1; // Reset CC3200 Network State Machine 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 application // // 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(); } 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(NULL,NULL,NULL); ASSERT_ON_ERROR(lRetVal); if(lRetVal != ROLE_P2P) { lRetVal = sl_WlanSetMode(ROLE_P2P); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(0xFF); // reset the Status bits CLR_STATUS_BIT_ALL(g_ulStatus); lRetVal = sl_Start(NULL,NULL,NULL); if(lRetVal < 0 || lRetVal != ROLE_P2P) { ASSERT_ON_ERROR(P2P_MODE_START_FAILED); } else { UART_PRINT("Started SimpleLink Device: P2P Mode\n\r"); return SUCCESS; } } return SUCCESS; }
static unsigned long cc3200helpers_startIn(SlWlanMode_t eWlanMode) { int iMode = 0; unsigned long dwResult = 0; s_flWLANConnected = 0; s_flAcquiredIP = 0; SlWlanMode_t s_eRequestedWlanMode = eWlanMode; dwResult = cc3200helpers_start(&iMode); if ( dwResult ) { return dwResult; } if ( eWlanMode == iMode ) { return cc3200Helpers_OK; } if (ROLE_AP == iMode) { // If the device is in AP mode, we need to wait for this event // before doing anything TRACE("Waiting for AP to initialize\n\r"); while(!s_flAcquiredIP) { #ifndef SL_PLATFORM_MULTI_THREADED _SlNonOsMainLoopTask(); #endif } s_flAcquiredIP = 0; TRACE("AP ready\n\r"); } if ( sl_WlanSetMode(eWlanMode) ) { return cc3200Helpers_SwitchFailed | cc3200Helpers_SetModeFailed; } if ( sl_Stop(0) ) { return cc3200Helpers_SwitchFailed | cc3200Helpers_StopFailed; } dwResult = cc3200helpers_start(&iMode); if ( dwResult ) { return cc3200Helpers_SwitchFailed | dwResult; } if ( iMode != eWlanMode ) { return cc3200Helpers_SwitchFailed; } return cc3200Helpers_OK; }
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; }
bool wifi_setup_ap(const char *ssid, const char *pass, int channel) { uint8_t v; if (sl_WlanSetMode(ROLE_AP) != 0) { return false; } if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid), (const uint8_t *) ssid) != 0) { return false; } v = strlen(pass) > 0 ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN; if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, &v) != 0) { return false; } if (v == SL_SEC_TYPE_WPA && sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(pass), (const uint8_t *) pass) != 0) { return false; } v = channel; if (sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, &v) != 0) { return false; } sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID); { SlNetCfgIpV4Args_t ipcfg; memset(&ipcfg, 0, sizeof(ipcfg)); if (!inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4) || !inet_pton(AF_INET, "255.255.255.0", &ipcfg.ipV4Mask) || /* This means "disable". 0.0.0.0 won't do. */ !inet_pton(AF_INET, "255.255.255.255", &ipcfg.ipV4DnsServer) || /* We'd like to disable gateway too, but DHCP server refuses to start. */ !inet_pton(AF_INET, "192.168.4.1", &ipcfg.ipV4Gateway) || sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(ipcfg), (uint8_t *) &ipcfg) != 0) { return false; } } { SlNetAppDhcpServerBasicOpt_t dhcpcfg; memset(&dhcpcfg, 0, sizeof(dhcpcfg)); dhcpcfg.lease_time = 900; if (!inet_pton(AF_INET, "192.168.4.20", &dhcpcfg.ipv4_addr_start) || !inet_pton(AF_INET, "192.168.4.200", &dhcpcfg.ipv4_addr_last) || sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, sizeof(dhcpcfg), (uint8_t *) &dhcpcfg) != 0) { return false; } } sl_Stop(0); sl_Start(NULL, NULL, NULL); if (sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID) != 0) { LOG(LL_ERROR, ("DHCP server failed to start")); return false; } LOG(LL_INFO, ("WiFi: AP %s configured", ssid)); return true; }
static int ensure_role_sta() { if (s_current_role == ROLE_STA) return 1; if (sl_WlanSetMode(ROLE_STA) != 0) return 0; if (!restart_nwp()) return 0; _u32 scan_interval = WIFI_SCAN_INTERVAL_SECONDS; sl_WlanPolicySet(SL_POLICY_SCAN, 1 /* enable */, (_u8 *) &scan_interval, sizeof(scan_interval)); return 1; }
//--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; }
//***************************************************************************** // //! Check the device mode and switch to STATION(STA) mode //! restart the NWP to activate STATION mode //! //! \param iMode (device mode) //! //! \return None // //***************************************************************************** void SwitchToStaMode(int iMode) { if(iMode != ROLE_STA) { sl_WlanSetMode(ROLE_STA); MAP_UtilsDelay(80000); sl_Stop(10); MAP_UtilsDelay(80000); sl_Start(0,0,0); } }
//**************************************************************************** // //! Confgiures the mode in which the device will work //! //! \param iMode is the current mode of the device //! //! //! \return SlWlanMode_t //! // //**************************************************************************** static int ConfigureMode(int iMode) { long lRetVal = -1; lRetVal = sl_WlanSetMode(iMode); ASSERT_ON_ERROR(lRetVal); /* Restart Network processor */ lRetVal = sl_Stop(SL_STOP_TIMEOUT); // reset status bits CLR_STATUS_BIT_ALL(g_ulStatus); return sl_Start(NULL,NULL,NULL); }
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); }
int WiFiClass::beginNetwork(char *ssid) { long retVal = -1; if (!_initialized) { init(); } retVal = sl_WlanSetMode(ROLE_AP); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid), (unsigned char *)ssid); /* Restart Network processor */ retVal = sl_Stop(30); role = ROLE_AP; return sl_Start(NULL,NULL,NULL); }
static int WifiSetModeAP() { long retval; if((retval = sl_WlanSetMode(ROLE_AP)) < 0) { RETURN_ERROR(ERROR_UNKNOWN, "WLAN mode fail"); } if((retval = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(WIFI_AP_SSID), (unsigned char*)WIFI_AP_SSID)) < 0) { RETURN_ERROR((int)retval, "Wifi: AP Name Set Failed"); } //restart if((retval = sl_Stop(SL_STOP_TIMEOUT)) < 0) { RETURN_ERROR(ERROR_UNKNOWN, "SL stop fail"); } CLR_STATUS_BIT_ALL(wifi_state.status); return sl_Start(0,0,0); }
int sj_wifi_connect() { int ret; SlSecParams_t sp; if (sl_WlanSetMode(ROLE_STA) != 0) { return 0; } /* Turning the device off and on for the role change to take effect. */ sl_Stop(0); sl_Start(NULL, NULL, NULL); sp.Key = (_i8 *) s_wifi_sta_config.pass; sp.KeyLen = strlen(s_wifi_sta_config.pass); sp.Type = sp.KeyLen ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN; ret = sl_WlanConnect((const _i8 *) s_wifi_sta_config.ssid, strlen(s_wifi_sta_config.ssid), 0, &sp, 0); if (ret != 0) { fprintf(stderr, "WlanConnect error: %d\n", ret); return 0; } return 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 //! - 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 }
/*! * \brief Start the device in STA mode * * Following function configures 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 application * * Note that all profiles and persistent settings that were done on the * device will be lost */ _i32 startAP(void){ SlPingStartCommand_t PingParams = {0}; SlPingReport_t Report = {0}; _u8 SecType = 0; _i32 mode = ROLE_STA; _i32 retVal = configureSimpleLinkToDefaultState(); if(retVal < 0) { if (DEVICE_NOT_IN_STATION_MODE == retVal) CLI_Write((_u8 *)" Failed to configure the device in its default state \n\r"); LOOP_FOREVER(); } CLI_Write((_u8 *)" 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 */ mode = sl_Start(0, 0, 0); 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)) { _SlNonOsMainLoopTask(); } } else { /* Configure CC3100 to start in AP mode */ retVal = sl_WlanSetMode(ROLE_AP); if(retVal < 0) LOOP_FOREVER(); /* Configure the SSID of the CC3100 */ retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, pal_Strlen(SSID_AP_MODE), (_u8 *)SSID_AP_MODE); if(retVal < 0) LOOP_FOREVER(); SecType = SEC_TYPE_AP_MODE; /* Configure the Security parameter the AP mode */ retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, (_u8 *)&SecType); if(retVal < 0) LOOP_FOREVER(); retVal = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, pal_Strlen(PASSWORD_AP_MODE), (_u8 *)PASSWORD_AP_MODE); if(retVal < 0) LOOP_FOREVER(); retVal = sl_Stop(SL_STOP_TIMEOUT); if(retVal < 0) LOOP_FOREVER(); CLR_STATUS_BIT(g_Status, STATUS_BIT_IP_ACQUIRED); mode = sl_Start(0, 0, 0); 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)) { _SlNonOsMainLoopTask(); } } else { CLI_Write((_u8 *)" Device couldn't be configured in AP mode \n\r"); LOOP_FOREVER(); } } CLI_Write((_u8 *)" Device started as Access Point\n\r"); /* Wait */ CLI_Write((_u8 *)" Waiting for clients to connect...!\n\r"); while((!IS_IP_LEASED(g_Status)) || (!IS_STA_CONNECTED(g_Status))) { _SlNonOsMainLoopTask(); } CLI_Write((_u8 *)" Client connected to the device \n\r"); CLI_Write((_u8 *)" Pinging...! \n\r"); /* Set the ping parameters */ PingParams.PingIntervalTime = PING_INTERVAL; PingParams.PingSize = PING_PKT_SIZE; PingParams.PingRequestTimeout = PING_TIMEOUT; PingParams.TotalNumberOfAttempts = NO_OF_ATTEMPTS; PingParams.Flags = 0; PingParams.Ip = g_StationIP; /* Fill the station IP address connected to CC3100 */ /* Ping client connected to CC3100 */ retVal = sl_NetAppPingStart((SlPingStartCommand_t*)&PingParams, SL_AF_INET, (SlPingReport_t*)&Report, SimpleLinkPingReport); if(retVal < 0) LOOP_FOREVER(); /* Wait */ // while(!IS_PING_DONE(g_Status)) { _SlNonOsMainLoopTask(); } // // if (0 == g_PingPacketsRecv) // { // CLI_Write((_u8 *)" A STATION couldn't connect to the device \n\r"); // ASSERT_ON_ERROR(LAN_CONNECTION_FAILED); // } CLI_Write((_u8 *)" Device and the station are successfully connected \n\r"); return SUCCESS; }
STATIC void wlan_set_mode (uint mode) { wlan_obj.mode = mode; ASSERT_ON_ERROR(sl_WlanSetMode(mode)); }
int sj_wifi_setup_ap(const struct sys_config_wifi_ap *cfg) { int ret; uint8_t v; SlNetCfgIpV4Args_t ipcfg; SlNetAppDhcpServerBasicOpt_t dhcpcfg; if ((ret = sl_WlanSetMode(ROLE_AP)) != 0) { fprintf(stderr, "sl_WlanSetMode: %d\n", ret); return 0; } if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(cfg->ssid), (const uint8_t *) cfg->ssid)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_SSID): %d\n", ret); return 0; } v = strlen(cfg->pass) > 0 ? SL_SEC_TYPE_WPA : SL_SEC_TYPE_OPEN; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SECURITY_TYPE, 1, &v)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_SECURITY_TYPE): %d\n", ret); return 0; } if (v == SL_SEC_TYPE_WPA && (ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_PASSWORD, strlen(cfg->pass), (const uint8_t *) cfg->pass)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_PASSWORD): %d\n", ret); return 0; } v = cfg->channel; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, (uint8_t *) &v)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_CHANNEL): %d\n", ret); return 0; } v = cfg->hidden; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_HIDDEN_SSID, 1, (uint8_t *) &v)) != 0) { fprintf(stderr, "sl_WlanSet(WLAN_AP_OPT_HIDDEN_SSID): %d\n", ret); return 0; } memset(&ipcfg, 0, sizeof(ipcfg)); if (!inet_pton(AF_INET, cfg->ip, &ipcfg.ipV4) || !inet_pton(AF_INET, cfg->netmask, &ipcfg.ipV4Mask) || !inet_pton(AF_INET, cfg->gw, &ipcfg.ipV4Gateway) || !inet_pton(AF_INET, cfg->gw, &ipcfg.ipV4DnsServer) || (ret = sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(ipcfg), (uint8_t *) &ipcfg)) != 0) { fprintf(stderr, "sl_NetCfgSet(IPCONFIG_MODE_ENABLE_IPV4): %d\n", ret); return 0; } memset(&dhcpcfg, 0, sizeof(dhcpcfg)); dhcpcfg.lease_time = 900; if (!inet_pton(AF_INET, cfg->dhcp_start, &dhcpcfg.ipv4_addr_start) || !inet_pton(AF_INET, cfg->dhcp_end, &dhcpcfg.ipv4_addr_last) || (ret = sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, sizeof(dhcpcfg), (uint8_t *) &dhcpcfg)) != 0) { fprintf(stderr, "sl_NetCfgSet(NETAPP_SET_DHCP_SRV_BASIC_OPT): %d\n", ret); return 0; } /* We don't need TI's web server. */ sl_NetAppStop(SL_NET_APP_HTTP_SERVER_ID); /* Turning the device off and on for the change to take effect. */ sl_Stop(0); sl_Start(NULL, NULL, NULL); osi_Sleep(100); fprintf(stderr, "AP %s configured\n", cfg->ssid); return 1; }
int main() { long lRetVal = -1; // // Initialize Board configurations // BoardInit(); // // Pinmuxing for GPIO, UART // PinMuxConfig(); // // configure LEDs // GPIO_IF_LedConfigure(LED1|LED2|LED3); // off all LEDs GPIO_IF_LedOff(MCU_ALL_LED_IND); #ifndef NOTERM // // Configuring UART // InitTerm(); #endif // // Display the Application Banner // DisplayBanner(APP_NAME); UART_PRINT("Scan Wi-FI direct device in your handheld device\n\r"); // Initializing the CC3200 device lRetVal = StartDeviceInP2P(); if(lRetVal < 0) { UART_PRINT("Start Device in P2P mode failed \n\r"); LOOP_FOREVER(); } lRetVal = P2PConfiguration(); if(lRetVal < 0) { UART_PRINT("Setting P2P configuration failed\n\r"); LOOP_FOREVER(); } /* Connect to configure P2P device */ lRetVal = WlanConnect(); if(lRetVal == 0) { GPIO_IF_LedOn(MCU_IP_ALLOC_IND); } else { UART_PRINT("Reset the device and try again\n\r"); LOOP_FOREVER(); } if(DisplayIP() < 0) { UART_PRINT("Get IP address failed \n\r"); LOOP_FOREVER(); } UART_PRINT("Send TCP packets from your handheld device to CC3200's IP\n\r"); /*After calling this function, you can start sending data to CC3200 IP * address on PORT_NUM */ if(!(IS_CONNECT_FAILED(g_ulStatus))) { lRetVal = BsdTcpServer(PORT_NUM); if(lRetVal >= 0) { UART_PRINT("Received TCP packets successfully \n\r"); } else if(lRetVal == CLIENT_DISCONNECTED) { UART_PRINT("P2P Client disconnected \n\r"); } else { UART_PRINT("TCP packets receive failed \n\r"); } } if(lRetVal >=0) { UART_PRINT("Test passed, exiting application... \n\r"); } else { UART_PRINT("Test failed, exiting application... \n\r"); } // revert Device into STA mode and power off Network Processor lRetVal = sl_WlanSetMode(ROLE_STA); if(lRetVal < 0) { ERR_PRINT(lRetVal); LOOP_FOREVER(); } lRetVal = sl_Stop(SL_STOP_TIMEOUT); GPIO_IF_LedOff(MCU_IP_ALLOC_IND); while(1) { _SlNonOsMainLoopTask(); } }
//***************************************************************************** // //! Network_IF_InitDriver //! The function initializes a CC3200 device and triggers it to start operation //! //! \param uiMode (device mode in which device will be configured) //! //! \return 0 : sucess, -ve : failure // //***************************************************************************** long Network_IF_InitDriver(unsigned int uiMode) { long lRetVal = -1; // Reset CC3200 Network State Machine 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 application // // 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(); } 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(NULL,NULL,NULL); if (lRetVal < 0 || lRetVal != ROLE_STA) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(); } UART_PRINT("Started SimpleLink Device: STA Mode\n\r"); if(uiMode == ROLE_AP) { UART_PRINT("Switching to AP mode on application request\n\r"); // Switch to AP role and restart lRetVal = sl_WlanSetMode(uiMode); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(0xFF); lRetVal = sl_Start(0, 0, 0); ASSERT_ON_ERROR(lRetVal); // Check if the device is up in AP Mode 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(); #else osi_Sleep(1); #endif } } else { // We don't want to proceed if the device is not coming up in AP-mode ASSERT_ON_ERROR(DEVICE_NOT_IN_AP_MODE); } UART_PRINT("Re-started SimpleLink Device: AP Mode\n\r"); } else if(uiMode == ROLE_P2P) { UART_PRINT("Switching to P2P mode on application request\n\r"); // Switch to AP role and restart lRetVal = sl_WlanSetMode(uiMode); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Stop(0xFF); lRetVal = sl_Start(0, 0, 0); ASSERT_ON_ERROR(lRetVal); // Check if the device is in station again if (ROLE_P2P != lRetVal) { // We don't want to proceed if the device is not coming up in P2P-mode ASSERT_ON_ERROR(DEVICE_NOT_IN_P2P_MODE); } UART_PRINT("Re-started SimpleLink Device: P2P Mode\n\r"); } else { // Device already started in STA-Mode } return 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 */ }
int main(void) { /* Init board */ BoardInit(); /* Init GPIO */ InitGPIO(); /* Init UART */ MAP_PRCMPeripheralClkEnable(PRCM_UARTA0, PRCM_RUN_MODE_CLK); InitTerm(); /* Init I2C */ I2C_IF_Open(I2C_MASTER_MODE_FST); /* Init the internet! */ // http://azug.minpet.unibas.ch/~lukas/bricol/ti_simplelink/resources/swru368.pdf SECTION 10 dhcpParams.lease_time = 1000; dhcpParams.ipv4_addr_start = 0xc0a80102; dhcpParams.ipv4_addr_last = 0xc0a801fe; sl_Start(NULL,NULL,NULL); MAP_UtilsDelay(8000000); // config IP etc ipV4.ipV4 = 0xc0a80101; ipV4.ipV4Mask = 0xFFFFFF00; ipV4.ipV4Gateway = 0xc0a80101; ipV4.ipV4DnsServer = 0xc0a80101; sl_NetCfgSet(SL_IPV4_AP_P2P_GO_STATIC_ENABLE, 1 ,sizeof(SlNetCfgIpV4Args_t), (unsigned char*) &ipV4); sl_WlanSetMode(ROLE_AP); // config SSID sl_WlanSet(SL_WLAN_CFG_AP_ID,WLAN_AP_OPT_SSID, strlen(myssid), (unsigned char*) myssid); sl_Stop(100); sl_Start(NULL,NULL,NULL); // start DHCP server sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID); sl_NetAppSet(SL_NET_APP_DHCP_SERVER_ID, NETAPP_SET_DHCP_SRV_BASIC_OPT, outLen,(unsigned char*) &dhcpParams); sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID); //Stop Internal HTTP Serve long lRetVal = -1; 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); /* Finished init the internet! */ setRLED(); /* Wait for operator */ while(!readDIP1()); clearRLED(); /* Init IMU (alongside timers and interrupt */ imu_setup(); /* Init odometers */ odometer_setup(); set_controller_parameters(kp, ki, kd); set__odo_controller_parameters(kp_odo, ki_odo, kd_odo); /* Init motors (alongside motor GPIO, timers and interrupt */ motorSetup(); controller_setup(); odometer_controller_setup(); // MUST be the last init called! while(1) { _SlNonOsMainLoopTask(); } }
int main() { unsigned char ucP2PParam[4]; long lRetVal = -1; // // Initialize Board configurations // BoardInit(); // // Pinmuxing for GPIO, UART // PinMuxConfig(); // // configure LEDs // GPIO_IF_LedConfigure(LED1|LED2|LED3); // off all LEDs GPIO_IF_LedOff(MCU_ALL_LED_IND); #ifndef NOTERM // // Configuring UART // InitTerm(); #endif // // Display the Application Banner // DisplayBanner(APP_NAME); UART_PRINT("Scan Wi-FI direct device in your handheld device\n\r"); // Initializing the CC3200 device lRetVal = StartDeviceInP2P(); if(lRetVal < 0) { LOOP_FOREVER(__LINE__); } // Set any p2p option (SL_CONNECTION_POLICY(0,0,0,any_p2p,0)) to connect to // first available p2p device sl_WlanPolicySet(SL_POLICY_CONNECTION,SL_CONNECTION_POLICY(1,0,0,0,0),NULL,0); // Set the negotiation role (SL_P2P_ROLE_NEGOTIATE). // CC3200 will negotiate with remote device GO/client mode. // Other valid options are: // - SL_P2P_ROLE_GROUP_OWNER // - SL_P2P_ROLE_CLIENT sl_WlanPolicySet(SL_POLICY_P2P, SL_P2P_POLICY(SL_P2P_ROLE_NEGOTIATE, SL_P2P_NEG_INITIATOR_ACTIVE),NULL,0); // Set P2P Device name sl_NetAppSet(SL_NET_APP_DEVICE_CONFIG_ID, NETAPP_SET_GET_DEV_CONF_OPT_DEVICE_URN, strlen(P2P_DEVICE_NAME), (unsigned char *)P2P_DEVICE_NAME); // Set P2P device type sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, WLAN_P2P_OPT_DEV_TYPE, strlen(P2P_CONFIG_VALUE), (unsigned char*)P2P_CONFIG_VALUE); // setting P2P channel parameters ucP2PParam[0] = LISENING_CHANNEL; ucP2PParam[1] = REGULATORY_CLASS; ucP2PParam[2] = OPERATING_CHANNEL; ucP2PParam[3] = REGULATORY_CLASS; // Set P2P Device listen and open channel valid channels are 1/6/11 sl_WlanSet(SL_WLAN_CFG_P2P_PARAM_ID, WLAN_P2P_OPT_CHANNEL_N_REGS, sizeof(ucP2PParam), ucP2PParam); // Restart as P2P device sl_Stop(SL_STOP_TIMEOUT); lRetVal = sl_Start(NULL,NULL,NULL); if(lRetVal < 0 || lRetVal != ROLE_P2P) { UART_PRINT("Failed to start the device \n\r"); LOOP_FOREVER(__LINE__); } else { UART_PRINT("Connect to %s \n\r",P2P_DEVICE_NAME); } /* Connect to configure P2P device */ lRetVal = WlanConnect(); if(lRetVal == 0) { GPIO_IF_LedOn(MCU_IP_ALLOC_IND); } else { UART_PRINT("Reset the device and try again\n\r"); LOOP_FOREVER(__LINE__); } DisplayIP(); UART_PRINT("Send TCP packets from your handheld device to CC3200's IP\n\r"); /*After calling this function, you can start sending data to CC3200 IP * address on PORT_NUM */ if(!(IS_CONNECT_FAILED(g_ulStatus))) BsdTcpServer(PORT_NUM); UART_PRINT("Received TCP packets successfully \n\r"); // revert Device into STA mode and power off Network Processor sl_WlanSetMode(ROLE_STA); sl_Stop(SL_STOP_TIMEOUT); UART_PRINT("Test passed, exiting application... \n\r"); while(1) { _SlNonOsMainLoopTask(); } }
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; }
/*! \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 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"); } } }