Skip to content

Commit a97e08d

Browse files
authored
Improve ESP32 Wifi scan (#2470)
1 parent 40e67b9 commit a97e08d

File tree

6 files changed

+64
-30
lines changed

6 files changed

+64
-30
lines changed

src/PAL/Include/nanoPAL_Sockets.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -522,7 +522,7 @@ bool Network_Interface_Close(int index);
522522
int Network_Interface_Disconnect(int index);
523523
int Network_Interface_Start_Connect(int index, const char *ssid, const char *passphase, int options);
524524
int Network_Interface_Connect_Result(int configIndex);
525-
bool Network_Interface_Start_Scan(int index);
525+
int Network_Interface_Start_Scan(int index);
526526

527527
// Wireless AP methods
528528
void Network_Interface_Add_Station(uint16_t index, uint8_t *macAddress);

targets/AzureRTOS/_common/target_network.cpp

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ bool Network_Interface_Close(int index)
111111
return false;
112112
}
113113

114-
bool Network_Interface_Start_Scan(int index)
114+
int Network_Interface_Start_Scan(int index)
115115
{
116116
HAL_Configuration_NetworkInterface networkConfiguration;
117117

@@ -121,18 +121,21 @@ bool Network_Interface_Start_Scan(int index)
121121
DeviceConfigurationOption_Network,
122122
index))
123123
{
124-
// failed to load configuration
125-
// FIXME output error?
126-
return SOCK_SOCKET_ERROR;
124+
// failed to get configuration
125+
// TODO include error code enum
126+
return 7777; // StartScanOutcome_FailedToGetConfiguration;
127127
}
128128

129129
// can only do this is this is STA
130-
if (networkConfiguration.InterfaceType == NetworkInterfaceType_Wireless80211)
130+
if (networkConfiguration.InterfaceType != NetworkInterfaceType_Wireless80211)
131131
{
132-
// return (NF_ESP32_Wireless_Scan() == 0);
132+
// TODO include error code enum
133+
return 8888; // StartScanOutcome_WrongInterfaceType;
133134
}
134135

135-
return false;
136+
// TODO return NF_ESP32_Wireless_Scan();
137+
// TODO include error code enum
138+
return 9999;
136139
}
137140

138141
bool GetWirelessConfig(int index, HAL_Configuration_Wireless80211 **wirelessConfig)

targets/ESP32/_Network/NF_ESP32_Wireless.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,7 @@ int NF_ESP32_Wireless_Scan()
349349

350350
// Start a Wi-Fi scan
351351
// When complete a Scan Complete event will be fired
352-
esp_err_t res = esp_wifi_scan_start(&config, false);
353-
354-
return (int)res;
352+
return esp_wifi_scan_start(&config, false);
355353
}
356354

357355
wifi_auth_mode_t MapAuthentication(AuthenticationType type)

targets/ESP32/_common/Target_Network.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ bool Network_Interface_Close(int index)
9797
return false;
9898
}
9999

100-
bool Network_Interface_Start_Scan(int index)
100+
int Network_Interface_Start_Scan(int index)
101101
{
102102
HAL_Configuration_NetworkInterface networkConfiguration;
103103

@@ -107,18 +107,17 @@ bool Network_Interface_Start_Scan(int index)
107107
DeviceConfigurationOption_Network,
108108
index))
109109
{
110-
// failed to load configuration
111-
// FIXME output error?
112-
return SOCK_SOCKET_ERROR;
110+
// failed to get configuration
111+
return StartScanOutcome_FailedToGetConfiguration;
113112
}
114113

115114
// can only do this is this is STA
116-
if (networkConfiguration.InterfaceType == NetworkInterfaceType_Wireless80211)
115+
if (networkConfiguration.InterfaceType != NetworkInterfaceType_Wireless80211)
117116
{
118-
return (NF_ESP32_Wireless_Scan() == 0);
117+
return StartScanOutcome_WrongInterfaceType;
119118
}
120119

121-
return false;
120+
return NF_ESP32_Wireless_Scan();
122121
}
123122

124123
bool GetWirelessConfig(int index, HAL_Configuration_Wireless80211 **wirelessConfig)

