From 5a3e6cbaa167148f7817ab9d87dc848544b4752e Mon Sep 17 00:00:00 2001 From: Niel Nielsen Date: Wed, 2 Sep 2026 14:40:22 +0200 Subject: [PATCH] Refactor bwm_wifi_forward_status function parameters Signed-off-by: Niel Nielsen --- armsrc/bwm_wifi.c | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/armsrc/bwm_wifi.c b/armsrc/bwm_wifi.c index abe081699..4683d4e15 100644 --- a/armsrc/bwm_wifi.c +++ b/armsrc/bwm_wifi.c @@ -225,20 +225,33 @@ int bwm_wifi_forward_up(const char *ssid, const char *password, return PM3_SUCCESS; } -int bwm_wifi_forward_status(uint8_t *connected, uint32_t *ip_out) { +int bwm_wifi_forward_status(uint8_t *state, uint32_t *ip_out) { const uint32_t TO = 2000; - uint32_t ip = 0; - uint8_t ipb[12]; - uint16_t il = sizeof(ipb); - int r = bwm_cmd(BWM_CMD_GET_WIFI_CFG_IP_ADDR, NULL, 0, ipb, &il, TO); - if (r != PM3_SUCCESS) { - return r; // BWM / UART not responding + *ip_out = 0; + *state = BWM_WIFI_STATE_OFF; + + // Ask the ESP for the WiFi connect state (not just the IP): this tells apart + // off / disconnected / connecting / connected, which the IP alone cannot. + uint8_t st = 0; + uint16_t sl = sizeof(st); + int r = bwm_cmd(BWM_CMD_GET_WIFI_CONNECT_STATUS, NULL, 0, &st, &sl, TO); + if (r == PM3_ETIMEOUT) { + return r; // BWM genuinely not responding } - if (il >= 4) { - ip = (uint32_t)ipb[0] | ((uint32_t)ipb[1] << 8) | ((uint32_t)ipb[2] << 16) | ((uint32_t)ipb[3] << 24); + if (r != PM3_SUCCESS || sl < 1) { + *state = BWM_WIFI_STATE_OFF; // BWM answered; WiFi subsystem is off (BLE-only) + return PM3_SUCCESS; + } + *state = st; // 0 down, 1 connecting, 2 connected, 3 reconnecting, 4 stopped + + // Only chase an IP when actually connected. + if (st == BWM_WIFI_STATE_CONNECTED) { + uint8_t ipb[12]; + uint16_t il = sizeof(ipb); + if ((bwm_cmd(BWM_CMD_GET_WIFI_CFG_IP_ADDR, NULL, 0, ipb, &il, TO) == PM3_SUCCESS) && (il >= 4)) { + *ip_out = (uint32_t)ipb[0] | ((uint32_t)ipb[1] << 8) | ((uint32_t)ipb[2] << 16) | ((uint32_t)ipb[3] << 24); + } } - *ip_out = ip; - *connected = (ip != 0) ? 1 : 0; // a lease == a usable connection return PM3_SUCCESS; }