SPSP
Simple publish-subscribe protocol. Connects low power IoT clients to MQTT.
All Classes Files Functions Variables Typedefs Enumerations
Classes | Public Types | Public Member Functions | Protected Member Functions | Protected Attributes | List of all members
SPSP::LocalLayers::ESPNOW::ESPNOW Class Reference

ESP-NOW local layer. More...

#include <espnow.hpp>

Inheritance diagram for SPSP::LocalLayers::ESPNOW::ESPNOW:
Inheritance graph
[legend]
Collaboration diagram for SPSP::LocalLayers::ESPNOW::ESPNOW:
Collaboration graph
[legend]

Classes

struct  BridgeConnInfoInternal
 Internal bridge connection info. More...
 

Public Types

using LocalAddrT = SPSP::LocalLayers::ESPNOW::LocalAddrT
 
using LocalMessageT = SPSP::LocalLayers::ESPNOW::LocalMessageT
 
- Public Types inherited from SPSP::ILocalLayer< LocalMessageT >
using LocalAddrT = typename LocalMessageT ::LocalAddrT
 
using LocalMessageT = LocalMessageT
 

Public Member Functions

 ESPNOW (IAdapter &adapter, WiFi::IESPNOW &wifi, const Config &conf)
 Constructs a new ESP-NOW layer object. More...
 
 ~ESPNOW ()
 Destroys ESP-NOW layer object. More...
 
bool send (const LocalMessageT &msg)
 Sends the message to given node. More...
 
bool connectToBridge (BridgeConnInfoRTC *rtndBr=nullptr, BridgeConnInfoRTC *connBr=nullptr)
 Connects to the bridge. More...
 
- Public Member Functions inherited from SPSP::ILocalLayer< LocalMessageT >
void setNode (void *n)
 Sets pointer to the owner node. More...
 
ILocalNode< ILocalLayer > * getNode () const
 Gets the node object. More...
 
bool nodeConnected () const
 Checks whether the owner node is connected. More...
 
virtual bool send (const LocalMessageT &msg)=0
 Sends the message to given node. More...
 

Protected Member Functions

void receive (const LocalMessageT &msg, int rssi)
 Receive message handler. More...
 
void sendRaw (const LocalAddrT &dst, const std::string &data)
 Sends raw packet to the underlaying library. More...
 
void recvCb (const LocalAddrT src, std::string data, int rssi)
 Receive callback for underlaying ESP-NOW adapter. More...
 
void sendCb (const LocalAddrT dst, bool delivered)
 Send callback for underlaying ESP-NOW adapter. More...
 
uint8_t getBucketIdFromLocalAddr (const LocalAddrT &addr) const
 Calculates bucket id from LocalAddrT object. More...
 

Protected Attributes

std::mutex m_mutex
 Mutex to prevent race conditions.
 
const Config m_conf
 Configuration.
 
WiFi::IESPNOWm_wifi
 WiFi instance.
 
IAdapterm_adapter
 Low level ESP-NOW adapter.
 
SerDes m_serdes
 Packet serializer/deserializer.
 
BridgeConnInfoInternal m_bestBridge = {}
 Bridge with best signal.
 
std::mutex m_bestBridgeMutex
 Mutex for modifying m_bestBridge* attributes.
 
std::array< std::promise< bool >, MAX_PEER_NUM > m_sendingPromises
 Container for promises of being-sent messages. More...
 
std::array< std::mutex, MAX_PEER_NUM > m_sendingMutexes
 Container for mutex of being-sent messages. More...
 

Detailed Description

ESP-NOW local layer.

Definition at line 54 of file espnow.hpp.

Constructor & Destructor Documentation

◆ ESPNOW()

SPSP::LocalLayers::ESPNOW::ESPNOW::ESPNOW ( IAdapter adapter,
WiFi::IESPNOW wifi,
const Config conf 
)

Constructs a new ESP-NOW layer object.

Requires already initialized WiFi.

Parameters
adapterESP-NOW low-level adapter
wifiWiFi instance
confConfiguration