targets/ESP32/_include/NF_ESP32_Network.h

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,27 @@
77
#define NF_ESP32_NETWORK_H
88

99
#include <nanoHAL.h>
10-
#include <lwIP_Sockets.h>
1110
#include <esp32_idf.h>
1211

1312
// ESP IDF 4.0 it's using an abstraction layer (esp_netif) that hides the netif index
1413
// we need that for our interface with lwIP, so we have to clone those here
1514
// not very elegant but will have to work for now
16-
#define IDF_WIFI_STA_DEF 0
17-
#define IDF_WIFI_AP_DEF 1
18-
#define IDF_ETH_DEF 2
15+
#define IDF_WIFI_STA_DEF 0
16+
#define IDF_WIFI_AP_DEF 1
17+
#define IDF_ETH_DEF 2
18+
19+
typedef enum __nfpack StartScanOutcome
20+
{
21+
StartScanOutcome_Success = 0,
22+
StartScanOutcome_FailedToGetConfiguration = 10,
23+
StartScanOutcome_WrongInterfaceType = 20,
24+
// these are the same as the IDF error codes
25+
StartScanOutcome_Esp32WifiNotInit = ESP_ERR_WIFI_NOT_INIT,
26+
StartScanOutcome_Esp32WifiNotStarted = ESP_ERR_WIFI_NOT_STARTED,
27+
StartScanOutcome_Esp32WifiTimeout = ESP_ERR_WIFI_TIMEOUT,
28+
StartScanOutcome_Esp32WifiState = ESP_ERR_WIFI_STATE,
29+
30+
} StartScanOutcome;
1931

2032
extern bool NF_ESP32_ConnectInProgress;
2133
extern int NF_ESP32_ConnectResult;

targets/ESP32/_nanoCLR/System.Device.Wifi/sys_dev_wifi_native_System_Device_Wifi_WifiAdapter.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@
77
#include <sys_dev_wifi_native.h>
88
#include <nf_rt_events_native.h>
99
#include <esp_wifi_types.h>
10+
#include <NF_ESP32_Network.h>
1011

11-
///////////////////////////////////////////////////////////////////////////////////////
12+
////////////////////////////////////////////////////////////////////////////////////
1213
// !!! KEEP IN SYNC WITH System.Device.Wifi (in managed code) !!! //
1314
///////////////////////////////////////////////////////////////////////////////////////
1415
struct ScanRecord
@@ -180,17 +181,38 @@ HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeDiscon
180181
HRESULT Library_sys_dev_wifi_native_System_Device_Wifi_WifiAdapter::NativeScanAsync___VOID(CLR_RT_StackFrame &stack)
181182
{
182183
NANOCLR_HEADER();
183-
{
184-
int netIndex;
185184

186-
NANOCLR_CHECK_HRESULT(GetNetInterfaceIndex(stack, &netIndex));
185+
int netIndex;
186+
int startScanResult;
187187

188-
// Start scan
189-
if (Network_Interface_Start_Scan(netIndex) == false)
190-
{
188+
NANOCLR_CHECK_HRESULT(GetNetInterfaceIndex(stack, &netIndex));
189+
190+
// Start scan
191+
startScanResult = Network_Interface_Start_Scan(netIndex);
192+
193+
switch (startScanResult)
194+
{
195+
196+
case StartScanOutcome_WrongInterfaceType:
197+
case StartScanOutcome_Esp32WifiNotInit:
198+
case StartScanOutcome_Esp32WifiNotStarted:
191199
NANOCLR_SET_AND_LEAVE(CLR_E_INVALID_OPERATION);
192-
}
200+
break;
201+
202+
case StartScanOutcome_Esp32WifiState:
203+
NANOCLR_SET_AND_LEAVE(CLR_E_BUSY);
204+
break;
205+
206+
case StartScanOutcome_Esp32WifiTimeout:
207+
NANOCLR_SET_AND_LEAVE(CLR_E_TIMEOUT);
208+
break;
209+
210+
case StartScanOutcome_FailedToGetConfiguration:
211+
default:
212+
NANOCLR_SET_AND_LEAVE(CLR_E_FAIL);
213+
break;
193214
}
215+
194216
NANOCLR_NOCLEANUP();
195217
}
196218

0 commit comments

Comments
 (0)