@@ -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
@@ -20177,20 +20197,17 @@ bool mg_wifi_scan(void) {
20177
20197
return false;
20178
20198
}
20179
20199
20180
- bool mg_wifi_connect(char *ssid, char *pass) {
20181
- (void) ssid;
20182
- (void) pass;
20200
+ bool mg_wifi_connect(struct mg_wifi_data *wifi) {
20201
+ (void) wifi;
20183
20202
return mg_wifi_scan();
20184
20203
}
20185
20204
20186
20205
bool mg_wifi_disconnect(void) {
20187
20206
return mg_wifi_scan();
20188
20207
}
20189
20208
20190
- bool mg_wifi_ap_start(char *ssid, char *pass, unsigned int channel) {
20191
- (void) ssid;
20192
- (void) pass;
20193
- (void) channel;
20209
+ bool mg_wifi_ap_start(struct mg_wifi_data *wifi) {
20210
+ (void) wifi;
20194
20211
return mg_wifi_scan();
20195
20212
}
20196
20213
@@ -20651,28 +20668,44 @@ static size_t cmsis_rx(void *buf, size_t buflen, struct mg_tcpip_if *ifp) {
20651
20668
#endif
20652
20669
20653
20670
static struct mg_tcpip_if *s_ifp;
20671
+ static uint32_t s_ip, s_mask;
20654
20672
static bool s_link, s_auth, s_join;
20655
20673
20674
+ static void wifi_cb(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
20675
+ struct mg_wifi_data *wifi = &((struct mg_tcpip_driver_cyw_data *) ifp->driver_data)->wifi;
20676
+ if (wifi->apmode && ev == MG_TCPIP_EV_ST_CHG && *(uint8_t *) ev_data == MG_TCPIP_STATE_UP) {
20677
+ MG_DEBUG(("Access Point started"));
20678
+ s_ip = ifp->ip, ifp->ip = wifi->apip;
20679
+ s_mask = ifp->mask, ifp->mask = wifi->apmask;
20680
+ ifp->enable_dhcp_client = false;
20681
+ ifp->enable_dhcp_server = true;
20682
+ }
20683
+ }
20684
+
20656
20685
static bool cyw_init(uint8_t *mac);
20657
20686
static void cyw_poll(void);
20658
20687
20659
20688
static bool mg_tcpip_driver_cyw_init(struct mg_tcpip_if *ifp) {
20660
20689
struct mg_tcpip_driver_cyw_data *d =
20661
20690
(struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
20691
+ struct mg_wifi_data *wifi = &d->wifi;
20662
20692
if (MG_BIG_ENDIAN) {
20663
20693
MG_ERROR(("Big-endian host"));
20664
20694
return false;
20665
20695
}
20666
20696
s_ifp = ifp;
20697
+ s_ip = ifp->ip;
20698
+ s_mask = ifp->mask;
20667
20699
s_link = s_auth = s_join = false;
20700
+ ifp->pfn = wifi_cb;
20668
20701
if (!cyw_init(ifp->mac)) return false;
20669
20702
20670
- if (d ->apmode) {
20671
- MG_DEBUG(("Starting AP '%s' (%u)", d ->apssid, d ->apchannel));
20672
- return mg_wifi_ap_start(d->apssid, d->appass, d->apchannel );
20673
- } else if (d ->ssid != NULL && d ->pass != NULL) {
20674
- MG_DEBUG(("Connecting to '%s'", d ->ssid));
20675
- return mg_wifi_connect(d->ssid, d->pass );
20703
+ if (wifi ->apmode) {
20704
+ MG_DEBUG(("Starting AP '%s' (%u)", wifi ->apssid, wifi ->apchannel));
20705
+ return mg_wifi_ap_start(wifi );
20706
+ } else if (wifi ->ssid != NULL && wifi ->pass != NULL) {
20707
+ MG_DEBUG(("Connecting to '%s'", wifi ->ssid));
20708
+ return mg_wifi_connect(wifi );
20676
20709
}
20677
20710
return true;
20678
20711
}
@@ -20682,15 +20715,15 @@ size_t mg_tcpip_driver_cyw_output(const void *buf, size_t len,
20682
20715
struct mg_tcpip_if *ifp) {
20683
20716
struct mg_tcpip_driver_cyw_data *d =
20684
20717
(struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
20685
- return mg_cyw_tx(d->apmode ? 1 : 0, (void *) buf, len) >= len ? len : 0;
20718
+ return mg_cyw_tx(d->wifi. apmode ? 1 : 0, (void *) buf, len) >= len ? len : 0;
20686
20719
}
20687
20720
20688
20721
static bool mg_tcpip_driver_cyw_poll(struct mg_tcpip_if *ifp, bool s1) {
20689
20722
cyw_poll();
20690
20723
if (!s1) return false;
20691
20724
struct mg_tcpip_driver_cyw_data *d =
20692
20725
(struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
20693
- return d->apmode ? s_link : s_link && s_auth && s_join;
20726
+ return d->wifi. apmode ? s_link : s_link && s_auth && s_join;
20694
20727
}
20695
20728
20696
20729
struct mg_tcpip_driver mg_tcpip_driver_cyw = {mg_tcpip_driver_cyw_init,
@@ -21673,7 +21706,7 @@ static size_t cyw_spi_poll(uint8_t *response) {
21673
21706
cyw_spi_write(CYW_SPID_FUNC_BUS, CYW_BUS_SPI_INT, &val, sizeof(val));
21674
21707
return 0;
21675
21708
}
21676
- cyw_spi_read(CYW_SPID_FUNC_WLAN, 0, response, len);
21709
+ cyw_spi_read(CYW_SPID_FUNC_WLAN, 0, response, (uint16_t) len);
21677
21710
return len;
21678
21711
}
21679
21712
@@ -21736,7 +21769,7 @@ static bool cyw_spi_init() {
21736
21769
cyw_set_backplane_window(CYW_CHIP_CHIPCOMMON); // set backplane window to start of CHIPCOMMON area
21737
21770
cyw_spi_read(CYW_SPID_FUNC_CHIP, (CYW_CHIP_CHIPCOMMON + 0x00) & CYW_CHIP_BCKPLN_ADDRMSK, &val, 2);
21738
21771
if (val == 43430) val = 4343;
21739
- MG_INFO(("WLAN chip is CYW%u%c", val) , val == 4343 ? 'W' : ' '));
21772
+ MG_INFO(("WLAN chip is CYW%u%c", val, val == 4343 ? 'W' : ' '));
21740
21773
21741
21774
// Load firmware (code and NVRAM)
21742
21775
if (!cyw_load_firmware(d->fw)) return false;
@@ -22014,16 +22047,21 @@ bool mg_wifi_scan(void) {
22014
22047
return cyw_wifi_scan();
22015
22048
}
22016
22049
22017
- bool mg_wifi_connect(char *ssid, char *pass) {
22018
- return cyw_wifi_connect(ssid, pass);
22050
+ bool mg_wifi_connect(struct mg_wifi_data *wifi) {
22051
+ s_ifp->ip = s_ip;
22052
+ s_ifp->mask = s_mask;
22053
+ if (s_ifp->ip == 0) s_ifp->enable_dhcp_client = true;
22054
+ s_ifp->enable_dhcp_server = false;
22055
+ MG_DEBUG(("Connecting to %s", wifi->ssid));
22056
+ return cyw_wifi_connect(wifi->ssid, wifi->pass);
22019
22057
}
22020
22058
22021
22059
bool mg_wifi_disconnect(void) {
22022
22060
return cyw_wifi_disconnect();
22023
22061
}
22024
22062
22025
- bool mg_wifi_ap_start(char *ssid, char *pass, unsigned int channel ) {
22026
- return cyw_wifi_ap_start(ssid, pass, channel );
22063
+ bool mg_wifi_ap_start(struct mg_wifi_data *wifi ) {
22064
+ return cyw_wifi_ap_start(wifi->apssid, wifi->appass, wifi->apchannel );
22027
22065
}
22028
22066
22029
22067
bool mg_wifi_ap_stop(void) {
@@ -22419,23 +22457,43 @@ bool mg_phy_up(struct mg_phy *phy, uint8_t phy_addr, bool *full_duplex,
22419
22457
22420
22458
22421
22459
static struct mg_tcpip_if *s_ifp;
22460
+ static uint32_t s_ip, s_mask;
22461
+ static bool s_aplink = false, s_scanning = false;
22462
+ static bool s_stalink = false, s_connecting = false;
22463
+
22464
+ static void wifi_cb(struct mg_tcpip_if *ifp, int ev, void *ev_data) {
22465
+ struct mg_wifi_data *wifi =
22466
+ &((struct mg_tcpip_driver_pico_w_data *) ifp->driver_data)->wifi;
22467
+ if (wifi->apmode && ev == MG_TCPIP_EV_ST_CHG &&
22468
+ *(uint8_t *) ev_data == MG_TCPIP_STATE_UP) {
22469
+ MG_DEBUG(("Access Point started"));
22470
+ s_ip = ifp->ip, ifp->ip = wifi->apip;
22471
+ s_mask = ifp->mask, ifp->mask = wifi->apmask;
22472
+ ifp->enable_dhcp_client = false;
22473
+ ifp->enable_dhcp_server = true;
22474
+ }
22475
+ }
22422
22476
22423
22477
static bool mg_tcpip_driver_pico_w_init(struct mg_tcpip_if *ifp) {
22424
22478
struct mg_tcpip_driver_pico_w_data *d =
22425
22479
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
22480
+ struct mg_wifi_data *wifi = &d->wifi;
22426
22481
s_ifp = ifp;
22482
+ s_ip = ifp->ip;
22483
+ s_mask = ifp->mask;
22484
+ ifp->pfn = wifi_cb;
22427
22485
if (cyw43_arch_init() != 0)
22428
22486
return false; // initialize async_context and WiFi chip
22429
- if (d ->apmode && d ->apssid != NULL) {
22430
- MG_DEBUG(("Starting AP '%s' (%u)", d ->apssid, d ->apchannel));
22431
- if (!mg_wifi_ap_start(d->apssid, d->appass, d->apchannel )) return false;
22487
+ if (wifi ->apmode && wifi ->apssid != NULL) {
22488
+ MG_DEBUG(("Starting AP '%s' (%u)", wifi ->apssid, wifi ->apchannel));
22489
+ if (!mg_wifi_ap_start(wifi )) return false;
22432
22490
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac); // same MAC
22433
22491
} else {
22434
22492
cyw43_arch_enable_sta_mode();
22435
22493
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac);
22436
- if (d ->ssid != NULL) {
22437
- MG_DEBUG(("Connecting to '%s'", d ->ssid));
22438
- return mg_wifi_connect(d->ssid, d->pass );
22494
+ if (wifi ->ssid != NULL) {
22495
+ MG_DEBUG(("Connecting to '%s'", wifi ->ssid));
22496
+ return mg_wifi_connect(wifi );
22439
22497
} else {
22440
22498
cyw43_arch_disable_sta_mode();
22441
22499
}
@@ -22448,35 +22506,33 @@ static size_t mg_tcpip_driver_pico_w_tx(const void *buf, size_t len,
22448
22506
struct mg_tcpip_driver_pico_w_data *d =
22449
22507
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
22450
22508
return cyw43_send_ethernet(&cyw43_state,
22451
- d->apmode ? CYW43_ITF_AP : CYW43_ITF_STA, len, buf ,
22452
- false) == 0
22509
+ d->wifi. apmode ? CYW43_ITF_AP : CYW43_ITF_STA, len,
22510
+ buf, false) == 0
22453
22511
? len
22454
22512
: 0;
22455
22513
}
22456
22514
22457
- static bool s_aplink = false, s_scanning = false;
22458
- static bool s_stalink = false, s_connecting = false;
22459
-
22460
22515
static bool mg_tcpip_driver_pico_w_poll(struct mg_tcpip_if *ifp, bool s1) {
22461
22516
cyw43_arch_poll(); // not necessary, except when IRQs are disabled (OTA)
22462
22517
if (s_scanning && !cyw43_wifi_scan_active(&cyw43_state)) {
22463
22518
MG_VERBOSE(("scan complete"));
22464
22519
s_scanning = 0;
22465
- mg_tcpip_call(s_ifp , MG_TCPIP_EV_WIFI_SCAN_END, NULL);
22520
+ mg_tcpip_call(ifp , MG_TCPIP_EV_WIFI_SCAN_END, NULL);
22466
22521
}
22467
22522
if (ifp->update_mac_hash_table) {
22468
22523
// first call to _poll() is after _init(), so this is safe
22469
- cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *)mcast_addr, true);
22524
+ cyw43_wifi_update_multicast_filter(&cyw43_state, (uint8_t *) mcast_addr,
22525
+ true);
22470
22526
ifp->update_mac_hash_table = false;
22471
22527
}
22472
22528
if (!s1) return false;
22473
22529
struct mg_tcpip_driver_pico_w_data *d =
22474
22530
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
22475
- if (d->apmode) return s_aplink;
22531
+ if (d->wifi. apmode) return s_aplink;
22476
22532
int sdkstate = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
22477
22533
MG_VERBOSE(("conn: %c state: %d", s_connecting ? '1' : '0', sdkstate));
22478
22534
if (sdkstate < 0 && s_connecting) {
22479
- mg_tcpip_call(s_ifp , MG_TCPIP_EV_WIFI_CONNECT_ERR, &sdkstate);
22535
+ mg_tcpip_call(ifp , MG_TCPIP_EV_WIFI_CONNECT_ERR, &sdkstate);
22480
22536
s_connecting = false;
22481
22537
}
22482
22538
return s_stalink;
@@ -22507,7 +22563,7 @@ void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) {
22507
22563
}
22508
22564
void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) {
22509
22565
if (itf == CYW43_ITF_AP) {
22510
- s_aplink = false;
22566
+ s_aplink = false;
22511
22567
} else {
22512
22568
s_stalink = false;
22513
22569
// SDK calls this before we check status, don't clear s_connecting here
@@ -22547,9 +22603,15 @@ bool mg_wifi_scan(void) {
22547
22603
return res;
22548
22604
}
22549
22605
22550
- bool mg_wifi_connect(char *ssid, char *pass) {
22606
+ bool mg_wifi_connect(struct mg_wifi_data *wifi) {
22607
+ s_ifp->ip = s_ip;
22608
+ s_ifp->mask = s_mask;
22609
+ if (s_ifp->ip == 0) s_ifp->enable_dhcp_client = true;
22610
+ s_ifp->enable_dhcp_server = false;
22551
22611
cyw43_arch_enable_sta_mode();
22552
- int res = cyw43_arch_wifi_connect_async(ssid, pass, CYW43_AUTH_WPA2_AES_PSK);
22612
+ MG_DEBUG(("Connecting to %s", wifi->ssid));
22613
+ int res = cyw43_arch_wifi_connect_async(wifi->ssid, wifi->pass,
22614
+ CYW43_AUTH_WPA2_AES_PSK);
22553
22615
MG_VERBOSE(("res: %d", res));
22554
22616
if (res == 0) s_connecting = true;
22555
22617
return (res == 0);
@@ -22561,9 +22623,10 @@ bool mg_wifi_disconnect(void) {
22561
22623
return true;
22562
22624
}
22563
22625
22564
- bool mg_wifi_ap_start(char *ssid, char *pass, unsigned int channel) {
22565
- cyw43_wifi_ap_set_channel(&cyw43_state, channel);
22566
- cyw43_arch_enable_ap_mode(ssid, pass, CYW43_AUTH_WPA2_AES_PSK);
22626
+ bool mg_wifi_ap_start(struct mg_wifi_data *wifi) {
22627
+ cyw43_wifi_ap_set_channel(&cyw43_state, wifi->apchannel);
22628
+ cyw43_arch_enable_ap_mode(wifi->apssid, wifi->appass,
22629
+ CYW43_AUTH_WPA2_AES_PSK);
22567
22630
return true;
22568
22631
}
22569
22632
0 commit comments