firmware/src/mesh/wifi/WiFiServerAPI.cpp

83 lines
1.9 KiB
C++
Raw Normal View History

2020-07-08 01:33:33 +00:00
#include "WiFiServerAPI.h"
#include "configuration.h"
#include <Arduino.h>
2021-02-07 01:17:46 +00:00
static WiFiServerPort *apiPort;
void initApiServer(int port)
2021-02-07 01:17:46 +00:00
{
// Start API server on port 4403
if (!apiPort) {
apiPort = new WiFiServerPort(port);
DEBUG_MSG("API server listening on TCP port %d\n", port);
2021-02-07 01:17:46 +00:00
apiPort->init();
}
}
2020-07-08 01:33:33 +00:00
WiFiServerAPI::WiFiServerAPI(WiFiClient &_client) : StreamAPI(&client), client(_client)
{
DEBUG_MSG("Incoming wifi connection\n");
2020-07-08 01:33:33 +00:00
}
WiFiServerAPI::~WiFiServerAPI()
{
client.stop();
// FIXME - delete this if the client dropps the connection!
}
/// override close to also shutdown the TCP link
void WiFiServerAPI::close()
{
client.stop(); // drop tcp connection
StreamAPI::close();
}
/// Check the current underlying physical link to see if the client is currently connected
bool WiFiServerAPI::checkIsConnected()
{
return client.connected();
}
int32_t WiFiServerAPI::runOnce()
2020-07-08 01:33:33 +00:00
{
if (client.connected()) {
return StreamAPI::runOnce();
} else {
DEBUG_MSG("Client dropped connection, suspending API service\n");
enabled = false; // we no longer need to run
return 0;
2020-07-08 01:33:33 +00:00
}
}
2021-08-18 16:25:17 +00:00
/// If an api server is running, we try to spit out debug 'serial' characters there
void WiFiServerPort::debugOut(char c)
{
if (apiPort && apiPort->openAPI)
apiPort->openAPI->debugOut(c);
}
2020-07-08 01:33:33 +00:00
WiFiServerPort::WiFiServerPort(int port) : WiFiServer(port), concurrency::OSThread("ApiServer") {}
2020-07-08 01:33:33 +00:00
void WiFiServerPort::init()
{
begin();
}
2020-10-10 01:57:57 +00:00
int32_t WiFiServerPort::runOnce()
2020-07-08 01:33:33 +00:00
{
auto client = available();
if (client) {
// Close any previous connection (see FIXME in header file)
if (openAPI) {
DEBUG_MSG("Force closing previous TCP connection\n");
delete openAPI;
}
openAPI = new WiFiServerAPI(client);
2020-07-08 01:33:33 +00:00
}
return 100; // only check occasionally for incoming connections
}