mirror of
https://github.com/alexhopeoconnor/firmware.git
synced 2026-10-22 03:02:08 +10:00
Merge branch 'develop' into pioarduino
This commit is contained in:
@@ -15,11 +15,11 @@ class PacketAPI : public PhoneAPI, public concurrency::OSThread
|
||||
static PacketAPI *create(PacketServer *_server);
|
||||
virtual ~PacketAPI(){};
|
||||
virtual int32_t runOnce();
|
||||
// Check the current underlying physical queue to see if the client is fetching packets
|
||||
bool checkIsConnected() override;
|
||||
|
||||
protected:
|
||||
explicit PacketAPI(PacketServer *_server);
|
||||
// Check the current underlying physical queue to see if the client is fetching packets
|
||||
bool checkIsConnected() override;
|
||||
|
||||
void onNowHasData(uint32_t fromRadioNum) override {}
|
||||
void onConnectionChanged(bool connected) override {}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
#include "ServerAPI.h"
|
||||
#include "Throttle.h"
|
||||
#include "configuration.h"
|
||||
#include <Arduino.h>
|
||||
|
||||
static constexpr uint32_t TCP_IDLE_TIMEOUT_MS = 15 * 60 * 1000UL;
|
||||
|
||||
template <typename T>
|
||||
ServerAPI<T>::ServerAPI(T &_client) : StreamAPI(&client), concurrency::OSThread("ServerAPI"), client(_client)
|
||||
{
|
||||
@@ -28,9 +31,16 @@ template <typename T> bool ServerAPI<T>::checkIsConnected()
|
||||
template <class T> int32_t ServerAPI<T>::runOnce()
|
||||
{
|
||||
if (client.connected()) {
|
||||
if (lastContactMsec > 0 && !Throttle::isWithinTimespanMs(lastContactMsec, TCP_IDLE_TIMEOUT_MS)) {
|
||||
LOG_WARN("TCP connection timeout, no data for %lu ms", (unsigned long)(millis() - lastContactMsec));
|
||||
close();
|
||||
enabled = false;
|
||||
return 0;
|
||||
}
|
||||
return StreamAPI::runOncePart();
|
||||
} else {
|
||||
LOG_INFO("Client dropped connection, suspend API service");
|
||||
close();
|
||||
enabled = false; // we no longer need to run
|
||||
return 0;
|
||||
}
|
||||
@@ -45,13 +55,18 @@ template <class T, class U> void APIServerPort<T, U>::init()
|
||||
|
||||
template <class T, class U> int32_t APIServerPort<T, U>::runOnce()
|
||||
{
|
||||
// Clean up previous connection if its client already disconnected
|
||||
if (openAPI && !openAPI->checkIsConnected()) {
|
||||
openAPI.reset();
|
||||
}
|
||||
|
||||
#ifdef ARCH_ESP32
|
||||
#if ESP_ARDUINO_VERSION >= ESP_ARDUINO_VERSION_VAL(3, 0, 0)
|
||||
auto client = U::accept();
|
||||
#else
|
||||
auto client = U::available();
|
||||
#endif
|
||||
#elif defined(ARCH_RP2040)
|
||||
#elif defined(ARCH_RP2040) || defined(ARCH_NRF52)
|
||||
auto client = U::accept();
|
||||
#else
|
||||
auto client = U::available();
|
||||
@@ -70,10 +85,10 @@ template <class T, class U> int32_t APIServerPort<T, U>::runOnce()
|
||||
}
|
||||
#endif
|
||||
LOG_INFO("Force close previous TCP connection");
|
||||
delete openAPI;
|
||||
openAPI.reset();
|
||||
}
|
||||
|
||||
openAPI = new T(client);
|
||||
openAPI.reset(new T(client));
|
||||
}
|
||||
|
||||
#if RAK_4631
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
#pragma once
|
||||
|
||||
#include "StreamAPI.h"
|
||||
#include <memory>
|
||||
|
||||
#define SERVER_API_DEFAULT_PORT 4403
|
||||
|
||||
@@ -21,15 +22,15 @@ template <class T> class ServerAPI : public StreamAPI, private concurrency::OSTh
|
||||
/// override close to also shutdown the TCP link
|
||||
virtual void close();
|
||||
|
||||
/// Check the current underlying physical link to see if the client is currently connected
|
||||
virtual bool checkIsConnected() override;
|
||||
|
||||
protected:
|
||||
/// We override this method to prevent publishing EVENT_SERIAL_CONNECTED/DISCONNECTED for wifi links (we want the board to
|
||||
/// stay in the POWERED state to prevent disabling wifi)
|
||||
virtual void onConnectionChanged(bool connected) override {}
|
||||
|
||||
virtual int32_t runOnce() override; // Check for dropped client connections
|
||||
|
||||
/// Check the current underlying physical link to see if the client is currently connected
|
||||
virtual bool checkIsConnected() override;
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -42,7 +43,7 @@ template <class T, class U> class APIServerPort : public U, private concurrency:
|
||||
* 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.
|
||||
*/
|
||||
T *openAPI = NULL;
|
||||
std::unique_ptr<T> openAPI;
|
||||
#if defined(RAK_4631) || defined(RAK11310)
|
||||
// Track wait time for RAK13800 Ethernet requests
|
||||
int32_t waitTime = 100;
|
||||
|
||||
@@ -17,6 +17,15 @@ void initApiServer(int port)
|
||||
}
|
||||
}
|
||||
|
||||
void deInitApiServer()
|
||||
{
|
||||
if (apiPort) {
|
||||
LOG_INFO("Deinit API server");
|
||||
delete apiPort;
|
||||
apiPort = nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
ethServerAPI::ethServerAPI(EthernetClient &_client) : ServerAPI(_client)
|
||||
{
|
||||
LOG_INFO("Incoming ethernet connection");
|
||||
|
||||
@@ -24,4 +24,5 @@ class ethServerPort : public APIServerPort<ethServerAPI, EthernetServer>
|
||||
};
|
||||
|
||||
void initApiServer(int port = SERVER_API_DEFAULT_PORT);
|
||||
void deInitApiServer();
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user