127 lines
4.0 KiB
C++
127 lines
4.0 KiB
C++
/*
|
|
* ESP-NOW Link Module Implementation (Main CTRL, transmit only)
|
|
*/
|
|
|
|
#include "espnow_link.h"
|
|
#include "protocol/automotive_data.h"
|
|
#include <WiFi.h>
|
|
#include <esp_wifi.h>
|
|
#include <esp_now.h>
|
|
#include "esp_log.h"
|
|
|
|
namespace EspNowLink {
|
|
|
|
static const uint8_t BROADCAST_MAC[6] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
|
|
|
|
static bool g_ready = false;
|
|
static uint32_t g_sent = 0;
|
|
static uint32_t g_fail = 0;
|
|
static receive_handler_t g_rx_handler = NULL;
|
|
|
|
// IDF 5.x receive callback: sender MAC is in the info struct.
|
|
static void onReceive(const esp_now_recv_info_t* info, const uint8_t* data, int len) {
|
|
if (g_rx_handler == NULL || info == NULL || len <= 0) return;
|
|
g_rx_handler(info->src_addr, data, (size_t)len);
|
|
}
|
|
|
|
// IDF 5.5 (pioarduino / Arduino 3.3) passes wifi_tx_info_t*; older cores passed the MAC.
|
|
static void onSent(const wifi_tx_info_t* info, esp_now_send_status_t status) {
|
|
(void)info;
|
|
// Broadcast frames are never ACKed, so "success" only means the radio
|
|
// transmitted. Failures here mean the radio itself refused the frame.
|
|
if (status != ESP_NOW_SEND_SUCCESS) g_fail++;
|
|
}
|
|
|
|
bool init() {
|
|
if (g_ready) return true;
|
|
|
|
// Radio must be up for ESP-NOW. Keep STA regardless of the WiFi toggle.
|
|
if (WiFi.getMode() == WIFI_OFF) {
|
|
WiFi.mode(WIFI_STA);
|
|
}
|
|
WiFi.setSleep(false); // modem sleep adds latency/loss to ESP-NOW
|
|
|
|
// Fixed channel while not associated. If the WiFi manager later joins an
|
|
// AP the channel follows the AP; B's receiver hops to find us.
|
|
if (WiFi.status() != WL_CONNECTED) {
|
|
esp_wifi_set_channel(ESPNOW_CHANNEL, WIFI_SECOND_CHAN_NONE);
|
|
}
|
|
|
|
if (esp_now_init() != ESP_OK) {
|
|
ESP_LOGE("EspNow", "esp_now_init failed");
|
|
return false;
|
|
}
|
|
esp_now_register_send_cb(onSent);
|
|
esp_now_register_recv_cb(onReceive);
|
|
|
|
esp_now_peer_info_t peer = {};
|
|
memcpy(peer.peer_addr, BROADCAST_MAC, 6);
|
|
peer.channel = 0; // current channel
|
|
peer.encrypt = false;
|
|
peer.ifidx = WIFI_IF_STA;
|
|
if (esp_now_add_peer(&peer) != ESP_OK) {
|
|
ESP_LOGE("EspNow", "esp_now_add_peer(broadcast) failed");
|
|
return false;
|
|
}
|
|
|
|
g_ready = true;
|
|
ESP_LOGI("EspNow", "ESP-NOW ready: STA %s, channel %d, broadcast peer added",
|
|
WiFi.macAddress().c_str(), WiFi.channel());
|
|
return true;
|
|
}
|
|
|
|
// Anything that restarts the Wi-Fi driver drops ESP-NOW with it. Re-init at
|
|
// most once a second instead of failing every frame from then on.
|
|
static void reinitIfDue() {
|
|
static uint32_t last = 0;
|
|
if (millis() - last < 1000) return;
|
|
last = millis();
|
|
g_ready = false;
|
|
ESP_LOGW("EspNow", "ESP-NOW not initialised - re-initialising");
|
|
init();
|
|
}
|
|
|
|
bool send(const uint8_t* frame, size_t len) {
|
|
if (len == 0 || len > ESP_NOW_MAX_DATA_LEN) return false;
|
|
if (!g_ready) {
|
|
reinitIfDue();
|
|
if (!g_ready) return false;
|
|
}
|
|
esp_err_t err = esp_now_send(BROADCAST_MAC, frame, len);
|
|
if (err == ESP_OK) {
|
|
g_sent++;
|
|
return true;
|
|
}
|
|
g_fail++;
|
|
if (err == ESP_ERR_ESPNOW_NOT_INIT) reinitIfDue();
|
|
return false;
|
|
}
|
|
|
|
bool isReady() { return g_ready; }
|
|
uint32_t sentCount() { return g_sent; }
|
|
uint32_t failCount() { return g_fail; }
|
|
|
|
void setReceiveHandler(receive_handler_t handler) { g_rx_handler = handler; }
|
|
|
|
bool sendTo(const uint8_t mac[6], const uint8_t* frame, size_t len) {
|
|
if (len == 0 || len > ESP_NOW_MAX_DATA_LEN) return false;
|
|
if (!g_ready) {
|
|
reinitIfDue();
|
|
if (!g_ready) return false;
|
|
}
|
|
if (!esp_now_is_peer_exist(mac)) {
|
|
esp_now_peer_info_t peer = {};
|
|
memcpy(peer.peer_addr, mac, 6);
|
|
peer.channel = 0; // current channel
|
|
peer.encrypt = false; // SECURE frames carry their own encryption
|
|
peer.ifidx = WIFI_IF_STA;
|
|
if (esp_now_add_peer(&peer) != ESP_OK) {
|
|
ESP_LOGE("EspNow", "esp_now_add_peer(B) failed");
|
|
return false;
|
|
}
|
|
}
|
|
return esp_now_send(mac, frame, len) == ESP_OK;
|
|
}
|
|
|
|
} // namespace EspNowLink
|