Skip to content

Commit 387f875

Browse files
committed
Move interface stuff out of user handlers
1 parent df994df commit 387f875

File tree

14 files changed

+311
-231
lines changed

14 files changed

+311
-231
lines changed

mongoose.c

Lines changed: 105 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -4157,6 +4157,7 @@ void mg_mgr_init(struct mg_mgr *mgr) {
41574157
#endif
41584158

41594159

4160+
41604161
#if MG_ENABLE_TCPIP
41614162
#define MG_EPHEMERAL_PORT_BASE 32768
41624163
#define PDIFF(a, b) ((size_t) (((char *) (b)) - ((char *) (a))))
@@ -4315,6 +4316,25 @@ struct pkt {
43154316
};
43164317

43174318
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);
43184338
if (ifp->fn != NULL) ifp->fn(ifp, ev, ev_data);
43194339
}
43204340

@@ -20182,20 +20202,17 @@ bool mg_wifi_scan(void) {
2018220202
return false;
2018320203
}
2018420204

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;
2018820207
return mg_wifi_scan();
2018920208
}
2019020209

2019120210
bool mg_wifi_disconnect(void) {
2019220211
return mg_wifi_scan();
2019320212
}
2019420213

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;
2019920216
return mg_wifi_scan();
2020020217
}
2020120218

@@ -20656,28 +20673,44 @@ static size_t cmsis_rx(void *buf, size_t buflen, struct mg_tcpip_if *ifp) {
2065620673
#endif
2065720674

2065820675
static struct mg_tcpip_if *s_ifp;
20676+
static uint32_t s_ip, s_mask;
2065920677
static bool s_link, s_auth, s_join;
2066020678

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+
2066120690
static bool cyw_init(uint8_t *mac);
2066220691
static void cyw_poll(void);
2066320692

2066420693
static bool mg_tcpip_driver_cyw_init(struct mg_tcpip_if *ifp) {
2066520694
struct mg_tcpip_driver_cyw_data *d =
2066620695
(struct mg_tcpip_driver_cyw_data *) ifp->driver_data;
20696+
struct mg_wifi_data *wifi = &d->wifi;
2066720697
if (MG_BIG_ENDIAN) {
2066820698
MG_ERROR(("Big-endian host"));
2066920699
return false;
2067020700
}
2067120701
s_ifp = ifp;
20702+
s_ip = ifp->ip;
20703+
s_mask = ifp->mask;
2067220704
s_link = s_auth = s_join = false;
20705+
ifp->pfn = wifi_cb;
2067320706
if (!cyw_init(ifp->mac)) return false;
2067420707

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);
2068120714
}
2068220715
return true;
2068320716
}
@@ -20687,15 +20720,15 @@ size_t mg_tcpip_driver_cyw_output(const void *buf, size_t len,
2068720720
struct mg_tcpip_if *ifp) {
2068820721
struct mg_tcpip_driver_cyw_data *d =
2068920722
(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;
2069120724
}
2069220725

2069320726
static bool mg_tcpip_driver_cyw_poll(struct mg_tcpip_if *ifp, bool s1) {
2069420727
cyw_poll();
2069520728
if (!s1) return false;
2069620729
struct mg_tcpip_driver_cyw_data *d =
2069720730
(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;
2069920732
}
2070020733

2070120734
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) {
2167821711
cyw_spi_write(CYW_SPID_FUNC_BUS, CYW_BUS_SPI_INT, &val, sizeof(val));
2167921712
return 0;
2168021713
}
21681-
cyw_spi_read(CYW_SPID_FUNC_WLAN, 0, response, len);
21714+
cyw_spi_read(CYW_SPID_FUNC_WLAN, 0, response, (uint16_t)len);
2168221715
return len;
2168321716
}
2168421717

@@ -21741,7 +21774,7 @@ static bool cyw_spi_init() {
2174121774
cyw_set_backplane_window(CYW_CHIP_CHIPCOMMON); // set backplane window to start of CHIPCOMMON area
2174221775
cyw_spi_read(CYW_SPID_FUNC_CHIP, (CYW_CHIP_CHIPCOMMON + 0x00) & CYW_CHIP_BCKPLN_ADDRMSK, &val, 2);
2174321776
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' : ' '));
2174521778

2174621779
// Load firmware (code and NVRAM)
2174721780
if (!cyw_load_firmware(d->fw)) return false;
@@ -22019,16 +22052,21 @@ bool mg_wifi_scan(void) {
2201922052
return cyw_wifi_scan();
2202022053
}
2202122054

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);
2202422062
}
2202522063

