|
| 1 | +#include "radio_ir_protocol_client.h" |
| 2 | +#include <stdbool.h> |
| 3 | +#include <stdint.h> |
| 4 | +#include <string.h> |
| 5 | +#include "esp_err.h" |
| 6 | +#include "esp_log.h" |
| 7 | +#include "freertos/FreeRTOS.h" |
| 8 | + |
| 9 | +#if defined(CONFIG_IDF_TARGET_ESP32P4) |
| 10 | +#include "esp_hosted.h" |
| 11 | + |
| 12 | +static const char TAG[] = "ir client"; |
| 13 | + |
| 14 | +static SemaphoreHandle_t ir_protocol_client_mutex = NULL; |
| 15 | +static SemaphoreHandle_t ir_protocol_client_transaction_semaphore = NULL; |
| 16 | +static uint8_t ir_protocol_client_packet_buffer[512] = {0}; |
| 17 | +static uint32_t ir_protocol_client_packet_size = 0; |
| 18 | +static uint32_t ir_protocol_client_sequence_number = 0; |
| 19 | + |
| 20 | +static esp_err_t ir_protocol_client_transaction(const uint8_t* request, size_t request_length, uint8_t* out_response, |
| 21 | + size_t* response_length, size_t max_response_length) { |
| 22 | + if (!ir_protocol_client_mutex || !ir_protocol_client_transaction_semaphore) { |
| 23 | + ESP_LOGE(TAG, "Invalid state"); |
| 24 | + return ESP_ERR_INVALID_STATE; |
| 25 | + } |
| 26 | + |
| 27 | + esp_err_t result = ESP_FAIL; |
| 28 | + xSemaphoreTake(ir_protocol_client_mutex, portMAX_DELAY); |
| 29 | + xSemaphoreTake(ir_protocol_client_transaction_semaphore, 0); // Clear semaphore |
| 30 | + result = esp_hosted_send_custom_data(0x04, (uint8_t*)request, request_length); |
| 31 | + if (result == ESP_OK) { |
| 32 | + if (xSemaphoreTake(ir_protocol_client_transaction_semaphore, pdMS_TO_TICKS(2000)) == |
| 33 | + pdTRUE) { // Wait for response |
| 34 | + if (ir_protocol_client_packet_size <= max_response_length) { |
| 35 | + memcpy(out_response, ir_protocol_client_packet_buffer, ir_protocol_client_packet_size); |
| 36 | + *response_length = ir_protocol_client_packet_size; |
| 37 | + result = ESP_OK; |
| 38 | + } else { |
| 39 | + ESP_LOGE(TAG, "Invalid size"); |
| 40 | + result = ESP_ERR_INVALID_SIZE; |
| 41 | + } |
| 42 | + } else { |
| 43 | + ESP_LOGE(TAG, "Timeout"); |
| 44 | + result = ESP_ERR_TIMEOUT; |
| 45 | + } |
| 46 | + } |
| 47 | + ir_protocol_client_sequence_number++; |
| 48 | + xSemaphoreGive(ir_protocol_client_mutex); |
| 49 | + return result; |
| 50 | +} |
| 51 | + |
| 52 | +static void ir_protocol_client_transaction_receive(uint32_t msg_id, const uint8_t* packet, size_t length) { |
| 53 | + if (msg_id != 0x04) { |
| 54 | + ESP_LOGW(TAG, "Received IR message with unknown ID: %u", msg_id); |
| 55 | + return; |
| 56 | + } |
| 57 | + |
| 58 | + if (!ir_protocol_client_mutex || !ir_protocol_client_transaction_semaphore) { |
| 59 | + ESP_LOGW(TAG, "Received IR message but client not initialized"); |
| 60 | + return; |
| 61 | + } |
| 62 | + if (length > sizeof(ir_protocol_client_packet_buffer) || length < sizeof(ir_protocol_client_header_t)) { |
| 63 | + ESP_LOGW(TAG, "Received IR message but size incorrect"); |
| 64 | + return; |
| 65 | + } |
| 66 | + memcpy(ir_protocol_client_packet_buffer, packet, length); |
| 67 | + ir_protocol_client_packet_size = length; |
| 68 | + xSemaphoreGive(ir_protocol_client_transaction_semaphore); |
| 69 | +} |
| 70 | + |
| 71 | +static esp_err_t validate_response(const uint8_t* response, size_t response_length, uint32_t expected_seq, |
| 72 | + uint32_t expected_type, size_t min_body_size) { |
| 73 | + if (response_length < sizeof(ir_protocol_client_header_t) + min_body_size) { |
| 74 | + ESP_LOGE(TAG, "Response too short: %zu bytes", response_length); |
| 75 | + return ESP_FAIL; |
| 76 | + } |
| 77 | + const ir_protocol_client_header_t* h = (const ir_protocol_client_header_t*)response; |
| 78 | + if (h->sequence_number != expected_seq) { |
| 79 | + ESP_LOGE(TAG, "Response sequence number mismatch: %u != %u", h->sequence_number, expected_seq); |
| 80 | + return ESP_FAIL; |
| 81 | + } |
| 82 | + if (h->type == IR_PROTOCOL_CLIENT_TYPE_NACK) { |
| 83 | + return ESP_FAIL; |
| 84 | + } |
| 85 | + if (h->type != expected_type) { |
| 86 | + ESP_LOGE(TAG, "Response type mismatch: %u != %u", h->type, expected_type); |
| 87 | + return ESP_FAIL; |
| 88 | + } |
| 89 | + return ESP_OK; |
| 90 | +} |
| 91 | + |
| 92 | +esp_err_t ir_protocol_client_init(void) { |
| 93 | + ir_protocol_client_mutex = xSemaphoreCreateMutex(); |
| 94 | + ir_protocol_client_transaction_semaphore = xSemaphoreCreateBinary(); |
| 95 | + if (ir_protocol_client_mutex == NULL || ir_protocol_client_transaction_semaphore == NULL) { |
| 96 | + if (ir_protocol_client_mutex != NULL) { |
| 97 | + vSemaphoreDelete(ir_protocol_client_mutex); |
| 98 | + } |
| 99 | + if (ir_protocol_client_transaction_semaphore != NULL) { |
| 100 | + vSemaphoreDelete(ir_protocol_client_transaction_semaphore); |
| 101 | + } |
| 102 | + return ESP_ERR_NO_MEM; |
| 103 | + } |
| 104 | + |
| 105 | + esp_hosted_register_custom_callback(0x04, ir_protocol_client_transaction_receive); |
| 106 | + return ESP_OK; |
| 107 | +} |
| 108 | + |
| 109 | +esp_err_t ir_protocol_client_get_supported(void) { |
| 110 | + uint8_t request[sizeof(ir_protocol_client_header_t)] = {0}; |
| 111 | + ir_protocol_client_header_t* header = (ir_protocol_client_header_t*)request; |
| 112 | + header->sequence_number = ir_protocol_client_sequence_number; |
| 113 | + header->type = IR_PROTOCOL_CLIENT_TYPE_GET_SUPPORTED; |
| 114 | + |
| 115 | + uint8_t response[sizeof(ir_protocol_client_header_t)] = {0}; |
| 116 | + size_t response_length = 0; |
| 117 | + esp_err_t result = |
| 118 | + ir_protocol_client_transaction(request, sizeof(request), response, &response_length, sizeof(response)); |
| 119 | + if (result != ESP_OK) return result; |
| 120 | + return validate_response(response, response_length, header->sequence_number, IR_PROTOCOL_CLIENT_TYPE_ACK, 0); |
| 121 | +} |
| 122 | + |
| 123 | +esp_err_t ir_protocol_client_set_config(const ir_protocol_client_config_t* config) { |
| 124 | + if (config == NULL) return ESP_ERR_INVALID_ARG; |
| 125 | + |
| 126 | + uint8_t request[sizeof(ir_protocol_client_header_t) + sizeof(ir_protocol_client_config_t)] = {0}; |
| 127 | + ir_protocol_client_header_t* header = (ir_protocol_client_header_t*)request; |
| 128 | + header->sequence_number = ir_protocol_client_sequence_number; |
| 129 | + header->type = IR_PROTOCOL_CLIENT_TYPE_SET_CONFIG; |
| 130 | + memcpy(request + sizeof(ir_protocol_client_header_t), config, sizeof(ir_protocol_client_config_t)); |
| 131 | + |
| 132 | + uint8_t response[sizeof(ir_protocol_client_header_t)] = {0}; |
| 133 | + size_t response_length = 0; |
| 134 | + esp_err_t result = |
| 135 | + ir_protocol_client_transaction(request, sizeof(request), response, &response_length, sizeof(response)); |
| 136 | + if (result != ESP_OK) return result; |
| 137 | + return validate_response(response, response_length, header->sequence_number, IR_PROTOCOL_CLIENT_TYPE_ACK, 0); |
| 138 | +} |
| 139 | + |
| 140 | +esp_err_t ir_protocol_client_send_nec(const ir_protocol_client_nec_scan_code_t* scan_code) { |
| 141 | + if (scan_code == NULL) return ESP_ERR_INVALID_ARG; |
| 142 | + |
| 143 | + uint8_t request[sizeof(ir_protocol_client_header_t) + sizeof(ir_protocol_client_nec_scan_code_t)] = {0}; |
| 144 | + ir_protocol_client_header_t* header = (ir_protocol_client_header_t*)request; |
| 145 | + header->sequence_number = ir_protocol_client_sequence_number; |
| 146 | + header->type = IR_PROTOCOL_CLIENT_TYPE_SEND_NEC; |
| 147 | + memcpy(request + sizeof(ir_protocol_client_header_t), scan_code, sizeof(ir_protocol_client_nec_scan_code_t)); |
| 148 | + |
| 149 | + uint8_t response[sizeof(ir_protocol_client_header_t)] = {0}; |
| 150 | + size_t response_length = 0; |
| 151 | + esp_err_t result = |
| 152 | + ir_protocol_client_transaction(request, sizeof(request), response, &response_length, sizeof(response)); |
| 153 | + if (result != ESP_OK) return result; |
| 154 | + return validate_response(response, response_length, header->sequence_number, IR_PROTOCOL_CLIENT_TYPE_ACK, 0); |
| 155 | +} |
| 156 | + |
| 157 | +#endif |
0 commit comments