SPSP
Simple publish-subscribe protocol. Connects low power IoT clients to MQTT.
All Classes Files Functions Variables Typedefs Enumerations
espnow.hpp
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include <array>
13 #include <climits>
14 #include <future>
15 #include <mutex>
16 #include <string>
17 
18 #include "spsp/espnow_adapter_if.hpp"
19 #include "spsp/espnow_packet.hpp"
20 #include "spsp/espnow_ser_des.hpp"
21 #include "spsp/espnow_types.hpp"
22 #include "spsp/layers.hpp"
23 #include "spsp/local_addr_mac.hpp"
24 #include "spsp/wifi_espnow_if.hpp"
25 
26 namespace SPSP::LocalLayers::ESPNOW
27 {
28  static constexpr int SIGNAL_MIN = INT_MIN;
29 
37  static constexpr uint8_t MAX_PEER_NUM = 15;
38 
45  {
46  uint8_t addr[6];
47  uint8_t ch;
48  };
49 
54  class ESPNOW : public ILocalLayer<LocalMessageT>
55  {
56  public:
59 
60  protected:
66  {
68  int rssi = SIGNAL_MIN;
69  uint8_t ch = 0;
70 
76 
83  : addr{brRTC.addr}, ch{brRTC.ch} {};
84 
91  bool empty() { return addr == LocalAddrT{}; }
92 
99  };
100 
101  std::mutex m_mutex;
102  const Config m_conf;
107  std::mutex m_bestBridgeMutex;
108 
118  std::array<std::promise<bool>, MAX_PEER_NUM> m_sendingPromises;
119 
128  std::array<std::mutex, MAX_PEER_NUM> m_sendingMutexes;
129 
130  public:
140  ESPNOW(IAdapter& adapter, WiFi::IESPNOW& wifi, const Config& conf);
141 
146  ~ESPNOW();
147 
159  bool send(const LocalMessageT& msg);
160 
182  bool connectToBridge(BridgeConnInfoRTC* rtndBr = nullptr,
183  BridgeConnInfoRTC* connBr = nullptr);
184 
185  protected:
194  void receive(const LocalMessageT& msg, int rssi);
195 
206  void sendRaw(const LocalAddrT& dst, const std::string& data);
207 
215  void recvCb(const LocalAddrT src, std::string data, int rssi);
216 
223  void sendCb(const LocalAddrT dst, bool delivered);
224 
233  uint8_t getBucketIdFromLocalAddr(const LocalAddrT& addr) const;
234  };
235 } // namespace SPSP::LocalLayers::ESPNOW
wifi_espnow_if.hpp
WiFi interface for ESP-NOW.
espnow_packet.hpp
ESP-NOW packet structures.
espnow_ser_des.hpp
ESP-NOW packet serializer and deserializer.
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal::BridgeConnInfoInternal
BridgeConnInfoInternal(BridgeConnInfoRTC brRTC)
Construct a new object from retained RTC version.
Definition: espnow.hpp:82
SPSP::LocalLayers::ESPNOW::ESPNOW::m_bestBridge
BridgeConnInfoInternal m_bestBridge
Bridge with best signal.
Definition: espnow.hpp:106
SPSP::LocalLayers::ESPNOW::ESPNOW::sendCb
void sendCb(const LocalAddrT dst, bool delivered)
Send callback for underlaying ESP-NOW adapter.
SPSP::LocalLayers::ESPNOW::ESPNOW::m_mutex
std::mutex m_mutex
Mutex to prevent race conditions.
Definition: espnow.hpp:101
SPSP::LocalLayers::ESPNOW::ESPNOW::m_conf
const Config m_conf
Configuration.
Definition: espnow.hpp:102
SPSP::LocalLayers::ESPNOW::ESPNOW::m_serdes
SerDes m_serdes
Packet serializer/deserializer.
Definition: espnow.hpp:105
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal::rssi
int rssi
Signal RSSI.
Definition: espnow.hpp:68
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal::toRTC
BridgeConnInfoRTC toRTC()
Convert internal info to RTC version.
SPSP::LocalLayers::ESPNOW::ESPNOW::~ESPNOW
~ESPNOW()
Destroys ESP-NOW layer object.
layers.hpp
Local and far layers for SPSP.
SPSP::LocalLayers::ESPNOW::ESPNOW::ESPNOW
ESPNOW(IAdapter &adapter, WiFi::IESPNOW &wifi, const Config &conf)
Constructs a new ESP-NOW layer object.
SPSP::LocalLayers::ESPNOW::ESPNOW::getBucketIdFromLocalAddr
uint8_t getBucketIdFromLocalAddr(const LocalAddrT &addr) const
Calculates bucket id from LocalAddrT object.
SPSP::LocalLayers::ESPNOW::ESPNOW::connectToBridge
bool connectToBridge(BridgeConnInfoRTC *rtndBr=nullptr, BridgeConnInfoRTC *connBr=nullptr)
Connects to the bridge.
SPSP::LocalLayers::ESPNOW::ESPNOW::m_sendingMutexes
std::array< std::mutex, MAX_PEER_NUM > m_sendingMutexes
Container for mutex of being-sent messages.
Definition: espnow.hpp:128
SPSP::WiFi::IESPNOW
Requirements of ESP-NOW from WiFi instance.
Definition: wifi_espnow_if.hpp:23
SPSP::LocalLayers::ESPNOW::ESPNOW::send
bool send(const LocalMessageT &msg)
Sends the message to given node.
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal::ch
uint8_t ch
Wireless channel.
Definition: espnow.hpp:69
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal
Internal bridge connection info.
Definition: espnow.hpp:65
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal::empty
bool empty()
Whether bridge info doesn't contain any meaningful bridge.
Definition: espnow.hpp:91
SPSP::LocalLayers::ESPNOW::ESPNOW::m_wifi
WiFi::IESPNOW & m_wifi
WiFi instance.
Definition: espnow.hpp:103
SPSP::LocalLayers::ESPNOW::ESPNOW::recvCb
void recvCb(const LocalAddrT src, std::string data, int rssi)
Receive callback for underlaying ESP-NOW adapter.
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal::addr
LocalAddrT addr
Address.
Definition: espnow.hpp:67
SPSP::LocalLayers::ESPNOW::SerDes
Serializer and deserializer of ESP-NOW packets.
Definition: espnow_ser_des.hpp:23
SPSP::LocalLayers::ESPNOW::ESPNOW::receive
void receive(const LocalMessageT &msg, int rssi)
Receive message handler.
SPSP::LocalLayers::ESPNOW::Config
ESP-NOW configuration.
Definition: espnow_types.hpp:26
SPSP::LocalLayers::ESPNOW::BridgeConnInfoRTC::addr
uint8_t addr[6]
Address.
Definition: espnow.hpp:46
SPSP::LocalLayers::ESPNOW::BridgeConnInfoRTC
RTC memory enabled bridge connection info.
Definition: espnow.hpp:44
SPSP::LocalLayers::ESPNOW::ESPNOW::sendRaw
void sendRaw(const LocalAddrT &dst, const std::string &data)
Sends raw packet to the underlaying library.
SPSP::LocalLayers::ESPNOW::IAdapter
Interface for platform-dependent ESP-NOW adapter.
Definition: espnow_adapter_if.hpp:38
espnow_types.hpp
Types for ESPNOW classes.
SPSP::LocalLayers::ESPNOW::ESPNOW
ESP-NOW local layer.
Definition: espnow.hpp:54
SPSP::LocalAddrMAC
Local layer address container for MAC address.
Definition: local_addr_mac.hpp:24
SPSP::LocalLayers::ESPNOW::BridgeConnInfoRTC::ch
uint8_t ch
Wireless channel.
Definition: espnow.hpp:47
SPSP::LocalMessage
Local message representation.
Definition: local_message.hpp:70
SPSP::LocalLayers::ESPNOW::ESPNOW::m_sendingPromises
std::array< std::promise< bool >, MAX_PEER_NUM > m_sendingPromises
Container for promises of being-sent messages.
Definition: espnow.hpp:118
SPSP::ILocalLayer
Interface for local layer.
Definition: layers.hpp:26
SPSP::LocalLayers::ESPNOW::ESPNOW::m_adapter
IAdapter & m_adapter
Low level ESP-NOW adapter.
Definition: espnow.hpp:104
SPSP::LocalLayers::ESPNOW::ESPNOW::BridgeConnInfoInternal::BridgeConnInfoInternal
BridgeConnInfoInternal()
Construct a new empty object.
Definition: espnow.hpp:75
local_addr_mac.hpp
Local layer address container for MAC address.
SPSP::LocalLayers::ESPNOW::ESPNOW::m_bestBridgeMutex
std::mutex m_bestBridgeMutex
Mutex for modifying m_bestBridge* attributes.
Definition: espnow.hpp:107