#include "client_registry.h" #include "cmd_tap_notify.h" #include "esp_log.h" #include "esp_now_comm.h" #include "uart_cmd.h" static const char *TAG = "[TAP_NOTIFY]"; static void reply(uint32_t client_id, bool success, uint32_t slaves_updated, bool single, bool double_tap, bool triple) { alox_UartMessage response; uart_cmd_init_response(&response, alox_MessageType_TAP_NOTIFY, alox_UartMessage_tap_notify_response_tag); response.payload.tap_notify_response.client_id = client_id; response.payload.tap_notify_response.success = success; response.payload.tap_notify_response.slaves_updated = slaves_updated; response.payload.tap_notify_response.single = single; response.payload.tap_notify_response.double_tap = double_tap; response.payload.tap_notify_response.triple = triple; uart_cmd_send(&response, TAG); } static esp_err_t push_tap_notify_to_slave(const client_info_t *client, bool single, bool double_tap, bool triple) { if (client == NULL) { return ESP_ERR_INVALID_ARG; } esp_err_t err = client_registry_set_tap_notify(client->id, single, double_tap, triple); if (err != ESP_OK) { return err; } return esp_now_comm_send_tap_notify(client->mac, client->id, single, double_tap, triple); } static void handle_tap_notify(const uint8_t *data, size_t len) { alox_UartMessage uart_msg; alox_TapNotifyRequest req = alox_TapNotifyRequest_init_zero; if (len > 0) { if (uart_cmd_decode(data, len, &uart_msg) != ESP_OK) { ESP_LOGW(TAG, "decode failed"); reply(0, false, 0, false, false, false); return; } const alox_TapNotifyRequest *req_ptr = UART_CMD_REQ( &uart_msg, alox_UartMessage_tap_notify_request_tag, tap_notify_request); if (req_ptr == NULL) { ESP_LOGW(TAG, "missing tap_notify_request"); reply(0, false, 0, false, false, false); return; } req = *req_ptr; } if (req.write) { if (req.all_clients) { size_t n = client_registry_set_tap_notify_all(req.single, req.double_tap, req.triple); 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 (esp_now_comm_send_tap_notify(client->mac, client->id, req.single, req.double_tap, req.triple) == ESP_OK) { sent++; } } ESP_LOGI(TAG, "tap notify single=%d double=%d triple=%d for %u/%u slaves", req.single, req.double_tap, req.triple, (unsigned)sent, (unsigned)n); reply(0, sent > 0, sent, req.single, req.double_tap, req.triple); return; } if (req.client_id == 0) { ESP_LOGW(TAG, "client_id required (or all_clients)"); reply(0, false, 0, req.single, req.double_tap, req.triple); 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(req.client_id, false, 0, req.single, req.double_tap, req.triple); return; } esp_err_t err = push_tap_notify_to_slave(client, req.single, req.double_tap, req.triple); reply(req.client_id, err == ESP_OK, err == ESP_OK ? 1u : 0u, req.single, req.double_tap, req.triple); return; } if (req.all_clients || req.client_id == 0) { reply(0, false, 0, false, false, false); return; } bool single = false; bool double_tap = false; bool triple = false; esp_err_t err = client_registry_get_tap_notify(req.client_id, &single, &double_tap, &triple); reply(req.client_id, err == ESP_OK, 0, single, double_tap, triple); } void cmd_tap_notify_register(void) { uart_cmd_register(alox_MessageType_TAP_NOTIFY, handle_tap_notify); }