From 2996c7c8e26472e69686a4221ca446f7ad9b7370 Mon Sep 17 00:00:00 2001 From: geeksville Date: Fri, 25 Sep 2020 16:18:30 -0700 Subject: [PATCH] Make tcp API now work. Sample usage and caveats below: Sample usage: First configure device to use @mc-hamster's new wifi stuff: meshtastic --set wifi_ssid mylanname --set wifi_password mylanpassword Then reboot the device (so wifi starts up). (assuming device was assigned addr 192.168.81.45) meshtastic --info --host 192.168.81.45 (See the usual device info you previously had to get over USB) Caveats: * Currently we are limiting to one active TCP connection open at once, if you open a new session the old one is closed automatically * There are no access controls/authentication needed to open a TCP connection to the device * Currently main.cpp is kinda dumb about how we should schedule work and we rely on too many helper loop() functions. Very soon in my queue (related to all the other cleanup) is to add a basic notion of coroutines, so that we can get away from freertos threads and this old school arduino loop function. Once that cleanup happens we can the a) have much lower battery consumption (always) and b) super fast response for all operations. --- src/esp32/WiFiServerAPI.cpp | 10 +++++++++- src/esp32/WiFiServerAPI.h | 7 +++++++ 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/src/esp32/WiFiServerAPI.cpp b/src/esp32/WiFiServerAPI.cpp index 974b0f3c1..109340bff 100644 --- a/src/esp32/WiFiServerAPI.cpp +++ b/src/esp32/WiFiServerAPI.cpp @@ -52,6 +52,14 @@ void WiFiServerPort::loop() { auto client = available(); if (client) { - new WiFiServerAPI(client); + // Close any previous connection (see FIXME in header file) + if (openAPI) + delete openAPI; + + openAPI = new WiFiServerAPI(client); } + + if (openAPI) + // Allow idle processing so the API can read from its incoming stream + openAPI->loop(); } \ No newline at end of file diff --git a/src/esp32/WiFiServerAPI.h b/src/esp32/WiFiServerAPI.h index dcafa419d..a9b1e39b4 100644 --- a/src/esp32/WiFiServerAPI.h +++ b/src/esp32/WiFiServerAPI.h @@ -29,6 +29,13 @@ class WiFiServerAPI : public StreamAPI */ class WiFiServerPort : public WiFiServer { + /** The currently open port + * + * FIXME: We currently only allow one open TCP connection at a time, because we depend on the loop() call in this class to + * delegate to the worker. Once coroutines are implemented we can relax this restriction. + */ + WiFiServerAPI *openAPI = NULL; + public: WiFiServerPort();