-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Wifi.cpp
608 lines (539 loc) · 17.8 KB
/
Wifi.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
/**
* @file Wifi.cpp
* @author Phil Hilger ([email protected])
* @brief
* @version 0.1
* @date 2023-03-02
*
* CAN-talk. A library for microcontrollers that allows decent comms
* over a CAN bus.
*
* Copyright (C) 2023, PeerGum
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https: //www.gnu.org/licenses/>.
*
*/
#include "Wifi.h"
// #include <string>
#include <cstring>
#include "esp_log.h"
#include "UPnP.h"
#include "arpa/inet.h"
#include "esp_netif.h"
#include "esp_wifi.h"
#include "netdb.h"
#include "esp_mac.h"
#include "sntp.h"
#include "UPnP.h"
/* FreeRTOS event group to signal when we are connected*/
/* The event group allows multiple bits for each event, but we only care about
* two events:
* - we are connected to the AP with an IP
* - we failed to connect after the maximum amount of retries */
#define WIFI_CONNECTED_BIT BIT0
#define WIFI_FAILED_BIT BIT1
#define AP_CONNECTED_BIT BIT2
#define AP_DISCONNECTED_BIT BIT3
static const char *TAG = "Wifi";
void wifiTask(void *arg) {
wifi_mode_t startMode = *(wifi_mode_t *)(arg);
wifi.init();
wifi.startEspNow();
wifi.scanSTA();
wifi.start(startMode);
while (true) {
wifi.checkUPnPMappings();
vTaskDelay(pdMS_TO_TICKS(50));
}
wifi.stop();
}
static void wifiEventHandler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
wifi.event_handler(arg, event_base, event_id, event_data);
}
/**
* @brief
*
* @param url
* @param protocol (max 20 chars)
* @param hostname (max 253 chars)
* @param port (max 5 chars)
* @param path (max 2048 chars)
*/
void parseUrl(const char *url, char *protocol, char *hostname, char *port,
char *path) {
if (!strstr(url, "://")) {
return;
}
int state = 0;
bool hostmode = true;
protocol[0] = hostname[0] = path[0] = port[0] = 0;
for (int i = 0, j = 0; i < strlen(url); i++) {
switch (url[i]) {
case ':':
if (state == 0) {
// first occurrence of :
state++;
sprintf(port, "%d", protocolToPort(protocol));
} else {
// second occurrence of :
hostmode = false;
}
j = 0;
break;
case '/':
if (state > 0 && state < 4) {
// after initial protocol
state++;
j = 0;
}
if (state == 4) {
path[j++] = url[i];
path[j] = 0;
}
break;
default:
if (state == 0 && j < 20) {
// protocol part
protocol[j++] = url[i];
protocol[j] = 0;
} else if (state == 3) {
// host and port part
if (hostmode && j < 253) {
hostname[j++] = url[i];
hostname[j] = 0;
} else if (!hostmode && j < 5) {
port[j++] = url[i];
port[j] = 0;
}
} else if (j < 2049) {
// uri part
path[j++] = url[i];
path[j] = 0;
}
break;
}
}
ESP_LOGD(TAG, "Extracted from url [%s]:", url);
ESP_LOGD(TAG, "proto: [%s]", protocol);
ESP_LOGD(TAG, "host: [%s]", hostname);
ESP_LOGD(TAG, "port: [%s]", port);
ESP_LOGD(TAG, "path: [%s]", path);
}
/**
* @brief resolve hostname and port to IP
*
* @param hostname
* @param port
* @param ip
* @return true
* @return false
*/
bool resolve(const char *hostname, IPAddress &ip) {
// *** TO DO: resolve IPv6 // check if not already an IP address
if (ip.fromChar(hostname).isValid()) {
return true;
}
struct hostent *server;
server = gethostbyname(hostname);
if (server == NULL) {
ESP_LOGD(TAG, "could not get host from dns: %d", errno);
return false;
}
ip = IPAddress((const uint8_t *)(server->h_addr_list[0]));
ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", ip.toChar());
return true;
}
/**
* @brief convert string protocol to number
*
* @param protocol
* @return uint16_t
*/
uint16_t protocolToPort(const char *protocol) {
if (!strcmp(protocol, "http")) {
return 80U;
} else if (!strcmp(protocol, "https")) {
return 443U;
}
return 0;
}
void ssidList(int &num, char ***labels, char ***values) {
wifi.ssidList(num, labels, values);
}
/**
* @brief Construct a new Wifi:: Wifi object
*
*/
Wifi::Wifi() {}
/**
* @brief Destroy the Wifi:: Wifi object
*
*/
Wifi::~Wifi() {}
void Wifi::setStartCB(void (*cb)()) { _startCB = cb; }
void Wifi::setStopCB(void (*cb)()) { _stopCB = cb; }
/**
* @brief set STA parameters
*
* @param ssid
* @param passwd
* @param authMode
*/
void Wifi::setSTA(const char *ssid, const char *passwd,
const wifi_auth_mode_t authMode) {
strcpy(_SSID, ssid);
strcpy(_passwd, passwd);
_authMode = authMode;
}
/**
* @brief set AP parameters
*
* @param ssid
* @param passwd
* @param authMode
*/
void Wifi::setAP(const char *ssid, const char *passwd,
const wifi_auth_mode_t authMode) {
strcpy(_apSSID, ssid);
strcpy(_apPasswd, passwd);
_apAuthMode = authMode;
}
bool Wifi::init() {
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
esp_netif_create_default_wifi_ap();
_wifiEventGroup = xEventGroupCreate();
esp_event_handler_instance_t instance_any_id;
esp_event_handler_instance_t instance_got_ip;
ESP_ERROR_CHECK(esp_event_handler_instance_register(
WIFI_EVENT, ESP_EVENT_ANY_ID, wifiEventHandler, NULL, &instance_any_id));
ESP_ERROR_CHECK(esp_event_handler_instance_register(
IP_EVENT, IP_EVENT_STA_GOT_IP, wifiEventHandler, NULL, &instance_got_ip));
wifi_init_config_t wifiConfig = WIFI_INIT_CONFIG_DEFAULT();
return (ESP_OK == esp_wifi_init(&wifiConfig));
}
/**
* @brief start wifi
*
* @return true
* @return false
*/
bool Wifi::start(wifi_mode_t mode) {
wifiAPConfig.ap.ssid_len = 0;
wifiAPConfig.ap.channel = 1;
wifiAPConfig.ap.authmode = _apAuthMode;
wifiAPConfig.ap.max_connection = WIFI_MAX_AP_CONNECTIONS;
strcpy((char *)wifiAPConfig.ap.password, _apPasswd);
strcpy((char *)wifiAPConfig.ap.ssid, _apSSID);
wifiSTAConfig.sta.scan_method = WIFI_FAST_SCAN;
wifiSTAConfig.sta.channel = 1;
wifiSTAConfig.sta.sort_method = WIFI_CONNECT_AP_BY_SIGNAL;
wifiSTAConfig.sta.threshold.rssi = WIFI_MIN_RSSI;
wifiSTAConfig.sta.threshold.authmode = _authMode;
wifiSTAConfig.sta.bssid_set = 0;
// wifiSTAConfig.sta.authmode = _authMode;
strcpy((char *)wifiSTAConfig.sta.ssid, _SSID);
strcpy((char *)wifiSTAConfig.sta.password, _passwd);
esp_wifi_set_mode(mode);
esp_wifi_set_config(WIFI_IF_AP, &wifiAPConfig);
esp_wifi_set_config(WIFI_IF_STA, &wifiSTAConfig);
if (ESP_OK != esp_wifi_start()) {
return false;
}
ESP_LOGI(TAG, "wifi started.");
_wifiStarted = true;
_startCB(); // short callback function
return true;
}
bool Wifi::isSTAConnected() {
return xEventGroupGetBits(_wifiEventGroup) & WIFI_CONNECTED_BIT;
}
bool Wifi::isSTAFailed() {
return xEventGroupGetBits(_wifiEventGroup) & WIFI_FAILED_BIT;
}
bool Wifi::isAPConnected() {
return xEventGroupGetBits(_wifiEventGroup) & AP_CONNECTED_BIT;
}
bool Wifi::isAPDisconnected() {
return xEventGroupGetBits(_wifiEventGroup) & AP_DISCONNECTED_BIT;
}
void Wifi::event_handler(void *arg, esp_event_base_t event_base,
int32_t event_id, void *event_data) {
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
_retry_num = 0;
} else if (event_base == WIFI_EVENT &&
(event_id == WIFI_EVENT_STA_DISCONNECTED ||
event_id == WIFI_EVENT_STA_BEACON_TIMEOUT)) {
_wifiState = DISCONNECTED;
if (_retry_num < CONFIG_ESP_MAXIMUM_RETRY) {
esp_wifi_connect();
_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(_wifiEventGroup, WIFI_FAILED_BIT);
_wifiState = WIFI_INCORRECT;
}
ESP_LOGI(TAG, "connect to the AP fail");
_wifiState = DISCONNECTED;
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
_localIP = event->ip_info.ip.addr;
_gatewayIP = event->ip_info.gw.addr;
ESP_LOGI(TAG, "got ip: %s gw: %s", _localIP.toChar(), _gatewayIP.toChar());
_retry_num = 0;
xEventGroupSetBits(_wifiEventGroup, WIFI_CONNECTED_BIT);
_wifiState = CONNECTED;
}
/* if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
esp_wifi_connect();
_retry_num = 0;
} else if (event_base == WIFI_EVENT &&
(event_id == WIFI_EVENT_STA_DISCONNECTED ||
event_id == WIFI_EVENT_STA_BEACON_TIMEOUT)) {
_wifiState = DISCONNECTED;
if (_retry_num < WIFI_MAXIMUM_RETRY) {
esp_wifi_connect();
_retry_num++;
ESP_LOGI(TAG, "retry to connect to the AP");
} else {
xEventGroupSetBits(_wifiEventGroup, WIFI_FAILED_BIT);
_wifiState = WIFI_INCORRECT;
}
ESP_LOGI(TAG, "connect to the AP fail");
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
ip_event_got_ip_t *event = (ip_event_got_ip_t *)event_data;
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
_retry_num = 0;
xEventGroupSetBits(_wifiEventGroup, WIFI_CONNECTED_BIT);
_wifiState = CONNECTED;
} */
else if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_AP_STACONNECTED) {
wifi_event_ap_staconnected_t *event =
(wifi_event_ap_staconnected_t *)event_data;
ESP_LOGI(TAG, "station " MACSTR " join, AID=%d", MAC2STR(event->mac),
event->aid);
xEventGroupSetBits(_wifiEventGroup, AP_CONNECTED_BIT);
_wifiState = AP_CONNECTED;
} else if (event_base == WIFI_EVENT &&
event_id == WIFI_EVENT_AP_STADISCONNECTED) {
wifi_event_ap_stadisconnected_t *event =
(wifi_event_ap_stadisconnected_t *)event_data;
ESP_LOGI(TAG, "station " MACSTR " leave, AID=%d", MAC2STR(event->mac),
event->aid);
xEventGroupSetBits(_wifiEventGroup, AP_DISCONNECTED_BIT);
_wifiState = AP_DISCONNECTED;
} else {
ESP_LOGW(TAG, "Event base: %s, event_id = %ld", event_base, event_id);
}
}
void Wifi::initialize_sntp(void) {
ESP_LOGI(TAG, "Initializing SNTP");
sntp_setoperatingmode(SNTP_OPMODE_POLL);
/*
* If 'NTP over DHCP' is enabled, we set dynamic pool address
* as a 'secondary' server. It will act as a fallback server in case that
* address provided via NTP over DHCP is not accessible
*/
#if LWIP_DHCP_GET_NTP_SRV && SNTP_MAX_SERVERS > 1
sntp_setservername(1, "pool.ntp.org");
#if LWIP_IPV6 && \
SNTP_MAX_SERVERS > 2 // statically assigned IPv6 address is also possible
ip_addr_t ip6;
if (ipaddr_aton("2a01:3f7::1", &ip6)) { // ipv6 ntp source "ntp.netnod.se"
sntp_setserver(2, &ip6);
}
#endif /* LWIP_IPV6 */
#else /* LWIP_DHCP_GET_NTP_SRV && (SNTP_MAX_SERVERS > 1) */
// otherwise, use DNS address from a pool
sntp_setservername(0, "pool.ntp.org");
sntp_setservername(1,
"pool.ntp.org"); // set the secondary NTP server (will be
// used only if SNTP_MAX_SERVERS > 1)
#endif
// sntp_set_time_sync_notification_cb(time_sync_notification_cb);
#ifdef CONFIG_SNTP_TIME_SYNC_METHOD_SMOOTH
sntp_set_sync_mode(SNTP_SYNC_MODE_SMOOTH);
#endif
sntp_init();
ESP_LOGI(TAG, "List of configured NTP servers:");
for (uint8_t i = 0; i < SNTP_MAX_SERVERS; ++i) {
if (sntp_getservername(i)) {
ESP_LOGI(TAG, "server %d: %s", i, sntp_getservername(i));
} else {
// we have either IPv4 or IPv6 address, let's print it
char buff[IP6ADDR_STRLEN_MAX];
ip_addr_t const *ip = sntp_getserver(i);
if (ipaddr_ntoa_r(ip, buff, IP6ADDR_STRLEN_MAX) != NULL)
ESP_LOGI(TAG, "server %d: %s", i, buff);
}
}
}
void Wifi::scanSTA() {
if (_wifiStarted) {
esp_wifi_stop();
}
wifiSTAConfig.sta.ssid[0] = 0;
wifiSTAConfig.sta.password[0] = 0;
wifiSTAConfig.sta.scan_method = WIFI_ALL_CHANNEL_SCAN;
wifiSTAConfig.sta.channel = 0;
wifiSTAConfig.sta.sort_method = WIFI_CONNECT_AP_BY_SECURITY;
wifiSTAConfig.sta.threshold.rssi = WIFI_MIN_RSSI;
wifiSTAConfig.sta.threshold.authmode = WIFI_AUTH_WPA2_PSK;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifiSTAConfig));
ESP_ERROR_CHECK(esp_wifi_start());
_apCount = 0;
memset(_apInfo, 0, sizeof(_apInfo));
esp_wifi_scan_start(NULL, true);
// vTaskDelay(pdMS_TO_TICKS(3000));
esp_wifi_scan_stop();
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&_apNum, _apInfo));
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&_apCount));
ESP_LOGI(TAG, "Total APs scanned = %u", _apCount);
for (int i = 0; (i < WIFI_SCAN_LIST_SIZE) && (i < _apCount); i++) {
ESP_LOGI(TAG, "SSID \t\t%s", _apInfo[i].ssid);
ESP_LOGI(TAG, "RSSI \t\t%d", _apInfo[i].rssi);
ESP_LOGI(TAG, "Auth \t\t%d", _apInfo[i].authmode);
// if (_apInfo[i].authmode != WIFI_AUTH_WEP) {
// print_cipher_type(_apInfo[i].pairwise_cipher,
// _apInfo[i].group_cipher);
// }
ESP_LOGI(TAG, "Channel \t\t%d\n", _apInfo[i].primary);
}
esp_wifi_stop();
}
void Wifi::ssidList(int &cnt, char ***pLabels, char ***pValues) {
if (!*pLabels) {
*pLabels = (char **)malloc(WIFI_SCAN_LIST_SIZE * sizeof(char *));
memset((void *)*pLabels, 0, WIFI_SCAN_LIST_SIZE * sizeof(char *));
}
if (!*pValues) {
*pValues = (char **)malloc(WIFI_SCAN_LIST_SIZE * sizeof(char *));
memset((void *)*pValues, 0, WIFI_SCAN_LIST_SIZE * sizeof(char *));
}
char temp[50];
char **values = *(pValues);
char **labels = *(pLabels);
cnt = 0;
for (int i = 0; i < _apCount && i < WIFI_SCAN_LIST_SIZE; i++) {
if (!_apInfo[i].ssid[0] || _apInfo[i].ssid[32] != 0) {
continue;
}
values[cnt] = (char *)(_apInfo[i].ssid);
// if (labels[i]) {
// free(labels[i]);
// }
sprintf(temp, "%s [%ddB]", _apInfo[i].ssid, _apInfo[i].rssi);
labels[cnt] = strdup(temp);
ESP_LOGI(TAG, "ptr #%d = %s -> %s", cnt, labels[cnt], values[cnt]);
// labels[i] = (char *)malloc(strlen(temp) + 1);
// strcpy(labels[i], temp);
cnt++;
}
}
void Wifi::switchToSTA() {
if (_wifiStarted) {
esp_wifi_stop();
}
strcpy((char *)wifiSTAConfig.sta.ssid, _SSID);
strcpy((char *)wifiSTAConfig.sta.password, _passwd);
wifiSTAConfig.sta.channel = 0;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifiSTAConfig));
ESP_ERROR_CHECK(esp_wifi_start());
_wifiStarted = true;
_wifiAPMode = false;
_wifiSTAReconfigured = false;
ESP_LOGI(TAG, "Switched to STA mode");
}
void Wifi::switchToAP() {
if (_wifiStarted) {
esp_wifi_stop();
}
// memset(wifiSTAConfig.ap.ssid, 0, sizeof(wifiSTAConfig.ap.ssid));
// strncpy((char *)wifiSTAConfig.ap.ssid, DEFAULT_AP_SSID,
// sizeof(wifiSTAConfig.ap.ssid));
// memset(wifiSTAConfig.ap.password, 0, sizeof(wifiSTAConfig.ap.password));
// strncpy((char *)wifiSTAConfig.ap.password, DEFAULT_AP_PASS,
// sizeof(wifiSTAConfig.ap.password));
strcpy((char *)wifiAPConfig.ap.ssid, _apSSID);
strcpy((char *)wifiAPConfig.ap.password, _apPasswd);
wifiAPConfig.ap.channel = 0;
wifiAPConfig.ap.authmode = _apAuthMode;
wifiAPConfig.ap.ssid_len = 0;
wifiAPConfig.ap.max_connection = WIFI_MAX_AP_CONNECTIONS;
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_AP, &wifiAPConfig));
ESP_ERROR_CHECK(esp_wifi_start());
// _apCount = 0;
// memset(_apInfo, 0, sizeof(_apInfo));
// esp_wifi_scan_start(NULL, true);
// ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&number, _apInfo));
// ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&_apCount));
// ESP_LOGI(TAG, "Total APs scanned = %u", _apCount);
// for (int i = 0; (i < WIFI_SCAN_LIST_SIZE) && (i < _apCount); i++) {
// ESP_LOGI(TAG, "SSID \t\t%s", _apInfo[i].ssid);
// ESP_LOGI(TAG, "RSSI \t\t%d", _apInfo[i].rssi);
// ESP_LOGI(TAG, "Auth \t\t%d", _apInfo[i].authmode);
// // if (_apInfo[i].authmode != WIFI_AUTH_WEP) {
// // print_cipher_type(_apInfo[i].pairwise_cipher,
// _apInfo[i].group_cipher);
// // }
// ESP_LOGI(TAG, "Channel \t\t%d\n", _apInfo[i].primary);
// }
_wifiStarted = true;
_wifiAPMode = true;
// _apTimer.initTimer();
ESP_LOGI(TAG, "Switched to AP mode");
}
bool Wifi::stop() {
if (ESP_OK != esp_wifi_stop()) {
return false;
}
_stopCB();
return true;
}
IPAddress &Wifi::localIP() { return _localIP; }
IPAddress &Wifi::gatewayIP() { return _gatewayIP; }
WifiStatus Wifi::status() { return _wifiState; }
void Wifi::addPortMappingConfig(int rulePort, const char *ruleProtocol,
int ruleLeaseDuration,
const char *ruleFriendlyName) {
upnp.addPortMappingConfig(localIP(), rulePort, ruleProtocol,
ruleLeaseDuration, ruleFriendlyName);
newMapping = true;
mappingTestCnt = 0;
}
void Wifi::checkUPnPMappings(void) {
if (newMapping && upnpTimer.check(30000UL)) {
portMappingResult portMappingAdded;
portMappingAdded = upnp.commitPortMappings();
mappingTestCnt++;
if (portMappingAdded == SUCCESS || portMappingAdded == ALREADY_MAPPED ||
mappingTestCnt > 3) {
newMapping = false;
upnp.printAllPortMappings();
}
}
}
void Wifi::startEspNow(void) {
if (config) {
espNow->init(config);
}
}
void Wifi::setConfig(Config &_config) { config = &_config; }
Wifi wifi;