RadioLibWrappers.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. #pragma once
  2. #include <Mesh.h>
  3. #include <RadioLib.h>
  4. class RadioLibWrapper : public mesh::Radio {
  5. protected:
  6. PhysicalLayer* _radio;
  7. mesh::MainBoard* _board;
  8. uint32_t n_recv, n_sent, n_recv_errors;
  9. int16_t _noise_floor, _threshold;
  10. uint16_t _num_floor_samples;
  11. int32_t _floor_sample_sum;
  12. uint8_t _preamble_sf;
  13. void idle();
  14. void startRecv();
  15. float packetScoreInt(float snr, int sf, int packet_len);
  16. virtual bool isReceivingPacket() =0;
  17. virtual void doResetAGC();
  18. public:
  19. RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board), _preamble_sf(0) { n_recv = n_sent = 0; }
  20. void begin() override;
  21. virtual void powerOff() { _radio->sleep(); }
  22. int recvRaw(uint8_t* bytes, int sz) override;
  23. uint32_t getEstAirtimeFor(int len_bytes) override;
  24. bool startSendRaw(const uint8_t* bytes, int len) override;
  25. bool isSendComplete() override;
  26. void onSendFinished() override;
  27. bool isInRecvMode() const override;
  28. bool isChannelActive();
  29. bool isReceiving() override {
  30. if (isReceivingPacket()) return true;
  31. return isChannelActive();
  32. }
  33. virtual void setParams(float freq, float bw, uint8_t sf, uint8_t cr) = 0;
  34. uint32_t getRngSeed();
  35. void setTxPower(int8_t dbm);
  36. virtual float getCurrentRSSI() =0;
  37. virtual uint8_t getSpreadingFactor() const { return LORA_SF; }
  38. static uint16_t preambleLengthForSF(uint8_t sf) { return sf <= 8 ? 32 : 16; }
  39. void updatePreamble(uint8_t sf) { _preamble_sf = sf; _radio->setPreambleLength(preambleLengthForSF(sf)); }
  40. int getNoiseFloor() const override { return _noise_floor; }
  41. void triggerNoiseFloorCalibrate(int threshold) override;
  42. void resetAGC() override;
  43. void loop() override;
  44. uint32_t getPacketsRecv() const { return n_recv; }
  45. uint32_t getPacketsRecvErrors() const { return n_recv_errors; }
  46. uint32_t getPacketsSent() const { return n_sent; }
  47. void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
  48. virtual float getLastRSSI() const override;
  49. virtual float getLastSNR() const override;
  50. float packetScore(float snr, int packet_len) override { return packetScoreInt(snr, 10, packet_len); } // assume sf=10
  51. virtual void setRxBoostedGainMode(bool) { }
  52. virtual bool getRxBoostedGainMode() const { return false; }
  53. };
  54. /**
  55. * \brief an RNG impl using the noise from the LoRa radio as entropy.
  56. * NOTE: this is VERY SLOW! Use only for things like creating new LocalIdentity
  57. */
  58. class RadioNoiseListener : public mesh::RNG {
  59. PhysicalLayer* _radio;
  60. public:
  61. RadioNoiseListener(PhysicalLayer& radio): _radio(&radio) { }
  62. void random(uint8_t* dest, size_t sz) override {
  63. for (int i = 0; i < sz; i++) {
  64. dest[i] = _radio->randomByte() ^ (::random(0, 256) & 0xFF);
  65. }
  66. }
  67. };