@@ -4157,6 +4157,7 @@ void mg_mgr_init(struct mg_mgr *mgr) {
4157
4157
#endif
4158
4158
4159
4159
4160
+
4160
4161
#if MG_ENABLE_TCPIP
4161
4162
#define MG_EPHEMERAL_PORT_BASE 32768
4162
4163
#define PDIFF(a, b) ((size_t) (((char *) (b)) - ((char *) (a))))
@@ -4315,6 +4316,25 @@ struct pkt {
4315
4316
};
4316
4317
4317
4318
static void mg_tcpip_call(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
4319
+ #if MG_ENABLE_PROFILE
4320
+ const char *names[] = {
4321
+ "TCPIP_EV_ST_CHG",
4322
+ "TCPIP_EV_DHCP_DNS",
4323
+ "TCPIP_EV_DHCP_SNTP",
4324
+ "TCPIP_EV_ARP",
4325
+ "TCPIP_EV_TIMER_1S",
4326
+ "TCPIP_EV_WIFI_SCAN_RESULT",
4327
+ "TCPIP_EV_WIFI_SCAN_END",
4328
+ "TCPIP_EV_WIFI_CONNECT_ERR",
4329
+ "TCPIP_EV_DRIVER",
4330
+ "TCPIP_EV_USER"
4331
+ };
4332
+ if (ev != MG_TCPIP_EV_POLL && ev < (int) (sizeof(names) / sizeof(names[0]))) {
4333
+ MG_PROF_ADD(c, names[ev]);
4334
+ }
4335
+ #endif
4336
+ // Fire protocol handler first, user handler second. See #2559
4337
+ if (ifp->pfn != NULL) ifp->pfn(ifp, ev, ev_data);
4318
4338
if (ifp->fn != NULL) ifp->fn(ifp, ev, ev_data);
4319
4339
}
4320
4340
@@ -20182,20 +20202,17 @@ bool mg_wifi_scan(void) {
20182
20202
return false;
20183
20203
}
20184
20204
20185
- bool mg_wifi_connect(char *ssid, char *pass) {
20186
- (void) ssid;
20187
- (void) pass;
20205
+ bool mg_wifi_connect(struct mg_wifi_data *wifi) {
20206
+ (void) wifi;
20188
20207
return mg_wifi_scan();
20189
20208
}
20190
20209
20191
20210
bool mg_wifi_disconnect(void) {
20192
20211
return mg_wifi_scan();
20193
20212
}
20194
20213
20195
- bool mg_wifi_ap_start(char *ssid, char *pass, unsigned int channel) {
20196
- (void) ssid;
20197
- (void) pass;
20198
- (void) channel;
20214
+ bool mg_wifi_ap_start(struct mg_wifi_data *wifi) {
20215
+ (void) wifi;
20199
20216
return mg_wifi_scan();
20200
20217
}
20201
20218
@@ -20656,28 +20673,44 @@ static size_t cmsis_rx(void *buf, size_t buflen, struct mg_tcpip_if *ifp) {
20656
20673
#endif
20657
20674
20658
20675
static struct mg_tcpip_if *s_ifp;
20676
+ static uint32_t s_ip, s_mask;
20659
20677
static bool s_link, s_auth, s_join;
20660
20678
20679
+ static void wifi_cb(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
20680
+ struct mg_wifi_data *wifi = &((struct mg_tcpip_driver_cyw_data *) ifp->driver_data)->wifi;
20681
+ if (wifi->apmode && ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_UP) {
20682
+ MG_DEBUG(("Access Point started"));
20683
+ s_ip = ifp->ip, ifp->ip = wifi->apip;
20684
+ s_mask = ifp->mask, ifp->mask = wifi->apmask;
20685
+ ifp->enable_dhcp_client = false;
20686
+ ifp->enable_dhcp_server = true;
20687
+ }
20688
+ }
20689
+
20661
20690
static bool cyw_init(uint8_t *mac);
20662
20691
static void cyw_poll(void);
20663
20692
20664
20693
static bool mg_tcpip_driver_cyw_init(struct mg_tcpip_if *ifp) {
20665
20694
struct mg_tcpip_driver_cyw_data *d =
20666
20695
(struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
20696
+ struct mg_wifi_data *wifi = &d->wifi;
20667
20697
if (MG_BIG_ENDIAN) {
20668
20698
MG_ERROR(("Big-endian host"));
20669
20699
return false;
20670
20700
}
20671
20701
s_ifp = ifp;
20702
+ s_ip = ifp->ip;
20703
+ s_mask = ifp->mask;
20672
20704
s_link = s_auth = s_join = false;
20705
+ ifp->pfn = wifi_cb;
20673
20706
if (!cyw_init(ifp->mac)) return false;
20674
20707
20675
- if (d ->apmode) {
20676
- MG_DEBUG(("Starting AP '%s' (%u)", d ->apssid, d ->apchannel));
20677
- return mg_wifi_ap_start(d->apssid, d->appass, d->apchannel );
20678
- } else if (d ->ssid != NULL && d ->pass != NULL) {
20679
- MG_DEBUG(("Connecting to '%s'", d ->ssid));
20680
- return mg_wifi_connect(d->ssid, d->pass );
20708
+ if (wifi ->apmode) {
20709
+ MG_DEBUG(("Starting AP '%s' (%u)", wifi ->apssid, wifi ->apchannel));
20710
+ return mg_wifi_ap_start(wifi );
20711
+ } else if (wifi ->ssid != NULL && wifi ->pass != NULL) {
20712
+ MG_DEBUG(("Connecting to '%s'", wifi ->ssid));
20713
+ return mg_wifi_connect(wifi );
20681
20714
}
20682
20715
return true;
20683
20716
}
@@ -20687,15 +20720,15 @@ size_t mg_tcpip_driver_cyw_output(const void *buf, size_t len,
20687
20720
struct mg_tcpip_if *ifp) {
20688
20721
struct mg_tcpip_driver_cyw_data *d =
20689
20722
(struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
20690
- return mg_cyw_tx(d->apmode ? 1 : 0, (void *) buf, len) >= len ? len : 0;
20723
+ return mg_cyw_tx(d->wifi. apmode ? 1 : 0, (void *) buf, len) >= len ? len : 0;
20691
20724
}
20692
20725
20693
20726
static bool mg_tcpip_driver_cyw_poll(struct mg_tcpip_if *ifp, bool s1) {
20694
20727
cyw_poll();
20695
20728
if (!s1) return false;
20696
20729
struct mg_tcpip_driver_cyw_data *d =
20697
20730
(struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
20698
- return d->apmode ? s_link : s_link && s_auth && s_join;
20731
+ return d->wifi. apmode ? s_link : s_link && s_auth && s_join;
20699
20732
}
20700
20733
20701
20734
struct mg_tcpip_driver mg_tcpip_driver_cyw = {mg_tcpip_driver_cyw_init,
@@ -21678,7 +21711,7 @@ static size_t cyw_spi_poll(uint8_t *response) {
21678
21711
cyw_spi_write(CYW_SPID_FUNC_BUS, CYW_BUS_SPI_INT, &val, sizeof(val));
21679
21712
return 0;
21680
21713
}
21681
- cyw_spi_read(CYW_SPID_FUNC_WLAN, 0, response, len);
21714
+ cyw_spi_read(CYW_SPID_FUNC_WLAN, 0, response, (uint16_t) len);
21682
21715
return len;
21683
21716
}
21684
21717
@@ -21741,7 +21774,7 @@ static bool cyw_spi_init() {
21741
21774
cyw_set_backplane_window(CYW_CHIP_CHIPCOMMON); // set backplane window to start of CHIPCOMMON area
21742
21775
cyw_spi_read(CYW_SPID_FUNC_CHIP, (CYW_CHIP_CHIPCOMMON + 0x00) & CYW_CHIP_BCKPLN_ADDRMSK, &val, 2);
21743
21776
if (val == 43430) val = 4343;
21744
- MG_INFO(("WLAN chip is CYW%u%c", val) , val == 4343 ? 'W' : ' '));
21777
+ MG_INFO(("WLAN chip is CYW%u%c", val, val == 4343 ? 'W' : ' '));
21745
21778
21746
21779
// Load firmware (code and NVRAM)
21747
21780
if (!cyw_load_firmware(d->fw)) return false;
@@ -22019,16 +22052,21 @@ bool mg_wifi_scan(void) {
22019
22052
return cyw_wifi_scan();
22020
22053
}
22021
22054
22022
- bool mg_wifi_connect(char *ssid, char *pass) {
22023
- return cyw_wifi_connect(ssid, pass);
22055
+ bool mg_wifi_connect(struct mg_wifi_data *wifi) {
22056
+ s_ifp->ip = s_ip;
22057
+ s_ifp->mask = s_mask;
22058
+ if (s_ifp->ip == 0) s_ifp->enable_dhcp_client = true;
22059
+ s_ifp->enable_dhcp_server = false;
22060
+ MG_DEBUG(("Connecting to %s", wifi->ssid));
22061
+ return cyw_wifi_connect(wifi->ssid, wifi->pass);
22024
22062
}
22025
22063
22026
22064
bool mg_wifi_disconnect(void) {
22027
22065
return cyw_wifi_disconnect();
22028
22066
}
22029
22067
22030
- bool mg_wifi_ap_start(char *ssid, char *pass, unsigned int channel ) {
22031
- return cyw_wifi_ap_start(ssid, pass, channel );
22068
+ bool mg_wifi_ap_start(struct mg_wifi_data *wifi ) {
22069
+ return cyw_wifi_ap_start(wifi->apssid, wifi->appass, wifi->apchannel );
22032
22070
}
22033
22071
22034
22072
bool mg_wifi_ap_stop(void) {
@@ -22424,23 +22462,43 @@ bool mg_phy_up(struct mg_phy *phy, uint8_t phy_addr, bool *full_duplex,
22424
22462
22425
22463
22426
22464
static struct mg_tcpip_if *s_ifp;
22465
+ static uint32_t s_ip, s_mask;
22466
+ static bool s_aplink = false, s_scanning = false;
22467
+ static bool s_stalink = false, s_connecting = false;
22468
+
22469
+ static void wifi_cb(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
22470
+ struct mg_wifi_data *wifi =
22471
+ &((struct mg_tcpip_driver_pico_w_data *) ifp->driver_data)->wifi;
22472
+ if (wifi->apmode && ev == MG_TCPIP_EV_ST_CHG &&
22473
+ *(uint8_t *) ev_data == MG_TCPIP_STATE_UP) {
22474
+ MG_DEBUG(("Access Point started"));
22475
+ s_ip = ifp->ip, ifp->ip = wifi->apip;
22476
+ s_mask = ifp->mask, ifp->mask = wifi->apmask;
22477
+ ifp->enable_dhcp_client = false;
22478
+ ifp->enable_dhcp_server = true;
22479
+ }
22480
+ }
22427
22481
22428
22482
static bool mg_tcpip_driver_pico_w_init(struct mg_tcpip_if *ifp) {
22429
22483
struct mg_tcpip_driver_pico_w_data *d =
22430
22484
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
22485
+ struct mg_wifi_data *wifi = &d->wifi;
22431
22486
s_ifp = ifp;
22487
+ s_ip = ifp->ip;
22488
+ s_mask = ifp->mask;
22489
+ ifp->pfn = wifi_cb;
22432
22490
if (cyw43_arch_init() != 0)
22433
22491
return false; // initialize async_context and WiFi chip
22434
- if (d ->apmode && d ->apssid != NULL) {
22435
- MG_DEBUG(("Starting AP '%s' (%u)", d ->apssid, d ->apchannel));
22436
- if (!mg_wifi_ap_start(d->apssid, d->appass, d->apchannel )) return false;
22492
+ if (wifi ->apmode && wifi ->apssid != NULL) {
22493
+ MG_DEBUG(("Starting AP '%s' (%u)", wifi ->apssid, wifi ->apchannel));
22494
+ if (!mg_wifi_ap_start(wifi )) return false;
22437
22495
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac); // same MAC
22438
22496
} else {
22439
22497
cyw43_arch_enable_sta_mode();
22440
22498
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac);
22441
- if (d ->ssid != NULL) {
22442
- MG_DEBUG(("Connecting to '%s'", d ->ssid));
22443
- return mg_wifi_connect(d->ssid, d->pass );
22499
+ if (wifi ->ssid != NULL) {
22500
+ MG_DEBUG(("Connecting to '%s'", wifi ->ssid));
22501
+ return mg_wifi_connect(wifi );
22444
22502
} else {
22445
22503
cyw43_arch_disable_sta_mode();
22446
22504
}
@@ -22453,35 +22511,33 @@ static size_t mg_tcpip_driver_pico_w_tx(const void *buf, size_t len,
22453
22511
struct mg_tcpip_driver_pico_w_data *d =
22454
22512
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
22455
22513
return cyw43_send_ethernet(&cyw43_state,
22456
- d->apmode ? CYW43_ITF_AP : CYW43_ITF_STA, len, buf ,
22457
- false) == 0
22514
+ d->wifi. apmode ? CYW43_ITF_AP : CYW43_ITF_STA, len,
22515
+ buf, false) == 0
22458
22516
? len
22459
22517
: 0;
22460
22518
}
22461
22519
22462
- static bool s_aplink = false, s_scanning = false;
22463
- static bool s_stalink = false, s_connecting = false;
22464
-
22465
22520
static bool mg_tcpip_driver_pico_w_poll(struct mg_tcpip_if *ifp, bool s1) {
22466
22521
cyw43_arch_poll(); // not necessary, except when IRQs are disabled (OTA)
22467
22522
if (s_scanning && !cyw43_wifi_scan_active(&cyw43_state)) {
22468
22523
MG_VERBOSE(("scan complete"));
22469
22524
s_scanning = 0;
22470
- mg_tcpip_call(s_ifp , MG_TCPIP_EV_WIFI_SCAN_END, NULL);
22525
+ mg_tcpip_call(ifp , MG_TCPIP_EV_WIFI_SCAN_END, NULL);
22471
22526
}
22472
22527
if (ifp->update_mac_hash_table) {
22473
22528
// first call to _poll() is after _init(), so this is safe
22474
- cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *)mcast_addr, true);
22529
+ cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *) mcast_addr,
22530
+ true);
22475
22531
ifp->update_mac_hash_table = false;
22476
22532
}
22477
22533
if (!s1) return false;
22478
22534
struct mg_tcpip_driver_pico_w_data *d =
22479
22535
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
22480
- if (d->apmode) return s_aplink;
22536
+ if (d->wifi. apmode) return s_aplink;
22481
22537
int sdkstate = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
22482
22538
MG_VERBOSE(("conn: %c state: %d", s_connecting ? '1' : '0', sdkstate));
22483
22539
if (sdkstate < 0 && s_connecting) {
22484
- mg_tcpip_call(s_ifp , MG_TCPIP_EV_WIFI_CONNECT_ERR, &sdkstate);
22540
+ mg_tcpip_call(ifp , MG_TCPIP_EV_WIFI_CONNECT_ERR, &sdkstate);
22485
22541
s_connecting = false;
22486
22542
}
22487
22543
return s_stalink;
@@ -22512,7 +22568,7 @@ void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) {
22512
22568
}
22513
22569
void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) {
22514
22570
if (itf == CYW43_ITF_AP) {
22515
- s_aplink = false;
22571
+ s_aplink = false;
22516
22572
} else {
22517
22573
s_stalink = false;
22518
22574
// SDK calls this before we check status, don't clear s_connecting here
@@ -22552,9 +22608,15 @@ bool mg_wifi_scan(void) {
22552
22608
return res;
22553
22609
}
22554
22610
22555
- bool mg_wifi_connect(char *ssid, char *pass) {
22611
+ bool mg_wifi_connect(struct mg_wifi_data *wifi) {
22612
+ s_ifp->ip = s_ip;
22613
+ s_ifp->mask = s_mask;
22614
+ if (s_ifp->ip == 0) s_ifp->enable_dhcp_client = true;
22615
+ s_ifp->enable_dhcp_server = false;
22556
22616
cyw43_arch_enable_sta_mode();
22557
- int res = cyw43_arch_wifi_connect_async(ssid, pass, CYW43_AUTH_WPA2_AES_PSK);
22617
+ MG_DEBUG(("Connecting to %s", wifi->ssid));
22618
+ int res = cyw43_arch_wifi_connect_async(wifi->ssid, wifi->pass,
22619
+ CYW43_AUTH_WPA2_AES_PSK);
22558
22620
MG_VERBOSE(("res: %d", res));
22559
22621
if (res == 0) s_connecting = true;
22560
22622
return (res == 0);
@@ -22566,9 +22628,10 @@ bool mg_wifi_disconnect(void) {
22566
22628
return true;
22567
22629
}
22568
22630
22569
- bool mg_wifi_ap_start(char *ssid, char *pass, unsigned int channel) {
22570
- cyw43_wifi_ap_set_channel(&cyw43_state, channel);
22571
- cyw43_arch_enable_ap_mode(ssid, pass, CYW43_AUTH_WPA2_AES_PSK);
22631
+ bool mg_wifi_ap_start(struct mg_wifi_data *wifi) {
22632
+ cyw43_wifi_ap_set_channel(&cyw43_state, wifi->apchannel);
22633
+ cyw43_arch_enable_ap_mode(wifi->apssid, wifi->appass,
22634
+ CYW43_AUTH_WPA2_AES_PSK);
22572
22635
return true;
22573
22636
}
22574
22637
0 commit comments