diff --git a/src/main.cpp b/src/main.cpp index c2b20b1c10..4a642ef6d8 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -92,6 +92,7 @@ NRF52Bluetooth *nrf52Bluetooth = nullptr; #include "mesh/raspihttp/PiWebServer.h" #include "platform/portduino/PortduinoGlue.h" #include "platform/portduino/USBHal.h" +#include #include #include #include @@ -1159,6 +1160,7 @@ void setup() #if __has_include() if (settingsMap[webserverport] != -1) { piwebServerThread = new PiWebServerThread(); + std::atexit([] { delete piwebServerThread; }); } #endif initApiServer(TCPPort); @@ -1278,4 +1280,4 @@ void loop() mainDelay.delay(delayMsec); } } -#endif +#endif \ No newline at end of file diff --git a/src/mesh/raspihttp/PiWebServer.cpp b/src/mesh/raspihttp/PiWebServer.cpp index 846d707236..9d2625410d 100644 --- a/src/mesh/raspihttp/PiWebServer.cpp +++ b/src/mesh/raspihttp/PiWebServer.cpp @@ -82,8 +82,6 @@ char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html" volatile bool isWebServerReady; volatile bool isCertReady; -HttpAPI webAPI; - PiWebServerThread *piwebServerThread; /** @@ -247,7 +245,7 @@ int handleAPIv1ToRadio(const struct _u_request *req, struct _u_response *res, vo portduinoVFS->mountpoint(configWeb.rootPath); LOG_DEBUG("Received %d bytes from PUT request", s); - webAPI.handleToRadio(buffer, s); + static_cast(user_data)->handleToRadio(buffer, s); LOG_DEBUG("end web->radio "); return U_CALLBACK_COMPLETE; } @@ -279,7 +277,7 @@ int handleAPIv1FromRadio(const struct _u_request *req, struct _u_response *res, if (valueAll == "true") { while (len) { - len = webAPI.getFromRadio(txBuf); + len = static_cast(user_data)->getFromRadio(txBuf); ulfius_set_response_properties(res, U_OPT_STATUS, 200, U_OPT_BINARY_BODY, txBuf, len); const char *tmpa = (const char *)txBuf; ulfius_set_string_body_response(res, 200, tmpa); @@ -289,7 +287,7 @@ int handleAPIv1FromRadio(const struct _u_request *req, struct _u_response *res, } // Otherwise, just return one protobuf } else { - len = webAPI.getFromRadio(txBuf); + len = static_cast(user_data)->getFromRadio(txBuf); const char *tmpa = (const char *)txBuf; ulfius_set_binary_body_response(res, 200, tmpa, len); // LOG_DEBUG("\n----webAPI response:"); @@ -497,10 +495,10 @@ PiWebServerThread::PiWebServerThread() u_map_put(instanceWeb.default_headers, "Access-Control-Allow-Origin", "*"); // Maximum body size sent by the client is 1 Kb instanceWeb.max_post_body_size = 1024; - ulfius_add_endpoint_by_val(&instanceWeb, "GET", PREFIX, "/api/v1/fromradio/*", 1, &handleAPIv1FromRadio, NULL); - ulfius_add_endpoint_by_val(&instanceWeb, "OPTIONS", PREFIX, "/api/v1/fromradio/*", 1, &handleAPIv1FromRadio, NULL); - ulfius_add_endpoint_by_val(&instanceWeb, "PUT", PREFIX, "/api/v1/toradio/*", 1, &handleAPIv1ToRadio, configWeb.rootPath); - ulfius_add_endpoint_by_val(&instanceWeb, "OPTIONS", PREFIX, "/api/v1/toradio/*", 1, &handleAPIv1ToRadio, NULL); + ulfius_add_endpoint_by_val(&instanceWeb, "GET", PREFIX, "/api/v1/fromradio/*", 1, &handleAPIv1FromRadio, &webAPI); + ulfius_add_endpoint_by_val(&instanceWeb, "OPTIONS", PREFIX, "/api/v1/fromradio/*", 1, &handleAPIv1FromRadio, &webAPI); + ulfius_add_endpoint_by_val(&instanceWeb, "PUT", PREFIX, "/api/v1/toradio/*", 1, &handleAPIv1ToRadio, &webAPI); + ulfius_add_endpoint_by_val(&instanceWeb, "OPTIONS", PREFIX, "/api/v1/toradio/*", 1, &handleAPIv1ToRadio, &webAPI); // Add callback function to all endpoints for the Web Server ulfius_add_endpoint_by_val(&instanceWeb, "GET", NULL, "/*", 2, &callback_static_file, &configWeb); @@ -525,13 +523,12 @@ PiWebServerThread::~PiWebServerThread() u_map_clean(&configWeb.mime_types); ulfius_stop_framework(&instanceWeb); - ulfius_stop_framework(&instanceWeb); + ulfius_clean_instance(&instanceWeb); free(configWeb.rootPath); - ulfius_clean_instance(&instanceService); - ulfius_clean_instance(&instanceService); + free(key_pem); free(cert_pem); LOG_INFO("End framework"); } #endif -#endif +#endif \ No newline at end of file diff --git a/src/mesh/raspihttp/PiWebServer.h b/src/mesh/raspihttp/PiWebServer.h index c4c49e9197..b45348cf32 100644 --- a/src/mesh/raspihttp/PiWebServer.h +++ b/src/mesh/raspihttp/PiWebServer.h @@ -23,6 +23,20 @@ struct _file_config { char *rootPath; }; +class HttpAPI : public PhoneAPI +{ + + public: + // Nothing here yet + + private: + // Nothing here yet + + protected: + /// Check the current underlying physical link to see if the client is currently connected + virtual bool checkIsConnected() override { return true; } // FIXME, be smarter about this +}; + class PiWebServerThread { private: @@ -30,6 +44,7 @@ class PiWebServerThread char *cert_pem = NULL; // struct _u_map mime_types; std::string webrootpath; + HttpAPI webAPI; public: PiWebServerThread(); @@ -38,21 +53,6 @@ class PiWebServerThread int CheckSSLandLoad(); uint32_t requestRestart = 0; struct _u_instance instanceWeb; - struct _u_instance instanceService; -}; - -class HttpAPI : public PhoneAPI -{ - - public: - // Nothing here yet - - private: - // Nothing here yet - - protected: - /// Check the current underlying physical link to see if the client is currently connected - virtual bool checkIsConnected() override { return true; } // FIXME, be smarter about this }; extern PiWebServerThread *piwebServerThread;