229 lines
9.2 KiB
C++
229 lines
9.2 KiB
C++
#pragma once
|
|
|
|
#include <Mesh.h>
|
|
#include <RadioLib.h>
|
|
#ifdef WITH_RX_POWERSAVING
|
|
// CUSTOM (TeTeHacko): RX duty cycle ("RXPS"), ported from EasySkyMesh PowerSaving
|
|
// v18 (IoTThinks/MeshCore, MIT). Build-flag only and off at runtime by default:
|
|
// without the flag this class is byte-for-byte what it was. See RXPowerSaving.h.
|
|
#include "RXPowerSaving.h"
|
|
#if defined(LORA_POLL_IRQ)
|
|
#error "WITH_RX_POWERSAVING and LORA_POLL_IRQ do not mix: polling the IRQ register wakes a duty-cycling chip"
|
|
#endif
|
|
#endif
|
|
|
|
#ifdef USE_CC310_HW_CRYPTO
|
|
#include <Adafruit_nRFCrypto.h>
|
|
#endif
|
|
struct PacketMillis {
|
|
uint32_t preambleMillis; // preamble-detect -> header-valid deadline
|
|
uint32_t payloadMillis; // header-valid -> rx-done deadline
|
|
};
|
|
|
|
class RadioLibWrapper : public mesh::Radio
|
|
#ifdef WITH_RX_POWERSAVING
|
|
, public RxPowerSavingControl
|
|
#endif
|
|
{
|
|
protected:
|
|
PhysicalLayer* _radio;
|
|
mesh::MainBoard* _board;
|
|
uint32_t n_recv, n_sent, n_recv_errors;
|
|
int16_t _noise_floor, _threshold;
|
|
bool _cad_enabled;
|
|
uint16_t _num_floor_samples;
|
|
int32_t _floor_sample_sum;
|
|
uint8_t _preamble_sf;
|
|
int16_t _last_txpow_status; // CUSTOM (TeTeHacko): see getLastTxPowerStatus()
|
|
int8_t _last_txpow_dbm;
|
|
|
|
void idle();
|
|
void startRecv();
|
|
#ifdef WITH_RX_POWERSAVING
|
|
// RXPS state. _rx_ps_enabled = what the config asks for, _rx_ps_armed = the
|
|
// chip is duty cycling right now (cleared by anything that takes it out).
|
|
bool _rx_ps_restart_pending = false;
|
|
bool _rx_ps_config_error = false;
|
|
bool _rx_ps_enabled = false;
|
|
bool _rx_ps_armed = false;
|
|
uint32_t _rx_ps_rx_us = RX_POWERSAVING_DEFAULT_RX_US;
|
|
uint32_t _rx_ps_sleep_us = RX_POWERSAVING_DEFAULT_SLEEP_US;
|
|
uint32_t _rx_ps_eff_rx_us = 0; // periods the driver actually armed with
|
|
uint32_t _rx_ps_eff_sleep_us = 0;
|
|
int16_t _rx_ps_last_error = 0;
|
|
uint32_t n_rx_ps_arm_failures = 0;
|
|
RxPowerSavingArmRetryState _rx_ps_arm_retry;
|
|
// Noise floor while duty cycling: RSSI cannot be sampled in the sleep phase,
|
|
// so every NF_CALIB_INTERVAL the receiver goes continuous for a short window.
|
|
bool _nf_calib_active = false;
|
|
unsigned long _nf_last_calib = 0;
|
|
unsigned long _nf_calib_deadline = 0;
|
|
unsigned long _nf_sample_from = 0;
|
|
// RSSI/SNR of the last packet, read at RX: reading the registers later, with
|
|
// the chip asleep between windows, would wake it and return nothing useful.
|
|
float _last_rssi = 0, _last_snr = 0;
|
|
bool _last_metrics_valid = false;
|
|
|
|
void requestRestartRecv();
|
|
bool isPacketPendingOrReceiving();
|
|
void prepareForRadioConfig();
|
|
void sampleNoiseFloorOnce();
|
|
bool publishNoiseFloor();
|
|
void noiseFloorCalibCheck();
|
|
void endNoiseFloorCalib(unsigned long now);
|
|
int16_t startReceiveMode();
|
|
void stopReceiveDutyCycle();
|
|
// eff_rx_us/eff_sleep_us report the periods the driver really programmed.
|
|
virtual int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags, RadioLibIrqFlags_t irq_mask,
|
|
uint32_t* eff_rx_us, uint32_t* eff_sleep_us) {
|
|
(void)irq_flags; (void)irq_mask; (void)eff_rx_us; (void)eff_sleep_us;
|
|
return RADIOLIB_ERR_UNSUPPORTED;
|
|
}
|
|
virtual int16_t stopDutyCycleHardware() { return _radio->standby(); }
|
|
virtual int16_t startContinuousReceive() { return _radio->startReceive(); }
|
|
virtual bool isPacketReady();
|
|
virtual bool isChipBusy() { return false; }
|
|
#endif
|
|
#ifdef LORA_POLL_IRQ
|
|
void pollIrq(); // CUSTOM (TeTeHacko): DIO1-less completion detection, see .cpp
|
|
#endif
|
|
float packetScoreInt(float snr, int sf, int packet_len);
|
|
virtual bool isReceivingPacket() =0;
|
|
virtual bool lastRxHadCRC() { return true; }
|
|
virtual void doResetAGC();
|
|
|
|
public:
|
|
RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board)
|
|
: _radio(&radio), _board(&board), _preamble_sf(0), _last_txpow_status(0), _last_txpow_dbm(0) {
|
|
n_recv = n_sent = n_recv_errors = 0;
|
|
}
|
|
|
|
void begin() override;
|
|
#ifdef WITH_RX_POWERSAVING
|
|
virtual void powerOff() { if (_rx_ps_armed) stopReceiveDutyCycle(); _radio->sleep(); }
|
|
|
|
virtual bool supportsRxPowerSaving() const { return false; }
|
|
void setRxPowerSavingConfigError(bool invalid) override { _rx_ps_config_error = invalid; }
|
|
bool setRxPowerSaving(bool enabled, uint32_t rx_us, uint32_t sleep_us) override;
|
|
RxPowerSavingStatus getRxPowerSavingStatus() const override;
|
|
bool isRxPowerSavingCalibrationActive() const override { return _nf_calib_active; }
|
|
#else
|
|
virtual void powerOff() { _radio->sleep(); }
|
|
#endif
|
|
int recvRaw(uint8_t* bytes, int sz) override;
|
|
uint32_t getEstAirtimeFor(int len_bytes) override;
|
|
bool startSendRaw(const uint8_t* bytes, int len) override;
|
|
bool isSendComplete() override;
|
|
void onSendFinished() override;
|
|
bool isInRecvMode() const override;
|
|
bool isChannelActive();
|
|
|
|
bool isReceiving() override {
|
|
if (isReceivingPacket()) return true;
|
|
|
|
return isChannelActive();
|
|
}
|
|
|
|
virtual void setParams(float freq, float bw, uint8_t sf, uint8_t cr) = 0;
|
|
uint32_t getRngSeed();
|
|
void setTxPower(int8_t dbm);
|
|
// CUSTOM (TeTeHacko): RadioLib status of the LAST setTxPower() call.
|
|
// setTxPower() has to stay void (it implements an upstream callback), but
|
|
// discarding the status made a failed change indistinguishable from a
|
|
// successful one: SX1262 only accepts -9..+22 dBm and REJECTS anything else
|
|
// without touching the PA, while `set tx` clamps to -9..30, saves the pref and
|
|
// answers "OK". The node then reports a tx power the radio never took.
|
|
// 0 = RADIOLIB_ERR_NONE, -13 = RADIOLIB_ERR_INVALID_OUTPUT_POWER.
|
|
int16_t getLastTxPowerStatus() const { return _last_txpow_status; }
|
|
int8_t getLastTxPowerRequested() const { return _last_txpow_dbm; }
|
|
|
|
virtual float getCurrentRSSI() =0;
|
|
virtual uint8_t getSpreadingFactor() const { return LORA_SF; }
|
|
static uint16_t preambleLengthForSF(uint8_t sf) { return sf <= 8 ? 32 : 16; }
|
|
void updatePreamble(uint8_t sf) { _preamble_sf = sf; _radio->setPreambleLength(preambleLengthForSF(sf)); }
|
|
PacketMillis calcMaxPacketMillis(uint8_t sf, float bw, uint8_t cr, uint8_t preambleSymbols);
|
|
virtual int16_t performChannelScan();
|
|
|
|
int getNoiseFloor() const override { return _noise_floor; }
|
|
void triggerNoiseFloorCalibrate(int threshold) override;
|
|
void setCADEnabled(bool enable) override { _cad_enabled = enable; }
|
|
void resetAGC() override;
|
|
|
|
void loop() override;
|
|
|
|
uint32_t getPacketsRecv() const { return n_recv; }
|
|
// CUSTOM (TeTeHacko): first recovery step of the radio silence watchdog --
|
|
// drop latched IRQs and re-arm RX. Leaves a transmit in progress alone.
|
|
void kickRx();
|
|
uint32_t getPacketsRecvErrors() const { return n_recv_errors; }
|
|
uint32_t getPacketsSent() const { return n_sent; }
|
|
#ifdef WITH_RX_POWERSAVING
|
|
void resetStats() {
|
|
n_recv = n_sent = n_recv_errors = n_rx_ps_arm_failures = 0;
|
|
_rx_ps_arm_retry.reset(); // a fresh set of arm attempts, see startReceiveMode()
|
|
}
|
|
#else
|
|
void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
|
|
#endif
|
|
|
|
virtual float getLastRSSI() const override;
|
|
virtual float getLastSNR() const override;
|
|
|
|
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; }
|
|
|
|
virtual bool configSideDetectors(const uint8_t sideDetSFs[], uint8_t num, float bw) { return false; }
|
|
|
|
#if defined(LORA_POLL_IRQ) || defined(PIN_DIAG)
|
|
// CUSTOM (TeTeHacko): chip-native IRQ status bits that mean "the operation the
|
|
// driver is waiting for has finished" -- i.e. exactly what the DIO1 interrupt
|
|
// would have told us. getIrqFlags() returns the RAW chip register (SX126x
|
|
// returns its own bitmask, not RadioLib's generic RADIOLIB_IRQ_* indices), so
|
|
// the mask has to come from the chip-specific wrapper. Returning 0 disables
|
|
// both the polling fallback and the DIO1 probe for radios that don't override.
|
|
virtual uint32_t irqDoneMask() const { return 0; }
|
|
#endif
|
|
|
|
#ifdef PIN_DIAG
|
|
// CUSTOM (TeTeHacko): salvage diagnostics -- see `dio1` in simple_repeater.
|
|
int probeDio1(uint32_t* irq_out, int* dio1_level, int* probe_level);
|
|
#endif
|
|
|
|
#ifdef RADIO_DIAG
|
|
// CUSTOM (TeTeHacko): `radiostat` / `radiokick` -- what state is a receiver
|
|
// that has gone deaf actually in? See RadioLibWrappers.cpp.
|
|
bool diagHandleCommand(const char* command, char* reply);
|
|
protected:
|
|
uint32_t _diag_last_rx_ms = 0; // millis() of the last packet read without error
|
|
// Chip-side half of the snapshot: mode, IRQ, errors, pins. Default = none.
|
|
virtual int diagChip(char* out, int sz) { return snprintf(out, sz, " chip=n/a"); }
|
|
public:
|
|
#endif
|
|
};
|
|
|
|
/**
|
|
* \brief an RNG impl using the noise from the LoRa radio as entropy.
|
|
* NOTE: this is VERY SLOW! Use only for things like creating new LocalIdentity
|
|
*/
|
|
class RadioNoiseListener : public mesh::RNG {
|
|
PhysicalLayer* _radio;
|
|
public:
|
|
RadioNoiseListener(PhysicalLayer& radio): _radio(&radio) { }
|
|
|
|
void random(uint8_t* dest, size_t sz) override {
|
|
#ifdef USE_CC310_HW_CRYPTO
|
|
nRFCrypto.Random.generate(dest, (uint16_t)sz);
|
|
for (int i = 0; i < sz; i++) {
|
|
dest[i] ^= _radio->randomByte() ^ (::random(0, 256) & 0xFF); // combine with Radio's entropy
|
|
}
|
|
#else
|
|
for (int i = 0; i < sz; i++) {
|
|
dest[i] = _radio->randomByte() ^ (::random(0, 256) & 0xFF);
|
|
}
|
|
#endif
|
|
}
|
|
};
|