Skip to content

Commit 240b5ea

Browse files
Refactor KissModem to integrate radio and sensor management directly, removing callback dependencies.
1 parent 1bcb52b commit 240b5ea

3 files changed

Lines changed: 20 additions & 88 deletions

File tree

examples/kiss_modem/KissModem.cpp

Lines changed: 11 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
#include "KissModem.h"
2+
#include <CayenneLPP.h>
23

3-
KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng)
4-
: _serial(serial), _identity(identity), _rng(rng) {
4+
KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng,
5+
mesh::Radio& radio, mesh::MainBoard& board, SensorManager& sensors)
6+
: _serial(serial), _identity(identity), _rng(rng), _radio(radio), _board(board), _sensors(sensors) {
57
_rx_len = 0;
68
_rx_escaped = false;
79
_rx_active = false;
@@ -11,12 +13,7 @@ KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& r
1113
_setTxPowerCallback = nullptr;
1214
_setSyncWordCallback = nullptr;
1315
_getCurrentRssiCallback = nullptr;
14-
_isChannelBusyCallback = nullptr;
15-
_getAirtimeCallback = nullptr;
16-
_getNoiseFloorCallback = nullptr;
1716
_getStatsCallback = nullptr;
18-
_getBatteryCallback = nullptr;
19-
_getSensorsCallback = nullptr;
2017
_config = {0, 0, 0, 0, 0, 0x12};
2118
}
2219

@@ -406,12 +403,7 @@ void KissModem::handleGetCurrentRssi() {
406403
}
407404

408405
void KissModem::handleIsChannelBusy() {
409-
if (!_isChannelBusyCallback) {
410-
writeErrorFrame(ERR_NO_CALLBACK);
411-
return;
412-
}
413-
414-
uint8_t busy = _isChannelBusyCallback() ? 0x01 : 0x00;
406+
uint8_t busy = _radio.isReceiving() ? 0x01 : 0x00;
415407
writeFrame(RESP_CHANNEL_BUSY, &busy, 1);
416408
}
417409

@@ -420,23 +412,14 @@ void KissModem::handleGetAirtime(const uint8_t* data, uint16_t len) {
420412
writeErrorFrame(ERR_INVALID_LENGTH);
421413
return;
422414
}
423-
if (!_getAirtimeCallback) {
424-
writeErrorFrame(ERR_NO_CALLBACK);
425-
return;
426-
}
427415

428416
uint8_t packet_len = data[0];
429-
uint32_t airtime = _getAirtimeCallback(packet_len);
417+
uint32_t airtime = _radio.getEstAirtimeFor(packet_len);
430418
writeFrame(RESP_AIRTIME, (uint8_t*)&airtime, 4);
431419
}
432420

433421
void KissModem::handleGetNoiseFloor() {
434-
if (!_getNoiseFloorCallback) {
435-
writeErrorFrame(ERR_NO_CALLBACK);
436-
return;
437-
}
438-
439-
int16_t noise_floor = _getNoiseFloorCallback();
422+
int16_t noise_floor = _radio.getNoiseFloor();
440423
writeFrame(RESP_NOISE_FLOOR, (uint8_t*)&noise_floor, 2);
441424
}
442425

@@ -456,12 +439,7 @@ void KissModem::handleGetStats() {
456439
}
457440

458441
void KissModem::handleGetBattery() {
459-
if (!_getBatteryCallback) {
460-
writeErrorFrame(ERR_NO_CALLBACK);
461-
return;
462-
}
463-
464-
uint16_t mv = _getBatteryCallback();
442+
uint16_t mv = _board.getBattMilliVolts();
465443
writeFrame(RESP_BATTERY, (uint8_t*)&mv, 2);
466444
}
467445

@@ -474,16 +452,11 @@ void KissModem::handleGetSensors(const uint8_t* data, uint16_t len) {
474452
writeErrorFrame(ERR_INVALID_LENGTH);
475453
return;
476454
}
477-
if (!_getSensorsCallback) {
478-
writeErrorFrame(ERR_NO_CALLBACK);
479-
return;
480-
}
481455

482456
uint8_t permissions = data[0];
483-
uint8_t buf[255];
484-
uint8_t result_len = _getSensorsCallback(permissions, buf, 255);
485-
if (result_len > 0) {
486-
writeFrame(RESP_SENSORS, buf, result_len);
457+
CayenneLPP telemetry(255);
458+
if (_sensors.querySensors(permissions, telemetry)) {
459+
writeFrame(RESP_SENSORS, telemetry.getBuffer(), telemetry.getSize());
487460
} else {
488461
writeFrame(RESP_SENSORS, nullptr, 0);
489462
}

examples/kiss_modem/KissModem.h

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
#include <Arduino.h>
44
#include <Identity.h>
55
#include <Utils.h>
6+
#include <Mesh.h>
7+
#include <helpers/SensorManager.h>
68

79
#define KISS_FEND 0xC0
810
#define KISS_FESC 0xDB
@@ -69,18 +71,13 @@
6971
#define ERR_ENCRYPT_FAILED 0x06
7072
#define ERR_TX_PENDING 0x07
7173

72-
#define KISS_FIRMWARE_VERSION 2
74+
#define KISS_FIRMWARE_VERSION 1
7375

7476
typedef void (*SetRadioCallback)(float freq, float bw, uint8_t sf, uint8_t cr);
7577
typedef void (*SetTxPowerCallback)(uint8_t power);
7678
typedef void (*SetSyncWordCallback)(uint8_t syncWord);
7779
typedef float (*GetCurrentRssiCallback)();
78-
typedef bool (*IsChannelBusyCallback)();
79-
typedef uint32_t (*GetAirtimeCallback)(uint8_t len);
80-
typedef int16_t (*GetNoiseFloorCallback)();
8180
typedef void (*GetStatsCallback)(uint32_t* rx, uint32_t* tx, uint32_t* errors);
82-
typedef uint16_t (*GetBatteryCallback)();
83-
typedef uint8_t (*GetSensorsCallback)(uint8_t permissions, uint8_t* buffer, uint8_t max_len);
8481

8582
struct RadioConfig {
8683
uint32_t freq_hz;
@@ -95,6 +92,9 @@ class KissModem {
9592
Stream& _serial;
9693
mesh::LocalIdentity& _identity;
9794
mesh::RNG& _rng;
95+
mesh::Radio& _radio;
96+
mesh::MainBoard& _board;
97+
SensorManager& _sensors;
9898

9999
uint8_t _rx_buf[KISS_MAX_FRAME_SIZE];
100100
uint16_t _rx_len;
@@ -109,12 +109,7 @@ class KissModem {
109109
SetTxPowerCallback _setTxPowerCallback;
110110
SetSyncWordCallback _setSyncWordCallback;
111111
GetCurrentRssiCallback _getCurrentRssiCallback;
112-
IsChannelBusyCallback _isChannelBusyCallback;
113-
GetAirtimeCallback _getAirtimeCallback;
114-
GetNoiseFloorCallback _getNoiseFloorCallback;
115112
GetStatsCallback _getStatsCallback;
116-
GetBatteryCallback _getBatteryCallback;
117-
GetSensorsCallback _getSensorsCallback;
118113

119114
RadioConfig _config;
120115

@@ -148,7 +143,8 @@ class KissModem {
148143
void handleGetSensors(const uint8_t* data, uint16_t len);
149144

150145
public:
151-
KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng);
146+
KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng,
147+
mesh::Radio& radio, mesh::MainBoard& board, SensorManager& sensors);
152148

153149
void begin();
154150
void loop();
@@ -157,12 +153,7 @@ class KissModem {
157153
void setTxPowerCallback(SetTxPowerCallback cb) { _setTxPowerCallback = cb; }
158154
void setSyncWordCallback(SetSyncWordCallback cb) { _setSyncWordCallback = cb; }
159155
void setGetCurrentRssiCallback(GetCurrentRssiCallback cb) { _getCurrentRssiCallback = cb; }
160-
void setIsChannelBusyCallback(IsChannelBusyCallback cb) { _isChannelBusyCallback = cb; }
161-
void setGetAirtimeCallback(GetAirtimeCallback cb) { _getAirtimeCallback = cb; }
162-
void setGetNoiseFloorCallback(GetNoiseFloorCallback cb) { _getNoiseFloorCallback = cb; }
163156
void setGetStatsCallback(GetStatsCallback cb) { _getStatsCallback = cb; }
164-
void setGetBatteryCallback(GetBatteryCallback cb) { _getBatteryCallback = cb; }
165-
void setGetSensorsCallback(GetSensorsCallback cb) { _getSensorsCallback = cb; }
166157

167158
bool getPacketToSend(uint8_t* packet, uint16_t* len);
168159
void onPacketReceived(int8_t snr, int8_t rssi, const uint8_t* packet, uint16_t len);

examples/kiss_modem/main.cpp

Lines changed: 1 addition & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
#include <target.h>
33
#include <helpers/ArduinoHelpers.h>
44
#include <helpers/IdentityStore.h>
5-
#include <CayenneLPP.h>
65
#include "KissModem.h"
76

87
#if defined(NRF52_PLATFORM)
@@ -61,38 +60,12 @@ float onGetCurrentRssi() {
6160
return radio_driver.getCurrentRSSI();
6261
}
6362

64-
bool onIsChannelBusy() {
65-
return radio_driver.isReceiving();
66-
}
67-
68-
uint32_t onGetAirtime(uint8_t len) {
69-
return radio_driver.getEstAirtimeFor(len);
70-
}
71-
72-
int16_t onGetNoiseFloor() {
73-
return radio_driver.getNoiseFloor();
74-
}
75-
7663
void onGetStats(uint32_t* rx, uint32_t* tx, uint32_t* errors) {
7764
*rx = radio_driver.getPacketsRecv();
7865
*tx = radio_driver.getPacketsSent();
7966
*errors = radio_driver.getPacketsRecvErrors();
8067
}
8168

82-
uint16_t onGetBattery() {
83-
return board.getBattMilliVolts();
84-
}
85-
86-
uint8_t onGetSensors(uint8_t permissions, uint8_t* buffer, uint8_t max_len) {
87-
CayenneLPP telemetry(max_len);
88-
if (sensors.querySensors(permissions, telemetry)) {
89-
uint8_t len = telemetry.getSize();
90-
memcpy(buffer, telemetry.getBuffer(), len);
91-
return len;
92-
}
93-
return 0;
94-
}
95-
9669
void setup() {
9770
board.begin();
9871

@@ -112,17 +85,12 @@ void setup() {
11285

11386
sensors.begin();
11487

115-
modem = new KissModem(Serial, identity, rng);
88+
modem = new KissModem(Serial, identity, rng, radio_driver, board, sensors);
11689
modem->setRadioCallback(onSetRadio);
11790
modem->setTxPowerCallback(onSetTxPower);
11891
modem->setSyncWordCallback(onSetSyncWord);
11992
modem->setGetCurrentRssiCallback(onGetCurrentRssi);
120-
modem->setIsChannelBusyCallback(onIsChannelBusy);
121-
modem->setGetAirtimeCallback(onGetAirtime);
122-
modem->setGetNoiseFloorCallback(onGetNoiseFloor);
12393
modem->setGetStatsCallback(onGetStats);
124-
modem->setGetBatteryCallback(onGetBattery);
125-
modem->setGetSensorsCallback(onGetSensors);
12694
modem->begin();
12795
}
12896

0 commit comments

Comments
 (0)