Skip to content

Commit

Permalink
split telemetry and wireless
Browse files Browse the repository at this point in the history
  • Loading branch information
rtlopez committed Nov 5, 2024
1 parent d2f4978 commit ae4a90a
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 140 deletions.
47 changes: 47 additions & 0 deletions lib/Espfc/src/TelemetryManager.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
#include "TelemetryManager.h"

namespace Espfc {

TelemetryManager::TelemetryManager(Model& model): _model(model), _msp(model), _text(model), _crsf(model) {}

int TelemetryManager::process(Device::SerialDevice& s, TelemetryProtocol protocol) const
{
Utils::Stats::Measure measure(_model.state.stats, COUNTER_TELEMETRY);

switch(protocol)
{
case TELEMETRY_PROTOCOL_TEXT:
_text.process(s);
break;
case TELEMETRY_PROTOCOL_CRSF:
_crsf.process(s);
break;
}

return 1;
}

int TelemetryManager::processMsp(Device::SerialDevice& s, TelemetryProtocol protocol, Connect::MspMessage m, uint8_t origin)
{
Connect::MspResponse r;

// not valid msp message, stop processing
if(!m.isReady() || !m.isCmd()) return 0;

_msp.processCommand(m, r, s);

switch(protocol)
{
case TELEMETRY_PROTOCOL_CRSF:
_crsf.sendMsp(s, r, origin);
break;
default:
break;
}

_msp.postCommand();

return 1;
}

}
45 changes: 4 additions & 41 deletions lib/Espfc/src/TelemetryManager.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include "Model.h"
#include "Device/SerialDevice.h"
#include "Telemetry/TelemetryText.h"
#include "Telemetry/TelemetryCRSF.h"
#include "Connect/Msp.h"
Expand All @@ -16,47 +17,9 @@ enum TelemetryProtocol {
class TelemetryManager
{
public:
TelemetryManager(Model& model): _model(model), _msp(model), _text(model), _crsf(model) {}

int process(Device::SerialDevice& s, TelemetryProtocol protocol) const
{
Utils::Stats::Measure measure(_model.state.stats, COUNTER_TELEMETRY);

switch(protocol)
{
case TELEMETRY_PROTOCOL_TEXT:
_text.process(s);
break;
case TELEMETRY_PROTOCOL_CRSF:
_crsf.process(s);
break;
}

return 1;
}

int processMsp(Device::SerialDevice& s, TelemetryProtocol protocol, Connect::MspMessage m, uint8_t origin)
{
Connect::MspResponse r;

// not valid msp message, stop processing
if(!m.isReady() || !m.isCmd()) return 0;

_msp.processCommand(m, r, s);

switch(protocol)
{
case TELEMETRY_PROTOCOL_CRSF:
_crsf.sendMsp(s, r, origin);
break;
default:
break;
}

_msp.postCommand();

return 1;
}
TelemetryManager(Model& model);
int process(Device::SerialDevice& s, TelemetryProtocol protocol) const;
int processMsp(Device::SerialDevice& s, TelemetryProtocol protocol, Connect::MspMessage m, uint8_t origin);

private:
Model& _model;
Expand Down
107 changes: 107 additions & 0 deletions lib/Espfc/src/Wireless.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
#include "Wireless.h"

#ifdef ESPFC_SERIAL_SOFT_0_WIFI

namespace Espfc {

Wireless::Wireless(Model& model): _model(model), _status(STOPPED), _server(1111), _adapter(_client) {}

int Wireless::begin()
{
WiFi.persistent(false);
#ifdef ESPFC_ESPNOW
if(_model.isFeatureActive(FEATURE_RX_SPI))
{
startAp();
}
#endif
return 1;
}

void Wireless::startAp()
{
bool status = WiFi.softAP("ESP-FC");
_model.logger.info().log(F("WIFI AP START")).logln(status);
}

int Wireless::connect()
{
#ifdef ESPFC_WIFI_ALT
// https://github.com/esp8266/Arduino/issues/2545#issuecomment-249222211
_events[0] = WiFi.onStationModeConnected([this](const WiFiEventStationModeConnected& ev) { this->wifiEventConnected(ev.ssid, ev.channel); });
_events[1] = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& ev) { this->wifiEventGotIp(ev.ip); });
_events[2] = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& ev) { this->wifiEventDisconnected(); });
_events[3] = WiFi.onSoftAPModeStationConnected([this](const WiFiEventSoftAPModeStationConnected& ev) { this->wifiEventApConnected(ev.mac); });
#elif defined(ESPFC_WIFI)
WiFi.onEvent([this](WiFiEvent_t ev, WiFiEventInfo_t info) {
this->wifiEventConnected(String(info.wifi_sta_connected.ssid, info.wifi_sta_connected.ssid_len), info.wifi_sta_connected.channel);
}, ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent([this](WiFiEvent_t ev, WiFiEventInfo_t info) { this->wifiEventGotIp(IPAddress(info.got_ip.ip_info.ip.addr)); }, ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent([this](WiFiEvent_t ev, WiFiEventInfo_t info) { this->wifiEventDisconnected(); }, ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
#endif
if(_model.config.wireless.ssid[0] != 0)
{
WiFi.begin(_model.config.wireless.ssid, _model.config.wireless.pass);
_model.logger.info().log(F("WIFI STA")).log(_model.config.wireless.ssid).log(_model.config.wireless.pass).log(WiFi.getMode()).logln(WiFi.status());
}
if(!(WiFi.getMode() & WIFI_AP))
{
startAp();
}
_server.begin(_model.config.wireless.port);
_server.setNoDelay(true);
_model.state.serial[SERIAL_SOFT_0].stream = &_adapter;
_model.logger.info().log(F("WIFI SERVER PORT")).log(WiFi.status()).logln(_model.config.wireless.port);
return 1;
}

void Wireless::wifiEventConnected(const String& ssid, int channel)
{
_model.logger.info().log(F("WIFI STA CONN")).log(ssid).logln(channel);
}

void Wireless::wifiEventApConnected(const uint8_t* mac)
{
char buf[20];
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
_model.logger.info().log(F("WIFI AP CONNECT")).logln(buf);
}

void Wireless::wifiEventGotIp(const IPAddress& ip)
{
_model.logger.info().log(F("WIFI STA IP")).logln(ip.toString());
}

void Wireless::wifiEventDisconnected()
{
_model.logger.info().logln(F("WIFI STA DISCONNECT"));
}

int Wireless::update()
{
Utils::Stats::Measure measure(_model.state.stats, COUNTER_WIFI);

switch(_status)
{
case STOPPED:
if(_model.state.mode.rescueConfigMode == RESCUE_CONFIG_ACTIVE && _model.isFeatureActive(FEATURE_SOFTSERIAL))
{
connect();
_status = STARTED;
return 1;
}
break;
case STARTED:
if(_server.hasClient())
{
_client = _server.accept();
}
break;
}

return 1;
}

}

#endif
109 changes: 10 additions & 99 deletions lib/Espfc/src/Wireless.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#ifndef _ESPFC_WIRELESS_H_
#define _ESPFC_WIRELESS_H_
#pragma once

#include "Model.h"
#include "Device/SerialDeviceAdapter.h"
Expand All @@ -20,103 +19,17 @@ class Wireless
STARTED,
};
public:
Wireless(Model& model): _model(model), _status(STOPPED), _server(1111), _adapter(_client) {}
Wireless(Model& model);

