SPSP
Simple publish-subscribe protocol. Connects low power IoT clients to MQTT.
All Classes Files Functions Variables Typedefs Enumerations
testing/spsp/espnow_adapter.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <string>
13 #include <unordered_set>
14 
15 #include "spsp/espnow_adapter_if.hpp"
16 #include "spsp/espnow_types.hpp"
17 
18 namespace SPSP::LocalLayers::ESPNOW
19 {
24  class Adapter : public IAdapter
25  {
26  AdapterRecvCb m_recvCb = nullptr;
27  AdapterSendCb m_sendCb = nullptr;
28  std::unordered_set<LocalAddrT> m_peers;
29 
30  public:
38  void setRecvCb(AdapterRecvCb cb) noexcept
39  {
40  m_recvCb = cb;
41  }
42 
48  AdapterRecvCb getRecvCb() const noexcept
49  {
50  return m_recvCb;
51  }
52 
58  void setSendCb(AdapterSendCb cb) noexcept
59  {
60  m_sendCb = cb;
61  }
62 
68  AdapterSendCb getSendCb() const noexcept
69  {
70  return m_sendCb;
71  }
72 
81  virtual void send(const LocalAddrT& dst, const std::string& data) const
82  {
83  std::thread t(this->getSendCb(), dst, true);
84  t.detach();
85  }
86 
93  void addPeer(const LocalAddrT& peer)
94  {
95  m_peers.insert(peer);
96  }
97 
104  void removePeer(const LocalAddrT& peer)
105  {
106  if (!m_peers.erase(peer)) {
107  throw AdapterError("Can't remove non-existing peer");
108  }
109  }
110  };
111 } // namespace SPSP::LocalLayers::ESPNOW
SPSP::LocalLayers::ESPNOW::Adapter::addPeer
void addPeer(const LocalAddrT &peer)
Adds peer to peer list.
Definition: testing/spsp/espnow_adapter.hpp:93
SPSP::LocalLayers::ESPNOW::Adapter::setRecvCb
void setRecvCb(AdapterRecvCb cb) noexcept
Sets receive callback.
Definition: testing/spsp/espnow_adapter.hpp:38
SPSP::LocalLayers::ESPNOW::Adapter::getSendCb
AdapterSendCb getSendCb() const noexcept
Gets send callback.
Definition: testing/spsp/espnow_adapter.hpp:68
SPSP::LocalLayers::ESPNOW::Adapter::send
virtual void send(const LocalAddrT &dst, const std::string &data) const
Sends local message.
Definition: testing/spsp/espnow_adapter.hpp:81
SPSP::LocalLayers::ESPNOW::Adapter::setSendCb
void setSendCb(AdapterSendCb cb) noexcept
Sets send callback.
Definition: testing/spsp/espnow_adapter.hpp:58
SPSP::LocalLayers::ESPNOW::Adapter::getRecvCb
AdapterRecvCb getRecvCb() const noexcept
Gets receive callback.
Definition: testing/spsp/espnow_adapter.hpp:48
SPSP::LocalLayers::ESPNOW::Adapter::removePeer
void removePeer(const LocalAddrT &peer)
Removes peer from peer list.
Definition: testing/spsp/espnow_adapter.hpp:104
SPSP::LocalLayers::ESPNOW::AdapterError
Adapter error.
Definition: espnow_adapter_if.hpp:24
espnow_types.hpp
Types for ESPNOW classes.
SPSP::LocalAddrMAC
Local layer address container for MAC address.
Definition: local_addr_mac.hpp:24