2202622064
bool mg_wifi_disconnect(void) {
2202722065
return cyw_wifi_disconnect();
2202822066
}
2202922067

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);
2203222070
}
2203322071

2203422072
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,
2242422462

2242522463

2242622464
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+
}
2242722481

2242822482
static bool mg_tcpip_driver_pico_w_init(struct mg_tcpip_if *ifp) {
2242922483
struct mg_tcpip_driver_pico_w_data *d =
2243022484
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
22485+
struct mg_wifi_data *wifi = &d->wifi;
2243122486
s_ifp = ifp;
22487+
s_ip = ifp->ip;
22488+
s_mask = ifp->mask;
22489+
ifp->pfn = wifi_cb;
2243222490
if (cyw43_arch_init() != 0)
2243322491
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;
2243722495
cyw43_wifi_get_mac(&cyw43_state, CYW43_ITF_STA, ifp->mac); // same MAC
2243822496
} else {
2243922497
cyw43_arch_enable_sta_mode();
2244022498
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);
2244422502
} else {
2244522503
cyw43_arch_disable_sta_mode();
2244622504
}
@@ -22453,35 +22511,33 @@ static size_t mg_tcpip_driver_pico_w_tx(const void *buf, size_t len,
2245322511
struct mg_tcpip_driver_pico_w_data *d =
2245422512
(struct mg_tcpip_driver_pico_w_data *) ifp->driver_data;
2245522513
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
2245822516
? len
2245922517
: 0;
2246022518
}
2246122519

22462-
static bool s_aplink = false, s_scanning = false;
22463-
static bool s_stalink = false, s_connecting = false;
22464-
2246522520
static bool mg_tcpip_driver_pico_w_poll(struct mg_tcpip_if *ifp, bool s1) {
2246622521
cyw43_arch_poll(); // not necessary, except when IRQs are disabled (OTA)
2246722522
if (s_scanning && !cyw43_wifi_scan_active(&cyw43_state)) {
2246822523
MG_VERBOSE(("scan complete"));
2246922524
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);
2247122526
}
2247222527
if (ifp->update_mac_hash_table) {
2247322528
// 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);
2247522531
ifp->update_mac_hash_table = false;
2247622532
}
2247722533
if (!s1) return false;
2247822534
struct mg_tcpip_driver_pico_w_data *d =
2247922535
(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;
2248122537
int sdkstate = cyw43_wifi_link_status(&cyw43_state, CYW43_ITF_STA);
2248222538
MG_VERBOSE(("conn: %c state: %d", s_connecting ? '1' : '0', sdkstate));
2248322539
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);
2248522541
s_connecting = false;
2248622542
}
2248722543
return s_stalink;
@@ -22512,7 +22568,7 @@ void cyw43_cb_tcpip_set_link_up(cyw43_t *self, int itf) {
2251222568
}
2251322569
void cyw43_cb_tcpip_set_link_down(cyw43_t *self, int itf) {
2251422570
if (itf == CYW43_ITF_AP) {
22515-
s_aplink = false;
22571+
s_aplink = false;
2251622572
} else {
2251722573
s_stalink = false;
2251822574
// SDK calls this before we check status, don't clear s_connecting here
@@ -22552,9 +22608,15 @@ bool mg_wifi_scan(void) {
2255222608
return res;
2255322609
}
2255422610

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;
2255622616
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);
2255822620
MG_VERBOSE(("res: %d", res));
2255922621
if (res == 0) s_connecting = true;
2256022622
return (res == 0);
@@ -22566,9 +22628,10 @@ bool mg_wifi_disconnect(void) {
2256622628
return true;
2256722629
}
2256822630

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);
2257222635
return true;
2257322636
}
2257422637

mongoose.h

Lines changed: 24 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3024,28 +3024,37 @@ bool mg_ota_flash_end(struct mg_flash *flash);
30243024

30253025

30263026

