Skip to content

Commit 11dfb2c

Browse files
authored
Add WiFi::beginNoBlock() (#2063)
1 parent 55ea2b5 commit 11dfb2c

File tree

2 files changed

+73
-24
lines changed

2 files changed

+73
-24
lines changed

libraries/WiFi/src/WiFiClass.cpp

Lines changed: 55 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -67,30 +67,11 @@ void WiFiClass::mode(WiFiMode_t m) {
6767
}
6868
}
6969

70-
71-
/* Start WiFi connection for OPEN networks
72-
73-
param ssid: Pointer to the SSID string.
74-
*/
75-
int WiFiClass::begin(const char* ssid) {
76-
return begin(ssid, nullptr);
77-
}
78-
79-
int WiFiClass::beginBSSID(const char* ssid, const uint8_t *bssid) {
80-
return begin(ssid, nullptr, bssid);
81-
}
82-
83-
/* Start WiFi connection with passphrase
84-
the most secure supported mode will be automatically selected
85-
86-
param ssid: Pointer to the SSID string.
87-
param passphrase: Passphrase. Valid characters in a passphrase
88-
must be between ASCII 32-126 (decimal).
89-
*/
90-
int WiFiClass::begin(const char* ssid, const char *passphrase, const uint8_t *bssid) {
70+
bool WiFiClass::_beginInternal(const char* ssid, const char *passphrase, const uint8_t *bssid) {
9171
// Simple ESP8266 compatibility hack
9272
if (_modeESP == WIFI_AP) {
93-
return beginAP(ssid, passphrase);
73+
// When beginAP was a success, it returns WL_CONNECTED and we return true as success.
74+
return beginAP(ssid, passphrase) == WL_CONNECTED;
9475
}
9576

9677
end();
@@ -109,19 +90,69 @@ int WiFiClass::begin(const char* ssid, const char *passphrase, const uint8_t *bs
10990
_wifi.setTimeout(_timeout);
11091
_apMode = false;
11192
_wifiHWInitted = true;
112-
uint32_t start = millis(); // The timeout starts from network init, not network link up
93+
94+
// Internal wifi.begin returns false when failed, therefore we return false as error
11395
if (!_wifi.begin()) {
114-
return WL_IDLE_STATUS;
96+
return false;
11597
}
11698
noLowPowerMode();
11799
// Enable CYW43 event debugging (make sure Debug Port is set)
118100
//cyw43_state.trace_flags = 0xffff;
101+
102+
return true;
103+
}
104+
105+
106+
/* Start WiFi connection for OPEN networks
107+
108+
param ssid: Pointer to the SSID string.
109+
*/
110+
int WiFiClass::begin(const char* ssid) {
111+
return begin(ssid, nullptr);
112+
}
113+
114+
/* Start WiFi connection for OPEN networks, but non blocking.
115+
116+
param ssid: Pointer to the SSID string.
117+
*/
118+
int WiFiClass::beginNoBlock(const char* ssid) {
119+
return beginNoBlock(ssid, nullptr);
120+
}
121+
122+
123+
int WiFiClass::beginBSSID(const char* ssid, const uint8_t *bssid) {
124+
return begin(ssid, nullptr, bssid);
125+
}
126+
127+
/* Start WiFi connection with passphrase
128+
the most secure supported mode will be automatically selected
129+
130+
param ssid: Pointer to the SSID string.
131+
param passphrase: Passphrase. Valid characters in a passphrase
132+
must be between ASCII 32-126 (decimal).
133+
*/
134+
int WiFiClass::begin(const char* ssid, const char *passphrase, const uint8_t *bssid) {
135+
uint32_t start = millis(); // The timeout starts from network init, not network link up
136+
137+
// Returns WL_IDLE_STATUS on error for compatibility.
138+
if (!_beginInternal(ssid, passphrase, bssid)) {
139+
return WL_IDLE_STATUS;
140+
}
141+
119142
while (!_calledESP && ((millis() - start < (uint32_t)2 * _timeout)) && !connected()) {
120143
delay(10);
121144
}
122145
return status();
123146
}
124147

148+
int WiFiClass::beginNoBlock(const char* ssid, const char *passphrase, const uint8_t *bssid) {
149+
// Returns WL_IDLE_STATUS on error for compatibility.
150+
if (!_beginInternal(ssid, passphrase, bssid)) {
151+
return WL_IDLE_STATUS;
152+
}
153+
return status();
154+
}
155+
125156
uint8_t WiFiClass::beginAP(const char *ssid) {
126157
return beginAP(ssid, nullptr);
127158
}

libraries/WiFi/src/WiFiClass.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ class WiFiClass {
7070
param ssid: Pointer to the SSID string.
7171
*/
7272
int begin(const char* ssid);
73+
/* Start WiFi connection for OPEN networks, without blocking
74+
75+
param ssid: Pointer to the SSID string.
76+
*/
77+
int beginNoBlock(const char* ssid);
78+
7379
int beginBSSID(const char* ssid, const uint8_t *bssid);
7480

7581
/* Start WiFi connection with WEP encryption.
@@ -92,6 +98,15 @@ class WiFiClass {
9298
param bssid: If non-null, the BSSID associated w/the SSID to connect to
9399
*/
94100
int begin(const char* ssid, const char *passphrase, const uint8_t *bssid = nullptr);
101+
/* Start WiFi connection with passphrase, without blocking. Check for .connected() for a connection
102+
the most secure supported mode will be automatically selected
103+
104+
param ssid: Pointer to the SSID string.
105+
param passphrase: Passphrase. Valid characters in a passphrase
106+
must be between ASCII 32-126 (decimal).
107+
param bssid: If non-null, the BSSID associated w/the SSID to connect to
108+
*/
109+
int beginNoBlock(const char* ssid, const char *passphrase, const uint8_t *bssid = nullptr);
95110

96111
bool connected();
97112
bool isConnected() {
@@ -413,6 +428,9 @@ class WiFiClass {
413428
}
414429

415430
private:
431+
// Internal wifi begin. Returns true on success
432+
bool _beginInternal(const char* ssid, const char *passphrase, const uint8_t *bssid = nullptr);
433+
416434
int _timeout = 15000;
417435
String _ssid;
418436
uint8_t _bssid[6];

0 commit comments

Comments
 (0)