Split ESP-NOW into core/master/slave modules, block non-OTA UART traffic during updates, and hold the host serial port exclusively so dashboard polling cannot interleave with firmware uploads. Co-authored-by: Cursor <cursoragent@cursor.com>
52 lines
1.4 KiB
C
52 lines
1.4 KiB
C
#include "esp_now_comm.h"
|
|
#include "client_registry.h"
|
|
#include "esp_now_core.h"
|
|
#include "esp_now_master.h"
|
|
#include "esp_now_slave.h"
|
|
#include "esp_err.h"
|
|
#include "esp_log.h"
|
|
#include "esp_now.h"
|
|
#include <stdio.h>
|
|
#include <string.h>
|
|
|
|
static const char *TAG = "[ESPNOW]";
|
|
|
|
static void espnow_recv_cb(const esp_now_recv_info_t *info, const uint8_t *data,
|
|
int len) {
|
|
if (esp_now_core_is_master()) {
|
|
esp_now_master_on_recv(info, data, len);
|
|
} else {
|
|
esp_now_slave_on_recv(info, data, len);
|
|
}
|
|
}
|
|
|
|
esp_err_t esp_now_comm_init(const app_config_t *config) {
|
|
if (config == NULL) {
|
|
return ESP_ERR_INVALID_ARG;
|
|
}
|
|
|
|
esp_now_core_store_config(config);
|
|
client_registry_init();
|
|
|
|
esp_err_t err = esp_now_core_init_radio(esp_now_core_wifi_channel());
|
|
if (err != ESP_OK) {
|
|
ESP_LOGE(TAG, "wifi init failed: %s", esp_err_to_name(err));
|
|
return err;
|
|
}
|
|
|
|
char mac_str[18];
|
|
esp_now_core_mac_to_str(esp_now_core_own_mac(), mac_str, sizeof(mac_str));
|
|
ESP_LOGI(TAG, "role=%s network=%u channel=%u mac=%s",
|
|
config->master ? "master" : "slave", (unsigned)config->network,
|
|
(unsigned)esp_now_core_wifi_channel(), mac_str);
|
|
|
|
ESP_ERROR_CHECK(esp_now_init());
|
|
ESP_ERROR_CHECK(esp_now_register_recv_cb(espnow_recv_cb));
|
|
esp_now_core_init_send_done();
|
|
|
|
if (config->master) {
|
|
return esp_now_master_start();
|
|
}
|
|
return esp_now_slave_start();
|
|
}
|