mirror of
https://github.com/meshtastic/firmware.git
synced 2025-02-01 10:19:59 +00:00
Manage when destructor is called for native HttpAPI (#5807)
This commit is contained in:
parent
077ee02426
commit
e7802d960f
@ -92,6 +92,7 @@ NRF52Bluetooth *nrf52Bluetooth = nullptr;
|
|||||||
#include "mesh/raspihttp/PiWebServer.h"
|
#include "mesh/raspihttp/PiWebServer.h"
|
||||||
#include "platform/portduino/PortduinoGlue.h"
|
#include "platform/portduino/PortduinoGlue.h"
|
||||||
#include "platform/portduino/USBHal.h"
|
#include "platform/portduino/USBHal.h"
|
||||||
|
#include <cstdlib>
|
||||||
#include <fstream>
|
#include <fstream>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
@ -1159,6 +1160,7 @@ void setup()
|
|||||||
#if __has_include(<ulfius.h>)
|
#if __has_include(<ulfius.h>)
|
||||||
if (settingsMap[webserverport] != -1) {
|
if (settingsMap[webserverport] != -1) {
|
||||||
piwebServerThread = new PiWebServerThread();
|
piwebServerThread = new PiWebServerThread();
|
||||||
|
std::atexit([] { delete piwebServerThread; });
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
initApiServer(TCPPort);
|
initApiServer(TCPPort);
|
||||||
|
@ -82,8 +82,6 @@ char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"
|
|||||||
volatile bool isWebServerReady;
|
volatile bool isWebServerReady;
|
||||||
volatile bool isCertReady;
|
volatile bool isCertReady;
|
||||||
|
|
||||||
HttpAPI webAPI;
|
|
||||||
|
|
||||||
PiWebServerThread *piwebServerThread;
|
PiWebServerThread *piwebServerThread;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -247,7 +245,7 @@ int handleAPIv1ToRadio(const struct _u_request *req, struct _u_response *res, vo
|
|||||||
portduinoVFS->mountpoint(configWeb.rootPath);
|
portduinoVFS->mountpoint(configWeb.rootPath);
|
||||||
|
|
||||||
LOG_DEBUG("Received %d bytes from PUT request", s);
|
LOG_DEBUG("Received %d bytes from PUT request", s);
|
||||||
webAPI.handleToRadio(buffer, s);
|
static_cast<HttpAPI *>(user_data)->handleToRadio(buffer, s);
|
||||||
LOG_DEBUG("end web->radio ");
|
LOG_DEBUG("end web->radio ");
|
||||||
return U_CALLBACK_COMPLETE;
|
return U_CALLBACK_COMPLETE;
|
||||||
}
|
}
|
||||||
@ -279,7 +277,7 @@ int handleAPIv1FromRadio(const struct _u_request *req, struct _u_response *res,
|
|||||||
|
|
||||||
if (valueAll == "true") {
|
if (valueAll == "true") {
|
||||||
while (len) {
|
while (len) {
|
||||||
len = webAPI.getFromRadio(txBuf);
|
len = static_cast<HttpAPI *>(user_data)->getFromRadio(txBuf);
|
||||||
ulfius_set_response_properties(res, U_OPT_STATUS, 200, U_OPT_BINARY_BODY, txBuf, len);
|
ulfius_set_response_properties(res, U_OPT_STATUS, 200, U_OPT_BINARY_BODY, txBuf, len);
|
||||||
const char *tmpa = (const char *)txBuf;
|
const char *tmpa = (const char *)txBuf;
|
||||||
ulfius_set_string_body_response(res, 200, tmpa);
|
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
|
// Otherwise, just return one protobuf
|
||||||
} else {
|
} else {
|
||||||
len = webAPI.getFromRadio(txBuf);
|
len = static_cast<HttpAPI *>(user_data)->getFromRadio(txBuf);
|
||||||
const char *tmpa = (const char *)txBuf;
|
const char *tmpa = (const char *)txBuf;
|
||||||
ulfius_set_binary_body_response(res, 200, tmpa, len);
|
ulfius_set_binary_body_response(res, 200, tmpa, len);
|
||||||
// LOG_DEBUG("\n----webAPI response:");
|
// LOG_DEBUG("\n----webAPI response:");
|
||||||
@ -497,10 +495,10 @@ PiWebServerThread::PiWebServerThread()
|
|||||||
u_map_put(instanceWeb.default_headers, "Access-Control-Allow-Origin", "*");
|
u_map_put(instanceWeb.default_headers, "Access-Control-Allow-Origin", "*");
|
||||||
// Maximum body size sent by the client is 1 Kb
|
// Maximum body size sent by the client is 1 Kb
|
||||||
instanceWeb.max_post_body_size = 1024;
|
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, "GET", PREFIX, "/api/v1/fromradio/*", 1, &handleAPIv1FromRadio, &webAPI);
|
||||||
ulfius_add_endpoint_by_val(&instanceWeb, "OPTIONS", PREFIX, "/api/v1/fromradio/*", 1, &handleAPIv1FromRadio, NULL);
|
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, configWeb.rootPath);
|
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, NULL);
|
ulfius_add_endpoint_by_val(&instanceWeb, "OPTIONS", PREFIX, "/api/v1/toradio/*", 1, &handleAPIv1ToRadio, &webAPI);
|
||||||
|
|
||||||
// Add callback function to all endpoints for the Web Server
|
// Add callback function to all endpoints for the Web Server
|
||||||
ulfius_add_endpoint_by_val(&instanceWeb, "GET", NULL, "/*", 2, &callback_static_file, &configWeb);
|
ulfius_add_endpoint_by_val(&instanceWeb, "GET", NULL, "/*", 2, &callback_static_file, &configWeb);
|
||||||
@ -525,10 +523,9 @@ PiWebServerThread::~PiWebServerThread()
|
|||||||
u_map_clean(&configWeb.mime_types);
|
u_map_clean(&configWeb.mime_types);
|
||||||
|
|
||||||
ulfius_stop_framework(&instanceWeb);
|
ulfius_stop_framework(&instanceWeb);
|
||||||
ulfius_stop_framework(&instanceWeb);
|
ulfius_clean_instance(&instanceWeb);
|
||||||
free(configWeb.rootPath);
|
free(configWeb.rootPath);
|
||||||
ulfius_clean_instance(&instanceService);
|
free(key_pem);
|
||||||
ulfius_clean_instance(&instanceService);
|
|
||||||
free(cert_pem);
|
free(cert_pem);
|
||||||
LOG_INFO("End framework");
|
LOG_INFO("End framework");
|
||||||
}
|
}
|
||||||
|
@ -23,24 +23,6 @@ struct _file_config {
|
|||||||
char *rootPath;
|
char *rootPath;
|
||||||
};
|
};
|
||||||
|
|
||||||
class PiWebServerThread
|
|
||||||
{
|
|
||||||
private:
|
|
||||||
char *key_pem = NULL;
|
|
||||||
char *cert_pem = NULL;
|
|
||||||
// struct _u_map mime_types;
|
|
||||||
std::string webrootpath;
|
|
||||||
|
|
||||||
public:
|
|
||||||
PiWebServerThread();
|
|
||||||
~PiWebServerThread();
|
|
||||||
int CreateSSLCertificate();
|
|
||||||
int CheckSSLandLoad();
|
|
||||||
uint32_t requestRestart = 0;
|
|
||||||
struct _u_instance instanceWeb;
|
|
||||||
struct _u_instance instanceService;
|
|
||||||
};
|
|
||||||
|
|
||||||
class HttpAPI : public PhoneAPI
|
class HttpAPI : public PhoneAPI
|
||||||
{
|
{
|
||||||
|
|
||||||
@ -55,6 +37,24 @@ class HttpAPI : public PhoneAPI
|
|||||||
virtual bool checkIsConnected() override { return true; } // FIXME, be smarter about this
|
virtual bool checkIsConnected() override { return true; } // FIXME, be smarter about this
|
||||||
};
|
};
|
||||||
|
|
||||||
|
class PiWebServerThread
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
char *key_pem = NULL;
|
||||||
|
char *cert_pem = NULL;
|
||||||
|
// struct _u_map mime_types;
|
||||||
|
std::string webrootpath;
|
||||||
|
HttpAPI webAPI;
|
||||||
|
|
||||||
|
public:
|
||||||
|
PiWebServerThread();
|
||||||
|
~PiWebServerThread();
|
||||||
|
int CreateSSLCertificate();
|
||||||
|
int CheckSSLandLoad();
|
||||||
|
uint32_t requestRestart = 0;
|
||||||
|
struct _u_instance instanceWeb;
|
||||||
|
};
|
||||||
|
|
||||||
extern PiWebServerThread *piwebServerThread;
|
extern PiWebServerThread *piwebServerThread;
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user