int begin()
{
WiFi.persistent(false);
#ifdef ESPFC_ESPNOW
if(_model.isFeatureActive(FEATURE_RX_SPI))
{
startAp();
}
#endif
return 1;
}

void startAp()
{
bool status = WiFi.softAP("ESP-FC");
_model.logger.info().log(F("WIFI AP START")).logln(status);
}

int connect()
{
#ifdef ESPFC_WIFI_ALT
// https://github.com/esp8266/Arduino/issues/2545#issuecomment-249222211
_events[0] = WiFi.onStationModeConnected([this](const WiFiEventStationModeConnected& ev) { this->wifiEventConnected(ev.ssid, ev.channel); });
_events[1] = WiFi.onStationModeGotIP([this](const WiFiEventStationModeGotIP& ev) { this->wifiEventGotIp(ev.ip); });
_events[2] = WiFi.onStationModeDisconnected([this](const WiFiEventStationModeDisconnected& ev) { this->wifiEventDisconnected(); });
_events[3] = WiFi.onSoftAPModeStationConnected([this](const WiFiEventSoftAPModeStationConnected& ev) { this->wifiEventApConnected(ev.mac); });
#elif defined(ESPFC_WIFI)
WiFi.onEvent([this](WiFiEvent_t ev, WiFiEventInfo_t info) {
this->wifiEventConnected(String(info.wifi_sta_connected.ssid, info.wifi_sta_connected.ssid_len), info.wifi_sta_connected.channel);
}, ARDUINO_EVENT_WIFI_STA_CONNECTED);
WiFi.onEvent([this](WiFiEvent_t ev, WiFiEventInfo_t info) { this->wifiEventGotIp(IPAddress(info.got_ip.ip_info.ip.addr)); }, ARDUINO_EVENT_WIFI_STA_GOT_IP);
WiFi.onEvent([this](WiFiEvent_t ev, WiFiEventInfo_t info) { this->wifiEventDisconnected(); }, ARDUINO_EVENT_WIFI_STA_DISCONNECTED);
#endif
if(_model.config.wireless.ssid[0] != 0)
{
WiFi.begin(_model.config.wireless.ssid, _model.config.wireless.pass);
_model.logger.info().log(F("WIFI STA")).log(_model.config.wireless.ssid).log(_model.config.wireless.pass).log(WiFi.getMode()).logln(WiFi.status());
}
if(!(WiFi.getMode() & WIFI_AP))
{
startAp();
}
_server.begin(_model.config.wireless.port);
_server.setNoDelay(true);
_model.state.serial[SERIAL_SOFT_0].stream = &_adapter;
_model.logger.info().log(F("WIFI SERVER PORT")).log(WiFi.status()).logln(_model.config.wireless.port);
return 1;
}

