From 25744de46c455994db9af4b64fbb7528540c2723 Mon Sep 17 00:00:00 2001 From: PlxEV Date: Sun, 8 Jun 2025 10:11:09 +0100 Subject: [PATCH] refact auth 2 --- components/auth/include/auth.h | 47 +++++++++++++++++++++++----- components/auth/src/auth.c | 56 ++++++++++++++++++++++++++++++---- 2 files changed, 90 insertions(+), 13 deletions(-) diff --git a/components/auth/include/auth.h b/components/auth/include/auth.h index 3208eaf..55da6f0 100755 --- a/components/auth/include/auth.h +++ b/components/auth/include/auth.h @@ -9,28 +9,61 @@ extern "C" { #endif +// Tamanho máximo da tag RFID (incluindo '\0') #define AUTH_TAG_MAX_LEN 20 +// Evento enviado ao EVSE Manager após leitura de tag typedef struct { - char tag[AUTH_TAG_MAX_LEN]; - bool authorized; + char tag[AUTH_TAG_MAX_LEN]; // Tag lida + bool authorized; // true se tag for válida } auth_event_t; -// Inicializa autenticação (ex: tabela de tags) +/** + * @brief Inicializa o sistema de autenticação. + * Carrega configuração e inicia o leitor Wiegand (wg26). + */ void auth_init(void); -// Ativa ou desativa o sistema de autenticação +/** + * @brief Define a fila de eventos que receberá auth_event_t. + */ +void auth_set_event_queue(QueueHandle_t queue); + +/** + * @brief Ativa ou desativa o módulo de autenticação (RFID). + * Essa configuração é salva em NVS. + */ void auth_set_enabled(bool value); + +/** + * @brief Verifica se a autenticação está habilitada. + */ bool auth_is_enabled(void); -// Gerenciamento de tags +/** + * @brief Adiciona uma nova tag válida. + */ bool auth_add_tag(const char *tag); + +/** + * @brief Remove uma tag previamente cadastrada. + */ bool auth_remove_tag(const char *tag); + +/** + * @brief Verifica se uma tag está cadastrada. + */ bool auth_tag_exists(const char *tag); + +/** + * @brief Lista as tags registradas (via ESP_LOG). + */ void auth_list_tags(void); -// Integração com fila de eventos -void auth_set_event_queue(QueueHandle_t queue); +/** + * @brief Processa uma tag lida (usado pelo leitor Wiegand). + */ +void auth_process_tag(const char *tag); #ifdef __cplusplus } diff --git a/components/auth/src/auth.c b/components/auth/src/auth.c index 1f1ed5d..04b6c08 100755 --- a/components/auth/src/auth.c +++ b/components/auth/src/auth.c @@ -8,7 +8,10 @@ #include #include #include -#include "wiegand_reader.h" // necessário para initWiegand() +#include "wiegand_reader.h" + +#include "nvs_flash.h" +#include "nvs.h" #define MAX_TAGS 50 @@ -17,13 +20,44 @@ static bool enabled = true; static char valid_tags[MAX_TAGS][AUTH_TAG_MAX_LEN]; static int tag_count = 0; -// Fila para enviar eventos de autenticação +// Fila de eventos enviada ao EVSE Manager static QueueHandle_t event_queue = NULL; -void auth_set_event_queue(QueueHandle_t queue) { - event_queue = queue; +// =========================== +// Persistência em NVS +// =========================== + +static void load_auth_config(void) { + nvs_handle_t handle; + esp_err_t err = nvs_open("auth", NVS_READONLY, &handle); + if (err == ESP_OK) { + uint8_t val; + if (nvs_get_u8(handle, "enabled", &val) == ESP_OK) { + enabled = val; + ESP_LOGI(TAG, "Loaded auth enabled = %d", enabled); + } + nvs_close(handle); + } else { + ESP_LOGW(TAG, "No stored auth config found. Using default."); + } } +static void save_auth_config(void) { + nvs_handle_t handle; + if (nvs_open("auth", NVS_READWRITE, &handle) == ESP_OK) { + nvs_set_u8(handle, "enabled", enabled); + nvs_commit(handle); + nvs_close(handle); + ESP_LOGI(TAG, "Auth config saved: enabled = %d", enabled); + } else { + ESP_LOGE(TAG, "Failed to save auth config."); + } +} + +// =========================== +// Internos +// =========================== + static bool is_tag_valid(const char *tag) { for (int i = 0; i < tag_count; i++) { if (strncmp(valid_tags[i], tag, AUTH_TAG_MAX_LEN) == 0) { @@ -33,8 +67,17 @@ static bool is_tag_valid(const char *tag) { return false; } +// =========================== +// API pública +// =========================== + +void auth_set_event_queue(QueueHandle_t queue) { + event_queue = queue; +} + void auth_set_enabled(bool value) { enabled = value; + save_auth_config(); ESP_LOGI(TAG, "Auth %s", enabled ? "ENABLED" : "DISABLED"); } @@ -80,10 +123,11 @@ void auth_list_tags(void) { } void auth_init(void) { - auth_set_enabled(true); // Default: enabled - initWiegand(); // Inicializa automaticamente o leitor Wiegand 26 + load_auth_config(); // carrega estado de ativação + initWiegand(); // inicia leitor RFID } +// Processa uma tag lida (chamada pelo leitor) void auth_process_tag(const char *tag) { if (!tag || !auth_is_enabled()) { ESP_LOGW(TAG, "Auth disabled or NULL tag received.");