Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
5f63ba303c
27 files changed
+1099
-90
No files matched your search
@@ -118,10 +118,17 @@ MeshCore-specific functionality uses the standard KISS SetHardware command. The
|
||||
| Reboot | `0x18` | - |
|
||||
| SetSignalReport | `0x19` | Enable (1): 0x00=disable, nonzero=enable |
|
||||
| GetSignalReport | `0x1A` | - |
|
||||
| GetCapabilities | `0x1B` | - |
|
||||
| SetAgcResetInterval | `0x1C` | Seconds (2): 0=disable, 0-1020 |
|
||||
| GetAgcResetInterval | `0x1D` | - |
|
||||
| SetFemState | `0x1E` | Apply mask (1) + Value mask (1) |
|
||||
| GetFemState | `0x1F` | - |
|
||||
| SetRxBoostedGain | `0x20` | Enable (1): 0x00=disable, nonzero=enable |
|
||||
| GetRxBoostedGain | `0x21` | - |
|
||||
|
||||
### Response Sub-commands (TNC to Host)
|
||||
|
||||
Response codes use the high-bit convention: `response = command | 0x80`. Generic and unsolicited responses use the `0xF0`+ range.
|
||||
Response codes use the high-bit convention: `response = command | 0x80`. Generic and unsolicited responses use the `0xF0`+ range. Set commands that report state (SetSignalReport, SetAgcResetInterval, SetFemState, SetRxBoostedGain) reply with the matching Get response, so `0x9C`, `0x9E` and `0xA0` are never sent.
|
||||
|
||||
| Sub-command | Value | Data |
|
||||
|--------------|--------|-----------------------------------------|
|
||||
@@ -147,6 +154,10 @@ Response codes use the high-bit convention: `response = command | 0x80`. Generic
|
||||
| DeviceName | `0x96` | Name (variable, UTF-8) |
|
||||
| Pong | `0x97` | - |
|
||||
| SignalReport | `0x9A` | Status (1): 0x00=disabled, 0x01=enabled |
|
||||
| Capabilities | `0x9B` | Feature bits (4) |
|
||||
| AgcResetInterval | `0x9D` | Effective seconds (2) |
|
||||
| FemState | `0x9F` | Capability mask (1) + Value mask (1) |
|
||||
| RxBoostedGain | `0xA1` | Status (1): 0x00=disabled, 0x01=enabled |
|
||||
| OK | `0xF0` | - |
|
||||
| Error | `0xF1` | Error code (1) |
|
||||
| TxDone | `0xF8` | Result (1): 0x00=failed, 0x01=success |
|
||||
@@ -163,6 +174,7 @@ Response codes use the high-bit convention: `response = command | 0x80`. Generic
|
||||
| UnknownCmd | `0x05` | Unknown sub-command |
|
||||
| EncryptFailed | `0x06` | Encryption failed |
|
||||
| TxBusy | `0x07` | Radio TX busy, or host output queue full |
|
||||
| Unsupported | `0x08` | Command known, but not supported by this board |
|
||||
|
||||
### Unsolicited Events
|
||||
|
||||
@@ -215,7 +227,75 @@ All values little-endian.
|
||||
|-------------|---------|-----------------------|
|
||||
| Noise floor | 2 bytes | int16_t, dBm (signed) |
|
||||
|
||||
The modem recalibrates the noise floor every 2 seconds with an AGC reset every 30 seconds.
|
||||
The modem recalibrates the noise floor every 2 seconds. AGC resets follow the configured AGC reset interval (default 32 seconds).
|
||||
|
||||
### Capabilities (Capabilities response)
|
||||
|
||||
All values little-endian. Hosts should probe capabilities rather than infer them from the firmware version: boards running the same version expose different hardware controls. Firmware older than version 2 answers GetCapabilities with `UnknownCmd`; treat that as no capabilities.
|
||||
|
||||
| Field | Size | Description |
|
||||
|--------------|---------|--------------------|
|
||||
| Feature bits | 4 bytes | uint32_t, see below |
|
||||
|
||||
| Bit | Value | Description |
|
||||
|-----|--------|-------------------------------------------|
|
||||
| 0 | `0x01` | AGC reset interval control (always set) |
|
||||
| 1 | `0x02` | External FEM RX (LNA) gain control |
|
||||
| 2 | `0x04` | External FEM TX (PA) gain control |
|
||||
| 3 | `0x08` | Radio chip boosted RX gain control |
|
||||
|
||||
Remaining bits are reserved and sent as 0.
|
||||
|
||||
### AGC Reset Interval (SetAgcResetInterval / AgcResetInterval response)
|
||||
|
||||
All values little-endian.
|
||||
|
||||
| Field | Size | Description |
|
||||
|---------|---------|-------------------------------------------|
|
||||
| Seconds | 2 bytes | uint16_t, 0 = disabled, maximum 1020 |
|
||||
|
||||
The modem periodically resets the radio's AGC while idle (no transmission in progress and host output not backed up). The interval has 4-second resolution, matching the MeshCore `agc.reset.interval` setting: requested values are rounded down to a multiple of 4, so 1-3 disable the reset. Values above 1020 return `InvalidParam`. SetAgcResetInterval replies with the AgcResetInterval response carrying the effective value, and restarts the countdown from the time of the request.
|
||||
|
||||
The default is 32 seconds (previously a fixed 30; changed so the default is a settable 4-second multiple). The setting is not persisted; hosts should re-send it after reconnecting or rebooting the modem.
|
||||
|
||||
### FEM State (SetFemState / FemState response)
|
||||
|
||||
Controls the board's external LoRa front-end module gain. This is separate from the radio chip's own RX boosted gain. TX/RX switching of the FEM is handled by the board and is not affected by these settings.
|
||||
|
||||
| Bit | Value | Description |
|
||||
|-----|--------|---------------------------------|
|
||||
| 0 | `0x01` | RX gain (LNA enabled) |
|
||||
| 1 | `0x02` | TX gain (PA high gain enabled) |
|
||||
|
||||
SetFemState data:
|
||||
|
||||
| Field | Size | Description |
|
||||
|------------|--------|------------------------------------------------|
|
||||
| Apply mask | 1 byte | Bits to change; bits not set are left as-is |
|
||||
| Value mask | 1 byte | New value for each bit in the apply mask |
|
||||
|
||||
FemState response data:
|
||||
|
||||
| Field | Size | Description |
|
||||
|-----------------|--------|---------------------------------------------------|
|
||||
| Capability mask | 1 byte | Bits this board can control |
|
||||
| Value mask | 1 byte | Current state; always 0 for uncontrollable bits |
|
||||
|
||||
Example: enable RX gain without changing TX gain: apply `0x01`, value `0x01`.
|
||||
|
||||
If any bit in the apply mask is not in the board's capability mask (including reserved bits), the modem replies `Unsupported` and changes nothing. Otherwise SetFemState replies with the FemState response carrying the state read back from the board; if a bit differs from the request, that change did not take effect. SetFemState and GetFemState requests, including their error replies, are answered strictly in the order received. A SetFemState received while a packet is on air (before its TxDone is queued) is held and applied after TxDone, so its reply follows TxDone, and FEM requests that arrive after it wait behind it. Up to 4 FEM requests can wait; beyond that the modem replies `TxBusy` immediately. Capabilities come from the board's own `get/set radio.fem.rxgain` and `radio.fem.txgain` CLI commands (see [cli_commands.md](./cli_commands.md)): a gain is controllable when the board answers `get` with `> on` or `> off`. Any board that implements those commands is supported without KISS changes, and a board with a fixed, board-managed FEM (for example RAK3401) reports none. Changes made over KISS are not saved to the board's preferences.
|
||||
|
||||
The setting is not persisted; the board's power-on default applies after reboot.
|
||||
|
||||
### RX Boosted Gain (SetRxBoostedGain / RxBoostedGain response)
|
||||
|
||||
Controls the radio chip's own boosted RX gain mode (SX1262, SX1268, LLCC68, LR11x0, LR2021), the equivalent of the MeshCore `radio.rxgain` setting. This is independent of the external FEM RX gain controlled by SetFemState; a board can support either, both or neither.
|
||||
|
||||
| Field | Size | Description |
|
||||
|--------|--------|------------------------------------|
|
||||
| Status | 1 byte | 0x00 = power-saving, 0x01 = boosted |
|
||||
|
||||
SetRxBoostedGain always replies with the RxBoostedGain response carrying the effective state read back from the radio; if it differs from the requested value, the change did not take effect. Some radios must drop to standby to apply it, so the modem replies `TxBusy` and changes nothing while a packet is on air or host output is backed up; retry later. Before applying, any completed received packet is delivered to the host first. A packet still being received at that moment can be lost. Radios whose driver has no boosted RX gain control (for example SX127x and STM32WLx) do not set capability bit 3 and reply `Unsupported` to both commands. The power-on default is the build's `SX126X_RX_BOOSTED_GAIN` setting; the value is not persisted, and periodic AGC resets preserve it.
|
||||
|
||||
### Stats (Stats response)
|
||||
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
@@ -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; }
|
||||
|
||||
@@ -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();
|
||||
|
||||
+2
-1
@@ -138,7 +138,8 @@ lib_deps =
|
||||
extends = arduino_base
|
||||
upload_protocol = picotool
|
||||
board_build.core = earlephilhower
|
||||
platform = https://github.com/maxgerhardt/platform-raspberrypi.git ; framework-arduinopico @ 1.50600.0+sha.6a1d13e9
|
||||
; commit points to framework-arduinopico 5.6.0
|
||||
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#4e22a0dcdd94eead72fdf56ea59a3d7c8aa2f379
|
||||
build_flags = ${arduino_base.build_flags}
|
||||
-D RP2040_PLATFORM
|
||||
|
||||
|
||||
@@ -40,8 +40,9 @@ public:
|
||||
}
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomLLCC68 *)_radio)->spreadingFactor; }
|
||||
|
||||
void doResetAGC() override { sx126xResetAGC((SX126x *)_radio); }
|
||||
void doResetAGC() override { sx126xResetAGC((SX126x *)_radio, getRxBoostedGainMode()); }
|
||||
|
||||
bool supportsRxBoostedGain() const override { return true; }
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
return ((CustomLLCC68 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE;
|
||||
}
|
||||
|
||||
@@ -39,8 +39,9 @@ class CustomLR1110 : public LR1110 {
|
||||
float getFreqMHz() const { return freqMHz; }
|
||||
|
||||
int16_t setRxBoostedGainMode(bool en) {
|
||||
_rx_boosted = en;
|
||||
return LR1110::setRxBoostedGainMode(en);
|
||||
int16_t status = LR1110::setRxBoostedGainMode(en);
|
||||
if (status == RADIOLIB_ERR_NONE) _rx_boosted = en;
|
||||
return status;
|
||||
}
|
||||
|
||||
bool getRxBoostedGainMode() const { return _rx_boosted; }
|
||||
|
||||
@@ -48,6 +48,7 @@ public:
|
||||
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomLR1110 *)_radio)->getSpreadingFactor(); }
|
||||
|
||||
bool supportsRxBoostedGain() const override { return true; }
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
return ((CustomLR1110 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE;
|
||||
}
|
||||
|
||||
@@ -68,8 +68,9 @@ class CustomLR1121 : public LR1121 {
|
||||
float getFreqMHz() const { return freqMHz; }
|
||||
|
||||
int16_t setRxBoostedGainMode(bool en) {
|
||||
_rx_boosted = en;
|
||||
return LR1121::setRxBoostedGainMode(en);
|
||||
int16_t status = LR1121::setRxBoostedGainMode(en);
|
||||
if (status == RADIOLIB_ERR_NONE) _rx_boosted = en;
|
||||
return status;
|
||||
}
|
||||
|
||||
bool getRxBoostedGainMode() const { return _rx_boosted; }
|
||||
|
||||
@@ -49,6 +49,7 @@ public:
|
||||
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomLR1121 *)_radio)->getSpreadingFactor(); }
|
||||
|
||||
bool supportsRxBoostedGain() const override { return true; }
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
return ((CustomLR1121 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE;
|
||||
}
|
||||
|
||||
@@ -8,7 +8,9 @@ class CustomLR2021 : public LR2021 {
|
||||
uint32_t _maxPayloadMillis = 3934;
|
||||
uint32_t _activityAt = 0;
|
||||
bool _headerSeen = false;
|
||||
bool _rx_boosted = false;
|
||||
// last applied gain level per RX path, mirroring RadioLib's gainModeLf/gainModeHf defaults
|
||||
uint8_t _rx_gain_lf = RADIOLIB_LR2021_RX_BOOST_LF;
|
||||
uint8_t _rx_gain_hf = RADIOLIB_LR2021_RX_BOOST_HF;
|
||||
|
||||
public:
|
||||
CustomLR2021(Module *mod) : LR2021(mod) { irqDioNum = LR2021_IRQ_DIO; }
|
||||
@@ -68,7 +70,21 @@ class CustomLR2021 : public LR2021 {
|
||||
|
||||
float getFreqMHz() const { return freqMHz; }
|
||||
|
||||
bool getRxBoostedGainMode() const { return _rx_boosted; }
|
||||
bool isHighFreqPath() const { return freqMHz > RADIOLIB_LR2021_LF_CUTOFF_FREQ; }
|
||||
|
||||
int16_t setRxBoostedGainMode(uint8_t level) {
|
||||
uint8_t& applied = isHighFreqPath() ? _rx_gain_hf : _rx_gain_lf;
|
||||
int16_t status = LR2021::setRxBoostedGainMode(level);
|
||||
if (status == RADIOLIB_ERR_NONE) {
|
||||
applied = level;
|
||||
} else {
|
||||
// RadioLib caches the level before sending it; restore it so the next startReceive keeps the old gain
|
||||
LR2021::setRxBoostedGainMode(applied);
|
||||
}
|
||||
return status;
|
||||
}
|
||||
|
||||
bool getRxBoostedGainMode() const { return (isHighFreqPath() ? _rx_gain_hf : _rx_gain_lf) != 0; }
|
||||
|
||||
int16_t startReceive() override {
|
||||
// include the PREAMBLE_DETECTED irq bit in reported flags
|
||||
|
||||
@@ -106,6 +106,7 @@ public:
|
||||
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomLR2021 *)_radio)->getSpreadingFactor(); }
|
||||
|
||||
bool supportsRxBoostedGain() const override { return true; }
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
((CustomLR2021 *)_radio)->standby(); // LR2021 must be in standby to accept setRxBoostedGainMode
|
||||
int16_t status = ((CustomLR2021 *)_radio)->setRxBoostedGainMode(en ? LR2021_RX_BOOST_LEVEL: 0);
|
||||
|
||||
@@ -54,6 +54,7 @@ public:
|
||||
((CustomSX1262 *)_radio)->sleep(false);
|
||||
}
|
||||
|
||||
bool supportsRxBoostedGain() const override { return true; }
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
return ((CustomSX1262 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE;
|
||||
}
|
||||
|
||||
@@ -43,6 +43,7 @@ public:
|
||||
}
|
||||
uint8_t getSpreadingFactor() const override { return ((CustomSX1268 *)_radio)->spreadingFactor; }
|
||||
|
||||
bool supportsRxBoostedGain() const override { return true; }
|
||||
bool setRxBoostedGainMode(bool en) override {
|
||||
return ((CustomSX1268 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE;
|
||||
}
|
||||
|
||||
@@ -15,7 +15,6 @@ inline void lr11x0ResetAGC(LR11x0* radio, float freqMHz, bool rx_boost_gain) {
|
||||
// Re-calibrate for the actual operating frequency (band=4MHz matches RadioLib default).
|
||||
radio->calibrateImageRejection(freqMHz - 4.0f, freqMHz + 4.0f);
|
||||
|
||||
#ifdef RX_BOOSTED_GAIN
|
||||
// always reapply: the gain may have been changed at runtime even when no build default is set
|
||||
radio->setRxBoostedGainMode(rx_boost_gain);
|
||||
#endif
|
||||
}
|
||||
@@ -96,6 +96,7 @@ public:
|
||||
|
||||
float packetScore(float snr, int packet_len) override { return packetScoreInt(snr, 10, packet_len); } // assume sf=10
|
||||
|
||||
virtual bool supportsRxBoostedGain() const { return false; }
|
||||
virtual bool setRxBoostedGainMode(bool) { return false; }
|
||||
virtual bool getRxBoostedGainMode() const { return false; }
|
||||
|
||||
|
||||
@@ -25,9 +25,8 @@ inline void sx126xResetAGC(SX126x* radio, bool rx_boost_gain) {
|
||||
#ifdef SX126X_DIO2_AS_RF_SWITCH
|
||||
radio->setDio2AsRfSwitch(SX126X_DIO2_AS_RF_SWITCH);
|
||||
#endif
|
||||
#ifdef SX126X_RX_BOOSTED_GAIN
|
||||
// always reapply: the gain may have been changed at runtime even when no build default is set
|
||||
radio->setRxBoostedGainMode(rx_boost_gain);
|
||||
#endif
|
||||
#ifdef SX126X_REGISTER_PATCH
|
||||
uint8_t r_data = 0;
|
||||
radio->readRegister(0x8B5, &r_data, 1);
|
||||
|
||||
@@ -13,6 +13,7 @@ public:
|
||||
virtual bool isSendComplete() { return true; }
|
||||
virtual void onSendFinished() {}
|
||||
virtual int16_t getNoiseFloor() { return -120; }
|
||||
virtual void resetAGC() {}
|
||||
};
|
||||
|
||||
class MainBoard {
|
||||
@@ -22,6 +23,7 @@ public:
|
||||
virtual float getMCUTemperature() { return 25.0f; }
|
||||
virtual const char* getManufacturerName() { return "mock-board"; }
|
||||
virtual void reboot() {}
|
||||
virtual bool handleCommand(const char*, uint32_t, char*) { return false; }
|
||||
};
|
||||
|
||||
}
|
||||
@@ -0,0 +1,546 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <queue>
|
||||
#include <vector>
|
||||
|
||||
#include "KissModem.h"
|
||||
|
||||
namespace {
|
||||
|
||||
class RecordingStream : public Stream {
|
||||
public:
|
||||
void pushRx(const std::vector<uint8_t>& bytes) {
|
||||
for (uint8_t b : bytes) _rx.push(b);
|
||||
}
|
||||
|
||||
int availableForWrite() override { return blocked ? 0 : 4096; }
|
||||
size_t write(const uint8_t* buffer, size_t size) override {
|
||||
if (blocked) return 0;
|
||||
_writes.insert(_writes.end(), buffer, buffer + size);
|
||||
return size;
|
||||
}
|
||||
size_t write(uint8_t b) override { return write(&b, 1); }
|
||||
int available() override { return (int)_rx.size(); }
|
||||
int read() override {
|
||||
if (_rx.empty()) return -1;
|
||||
int b = _rx.front();
|
||||
_rx.pop();
|
||||
return b;
|
||||
}
|
||||
|
||||
// Decoded SetHardware payloads (sub-command + data), consumed on read.
|
||||
std::vector<std::vector<uint8_t>> takeHardwareFrames() {
|
||||
std::vector<std::vector<uint8_t>> frames;
|
||||
std::vector<uint8_t> cur;
|
||||
bool in_frame = false, esc = false;
|
||||
for (uint8_t b : _writes) {
|
||||
if (b == KISS_FEND) {
|
||||
if (in_frame && cur.size() > 1 && cur[0] == KISS_CMD_SETHARDWARE) {
|
||||
frames.emplace_back(cur.begin() + 1, cur.end());
|
||||
}
|
||||
cur.clear();
|
||||
in_frame = true;
|
||||
continue;
|
||||
}
|
||||
if (b == KISS_FESC) { esc = true; continue; }
|
||||
if (esc) { b = (b == KISS_TFEND) ? KISS_FEND : KISS_FESC; esc = false; }
|
||||
cur.push_back(b);
|
||||
}
|
||||
_writes.clear();
|
||||
return frames;
|
||||
}
|
||||
|
||||
bool blocked = false;
|
||||
|
||||
private:
|
||||
std::queue<uint8_t> _rx;
|
||||
std::vector<uint8_t> _writes;
|
||||
};
|
||||
|
||||
class ZeroRNG : public mesh::RNG {
|
||||
public:
|
||||
void random(uint8_t* dest, size_t sz) override { memset(dest, 0, sz); }
|
||||
};
|
||||
|
||||
class AgcRadio : public mesh::Radio {
|
||||
public:
|
||||
bool startSendRaw(const uint8_t*, uint16_t) override { return true; }
|
||||
bool isSendComplete() override { return send_complete; }
|
||||
void resetAGC() override { agc_resets++; }
|
||||
|
||||
bool send_complete = true;
|
||||
int agc_resets = 0;
|
||||
};
|
||||
|
||||
// Implements FEM through the "radio.fem.*" CLI text, worded like the Heltec / Station G3 boards.
|
||||
class FemBoard : public mesh::MainBoard {
|
||||
public:
|
||||
const char* getManufacturerName() override { return "fem-board"; }
|
||||
|
||||
bool handleCommand(const char* command, uint32_t, char* reply) override {
|
||||
commands++;
|
||||
if (handleGain(command, reply, "rxgain", has_lna, lna, lna_sets, lna_sticks)) return true;
|
||||
if (handleGain(command, reply, "txgain", has_pa, pa, pa_sets, pa_sticks)) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool has_lna = false, has_pa = false;
|
||||
bool lna = false, pa = false;
|
||||
int lna_sets = 0, pa_sets = 0;
|
||||
bool lna_sticks = false, pa_sticks = false; // set reports OK but the state does not change
|
||||
bool unhandled = false; // board does not implement radio.fem.* at all
|
||||
const char* odd_get_reply = nullptr; // unexpected get wording
|
||||
int commands = 0;
|
||||
|
||||
private:
|
||||
bool handleGain(const char* command, char* reply, const char* name, bool has, bool& state, int& sets, bool sticks) {
|
||||
if (unhandled) return false;
|
||||
char get_cmd[32], set_cmd[32];
|
||||
snprintf(get_cmd, sizeof(get_cmd), "get radio.fem.%s", name);
|
||||
snprintf(set_cmd, sizeof(set_cmd), "set radio.fem.%s ", name);
|
||||
if (strcmp(command, get_cmd) == 0) {
|
||||
if (!has) strcpy(reply, "Error: unsupported");
|
||||
else if (odd_get_reply) strcpy(reply, odd_get_reply);
|
||||
else sprintf(reply, "> %s", state ? "on" : "off");
|
||||
return true;
|
||||
}
|
||||
if (strncmp(command, set_cmd, strlen(set_cmd)) == 0) {
|
||||
if (!has) {
|
||||
strcpy(reply, "Error: unsupported");
|
||||
} else {
|
||||
sets++;
|
||||
if (!sticks) state = strcmp(command + strlen(set_cmd), "on") == 0;
|
||||
strcpy(reply, "OK - LoRa FEM gain changed");
|
||||
}
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
};
|
||||
|
||||
class NoSensors : public SensorManager {
|
||||
public:
|
||||
bool querySensors(uint8_t, CayenneLPP&) override { return false; }
|
||||
};
|
||||
|
||||
class KissHwControlTest : public ::testing::Test {
|
||||
protected:
|
||||
RecordingStream serial;
|
||||
mesh::LocalIdentity identity;
|
||||
ZeroRNG rng;
|
||||
AgcRadio radio;
|
||||
FemBoard board;
|
||||
NoSensors sensors;
|
||||
KissModem modem;
|
||||
|
||||
KissHwControlTest() : modem(serial, identity, rng, radio, board, sensors) {
|
||||
g_mock_millis = 1000;
|
||||
modem.begin();
|
||||
}
|
||||
|
||||
std::vector<std::vector<uint8_t>> hw(const std::vector<uint8_t>& payload) {
|
||||
std::vector<uint8_t> frame = {KISS_FEND, KISS_CMD_SETHARDWARE};
|
||||
frame.insert(frame.end(), payload.begin(), payload.end());
|
||||
frame.push_back(KISS_FEND);
|
||||
serial.pushRx(frame);
|
||||
modem.loop();
|
||||
return serial.takeHardwareFrames();
|
||||
}
|
||||
|
||||
std::vector<uint8_t> hw1(const std::vector<uint8_t>& payload) {
|
||||
auto frames = hw(payload);
|
||||
EXPECT_EQ(frames.size(), 1U);
|
||||
return frames.empty() ? std::vector<uint8_t>{} : frames[0];
|
||||
}
|
||||
|
||||
void advance(uint32_t ms) {
|
||||
g_mock_millis += ms;
|
||||
modem.loop();
|
||||
}
|
||||
|
||||
void startTxAndHold() {
|
||||
radio.send_complete = false;
|
||||
serial.pushRx({KISS_FEND, KISS_CMD_DATA, 0x42, KISS_FEND});
|
||||
modem.loop();
|
||||
modem.loop();
|
||||
advance((uint32_t)KISS_DEFAULT_TXDELAY * 10);
|
||||
ASSERT_TRUE(modem.isActuallyTransmitting());
|
||||
}
|
||||
};
|
||||
|
||||
bool g_rx_boost = false;
|
||||
bool g_rx_boost_set_ok = true;
|
||||
int g_rx_boost_sets = 0;
|
||||
bool fakeSetRxBoost(bool en) {
|
||||
g_rx_boost_sets++;
|
||||
if (!g_rx_boost_set_ok) return false;
|
||||
g_rx_boost = en;
|
||||
return true;
|
||||
}
|
||||
bool fakeGetRxBoost() { return g_rx_boost; }
|
||||
|
||||
KissModem* g_poll_modem = nullptr;
|
||||
bool g_poll_has_packet = false;
|
||||
int g_polls = 0;
|
||||
void fakePollRx() {
|
||||
g_polls++;
|
||||
if (g_poll_has_packet && g_poll_modem) {
|
||||
static constexpr uint8_t PKT[] = {0x55};
|
||||
g_poll_has_packet = false;
|
||||
g_poll_modem->onPacketReceived(4, -80, PKT, sizeof(PKT));
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<uint8_t> ERR_UNSUPPORTED = {HW_RESP_ERROR, HW_ERR_UNSUPPORTED};
|
||||
|
||||
TEST_F(KissHwControlTest, VersionIsTwo) {
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_VERSION}), (std::vector<uint8_t>{HW_RESP(HW_CMD_GET_VERSION), 2, 0}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, CapabilitiesOnBareBoardAreAgcOnly) {
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_CAPABILITIES}), (std::vector<uint8_t>{0x9B, 0x01, 0x00, 0x00, 0x00}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, CapabilitiesReflectBoardFem) {
|
||||
board.has_lna = true;
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_CAPABILITIES}), (std::vector<uint8_t>{0x9B, 0x03, 0x00, 0x00, 0x00}));
|
||||
board.has_pa = true;
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_CAPABILITIES}), (std::vector<uint8_t>{0x9B, 0x07, 0x00, 0x00, 0x00}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, AgcIntervalDefaultsToThirtyTwoSeconds) {
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_AGC_RESET_INTERVAL}), (std::vector<uint8_t>{0x9D, 32, 0}));
|
||||
// the default must be restorable, i.e. a multiple of the 4 s step
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, 4, 0}), (std::vector<uint8_t>{0x9D, 4, 0}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, KISS_AGC_RESET_DEFAULT_SEC, 0}), (std::vector<uint8_t>{0x9D, KISS_AGC_RESET_DEFAULT_SEC, 0}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, AgcIntervalSetReturnsEffectiveRoundedValue) {
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, 10, 0}), (std::vector<uint8_t>{0x9D, 8, 0}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, 4, 0}), (std::vector<uint8_t>{0x9D, 4, 0}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, 0xFC, 0x03}), (std::vector<uint8_t>{0x9D, 0xFC, 0x03}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, 0, 0}), (std::vector<uint8_t>{0x9D, 0, 0}));
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_AGC_RESET_INTERVAL}), (std::vector<uint8_t>{0x9D, 0, 0}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, AgcIntervalRejectsOutOfRangeAndShortPayload) {
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, 0xFD, 0x03}), (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_INVALID_PARAM}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_AGC_RESET_INTERVAL, 4}), (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_INVALID_LENGTH}));
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_AGC_RESET_INTERVAL}), (std::vector<uint8_t>{0x9D, 32, 0}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, AgcResetsOnDefaultSchedule) {
|
||||
advance(31999);
|
||||
EXPECT_EQ(radio.agc_resets, 0);
|
||||
advance(1);
|
||||
EXPECT_EQ(radio.agc_resets, 1);
|
||||
advance(31999);
|
||||
EXPECT_EQ(radio.agc_resets, 1);
|
||||
advance(1);
|
||||
EXPECT_EQ(radio.agc_resets, 2);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, AgcZeroDisablesResets) {
|
||||
hw({HW_CMD_SET_AGC_RESET_INTERVAL, 0, 0});
|
||||
for (int i = 0; i < 10; i++) advance(60000);
|
||||
EXPECT_EQ(radio.agc_resets, 0);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, AgcIntervalChangeRestartsDeadline) {
|
||||
hw({HW_CMD_SET_AGC_RESET_INTERVAL, 0x2C, 0x01}); // 300 s
|
||||
advance(100000);
|
||||
hw({HW_CMD_SET_AGC_RESET_INTERVAL, 4, 0});
|
||||
EXPECT_EQ(radio.agc_resets, 0);
|
||||
advance(3999);
|
||||
EXPECT_EQ(radio.agc_resets, 0);
|
||||
advance(1);
|
||||
EXPECT_EQ(radio.agc_resets, 1);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, AgcResetWaitsForTxToFinish) {
|
||||
hw({HW_CMD_SET_AGC_RESET_INTERVAL, 4, 0});
|
||||
startTxAndHold();
|
||||
advance(10000);
|
||||
EXPECT_EQ(radio.agc_resets, 0);
|
||||
radio.send_complete = true;
|
||||
modem.loop(); // TX done -> TX_DONE_PENDING
|
||||
modem.loop(); // TxDone flushed -> idle, overdue reset runs
|
||||
EXPECT_EQ(radio.agc_resets, 1);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemUnsupportedOnBareBoard) {
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_FEM_STATE}), (std::vector<uint8_t>{0x9F, 0x00, 0x00}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}), ERR_UNSUPPORTED);
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_TX_GAIN, 0}), ERR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemSetRxLeavesTxUntouched) {
|
||||
board.has_lna = board.has_pa = true;
|
||||
board.pa = true;
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}), (std::vector<uint8_t>{0x9F, 0x03, 0x03}));
|
||||
EXPECT_TRUE(board.lna);
|
||||
EXPECT_EQ(board.pa_sets, 0);
|
||||
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_TX_GAIN, 0}), (std::vector<uint8_t>{0x9F, 0x03, 0x01}));
|
||||
EXPECT_FALSE(board.pa);
|
||||
EXPECT_EQ(board.lna_sets, 1);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemPartialCapabilityRejectsWholeRequest) {
|
||||
board.has_lna = true;
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN | HW_FEM_TX_GAIN, HW_FEM_RX_GAIN | HW_FEM_TX_GAIN}), ERR_UNSUPPORTED);
|
||||
EXPECT_EQ(board.lna_sets, 0);
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_FEM_STATE}), (std::vector<uint8_t>{0x9F, 0x01, 0x00}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemUnknownBitsAreUnsupported) {
|
||||
board.has_lna = board.has_pa = true;
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, 0x04, 0x04}), ERR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemUnhandledCommandsMeanUnsupported) {
|
||||
board.has_lna = board.has_pa = true;
|
||||
board.unhandled = true;
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_FEM_STATE}), (std::vector<uint8_t>{0x9F, 0x00, 0x00}));
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_CAPABILITIES}), (std::vector<uint8_t>{0x9B, 0x01, 0x00, 0x00, 0x00}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}), ERR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemUnexpectedGetWordingMeansUnsupported) {
|
||||
board.has_lna = true;
|
||||
board.odd_get_reply = "> enabled";
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_FEM_STATE}), (std::vector<uint8_t>{0x9F, 0x00, 0x00}));
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}), ERR_UNSUPPORTED);
|
||||
EXPECT_EQ(board.lna_sets, 0);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemSetThatDoesNotTakeRepliesWithReadBackState) {
|
||||
board.has_lna = board.has_pa = true;
|
||||
board.lna_sticks = true; // board says OK but LNA stays off
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN | HW_FEM_TX_GAIN, HW_FEM_RX_GAIN | HW_FEM_TX_GAIN}),
|
||||
(std::vector<uint8_t>{0x9F, 0x03, 0x02}));
|
||||
EXPECT_FALSE(board.lna);
|
||||
EXPECT_TRUE(board.pa);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemShortPayloadIsInvalidLength) {
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN}), (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_INVALID_LENGTH}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemSetDuringTxIsDeferredUntilAfterTxDone) {
|
||||
board.has_lna = true;
|
||||
startTxAndHold();
|
||||
serial.takeHardwareFrames();
|
||||
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}).empty());
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, 0}).empty()); // queued behind the first
|
||||
EXPECT_EQ(board.lna_sets, 0);
|
||||
|
||||
radio.send_complete = true;
|
||||
modem.loop(); // TX done -> TX_DONE_PENDING; FEM still held
|
||||
modem.loop(); // TxDone queued -> idle; FEM applied in order
|
||||
for (int i = 0; i < 3; i++) modem.loop();
|
||||
auto frames = serial.takeHardwareFrames();
|
||||
ASSERT_EQ(frames.size(), 3U);
|
||||
EXPECT_EQ(frames[0], (std::vector<uint8_t>{HW_RESP_TX_DONE, 0x01}));
|
||||
EXPECT_EQ(frames[1], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
EXPECT_EQ(frames[2], (std::vector<uint8_t>{0x9F, 0x01, 0x00}));
|
||||
EXPECT_FALSE(board.lna);
|
||||
EXPECT_EQ(board.lna_sets, 2);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, PipelinedFemRequestsAreAnsweredInArrivalOrder) {
|
||||
board.has_lna = true;
|
||||
startTxAndHold();
|
||||
serial.takeHardwareFrames();
|
||||
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}).empty());
|
||||
EXPECT_TRUE(hw({HW_CMD_GET_FEM_STATE}).empty());
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_TX_GAIN, HW_FEM_TX_GAIN}).empty()); // unsupported bit
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN}).empty()); // short payload
|
||||
|
||||
radio.send_complete = true;
|
||||
for (int i = 0; i < 6; i++) modem.loop();
|
||||
auto frames = serial.takeHardwareFrames();
|
||||
ASSERT_EQ(frames.size(), 5U);
|
||||
EXPECT_EQ(frames[0], (std::vector<uint8_t>{HW_RESP_TX_DONE, 0x01}));
|
||||
EXPECT_EQ(frames[1], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
EXPECT_EQ(frames[2], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
EXPECT_EQ(frames[3], ERR_UNSUPPORTED);
|
||||
EXPECT_EQ(frames[4], (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_INVALID_LENGTH}));
|
||||
EXPECT_EQ(board.lna_sets, 1);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemQueueOverflowRepliesTxBusyImmediately) {
|
||||
board.has_lna = true;
|
||||
startTxAndHold();
|
||||
serial.takeHardwareFrames();
|
||||
for (int i = 0; i < KISS_FEM_OP_QUEUE_DEPTH; i++) {
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}).empty());
|
||||
}
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_FEM_STATE}), (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_TX_BUSY}));
|
||||
EXPECT_EQ(board.lna_sets, 0);
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemGetWithNothingQueuedIsAnsweredDuringTx) {
|
||||
board.has_lna = true;
|
||||
startTxAndHold();
|
||||
serial.takeHardwareFrames();
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_FEM_STATE}), (std::vector<uint8_t>{0x9F, 0x01, 0x00}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, FemGetDuringDeferredSetIsAnsweredAfterSet) {
|
||||
board.has_lna = true;
|
||||
startTxAndHold();
|
||||
serial.takeHardwareFrames();
|
||||
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}).empty());
|
||||
EXPECT_TRUE(hw({HW_CMD_GET_FEM_STATE}).empty());
|
||||
|
||||
radio.send_complete = true;
|
||||
modem.loop();
|
||||
modem.loop();
|
||||
auto frames = serial.takeHardwareFrames();
|
||||
ASSERT_EQ(frames.size(), 3U);
|
||||
EXPECT_EQ(frames[0], (std::vector<uint8_t>{HW_RESP_TX_DONE, 0x01}));
|
||||
EXPECT_EQ(frames[1], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
EXPECT_EQ(frames[2], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, DeferredFemRepliesSurviveHostBackpressure) {
|
||||
board.has_lna = true;
|
||||
hw({HW_CMD_SET_SIGNAL_REPORT, 0}); // one output frame per RX packet
|
||||
startTxAndHold();
|
||||
serial.takeHardwareFrames();
|
||||
EXPECT_TRUE(hw({HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN}).empty());
|
||||
EXPECT_TRUE(hw({HW_CMD_GET_FEM_STATE}).empty());
|
||||
|
||||
// with writes blocked: RX packet takes slot 1, TxDone slot 2, so the Set reply cannot be queued
|
||||
serial.blocked = true;
|
||||
static constexpr uint8_t PKT[] = {0x01};
|
||||
modem.onPacketReceived(0, 0, PKT, sizeof(PKT));
|
||||
radio.send_complete = true;
|
||||
for (int i = 0; i < 5; i++) modem.loop();
|
||||
EXPECT_TRUE(board.lna); // applied once TxDone was queued, reply still held
|
||||
|
||||
serial.blocked = false;
|
||||
for (int i = 0; i < 5; i++) modem.loop();
|
||||
EXPECT_EQ(board.lna_sets, 1);
|
||||
|
||||
auto frames = serial.takeHardwareFrames();
|
||||
ASSERT_EQ(frames.size(), 3U);
|
||||
EXPECT_EQ(frames[0], (std::vector<uint8_t>{HW_RESP_TX_DONE, 0x01}));
|
||||
EXPECT_EQ(frames[1], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
EXPECT_EQ(frames[2], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
}
|
||||
|
||||
TEST_F(KissHwControlTest, ImmediateFemReplySurvivesHostBackpressure) {
|
||||
board.has_lna = true;
|
||||
hw({HW_CMD_SET_SIGNAL_REPORT, 0});
|
||||
|
||||
serial.blocked = true;
|
||||
static constexpr uint8_t PKT[] = {0x01};
|
||||
modem.onPacketReceived(0, 0, PKT, sizeof(PKT));
|
||||
modem.onPacketReceived(0, 0, PKT, sizeof(PKT)); // output queue now full
|
||||
serial.pushRx({KISS_FEND, KISS_CMD_SETHARDWARE, HW_CMD_SET_FEM_STATE, HW_FEM_RX_GAIN, HW_FEM_RX_GAIN, KISS_FEND});
|
||||
modem.loop();
|
||||
EXPECT_TRUE(board.lna);
|
||||
|
||||
serial.blocked = false;
|
||||
for (int i = 0; i < 5; i++) modem.loop();
|
||||
auto frames = serial.takeHardwareFrames();
|
||||
ASSERT_EQ(frames.size(), 1U); // FemState, not TxBusy
|
||||
EXPECT_EQ(frames[0], (std::vector<uint8_t>{0x9F, 0x01, 0x01}));
|
||||
EXPECT_EQ(board.lna_sets, 1);
|
||||
}
|
||||
|
||||
class KissRxBoostTest : public KissHwControlTest {
|
||||
protected:
|
||||
KissRxBoostTest() {
|
||||
g_rx_boost = true;
|
||||
g_rx_boost_set_ok = true;
|
||||
g_rx_boost_sets = 0;
|
||||
modem.setRxBoostedGainCallbacks(fakeSetRxBoost, fakeGetRxBoost);
|
||||
g_poll_modem = &modem;
|
||||
g_poll_has_packet = false;
|
||||
g_polls = 0;
|
||||
modem.setPollRxCallback(fakePollRx);
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(KissHwControlTest, RxBoostedGainUnsupportedWithoutCallbacks) {
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_RX_BOOSTED_GAIN}), ERR_UNSUPPORTED);
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_RX_BOOSTED_GAIN, 1}), ERR_UNSUPPORTED);
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, CapabilityAdvertised) {
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_CAPABILITIES}), (std::vector<uint8_t>{0x9B, 0x09, 0x00, 0x00, 0x00}));
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, GetReportsCurrentState) {
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_RX_BOOSTED_GAIN}), (std::vector<uint8_t>{0xA1, 0x01}));
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, SetRepliesWithEffectiveState) {
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_RX_BOOSTED_GAIN, 0}), (std::vector<uint8_t>{0xA1, 0x00}));
|
||||
EXPECT_FALSE(g_rx_boost);
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_RX_BOOSTED_GAIN, 0x7F}), (std::vector<uint8_t>{0xA1, 0x01}));
|
||||
EXPECT_TRUE(g_rx_boost);
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, SetFailureRepliesWithUnchangedState) {
|
||||
g_rx_boost_set_ok = false;
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_RX_BOOSTED_GAIN, 0}), (std::vector<uint8_t>{0xA1, 0x01}));
|
||||
EXPECT_TRUE(g_rx_boost);
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, SetDuringTxIsRejectedWithoutTouchingRadio) {
|
||||
startTxAndHold();
|
||||
serial.takeHardwareFrames();
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_RX_BOOSTED_GAIN, 0}), (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_TX_BUSY}));
|
||||
EXPECT_EQ(g_rx_boost_sets, 0);
|
||||
EXPECT_EQ(hw1({HW_CMD_GET_RX_BOOSTED_GAIN}), (std::vector<uint8_t>{0xA1, 0x01}));
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, SetShortPayloadIsInvalidLength) {
|
||||
EXPECT_EQ(hw1({HW_CMD_SET_RX_BOOSTED_GAIN}), (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_INVALID_LENGTH}));
|
||||
EXPECT_EQ(g_rx_boost_sets, 0);
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, CompletedRxPacketIsDeliveredBeforeGainChange) {
|
||||
g_poll_has_packet = true;
|
||||
auto frames = hw({HW_CMD_SET_RX_BOOSTED_GAIN, 0});
|
||||
EXPECT_EQ(g_polls, 1);
|
||||
EXPECT_FALSE(g_rx_boost);
|
||||
ASSERT_EQ(frames.size(), 2U); // RxMeta for the drained packet, then the gain reply
|
||||
EXPECT_EQ(frames[0][0], HW_RESP_RX_META);
|
||||
EXPECT_EQ(frames[1], (std::vector<uint8_t>{0xA1, 0x00}));
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, NoRoomForReplyAfterDrainChangesNothing) {
|
||||
serial.blocked = true;
|
||||
g_poll_has_packet = true; // data + meta fill both output slots
|
||||
serial.pushRx({KISS_FEND, KISS_CMD_SETHARDWARE, HW_CMD_SET_RX_BOOSTED_GAIN, 0, KISS_FEND});
|
||||
modem.loop();
|
||||
EXPECT_EQ(g_rx_boost_sets, 0);
|
||||
EXPECT_TRUE(g_rx_boost);
|
||||
|
||||
serial.blocked = false;
|
||||
for (int i = 0; i < 5; i++) modem.loop();
|
||||
auto frames = serial.takeHardwareFrames();
|
||||
ASSERT_EQ(frames.size(), 2U);
|
||||
EXPECT_EQ(frames[0][0], HW_RESP_RX_META);
|
||||
EXPECT_EQ(frames[1], (std::vector<uint8_t>{HW_RESP_ERROR, HW_ERR_TX_BUSY}));
|
||||
}
|
||||
|
||||
TEST_F(KissRxBoostTest, SetWhileHostOutputBackedUpIsRejected) {
|
||||
serial.blocked = true;
|
||||
static constexpr uint8_t PKT[] = {0x01};
|
||||
modem.onPacketReceived(0, 0, PKT, sizeof(PKT));
|
||||
serial.pushRx({KISS_FEND, KISS_CMD_SETHARDWARE, HW_CMD_SET_RX_BOOSTED_GAIN, 0, KISS_FEND});
|
||||
modem.loop();
|
||||
EXPECT_EQ(g_polls, 0);
|
||||
EXPECT_EQ(g_rx_boost_sets, 0);
|
||||
}
|
||||
|
||||
} // namespace
|
||||
@@ -158,14 +158,14 @@ bool T096Board::handleCommand(const char* command, uint32_t sender_timestamp, ch
|
||||
strcpy(reply, "Error: unsupported");
|
||||
} else if (memcmp(&command[21], "on", 2) == 0) {
|
||||
if (setLoRaFemLnaEnabled(true)) {
|
||||
_prefs->setByKey("fem_rxgain", "1");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "1");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain on");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
}
|
||||
} else if (memcmp(&command[21], "off", 3) == 0) {
|
||||
if (setLoRaFemLnaEnabled(false)) {
|
||||
_prefs->setByKey("fem_rxgain", "0");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "0");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain off");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
|
||||
@@ -99,14 +99,14 @@ bool HeltecTrackerV2Board::handleCommand(const char* command, uint32_t sender_ti
|
||||
strcpy(reply, "Error: unsupported");
|
||||
} else if (memcmp(&command[21], "on", 2) == 0) {
|
||||
if (setLoRaFemLnaEnabled(true)) {
|
||||
_prefs->setByKey("fem_rxgain", "1");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "1");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain on");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
}
|
||||
} else if (memcmp(&command[21], "off", 3) == 0) {
|
||||
if (setLoRaFemLnaEnabled(false)) {
|
||||
_prefs->setByKey("fem_rxgain", "0");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "0");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain off");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
|
||||
@@ -100,14 +100,14 @@ bool HeltecV4Board::handleCommand(const char* command, uint32_t sender_timestamp
|
||||
strcpy(reply, "Error: unsupported");
|
||||
} else if (memcmp(&command[21], "on", 2) == 0) {
|
||||
if (setLoRaFemLnaEnabled(true)) {
|
||||
_prefs->setByKey("fem_rxgain", "1");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "1");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain on");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
}
|
||||
} else if (memcmp(&command[21], "off", 3) == 0) {
|
||||
if (setLoRaFemLnaEnabled(false)) {
|
||||
_prefs->setByKey("fem_rxgain", "0");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "0");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain off");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
|
||||
@@ -122,14 +122,14 @@ bool HeltecV4R8Board::handleCommand(const char* command, uint32_t sender_timesta
|
||||
strcpy(reply, "Error: unsupported");
|
||||
} else if (memcmp(&command[21], "on", 2) == 0) {
|
||||
if (setLoRaFemLnaEnabled(true)) {
|
||||
_prefs->setByKey("fem_rxgain", "1");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "1");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain on");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
}
|
||||
} else if (memcmp(&command[21], "off", 3) == 0) {
|
||||
if (setLoRaFemLnaEnabled(false)) {
|
||||
_prefs->setByKey("fem_rxgain", "0");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "0");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain off");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
|
||||
@@ -42,7 +42,11 @@ public:
|
||||
}
|
||||
|
||||
const char* getManufacturerName() const override {
|
||||
#if defined(PICO_RP2350)
|
||||
return "Pico 2 W";
|
||||
#else
|
||||
return "Pico W";
|
||||
#endif
|
||||
}
|
||||
|
||||
void reboot() override {
|
||||
|
||||
@@ -56,8 +56,8 @@ build_src_filter = ${rpi_picow.build_src_filter}
|
||||
[env:PicoW_companion_radio_usb]
|
||||
extends = rpi_picow
|
||||
build_flags = ${rpi_picow.build_flags}
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D MAX_CONTACTS=350
|
||||
-D MAX_GROUP_CHANNELS=40
|
||||
-D ENABLE_USB_INTERFACE
|
||||
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
|
||||
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
|
||||
@@ -70,33 +70,14 @@ lib_ignore = BLE
|
||||
[env:PicoW_companion_radio_ble]
|
||||
extends = rpi_picow
|
||||
build_flags = ${rpi_picow.build_flags}
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D MAX_CONTACTS=350
|
||||
-D MAX_GROUP_CHANNELS=40
|
||||
-D PIO_FRAMEWORK_ARDUINO_ENABLE_BLUETOOTH
|
||||
-D ENABLE_WIFI_INTERFACE
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_picow.build_src_filter}
|
||||
+<helpers/rp2040/SerialBLEInterface.cpp>
|
||||
+<../examples/companion_radio/*.cpp>
|
||||
lib_deps = ${rpi_picow.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
|
||||
; USB + WiFi + BLE together; the interface manager fans frames out to all of them
|
||||
[env:PicoW_companion_radio]
|
||||
extends = rpi_picow
|
||||
build_flags = ${rpi_picow.build_flags}
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D ENABLE_USB_INTERFACE
|
||||
-D ENABLE_WIFI_INTERFACE
|
||||
-D PIO_FRAMEWORK_ARDUINO_ENABLE_BLUETOOTH
|
||||
-D BLE_PIN_CODE=123456
|
||||
; NOTE: DO NOT ENABLE --> -D BLE_DEBUG_LOGGING=1 (shares Serial with the USB interface)
|
||||
; NOTE: DO NOT ENABLE --> -D WIFI_DEBUG_LOGGING=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_picow.build_src_filter}
|
||||
+<helpers/rp2040/SerialBLEInterface.cpp>
|
||||
+<helpers/wifi/*.cpp>
|
||||
@@ -104,29 +85,11 @@ build_src_filter = ${rpi_picow.build_src_filter}
|
||||
lib_deps = ${rpi_picow.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
|
||||
[env:PicoW_companion_radio_wifi]
|
||||
extends = rpi_picow
|
||||
build_flags = ${rpi_picow.build_flags}
|
||||
-D ENABLE_WIFI_INTERFACE
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=8
|
||||
-D WIFI_DEBUG_LOGGING=1
|
||||
-D WIFI_SSID='""' ; no default network, configure with 'set wifi.ssid' / 'set wifi.pwd'
|
||||
-D WIFI_PWD='""'
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_picow.build_src_filter}
|
||||
+<helpers/wifi/*.cpp>
|
||||
+<../examples/companion_radio/*.cpp>
|
||||
lib_deps = ${rpi_picow.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
lib_ignore = BLE
|
||||
|
||||
[env:PicoW_terminal_chat]
|
||||
extends = rpi_picow
|
||||
build_flags = ${rpi_picow.build_flags}
|
||||
-D MAX_CONTACTS=100
|
||||
-D MAX_GROUP_CHANNELS=1
|
||||
-D MAX_CONTACTS=350
|
||||
-D MAX_GROUP_CHANNELS=40
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_picow.build_src_filter}
|
||||
@@ -138,3 +101,87 @@ lib_deps = ${rpi_picow.lib_deps}
|
||||
extends = rpi_picow
|
||||
build_src_filter = ${rpi_picow.build_src_filter}
|
||||
+<../examples/kiss_modem/>
|
||||
|
||||
[rpi_pico2w]
|
||||
extends = rp2040_base
|
||||
board = rpipico2w
|
||||
board_build.core = earlephilhower
|
||||
board_build.filesystem_size = 0.5m
|
||||
build_flags = ${rpi_picow.build_flags}
|
||||
build_src_filter = ${rpi_picow.build_src_filter}
|
||||
lib_deps = ${rpi_picow.lib_deps}
|
||||
|
||||
[env:Pico2W_repeater]
|
||||
extends = rpi_pico2w
|
||||
build_flags = ${rpi_pico2w.build_flags}
|
||||
-D ADVERT_NAME='"Pico2W Repeater"'
|
||||
-D ADVERT_LAT=0.0
|
||||
-D ADVERT_LON=0.0
|
||||
-D ADMIN_PASSWORD='"password"'
|
||||
-D MAX_NEIGHBOURS=50
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_pico2w.build_src_filter}
|
||||
+<../examples/simple_repeater>
|
||||
|
||||
[env:Pico2W_room_server]
|
||||
extends = rpi_pico2w
|
||||
build_flags = ${rpi_pico2w.build_flags}
|
||||
-D ADVERT_NAME='"Test Room"'
|
||||
-D ADVERT_LAT=0.0
|
||||
-D ADVERT_LON=0.0
|
||||
-D ADMIN_PASSWORD='"password"'
|
||||
-D ROOM_PASSWORD='"hello"'
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_pico2w.build_src_filter}
|
||||
+<../examples/simple_room_server>
|
||||
|
||||
[env:Pico2W_companion_radio_usb]
|
||||
extends = rpi_pico2w
|
||||
build_flags = ${rpi_pico2w.build_flags}
|
||||
-D MAX_CONTACTS=350
|
||||
-D MAX_GROUP_CHANNELS=40
|
||||
-D ENABLE_USB_INTERFACE
|
||||
; NOTE: DO NOT ENABLE --> -D MESH_PACKET_LOGGING=1
|
||||
; NOTE: DO NOT ENABLE --> -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_pico2w.build_src_filter}
|
||||
+<../examples/companion_radio/*.cpp>
|
||||
lib_deps = ${rpi_pico2w.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
lib_ignore = BLE
|
||||
|
||||
[env:Pico2W_companion_radio_ble]
|
||||
extends = rpi_pico2w
|
||||
build_flags = ${rpi_pico2w.build_flags}
|
||||
-D MAX_CONTACTS=350
|
||||
-D MAX_GROUP_CHANNELS=40
|
||||
-D PIO_FRAMEWORK_ARDUINO_ENABLE_BLUETOOTH
|
||||
-D ENABLE_WIFI_INTERFACE
|
||||
-D BLE_PIN_CODE=123456
|
||||
-D BLE_DEBUG_LOGGING=1
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_pico2w.build_src_filter}
|
||||
+<helpers/rp2040/SerialBLEInterface.cpp>
|
||||
+<helpers/wifi/*.cpp>
|
||||
+<../examples/companion_radio/*.cpp>
|
||||
lib_deps = ${rpi_pico2w.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
|
||||
[env:Pico2W_terminal_chat]
|
||||
extends = rpi_pico2w
|
||||
build_flags = ${rpi_pico2w.build_flags}
|
||||
-D MAX_CONTACTS=350
|
||||
-D MAX_GROUP_CHANNELS=40
|
||||
; -D MESH_PACKET_LOGGING=1
|
||||
; -D MESH_DEBUG=1
|
||||
build_src_filter = ${rpi_pico2w.build_src_filter}
|
||||
+<../examples/simple_secure_chat/main.cpp>
|
||||
lib_deps = ${rpi_pico2w.lib_deps}
|
||||
densaugeo/base64 @ ~1.4.0
|
||||
|
||||
[env:Pico2W_kiss_modem]
|
||||
extends = rpi_pico2w
|
||||
build_src_filter = ${rpi_pico2w.build_src_filter}
|
||||
+<../examples/kiss_modem/>
|
||||
@@ -65,14 +65,14 @@ bool StationG3Board::handleCommand(const char* command, uint32_t sender_timestam
|
||||
strcpy(reply, "Error: unsupported");
|
||||
} else if (memcmp(&command[21], "on", 2) == 0) {
|
||||
if (setLoRaFemLnaEnabled(true)) {
|
||||
_prefs->setByKey("fem_rxgain", "1");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "1");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain on");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
}
|
||||
} else if (memcmp(&command[21], "off", 3) == 0) {
|
||||
if (setLoRaFemLnaEnabled(false)) {
|
||||
_prefs->setByKey("fem_rxgain", "0");
|
||||
if (_prefs) _prefs->setByKey("fem_rxgain", "0");
|
||||
strcpy(reply, "OK - LoRa FEM RX gain off");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM RX gain");
|
||||
@@ -96,14 +96,14 @@ bool StationG3Board::handleCommand(const char* command, uint32_t sender_timestam
|
||||
strcpy(reply, "Error: unsupported");
|
||||
} else if (memcmp(&command[21], "on", 2) == 0) {
|
||||
if (setLoRaFemPaGainEnabled(true)) {
|
||||
_prefs->setByKey("fem_txgain", "1");
|
||||
if (_prefs) _prefs->setByKey("fem_txgain", "1");
|
||||
strcpy(reply, "OK - LoRa FEM TX gain on");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM TX gain");
|
||||
}
|
||||
} else if (memcmp(&command[21], "off", 3) == 0) {
|
||||
if (setLoRaFemPaGainEnabled(false)) {
|
||||
_prefs->setByKey("fem_txgain", "0");
|
||||
if (_prefs) _prefs->setByKey("fem_txgain", "0");
|
||||
strcpy(reply, "OK - LoRa FEM TX gain off");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM TX gain");
|
||||
|
||||
@@ -65,14 +65,14 @@ bool WioTrackerL1Board::handleCommand(const char* command, uint32_t sender_times
|
||||
if (memcmp(command, "set radio.fem.txgain ", 21) == 0) {
|
||||
if (memcmp(&command[21], "on", 2) == 0) {
|
||||
if (setLoRaFemPaGainEnabled(true)) {
|
||||
_prefs->setByKey("fem_txgain", "1");
|
||||
if (_prefs) _prefs->setByKey("fem_txgain", "1");
|
||||
strcpy(reply, "OK - LoRa FEM TX gain on");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM TX gain");
|
||||
}
|
||||
} else if (memcmp(&command[21], "off", 3) == 0) {
|
||||
if (setLoRaFemPaGainEnabled(false)) {
|
||||
_prefs->setByKey("fem_txgain", "0");
|
||||
if (_prefs) _prefs->setByKey("fem_txgain", "0");
|
||||
strcpy(reply, "OK - LoRa FEM TX gain off");
|
||||
} else {
|
||||
strcpy(reply, "Error: failed to apply LoRa FEM TX gain");
|
||||
|
||||
Reference in new issue
Block a user