void wifiEventConnected(const String& ssid, int channel)
{
_model.logger.info().log(F("WIFI STA CONN")).log(ssid).logln(channel);
}
int begin();
int update();

void wifiEventApConnected(const uint8_t* mac)
{
char buf[20];
snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
_model.logger.info().log(F("WIFI AP CONNECT")).logln(buf);
}

void wifiEventGotIp(const IPAddress& ip)
{
_model.logger.info().log(F("WIFI STA IP")).logln(ip.toString());
}

void wifiEventDisconnected()
{
_model.logger.info().logln(F("WIFI STA DISCONNECT"));
}

int update()
{
Utils::Stats::Measure measure(_model.state.stats, COUNTER_WIFI);

switch(_status)
{
case STOPPED:
if(_model.state.mode.rescueConfigMode == RESCUE_CONFIG_ACTIVE && _model.isFeatureActive(FEATURE_SOFTSERIAL))
{
connect();
_status = STARTED;
return 1;
}
break;
case STARTED:
if(_server.hasClient())
{
_client = _server.accept();
}
break;
}

return 1;
}
void startAp();
int connect();
void wifiEventConnected(const String& ssid, int channel);
void wifiEventApConnected(const uint8_t* mac);
void wifiEventGotIp(const IPAddress& ip);
void wifiEventDisconnected();

private:
Model& _model;
Expand All @@ -132,5 +45,3 @@ class Wireless
}

#endif

#endif

0 comments on commit ae4a90a

Please sign in to comment.