From 9e9c50e6d8f10a9eb07259f75ef4e8f211ff5935 Mon Sep 17 00:00:00 2001 From: geeksville Date: Sat, 19 Sep 2020 12:54:49 -0700 Subject: [PATCH] Add API server on port 4403 (kinda a WIP, seems to work but I haven't finished the python client code) --- proto | 2 +- src/esp32/WiFiServerAPI.cpp | 2 +- src/main.cpp | 6 ++++-- src/meshwifi/meshwifi.cpp | 32 ++++++++++++++++++++++++++------ src/meshwifi/meshwifi.h | 6 +++++- src/nrf52/wifi-nrf52.cpp | 5 ++++- 6 files changed, 41 insertions(+), 12 deletions(-) diff --git a/proto b/proto index ce422b7c..4e431c84 160000 --- a/proto +++ b/proto @@ -1 +1 @@ -Subproject commit ce422b7c448906c6fee3eef64bbd41adfbc990f0 +Subproject commit 4e431c841015edfdde925acf5ee4ac0a2272edff diff --git a/src/esp32/WiFiServerAPI.cpp b/src/esp32/WiFiServerAPI.cpp index b5d55560..974b0f3c 100644 --- a/src/esp32/WiFiServerAPI.cpp +++ b/src/esp32/WiFiServerAPI.cpp @@ -44,7 +44,7 @@ WiFiServerPort::WiFiServerPort() : WiFiServer(MESHTASTIC_PORTNUM) {} void WiFiServerPort::init() { - DEBUG_MSG("Listening on TCP port %d\n", MESHTASTIC_PORTNUM); + DEBUG_MSG("API server sistening on TCP port %d\n", MESHTASTIC_PORTNUM); begin(); } diff --git a/src/main.cpp b/src/main.cpp index b0d133f2..bb4b12e1 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -37,12 +37,12 @@ #include "SPILock.h" #include "graphics/Screen.h" #include "main.h" +#include "meshwifi/meshhttp.h" +#include "meshwifi/meshwifi.h" #include "sleep.h" #include "timing.h" #include #include -#include "meshwifi/meshwifi.h" -#include "meshwifi/meshhttp.h" // #include #ifndef NO_ESP32 @@ -395,6 +395,8 @@ void loop() userButtonAlt.tick(); #endif + loopWifi(); + // Show boot screen for first 3 seconds, then switch to normal operation. static bool showingBootScreen = true; if (showingBootScreen && (timing::millis() > 3000)) { diff --git a/src/meshwifi/meshwifi.cpp b/src/meshwifi/meshwifi.cpp index f395ff7b..b5bef964 100644 --- a/src/meshwifi/meshwifi.cpp +++ b/src/meshwifi/meshwifi.cpp @@ -1,16 +1,18 @@ #include "meshwifi.h" #include "NodeDB.h" +#include "WiFiServerAPI.h" #include "configuration.h" #include "main.h" #include "meshwifi/meshhttp.h" -#include #include +#include static void WiFiEvent(WiFiEvent_t event); DNSServer dnsServer; +static WiFiServerPort *apiPort; -bool isWifiAvailable() +bool isWifiAvailable() { const char *wifiName = radioConfig.preferences.wifi_ssid; const char *wifiPsw = radioConfig.preferences.wifi_password; @@ -66,7 +68,7 @@ void initWifi() if (*wifiName && *wifiPsw) { if (radioConfig.preferences.wifi_ap_mode) { - + IPAddress apIP(192, 168, 42, 1); WiFi.onEvent(WiFiEvent); @@ -76,7 +78,6 @@ void initWifi() dnsServer.start(53, "*", apIP); - } else { WiFi.mode(WIFI_MODE_STA); WiFi.onEvent(WiFiEvent); @@ -94,6 +95,23 @@ void initWifi() DEBUG_MSG("Not using WIFI\n"); } +/// Perform idle loop processing required by the wifi layer +void loopWifi() +{ + // FIXME, once we have coroutines - just use a coroutine instead of this nasty loopWifi() + if (apiPort) + apiPort->loop(); +} + +static void initApiServer() +{ + // Start API server on port 4403 + if (!apiPort) { + apiPort = new WiFiServerPort(); + apiPort->init(); + } +} + static void WiFiEvent(WiFiEvent_t event) { DEBUG_MSG("************ [WiFi-event] event: %d ************\n", event); @@ -129,6 +147,7 @@ static void WiFiEvent(WiFiEvent_t event) // Start web server initWebServer(); + initApiServer(); break; case SYSTEM_EVENT_STA_LOST_IP: @@ -152,6 +171,7 @@ static void WiFiEvent(WiFiEvent_t event) // Start web server initWebServer(); + initApiServer(); break; case SYSTEM_EVENT_AP_STOP: @@ -189,11 +209,11 @@ static void WiFiEvent(WiFiEvent_t event) break; default: break; - } } -void handleDNSResponse() { +void handleDNSResponse() +{ if (radioConfig.preferences.wifi_ap_mode) { dnsServer.processNextRequest(); } diff --git a/src/meshwifi/meshwifi.h b/src/meshwifi/meshwifi.h index 44fff707..0b74592f 100644 --- a/src/meshwifi/meshwifi.h +++ b/src/meshwifi/meshwifi.h @@ -5,12 +5,16 @@ #include #ifdef HAS_WIFI -#include #include +#include #endif void initWifi(); void deinitWifi(); + +/// Perform idle loop processing required by the wifi layer +void loopWifi(); + bool isWifiAvailable(); void handleDNSResponse(); diff --git a/src/nrf52/wifi-nrf52.cpp b/src/nrf52/wifi-nrf52.cpp index e4885add..30321e5e 100644 --- a/src/nrf52/wifi-nrf52.cpp +++ b/src/nrf52/wifi-nrf52.cpp @@ -10,4 +10,7 @@ bool isWifiAvailable() return false; } -void handleWebResponse() {} \ No newline at end of file +void handleWebResponse() {} + +/// Perform idle loop processing required by the wifi layer +void loopWifi() {} \ No newline at end of file