mirror of
https://github.com/meshcore-dev/MeshCore.git
synced 2026-03-29 15:10:10 +00:00
Refactor KissModem to integrate radio and sensor management directly, removing callback dependencies.
This commit is contained in:
@@ -1,7 +1,9 @@
|
||||
#include "KissModem.h"
|
||||
#include <CayenneLPP.h>
|
||||
|
||||
KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng)
|
||||
: _serial(serial), _identity(identity), _rng(rng) {
|
||||
KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng,
|
||||
mesh::Radio& radio, mesh::MainBoard& board, SensorManager& sensors)
|
||||
: _serial(serial), _identity(identity), _rng(rng), _radio(radio), _board(board), _sensors(sensors) {
|
||||
_rx_len = 0;
|
||||
_rx_escaped = false;
|
||||
_rx_active = false;
|
||||
@@ -11,12 +13,7 @@ KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& r
|
||||
_setTxPowerCallback = nullptr;
|
||||
_setSyncWordCallback = nullptr;
|
||||
_getCurrentRssiCallback = nullptr;
|
||||
_isChannelBusyCallback = nullptr;
|
||||
_getAirtimeCallback = nullptr;
|
||||
_getNoiseFloorCallback = nullptr;
|
||||
_getStatsCallback = nullptr;
|
||||
_getBatteryCallback = nullptr;
|
||||
_getSensorsCallback = nullptr;
|
||||
_config = {0, 0, 0, 0, 0, 0x12};
|
||||
}
|
||||
|
||||
@@ -406,12 +403,7 @@ void KissModem::handleGetCurrentRssi() {
|
||||
}
|
||||
|
||||
void KissModem::handleIsChannelBusy() {
|
||||
if (!_isChannelBusyCallback) {
|
||||
writeErrorFrame(ERR_NO_CALLBACK);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t busy = _isChannelBusyCallback() ? 0x01 : 0x00;
|
||||
uint8_t busy = _radio.isReceiving() ? 0x01 : 0x00;
|
||||
writeFrame(RESP_CHANNEL_BUSY, &busy, 1);
|
||||
}
|
||||
|
||||
@@ -420,23 +412,14 @@ void KissModem::handleGetAirtime(const uint8_t* data, uint16_t len) {
|
||||
writeErrorFrame(ERR_INVALID_LENGTH);
|
||||
return;
|
||||
}
|
||||
if (!_getAirtimeCallback) {
|
||||
writeErrorFrame(ERR_NO_CALLBACK);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t packet_len = data[0];
|
||||
uint32_t airtime = _getAirtimeCallback(packet_len);
|
||||
uint32_t airtime = _radio.getEstAirtimeFor(packet_len);
|
||||
writeFrame(RESP_AIRTIME, (uint8_t*)&airtime, 4);
|
||||
}
|
||||
|
||||
void KissModem::handleGetNoiseFloor() {
|
||||
if (!_getNoiseFloorCallback) {
|
||||
writeErrorFrame(ERR_NO_CALLBACK);
|
||||
return;
|
||||
}
|
||||
|
||||
int16_t noise_floor = _getNoiseFloorCallback();
|
||||
int16_t noise_floor = _radio.getNoiseFloor();
|
||||
writeFrame(RESP_NOISE_FLOOR, (uint8_t*)&noise_floor, 2);
|
||||
}
|
||||
|
||||
@@ -456,12 +439,7 @@ void KissModem::handleGetStats() {
|
||||
}
|
||||
|
||||
void KissModem::handleGetBattery() {
|
||||
if (!_getBatteryCallback) {
|
||||
writeErrorFrame(ERR_NO_CALLBACK);
|
||||
return;
|
||||
}
|
||||
|
||||
uint16_t mv = _getBatteryCallback();
|
||||
uint16_t mv = _board.getBattMilliVolts();
|
||||
writeFrame(RESP_BATTERY, (uint8_t*)&mv, 2);
|
||||
}
|
||||
|
||||
@@ -474,16 +452,11 @@ void KissModem::handleGetSensors(const uint8_t* data, uint16_t len) {
|
||||
writeErrorFrame(ERR_INVALID_LENGTH);
|
||||
return;
|
||||
}
|
||||
if (!_getSensorsCallback) {
|
||||
writeErrorFrame(ERR_NO_CALLBACK);
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t permissions = data[0];
|
||||
uint8_t buf[255];
|
||||
uint8_t result_len = _getSensorsCallback(permissions, buf, 255);
|
||||
if (result_len > 0) {
|
||||
writeFrame(RESP_SENSORS, buf, result_len);
|
||||
CayenneLPP telemetry(255);
|
||||
if (_sensors.querySensors(permissions, telemetry)) {
|
||||
writeFrame(RESP_SENSORS, telemetry.getBuffer(), telemetry.getSize());
|
||||
} else {
|
||||
writeFrame(RESP_SENSORS, nullptr, 0);
|
||||
}
|
||||
|
||||
@@ -3,6 +3,8 @@
|
||||
#include <Arduino.h>
|
||||
#include <Identity.h>
|
||||
#include <Utils.h>
|
||||
#include <Mesh.h>
|
||||
#include <helpers/SensorManager.h>
|
||||
|
||||
#define KISS_FEND 0xC0
|
||||
#define KISS_FESC 0xDB
|
||||
@@ -69,18 +71,13 @@
|
||||
#define ERR_ENCRYPT_FAILED 0x06
|
||||
#define ERR_TX_PENDING 0x07
|
||||
|
||||
#define KISS_FIRMWARE_VERSION 2
|
||||
#define KISS_FIRMWARE_VERSION 1
|
||||
|
||||
typedef void (*SetRadioCallback)(float freq, float bw, uint8_t sf, uint8_t cr);
|
||||
typedef void (*SetTxPowerCallback)(uint8_t power);
|
||||
typedef void (*SetSyncWordCallback)(uint8_t syncWord);
|
||||
typedef float (*GetCurrentRssiCallback)();
|
||||
typedef bool (*IsChannelBusyCallback)();
|
||||
typedef uint32_t (*GetAirtimeCallback)(uint8_t len);
|
||||
typedef int16_t (*GetNoiseFloorCallback)();
|
||||
typedef void (*GetStatsCallback)(uint32_t* rx, uint32_t* tx, uint32_t* errors);
|
||||
typedef uint16_t (*GetBatteryCallback)();
|
||||
typedef uint8_t (*GetSensorsCallback)(uint8_t permissions, uint8_t* buffer, uint8_t max_len);
|
||||
|
||||
struct RadioConfig {
|
||||
uint32_t freq_hz;
|
||||
@@ -95,6 +92,9 @@ class KissModem {
|
||||
Stream& _serial;
|
||||
mesh::LocalIdentity& _identity;
|
||||
mesh::RNG& _rng;
|
||||
mesh::Radio& _radio;
|
||||
mesh::MainBoard& _board;
|
||||
SensorManager& _sensors;
|
||||
|
||||
uint8_t _rx_buf[KISS_MAX_FRAME_SIZE];
|
||||
uint16_t _rx_len;
|
||||
@@ -109,12 +109,7 @@ class KissModem {
|
||||
SetTxPowerCallback _setTxPowerCallback;
|
||||
SetSyncWordCallback _setSyncWordCallback;
|
||||
GetCurrentRssiCallback _getCurrentRssiCallback;
|
||||
IsChannelBusyCallback _isChannelBusyCallback;
|
||||
GetAirtimeCallback _getAirtimeCallback;
|
||||
GetNoiseFloorCallback _getNoiseFloorCallback;
|
||||
GetStatsCallback _getStatsCallback;
|
||||
GetBatteryCallback _getBatteryCallback;
|
||||
GetSensorsCallback _getSensorsCallback;
|
||||
|
||||
RadioConfig _config;
|
||||
|
||||
@@ -148,7 +143,8 @@ class KissModem {
|
||||
void handleGetSensors(const uint8_t* data, uint16_t len);
|
||||
|
||||
public:
|
||||
KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng);
|
||||
KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng,
|
||||
mesh::Radio& radio, mesh::MainBoard& board, SensorManager& sensors);
|
||||
|
||||
void begin();
|
||||
void loop();
|
||||
@@ -157,12 +153,7 @@ public:
|
||||
void setTxPowerCallback(SetTxPowerCallback cb) { _setTxPowerCallback = cb; }
|
||||
void setSyncWordCallback(SetSyncWordCallback cb) { _setSyncWordCallback = cb; }
|
||||
void setGetCurrentRssiCallback(GetCurrentRssiCallback cb) { _getCurrentRssiCallback = cb; }
|
||||
void setIsChannelBusyCallback(IsChannelBusyCallback cb) { _isChannelBusyCallback = cb; }
|
||||
void setGetAirtimeCallback(GetAirtimeCallback cb) { _getAirtimeCallback = cb; }
|
||||
void setGetNoiseFloorCallback(GetNoiseFloorCallback cb) { _getNoiseFloorCallback = cb; }
|
||||
void setGetStatsCallback(GetStatsCallback cb) { _getStatsCallback = cb; }
|
||||
void setGetBatteryCallback(GetBatteryCallback cb) { _getBatteryCallback = cb; }
|
||||
void setGetSensorsCallback(GetSensorsCallback cb) { _getSensorsCallback = cb; }
|
||||
|
||||
bool getPacketToSend(uint8_t* packet, uint16_t* len);
|
||||
void onPacketReceived(int8_t snr, int8_t rssi, const uint8_t* packet, uint16_t len);
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
#include <target.h>
|
||||
#include <helpers/ArduinoHelpers.h>
|
||||
#include <helpers/IdentityStore.h>
|
||||
#include <CayenneLPP.h>
|
||||
#include "KissModem.h"
|
||||
|
||||
#if defined(NRF52_PLATFORM)
|
||||
@@ -61,38 +60,12 @@ float onGetCurrentRssi() {
|
||||
return radio_driver.getCurrentRSSI();
|
||||
}
|
||||
|
||||
bool onIsChannelBusy() {
|
||||
return radio_driver.isReceiving();
|
||||
}
|
||||
|
||||
uint32_t onGetAirtime(uint8_t len) {
|
||||
return radio_driver.getEstAirtimeFor(len);
|
||||
}
|
||||
|
||||
int16_t onGetNoiseFloor() {
|
||||
return radio_driver.getNoiseFloor();
|
||||
}
|
||||
|
||||
void onGetStats(uint32_t* rx, uint32_t* tx, uint32_t* errors) {
|
||||
*rx = radio_driver.getPacketsRecv();
|
||||
*tx = radio_driver.getPacketsSent();
|
||||
*errors = radio_driver.getPacketsRecvErrors();
|
||||
}
|
||||
|
||||
uint16_t onGetBattery() {
|
||||
return board.getBattMilliVolts();
|
||||
}
|
||||
|
||||
uint8_t onGetSensors(uint8_t permissions, uint8_t* buffer, uint8_t max_len) {
|
||||
CayenneLPP telemetry(max_len);
|
||||
if (sensors.querySensors(permissions, telemetry)) {
|
||||
uint8_t len = telemetry.getSize();
|
||||
memcpy(buffer, telemetry.getBuffer(), len);
|
||||
return len;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void setup() {
|
||||
board.begin();
|
||||
|
||||
@@ -112,17 +85,12 @@ void setup() {
|
||||
|
||||
sensors.begin();
|
||||
|
||||
modem = new KissModem(Serial, identity, rng);
|
||||
modem = new KissModem(Serial, identity, rng, radio_driver, board, sensors);
|
||||
modem->setRadioCallback(onSetRadio);
|
||||
modem->setTxPowerCallback(onSetTxPower);
|
||||
modem->setSyncWordCallback(onSetSyncWord);
|
||||
modem->setGetCurrentRssiCallback(onGetCurrentRssi);
|
||||
modem->setIsChannelBusyCallback(onIsChannelBusy);
|
||||
modem->setGetAirtimeCallback(onGetAirtime);
|
||||
modem->setGetNoiseFloorCallback(onGetNoiseFloor);
|
||||
modem->setGetStatsCallback(onGetStats);
|
||||
modem->setGetBatteryCallback(onGetBattery);
|
||||
modem->setGetSensorsCallback(onGetSensors);
|
||||
modem->begin();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user