EnigmaIOT  0.9.3
Secure sensor and gateway platform based on ESP8266 and ESP32
espnow_hal.cpp
Go to the documentation of this file.
1 
9 #include "espnow_hal.h"
10 extern "C" {
11 #ifdef ESP8266
12 #include <espnow.h>
13 #elif defined ESP32
14 #include <esp_now.h>
15 #include <esp_wifi.h>
16 #endif
17 }
18 
19 
21 
23 
25  if (esp_now_init () != 0) {
26  ESP.restart ();
27  delay (1);
28  }
29  if (peerType == COMM_NODE) {
30 #ifdef ESP8266
31  esp_now_set_self_role (ESP_NOW_ROLE_CONTROLLER);
32  esp_now_add_peer (gateway, ESP_NOW_ROLE_SLAVE, channel, NULL, 0);
33 #elif defined ESP32
34  esp_now_peer_info_t networkGw;
35  memcpy (networkGw.peer_addr, gateway, COMMS_HAL_ADDR_LEN);
36  networkGw.channel = channel;
37  networkGw.ifidx = ESP_IF_WIFI_STA;
38  networkGw.encrypt = false;
39  esp_err_t result = esp_now_add_peer (&networkGw);
40  DEBUG_INFO ("Gateway peer Added in channel %d. Result = %s", channel, esp_err_to_name(result));
41  DEBUG_DBG ("WIFI channel is %d", WiFi.channel ());
42 #endif
43  }
44 #ifdef ESP8266
45  else {
46  esp_now_set_self_role (ESP_NOW_ROLE_SLAVE);
47  }
48 #endif
49 
50  esp_now_register_recv_cb (reinterpret_cast<esp_now_recv_cb_t>(rx_cb));
51  esp_now_register_send_cb (reinterpret_cast<esp_now_send_cb_t>(tx_cb));
52 
53 }
54 
55 void ICACHE_FLASH_ATTR Espnow_halClass::rx_cb (uint8_t* mac_addr, uint8_t* data, uint8_t len) {
56  if (Espnow_hal.dataRcvd) {
57  Espnow_hal.dataRcvd (mac_addr, data, len);
58  }
59 }
60 
61 void ICACHE_FLASH_ATTR Espnow_halClass::tx_cb (uint8_t* mac_addr, uint8_t status) {
62  if (Espnow_hal.sentResult) {
63  Espnow_hal.sentResult (mac_addr, status);
64  }
65 }
66 
67 void Espnow_halClass::begin (uint8_t* gateway, uint8_t channel, peerType_t peerType) {
68  _ownPeerType = peerType;
69  _peerType = peerType;
70  DEBUG_INFO ("Starting ESP-NOW as %s", _peerType == COMM_GATEWAY ? "gateway" : "node");
71  if (peerType == COMM_NODE) {
72  DEBUG_DBG ("Gateway address is " MACSTR, MAC2STR (gateway));
73  memcpy (this->gateway, gateway, COMMS_HAL_ADDR_LEN);
74  this->channel = channel;
75  }
76  initComms (peerType);
77 }
78 
80  DEBUG_INFO ("-------------> ESP-NOW STOP");
81  esp_now_unregister_recv_cb ();
82  esp_now_unregister_send_cb ();
83  esp_now_deinit ();
84 }
85 
86 int32_t Espnow_halClass::send (uint8_t* da, uint8_t* data, int len) {
87 #ifdef ESP32
88  char buffer[18];
89  mac2str (da, buffer);
90  DEBUG_DBG ("ESP-NOW message to %s", buffer);
91  if (_ownPeerType == COMM_GATEWAY) {
92  esp_now_peer_info_t peer;
93  memcpy (peer.peer_addr, da, COMMS_HAL_ADDR_LEN);
94  uint8_t ch;
95  wifi_second_chan_t secondCh;
96  esp_wifi_get_channel (&ch, &secondCh);
97  peer.channel = ch;
98  peer.ifidx = ESP_IF_WIFI_AP;
99  peer.encrypt = false;
100  esp_err_t error = esp_now_add_peer (&peer);
101  DEBUG_DBG ("Peer added on channel %u. Result %d", ch, error);
102 
103  //wifi_bandwidth_t bw;
104  //esp_wifi_get_bandwidth (ESP_IF_WIFI_AP, &bw);
105  //ESP_LOGD (TAG, "WiFi bandwidth: %s", bw == WIFI_BW_HT20 ? "20 MHz" : "40 MHz");
106  }
107 #endif
108 
109  // Serial.printf ("Phy Mode ---> %d\n", (int)wifi_get_phy_mode ());
110  int32_t error = esp_now_send (da, data, len);
111 #ifdef ESP32
112  DEBUG_DBG ("esp now send result = %d", error);
113  if (_ownPeerType == COMM_GATEWAY) {
114  esp_err_t error = esp_now_del_peer (da);
115  DEBUG_DBG ("Peer deleted. Result %d", error);
116 
117  }
118 #endif
119  return error;
120 }
121 
123  this->dataRcvd = dataRcvd;
124 }
125 
127  this->sentResult = sentResult;
128 }
peerType_t
peerType_t
Peer role on communication.
Definition: Comms_hal.h:22
Espnow_halClass::onDataRcvd
void onDataRcvd(comms_hal_rcvd_data dataRcvd)
Attach a callback function to be run on every received message.
Definition: espnow_hal.cpp:122
Espnow_halClass::send
int32_t send(uint8_t *da, uint8_t *data, int len)
Sends data to the other peer.
Definition: espnow_hal.cpp:86
Espnow_halClass::dataRcvd
comms_hal_rcvd_data dataRcvd
Pointer to a function to be called on every received message.
Definition: espnow_hal.h:41
Espnow_halClass::gateway
uint8_t gateway[COMMS_HAL_ADDR_LEN]
Gateway address.
Definition: espnow_hal.h:38
Espnow_halClass
Definition for ESP-NOW hardware abstraction layer.
Definition: espnow_hal.h:31
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:61
Espnow_halClass::onDataSent
void onDataSent(comms_hal_sent_data dataRcvd)
Attach a callback function to be run after sending a message to receive its status.
Definition: espnow_hal.cpp:126
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:55
Espnow_halClass::stop
void stop()
Terminates communication and closes all connectrions.
Definition: espnow_hal.cpp:79
Espnow_halClass::channel
uint8_t channel
WiFi channel to be used.
Definition: espnow_hal.h:39
mac2str
char * mac2str(const uint8_t *mac, char *buffer)
Debug helper function that generates a string that represent a MAC address.
Definition: helperFunctions.cpp:84
COMM_NODE
@ COMM_NODE
Definition: Comms_hal.h:23
comms_hal_rcvd_data
void(* comms_hal_rcvd_data)(uint8_t *address, uint8_t *data, uint8_t len)
Definition: Comms_hal.h:27
MACSTR
#define MACSTR
Definition: helperFunctions.cpp:82
Espnow_halClass::begin
void begin(uint8_t *gateway, uint8_t channel=0, peerType_t peerType=COMM_NODE)
Setup communication environment and establish the connection from node to gateway.
Definition: espnow_hal.cpp:67
COMM_GATEWAY
@ COMM_GATEWAY
Definition: Comms_hal.h:24
data
@ data
Definition: GwOutput_generic.h:23
Comms_halClass::_ownPeerType
peerType_t _ownPeerType
Stores peer type, node or gateway.
Definition: Comms_hal.h:44
Espnow_hal
Espnow_halClass Espnow_hal
Singleton instance of ESP-NOW class.
Definition: espnow_hal.cpp:20
Espnow_halClass::initComms
void initComms(peerType_t peerType)
Communication subsistem initialization.
Definition: espnow_hal.cpp:24
_peerType
peerType_t _peerType
Definition: espnow_hal.cpp:22
Espnow_halClass::sentResult
comms_hal_sent_data sentResult
Pointer to a function to be called to notify last sending status.
Definition: espnow_hal.h:42
comms_hal_sent_data
void(* comms_hal_sent_data)(uint8_t *address, uint8_t status)
Definition: Comms_hal.h:28
espnow_hal.h
ESP-NOW communication system abstraction layer. To be used on ESP8266 or ESP32 platforms.
status
@ status
Definition: GwOutput_generic.h:25
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