3027+
struct mg_wifi_data {
3028+
char *ssid, *pass; // STA mode, SSID to connect to
3029+
char *apssid, *appass; // AP mode, our SSID
3030+
uint32_t apip, apmask; // AP mode, our IP address and mask
3031+
uint8_t security; // STA mode, TBD
3032+
uint8_t apsecurity; // AP mode, TBD
3033+
uint8_t apchannel; // AP mode, channel to use
3034+
bool apmode; // start in AP mode; 'false' -> connect to 'ssid' != NULL
3035+
};
3036+
30273037
struct mg_wifi_scan_bss_data {
3028-
struct mg_str SSID;
3029-
char *BSSID;
3030-
int16_t RSSI;
3031-
uint8_t security;
3038+
struct mg_str SSID;
3039+
char *BSSID;
3040+
int16_t RSSI;
3041+
uint8_t security;
30323042
#define MG_WIFI_SECURITY_OPEN 0
3033-
#define MG_WIFI_SECURITY_WEP MG_BIT(0)
3034-
#define MG_WIFI_SECURITY_WPA MG_BIT(1)
3043+
#define MG_WIFI_SECURITY_WEP MG_BIT(0)
3044+
#define MG_WIFI_SECURITY_WPA MG_BIT(1)
30353045
#define MG_WIFI_SECURITY_WPA2 MG_BIT(2)
30363046
#define MG_WIFI_SECURITY_WPA3 MG_BIT(3)
3037-
uint8_t channel;
3038-
unsigned band :2;
3047+
uint8_t channel;
3048+
unsigned band : 2;
30393049
#define MG_WIFI_BAND_2G 0
30403050
#define MG_WIFI_BAND_5G 1
3041-
unsigned has_n :1;
3051+
unsigned has_n : 1;
30423052
};
30433053

3044-
30453054
bool mg_wifi_scan(void);
3046-
bool mg_wifi_connect(char *ssid, char *pass);
3055+
bool mg_wifi_connect(struct mg_wifi_data *);
30473056
bool mg_wifi_disconnect(void);
3048-
bool mg_wifi_ap_start(char *ssid, char *pass, unsigned int channel);
3057+
bool mg_wifi_ap_start(struct mg_wifi_data *);
30493058
bool mg_wifi_ap_stop(void);
30503059

30513060

@@ -3097,6 +3106,7 @@ struct mg_tcpip_if {
30973106
bool update_mac_hash_table; // Signal drivers to update MAC controller
30983107
struct mg_tcpip_driver *driver; // Low level driver
30993108
void *driver_data; // Driver-specific data
3109+
mg_tcpip_event_handler_t pfn; // Driver-specific event handler function
31003110
mg_tcpip_event_handler_t fn; // User-specified event handler function
31013111
struct mg_mgr *mgr; // Mongoose event manager
31023112
struct mg_queue recv_queue; // Receive queue
@@ -3187,16 +3197,9 @@ struct mg_tcpip_driver_cyw_firmware {
31873197
};
31883198

31893199
struct mg_tcpip_driver_cyw_data {
3200+
struct mg_wifi_data wifi;
31903201
void *bus;
31913202
struct mg_tcpip_driver_cyw_firmware *fw;
3192-
char *ssid;
3193-
char *pass;
3194-
char *apssid;
3195-
char *appass;
3196-
uint8_t security; // TBD
3197-
uint8_t apsecurity; // TBD
3198-
uint8_t apchannel;
3199-
bool apmode; // start in AP mode; 'false' starts connection to 'ssid' if not NULL
32003203
bool hs; // use chip "high-speed" mode; otherwise SPI CPOL0 CPHA0 (DS 4.2.3 Table 6)
32013204
};
32023205

@@ -3299,14 +3302,7 @@ bool mg_phy_up(struct mg_phy *, uint8_t addr, bool *full_duplex,
32993302
#include "pico/unique_id.h" // keep this include
33003303

33013304
struct mg_tcpip_driver_pico_w_data {
3302-
char *ssid;
3303-
char *pass;
3304-
char *apssid;
3305-
char *appass;
3306-
uint8_t security; // TBD
3307-
uint8_t apsecurity; // TBD
3308-
uint8_t apchannel;
3309-
bool apmode; // start in AP mode; 'false' starts connection to 'ssid' if not NULL
3305+
struct mg_wifi_data wifi;
33103306
};
33113307

33123308
#define MG_TCPIP_DRIVER_INIT(mgr) \

0 commit comments

Comments
 (0)