Added ECHO cmd and fixed some timing issues
This commit is contained in:
+73
-6
@@ -21,6 +21,8 @@
|
||||
|
||||
#define ESPNOW_HEARTBEAT_INTERVAL_MS 1000
|
||||
#define SLAVE_MASTER_LOST_MS (ESPNOW_HEARTBEAT_INTERVAL_MS * 5)
|
||||
/** While master or slave OTA is in progress (discover may be sparse). */
|
||||
#define SLAVE_MASTER_OTA_GRACE_MS 300000u
|
||||
#define ESPNOW_ACCEL_INTERVAL_MS 16
|
||||
#define ESPNOW_BATTERY_INTERVAL_MS 30000
|
||||
#define SLAVE_BATTERY_AFTER_JOIN_MS 150
|
||||
@@ -28,6 +30,7 @@
|
||||
static const char *TAG = "[ESPNOW_S]";
|
||||
|
||||
static bool s_joined;
|
||||
static bool s_master_ota_grace;
|
||||
static bool s_accel_stream_enabled;
|
||||
static bool s_tap_notify_single;
|
||||
static bool s_tap_notify_double;
|
||||
@@ -106,8 +109,18 @@ static esp_err_t send_battery_report(const uint8_t *dest_mac,
|
||||
return esp_now_core_send(dest_mac, &msg);
|
||||
}
|
||||
|
||||
static uint32_t slave_master_lost_ms(void) {
|
||||
if (ota_uart_is_active() || s_master_ota_grace) {
|
||||
return SLAVE_MASTER_OTA_GRACE_MS;
|
||||
}
|
||||
return SLAVE_MASTER_LOST_MS;
|
||||
}
|
||||
|
||||
static void touch_master_presence(uint32_t now) { s_last_discover_ms = now; }
|
||||
|
||||
static void reset_join(void) {
|
||||
s_joined = false;
|
||||
s_master_ota_grace = false;
|
||||
s_accel_stream_enabled = false;
|
||||
memset(s_master_mac, 0, sizeof(s_master_mac));
|
||||
s_last_discover_ms = 0;
|
||||
@@ -216,6 +229,36 @@ static void handle_unicast_test(const uint8_t *master_mac,
|
||||
(unsigned long)test->seq, (int)s_joined);
|
||||
}
|
||||
|
||||
static void handle_echo_ping(const uint8_t *master_mac,
|
||||
const alox_EspNowEchoPing *ping) {
|
||||
if (ping == NULL || !s_joined ||
|
||||
!esp_now_core_mac_equal(master_mac, s_master_mac)) {
|
||||
return;
|
||||
}
|
||||
|
||||
char mac_str[18];
|
||||
esp_now_core_mac_to_str(master_mac, mac_str, sizeof(mac_str));
|
||||
ESP_LOGI(TAG, "ESP-NOW PING recv from %s host_ts=%llu master_time_us=%llu",
|
||||
mac_str, (unsigned long long)ping->host_timestamp_us,
|
||||
(unsigned long long)ping->master_time_us);
|
||||
|
||||
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
|
||||
msg.type = alox_EspNowMessageType_ESPNOW_ECHO_PONG;
|
||||
msg.which_payload = alox_EspNowMessage_echo_pong_tag;
|
||||
msg.payload.echo_pong.host_timestamp_us = ping->host_timestamp_us;
|
||||
msg.payload.echo_pong.master_time_us = ping->master_time_us;
|
||||
|
||||
esp_err_t err = esp_now_core_send_fast(s_master_mac, &msg);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "ECHO PONG send failed: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "ESP-NOW PONG send to %s host_ts=%llu master_time_us=%llu",
|
||||
mac_str, (unsigned long long)ping->host_timestamp_us,
|
||||
(unsigned long long)ping->master_time_us);
|
||||
}
|
||||
|
||||
static void handle_restart(const uint8_t *master_mac,
|
||||
const alox_EspNowRestart *req) {
|
||||
const uint8_t *own = esp_now_core_own_mac();
|
||||
@@ -358,8 +401,13 @@ static void handle_discover(const uint8_t *sender_mac,
|
||||
if (!esp_now_core_mac_equal(sender_mac, s_master_mac)) {
|
||||
return;
|
||||
}
|
||||
if ((now - s_last_discover_ms) <= SLAVE_MASTER_LOST_MS) {
|
||||
s_last_discover_ms = now;
|
||||
s_master_ota_grace = discover->master_ota_pending;
|
||||
if ((now - s_last_discover_ms) <= slave_master_lost_ms()) {
|
||||
touch_master_presence(now);
|
||||
return;
|
||||
}
|
||||
if (ota_uart_is_active()) {
|
||||
touch_master_presence(now);
|
||||
return;
|
||||
}
|
||||
ESP_LOGW(TAG, "master lost, rejoining");
|
||||
@@ -368,7 +416,8 @@ static void handle_discover(const uint8_t *sender_mac,
|
||||
|
||||
memcpy(s_master_mac, sender_mac, ESP_NOW_ETH_ALEN);
|
||||
s_joined = true;
|
||||
s_last_discover_ms = now;
|
||||
s_master_ota_grace = discover->master_ota_pending;
|
||||
touch_master_presence(now);
|
||||
esp_now_core_ensure_peer(sender_mac);
|
||||
|
||||
char mac_str[18];
|
||||
@@ -384,10 +433,14 @@ static void check_master_timeout(void) {
|
||||
if (!s_joined || s_last_discover_ms == 0) {
|
||||
return;
|
||||
}
|
||||
if (ota_uart_is_active()) {
|
||||
return;
|
||||
}
|
||||
uint32_t now = esp_now_core_now_ms();
|
||||
if ((now - s_last_discover_ms) > SLAVE_MASTER_LOST_MS) {
|
||||
ESP_LOGW(TAG, "no master discover for %u ms, reconnecting",
|
||||
(unsigned)(now - s_last_discover_ms));
|
||||
uint32_t limit = slave_master_lost_ms();
|
||||
if ((now - s_last_discover_ms) > limit) {
|
||||
ESP_LOGW(TAG, "no master discover for %u ms (limit %u), reconnecting",
|
||||
(unsigned)(now - s_last_discover_ms), (unsigned)limit);
|
||||
reset_join();
|
||||
}
|
||||
}
|
||||
@@ -481,12 +534,16 @@ void esp_now_slave_on_recv(const esp_now_recv_info_t *info, const uint8_t *data,
|
||||
|
||||
if (ota_uart_is_active()) {
|
||||
switch (msg.which_payload) {
|
||||
case alox_EspNowMessage_discover_tag:
|
||||
handle_discover(info->src_addr, &msg.payload.discover);
|
||||
break;
|
||||
case alox_EspNowMessage_ota_start_tag:
|
||||
case alox_EspNowMessage_ota_payload_tag:
|
||||
case alox_EspNowMessage_ota_end_tag:
|
||||
if (!from_joined_master(info->src_addr)) {
|
||||
break;
|
||||
}
|
||||
touch_master_presence(esp_now_core_now_ms());
|
||||
if (msg.which_payload == alox_EspNowMessage_ota_start_tag) {
|
||||
ota_espnow_slave_on_start(info->src_addr, &msg.payload.ota_start);
|
||||
} else if (msg.which_payload == alox_EspNowMessage_ota_payload_tag) {
|
||||
@@ -510,6 +567,11 @@ void esp_now_slave_on_recv(const esp_now_recv_info_t *info, const uint8_t *data,
|
||||
handle_unicast_test(info->src_addr, &msg.payload.unicast_test);
|
||||
}
|
||||
break;
|
||||
case alox_EspNowMessage_echo_ping_tag:
|
||||
if (from_joined_master(info->src_addr)) {
|
||||
handle_echo_ping(info->src_addr, &msg.payload.echo_ping);
|
||||
}
|
||||
break;
|
||||
case alox_EspNowMessage_accel_deadzone_tag:
|
||||
if (from_joined_master(info->src_addr)) {
|
||||
handle_accel_deadzone(info->src_addr, &msg.payload.accel_deadzone);
|
||||
@@ -545,6 +607,11 @@ void esp_now_slave_on_recv(const esp_now_recv_info_t *info, const uint8_t *data,
|
||||
handle_restart(info->src_addr, &msg.payload.restart);
|
||||
}
|
||||
break;
|
||||
case alox_EspNowMessage_ota_start_tag:
|
||||
if (from_joined_master(info->src_addr)) {
|
||||
ota_espnow_slave_on_start(info->src_addr, &msg.payload.ota_start);
|
||||
}
|
||||
break;
|
||||
default:
|
||||
ESP_LOGW(TAG, "unhandled which=%u type=%u", msg.which_payload,
|
||||
(unsigned)msg.type);
|
||||
|
||||
Reference in New Issue
Block a user