diff --git a/src/esp32/WiFiServerAPI.cpp b/src/esp32/WiFiServerAPI.cpp new file mode 100644 index 00000000..b5d55560 --- /dev/null +++ b/src/esp32/WiFiServerAPI.cpp @@ -0,0 +1,57 @@ +#include "WiFiServerAPI.h" +#include "PowerFSM.h" +#include "configuration.h" +#include + +WiFiServerAPI::WiFiServerAPI(WiFiClient &_client) : StreamAPI(&client), client(_client) +{ + DEBUG_MSG("Incoming connection from %s\n", client.remoteIP().toString().c_str()); +} + +WiFiServerAPI::~WiFiServerAPI() +{ + client.stop(); + + // FIXME - delete this if the client dropps the connection! +} + +/// Hookable to find out when connection changes +void WiFiServerAPI::onConnectionChanged(bool connected) +{ + // FIXME - we really should be doing global reference counting to see if anyone is currently using serial or wifi and if so, + // block sleep + + if (connected) { // To prevent user confusion, turn off bluetooth while using the serial port api + powerFSM.trigger(EVENT_SERIAL_CONNECTED); + } else { + powerFSM.trigger(EVENT_SERIAL_DISCONNECTED); + } +} + +void WiFiServerAPI::loop() +{ + if (client.connected()) { + StreamAPI::loop(); + } else { + DEBUG_MSG("Client dropped connection, closing UDP server\n"); + delete this; + } +} + +#define MESHTASTIC_PORTNUM 4403 + +WiFiServerPort::WiFiServerPort() : WiFiServer(MESHTASTIC_PORTNUM) {} + +void WiFiServerPort::init() +{ + DEBUG_MSG("Listening on TCP port %d\n", MESHTASTIC_PORTNUM); + begin(); +} + +void WiFiServerPort::loop() +{ + auto client = available(); + if (client) { + new WiFiServerAPI(client); + } +} \ No newline at end of file diff --git a/src/esp32/WiFiServerAPI.h b/src/esp32/WiFiServerAPI.h new file mode 100644 index 00000000..dcafa419 --- /dev/null +++ b/src/esp32/WiFiServerAPI.h @@ -0,0 +1,38 @@ +#pragma once + +#include "StreamAPI.h" +#include + +/** + * Provides both debug printing and, if the client starts sending protobufs to us, switches to send/receive protobufs + * (and starts dropping debug printing - FIXME, eventually those prints should be encapsulated in protobufs). + */ +class WiFiServerAPI : public StreamAPI +{ + private: + WiFiClient client; + + public: + WiFiServerAPI(WiFiClient &_client); + + virtual ~WiFiServerAPI(); + + virtual void loop(); // Check for dropped client connections + + protected: + /// Hookable to find out when connection changes + virtual void onConnectionChanged(bool connected); +}; + +/** + * Listens for incoming connections and does accepts and creates instances of WiFiServerAPI as needed + */ +class WiFiServerPort : public WiFiServer +{ + public: + WiFiServerPort(); + + void init(); + + void loop(); +}; diff --git a/src/main.cpp b/src/main.cpp index c8b38e1c..c5823f1b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -25,17 +25,17 @@ #include "MeshService.h" #include "NEMAGPS.h" #include "NodeDB.h" -#include "concurrency/Periodic.h" #include "PowerFSM.h" #include "UBloxGPS.h" +#include "concurrency/Periodic.h" #include "configuration.h" #include "error.h" #include "power.h" // #include "rom/rtc.h" #include "DSRRouter.h" #include "debug.h" -#include "main.h" #include "graphics/Screen.h" +#include "main.h" #include "sleep.h" #include "timing.h" #include @@ -152,8 +152,8 @@ void userButtonPressedLong() #ifndef NO_ESP32 void initWifi() { - strcpy(radioConfig.preferences.wifi_ssid, "geeksville"); - strcpy(radioConfig.preferences.wifi_password, "xxx"); + // strcpy(radioConfig.preferences.wifi_ssid, "xxx"); + // strcpy(radioConfig.preferences.wifi_password, "xxx"); if (radioConfig.has_preferences) { const char *wifiName = radioConfig.preferences.wifi_ssid; @@ -164,7 +164,11 @@ void initWifi() } else { WiFi.mode(WIFI_MODE_STA); DEBUG_MSG("JOINING WIFI: ssid=%s\n", wifiName); - WiFi.begin(wifiName, wifiPsw); + if (WiFi.begin(wifiName, wifiPsw) == WL_CONNECTED) { + DEBUG_MSG("MY IP ADDRESS: %s\n", WiFi.localIP().toString().c_str()); + } else { + DEBUG_MSG("FAILED JOINING WIFI\n"); + } } } } else