@@ -41,27 +41,30 @@ static void setDNSServer(char *ip) {
41
41
}
42
42
*/
43
43
44
+
45
+
46
+
44
47
/* *
45
48
* @brief Creates and uses a default event handler
46
49
*/
47
50
WiFi::WiFi ()
48
51
: ip(0 )
49
52
, gw(0 )
50
53
, netmask(0 )
51
- , m_wifiEventHandler (nullptr )
54
+ , m_pWifiEventHandler (nullptr )
52
55
{
53
56
::nvs_flash_init ();
54
57
wifi_init_config_t config = WIFI_INIT_CONFIG_DEFAULT ();
55
58
esp_wifi_init (&config);
56
59
::tcpip_adapter_init ();
57
- m_wifiEventHandler = new WiFiEventHandler ();
60
+ m_pWifiEventHandler = new WiFiEventHandler ();
58
61
} // WiFi
59
62
60
63
/* *
61
64
* @brief Deletes the event handler that was used by the class
62
65
*/
63
66
WiFi::~WiFi () {
64
- delete m_wifiEventHandler ;
67
+ delete m_pWifiEventHandler ;
65
68
}
66
69
67
70
/* *
@@ -134,11 +137,12 @@ void WiFi::setDNSServer(int numdns, ip_addr_t ip) {
134
137
*
135
138
* The event handler will be called back with the outcome of the connection.
136
139
*
137
- * @param[in] ssid The network SSID of the access point to which we wish to connect.
138
- * @param[in] password The password of the access point to which we wish to connect.
140
+ * @param [in] ssid The network SSID of the access point to which we wish to connect.
141
+ * @param [in] password The password of the access point to which we wish to connect.
142
+ * @param [in] waitForConnection Block until the connection has an outcome.
139
143
* @return N/A.
140
144
*/
141
- void WiFi::connectAP (const std::string& ssid, const std::string& password){
145
+ void WiFi::connectAP (const std::string& ssid, const std::string& password, bool waitForConnection ){
142
146
ESP_LOGD (LOG_TAG, " >> connectAP" );
143
147
144
148
if (ip != 0 && gw != 0 && netmask != 0 ) {
@@ -153,7 +157,8 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password){
153
157
}
154
158
155
159
156
- ESP_ERROR_CHECK ( esp_event_loop_init (m_wifiEventHandler->getEventHandler (), m_wifiEventHandler));
160
+ ESP_ERROR_CHECK (esp_event_loop_init (WiFi::eventHandler, this ));
161
+ // ESP_ERROR_CHECK(esp_event_loop_init(m_pWifiEventHandler->getEventHandler(), m_pWifiEventHandler));
157
162
ESP_ERROR_CHECK (::esp_wifi_set_storage (WIFI_STORAGE_RAM));
158
163
ESP_ERROR_CHECK (::esp_wifi_set_mode (WIFI_MODE_STA));
159
164
wifi_config_t sta_config;
@@ -164,7 +169,9 @@ void WiFi::connectAP(const std::string& ssid, const std::string& password){
164
169
ESP_ERROR_CHECK (::esp_wifi_set_config (WIFI_IF_STA, &sta_config));
165
170
ESP_ERROR_CHECK (::esp_wifi_start ());
166
171
172
+ m_gotIpEvt.take (" connectAP" );
167
173
ESP_ERROR_CHECK (::esp_wifi_connect ());
174
+ m_gotIpEvt.wait (" connectAP" );
168
175
ESP_LOGD (LOG_TAG, " << connectAP" );
169
176
} // connectAP
170
177
@@ -180,6 +187,22 @@ void WiFi::dump() {
180
187
ESP_LOGD (LOG_TAG, " DNS Server[0]: %s" , ipAddrStr);
181
188
} // dump
182
189
190
+
191
+ /* *
192
+ * @brief Primary event handler interface.
193
+ */
194
+ esp_err_t WiFi::eventHandler (void * ctx, system_event_t * event) {
195
+ WiFi *pWiFi = (WiFi *)ctx;
196
+ esp_err_t rc = pWiFi->m_pWifiEventHandler ->getEventHandler ()(pWiFi->m_pWifiEventHandler , event);
197
+ // If the event we received indicates that we now have an IP address then unlock the mutex that
198
+ // indicates we are waiting for an IP.
199
+ if (event->event_id == SYSTEM_EVENT_STA_GOT_IP) {
200
+ pWiFi->m_gotIpEvt .give ();
201
+ }
202
+ return rc;
203
+ } // eventHandler
204
+
205
+
183
206
/* *
184
207
* @brief Get the AP IP Info.
185
208
* @return The AP IP Info.
@@ -311,7 +334,7 @@ std::string WiFi::getStaSSID() {
311
334
std::vector<WiFiAPRecord> WiFi::scan () {
312
335
::nvs_flash_init ();
313
336
::tcpip_adapter_init ();
314
- ESP_ERROR_CHECK (esp_event_loop_init (m_wifiEventHandler ->getEventHandler (), m_wifiEventHandler ));
337
+ ESP_ERROR_CHECK (esp_event_loop_init (m_pWifiEventHandler ->getEventHandler (), m_pWifiEventHandler ));
315
338
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
316
339
ESP_ERROR_CHECK ( esp_wifi_init (&cfg) );
317
340
ESP_ERROR_CHECK (::esp_wifi_set_storage (WIFI_STORAGE_RAM));
@@ -353,7 +376,7 @@ std::vector<WiFiAPRecord> WiFi::scan() {
353
376
void WiFi::startAP (const std::string& ssid, const std::string& password) {
354
377
::nvs_flash_init ();
355
378
::tcpip_adapter_init ();
356
- ESP_ERROR_CHECK (esp_event_loop_init (m_wifiEventHandler ->getEventHandler (), m_wifiEventHandler ));
379
+ ESP_ERROR_CHECK (esp_event_loop_init (m_pWifiEventHandler ->getEventHandler (), m_pWifiEventHandler ));
357
380
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT ();
358
381
ESP_ERROR_CHECK ( esp_wifi_init (&cfg) );
359
382
ESP_ERROR_CHECK ( esp_wifi_set_storage (WIFI_STORAGE_RAM) );
@@ -378,7 +401,7 @@ void WiFi::startAP(const std::string& ssid, const std::string& password) {
378
401
* @param[in] wifiEventHandler The class that will be used to process events.
379
402
*/
380
403
void WiFi::setWifiEventHandler (WiFiEventHandler *wifiEventHandler) {
381
- this ->m_wifiEventHandler = wifiEventHandler;
404
+ this ->m_pWifiEventHandler = wifiEventHandler;
382
405
} // setWifiEventHandler
383
406
384
407
0 commit comments