#include "client_registry.h" #include "cmd_espnow_unicast_test.h" #include "cmd_handler.h" #include "esp_log.h" #include "esp_now_comm.h" #include "pb_decode.h" #include "uart_messages.pb.h" #include "uart_proto.h" static const char *TAG = "[UNICAST_TEST]"; static void send_response(bool success, uint32_t seq) { alox_UartMessage response = alox_UartMessage_init_zero; response.type = alox_MessageType_ESPNOW_UNICAST_TEST; response.which_payload = alox_UartMessage_espnow_unicast_test_response_tag; response.payload.espnow_unicast_test_response.success = success; response.payload.espnow_unicast_test_response.seq = seq; if (uart_send_uart_message(&response) != ESP_OK) { ESP_LOGE(TAG, "failed to send response"); } } static void handle_espnow_unicast_test(const uint8_t *data, size_t len) { alox_UartMessage uart_msg = alox_UartMessage_init_zero; alox_EspNowUnicastTestRequest req = alox_EspNowUnicastTestRequest_init_zero; bool have_request = false; if (len > 0) { pb_istream_t stream = pb_istream_from_buffer(data, len); if (!pb_decode(&stream, alox_UartMessage_fields, &uart_msg)) { ESP_LOGW(TAG, "decode failed"); send_response(false, 0); return; } if (uart_msg.which_payload == alox_UartMessage_espnow_unicast_test_request_tag) { req = uart_msg.payload.espnow_unicast_test_request; have_request = true; } } if (!have_request || req.client_id == 0) { ESP_LOGW(TAG, "need client_id in request"); send_response(false, 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); send_response(false, req.seq); return; } esp_err_t err = esp_now_comm_send_unicast_test(client->mac, req.seq); send_response(err == ESP_OK, req.seq); } void cmd_espnow_unicast_test_register(void) { if (msg_register_handler(alox_MessageType_ESPNOW_UNICAST_TEST, handle_espnow_unicast_test) != ESP_OK) { ESP_LOGE(TAG, "register failed"); } }