11// #define DEBUG
22
3- #if ESP8266
3+ #ifdef ESP8266
44#include < ESP8266WiFi.h>
55#else // ESP32
66#include < WiFi.h>
77#endif
88
9+ #include < climits>
910#include " DebugHelpers.h"
1011#include " FlipperZeroWiFiModuleDefines.h"
1112
13+ const uint16_t ASSOCIATIVE_INDEX_ARRAY_MAX_ELEMENTS = UCHAR_MAX + 1 ;
14+ uint8_t g_associativeIndexArray[ASSOCIATIVE_INDEX_ARRAY_MAX_ELEMENTS];
15+
1216enum EContext
1317{
1418 INVALID = -1 ,
@@ -84,7 +88,7 @@ void ChangeContext(EContext context)
8488
8589const char * EncryptionTypeToString (uint8_t encType)
8690{
87- #if ESP8266
91+ #ifdef ESP8266
8892 switch (encType)
8993 {
9094 case ENC_TYPE_WEP:
@@ -154,8 +158,8 @@ bool IsAnyAPLocked()
154158
155159void LockAP ()
156160{
157- g_bssid = WiFi.BSSIDstr (g_currentNetworkIndex);
158- g_ssid = WiFi.SSID (g_currentNetworkIndex);
161+ g_bssid = WiFi.BSSIDstr (g_associativeIndexArray[ g_currentNetworkIndex] );
162+ g_ssid = WiFi.SSID (g_associativeIndexArray[ g_currentNetworkIndex] );
159163 g_apLocked = true ;
160164}
161165
@@ -182,6 +186,22 @@ bool FindLockedAP(int& foundAPIndex)
182186 return false ;
183187}
184188
189+ void NextAP ()
190+ {
191+ DEBUG_LOG_LN (F (" NextAP" ));
192+
193+ if (g_context != EContext::SCAN_PAGE)
194+ {
195+ return ;
196+ }
197+
198+ g_currentNetworkIndex = ++g_currentNetworkIndex % g_totalAp;
199+ pageChanged = true ;
200+
201+ DEBUG_LOG (F (" NextAP Index: " ));
202+ DEBUG_LOG_LN (g_currentNetworkIndex);
203+ }
204+
185205void PreviousAP ()
186206{
187207 DEBUG_LOG_LN (F (" PreviousAP" ));
@@ -213,22 +233,6 @@ void ScanMode()
213233 ScanNetworks ();
214234}
215235
216- void NextAP ()
217- {
218- DEBUG_LOG_LN (F (" NextAP" ));
219-
220- if (g_context != EContext::SCAN_PAGE)
221- {
222- return ;
223- }
224-
225- g_currentNetworkIndex = ++g_currentNetworkIndex % g_totalAp;
226- pageChanged = true ;
227-
228- DEBUG_LOG (F (" NextAP Index: " ));
229- DEBUG_LOG_LN (g_currentNetworkIndex);
230- }
231-
232236void setup () {
233237 Serial.begin (115200 );
234238 while (!Serial) {
@@ -248,27 +252,62 @@ void setup() {
248252bool GetNetworkInfo (const uint8_t currentAp, String& ssid, uint8_t & encType, int32_t & rssi, String& bssid, int32_t & channel, bool & isHidden)
249253{
250254 FUNCTION_PERF ();
251-
252- # if ESP8266
255+
256+ uint8_t index = g_associativeIndexArray[currentAp];
253257 uint8_t * fake_BSSID;
254- bssid = WiFi.BSSIDstr (currentAp);
255- return WiFi.getNetworkInfo (currentAp, ssid, encType, rssi, fake_BSSID, channel, isHidden);
258+
259+ #ifdef ESP8266
260+ bssid = WiFi.BSSIDstr (index);
261+ return WiFi.getNetworkInfo (index, ssid, encType, rssi, fake_BSSID, channel, isHidden);
256262#elif ESP32
257- uint8_t * fake_BSSID;
258- bssid = WiFi.BSSIDstr (currentAp);
259- const bool result = WiFi.getNetworkInfo (currentAp, ssid, encType, rssi, fake_BSSID, channel);
263+ bssid = WiFi.BSSIDstr (index);
264+ const bool result = WiFi.getNetworkInfo (index, ssid, encType, rssi, fake_BSSID, channel);
260265 isHidden = ssid.length () == 0 ? true : false ;
261266 return result;
267+ #else
268+ return false ;
262269#endif
263270}
264271
272+ void clearSortArray ()
273+ {
274+ FUNCTION_PERF ();
275+
276+ for (uint16_t i = 0 ; i < ASSOCIATIVE_INDEX_ARRAY_MAX_ELEMENTS; ++i)
277+ {
278+ g_associativeIndexArray[i] = i;
279+ }
280+ }
281+
282+ void SortScan (int16_t totalAps) // Sort scan by RSSI
283+ {
284+ FUNCTION_PERF ();
285+
286+ clearSortArray ();
287+
288+ uint8_t temp = 0 ;
289+ for (uint16_t i = 0 ; i < totalAps; ++i)
290+ {
291+ for (uint16_t j = i; j < totalAps; ++j)
292+ {
293+ if (WiFi.RSSI (g_associativeIndexArray[j]) > WiFi.RSSI (g_associativeIndexArray[i]))
294+ {
295+ temp = g_associativeIndexArray[i];
296+ g_associativeIndexArray[i] = g_associativeIndexArray[j];
297+ g_associativeIndexArray[j] = temp;
298+ }
299+ }
300+ }
301+ }
302+
265303void OnScanComplete (int16_t totalAps)
266304{
267305 g_totalAp = totalAps;
268306 g_currentNetworkIndex = 0 ;
269307 pageChanged = true ;
270308 if (g_totalAp > 0 )
271309 {
310+ SortScan (totalAps);
272311 ChangeContext (EContext::SCAN_PAGE);
273312 }
274313 else
@@ -341,14 +380,18 @@ void ScanNetworks()
341380 ChangeContext (EContext::SCAN_ANIMATION);
342381}
343382
344- bool MonitorNetwork (const bool firstRun = true )
383+ bool MonitorNetwork (const bool firstRun = false )
345384{
346385 int16_t scanResult = WiFi.scanComplete ();
347386 if (scanResult != WIFI_SCAN_RUNNING)
348387 {
349388 DEBUG_LOG_LN (F (" MonitorNetwork()" ));
350389
351390 if (firstRun)
391+ {
392+ clearSortArray ();
393+ }
394+ else
352395 {
353396 OnMonScanComplete (scanResult);
354397 }
@@ -385,7 +428,7 @@ void ChangeBetweenMonitorAndScanMode()
385428 case SCAN_PAGE:
386429 {
387430 LockAP ();
388- const bool firstRun = false ;
431+ const bool firstRun = true ;
389432 MonitorNetwork (firstRun);
390433 ChangeContext (EContext::MONITOR_ANIMATION);
391434 }
@@ -451,7 +494,11 @@ void CheckForFlipperCommands()
451494 }
452495 else
453496 {
454- DEBUG_LOG_LN (printf (" Skip command due to unpropriate context %c" , char (incommingCommand)));
497+ #ifdef DEBUG
498+ char buffer[80 ];
499+ sprintf (buffer, " Skip command due to unpropriate context for command '%c'" , char (incommingCommand));
500+ DEBUG_LOG_LN (buffer);
501+ #endif // DEBUG
455502 }
456503 }
457504}
0 commit comments