◆ ~ESPNOW()

SPSP::LocalLayers::ESPNOW::ESPNOW::~ESPNOW ( )

Destroys ESP-NOW layer object.

Member Function Documentation

◆ connectToBridge()

bool SPSP::LocalLayers::ESPNOW::ESPNOW::connectToBridge ( BridgeConnInfoRTC rtndBr = nullptr,
BridgeConnInfoRTC connBr = nullptr 
)

Connects to the bridge.

If rtndBr is not nullptr, uses it's address, returns true immediately, does no scan whatsoever and connBr = rtndBr. Otherwise probes all wireless channels (available in the currently configured country) and selects bridge with the best signal.

You can use retainedBridge and connectedBridge parameters to implement custom logic around deep-sleep reconnection without scans.

rtndBr and connBr may be the same pointers.

Automatically resubscribes to all topics.

Parameters
rtndBrRetained bridge peer info (for reconnection)
connBrConnected bridge peer info storage (if connection successful and connBr != nullptr)
Returns
true Connection successful
false Connection failed

◆ getBucketIdFromLocalAddr()

uint8_t SPSP::LocalLayers::ESPNOW::ESPNOW::getBucketIdFromLocalAddr ( const LocalAddrT addr) const
protected

Calculates bucket id from LocalAddrT object.

Used for m_sendingPromises and m_sendingMutexes arrays.

Parameters
addrAddress of the peer
Returns
Bucket id

◆ receive()

void SPSP::LocalLayers::ESPNOW::ESPNOW::receive ( const LocalMessageT msg,
int  rssi 
)
protected

Receive message handler.

Separate from recvCb to allow simpler testing.

Parameters
msgMessage
rssiReceived signal strength indicator (in dBm)

◆ recvCb()

void SPSP::LocalLayers::ESPNOW::ESPNOW::recvCb ( const LocalAddrT  src,
std::string  data,
int  rssi 
)
protected

Receive callback for underlaying ESP-NOW adapter.

Parameters
srcSource address
dataRaw data
rssiReceived signal strength indicator (in dBm)

◆ send()

bool SPSP::LocalLayers::ESPNOW::ESPNOW::send ( const LocalMessageT msg)

Sends the message to given node.

In the message, empty address means send to the bridge peer.

Note: this will block until delivery status is confirmed!

Parameters
msgMessage
Returns
true Delivery successful
false Delivery failed

◆ sendCb()

void SPSP::LocalLayers::ESPNOW::ESPNOW::sendCb ( const LocalAddrT  dst,
bool  delivered 
)
protected

Send callback for underlaying ESP-NOW adapter.

Parameters
dstDestination address
deliveredWhether packet has been delivered successfully

◆ sendRaw()

void SPSP::LocalLayers::ESPNOW::ESPNOW::sendRaw ( const LocalAddrT dst,
const std::string &  data 
)
protected

Sends raw packet to the underlaying library.

Also registers and unregisters the peer temporarily.

This is not multi-thread safe.

Parameters
dstDestination address
dataRaw data

Member Data Documentation

◆ m_sendingMutexes

std::array<std::mutex, MAX_PEER_NUM> SPSP::LocalLayers::ESPNOW::ESPNOW::m_sendingMutexes
protected

Container for mutex of being-sent messages.

We need only one thread waiting for message status per bucket in m_sendingPromises. Efectively allows maximum of MAX_PEER_NUM messages being sent at the same time.

Definition at line 128 of file espnow.hpp.

◆ m_sendingPromises

std::array<std::promise<bool>, MAX_PEER_NUM> SPSP::LocalLayers::ESPNOW::ESPNOW::m_sendingPromises
protected

Container for promises of being-sent messages.

send() blocks until status of delivery is available (and no other message is being sent to the same node). Assign each peer a "bucket". When sending a message to other peer from the same bucket, wait for it to finish. Additional support for this mechanism is provided by m_sendingMutexes.

Definition at line 118 of file espnow.hpp.


The documentation for this class was generated from the following file: