diff --git a/esp-link/cgiwifi.c b/esp-link/cgiwifi.c index 821328a..1fe7ade 100644 --- a/esp-link/cgiwifi.c +++ b/esp-link/cgiwifi.c @@ -21,8 +21,11 @@ Cgi/template routines for the /wifi url. #include "config.h" #include "log.h" -//#define SLEEP_MODE LIGHT_SLEEP_T -#define SLEEP_MODE MODEM_SLEEP_T +#ifdef CGIWIFI_DBG +#define DBG(format, ...) os_printf(format, ## __VA_ARGS__) +#else +#define DBG(format, ...) do { } while(0) +#endif // ===== wifi status change callbacks static WifiStateChangeCb wifi_state_change_cb[4]; @@ -55,48 +58,36 @@ static void ICACHE_FLASH_ATTR wifiHandleEventCb(System_Event_t *evt) { case EVENT_STAMODE_CONNECTED: wifiState = wifiIsConnected; wifiReason = 0; -#ifdef CGIWIFI_DBG - os_printf("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid, + DBG("Wifi connected to ssid %s, ch %d\n", evt->event_info.connected.ssid, evt->event_info.connected.channel); -#endif statusWifiUpdate(wifiState); break; case EVENT_STAMODE_DISCONNECTED: wifiState = wifiIsDisconnected; wifiReason = evt->event_info.disconnected.reason; -#ifdef CGIWIFI_DBG - os_printf("Wifi disconnected from ssid %s, reason %s (%d)\n", + DBG("Wifi disconnected from ssid %s, reason %s (%d)\n", evt->event_info.disconnected.ssid, wifiGetReason(), evt->event_info.disconnected.reason); -#endif statusWifiUpdate(wifiState); break; case EVENT_STAMODE_AUTHMODE_CHANGE: -#ifdef CGIWIFI_DBG - os_printf("Wifi auth mode: %d -> %d\n", + DBG("Wifi auth mode: %d -> %d\n", evt->event_info.auth_change.old_mode, evt->event_info.auth_change.new_mode); -#endif break; case EVENT_STAMODE_GOT_IP: wifiState = wifiGotIP; wifiReason = 0; -#ifdef CGIWIFI_DBG - os_printf("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n", + DBG("Wifi got ip:" IPSTR ",mask:" IPSTR ",gw:" IPSTR "\n", IP2STR(&evt->event_info.got_ip.ip), IP2STR(&evt->event_info.got_ip.mask), IP2STR(&evt->event_info.got_ip.gw)); -#endif statusWifiUpdate(wifiState); break; case EVENT_SOFTAPMODE_STACONNECTED: -#ifdef CGIWIFI_DBG - os_printf("Wifi AP: station " MACSTR " joined, AID = %d\n", + DBG("Wifi AP: station " MACSTR " joined, AID = %d\n", MAC2STR(evt->event_info.sta_connected.mac), evt->event_info.sta_connected.aid); -#endif break; case EVENT_SOFTAPMODE_STADISCONNECTED: -#ifdef CGIWIFI_DBG - os_printf("Wifi AP: station " MACSTR " left, AID = %d\n", + DBG("Wifi AP: station " MACSTR " left, AID = %d\n", MAC2STR(evt->event_info.sta_disconnected.mac), evt->event_info.sta_disconnected.aid); -#endif break; default: break; @@ -116,9 +107,7 @@ wifiAddStateChangeCb(WifiStateChangeCb cb) { return; } } -#ifdef CGIWIFI_DBG - os_printf("WIFI: max state change cb count exceeded\n"); -#endif + DBG("WIFI: max state change cb count exceeded\n"); } // ===== wifi scanning @@ -147,9 +136,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { struct bss_info *bss_link = (struct bss_info *)arg; if (status!=OK) { -#ifdef CGIWIFI_DBG - os_printf("wifiScanDoneCb status=%d\n", status); -#endif + DBG("wifiScanDoneCb status=%d\n", status); cgiWifiAps.scanInProgress=0; return; } @@ -169,9 +156,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { //Allocate memory for access point data cgiWifiAps.apData=(ApData **)os_malloc(sizeof(ApData *)*n); cgiWifiAps.noAps=n; -#ifdef CGIWIFI_DBG - os_printf("Scan done: found %d APs\n", n); -#endif + DBG("Scan done: found %d APs\n", n); //Copy access point data to the static struct n=0; @@ -180,9 +165,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { if (n>=cgiWifiAps.noAps) { //This means the bss_link changed under our nose. Shouldn't happen! //Break because otherwise we will write in unallocated memory. -#ifdef CGIWIFI_DBG - os_printf("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps); -#endif + DBG("Huh? I have more than the allocated %d aps!\n", cgiWifiAps.noAps); break; } //Save the ap data. @@ -190,9 +173,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { cgiWifiAps.apData[n]->rssi=bss_link->rssi; cgiWifiAps.apData[n]->enc=bss_link->authmode; strncpy(cgiWifiAps.apData[n]->ssid, (char*)bss_link->ssid, 32); -#ifdef CGIWIFI_DBG - os_printf("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi); -#endif + DBG("bss%d: %s (%d)\n", n+1, (char*)bss_link->ssid, bss_link->rssi); bss_link = bss_link->next.stqe_next; n++; @@ -203,9 +184,7 @@ void ICACHE_FLASH_ATTR wifiScanDoneCb(void *arg, STATUS status) { static ETSTimer scanTimer; static void ICACHE_FLASH_ATTR scanStartCb(void *arg) { -#ifdef CGIWIFI_DBG - os_printf("Starting a scan\n"); -#endif + DBG("Starting a scan\n"); wifi_station_scan(NULL, wifiScanDoneCb); } @@ -216,7 +195,7 @@ static int ICACHE_FLASH_ATTR cgiWiFiStartScan(HttpdConnData *connData) { cgiWifiAps.scanInProgress = 1; os_timer_disarm(&scanTimer); os_timer_setfn(&scanTimer, scanStartCb, NULL); - os_timer_arm(&scanTimer, 1000, 0); + os_timer_arm(&scanTimer, 200, 0); } return HTTPD_CGI_DONE; } @@ -290,35 +269,26 @@ static ETSTimer resetTimer; static void ICACHE_FLASH_ATTR resetTimerCb(void *arg) { int x = wifi_station_get_connect_status(); int m = wifi_get_opmode() & 0x3; -#ifdef CGIWIFI_DBG - os_printf("Wifi check: mode=%s status=%d\n", wifiMode[m], x); -#endif + DBG("Wifi check: mode=%s status=%d\n", wifiMode[m], x); if (x == STATION_GOT_IP) { if (m != 1) { #ifdef CHANGE_TO_STA // We're happily connected, go to STA mode -#ifdef CGIWIFI_DBG - os_printf("Wifi got IP. Going into STA mode..\n"); -#endif + DBG("Wifi got IP. Going into STA mode..\n"); wifi_set_opmode(1); - wifi_set_sleep_type(SLEEP_MODE); - os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); + os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); // check one more time after switching to STA-only #endif } log_uart(false); // no more resetTimer at this point, gotta use physical reset to recover if in trouble } else { if (m != 3) { -#ifdef CGIWIFI_DBG - os_printf("Wifi connect failed. Going into STA+AP mode..\n"); -#endif + DBG("Wifi connect failed. Going into STA+AP mode..\n"); wifi_set_opmode(3); } log_uart(true); -#ifdef CGIWIFI_DBG - os_printf("Enabling/continuing uart log\n"); -#endif + DBG("Enabling/continuing uart log\n"); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); } } @@ -330,17 +300,17 @@ static ETSTimer reassTimer; // Callback actually doing reassociation static void ICACHE_FLASH_ATTR reassTimerCb(void *arg) { -#ifdef CGIWIFI_DBG - os_printf("Wifi changing association\n"); -#endif + DBG("Wifi changing association\n"); wifi_station_disconnect(); stconf.bssid_set = 0; wifi_station_set_config(&stconf); wifi_station_connect(); - // Schedule check + // Schedule check, we give some extra time (4x) 'cause the reassociation can cause the AP + // to have to change channel, and then the client needs to follow before it can see the + // IP address os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); - os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); + os_timer_arm(&resetTimer, 4*RESET_TIMEOUT, 0); } // This cgi uses the routines above to connect to a specific access point with the @@ -358,14 +328,12 @@ int ICACHE_FLASH_ATTR cgiWiFiConnect(HttpdConnData *connData) { //Set to 0 if you want to disable the actual reconnecting bit os_strncpy((char*)stconf.ssid, essid, 32); os_strncpy((char*)stconf.password, passwd, 64); -#ifdef CGIWIFI_DBG - os_printf("Wifi try to connect to AP %s pw %s\n", essid, passwd); -#endif + DBG("Wifi try to connect to AP %s pw %s\n", essid, passwd); //Schedule disconnect/connect os_timer_disarm(&reassTimer); os_timer_setfn(&reassTimer, reassTimerCb, NULL); - os_timer_arm(&reassTimer, 1000, 0); + os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it jsonHeader(connData, 200); } else { jsonHeader(connData, 400); @@ -421,9 +389,7 @@ static void ICACHE_FLASH_ATTR configWifiIP() { if (wifi_station_dhcpc_status() == DHCP_STARTED) wifi_station_dhcpc_stop(); wifi_station_dhcpc_start(); -#ifdef CGIWIFI_DBG - os_printf("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); -#endif + DBG("Wifi uses DHCP, hostname=%s\n", flashConfig.hostname); } else { // no DHCP, we got static network config! wifi_station_dhcpc_stop(); @@ -432,9 +398,7 @@ static void ICACHE_FLASH_ATTR configWifiIP() { ipi.netmask.addr = flashConfig.netmask; ipi.gw.addr = flashConfig.gateway; wifi_set_ip_info(0, &ipi); -#ifdef CGIWIFI_DBG - os_printf("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); -#endif + DBG("Wifi uses static IP %d.%d.%d.%d\n", IP2STR(&ipi.ip.addr)); } #ifdef DEBUGIP debugIP(); @@ -497,7 +461,7 @@ int ICACHE_FLASH_ATTR cgiWiFiSpecial(HttpdConnData *connData) { // schedule change-over os_timer_disarm(&reassTimer); os_timer_setfn(&reassTimer, configWifiIP, NULL); - os_timer_arm(&reassTimer, 1000, 0); + os_timer_arm(&reassTimer, 1000, 0); // 1 second for the response of this request to make it // return redirect info jsonHeader(connData, 200); httpdSend(connData, url, -1); @@ -514,13 +478,10 @@ int ICACHE_FLASH_ATTR cgiWiFiSetMode(HttpdConnData *connData) { len=httpdFindArg(connData->getArgs, "mode", buff, sizeof(buff)); if (len!=0) { int m = atoi(buff); -#ifdef CGIWIFI_DBG - os_printf("Wifi switching to mode %d\n", m); -#endif + DBG("Wifi switching to mode %d\n", m); wifi_set_opmode(m&3); if (m == 1) { - wifi_set_sleep_type(SLEEP_MODE); - // STA-only mode, reset into STA+AP after a timeout + // STA-only mode, reset into STA+AP after a timeout if we don't get an IP address os_timer_disarm(&resetTimer); os_timer_setfn(&resetTimer, resetTimerCb, NULL); os_timer_arm(&resetTimer, RESET_TIMEOUT, 0); @@ -619,9 +580,7 @@ int ICACHE_FLASH_ATTR cgiWiFiConnStatus(HttpdConnData *connData) { #endif len += os_sprintf(buff+len, "\"x\":0}\n"); -#ifdef CGIWIFI_DBG - //os_printf(" -> %s\n", buff); -#endif + //DBG(" -> %s\n", buff); httpdSend(connData, buff, len); return HTTPD_CGI_DONE; } @@ -644,13 +603,16 @@ int ICACHE_FLASH_ATTR cgiWifiInfo(HttpdConnData *connData) { // Init the wireless, which consists of setting a timer if we expect to connect to an AP // so we can revert to STA+AP mode if we can't connect. void ICACHE_FLASH_ATTR wifiInit() { - wifi_set_phy_mode(2); -#ifdef CGIWIFI_DBG + // wifi_set_phy_mode(2); // limit to 802.11b/g 'cause n is flaky int x = wifi_get_opmode() & 0x3; - os_printf("Wifi init, mode=%s\n", wifiMode[x]); -#endif + DBG("Wifi init, mode=%s\n", wifiMode[x]); configWifiIP(); + // The default sleep mode should be modem_sleep, but we set it here explicitly for good + // measure. We can't use light_sleep because that powers off everthing and we would loose + // all connections. + wifi_set_sleep_type(MODEM_SLEEP_T); + wifi_set_event_handler_cb(wifiHandleEventCb); // check on the wifi in a few seconds to see whether we need to switch mode os_timer_disarm(&resetTimer);