Added ECHO cmd and fixed some timing issues
This commit is contained in:
@@ -22,6 +22,7 @@ idf_component_register(
|
||||
"cmd/cmd_tap_notify.c"
|
||||
"cmd/cmd_cache_status.c"
|
||||
"cmd/cmd_espnow_unicast_test.c"
|
||||
"cmd/cmd_espnow_echo_ping.c"
|
||||
"cmd/cmd_espnow_find_me.c"
|
||||
"cmd/cmd_restart.c"
|
||||
"pod_reboot.c"
|
||||
@@ -61,6 +62,7 @@ idf_component_register(
|
||||
esp_driver_i2c
|
||||
esp_adc
|
||||
app_update
|
||||
esp_timer
|
||||
bma456)
|
||||
|
||||
target_compile_definitions(${COMPONENT_LIB}
|
||||
|
||||
@@ -0,0 +1,70 @@
|
||||
#include "client_registry.h"
|
||||
#include "cmd_espnow_echo_ping.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_now_comm.h"
|
||||
#include "uart_cmd.h"
|
||||
|
||||
static const char *TAG = "[ECHO_PING]";
|
||||
|
||||
static void reply(bool success, uint32_t client_id, uint64_t timestamp_us,
|
||||
uint32_t esp_rtt_us) {
|
||||
alox_UartMessage response;
|
||||
uart_cmd_init_response(&response, alox_MessageType_ESPNOW_ECHO_PING,
|
||||
alox_UartMessage_espnow_echo_ping_response_tag);
|
||||
response.payload.espnow_echo_ping_response.success = success;
|
||||
response.payload.espnow_echo_ping_response.client_id = client_id;
|
||||
response.payload.espnow_echo_ping_response.timestamp_us = timestamp_us;
|
||||
response.payload.espnow_echo_ping_response.esp_rtt_us = esp_rtt_us;
|
||||
uart_cmd_send(&response, TAG);
|
||||
}
|
||||
|
||||
static void handle_espnow_echo_ping(const uint8_t *data, size_t len) {
|
||||
alox_UartMessage uart_msg;
|
||||
|
||||
if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "decode failed");
|
||||
reply(false, 0, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const alox_EspNowEchoPingRequest *req = UART_CMD_REQ(
|
||||
&uart_msg, alox_UartMessage_espnow_echo_ping_request_tag,
|
||||
espnow_echo_ping_request);
|
||||
if (req == NULL || req->client_id == 0) {
|
||||
ESP_LOGW(TAG, "need client_id in request");
|
||||
reply(false, 0, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
const client_info_t *client = client_registry_find_by_id(req->client_id);
|
||||
if (client == NULL) {
|
||||
ESP_LOGW(TAG, "client id %lu not in registry",
|
||||
(unsigned long)req->client_id);
|
||||
reply(false, req->client_id, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "UART request client_id=%lu host_ts=%llu",
|
||||
(unsigned long)req->client_id,
|
||||
(unsigned long long)req->timestamp_us);
|
||||
|
||||
esp_now_echo_ping_result_t ping_result = {0};
|
||||
esp_err_t err =
|
||||
esp_now_comm_echo_ping(client->mac, req->timestamp_us, &ping_result);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "echo ping to id=%lu failed: %s", (unsigned long)req->client_id,
|
||||
esp_err_to_name(err));
|
||||
reply(false, req->client_id, 0, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "UART reply client_id=%lu host_ts=%llu esp_rtt_us=%lu",
|
||||
(unsigned long)req->client_id,
|
||||
(unsigned long long)ping_result.echoed_us,
|
||||
(unsigned long)ping_result.esp_rtt_us);
|
||||
reply(true, req->client_id, ping_result.echoed_us, ping_result.esp_rtt_us);
|
||||
}
|
||||
|
||||
void cmd_espnow_echo_ping_register(void) {
|
||||
uart_cmd_register(alox_MessageType_ESPNOW_ECHO_PING, handle_espnow_echo_ping);
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
#ifndef CMD_ESPNOW_ECHO_PING_H
|
||||
#define CMD_ESPNOW_ECHO_PING_H
|
||||
|
||||
void cmd_espnow_echo_ping_register(void);
|
||||
|
||||
#endif
|
||||
@@ -57,6 +57,8 @@ static const char *message_type_name(uint16_t id) {
|
||||
return "TAP_NOTIFY";
|
||||
case alox_MessageType_CACHE_STATUS:
|
||||
return "CACHE_STATUS";
|
||||
case alox_MessageType_ESPNOW_ECHO_PING:
|
||||
return "ESPNOW_ECHO_PING";
|
||||
default:
|
||||
return "UNKNOWN";
|
||||
}
|
||||
|
||||
@@ -26,6 +26,17 @@ esp_err_t esp_now_comm_send_accel_deadzone(const uint8_t mac[CLIENT_MAC_LEN],
|
||||
esp_err_t esp_now_comm_send_unicast_test(const uint8_t mac[CLIENT_MAC_LEN],
|
||||
uint32_t seq);
|
||||
|
||||
/** Result of a master-side ESP-NOW echo ping round-trip. */
|
||||
typedef struct {
|
||||
uint64_t echoed_us;
|
||||
uint32_t esp_rtt_us;
|
||||
} esp_now_echo_ping_result_t;
|
||||
|
||||
/** Master: ESP-NOW echo ping round-trip; fills result on success. */
|
||||
esp_err_t esp_now_comm_echo_ping(const uint8_t mac[CLIENT_MAC_LEN],
|
||||
uint64_t timestamp_us,
|
||||
esp_now_echo_ping_result_t *result);
|
||||
|
||||
/** Master: trigger find-me LED sequence on one slave. */
|
||||
esp_err_t esp_now_comm_send_find_me(const uint8_t mac[CLIENT_MAC_LEN],
|
||||
uint32_t client_id);
|
||||
|
||||
+123
-13
@@ -21,7 +21,18 @@ static app_config_t s_config;
|
||||
static uint8_t s_wifi_channel;
|
||||
static uint8_t s_own_mac[ESP_NOW_ETH_ALEN];
|
||||
static SemaphoreHandle_t s_send_done;
|
||||
static SemaphoreHandle_t s_send_lock;
|
||||
static bool s_send_cb_ready;
|
||||
static volatile esp_now_send_status_t s_last_send_status;
|
||||
static volatile bool s_last_send_ok;
|
||||
|
||||
#define ESPNOW_SEND_DONE_TIMEOUT_MS 500u
|
||||
#define ESPNOW_SEND_MAX_ATTEMPTS 8u
|
||||
#define ESPNOW_SEND_RETRY_DELAY_MS 10u
|
||||
#define ESPNOW_NOMEM_RETRY_DELAY_MS 50u
|
||||
|
||||
#define ESPNOW_RELIABLE_MAX_ATTEMPTS 24u
|
||||
#define ESPNOW_RELIABLE_NOMEM_DELAY_MS 50u
|
||||
|
||||
static uint8_t network_to_channel(uint8_t network) {
|
||||
if (network < 1 || network > 13) {
|
||||
@@ -33,7 +44,8 @@ static uint8_t network_to_channel(uint8_t network) {
|
||||
static void espnow_send_done_cb(const esp_now_send_info_t *tx_info,
|
||||
esp_now_send_status_t status) {
|
||||
(void)tx_info;
|
||||
(void)status;
|
||||
s_last_send_status = status;
|
||||
s_last_send_ok = (status == ESP_NOW_SEND_SUCCESS);
|
||||
if (s_send_done != NULL) {
|
||||
xSemaphoreGive(s_send_done);
|
||||
}
|
||||
@@ -93,43 +105,140 @@ esp_err_t esp_now_core_ensure_broadcast_peer(void) {
|
||||
return esp_now_core_ensure_peer(ESPNOW_BCAST);
|
||||
}
|
||||
|
||||
static esp_err_t send_with_backpressure(const uint8_t *dest_mac,
|
||||
const alox_EspNowMessage *msg,
|
||||
uint32_t max_attempts,
|
||||
uint32_t nomem_delay_ms,
|
||||
uint32_t retry_delay_ms,
|
||||
bool quiet_retries) {
|
||||
if (s_send_lock == NULL ||
|
||||
xSemaphoreTake(s_send_lock, pdMS_TO_TICKS(5000)) != pdTRUE) {
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
uint8_t buf[ESPNOW_PB_MAX_SIZE];
|
||||
size_t len = 0;
|
||||
esp_err_t result = ESP_FAIL;
|
||||
|
||||
esp_err_t err = esp_now_proto_encode(msg, buf, sizeof(buf), &len);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "encode failed");
|
||||
result = err;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (len > ESP_NOW_MAX_DATA_LEN) {
|
||||
ESP_LOGW(TAG, "encoded len %u > ESP-NOW max %u", (unsigned)len,
|
||||
(unsigned)ESP_NOW_MAX_DATA_LEN);
|
||||
result = ESP_ERR_INVALID_SIZE;
|
||||
goto out;
|
||||
}
|
||||
|
||||
if (esp_now_core_ensure_peer(dest_mac) != ESP_OK) {
|
||||
result = ESP_FAIL;
|
||||
goto out;
|
||||
}
|
||||
|
||||
for (uint32_t attempt = 0; attempt < max_attempts; attempt++) {
|
||||
if (s_send_cb_ready && s_send_done != NULL) {
|
||||
xSemaphoreTake(s_send_done, 0);
|
||||
}
|
||||
s_last_send_ok = false;
|
||||
|
||||
err = esp_now_send(dest_mac, buf, len);
|
||||
if (err != ESP_OK) {
|
||||
const bool last = (attempt + 1 >= max_attempts);
|
||||
if (!quiet_retries || last) {
|
||||
ESP_LOGW(TAG, "send type=%u failed (attempt %lu/%lu): %s",
|
||||
(unsigned)msg->type, (unsigned long)(attempt + 1),
|
||||
(unsigned long)max_attempts, esp_err_to_name(err));
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(err == ESP_ERR_ESPNOW_NO_MEM ? nomem_delay_ms
|
||||
: retry_delay_ms));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (s_send_cb_ready && s_send_done != NULL) {
|
||||
if (xSemaphoreTake(s_send_done, pdMS_TO_TICKS(ESPNOW_SEND_DONE_TIMEOUT_MS)) !=
|
||||
pdTRUE) {
|
||||
const bool last = (attempt + 1 >= max_attempts);
|
||||
if (!quiet_retries || last) {
|
||||
ESP_LOGW(TAG, "send type=%u done timeout (attempt %lu/%lu)",
|
||||
(unsigned)msg->type, (unsigned long)(attempt + 1),
|
||||
(unsigned long)max_attempts);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(retry_delay_ms));
|
||||
continue;
|
||||
}
|
||||
if (!s_last_send_ok) {
|
||||
const bool last = (attempt + 1 >= max_attempts);
|
||||
if (!quiet_retries || last) {
|
||||
ESP_LOGW(TAG, "send type=%u peer status=%d (attempt %lu/%lu)",
|
||||
(unsigned)msg->type, (int)s_last_send_status,
|
||||
(unsigned long)(attempt + 1), (unsigned long)max_attempts);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(retry_delay_ms));
|
||||
continue;
|
||||
}
|
||||
}
|
||||
result = ESP_OK;
|
||||
goto out;
|
||||
}
|
||||
|
||||
out:
|
||||
xSemaphoreGive(s_send_lock);
|
||||
return result;
|
||||
}
|
||||
|
||||
esp_err_t esp_now_core_send_wait(const uint8_t *dest_mac,
|
||||
const alox_EspNowMessage *msg) {
|
||||
return send_with_backpressure(dest_mac, msg, ESPNOW_SEND_MAX_ATTEMPTS,
|
||||
ESPNOW_NOMEM_RETRY_DELAY_MS,
|
||||
ESPNOW_SEND_RETRY_DELAY_MS, false);
|
||||
}
|
||||
|
||||
esp_err_t esp_now_core_send_reliable(const uint8_t *dest_mac,
|
||||
const alox_EspNowMessage *msg) {
|
||||
return send_with_backpressure(dest_mac, msg, ESPNOW_RELIABLE_MAX_ATTEMPTS,
|
||||
ESPNOW_RELIABLE_NOMEM_DELAY_MS,
|
||||
ESPNOW_SEND_RETRY_DELAY_MS, true);
|
||||
}
|
||||
|
||||
esp_err_t esp_now_core_send_fast(const uint8_t *dest_mac,
|
||||
const alox_EspNowMessage *msg) {
|
||||
if (s_send_lock == NULL ||
|
||||
xSemaphoreTake(s_send_lock, pdMS_TO_TICKS(5000)) != pdTRUE) {
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
uint8_t buf[ESPNOW_PB_MAX_SIZE];
|
||||
size_t len = 0;
|
||||
|
||||
esp_err_t err = esp_now_proto_encode(msg, buf, sizeof(buf), &len);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "encode failed");
|
||||
xSemaphoreGive(s_send_lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
if (len > ESP_NOW_MAX_DATA_LEN) {
|
||||
ESP_LOGW(TAG, "encoded len %u > ESP-NOW max %u", (unsigned)len,
|
||||
(unsigned)ESP_NOW_MAX_DATA_LEN);
|
||||
xSemaphoreGive(s_send_lock);
|
||||
return ESP_ERR_INVALID_SIZE;
|
||||
}
|
||||
|
||||
if (esp_now_core_ensure_peer(dest_mac) != ESP_OK) {
|
||||
xSemaphoreGive(s_send_lock);
|
||||
return ESP_FAIL;
|
||||
}
|
||||
|
||||
if (s_send_cb_ready && s_send_done != NULL) {
|
||||
xSemaphoreTake(s_send_done, 0);
|
||||
}
|
||||
|
||||
err = esp_now_send(dest_mac, buf, len);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGW(TAG, "send type=%u failed: %s", (unsigned)msg->type,
|
||||
esp_err_to_name(err));
|
||||
return err;
|
||||
}
|
||||
|
||||
if (s_send_cb_ready && s_send_done != NULL) {
|
||||
if (xSemaphoreTake(s_send_done, pdMS_TO_TICKS(50)) != pdTRUE) {
|
||||
ESP_LOGW(TAG, "send type=%u done timeout", (unsigned)msg->type);
|
||||
}
|
||||
}
|
||||
xSemaphoreGive(s_send_lock);
|
||||
return err;
|
||||
}
|
||||
|
||||
@@ -163,7 +272,8 @@ esp_err_t esp_now_core_init_radio(uint8_t channel) {
|
||||
|
||||
void esp_now_core_init_send_done(void) {
|
||||
s_send_done = xSemaphoreCreateBinary();
|
||||
if (s_send_done != NULL &&
|
||||
s_send_lock = xSemaphoreCreateMutex();
|
||||
if (s_send_done != NULL && s_send_lock != NULL &&
|
||||
esp_now_register_send_cb(espnow_send_done_cb) == ESP_OK) {
|
||||
s_send_cb_ready = true;
|
||||
} else {
|
||||
|
||||
+115
-2
@@ -5,8 +5,10 @@
|
||||
#include "esp_now_proto.h"
|
||||
#include "board_input.h"
|
||||
#include "ota_espnow.h"
|
||||
#include "ota_session.h"
|
||||
#include "ota_uart.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_timer.h"
|
||||
#include "freertos/FreeRTOS.h"
|
||||
#include "freertos/idf_additions.h"
|
||||
#include <string.h>
|
||||
@@ -51,6 +53,66 @@ static esp_err_t send_unicast_test(const uint8_t *dest_mac, uint32_t seq) {
|
||||
return esp_now_core_send(dest_mac, &msg);
|
||||
}
|
||||
|
||||
#define ESPNOW_ECHO_PING_TIMEOUT_MS 500
|
||||
#define ECHO_PING_TAG "[ECHO_PING]"
|
||||
|
||||
static SemaphoreHandle_t s_echo_pong_sem;
|
||||
static bool s_echo_waiting;
|
||||
static uint64_t s_echo_expect_ts;
|
||||
static uint32_t s_echo_esp_rtt_us;
|
||||
static uint8_t s_echo_expect_mac[ESP_NOW_ETH_ALEN];
|
||||
|
||||
static esp_err_t echo_ping_init(void) {
|
||||
if (s_echo_pong_sem != NULL) {
|
||||
return ESP_OK;
|
||||
}
|
||||
s_echo_pong_sem = xSemaphoreCreateBinary();
|
||||
if (s_echo_pong_sem == NULL) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
static esp_err_t send_echo_ping(const uint8_t *dest_mac, uint64_t host_timestamp_us,
|
||||
uint64_t master_time_us) {
|
||||
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
|
||||
msg.type = alox_EspNowMessageType_ESPNOW_ECHO_PING;
|
||||
msg.which_payload = alox_EspNowMessage_echo_ping_tag;
|
||||
msg.payload.echo_ping.host_timestamp_us = host_timestamp_us;
|
||||
msg.payload.echo_ping.master_time_us = master_time_us;
|
||||
char mac_str[18];
|
||||
esp_now_core_mac_to_str(dest_mac, mac_str, sizeof(mac_str));
|
||||
ESP_LOGI(ECHO_PING_TAG, "ESP-NOW PING send to %s host_ts=%llu master_time_us=%llu",
|
||||
mac_str, (unsigned long long)host_timestamp_us,
|
||||
(unsigned long long)master_time_us);
|
||||
return esp_now_core_send_fast(dest_mac, &msg);
|
||||
}
|
||||
|
||||
static void echo_ping_on_pong(const uint8_t mac[ESP_NOW_ETH_ALEN],
|
||||
const alox_EspNowEchoPong *pong) {
|
||||
if (pong == NULL || !s_echo_waiting) {
|
||||
return;
|
||||
}
|
||||
if (!esp_now_core_mac_equal(mac, s_echo_expect_mac)) {
|
||||
return;
|
||||
}
|
||||
if (pong->host_timestamp_us != s_echo_expect_ts) {
|
||||
return;
|
||||
}
|
||||
int64_t now_us = esp_timer_get_time();
|
||||
s_echo_esp_rtt_us =
|
||||
(uint32_t)(now_us - (int64_t)pong->master_time_us);
|
||||
char mac_str[18];
|
||||
esp_now_core_mac_to_str(mac, mac_str, sizeof(mac_str));
|
||||
ESP_LOGI(ECHO_PING_TAG,
|
||||
"ESP-NOW PONG recv from %s host_ts=%llu master_time_us=%llu "
|
||||
"recv_time_us=%lld esp_rtt_us=%lu",
|
||||
mac_str, (unsigned long long)pong->host_timestamp_us,
|
||||
(unsigned long long)pong->master_time_us, (long long)now_us,
|
||||
(unsigned long)s_echo_esp_rtt_us);
|
||||
xSemaphoreGive(s_echo_pong_sem);
|
||||
}
|
||||
|
||||
static esp_err_t send_find_me(const uint8_t *dest_mac, uint32_t client_id) {
|
||||
alox_EspNowMessage msg = alox_EspNowMessage_init_zero;
|
||||
msg.type = alox_EspNowMessageType_ESPNOW_FIND_ME;
|
||||
@@ -119,7 +181,7 @@ static esp_err_t send_ota_payload(const uint8_t *dest_mac, uint32_t seq,
|
||||
msg.payload.ota_payload.seq = seq;
|
||||
msg.payload.ota_payload.data.size = len;
|
||||
memcpy(msg.payload.ota_payload.data.bytes, data, len);
|
||||
return esp_now_core_send_wait(dest_mac, &msg);
|
||||
return esp_now_core_send_reliable(dest_mac, &msg);
|
||||
}
|
||||
|
||||
static esp_err_t send_ota_end(const uint8_t *dest_mac) {
|
||||
@@ -224,6 +286,48 @@ esp_err_t esp_now_comm_send_unicast_test(const uint8_t mac[CLIENT_MAC_LEN],
|
||||
return err;
|
||||
}
|
||||
|
||||
esp_err_t esp_now_comm_echo_ping(const uint8_t mac[CLIENT_MAC_LEN],
|
||||
uint64_t timestamp_us,
|
||||
esp_now_echo_ping_result_t *result) {
|
||||
if (mac == NULL || !esp_now_core_is_master()) {
|
||||
return ESP_ERR_INVALID_STATE;
|
||||
}
|
||||
if (echo_ping_init() != ESP_OK) {
|
||||
return ESP_ERR_NO_MEM;
|
||||
}
|
||||
|
||||
xSemaphoreTake(s_echo_pong_sem, 0);
|
||||
s_echo_waiting = true;
|
||||
s_echo_expect_ts = timestamp_us;
|
||||
s_echo_esp_rtt_us = 0;
|
||||
memcpy(s_echo_expect_mac, mac, ESP_NOW_ETH_ALEN);
|
||||
|
||||
uint64_t master_time_us = (uint64_t)esp_timer_get_time();
|
||||
esp_err_t err = send_echo_ping(mac, timestamp_us, master_time_us);
|
||||
if (err != ESP_OK) {
|
||||
s_echo_waiting = false;
|
||||
return err;
|
||||
}
|
||||
|
||||
if (xSemaphoreTake(s_echo_pong_sem,
|
||||
pdMS_TO_TICKS(ESPNOW_ECHO_PING_TIMEOUT_MS)) != pdTRUE) {
|
||||
s_echo_waiting = false;
|
||||
char mac_str[18];
|
||||
esp_now_core_mac_to_str(mac, mac_str, sizeof(mac_str));
|
||||
ESP_LOGW(ECHO_PING_TAG,
|
||||
"ESP-NOW PONG timeout to %s host_ts=%llu",
|
||||
mac_str, (unsigned long long)timestamp_us);
|
||||
return ESP_ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
s_echo_waiting = false;
|
||||
if (result != NULL) {
|
||||
result->echoed_us = timestamp_us;
|
||||
result->esp_rtt_us = s_echo_esp_rtt_us;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
esp_err_t esp_now_comm_send_accel_stream(const uint8_t mac[CLIENT_MAC_LEN],
|
||||
uint32_t client_id, bool enable) {
|
||||
if (mac == NULL || !esp_now_core_is_master()) {
|
||||
@@ -415,6 +519,12 @@ void esp_now_master_on_recv(const esp_now_recv_info_t *info, const uint8_t *data
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.which_payload == alox_EspNowMessage_echo_pong_tag) {
|
||||
esp_now_core_ensure_peer(info->src_addr);
|
||||
echo_ping_on_pong(info->src_addr, &msg.payload.echo_pong);
|
||||
return;
|
||||
}
|
||||
|
||||
if (msg.type == alox_EspNowMessageType_ESPNOW_BATTERY_REPORT &&
|
||||
msg.which_payload != alox_EspNowMessage_battery_report_tag) {
|
||||
ESP_LOGW(TAG, "BATTERY_REPORT type but which=%u", msg.which_payload);
|
||||
@@ -439,7 +549,10 @@ static void discover_task(void *param) {
|
||||
(unsigned)esp_now_core_wifi_channel());
|
||||
|
||||
while (1) {
|
||||
esp_now_core_send(ESPNOW_BCAST, &msg);
|
||||
msg.payload.discover.master_ota_pending = ota_session_busy();
|
||||
if (!ota_espnow_distribution_active()) {
|
||||
esp_now_core_send_fast(ESPNOW_BCAST, &msg);
|
||||
}
|
||||
vTaskDelay(pdMS_TO_TICKS(ESPNOW_DISCOVER_INTERVAL_MS));
|
||||
}
|
||||
}
|
||||
|
||||
+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);
|
||||
|
||||
+1
-1
@@ -158,7 +158,7 @@ void led_ring_init(void) {
|
||||
|
||||
void led_ring_send_command(led_command_t *cmd) {
|
||||
if (led_queue != NULL) {
|
||||
xQueueSend(led_queue, cmd, portMAX_DELAY);
|
||||
(void)xQueueSend(led_queue, cmd, 0);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
#include "cmd_tap_notify.h"
|
||||
#include "cmd_cache_status.h"
|
||||
#include "cmd_espnow_unicast_test.h"
|
||||
#include "cmd_espnow_echo_ping.h"
|
||||
#include "cmd_espnow_find_me.h"
|
||||
#include "cmd_restart.h"
|
||||
#include "cmd_client_info.h"
|
||||
@@ -72,6 +73,9 @@ uint8_t reverse_high_nibble_lut(uint8_t n) {
|
||||
}
|
||||
|
||||
void app_main(void) {
|
||||
|
||||
esp_log_level_set("*", ESP_LOG_NONE);
|
||||
|
||||
if (pod_settings_init() != ESP_OK) {
|
||||
ESP_LOGW(TAG, "settings NVS init failed; using defaults");
|
||||
}
|
||||
@@ -185,6 +189,7 @@ void app_main(void) {
|
||||
cmd_tap_notify_register();
|
||||
cmd_cache_status_register();
|
||||
cmd_espnow_unicast_test_register();
|
||||
cmd_espnow_echo_ping_register();
|
||||
cmd_espnow_find_me_register();
|
||||
cmd_restart_register();
|
||||
cmd_led_ring_register();
|
||||
|
||||
@@ -9,6 +9,12 @@
|
||||
PB_BIND(alox_EspNowUnicastTest, alox_EspNowUnicastTest, AUTO)
|
||||
|
||||
|
||||
PB_BIND(alox_EspNowEchoPing, alox_EspNowEchoPing, AUTO)
|
||||
|
||||
|
||||
PB_BIND(alox_EspNowEchoPong, alox_EspNowEchoPong, AUTO)
|
||||
|
||||
|
||||
PB_BIND(alox_EspNowFindMe, alox_EspNowFindMe, AUTO)
|
||||
|
||||
|
||||
|
||||
@@ -29,7 +29,9 @@ typedef enum _alox_EspNowMessageType {
|
||||
alox_EspNowMessageType_ESPNOW_BATTERY_QUERY = 15,
|
||||
alox_EspNowMessageType_ESPNOW_BATTERY_REPORT = 16,
|
||||
alox_EspNowMessageType_ESPNOW_SET_TAP_NOTIFY = 17,
|
||||
alox_EspNowMessageType_ESPNOW_TAP_EVENT = 18
|
||||
alox_EspNowMessageType_ESPNOW_TAP_EVENT = 18,
|
||||
alox_EspNowMessageType_ESPNOW_ECHO_PING = 19,
|
||||
alox_EspNowMessageType_ESPNOW_ECHO_PONG = 20
|
||||
} alox_EspNowMessageType;
|
||||
|
||||
/* Struct definitions */
|
||||
@@ -37,6 +39,19 @@ typedef struct _alox_EspNowUnicastTest {
|
||||
uint32_t seq;
|
||||
} alox_EspNowUnicastTest;
|
||||
|
||||
/* * Master → slave: echo ping (host ts + master monotonic time for RTT). */
|
||||
typedef struct _alox_EspNowEchoPing {
|
||||
uint64_t host_timestamp_us;
|
||||
/* * esp_timer_get_time() (µs since boot) when master forwarded this message. */
|
||||
uint64_t master_time_us;
|
||||
} alox_EspNowEchoPing;
|
||||
|
||||
/* * Slave → master: echo ping payload unchanged. */
|
||||
typedef struct _alox_EspNowEchoPong {
|
||||
uint64_t host_timestamp_us;
|
||||
uint64_t master_time_us;
|
||||
} alox_EspNowEchoPong;
|
||||
|
||||
/* * Master → slave: locate pod (LED ring R/G/B ×3 @ full brightness). */
|
||||
typedef struct _alox_EspNowFindMe {
|
||||
/* * 0 = any slave; otherwise only slave_id must match */
|
||||
@@ -50,6 +65,8 @@ typedef struct _alox_EspNowRestart {
|
||||
|
||||
typedef struct _alox_EspNowDiscover {
|
||||
uint32_t network;
|
||||
/* * Master is in an OTA session (UART upload or ESP-NOW distribution). */
|
||||
bool master_ota_pending;
|
||||
} alox_EspNowDiscover;
|
||||
|
||||
typedef struct _alox_EspNowSlavePresence {
|
||||
@@ -169,6 +186,8 @@ typedef struct _alox_EspNowMessage {
|
||||
alox_EspNowBatteryReport battery_report;
|
||||
alox_EspNowTapNotify tap_notify;
|
||||
alox_EspNowTapEvent tap_event;
|
||||
alox_EspNowEchoPing echo_ping;
|
||||
alox_EspNowEchoPong echo_pong;
|
||||
} payload;
|
||||
} alox_EspNowMessage;
|
||||
|
||||
@@ -179,8 +198,10 @@ extern "C" {
|
||||
|
||||
/* Helper constants for enums */
|
||||
#define _alox_EspNowMessageType_MIN alox_EspNowMessageType_ESPNOW_UNKNOWN
|
||||
#define _alox_EspNowMessageType_MAX alox_EspNowMessageType_ESPNOW_TAP_EVENT
|
||||
#define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_TAP_EVENT+1))
|
||||
#define _alox_EspNowMessageType_MAX alox_EspNowMessageType_ESPNOW_ECHO_PONG
|
||||
#define _alox_EspNowMessageType_ARRAYSIZE ((alox_EspNowMessageType)(alox_EspNowMessageType_ESPNOW_ECHO_PONG+1))
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
@@ -204,9 +225,11 @@ extern "C" {
|
||||
|
||||
/* Initializer values for message structs */
|
||||
#define alox_EspNowUnicastTest_init_default {0}
|
||||
#define alox_EspNowEchoPing_init_default {0, 0}
|
||||
#define alox_EspNowEchoPong_init_default {0, 0}
|
||||
#define alox_EspNowFindMe_init_default {0}
|
||||
#define alox_EspNowRestart_init_default {0}
|
||||
#define alox_EspNowDiscover_init_default {0}
|
||||
#define alox_EspNowDiscover_init_default {0, 0}
|
||||
#define alox_EspNowSlavePresence_init_default {0, {{NULL}, NULL}, 0, 0, 0, 0}
|
||||
#define alox_EspNowAccelDeadzone_init_default {0, 0}
|
||||
#define alox_EspNowAccelStream_init_default {0, 0}
|
||||
@@ -222,9 +245,11 @@ extern "C" {
|
||||
#define alox_EspNowOtaStatus_init_default {0, 0, 0}
|
||||
#define alox_EspNowMessage_init_default {_alox_EspNowMessageType_MIN, 0, {alox_EspNowDiscover_init_default}}
|
||||
#define alox_EspNowUnicastTest_init_zero {0}
|
||||
#define alox_EspNowEchoPing_init_zero {0, 0}
|
||||
#define alox_EspNowEchoPong_init_zero {0, 0}
|
||||
#define alox_EspNowFindMe_init_zero {0}
|
||||
#define alox_EspNowRestart_init_zero {0}
|
||||
#define alox_EspNowDiscover_init_zero {0}
|
||||
#define alox_EspNowDiscover_init_zero {0, 0}
|
||||
#define alox_EspNowSlavePresence_init_zero {0, {{NULL}, NULL}, 0, 0, 0, 0}
|
||||
#define alox_EspNowAccelDeadzone_init_zero {0, 0}
|
||||
#define alox_EspNowAccelStream_init_zero {0, 0}
|
||||
@@ -242,9 +267,14 @@ extern "C" {
|
||||
|
||||
/* Field tags (for use in manual encoding/decoding) */
|
||||
#define alox_EspNowUnicastTest_seq_tag 1
|
||||
#define alox_EspNowEchoPing_host_timestamp_us_tag 1
|
||||
#define alox_EspNowEchoPing_master_time_us_tag 2
|
||||
#define alox_EspNowEchoPong_host_timestamp_us_tag 1
|
||||
#define alox_EspNowEchoPong_master_time_us_tag 2
|
||||
#define alox_EspNowFindMe_client_id_tag 1
|
||||
#define alox_EspNowRestart_client_id_tag 1
|
||||
#define alox_EspNowDiscover_network_tag 1
|
||||
#define alox_EspNowDiscover_master_ota_pending_tag 2
|
||||
#define alox_EspNowSlavePresence_network_tag 1
|
||||
#define alox_EspNowSlavePresence_mac_tag 2
|
||||
#define alox_EspNowSlavePresence_version_tag 3
|
||||
@@ -306,6 +336,8 @@ extern "C" {
|
||||
#define alox_EspNowMessage_battery_report_tag 17
|
||||
#define alox_EspNowMessage_tap_notify_tag 18
|
||||
#define alox_EspNowMessage_tap_event_tag 19
|
||||
#define alox_EspNowMessage_echo_ping_tag 20
|
||||
#define alox_EspNowMessage_echo_pong_tag 21
|
||||
|
||||
/* Struct field encoding specification for nanopb */
|
||||
#define alox_EspNowUnicastTest_FIELDLIST(X, a) \
|
||||
@@ -313,6 +345,18 @@ X(a, STATIC, SINGULAR, UINT32, seq, 1)
|
||||
#define alox_EspNowUnicastTest_CALLBACK NULL
|
||||
#define alox_EspNowUnicastTest_DEFAULT NULL
|
||||
|
||||
#define alox_EspNowEchoPing_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT64, host_timestamp_us, 1) \
|
||||
X(a, STATIC, SINGULAR, UINT64, master_time_us, 2)
|
||||
#define alox_EspNowEchoPing_CALLBACK NULL
|
||||
#define alox_EspNowEchoPing_DEFAULT NULL
|
||||
|
||||
#define alox_EspNowEchoPong_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT64, host_timestamp_us, 1) \
|
||||
X(a, STATIC, SINGULAR, UINT64, master_time_us, 2)
|
||||
#define alox_EspNowEchoPong_CALLBACK NULL
|
||||
#define alox_EspNowEchoPong_DEFAULT NULL
|
||||
|
||||
#define alox_EspNowFindMe_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT32, client_id, 1)
|
||||
#define alox_EspNowFindMe_CALLBACK NULL
|
||||
@@ -324,7 +368,8 @@ X(a, STATIC, SINGULAR, UINT32, client_id, 1)
|
||||
#define alox_EspNowRestart_DEFAULT NULL
|
||||
|
||||
#define alox_EspNowDiscover_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT32, network, 1)
|
||||
X(a, STATIC, SINGULAR, UINT32, network, 1) \
|
||||
X(a, STATIC, SINGULAR, BOOL, master_ota_pending, 2)
|
||||
#define alox_EspNowDiscover_CALLBACK NULL
|
||||
#define alox_EspNowDiscover_DEFAULT NULL
|
||||
|
||||
@@ -442,7 +487,9 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,led_ring,payload.led_ring), 15) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,battery_query,payload.battery_query), 16) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,battery_report,payload.battery_report), 17) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_notify,payload.tap_notify), 18) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_event,payload.tap_event), 19)
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_event,payload.tap_event), 19) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,echo_ping,payload.echo_ping), 20) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,echo_pong,payload.echo_pong), 21)
|
||||
#define alox_EspNowMessage_CALLBACK NULL
|
||||
#define alox_EspNowMessage_DEFAULT NULL
|
||||
#define alox_EspNowMessage_payload_discover_MSGTYPE alox_EspNowDiscover
|
||||
@@ -463,8 +510,12 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,tap_event,payload.tap_event), 19)
|
||||
#define alox_EspNowMessage_payload_battery_report_MSGTYPE alox_EspNowBatteryReport
|
||||
#define alox_EspNowMessage_payload_tap_notify_MSGTYPE alox_EspNowTapNotify
|
||||
#define alox_EspNowMessage_payload_tap_event_MSGTYPE alox_EspNowTapEvent
|
||||
#define alox_EspNowMessage_payload_echo_ping_MSGTYPE alox_EspNowEchoPing
|
||||
#define alox_EspNowMessage_payload_echo_pong_MSGTYPE alox_EspNowEchoPong
|
||||
|
||||
extern const pb_msgdesc_t alox_EspNowUnicastTest_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowEchoPing_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowEchoPong_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowFindMe_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowRestart_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowDiscover_msg;
|
||||
@@ -485,6 +536,8 @@ extern const pb_msgdesc_t alox_EspNowMessage_msg;
|
||||
|
||||
/* Defines for backwards compatibility with code written before nanopb-0.4.0 */
|
||||
#define alox_EspNowUnicastTest_fields &alox_EspNowUnicastTest_msg
|
||||
#define alox_EspNowEchoPing_fields &alox_EspNowEchoPing_msg
|
||||
#define alox_EspNowEchoPong_fields &alox_EspNowEchoPong_msg
|
||||
#define alox_EspNowFindMe_fields &alox_EspNowFindMe_msg
|
||||
#define alox_EspNowRestart_fields &alox_EspNowRestart_msg
|
||||
#define alox_EspNowDiscover_fields &alox_EspNowDiscover_msg
|
||||
@@ -512,7 +565,9 @@ extern const pb_msgdesc_t alox_EspNowMessage_msg;
|
||||
#define alox_EspNowAccelStream_size 8
|
||||
#define alox_EspNowBatteryQuery_size 6
|
||||
#define alox_EspNowBatteryReport_size 22
|
||||
#define alox_EspNowDiscover_size 6
|
||||
#define alox_EspNowDiscover_size 8
|
||||
#define alox_EspNowEchoPing_size 22
|
||||
#define alox_EspNowEchoPong_size 22
|
||||
#define alox_EspNowFindMe_size 6
|
||||
#define alox_EspNowLedRing_size 60
|
||||
#define alox_EspNowOtaEnd_size 0
|
||||
|
||||
@@ -24,12 +24,27 @@ enum EspNowMessageType {
|
||||
ESPNOW_BATTERY_REPORT = 16;
|
||||
ESPNOW_SET_TAP_NOTIFY = 17;
|
||||
ESPNOW_TAP_EVENT = 18;
|
||||
ESPNOW_ECHO_PING = 19;
|
||||
ESPNOW_ECHO_PONG = 20;
|
||||
}
|
||||
|
||||
message EspNowUnicastTest {
|
||||
uint32 seq = 1;
|
||||
}
|
||||
|
||||
/** Master → slave: echo ping (host ts + master monotonic time for RTT). */
|
||||
message EspNowEchoPing {
|
||||
uint64 host_timestamp_us = 1;
|
||||
/** esp_timer_get_time() (µs since boot) when master forwarded this message. */
|
||||
uint64 master_time_us = 2;
|
||||
}
|
||||
|
||||
/** Slave → master: echo ping payload unchanged. */
|
||||
message EspNowEchoPong {
|
||||
uint64 host_timestamp_us = 1;
|
||||
uint64 master_time_us = 2;
|
||||
}
|
||||
|
||||
/** Master → slave: locate pod (LED ring R/G/B ×3 @ full brightness). */
|
||||
message EspNowFindMe {
|
||||
/** 0 = any slave; otherwise only slave_id must match */
|
||||
@@ -43,6 +58,8 @@ message EspNowRestart {
|
||||
|
||||
message EspNowDiscover {
|
||||
uint32 network = 1;
|
||||
/** Master is in an OTA session (UART upload or ESP-NOW distribution). */
|
||||
bool master_ota_pending = 2;
|
||||
}
|
||||
|
||||
message EspNowSlavePresence {
|
||||
@@ -158,5 +175,7 @@ message EspNowMessage {
|
||||
EspNowBatteryReport battery_report = 17;
|
||||
EspNowTapNotify tap_notify = 18;
|
||||
EspNowTapEvent tap_event = 19;
|
||||
EspNowEchoPing echo_ping = 20;
|
||||
EspNowEchoPong echo_pong = 21;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -87,6 +87,12 @@ PB_BIND(alox_EspNowUnicastTestRequest, alox_EspNowUnicastTestRequest, AUTO)
|
||||
PB_BIND(alox_EspNowUnicastTestResponse, alox_EspNowUnicastTestResponse, AUTO)
|
||||
|
||||
|
||||
PB_BIND(alox_EspNowEchoPingRequest, alox_EspNowEchoPingRequest, AUTO)
|
||||
|
||||
|
||||
PB_BIND(alox_EspNowEchoPingResponse, alox_EspNowEchoPingResponse, AUTO)
|
||||
|
||||
|
||||
PB_BIND(alox_LedRingProgressRequest, alox_LedRingProgressRequest, AUTO)
|
||||
|
||||
|
||||
|
||||
@@ -32,7 +32,9 @@ typedef enum _alox_MessageType {
|
||||
alox_MessageType_BATTERY_STATUS = 26,
|
||||
alox_MessageType_TAP_NOTIFY = 27,
|
||||
/* * Combined cached accel + tap poll (one UART round-trip, ~16 ms cadence). */
|
||||
alox_MessageType_CACHE_STATUS = 29
|
||||
alox_MessageType_CACHE_STATUS = 29,
|
||||
/* * Host → master → slave → master: timestamp echo round-trip (latency test). */
|
||||
alox_MessageType_ESPNOW_ECHO_PING = 30
|
||||
} alox_MessageType;
|
||||
|
||||
typedef enum _alox_TapKind {
|
||||
@@ -235,6 +237,22 @@ typedef struct _alox_EspNowUnicastTestResponse {
|
||||
uint32_t seq;
|
||||
} alox_EspNowUnicastTestResponse;
|
||||
|
||||
/* * Host → master: ESP-NOW echo ping to one slave (timestamp echoed back). */
|
||||
typedef struct _alox_EspNowEchoPingRequest {
|
||||
uint32_t client_id;
|
||||
/* * Microseconds since Unix epoch (host clock). */
|
||||
uint64_t timestamp_us;
|
||||
} alox_EspNowEchoPingRequest;
|
||||
|
||||
typedef struct _alox_EspNowEchoPingResponse {
|
||||
bool success;
|
||||
uint32_t client_id;
|
||||
/* * Echoed host timestamp from goTool request. */
|
||||
uint64_t timestamp_us;
|
||||
/* * esp_timer_get_time() delta from ping send to pong recv (master→slave→master). */
|
||||
uint32_t esp_rtt_us;
|
||||
} alox_EspNowEchoPingResponse;
|
||||
|
||||
/* Host → master: LED ring on master (client_id=0) and/or slaves via ESP-NOW.
|
||||
mode: 0=clear, 1=progress (0–100 %), 2=digit (0–10), 3=blink, 4=find-me, 5=all LEDs solid color. */
|
||||
typedef struct _alox_LedRingProgressRequest {
|
||||
@@ -371,6 +389,8 @@ typedef struct _alox_UartMessage {
|
||||
alox_TapNotifyResponse tap_notify_response;
|
||||
alox_CacheStatusRequest cache_status_request;
|
||||
alox_CacheStatusResponse cache_status_response;
|
||||
alox_EspNowEchoPingRequest espnow_echo_ping_request;
|
||||
alox_EspNowEchoPingResponse espnow_echo_ping_response;
|
||||
} payload;
|
||||
} alox_UartMessage;
|
||||
|
||||
@@ -381,8 +401,8 @@ extern "C" {
|
||||
|
||||
/* Helper constants for enums */
|
||||
#define _alox_MessageType_MIN alox_MessageType_UNKNOWN
|
||||
#define _alox_MessageType_MAX alox_MessageType_CACHE_STATUS
|
||||
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_CACHE_STATUS+1))
|
||||
#define _alox_MessageType_MAX alox_MessageType_ESPNOW_ECHO_PING
|
||||
#define _alox_MessageType_ARRAYSIZE ((alox_MessageType)(alox_MessageType_ESPNOW_ECHO_PING+1))
|
||||
|
||||
#define _alox_TapKind_MIN alox_TapKind_TAP_NONE
|
||||
#define _alox_TapKind_MAX alox_TapKind_TAP_TRIPLE
|
||||
@@ -431,6 +451,8 @@ extern "C" {
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
/* Initializer values for message structs */
|
||||
#define alox_UartMessage_init_default {_alox_MessageType_MIN, 0, {alox_Ack_init_default}}
|
||||
@@ -460,6 +482,8 @@ extern "C" {
|
||||
#define alox_CacheStatusResponse_init_default {0, {alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default, alox_CacheClientStatus_init_default}}
|
||||
#define alox_EspNowUnicastTestRequest_init_default {0, 0}
|
||||
#define alox_EspNowUnicastTestResponse_init_default {0, 0}
|
||||
#define alox_EspNowEchoPingRequest_init_default {0, 0}
|
||||
#define alox_EspNowEchoPingResponse_init_default {0, 0, 0, 0}
|
||||
#define alox_LedRingProgressRequest_init_default {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||
#define alox_LedRingProgressResponse_init_default {0, 0, 0, 0, 0, 0}
|
||||
#define alox_EspNowFindMeRequest_init_default {0}
|
||||
@@ -500,6 +524,8 @@ extern "C" {
|
||||
#define alox_CacheStatusResponse_init_zero {0, {alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero, alox_CacheClientStatus_init_zero}}
|
||||
#define alox_EspNowUnicastTestRequest_init_zero {0, 0}
|
||||
#define alox_EspNowUnicastTestResponse_init_zero {0, 0}
|
||||
#define alox_EspNowEchoPingRequest_init_zero {0, 0}
|
||||
#define alox_EspNowEchoPingResponse_init_zero {0, 0, 0, 0}
|
||||
#define alox_LedRingProgressRequest_init_zero {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
|
||||
#define alox_LedRingProgressResponse_init_zero {0, 0, 0, 0, 0, 0}
|
||||
#define alox_EspNowFindMeRequest_init_zero {0}
|
||||
@@ -599,6 +625,12 @@ extern "C" {
|
||||
#define alox_EspNowUnicastTestRequest_seq_tag 2
|
||||
#define alox_EspNowUnicastTestResponse_success_tag 1
|
||||
#define alox_EspNowUnicastTestResponse_seq_tag 2
|
||||
#define alox_EspNowEchoPingRequest_client_id_tag 1
|
||||
#define alox_EspNowEchoPingRequest_timestamp_us_tag 2
|
||||
#define alox_EspNowEchoPingResponse_success_tag 1
|
||||
#define alox_EspNowEchoPingResponse_client_id_tag 2
|
||||
#define alox_EspNowEchoPingResponse_timestamp_us_tag 3
|
||||
#define alox_EspNowEchoPingResponse_esp_rtt_us_tag 4
|
||||
#define alox_LedRingProgressRequest_mode_tag 1
|
||||
#define alox_LedRingProgressRequest_progress_tag 2
|
||||
#define alox_LedRingProgressRequest_digit_tag 3
|
||||
@@ -671,6 +703,8 @@ extern "C" {
|
||||
#define alox_UartMessage_tap_notify_response_tag 30
|
||||
#define alox_UartMessage_cache_status_request_tag 33
|
||||
#define alox_UartMessage_cache_status_response_tag 34
|
||||
#define alox_UartMessage_espnow_echo_ping_request_tag 35
|
||||
#define alox_UartMessage_espnow_echo_ping_response_tag 36
|
||||
|
||||
/* Struct field encoding specification for nanopb */
|
||||
#define alox_UartMessage_FIELDLIST(X, a) \
|
||||
@@ -703,7 +737,9 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,battery_status_response,payload.batt
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_notify_request,payload.tap_notify_request), 29) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,tap_notify_response,payload.tap_notify_response), 30) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,cache_status_request,payload.cache_status_request), 33) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,cache_status_response,payload.cache_status_response), 34)
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,cache_status_response,payload.cache_status_response), 34) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_echo_ping_request,payload.espnow_echo_ping_request), 35) \
|
||||
X(a, STATIC, ONEOF, MESSAGE, (payload,espnow_echo_ping_response,payload.espnow_echo_ping_response), 36)
|
||||
#define alox_UartMessage_CALLBACK NULL
|
||||
#define alox_UartMessage_DEFAULT NULL
|
||||
#define alox_UartMessage_payload_ack_payload_MSGTYPE alox_Ack
|
||||
@@ -735,6 +771,8 @@ X(a, STATIC, ONEOF, MESSAGE, (payload,cache_status_response,payload.cache_
|
||||
#define alox_UartMessage_payload_tap_notify_response_MSGTYPE alox_TapNotifyResponse
|
||||
#define alox_UartMessage_payload_cache_status_request_MSGTYPE alox_CacheStatusRequest
|
||||
#define alox_UartMessage_payload_cache_status_response_MSGTYPE alox_CacheStatusResponse
|
||||
#define alox_UartMessage_payload_espnow_echo_ping_request_MSGTYPE alox_EspNowEchoPingRequest
|
||||
#define alox_UartMessage_payload_espnow_echo_ping_response_MSGTYPE alox_EspNowEchoPingResponse
|
||||
|
||||
#define alox_Ack_FIELDLIST(X, a) \
|
||||
|
||||
@@ -934,6 +972,20 @@ X(a, STATIC, SINGULAR, UINT32, seq, 2)
|
||||
#define alox_EspNowUnicastTestResponse_CALLBACK NULL
|
||||
#define alox_EspNowUnicastTestResponse_DEFAULT NULL
|
||||
|
||||
#define alox_EspNowEchoPingRequest_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT32, client_id, 1) \
|
||||
X(a, STATIC, SINGULAR, UINT64, timestamp_us, 2)
|
||||
#define alox_EspNowEchoPingRequest_CALLBACK NULL
|
||||
#define alox_EspNowEchoPingRequest_DEFAULT NULL
|
||||
|
||||
#define alox_EspNowEchoPingResponse_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, BOOL, success, 1) \
|
||||
X(a, STATIC, SINGULAR, UINT32, client_id, 2) \
|
||||
X(a, STATIC, SINGULAR, UINT64, timestamp_us, 3) \
|
||||
X(a, STATIC, SINGULAR, UINT32, esp_rtt_us, 4)
|
||||
#define alox_EspNowEchoPingResponse_CALLBACK NULL
|
||||
#define alox_EspNowEchoPingResponse_DEFAULT NULL
|
||||
|
||||
#define alox_LedRingProgressRequest_FIELDLIST(X, a) \
|
||||
X(a, STATIC, SINGULAR, UINT32, mode, 1) \
|
||||
X(a, STATIC, SINGULAR, UINT32, progress, 2) \
|
||||
@@ -1057,6 +1109,8 @@ extern const pb_msgdesc_t alox_CacheClientStatus_msg;
|
||||
extern const pb_msgdesc_t alox_CacheStatusResponse_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowUnicastTestRequest_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowUnicastTestResponse_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowEchoPingRequest_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowEchoPingResponse_msg;
|
||||
extern const pb_msgdesc_t alox_LedRingProgressRequest_msg;
|
||||
extern const pb_msgdesc_t alox_LedRingProgressResponse_msg;
|
||||
extern const pb_msgdesc_t alox_EspNowFindMeRequest_msg;
|
||||
@@ -1099,6 +1153,8 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
|
||||
#define alox_CacheStatusResponse_fields &alox_CacheStatusResponse_msg
|
||||
#define alox_EspNowUnicastTestRequest_fields &alox_EspNowUnicastTestRequest_msg
|
||||
#define alox_EspNowUnicastTestResponse_fields &alox_EspNowUnicastTestResponse_msg
|
||||
#define alox_EspNowEchoPingRequest_fields &alox_EspNowEchoPingRequest_msg
|
||||
#define alox_EspNowEchoPingResponse_fields &alox_EspNowEchoPingResponse_msg
|
||||
#define alox_LedRingProgressRequest_fields &alox_LedRingProgressRequest_msg
|
||||
#define alox_LedRingProgressResponse_fields &alox_LedRingProgressResponse_msg
|
||||
#define alox_EspNowFindMeRequest_fields &alox_EspNowFindMeRequest_msg
|
||||
@@ -1136,6 +1192,8 @@ extern const pb_msgdesc_t alox_OtaSlaveProgressResponse_msg;
|
||||
#define alox_CacheStatusRequest_size 0
|
||||
#define alox_CacheStatusResponse_size 736
|
||||
#define alox_ClientInput_size 22
|
||||
#define alox_EspNowEchoPingRequest_size 17
|
||||
#define alox_EspNowEchoPingResponse_size 25
|
||||
#define alox_EspNowFindMeRequest_size 6
|
||||
#define alox_EspNowFindMeResponse_size 8
|
||||
#define alox_EspNowUnicastTestRequest_size 12
|
||||
|
||||
@@ -29,6 +29,8 @@ enum MessageType {
|
||||
reserved 28;
|
||||
/** Combined cached accel + tap poll (one UART round-trip, ~16 ms cadence). */
|
||||
CACHE_STATUS = 29;
|
||||
/** Host → master → slave → master: timestamp echo round-trip (latency test). */
|
||||
ESPNOW_ECHO_PING = 30;
|
||||
}
|
||||
|
||||
message UartMessage {
|
||||
@@ -63,6 +65,8 @@ message UartMessage {
|
||||
TapNotifyResponse tap_notify_response = 30;
|
||||
CacheStatusRequest cache_status_request = 33;
|
||||
CacheStatusResponse cache_status_response = 34;
|
||||
EspNowEchoPingRequest espnow_echo_ping_request = 35;
|
||||
EspNowEchoPingResponse espnow_echo_ping_response = 36;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -255,6 +259,22 @@ message EspNowUnicastTestResponse {
|
||||
uint32 seq = 2;
|
||||
}
|
||||
|
||||
/** Host → master: ESP-NOW echo ping to one slave (timestamp echoed back). */
|
||||
message EspNowEchoPingRequest {
|
||||
uint32 client_id = 1;
|
||||
/** Microseconds since Unix epoch (host clock). */
|
||||
uint64 timestamp_us = 2;
|
||||
}
|
||||
|
||||
message EspNowEchoPingResponse {
|
||||
bool success = 1;
|
||||
uint32 client_id = 2;
|
||||
/** Echoed host timestamp from goTool request. */
|
||||
uint64 timestamp_us = 3;
|
||||
/** esp_timer_get_time() delta from ping send to pong recv (master→slave→master). */
|
||||
uint32 esp_rtt_us = 4;
|
||||
}
|
||||
|
||||
// Host → master: LED ring on master (client_id=0) and/or slaves via ESP-NOW.
|
||||
// mode: 0=clear, 1=progress (0–100 %), 2=digit (0–10), 3=blink, 4=find-me, 5=all LEDs solid color.
|
||||
message LedRingProgressRequest {
|
||||
|
||||
+2
-1
@@ -52,7 +52,8 @@ void init_uart(QueueHandle_t cmd_queue) {
|
||||
.flow_ctrl = UART_HW_FLOWCTRL_DISABLE,
|
||||
};
|
||||
|
||||
err = uart_driver_install(UART_NUM, UART_BUF_SIZE * 2, UART_BUF_SIZE, 0, NULL, 0);
|
||||
err = uart_driver_install(UART_NUM, UART_DRIVER_RX_BUF_SIZE,
|
||||
UART_DRIVER_TX_BUF_SIZE, 0, NULL, 0);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "uart_driver_install failed: %s", esp_err_to_name(err));
|
||||
return;
|
||||
|
||||
+4
-1
@@ -16,7 +16,10 @@
|
||||
#define UART_RXD_PIN 3
|
||||
|
||||
|
||||
#define UART_BUF_SIZE 2048
|
||||
#define UART_BUF_SIZE 4096
|
||||
/** Driver RX ring — must hold a full OTA block burst (~20 × ~215 B frames). */
|
||||
#define UART_DRIVER_RX_BUF_SIZE 16384
|
||||
#define UART_DRIVER_TX_BUF_SIZE 4096
|
||||
#define START_MARKER 0xAA
|
||||
#define STOP_MARKER 0xCC
|
||||
#define MAX_BUF_SIZE 252
|
||||
|
||||
Reference in New Issue
Block a user