8000 Modified WiFi::connectAP so that it returns ESP_OK if successfully re… · llawall/esp32-snippets@6014b06 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6014b06

Browse files
committed
Modified WiFi::connectAP so that it returns ESP_OK if successfully receives a SYSTEM_EVENT_STA_GOT_IP event. Otherwise returns wifi_err_reason_t
1 parent 6817fee commit 6014b06

File tree

2 files changed

+12
-11
lines changed

2 files changed

+12
-11
lines changed

cpp_utils/WiFi.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ WiFi::WiFi()
5555
m_eventLoopStarted = false;
5656
m_initCalled = false;
5757
//m_pWifiEventHandler = new WiFiEventHandler();
58-
m_apConnected = false; // Are we connected to an access point?
58+
m_apConnectionStatus = UINT8_MAX; // Are we connected to an access point?
5959
} // WiFi
6060

6161

@@ -153,12 +153,12 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
153153
* @param [in] ssid The network SSID of the access point to which we wish to connect.
154154
* @param [in] password The password of the access point to which we wish to connect.
155155
* @param [in] waitForConnection Block until the connection has an outcome.
156-
* @return N/A.
156+
* @returns ESP_OK if successfully connected to an access point. Otherwise returns wifi_err_reason_t - use GeneralUtils::wifiErrorToString(uint8_t errCode) to print the error.
157157
*/
158-
bool WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
158+
uint8_t WiFi::connectAP(const std::string& ssid, const std::string& password, bool waitForConnection){
159159
ESP_LOGD(LOG_TAG, ">> connectAP");
160160

161-
m_apConnected = false;
161+
m_apConnectionStatus = UINT8_MAX;
162162
init();
163163

164164
if (ip != 0 && gw != 0 && netmask != 0) {
@@ -206,7 +206,7 @@ bool WiFi::connectAP(const std::string& ssid, const std::string& password, bool
206206
m_connectFinished.give();
207207

208208
ESP_LOGD(LOG_TAG, "<< connectAP");
209-
return m_apConnected; // Return true if we are now connected and false if not.
209+
return m_apConnectionStatus; // Return ESP_OK if we are now connected and wifi_err_reason_t if not.
210210
} // connectAP
211211

212212

@@ -228,7 +228,7 @@ void WiFi::dump() {
228228
* @brief Returns whether wifi is connected to an access point
229229
*/
230230
bool WiFi::isConnectedToAP() {
231-
return m_apConnected;
231+
return m_apConnectionStatus;
232232
} // isConnected
233233

234234

@@ -255,10 +255,11 @@ bool WiFi::isConnectedToAP() {
255255
// If the event we received indicates that we now have an IP address or that a connection was disconnected then unlock the mutex that
256256
// indicates we are waiting for a connection complete.
257257
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP || event->event_id == SYSTEM_EVENT_STA_DISCONNECTED) {
258-
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { // If we connected and have an IP, change the flag.
259-
pWiFi->m_apConnected = true;
258+
259+
if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) { // If we connected and have an IP, change the status to ESP_OK. Otherwise, change it to the reason code.
260+
pWiFi->m_apConnectionStatus = ESP_OK;
260261
} else {
261-
pWiFi->m_apConnected = false;
262+
pWiFi->m_apConnectionStatus = event->event_info.disconnected.reason;
262263
}
263264
pWiFi->m_connectFinished.give();
264265
}

cpp_utils/WiFi.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ class WiFi {
116116
uint8_t m_dnsCount=0;
117117
bool m_eventLoopStarted;
118118
bool m_initCalled;
119-
bool m_apConnected; // Are we connected to an access point?
119+
uint8_t m_apConnectionStatus; // ESP_OK = we are connected to an access point. Otherwise receives wifi_err_reason_t.
120120
FreeRTOS::Semaphore m_connectFinished = FreeRTOS::Semaphore("ConnectFinished");
121121

122122
public:
@@ -130,7 +130,7 @@ class WiFi {
130130
void setDNSServer(int numdns, ip_addr_t ip);
131131
struct in_addr getHostByName(const std::string& hostName);
132132
struct in_addr getHostByName(const char* hostName);
133-
bool connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true);
133+
uint8_t connectAP(const std::string& ssid, const std::string& password, bool waitForConnection=true);
134134
void dump();
135135
bool isConnectedToAP();
136136
static std::string getApMac();

0 commit comments

Comments
 (0)
0