Skip to content

Commit 728c336

Browse files
committed
Added ESP32 support
1 parent d0f097a commit 728c336

5 files changed

Lines changed: 150 additions & 57 deletions

File tree

FlipperZero-WiFi-Scanner_Module-ESP8266/.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.

FlipperZero-WiFi-Scanner_Module-ESP8266/DebugHelpers.h

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
class PerfTimer
2+
{
3+
public:
4+
PerfTimer(const char* debugName) : m_debugName(debugName)
5+
{
6+
m_startTime = millis();
7+
}
8+
9+
~PerfTimer()
10+
{
11+
char buffer[128];
12+
sprintf(buffer, "[%s] %ul millis", m_debugName, millis() - m_startTime);
13+
Serial.println();
14+
Serial.println(buffer);
15+
}
16+
17+
private:
18+
unsigned long m_startTime = 0;
19+
const char* m_debugName;
20+
};
21+
22+
23+
#ifdef DEBUG
24+
#define DEBUG_LOG(text) Serial.print(text)
25+
#define DEBUG_LOG_LN(text) Serial.println(text)
26+
#define FUNCTION_PERF() PerfTimer perfTimer(__FUNCTION__)
27+
#else
28+
#define DEBUG_LOG(text)
29+
#define DEBUG_LOG_LN(text)
30+
#define FUNCTION_PERF()
31+
#endif

FlipperZero-WiFi-Scanner_Module-ESP8266/FlipperZero-WiFi-Scanner_Module-ESP8266.ino renamed to FlipperZero-WiFi-Scanner_Module/FlipperZero-WiFi-Scanner_Module.ino

Lines changed: 119 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
//#define DEBUG
22

3-
#include "ESP8266WiFi.h"
3+
#if ESP8266
4+
#include <ESP8266WiFi.h>
5+
#else // ESP32
6+
#include <WiFi.h>
7+
#endif
8+
49
#include "DebugHelpers.h"
510
#include "FlipperZeroWiFiModuleDefines.h"
611

@@ -77,23 +82,45 @@ void ChangeContext(EContext context)
7782
}
7883
}
7984

80-
const char* EncryptionTypeToString(short encType)
85+
const char* EncryptionTypeToString(uint8_t encType)
8186
{
87+
#if ESP8266
8288
switch(encType)
8389
{
84-
case 5:
90+
case ENC_TYPE_WEP:
8591
return "WEP";
86-
case 2:
92+
case ENC_TYPE_TKIP:
8793
return "TKIP";
88-
case 4:
94+
case ENC_TYPE_CCMP:
8995
return "CCMP";
90-
case 7:
91-
return "NONE";
92-
case 8:
96+
case ENC_TYPE_NONE:
97+
return "OPEN";
98+
case ENC_TYPE_AUTO:
9399
return "AUTO";
94100
default:
95101
return "FAIL";
96102
}
103+
#elif ESP32
104+
switch(encType)
105+
{
106+
case WIFI_AUTH_OPEN:
107+
return "OPEN";
108+
case WIFI_AUTH_WEP:
109+
return "WEP";
110+
case WIFI_AUTH_WPA_PSK:
111+
return "WPA";
112+
case WIFI_AUTH_WPA2_PSK:
113+
return "WPA2";
114+
case WIFI_AUTH_WPA_WPA2_PSK:
115+
return "WP1/2";
116+
case WIFI_AUTH_WPA2_ENTERPRISE:
117+
return "WPA2 E";
118+
default:
119+
return "FAIL";
120+
}
121+
#else
122+
return "FAIL";
123+
#endif
97124
}
98125

99126
void SendError(const char* str)
@@ -103,6 +130,8 @@ void SendError(const char* str)
103130

