int miot_wifi_connect(void) { int ret; SlSecParams_t sp; if (!ensure_role_sta()) return 0; if (s_wifi_sta_config.static_ip.ipV4 != 0) { ret = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_STATIC_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(s_wifi_sta_config.static_ip), (unsigned char *) &s_wifi_sta_config.static_ip); } else { _u8 val = 1; ret = sl_NetCfgSet(SL_IPV4_STA_P2P_CL_DHCP_ENABLE, IPCONFIG_MODE_ENABLE_IPV4, sizeof(val), &val); } if (ret != 0) return 0; /* Turning the device off and on for the role change to take effect. */ if (!restart_nwp()) return 0; 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) return 0; sl_WlanRxStatStart(); LOG(LL_INFO, ("Connecting to %s", s_wifi_sta_config.ssid)); 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; }
/*! \brief Entering raw Transmitter\Receiver mode in order to perform Rx statistic over a specific WLAN channel \param[in] Channel number on which the statistics will be calculated \return 0 for success, -ve otherwise \note \warning We must be disconnected from WLAN AP in order to succeed changing to Receiver mode */ static _i32 RxStatisticsCollect(_i16 channel) { SlGetRxStatResponse_t rxStatResp; _u8 buffer[MAX_BUF_RX_STAT] = {'\0'}; _u8 var[MAX_BUF_SIZE] = {'\0'}; _i32 idx = -1; _i16 sd = -1; _i32 retVal = -1; memset(&rxStatResp, 0, sizeof(rxStatResp)); sd = sl_Socket(SL_AF_RF,SL_SOCK_RAW,channel); if(sd < 0) { printf("Error In Creating the Socket\n"); ASSERT_ON_ERROR(sd); } retVal = sl_Recv(sd, buffer, BYTES_TO_RECV, 0); ASSERT_ON_ERROR(retVal); printf("Press \"Enter\" to start collecting statistics.\n"); fgets((char *)var, sizeof(var), stdin); retVal = sl_WlanRxStatStart(); ASSERT_ON_ERROR(retVal); printf("Press \"Enter\" to get the statistics.\n"); fgets((char *)var, sizeof(var), stdin); retVal = sl_WlanRxStatGet(&rxStatResp, 0); ASSERT_ON_ERROR(retVal); printf("\n\n*********************Rx Statistics**********************\n\n"); printf("Received Packets - %d\n",rxStatResp.ReceivedValidPacketsNumber); printf(" Received FCS - %d\n",rxStatResp.ReceivedFcsErrorPacketsNumber); printf(" Received PLCP - %d\n",rxStatResp.ReceivedPlcpErrorPacketsNumber); printf("Average Rssi for management: %d Average Rssi for other packets: %d\n", rxStatResp.AvarageMgMntRssi,rxStatResp.AvarageDataCtrlRssi); for(idx = 0 ; idx < SIZE_OF_RSSI_HISTOGRAM ; idx++) { printf("Rssi Histogram cell %d is %d\n", idx, rxStatResp.RssiHistogram[idx]); } printf("\n"); for(idx = 0 ; idx < NUM_OF_RATE_INDEXES; idx++) { printf("Rate Histogram cell %d is %d\n", idx, rxStatResp.RateHistogram[idx]); } printf("The data was sampled in %u microseconds.\n", ((_i16)rxStatResp.GetTimeStamp - rxStatResp.StartTimeStamp)); printf("\n\n*******************End Rx Statistics********************\n"); retVal = sl_WlanRxStatStop(); ASSERT_ON_ERROR(retVal); retVal = sl_Close(sd); ASSERT_ON_ERROR(retVal); return SUCCESS; }
//***************************************************************************** // //! RxStatisticsCollect //! //! This function //! 1. Function for performing the statistics by listening on the //! channel given by the user. //! //! \return none // //***************************************************************************** static int RxStatisticsCollect() { int iSoc; char acBuffer[1500]; SlGetRxStatResponse_t rxStatResp; //char cChar; int iIndex; int iChannel; long lRetVal = -1; int iRightInput = 0; char acCmdStore[512]; struct SlTimeval_t timeval; timeval.tv_sec = 0; // Seconds timeval.tv_usec = 20000; // Microseconds. // //Clear all fields to defaults // rxStatResp.ReceivedValidPacketsNumber = 0; rxStatResp.ReceivedFcsErrorPacketsNumber = 0; rxStatResp.ReceivedAddressMismatchPacketsNumber = 0; rxStatResp.AvarageMgMntRssi = 0; rxStatResp.AvarageDataCtrlRssi = 0; for(iIndex = 0 ; iIndex < SIZE_OF_RSSI_HISTOGRAM ; iIndex++) { rxStatResp.RssiHistogram[iIndex] = 0; } for(iIndex = 0 ; iIndex < NUM_OF_RATE_INDEXES ; iIndex++) { rxStatResp.RateHistogram[iIndex] = 0; } rxStatResp.GetTimeStamp = 0; rxStatResp.StartTimeStamp = 0; // //Prompt the user for channel number // do { UART_PRINT("\n\rEnter the channel to listen[1-13]:"); // // Wait to receive a character over UART // lRetVal = GetCmd(acCmdStore, sizeof(acCmdStore)); if(lRetVal == 0) { // // No input. Just an enter pressed probably. Display a prompt. // UART_PRINT("\n\rEnter Valid Input."); iRightInput = 0; } else { iChannel = (int)strtoul(acCmdStore,0,10); if(iChannel <= 0 || iChannel > 13) { UART_PRINT("\n\rWrong Input"); iRightInput = 0; } else { iRightInput = 1; } } }while(!iRightInput); UART_PRINT("\n\rPress any key to start collecting statistics..."); // // Wait to receive a character over UART // MAP_UARTCharGet(CONSOLE); // //Start Rx statistics collection // lRetVal = sl_WlanRxStatStart(); ASSERT_ON_ERROR(lRetVal); // //Open Socket on the channel to listen // iSoc = sl_Socket(SL_AF_RF,SL_SOCK_RAW,iChannel); ASSERT_ON_ERROR(iSoc); // Enable receive timeout lRetVal = sl_SetSockOpt(iSoc,SL_SOL_SOCKET,SL_SO_RCVTIMEO, &timeval, \ sizeof(timeval)); ASSERT_ON_ERROR(lRetVal); lRetVal = sl_Recv(iSoc,acBuffer,1470,0); if(lRetVal < 0 && lRetVal != SL_EAGAIN) { //error ASSERT_ON_ERROR(sl_Close(iSoc)); ASSERT_ON_ERROR(lRetVal); } UART_PRINT("\n\rPress any key to stop and display the statistics..."); // // Wait to receive a character over UART // MAP_UARTCharGet(CONSOLE); // //Get the Statistics collected in this time window // lRetVal = sl_WlanRxStatGet(&rxStatResp,0); if(lRetVal < 0) { //error ASSERT_ON_ERROR(sl_Close(iSoc)); ASSERT_ON_ERROR(lRetVal); } // //Printing the collected statistics // UART_PRINT("\n\n\n\r\t\t=========================================== \n \ \r"); UART_PRINT("\n\r\t\t\t\tRx Statistics \n\r"); UART_PRINT("\t\t=========================================== \n\r"); UART_PRINT("\n\n\rThe data sampled over %ld microsec\n\n\r", \ (unsigned int)(rxStatResp.GetTimeStamp - rxStatResp.StartTimeStamp)); UART_PRINT("Number of Valid Packets Received: %d\n\r", rxStatResp.ReceivedValidPacketsNumber); UART_PRINT("Number of Packets Received Packets with FCS: %d\n\r", rxStatResp.ReceivedFcsErrorPacketsNumber); UART_PRINT("Number of Packets Received Packets with PLCP: %d\n\n\r", \ rxStatResp.ReceivedAddressMismatchPacketsNumber); UART_PRINT("Average Rssi for management packets: %d\ \n\rAverage Rssi for other packets: %d\n\r", rxStatResp.AvarageMgMntRssi,rxStatResp.AvarageDataCtrlRssi); for(iIndex = 0 ; iIndex < SIZE_OF_RSSI_HISTOGRAM ; iIndex++) { UART_PRINT("Number of packets with RSSI in range %d dbm - %d dbm: %d\n\r", ((-40+(-8*iIndex))),((-40+(-8*(iIndex+1)))+1), rxStatResp.RssiHistogram[iIndex]); } UART_PRINT("\n\r"); //for(iIndex = 0 ; iIndex < NUM_OF_RATE_INDEXES ; iIndex++) iIndex = 0; { UART_PRINT("Number of Packets with Rate 1Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 2Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 5.5Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 11Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 6Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 9Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 12Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 18Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 24Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 36Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 48Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate 54Mbps : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_0 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_1 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_2 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_3 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_4 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_5 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_6 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); UART_PRINT("Number of Packets with Rate MCS_7 : %d\n\r", rxStatResp.RateHistogram[iIndex++]); } // //Stop Rx statistics collection // lRetVal = sl_WlanRxStatStop(); ASSERT_ON_ERROR(lRetVal); // //Close the socket // lRetVal = sl_Close(iSoc); ASSERT_ON_ERROR(lRetVal); return SUCCESS; }
int miot_wifi_setup_ap(const struct sys_config_wifi_ap *cfg) { int ret; uint8_t v; SlNetCfgIpV4Args_t ipcfg; SlNetAppDhcpServerBasicOpt_t dhcpcfg; char ssid[64]; if ((ret = sl_WlanSetMode(ROLE_AP)) != 0) { return 0; } strncpy(ssid, cfg->ssid, sizeof(ssid)); miot_expand_mac_address_placeholders(ssid); if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_SSID, strlen(ssid), (const uint8_t *) ssid)) != 0) { return 0; } v = (cfg->pass != NULL && 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) { 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) { return 0; } v = cfg->channel; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_CHANNEL, 1, (uint8_t *) &v)) != 0) { return 0; } v = cfg->hidden; if ((ret = sl_WlanSet(SL_WLAN_CFG_AP_ID, WLAN_AP_OPT_HIDDEN_SSID, 1, (uint8_t *) &v)) != 0) { return 0; } sl_NetAppStop(SL_NET_APP_DHCP_SERVER_ID); 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) { 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) { return 0; } /* Turning the device off and on for the change to take effect. */ if (!restart_nwp()) return 0; if ((ret = sl_NetAppStart(SL_NET_APP_DHCP_SERVER_ID)) != 0) { LOG(LL_ERROR, ("DHCP server failed to start: %d", ret)); } sl_WlanRxStatStart(); LOG(LL_INFO, ("AP %s configured", ssid)); return 1; }