powerpods/main/cmd/cmd_espnow_find_me.c
simon 16c521f71c Move UART command handlers into main/cmd/ for clearer layout.
Add cmd/ to CMake include paths and update documentation paths.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-05-19 22:49:11 +02:00

65 lines
1.8 KiB
C

#include "client_registry.h"
#include "cmd_espnow_find_me.h"
#include "esp_log.h"
#include "esp_now_comm.h"
#include "led_ring.h"
#include "uart_cmd.h"
static const char *TAG = "[FIND_ME]";
static void reply(bool success, uint32_t client_id) {
alox_UartMessage response;
uart_cmd_init_response(&response, alox_MessageType_FIND_ME,
alox_UartMessage_espnow_find_me_response_tag);
response.payload.espnow_find_me_response.success = success;
response.payload.espnow_find_me_response.client_id = client_id;
uart_cmd_send(&response, TAG);
}
static void handle_find_me(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);
return;
}
const alox_EspNowFindMeRequest *req = UART_CMD_REQ(
&uart_msg, alox_UartMessage_espnow_find_me_request_tag,
espnow_find_me_request);
if (req == NULL) {
ESP_LOGW(TAG, "missing find_me request");
reply(false, 0);
return;
}
if (req->client_id == 0) {
led_ring_find_me();
ESP_LOGI(TAG, "find-me on master");
reply(true, 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);
return;
}
esp_err_t err = esp_now_comm_send_find_me(client->mac, req->client_id);
if (err == ESP_OK) {
ESP_LOGI(TAG, "find-me sent to slave %lu", (unsigned long)req->client_id);
} else {
ESP_LOGW(TAG, "find-me to slave %lu failed: %s",
(unsigned long)req->client_id, esp_err_to_name(err));
}
reply(err == ESP_OK, req->client_id);
}
void cmd_espnow_find_me_register(void) {
uart_cmd_register(alox_MessageType_FIND_ME, handle_find_me);
}