#include "cmd_input_send_mode.h" #include "client_registry.h" #include "esp_log.h" #include "esp_now_comm.h" #include "uart_cmd.h" static const char *TAG = "[INPUT_SEND]"; /** Last mode pushed from host (GET returns this; slaves may lag until re-push). */ static alox_InputSendMode s_mode = alox_InputSendMode_INPUT_SEND_BEST_EFFORT; static bool valid_mode(alox_InputSendMode mode) { return mode <= alox_InputSendMode_INPUT_SEND_RELIABLE; } static void reply(bool success, alox_InputSendMode mode, uint32_t client_id, uint32_t slaves_updated) { alox_UartMessage response; uart_cmd_init_response(&response, alox_MessageType_INPUT_SEND_MODE, alox_UartMessage_input_send_mode_response_tag); response.payload.input_send_mode_response.success = success; response.payload.input_send_mode_response.mode = mode; response.payload.input_send_mode_response.client_id = client_id; response.payload.input_send_mode_response.slaves_updated = slaves_updated; uart_cmd_send(&response, TAG); } static esp_err_t push_mode_to_slave(const client_info_t *client, alox_InputSendMode mode) { if (client == NULL) { return ESP_ERR_INVALID_ARG; } return esp_now_comm_send_input_send_mode(client->mac, client->id, (uint32_t)mode); } static void handle_input_send_mode(const uint8_t *data, size_t len) { alox_UartMessage uart_msg; alox_InputSendModeRequest req = alox_InputSendModeRequest_init_zero; if (len > 0) { if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) { ESP_LOGW(TAG, "decode failed"); reply(false, s_mode, 0, 0); return; } const alox_InputSendModeRequest *req_ptr = UART_CMD_REQ(&uart_msg, alox_UartMessage_input_send_mode_request_tag, input_send_mode_request); if (req_ptr == NULL) { ESP_LOGW(TAG, "missing input_send_mode_request"); reply(false, s_mode, 0, 0); return; } req = *req_ptr; } if (req.write) { if (!valid_mode(req.mode)) { ESP_LOGW(TAG, "invalid mode %lu", (unsigned long)req.mode); reply(false, s_mode, req.client_id, 0); return; } if (req.all_clients) { uint32_t sent = 0; for (size_t i = 0; i < client_registry_count(); i++) { const client_info_t *client = client_registry_at(i); if (client == NULL) { continue; } if (push_mode_to_slave(client, req.mode) == ESP_OK) { sent++; } } s_mode = req.mode; ESP_LOGI(TAG, "input send mode %lu for %u slaves", (unsigned long)req.mode, (unsigned)sent); reply(sent > 0 || client_registry_count() == 0, req.mode, 0, sent); return; } if (req.client_id == 0) { ESP_LOGW(TAG, "client_id required (or all_clients)"); reply(false, req.mode, 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 found", (unsigned long)req.client_id); reply(false, req.mode, req.client_id, 0); return; } esp_err_t err = push_mode_to_slave(client, req.mode); if (err == ESP_OK) { s_mode = req.mode; } reply(err == ESP_OK, req.mode, req.client_id, err == ESP_OK ? 1u : 0u); return; } reply(true, s_mode, 0, 0); } void cmd_input_send_mode_register(void) { uart_cmd_register(alox_MessageType_INPUT_SEND_MODE, handle_input_send_mode); }