104131
void SerilizeAndSend(EContext context, const SScanInfoDisplay& scanInfo)
105132
{
133+
FUNCTION_PERF();
134+
106135
char serilizedData[150];
107136
sprintf(serilizedData, "%s+%s+%s+%d+%s+%u+%u+%u+%u",
108137
ContextToString(context),
@@ -137,11 +166,13 @@ void UnlockAP()
137166

138167
bool FindLockedAP(int& foundAPIndex)
139168
{
169+
FUNCTION_PERF();
170+
140171
if(IsAnyAPLocked())
141172
{
142173
for(int i = 0; i < g_totalAp; ++i)
143174
{
144-
if(strcmp(g_bssid.c_str(), WiFi.BSSIDstr(i).c_str()) == 0)
175+
if(g_bssid.equals(WiFi.BSSIDstr(i)))
145176
{
146177
foundAPIndex = i;
147178
return true;
@@ -198,29 +229,6 @@ void NextAP()
198229
DEBUG_LOG_LN(g_currentNetworkIndex);
199230
}
200231

201-
void ChangeBetweenMonitorAndScanMode()
202-
{
203-
DEBUG_LOG_LN(F("ChangeBetweenMonitorAndScanMode"));
204-
205-
if(g_context != EContext::SCAN_PAGE)
206-
{
207-
return;
208-
}
209-
210-
DEBUG_LOG_LN(F("ChangeBetweenMonitorAndScanMode processing"));
211-
212-
switch(g_context)
213-
{
214-
case SCAN_PAGE:
215-
LockAP();
216-
MonitorNetwork();
217-
ChangeContext(EContext::MONITOR_ANIMATION);
218-
break;
219-
default:
220-
break;
221-
}
222-
}
223-
224232
void setup() {
225233
Serial.begin(115200);
226234
while (!Serial) {
@@ -237,7 +245,24 @@ void setup() {
237245
ScanNetworks();
238246
}
239247

240-
void OnScanComplete(int totalAps)
248+
bool GetNetworkInfo(const uint8_t currentAp, String& ssid, uint8_t& encType, int32_t& rssi, String& bssid, int32_t& channel, bool& isHidden)
249+
{
250+
FUNCTION_PERF();
251+
252+
#if ESP8266
253+
uint8_t* fake_BSSID;
254+
bssid = WiFi.BSSIDstr(currentAp);
255+
return WiFi.getNetworkInfo(currentAp, ssid, encType, rssi, fake_BSSID, channel, isHidden);
256+
#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);
260+
isHidden = ssid.length() == 0 ? true : false;
261+
return result;
262+
#endif
263+
}
264+
265+
void OnScanComplete(int16_t totalAps)
241266
{
242267
g_totalAp = totalAps;
243268
g_currentNetworkIndex = 0;
@@ -249,14 +274,15 @@ void OnScanComplete(int totalAps)
249274
else
250275
{
251276
DEBUG_LOG_LN(F("No APs found. Scan again."));
252-
SendError("No APs found. Scan again.");
253-
//scannerDisplay.DisplayError("No APs found. Scan again.");
254-
ChangeContext(EContext::ERROR);
277+
ScanNetworks();
278+
//SendError("No APs found. Scan again.");
279+
//ChangeContext(EContext::ERROR);
255280
}
256281
}
257282

258-
void OnMonScanComplete(int totalAps)
283+
void OnMonScanComplete(int16_t totalAps)
259284
{
285+
FUNCTION_PERF();
260286
DEBUG_LOG_LN(F("DisplayMonitorInfo()"));
261287

262288
g_totalAp = totalAps;
@@ -272,14 +298,13 @@ void OnMonScanComplete(int totalAps)
272298
{
273299
scanInfo.m_currentAp = foundAp;
274300

275-
uint8_t* bssid_fake;
276-
if(WiFi.getNetworkInfo(scanInfo.m_currentAp, scanInfo.m_ssid, scanInfo.m_encryptionType, scanInfo.m_rssi, bssid_fake, scanInfo.m_channel, scanInfo.m_isHidden))
301+
if(GetNetworkInfo(scanInfo.m_currentAp, scanInfo.m_ssid, scanInfo.m_encryptionType, scanInfo.m_rssi, scanInfo.m_bssid, scanInfo.m_channel, scanInfo.m_isHidden))
277302
{
278303
SerilizeAndSend(EContext::MONITOR_PAGE, scanInfo);
279304
}
280305
else
281306
{
282-
DEBUG_LOG_LN(F("WiFi.getNetworkInfo() - Failed"));
307+
DEBUG_LOG_LN(F("GetNetworkInfo() - Failed"));
283308
scanInfo.m_rssi = NA;
284309
SerilizeAndSend(EContext::MONITOR_PAGE, scanInfo);
285310
}
@@ -308,21 +333,65 @@ void ScanNetworks()
308333
DEBUG_LOG_LN(F("ScanNetworks()"));
309334

310335
WiFi.scanDelete();
336+
337+
const bool async = true;
311338
const bool showHidden = true;
312-
WiFi.scanNetworksAsync(OnScanComplete, showHidden);
339+
WiFi.scanNetworks(async, showHidden);
313340

314341
ChangeContext(EContext::SCAN_ANIMATION);
315342
}
316343

317-
bool MonitorNetwork()
344+
bool MonitorNetwork(const bool firstRun = true)
318345
{
319-
if(WiFi.scanComplete() >= 0)
346+
int16_t scanResult = WiFi.scanComplete();
347+
if(scanResult != WIFI_SCAN_RUNNING)
320348
{
321349
DEBUG_LOG_LN(F("MonitorNetwork()"));
322350

351+
if(firstRun)
352+
{
353+
OnMonScanComplete(scanResult);
354+
}
355+
323356
WiFi.scanDelete();
357+
const bool async = true;
324358
const bool showHidden = true;
325-
WiFi.scanNetworksAsync(OnMonScanComplete, showHidden);
359+
WiFi.scanNetworks(async, showHidden);
360+
}
361+
}
362+
363+
void CheckScanComplition()
364+
{
365+
int16_t scanResult = WiFi.scanComplete();
366+
if(scanResult != WIFI_SCAN_RUNNING)
367+
{
368+
OnScanComplete(scanResult);
369+
}
370+
}
371+
372+
void ChangeBetweenMonitorAndScanMode()
373+
{
374+
DEBUG_LOG_LN(F("ChangeBetweenMonitorAndScanMode"));
375+
376+
if(g_context != EContext::SCAN_PAGE)
377+
{
378+
return;
379+
}
380+
381+
DEBUG_LOG_LN(F("ChangeBetweenMonitorAndScanMode processing"));
382+
383+
switch(g_context)
384+
{
385+
case SCAN_PAGE:
386+
{
387+
LockAP();
388+
const bool firstRun = false;
389+
MonitorNetwork(firstRun);
390+
ChangeContext(EContext::MONITOR_ANIMATION);
391+
}
392+
break;
393+
default:
394+
break;
326395
}
327396
}
328397

@@ -336,10 +405,8 @@ void DisplayScannedAP()
336405
SScanInfoDisplay scanInfo;
337406
scanInfo.m_currentAp = g_currentNetworkIndex + 1;
338407
scanInfo.m_totalAp = g_totalAp;
339-
scanInfo.m_bssid = WiFi.BSSIDstr(scanInfo.m_currentAp);
340-
341-
uint8_t* bssid_fake;
342-
if(WiFi.getNetworkInfo(g_currentNetworkIndex, scanInfo.m_ssid, scanInfo.m_encryptionType, scanInfo.m_rssi, bssid_fake, scanInfo.m_channel, scanInfo.m_isHidden))
408+
409+
if(GetNetworkInfo(g_currentNetworkIndex, scanInfo.m_ssid, scanInfo.m_encryptionType, scanInfo.m_rssi, scanInfo.m_bssid, scanInfo.m_channel, scanInfo.m_isHidden))
343410
{
344411
SerilizeAndSend(EContext::SCAN_PAGE, scanInfo);
345412
}
@@ -355,6 +422,8 @@ void CheckForFlipperCommands()
355422
{
356423
while(Serial.available() > 0)
357424
{
425+
FUNCTION_PERF();
426+
358427
int incommingCommand = Serial.read();
359428
if(g_context >= EContext::SCAN_PAGE)
360429
{
@@ -400,8 +469,10 @@ void loop()
400469
MonitorNetwork();
401470
break;
402471
case EContext::SCAN_ANIMATION:
472+
CheckScanComplition();
403473
break;
404474
case EContext::MONITOR_ANIMATION:
475+
MonitorNetwork();
405476
break;
406477
case EContext::ERROR:
407478
break;

FlipperZero-WiFi-Scanner_Module-ESP8266/FlipperZeroWiFiModuleDefines.h renamed to FlipperZero-WiFi-Scanner_Module/FlipperZeroWiFiModuleDefines.h

File renamed without changes.

0 commit comments

Comments
 (0)