mirror of https://github.com/meshcore-dev/MeshCore
6 changed files with 512 additions and 61 deletions
@ -0,0 +1,184 @@ |
|||
#include "ESPNowBridge.h" |
|||
|
|||
#include <RTClib.h> |
|||
#include <WiFi.h> |
|||
#include <esp_wifi.h> |
|||
|
|||
#ifdef WITH_ESPNOW_BRIDGE |
|||
|
|||
// Static member to handle callbacks
|
|||
ESPNowBridge *ESPNowBridge::_instance = nullptr; |
|||
|
|||
// Static callback wrappers
|
|||
void ESPNowBridge::recv_cb(const uint8_t *mac, const uint8_t *data, int len) { |
|||
if (_instance) { |
|||
_instance->onDataRecv(mac, data, len); |
|||
} |
|||
} |
|||
|
|||
void ESPNowBridge::send_cb(const uint8_t *mac, esp_now_send_status_t status) { |
|||
if (_instance) { |
|||
_instance->onDataSent(mac, status); |
|||
} |
|||
} |
|||
|
|||
// Fletcher16 checksum calculation
|
|||
static uint16_t fletcher16(const uint8_t *data, size_t len) { |
|||
uint16_t sum1 = 0; |
|||
uint16_t sum2 = 0; |
|||
|
|||
while (len--) { |
|||
sum1 = (sum1 + *data++) % 255; |
|||
sum2 = (sum2 + sum1) % 255; |
|||
} |
|||
|
|||
return (sum2 << 8) | sum1; |
|||
} |
|||
|
|||
ESPNowBridge::ESPNowBridge(mesh::PacketManager *mgr, mesh::RTCClock *rtc) |
|||
: _mgr(mgr), _rtc(rtc), _rx_buffer_pos(0) { |
|||
_instance = this; |
|||
} |
|||
|
|||
void ESPNowBridge::begin() { |
|||
// Initialize WiFi in station mode
|
|||
WiFi.mode(WIFI_STA); |
|||
|
|||
// Initialize ESP-NOW
|
|||
if (esp_now_init() != ESP_OK) { |
|||
Serial.printf("%s: ESPNOW BRIDGE: Error initializing ESP-NOW\n", getLogDateTime()); |
|||
return; |
|||
} |
|||
|
|||
// Register callbacks
|
|||
esp_now_register_recv_cb(recv_cb); |
|||
esp_now_register_send_cb(send_cb); |
|||
|
|||
// Add broadcast peer
|
|||
esp_now_peer_info_t peerInfo = {}; |
|||
memset(&peerInfo, 0, sizeof(peerInfo)); |
|||
memset(peerInfo.peer_addr, 0xFF, ESP_NOW_ETH_ALEN); // Broadcast address
|
|||
peerInfo.channel = 0; |
|||
peerInfo.encrypt = false; |
|||
|
|||
if (esp_now_add_peer(&peerInfo) != ESP_OK) { |
|||
Serial.printf("%s: ESPNOW BRIDGE: Failed to add broadcast peer\n", getLogDateTime()); |
|||
return; |
|||
} |
|||
} |
|||
|
|||
void ESPNowBridge::loop() { |
|||
// Nothing to do here - ESP-NOW is callback based
|
|||
} |
|||
|
|||
void ESPNowBridge::xorCrypt(uint8_t *data, size_t len) { |
|||
size_t keyLen = strlen(_secret); |
|||
for (size_t i = 0; i < len; i++) { |
|||
data[i] ^= _secret[i % keyLen]; |
|||
} |
|||
} |
|||
|
|||
void ESPNowBridge::onDataRecv(const uint8_t *mac, const uint8_t *data, int len) { |
|||
// Ignore packets that are too small
|
|||
if (len < 3) { |
|||
#if MESH_PACKET_LOGGING |
|||
Serial.printf("%s: ESPNOW BRIDGE: RX packet too small, len=%d\n", getLogDateTime(), len); |
|||
#endif |
|||
return; |
|||
} |
|||
|
|||
// Check packet header magic
|
|||
if (data[0] != ESPNOW_HEADER_MAGIC) { |
|||
#if MESH_PACKET_LOGGING |
|||
Serial.printf("%s: ESPNOW BRIDGE: RX invalid magic 0x%02X\n", getLogDateTime(), data[0]); |
|||
#endif |
|||
return; |
|||
} |
|||
|
|||
// Make a copy we can decrypt
|
|||
uint8_t decrypted[MAX_ESPNOW_PACKET_SIZE]; |
|||
memcpy(decrypted, data + 1, len - 1); // Skip magic byte
|
|||
|
|||
// Try to decrypt
|
|||
xorCrypt(decrypted, len - 1); |
|||
|
|||
// Validate checksum
|
|||
uint16_t received_checksum = (decrypted[0] << 8) | decrypted[1]; |
|||
uint16_t calculated_checksum = fletcher16(decrypted + 2, len - 3); |
|||
|
|||
if (received_checksum != calculated_checksum) { |
|||
// Failed to decrypt - likely from a different network
|
|||
#if MESH_PACKET_LOGGING |
|||
Serial.printf("%s: ESPNOW BRIDGE: RX checksum mismatch, rcv=0x%04X calc=0x%04X\n", getLogDateTime(), |
|||
received_checksum, calculated_checksum); |
|||
#endif |
|||
return; |
|||
} |
|||
|
|||
#if MESH_PACKET_LOGGING |
|||
Serial.printf("%s: ESPNOW BRIDGE: RX, len=%d\n", getLogDateTime(), len - 3); |
|||
#endif |
|||
|
|||
// Create mesh packet
|
|||
mesh::Packet *pkt = _instance->_mgr->allocNew(); |
|||
if (!pkt) return; |
|||
|
|||
if (pkt->readFrom(decrypted + 2, len - 3)) { |
|||
_instance->onPacketReceived(pkt); |
|||
} else { |
|||
_instance->_mgr->free(pkt); |
|||
} |
|||
} |
|||
|
|||
void ESPNowBridge::onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { |
|||
// Could add transmission error handling here if needed
|
|||
} |
|||
|
|||
void ESPNowBridge::onPacketReceived(mesh::Packet *packet) { |
|||
if (!_seen_packets.hasSeen(packet)) { |
|||
_mgr->queueInbound(packet, 0); |
|||
} else { |
|||
_mgr->free(packet); |
|||
} |
|||
} |
|||
|
|||
void ESPNowBridge::onPacketTransmitted(mesh::Packet *packet) { |
|||
if (!_seen_packets.hasSeen(packet)) { |
|||
uint8_t buffer[MAX_ESPNOW_PACKET_SIZE]; |
|||
buffer[0] = ESPNOW_HEADER_MAGIC; |
|||
|
|||
// Write packet to buffer starting after magic byte and checksum
|
|||
uint16_t len = packet->writeTo(buffer + 3); |
|||
|
|||
// Calculate and add checksum
|
|||
uint16_t checksum = fletcher16(buffer + 3, len); |
|||
buffer[1] = (checksum >> 8) & 0xFF; |
|||
buffer[2] = checksum & 0xFF; |
|||
|
|||
// Encrypt payload (not including magic byte)
|
|||
xorCrypt(buffer + 1, len + 2); |
|||
|
|||
// Broadcast using ESP-NOW
|
|||
uint8_t broadcastAddress[] = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF }; |
|||
esp_err_t result = esp_now_send(broadcastAddress, buffer, len + 3); |
|||
|
|||
#if MESH_PACKET_LOGGING |
|||
if (result == ESP_OK) { |
|||
Serial.printf("%s: ESPNOW BRIDGE: TX, len=%d\n", getLogDateTime(), len); |
|||
} else { |
|||
Serial.printf("%s: ESPNOW BRIDGE: TX FAILED!\n", getLogDateTime()); |
|||
} |
|||
#endif |
|||
} |
|||
} |
|||
|
|||
const char *ESPNowBridge::getLogDateTime() { |
|||
static char tmp[32]; |
|||
uint32_t now = _rtc->getCurrentTime(); |
|||
DateTime dt = DateTime(now); |
|||
sprintf(tmp, "%02d:%02d:%02d - %d/%d/%d U", dt.hour(), dt.minute(), dt.second(), dt.day(), dt.month(), |
|||
dt.year()); |
|||
return tmp; |
|||
} |
|||
|
|||
#endif |
|||
@ -0,0 +1,170 @@ |
|||
#pragma once |
|||
|
|||
#include "MeshCore.h" |
|||
#include "esp_now.h" |
|||
#include "helpers/AbstractBridge.h" |
|||
#include "helpers/SimpleMeshTables.h" |
|||
|
|||
#ifdef WITH_ESPNOW_BRIDGE |
|||
|
|||
#ifndef WITH_ESPNOW_BRIDGE_SECRET |
|||
#error WITH_ESPNOW_BRIDGE_SECRET must be defined to use ESPNowBridge |
|||
#endif |
|||
|
|||
/**
|
|||
* @brief Bridge implementation using ESP-NOW protocol for packet transport |
|||
* |
|||
* This bridge enables mesh packet transport over ESP-NOW, a connectionless communication |
|||
* protocol provided by Espressif that allows ESP32 devices to communicate directly |
|||
* without WiFi router infrastructure. |
|||
* |
|||
* Features: |
|||
* - Broadcast-based communication (all bridges receive all packets) |
|||
* - Network isolation using XOR encryption with shared secret |
|||
* - Duplicate packet detection using SimpleMeshTables tracking |
|||
* - Maximum packet size of 250 bytes (ESP-NOW limitation) |
|||
* |
|||
* Packet Structure: |
|||
* [1 byte] Magic Header (0xAB) - Used to identify ESPNowBridge packets |
|||
* [2 bytes] Fletcher-16 checksum of encrypted payload (calculated over payload only) |
|||
* [n bytes] Encrypted payload containing the mesh packet |
|||
* |
|||
* The Fletcher-16 checksum is used to validate packet integrity and detect |
|||
* corrupted or tampered packets. It's calculated over the encrypted payload |
|||
* and provides a simple but effective way to verify packets are both |
|||
* uncorrupted and from the same network (since the checksum is calculated |
|||
* after encryption). |
|||
* |
|||
* Configuration: |
|||
* - Define WITH_ESPNOW_BRIDGE to enable this bridge |
|||
* - Define WITH_ESPNOW_BRIDGE_SECRET with a string to set the network encryption key |
|||
* |
|||
* Network Isolation: |
|||
* Multiple independent mesh networks can coexist by using different |
|||
* WITH_ESPNOW_BRIDGE_SECRET values. Packets encrypted with a different key will |
|||
* fail the checksum validation and be discarded. |
|||
*/ |
|||
class ESPNowBridge : public AbstractBridge { |
|||
private: |
|||
static ESPNowBridge *_instance; |
|||
static void recv_cb(const uint8_t *mac, const uint8_t *data, int len); |
|||
static void send_cb(const uint8_t *mac, esp_now_send_status_t status); |
|||
|
|||
/** Packet manager for allocating and queuing mesh packets */ |
|||
mesh::PacketManager *_mgr; |
|||
|
|||
/** RTC clock for timestamping debug messages */ |
|||
mesh::RTCClock *_rtc; |
|||
|
|||
/** Tracks seen packets to prevent loops in broadcast communications */ |
|||
SimpleMeshTables _seen_packets; |
|||
|
|||
/**
|
|||
* Maximum ESP-NOW packet size (250 bytes) |
|||
* This is a hardware limitation of ESP-NOW protocol: |
|||
* - ESP-NOW header: 20 bytes |
|||
* - Max payload: 250 bytes |
|||
* Source: ESP-NOW API documentation |
|||
*/ |
|||
static const size_t MAX_ESPNOW_PACKET_SIZE = 250; |
|||
|
|||
/**
|
|||
* Magic byte to identify ESPNowBridge packets (0xAB) |
|||
*/ |
|||
static const uint8_t ESPNOW_HEADER_MAGIC = 0xAB; |
|||
|
|||
/** Buffer for receiving ESP-NOW packets */ |
|||
uint8_t _rx_buffer[MAX_ESPNOW_PACKET_SIZE]; |
|||
|
|||
/** Current position in receive buffer */ |
|||
size_t _rx_buffer_pos; |
|||
|
|||
/**
|
|||
* Network encryption key from build define |
|||
* Must be defined with WITH_ESPNOW_BRIDGE_SECRET |
|||
* Used for XOR encryption to isolate different mesh networks |
|||
*/ |
|||
const char *_secret = WITH_ESPNOW_BRIDGE_SECRET; |
|||
|
|||
/**
|
|||
* Performs XOR encryption/decryption of data |
|||
* |
|||
* Uses WITH_ESPNOW_BRIDGE_SECRET as the key in a simple XOR operation. |
|||
* The same operation is used for both encryption and decryption. |
|||
* While not cryptographically secure, it provides basic network isolation. |
|||
* |
|||
* @param data Pointer to data to encrypt/decrypt |
|||
* @param len Length of data in bytes |
|||
*/ |
|||
void xorCrypt(uint8_t *data, size_t len); |
|||
|
|||
/**
|
|||
* ESP-NOW receive callback |
|||
* Called by ESP-NOW when a packet is received |
|||
* |
|||
* @param mac Source MAC address |
|||
* @param data Received data |
|||
* @param len Length of received data |
|||
*/ |
|||
void onDataRecv(const uint8_t *mac, const uint8_t *data, int len); |
|||
|
|||
/**
|
|||
* ESP-NOW send callback |
|||
* Called by ESP-NOW after a transmission attempt |
|||
* |
|||
* @param mac_addr Destination MAC address |
|||
* @param status Transmission status |
|||
*/ |
|||
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status); |
|||
|
|||
public: |
|||
/**
|
|||
* Constructs an ESPNowBridge instance |
|||
* |
|||
* @param mgr PacketManager for allocating and queuing packets |
|||
* @param rtc RTCClock for timestamping debug messages |
|||
*/ |
|||
ESPNowBridge(mesh::PacketManager *mgr, mesh::RTCClock *rtc); |
|||
|
|||
/**
|
|||
* Initializes the ESP-NOW bridge |
|||
* |
|||
* - Configures WiFi in station mode |
|||
* - Initializes ESP-NOW protocol |
|||
* - Registers callbacks |
|||
* - Sets up broadcast peer |
|||
*/ |
|||
void begin() override; |
|||
|
|||
/**
|
|||
* Main loop handler |
|||
* ESP-NOW is callback-based, so this is currently empty |
|||
*/ |
|||
void loop() override; |
|||
|
|||
/**
|
|||
* Called when a packet is received via ESP-NOW |
|||
* Queues the packet for mesh processing if not seen before |
|||
* |
|||
* @param packet The received mesh packet |
|||
*/ |
|||
void onPacketReceived(mesh::Packet *packet) override; |
|||
|
|||
/**
|
|||
* Called when a packet needs to be transmitted via ESP-NOW |
|||
* Encrypts and broadcasts the packet if not seen before |
|||
* |
|||
* @param packet The mesh packet to transmit |
|||
*/ |
|||
void onPacketTransmitted(mesh::Packet *packet) override; |
|||
|
|||
/**
|
|||
* Gets formatted date/time string for logging |
|||
* Format: "HH:MM:SS - DD/MM/YYYY U" |
|||
* |
|||
* @return Formatted date/time string |
|||
*/ |
|||
const char *getLogDateTime(); |
|||
}; |
|||
|
|||
#endif |
|||
Loading…
Reference in new issue