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>
This commit is contained in:
@@ -0,0 +1,63 @@
|
||||
#include "client_registry.h"
|
||||
#include "cmd_restart.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_now_comm.h"
|
||||
#include "pod_reboot.h"
|
||||
#include "uart_cmd.h"
|
||||
|
||||
static const char *TAG = "[RESTART_CMD]";
|
||||
|
||||
static void reply(bool success, uint32_t client_id) {
|
||||
alox_UartMessage response;
|
||||
uart_cmd_init_response(&response, alox_MessageType_RESTART,
|
||||
alox_UartMessage_restart_response_tag);
|
||||
response.payload.restart_response.success = success;
|
||||
response.payload.restart_response.client_id = client_id;
|
||||
uart_cmd_send(&response, TAG);
|
||||
}
|
||||
|
||||
static void handle_restart(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_RestartRequest *req = UART_CMD_REQ(
|
||||
&uart_msg, alox_UartMessage_restart_request_tag, restart_request);
|
||||
if (req == NULL) {
|
||||
ESP_LOGW(TAG, "missing restart request");
|
||||
reply(false, 0);
|
||||
return;
|
||||
}
|
||||
|
||||
if (req->client_id == 0) {
|
||||
ESP_LOGI(TAG, "restart master");
|
||||
reply(true, 0);
|
||||
pod_schedule_restart();
|
||||
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_restart(client->mac, req->client_id);
|
||||
if (err == ESP_OK) {
|
||||
ESP_LOGI(TAG, "restart sent to slave %lu", (unsigned long)req->client_id);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "restart to slave %lu failed: %s",
|
||||
(unsigned long)req->client_id, esp_err_to_name(err));
|
||||
}
|
||||
reply(err == ESP_OK, req->client_id);
|
||||
}
|
||||
|
||||
void cmd_restart_register(void) {
|
||||
uart_cmd_register(alox_MessageType_RESTART, handle_restart);
|
||||
}
|
||||
Reference in New Issue
Block a user