RadioLibWrappers.h 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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;
  9. void idle();
  10. public:
  11. RadioLibWrapper(PhysicalLayer& radio, mesh::MainBoard& board) : _radio(&radio), _board(&board) { n_recv = n_sent = 0; }
  12. void begin() override;
  13. int recvRaw(uint8_t* bytes, int sz) override;
  14. uint32_t getEstAirtimeFor(int len_bytes) override;
  15. void startSendRaw(const uint8_t* bytes, int len) override;
  16. bool isSendComplete() override;
  17. void onSendFinished() override;
  18. uint32_t getPacketsRecv() const { return n_recv; }
  19. uint32_t getPacketsSent() const { return n_sent; }
  20. virtual float getLastRSSI() const override;
  21. virtual float getLastSNR() const override;
  22. };
  23. /**
  24. * \brief an RNG impl using the noise from the LoRa radio as entropy.
  25. * NOTE: this is VERY SLOW! Use only for things like creating new LocalIdentity
  26. */
  27. class RadioNoiseListener : public mesh::RNG {
  28. PhysicalLayer* _radio;
  29. public:
  30. RadioNoiseListener(PhysicalLayer& radio): _radio(&radio) { }
  31. void random(uint8_t* dest, size_t sz) override {
  32. for (int i = 0; i < sz; i++) {
  33. dest[i] = _radio->randomByte() ^ (::random(0, 256) & 0xFF);
  34. }
  35. }
  36. };