RadioLibWrappers.h 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. void idle();
  13. void startRecv();
  14. float packetScoreInt(float snr, int sf, int packet_len);
  15. virtual bool isReceivingPacket() =0;
  16. virtual void doResetAGC();
  17. public:
  18. RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board) { n_recv = n_sent = 0; }
  19. void begin() override;
  20. virtual void powerOff() { _radio->sleep(); }
  21. int recvRaw(uint8_t* bytes, int sz) override;
  22. uint32_t getEstAirtimeFor(int len_bytes) override;
  23. bool startSendRaw(const uint8_t* bytes, int len) override;
  24. bool isSendComplete() override;
  25. void onSendFinished() override;
  26. bool isInRecvMode() const override;
  27. bool isChannelActive();
  28. bool isReceiving() override {
  29. if (isReceivingPacket()) return true;
  30. return isChannelActive();
  31. }
  32. virtual float getCurrentRSSI() =0;
  33. int getNoiseFloor() const override { return _noise_floor; }
  34. void triggerNoiseFloorCalibrate(int threshold) override;
  35. void resetAGC() override;
  36. void loop() override;
  37. uint32_t getPacketsRecv() const { return n_recv; }
  38. uint32_t getPacketsRecvErrors() const { return n_recv_errors; }
  39. uint32_t getPacketsSent() const { return n_sent; }
  40. void resetStats() { n_recv = n_sent = n_recv_errors = 0; }
  41. virtual float getLastRSSI() const override;
  42. virtual float getLastSNR() const override;
  43. float packetScore(float snr, int packet_len) override { return packetScoreInt(snr, 10, packet_len); } // assume sf=10
  44. };
  45. /**
  46. * \brief an RNG impl using the noise from the LoRa radio as entropy.
  47. * NOTE: this is VERY SLOW! Use only for things like creating new LocalIdentity
  48. */
  49. class RadioNoiseListener : public mesh::RNG {
  50. PhysicalLayer* _radio;
  51. public:
  52. RadioNoiseListener(PhysicalLayer& radio): _radio(&radio) { }
  53. void random(uint8_t* dest, size_t sz) override {
  54. for (int i = 0; i < sz; i++) {
  55. dest[i] = _radio->randomByte() ^ (::random(0, 256) & 0xFF);
  56. }
  57. }
  58. };