EnigmaIOT  0.9.8
Secure sensor and gateway platform based on ESP8266 and ESP32
espnow_hal.h
Go to the documentation of this file.
1 
9 #ifndef _ESPNOW_HAL_h
10 #define _ESPNOW_HAL_h
11 
12 #if defined(ARDUINO) && ARDUINO >= 100
13 #include "Arduino.h"
14 #else
15 #include "WProgram.h"
16 #endif
17 
18 #if defined(ESP8266)
19 #include <ESP8266WiFi.h>
20 #elif defined(ESP32)
21 #include <WiFi.h>
22 #include <esp_now.h>
23 #endif
24 #include "Comms_hal.h"
25 #include "helperFunctions.h"
26 #include "EnigmaIOTRingBuffer.h"
27 
32 public:
33  static const size_t COMMS_HAL_MAX_MESSAGE_LENGTH = 250;
34  static const uint8_t COMMS_HAL_ADDR_LEN = 6;
35 
36 protected:
37 
39  bool readyToSend = true;
40 #ifdef ESP32
41  TaskHandle_t espnowLoopTask;
42 #else // ESP8266
43  ETSTimer espnowLoopTask;
44 #endif
45 
50  void initComms (peerType_t peerType) override;
51 
56  bool addPeer (const uint8_t* da);
57 
64  static void ICACHE_FLASH_ATTR rx_cb (uint8_t* mac_addr, uint8_t* data, uint8_t len);
65 
71  static void ICACHE_FLASH_ATTR tx_cb (uint8_t* mac_addr, uint8_t status);
72 
73  int32_t sendEspNowMessage (comms_queue_item_t* message);
75  void popCommsQueue ();
76 
77 public:
83 
84 
91  void begin (uint8_t* gateway, uint8_t channel = 0, peerType_t peerType = COMM_NODE) override;
92 
96  void stop () override;
97 
105  int32_t send (uint8_t* da, uint8_t* data, int len) override;
106 
111  void onDataRcvd (comms_hal_rcvd_data dataRcvd) override;
112 
117  void onDataSent (comms_hal_sent_data dataRcvd) override;
118 
123  uint8_t getAddressLength () override {
124  return COMMS_HAL_ADDR_LEN;
125  }
126 
133  }
134 
139  void enableTransmit (bool enable) override {
140  DEBUG_DBG ("Send esp-now task %s", enable ? "enabled" : "disabled");
141  if (enable) {
142 #ifdef ESP8266
143  os_timer_arm (&espnowLoopTask, 20, true);
144  //timer1_enable (TIM_DIV16, TIM_EDGE, TIM_LOOP);
145 #else
146  if (espnowLoopTask) {
147  vTaskResume (espnowLoopTask);
148  }
149 #endif
150  } else {
151 #ifdef ESP8266
152  os_timer_disarm (&espnowLoopTask);
153  //timer1_disable ();
154 #else
155  if (espnowLoopTask) {
156  vTaskSuspend (espnowLoopTask);
157  }
158 #endif
159  }
160  }
161 
165  void handle () override;
166 
170  static void runHandle (void* param);
171 
172 };
173 
175 
176 #endif
177 
Espnow_halClass::out_queue
EnigmaIOTRingBuffer< comms_queue_item_t > out_queue
Definition: espnow_hal.h:38
peerType_t
peerType_t
Peer role on communication.
Definition: Comms_hal.h:23
EnigmaIOTRingBuffer.h
Library to build a gateway for EnigmaIoT system.
comms_queue_item_t
Definition: Comms_hal.h:28
Espnow_halClass::COMMS_HAL_MAX_MESSAGE_LENGTH
static const size_t COMMS_HAL_MAX_MESSAGE_LENGTH
Maximum message length for ESP-NOW.
Definition: espnow_hal.h:33
EnigmaIOTRingBuffer< comms_queue_item_t >
Espnow_halClass::addPeer
bool addPeer(const uint8_t *da)
Adds a peer to esp-now peer list.
Definition: espnow_hal.cpp:93
Espnow_halClass
Definition for ESP-NOW hardware abstraction layer.
Definition: espnow_hal.h:31
Espnow_halClass::readyToSend
bool readyToSend
Definition: espnow_hal.h:39
Espnow_halClass::handle
void handle() override
Sends next message in the queue.
Definition: espnow_hal.cpp:212
Espnow_halClass::begin
void begin(uint8_t *gateway, uint8_t channel=0, peerType_t peerType=COMM_NODE) override
Setup communication environment and establish the connection from node to gateway.
Definition: espnow_hal.cpp:78
Espnow_halClass::enableTransmit
void enableTransmit(bool enable) override
Enables or disables transmission of queued messages. Used to disable communication during wifi scan.
Definition: espnow_hal.h:139
Espnow_halClass::tx_cb
static void ICACHE_FLASH_ATTR tx_cb(uint8_t *mac_addr, uint8_t status)
Function that gets sending status.
Definition: espnow_hal.cpp:70
Espnow_halClass::rx_cb
static void ICACHE_FLASH_ATTR rx_cb(uint8_t *mac_addr, uint8_t *data, uint8_t len)
Function that processes incoming messages and passes them to upper layer.
Definition: espnow_hal.cpp:64
Espnow_halClass::getCommsQueue
comms_queue_item_t * getCommsQueue()
Definition: espnow_hal.cpp:152
Espnow_halClass::send
int32_t send(uint8_t *da, uint8_t *data, int len) override
Sends data to the other peer.
Definition: espnow_hal.cpp:122
Espnow_halClass::espnowLoopTask
ETSTimer espnowLoopTask
Definition: espnow_hal.h:43
COMM_NODE
@ COMM_NODE
Definition: Comms_hal.h:24
COMMS_QUEUE_SIZE
static const uint8_t COMMS_QUEUE_SIZE
Definition: EnigmaIoTconfigAdvanced.h:28
Espnow_halClass::initComms
void initComms(peerType_t peerType) override
Communication subsistem initialization.
Definition: espnow_hal.cpp:24
Espnow_halClass::onDataSent
void onDataSent(comms_hal_sent_data dataRcvd) override
Attach a callback function to be run after sending a message to receive its status.
Definition: espnow_hal.cpp:208
comms_hal_rcvd_data
void(* comms_hal_rcvd_data)(uint8_t *address, uint8_t *data, uint8_t len)
Definition: Comms_hal.h:35
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
Espnow_halClass::onDataRcvd
void onDataRcvd(comms_hal_rcvd_data dataRcvd) override
Attach a callback function to be run on every received message.
Definition: espnow_hal.cpp:204
Comms_halClass::dataRcvd
comms_hal_rcvd_data dataRcvd
Pointer to a function to be called on every received message.
Definition: Comms_hal.h:50
data
@ data
Definition: GwOutput_generic.h:23
Espnow_halClass::Espnow_halClass
Espnow_halClass()
Class constructor.
Definition: espnow_hal.h:81
Espnow_halClass::sendEspNowMessage
int32_t sendEspNowMessage(comms_queue_item_t *message)
Definition: espnow_hal.cpp:173
helperFunctions.h
Auxiliary function definition.
Espnow_halClass::getAddressLength
uint8_t getAddressLength() override
Get address length used on ESP-NOW subsystem.
Definition: espnow_hal.h:123
Espnow_halClass::runHandle
static void runHandle(void *param)
Static function that calls handle inside task.
Definition: espnow_hal.cpp:230
Espnow_halClass::getMaxMessageLength
size_t getMaxMessageLength()
Get maximum message length on ESP-NOW subsystem.
Definition: espnow_hal.h:131
Comms_hal.h
Generic communication system abstraction layer.
comms_hal_sent_data
void(* comms_hal_sent_data)(uint8_t *address, uint8_t status)
Definition: Comms_hal.h:36
Espnow_halClass::stop
void stop() override
Terminates communication and closes all connectrions.
Definition: espnow_hal.cpp:115
Espnow_halClass::popCommsQueue
void popCommsQueue()
Definition: espnow_hal.cpp:160
Comms_halClass::gateway
uint8_t gateway[COMMS_HAL_ADDR_LEN]
Gateway address.
Definition: Comms_hal.h:47
Comms_halClass::channel
uint8_t channel
Comms channel to be used.
Definition: Comms_hal.h:48
status
@ status
Definition: GwOutput_generic.h:25
Comms_halClass
Interface for communication subsystem abstraction layer definition.
Definition: Comms_hal.h:41
Espnow_halClass::COMMS_HAL_ADDR_LEN
static const uint8_t COMMS_HAL_ADDR_LEN
Address length for ESP-NOW. Correspond to mac address.
Definition: espnow_hal.h:34