Merge upstream/dev (13 commits) [upstream-sync]
pub-release / release (push) Successful in 23m45s

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
tthandClaude Opus 5.5 committed 2026-11-08 00:28:06 +01:00
commit 5f63ba303c
27 files changed
+1099 -90

No files matched your search

+226 -1
View File
@@ -1,6 +1,6 @@
#include "KissModem.h"
#include <CayenneLPP.h>
#include <cstdio> // snprintf: na desce ho tahne Arduino.h, na hostu (env:native_kiss_modem) ne
#include <stdio.h>
KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng,
mesh::Radio& radio, mesh::MainBoard& board, SensorManager& sensors)
@@ -21,8 +21,15 @@ KissModem::KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& r
_setTxPowerCallback = nullptr;
_getCurrentRssiCallback = nullptr;
_getStatsCallback = nullptr;
_setRxBoostedGainCallback = nullptr;
_getRxBoostedGainCallback = nullptr;
_pollRxCallback = nullptr;
_config = {0, 0, 0, 0, 0};
_signal_report_enabled = true;
_agc_reset_interval_sec = KISS_AGC_RESET_DEFAULT_SEC;
_next_agc_reset_ms = 0;
_fem_op_head = 0;
_fem_op_count = 0;
resetOutputQueue();
}
@@ -32,6 +39,9 @@ void KissModem::begin() {
_rx_active = false;
_has_pending_tx = false;
_tx_state = TX_IDLE;
_fem_op_head = 0;
_fem_op_count = 0;
_next_agc_reset_ms = millis() + (uint32_t)_agc_reset_interval_sec * 1000;
resetOutputQueue();
}
@@ -241,10 +251,24 @@ void KissModem::loop() {
}
processTx();
processFemOps();
maybeResetAgc();
tryFlushFrames();
queuePendingBusyError();
}
void KissModem::maybeResetAgc() {
if (_agc_reset_interval_sec == 0) return;
// same guard as the RX path in main.cpp, so receive is restarted right after the reset
if (_tx_state != TX_IDLE || isHostOutputBackedUp()) return;
uint32_t now = millis();
if ((int32_t)(now - _next_agc_reset_ms) >= 0) {
_radio.resetAGC();
_next_agc_reset_ms = now + (uint32_t)_agc_reset_interval_sec * 1000;
}
}
void KissModem::processFrame() {
if (_rx_len < 1) return;
@@ -382,6 +406,27 @@ void KissModem::handleHardwareCommand(uint8_t sub_cmd, const uint8_t* data, uint
case HW_CMD_GET_SIGNAL_REPORT:
handleGetSignalReport();
break;
case HW_CMD_GET_CAPABILITIES:
handleGetCapabilities();
break;
case HW_CMD_SET_AGC_RESET_INTERVAL:
handleSetAgcResetInterval(data, len);
break;
case HW_CMD_GET_AGC_RESET_INTERVAL:
handleGetAgcResetInterval();
break;
case HW_CMD_SET_FEM_STATE:
handleSetFemState(data, len);
break;
case HW_CMD_GET_FEM_STATE:
handleGetFemState();
break;
case HW_CMD_SET_RX_BOOSTED_GAIN:
handleSetRxBoostedGain(data, len);
break;
case HW_CMD_GET_RX_BOOSTED_GAIN:
handleGetRxBoostedGain();
break;
default:
writeHardwareError(HW_ERR_UNKNOWN_CMD);
break;
@@ -751,3 +796,183 @@ void KissModem::handleGetSignalReport() {
uint8_t val = _signal_report_enabled ? 0x01 : 0x00;
writeHardwareFrame(HW_RESP(HW_CMD_GET_SIGNAL_REPORT), &val, 1);
}
void KissModem::handleGetCapabilities() {
uint32_t caps = HW_CAP_AGC_RESET;
uint8_t fem_caps, fem_values;
readFemState(&fem_caps, &fem_values);
if (fem_caps & HW_FEM_RX_GAIN) caps |= HW_CAP_FEM_RX_GAIN;
if (fem_caps & HW_FEM_TX_GAIN) caps |= HW_CAP_FEM_TX_GAIN;
if (_setRxBoostedGainCallback && _getRxBoostedGainCallback) caps |= HW_CAP_RX_BOOSTED_GAIN;
uint8_t buf[4] = { (uint8_t)caps, (uint8_t)(caps >> 8), (uint8_t)(caps >> 16), (uint8_t)(caps >> 24) };
writeHardwareFrame(HW_RESP(HW_CMD_GET_CAPABILITIES), buf, 4);
}
void KissModem::handleSetAgcResetInterval(const uint8_t* data, uint16_t len) {
if (len < 2) {
writeHardwareError(HW_ERR_INVALID_LENGTH);
return;
}
uint16_t secs = (uint16_t)(data[0] | (data[1] << 8));
if (secs > KISS_AGC_RESET_MAX_SEC) {
writeHardwareError(HW_ERR_INVALID_PARAM);
return;
}
_agc_reset_interval_sec = secs - (secs % KISS_AGC_RESET_STEP_SEC);
_next_agc_reset_ms = millis() + (uint32_t)_agc_reset_interval_sec * 1000;
handleGetAgcResetInterval();
}
void KissModem::handleGetAgcResetInterval() {
uint8_t buf[2] = { (uint8_t)_agc_reset_interval_sec, (uint8_t)(_agc_reset_interval_sec >> 8) };
writeHardwareFrame(HW_RESP(HW_CMD_GET_AGC_RESET_INTERVAL), buf, 2);
}
// FEM gain is reached through the board's own "radio.fem.*" CLI commands, the same ones users run.
static const char* femGainName(uint8_t bit) {
return bit == HW_FEM_RX_GAIN ? "rxgain" : "txgain";
}
// Anything but an exact "> on" / "> off" (not handled, "Error: ...", unexpected text) means unsupported.
bool KissModem::queryFemGain(uint8_t bit, bool* enabled) {
char cmd[32];
char reply[KISS_BOARD_REPLY_SIZE] = { 0 };
snprintf(cmd, sizeof(cmd), "get radio.fem.%s", femGainName(bit));
if (!_board.handleCommand(cmd, 0, reply)) return false;
if (strcmp(reply, "> on") == 0) {
*enabled = true;
} else if (strcmp(reply, "> off") == 0) {
*enabled = false;
} else {
return false;
}
return true;
}
// Success is judged by reading the state back, not by the wording of the set reply.
bool KissModem::setFemGain(uint8_t bit, bool enable) {
char cmd[32];
char reply[KISS_BOARD_REPLY_SIZE] = { 0 };
snprintf(cmd, sizeof(cmd), "set radio.fem.%s %s", femGainName(bit), enable ? "on" : "off");
_board.handleCommand(cmd, 0, reply);
bool now;
return queryFemGain(bit, &now) && now == enable;
}
void KissModem::readFemState(uint8_t* caps, uint8_t* values) {
*caps = 0;
*values = 0;
const uint8_t bits[] = { HW_FEM_RX_GAIN, HW_FEM_TX_GAIN };
for (uint8_t bit : bits) {
bool enabled;
if (queryFemGain(bit, &enabled)) {
*caps |= bit;
if (enabled) *values |= bit;
}
}
}
bool KissModem::queueFemReply() {
uint8_t buf[2];
readFemState(&buf[0], &buf[1]);
return queueHardwareFrame(HW_RESP(HW_CMD_GET_FEM_STATE), buf, 2, false);
}
// The reply always carries the read-back state, so a bit that failed to apply shows its real value.
void KissModem::applyFemState(uint8_t apply_mask, uint8_t value_mask) {
if (apply_mask & HW_FEM_RX_GAIN) {
setFemGain(HW_FEM_RX_GAIN, (value_mask & HW_FEM_RX_GAIN) != 0);
}
if (apply_mask & HW_FEM_TX_GAIN) {
setFemGain(HW_FEM_TX_GAIN, (value_mask & HW_FEM_TX_GAIN) != 0);
}
}
void KissModem::enqueueFemOp(FemOpKind kind, uint8_t apply_mask, uint8_t value_mask) {
if (_fem_op_count >= KISS_FEM_OP_QUEUE_DEPTH) {
writeHardwareError(HW_ERR_TX_BUSY); // too many FEM requests waiting
return;
}
FemOp& op = _fem_ops[(_fem_op_head + _fem_op_count) % KISS_FEM_OP_QUEUE_DEPTH];
op.kind = kind;
op.apply_mask = apply_mask;
op.value_mask = value_mask;
op.applied = false;
_fem_op_count++;
processFemOps();
}
// Every FEM reply goes out in request order. A Set waits until TxDone is queued, since board
// setters drive FEM pins directly; each reply is retained until there is room to queue it.
void KissModem::processFemOps() {
while (_fem_op_count > 0) {
FemOp& op = _fem_ops[_fem_op_head];
if (op.kind == FEM_OP_ERROR) {
if (!queueHardwareFrame(HW_RESP_ERROR, &op.value_mask, 1, false)) return;
} else {
if (op.kind == FEM_OP_SET && !op.applied) {
if (_tx_state == TX_SENDING || _tx_state == TX_DONE_PENDING) return;
uint8_t caps, values;
readFemState(&caps, &values);
if (op.apply_mask & ~caps) {
// all-or-nothing: any bit the board cannot control rejects the whole request
op.kind = FEM_OP_ERROR;
op.value_mask = HW_ERR_UNSUPPORTED;
continue;
}
applyFemState(op.apply_mask, op.value_mask);
op.applied = true;
}
if (!queueFemReply()) return;
}
_fem_op_head = (uint8_t)((_fem_op_head + 1) % KISS_FEM_OP_QUEUE_DEPTH);
_fem_op_count--;
}
}
void KissModem::handleSetFemState(const uint8_t* data, uint16_t len) {
if (len < 2) {
enqueueFemOp(FEM_OP_ERROR, 0, HW_ERR_INVALID_LENGTH);
return;
}
enqueueFemOp(FEM_OP_SET, data[0], data[1]);
}
void KissModem::handleGetFemState() {
enqueueFemOp(FEM_OP_GET, 0, 0);
}
void KissModem::handleSetRxBoostedGain(const uint8_t* data, uint16_t len) {
if (len < 1) {
writeHardwareError(HW_ERR_INVALID_LENGTH);
return;
}
if (!_setRxBoostedGainCallback || !_getRxBoostedGainCallback) {
writeHardwareError(HW_ERR_UNSUPPORTED);
return;
}
// some drivers (LR2021) drop to standby to apply the gain, which would abort a TX and
// discard a completed RX packet; so never during TX, and only after draining RX
if (_tx_state == TX_SENDING || isHostOutputBackedUp()) {
writeHardwareError(HW_ERR_TX_BUSY);
return;
}
if (_pollRxCallback) _pollRxCallback();
tryFlushFrames();
if (_tx_frame_count >= KISS_TX_FRAME_QUEUE_DEPTH) {
writeHardwareError(HW_ERR_TX_BUSY); // no room left for the reply; change nothing
return;
}
// always reply with the state read back, so a failed or partial write is never misreported
_setRxBoostedGainCallback(data[0] != 0x00);
handleGetRxBoostedGain();
}
void KissModem::handleGetRxBoostedGain() {
if (!_getRxBoostedGainCallback) {
writeHardwareError(HW_ERR_UNSUPPORTED);
return;
}
uint8_t val = _getRxBoostedGainCallback() ? 0x01 : 0x00;
writeHardwareFrame(HW_RESP(HW_CMD_GET_RX_BOOSTED_GAIN), &val, 1);
}
+75 -1
View File
@@ -62,6 +62,13 @@
#define HW_CMD_REBOOT 0x18
#define HW_CMD_SET_SIGNAL_REPORT 0x19
#define HW_CMD_GET_SIGNAL_REPORT 0x1A
#define HW_CMD_GET_CAPABILITIES 0x1B
#define HW_CMD_SET_AGC_RESET_INTERVAL 0x1C
#define HW_CMD_GET_AGC_RESET_INTERVAL 0x1D
#define HW_CMD_SET_FEM_STATE 0x1E
#define HW_CMD_GET_FEM_STATE 0x1F
#define HW_CMD_SET_RX_BOOSTED_GAIN 0x20
#define HW_CMD_GET_RX_BOOSTED_GAIN 0x21
/* Response code = command code | 0x80. Generic / unsolicited use 0xF0+. */
#define HW_RESP(cmd) ((cmd) | 0x80)
@@ -81,13 +88,37 @@
#define HW_ERR_UNKNOWN_CMD 0x05
#define HW_ERR_ENCRYPT_FAILED 0x06
#define HW_ERR_TX_BUSY 0x07
#define HW_ERR_UNSUPPORTED 0x08
#define KISS_FIRMWARE_VERSION 1
/* GetCapabilities feature bits */
#define HW_CAP_AGC_RESET (1UL << 0)
#define HW_CAP_FEM_RX_GAIN (1UL << 1)
#define HW_CAP_FEM_TX_GAIN (1UL << 2)
#define HW_CAP_RX_BOOSTED_GAIN (1UL << 3)
/* SetFemState / GetFemState mask bits */
#define HW_FEM_RX_GAIN (1 << 0)
#define HW_FEM_TX_GAIN (1 << 1)
/* Matches the CLI reply buffer boards write into from handleCommand() */
#define KISS_BOARD_REPLY_SIZE 160
/* FEM requests are answered strictly in arrival order; this many may wait (e.g. during a TX) */
#define KISS_FEM_OP_QUEUE_DEPTH 4
#define KISS_AGC_RESET_DEFAULT_SEC 32 // a multiple of the 4 s step, so hosts can set it back
#define KISS_AGC_RESET_MAX_SEC 1020
#define KISS_AGC_RESET_STEP_SEC 4
#define KISS_FIRMWARE_VERSION 2
typedef void (*SetRadioCallback)(float freq, float bw, uint8_t sf, uint8_t cr);
typedef void (*SetTxPowerCallback)(uint8_t power);
typedef float (*GetCurrentRssiCallback)();
typedef void (*GetStatsCallback)(uint32_t* rx, uint32_t* tx, uint32_t* errors);
typedef bool (*SetRxBoostedGainCallback)(bool enable);
typedef bool (*GetRxBoostedGainCallback)();
typedef void (*PollRxCallback)();
struct RadioConfig {
uint32_t freq_hz;
@@ -97,6 +128,19 @@ struct RadioConfig {
uint8_t tx_power;
};
enum FemOpKind : uint8_t {
FEM_OP_SET,
FEM_OP_GET,
FEM_OP_ERROR
};
struct FemOp {
FemOpKind kind;
uint8_t apply_mask; // FEM_OP_SET
uint8_t value_mask; // FEM_OP_SET; error code for FEM_OP_ERROR
bool applied; // FEM_OP_SET: hardware changed, reply still to be queued
};
enum TxState {
TX_IDLE,
TX_WAIT_CLEAR,
@@ -136,9 +180,17 @@ class KissModem {
SetTxPowerCallback _setTxPowerCallback;
GetCurrentRssiCallback _getCurrentRssiCallback;
GetStatsCallback _getStatsCallback;
SetRxBoostedGainCallback _setRxBoostedGainCallback;
GetRxBoostedGainCallback _getRxBoostedGainCallback;
PollRxCallback _pollRxCallback;
RadioConfig _config;
bool _signal_report_enabled;
uint16_t _agc_reset_interval_sec;
uint32_t _next_agc_reset_ms;
FemOp _fem_ops[KISS_FEM_OP_QUEUE_DEPTH];
uint8_t _fem_op_head;
uint8_t _fem_op_count;
uint8_t _tx_frame_buf[KISS_TX_FRAME_QUEUE_DEPTH][KISS_MAX_ENCODED_FRAME_SIZE];
uint16_t _tx_frame_len[KISS_TX_FRAME_QUEUE_DEPTH];
uint16_t _tx_frame_written[KISS_TX_FRAME_QUEUE_DEPTH];
@@ -165,6 +217,14 @@ class KissModem {
void processFrame();
void handleHardwareCommand(uint8_t sub_cmd, const uint8_t* data, uint16_t len);
void processTx();
void maybeResetAgc();
bool queryFemGain(uint8_t bit, bool* enabled);
bool setFemGain(uint8_t bit, bool enable);
void readFemState(uint8_t* caps, uint8_t* values);
bool queueFemReply();
void applyFemState(uint8_t apply_mask, uint8_t value_mask);
void enqueueFemOp(FemOpKind kind, uint8_t apply_mask, uint8_t value_mask);
void processFemOps();
void handleGetIdentity();
void handleGetRandom(const uint8_t* data, uint16_t len);
@@ -192,6 +252,13 @@ class KissModem {
void handleGetDeviceName();
void handleSetSignalReport(const uint8_t* data, uint16_t len);
void handleGetSignalReport();
void handleGetCapabilities();
void handleSetAgcResetInterval(const uint8_t* data, uint16_t len);
void handleGetAgcResetInterval();
void handleSetFemState(const uint8_t* data, uint16_t len);
void handleGetFemState();
void handleSetRxBoostedGain(const uint8_t* data, uint16_t len);
void handleGetRxBoostedGain();
public:
KissModem(Stream& serial, mesh::LocalIdentity& identity, mesh::RNG& rng,
@@ -204,6 +271,13 @@ public:
void setTxPowerCallback(SetTxPowerCallback cb) { _setTxPowerCallback = cb; }
void setGetCurrentRssiCallback(GetCurrentRssiCallback cb) { _getCurrentRssiCallback = cb; }
void setGetStatsCallback(GetStatsCallback cb) { _getStatsCallback = cb; }
// Only register these when the radio supports boosted RX gain; they define the capability.
// Delivers any completed RX packet to the host; called before settings that restart the receiver.
void setPollRxCallback(PollRxCallback cb) { _pollRxCallback = cb; }
void setRxBoostedGainCallbacks(SetRxBoostedGainCallback set_cb, GetRxBoostedGainCallback get_cb) {
_setRxBoostedGainCallback = set_cb;
_getRxBoostedGainCallback = get_cb;
}
void onPacketReceived(int8_t snr, int8_t rssi, const uint8_t* packet, uint16_t len);
bool isTxBusy() const { return _tx_state != TX_IDLE; }
+23 -16
View File
@@ -19,7 +19,6 @@
#endif
#define NOISE_FLOOR_CALIB_INTERVAL_MS 2000
#define AGC_RESET_INTERVAL_MS 30000
#define USB_TX_TIMEOUT_MS 50
#define USB_TX_BUFFER_SIZE 1024
@@ -27,7 +26,6 @@ StdRNG rng;
mesh::LocalIdentity identity;
KissModem* modem;
static uint32_t next_noise_floor_calib_ms = 0;
static uint32_t next_agc_reset_ms = 0;
void halt() {
while (1) ;
@@ -75,6 +73,24 @@ void onGetStats(uint32_t* rx, uint32_t* tx, uint32_t* errors) {
*errors = radio_driver.getPacketsRecvErrors();
}
void pollRadioRx() {
uint8_t rx_buf[256];
int rx_len = radio_driver.recvRaw(rx_buf, sizeof(rx_buf));
if (rx_len > 0) {
int8_t snr = (int8_t)(radio_driver.getLastSNR() * 4);
int8_t rssi = (int8_t)radio_driver.getLastRSSI();
modem->onPacketReceived(snr, rssi, rx_buf, rx_len);
}
}
bool onSetRxBoostedGain(bool enable) {
return radio_driver.setRxBoostedGainMode(enable);
}
bool onGetRxBoostedGain() {
return radio_driver.getRxBoostedGainMode();
}
void setup() {
board.begin();
@@ -127,6 +143,10 @@ void setup() {
modem->setTxPowerCallback(onSetTxPower);
modem->setGetCurrentRssiCallback(onGetCurrentRssi);
modem->setGetStatsCallback(onGetStats);
modem->setPollRxCallback(pollRadioRx);
if (radio_driver.supportsRxBoostedGain()) {
modem->setRxBoostedGainCallbacks(onSetRxBoostedGain, onGetRxBoostedGain);
}
modem->begin();
board.onBootComplete();
@@ -136,20 +156,7 @@ void loop() {
modem->loop();
if (!modem->isActuallyTransmitting() && !modem->isHostOutputBackedUp()) {
if (!modem->isTxBusy()) {
if ((uint32_t)(millis() - next_agc_reset_ms) >= AGC_RESET_INTERVAL_MS) {
radio_driver.resetAGC();
next_agc_reset_ms = millis();
}
}
uint8_t rx_buf[256];
int rx_len = radio_driver.recvRaw(rx_buf, sizeof(rx_buf));
if (rx_len > 0) {
int8_t snr = (int8_t)(radio_driver.getLastSNR() * 4);
int8_t rssi = (int8_t)radio_driver.getLastRSSI();
modem->onPacketReceived(snr, rssi, rx_buf, rx_len);
}
pollRadioRx();
}
board.loop();