Skip to content

Commit

Permalink
Manage when destructor is called for native HttpAPI (#5807)
Browse files Browse the repository at this point in the history
  • Loading branch information
esev authored Jan 11, 2025
1 parent 077ee02 commit e7802d9
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 29 deletions.
4 changes: 3 additions & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ NRF52Bluetooth *nrf52Bluetooth = nullptr;
#include "mesh/raspihttp/PiWebServer.h"
#include "platform/portduino/PortduinoGlue.h"
#include "platform/portduino/USBHal.h"
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <string>
Expand Down Expand Up @@ -1159,6 +1160,7 @@ void setup()
#if __has_include(<ulfius.h>)
if (settingsMap[webserverport] != -1) {
piwebServerThread = new PiWebServerThread();
std::atexit([] { delete piwebServerThread; });
}
#endif
initApiServer(TCPPort);
Expand Down Expand Up @@ -1278,4 +1280,4 @@ void loop()
mainDelay.delay(delayMsec);
}
}
#endif
#endif
23 changes: 10 additions & 13 deletions src/mesh/raspihttp/PiWebServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,6 @@ char contentTypes[][2][32] = {{".txt", "text/plain"}, {".html", "text/html"
volatile bool isWebServerReady;
volatile bool isCertReady;

HttpAPI webAPI;

PiWebServerThread *piwebServerThread;

/**
Expand Down Expand Up @@ -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<HttpAPI *>(user_data)->handleToRadio(buffer, s);
LOG_DEBUG("end web->radio ");
return U_CALLBACK_COMPLETE;
}
Expand Down Expand Up @@ -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<HttpAPI *>(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);
Expand All @@ -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<HttpAPI *>(user_data)->getFromRadio(txBuf);
const char *tmpa = (const char *)txBuf;
ulfius_set_binary_body_response(res, 200, tmpa, len);
// LOG_DEBUG("\n----webAPI response:");
Expand Down Expand Up @@ -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);
Expand All @@ -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
30 changes: 15 additions & 15 deletions src/mesh/raspihttp/PiWebServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,28 @@ 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:
char *key_pem = NULL;
char *cert_pem = NULL;
// struct _u_map mime_types;
std::string webrootpath;
HttpAPI webAPI;

public:
PiWebServerThread();
Expand All @@ -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;
Expand Down

0 comments on commit e7802d9

Please sign in to comment.