new meter

This commit is contained in:
2025-06-14 10:27:29 +01:00
parent 4892718736
commit 6f95c7ba59
228 changed files with 3178 additions and 3115 deletions

View File

@@ -1,4 +1,4 @@
set(srcs "src/auth.c" "src/wiegand.c" "src/wiegand_reader.c")
set(srcs "src/auth.c" "src/wiegand.c" "src/wiegand_reader.c" "src/auth_events.c")
idf_component_register(SRCS "${srcs}"
INCLUDE_DIRS "include"

View File

@@ -3,68 +3,80 @@
#include <stdbool.h>
#include <freertos/FreeRTOS.h>
#include <freertos/queue.h>
#ifdef __cplusplus
extern "C" {
#endif
// Tamanho máximo da tag RFID (incluindo '\0')
/// Tamanho máximo de uma tag RFID (incluindo '\0')
#define AUTH_TAG_MAX_LEN 20
// Evento enviado ao EVSE Manager após leitura de tag
/// Estrutura de evento emitida após leitura de uma tag
typedef struct {
char tag[AUTH_TAG_MAX_LEN]; // Tag lida
bool authorized; // true se tag for válida
char tag[AUTH_TAG_MAX_LEN]; ///< Tag lida
bool authorized; ///< true se a tag for reconhecida como válida
} auth_event_t;
/**
* @brief Inicializa o sistema de autenticação.
* Carrega configuração e inicia o leitor Wiegand (wg26).
*
* - Carrega a configuração (enabled) da NVS
* - Inicia o leitor Wiegand
* - Emite evento AUTH_EVENT_INIT com estado atual
*/
void auth_init(void);
/**
* @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.
* @brief Ativa ou desativa o uso de autenticação via RFID.
*
* Esta configuração é persistida em NVS. Se desativado, o sistema
* considerará todas as autorizações como aceitas.
*
* @param value true para ativar, false para desativar
*/
void auth_set_enabled(bool value);
/**
* @brief Verifica se a autenticação está habilitada.
* @brief Verifica se o sistema de autenticação está habilitado.
*/
bool auth_is_enabled(void);
/**
* @brief Adiciona uma nova tag válida.
* @brief Adiciona uma nova tag RFID à lista de autorizadas.
*
* @param tag String da tag (máx AUTH_TAG_MAX_LEN-1)
* @return true se a tag foi adicionada, false se já existia ou inválida
*/
bool auth_add_tag(const char *tag);
/**
* @brief Remove uma tag previamente cadastrada.
*
* @param tag String da tag
* @return true se foi removida, false se não encontrada
*/
bool auth_remove_tag(const char *tag);
/**
* @brief Verifica se uma tag está cadastrada.
* @brief Verifica se uma tag está registrada como válida.
*/
bool auth_tag_exists(const char *tag);
/**
* @brief Lista as tags registradas (via ESP_LOG).
* @brief Lista todas as tags válidas atualmente registradas (via logs).
*/
void auth_list_tags(void);
/**
* @brief Processa uma tag lida (usado pelo leitor Wiegand).
* @brief Processa uma tag RFID lida (chamada normalmente pelo leitor).
*
* - Verifica validade
* - Emite evento AUTH_EVENT_TAG_PROCESSED
* - Inicia timer de expiração se autorizada
*/
void auth_process_tag(const char *tag);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,21 @@
#pragma once
#include "esp_event.h"
#define AUTH_EVENT_TAG_MAX_LEN 32
ESP_EVENT_DECLARE_BASE(AUTH_EVENTS);
typedef enum {
AUTH_EVENT_TAG_PROCESSED,
AUTH_EVENT_ENABLED_CHANGED,
AUTH_EVENT_INIT,
} auth_event_id_t;
typedef struct {
char tag[AUTH_EVENT_TAG_MAX_LEN];
bool authorized;
} auth_tag_event_data_t;
typedef struct {
bool enabled;
} auth_enabled_event_data_t;

View File

@@ -3,26 +3,25 @@
*/
#include "auth.h"
#include "auth_events.h"
#include "esp_event.h"
#include <freertos/FreeRTOS.h>
#include <freertos/task.h>
#include <freertos/queue.h>
#include <esp_log.h>
#include <string.h>
#include "wiegand_reader.h"
#include "nvs_flash.h"
#include "nvs.h"
#define MAX_TAGS 50
static const char *TAG = "Auth";
static bool enabled = true;
static bool enabled = false;
static char valid_tags[MAX_TAGS][AUTH_TAG_MAX_LEN];
static int tag_count = 0;
// Fila de eventos enviada ao EVSE Manager
static QueueHandle_t event_queue = NULL;
// ===========================
// Persistência em NVS
// ===========================
@@ -64,21 +63,22 @@ static bool is_tag_valid(const char *tag) {
return true;
}
}
return false;
return true;
//TODO
//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");
auth_enabled_event_data_t event = { .enabled = enabled };
esp_event_post(AUTH_EVENTS, AUTH_EVENT_ENABLED_CHANGED, &event, sizeof(event), portMAX_DELAY);
}
bool auth_is_enabled(void) {
@@ -124,28 +124,32 @@ void auth_list_tags(void) {
void auth_init(void) {
load_auth_config(); // carrega estado de ativação
initWiegand(); // inicia leitor RFID
if (enabled) {
initWiegand(); // só inicia se estiver habilitado
ESP_LOGI(TAG, "Wiegand reader initialized (Auth enabled)");
} else {
ESP_LOGI(TAG, "Auth disabled, Wiegand reader not started");
}
auth_enabled_event_data_t evt = { .enabled = enabled };
esp_event_post(AUTH_EVENTS, AUTH_EVENT_INIT, &evt, sizeof(evt), portMAX_DELAY);
ESP_LOGI(TAG, "Estado inicial AUTH enviado (enabled = %d)", enabled);
}
// 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.");
return;
}
auth_event_t event;
strncpy(event.tag, tag, AUTH_TAG_MAX_LEN - 1);
event.tag[AUTH_TAG_MAX_LEN - 1] = '\0';
auth_tag_event_data_t event;
strncpy(event.tag, tag, AUTH_EVENT_TAG_MAX_LEN - 1);
event.tag[AUTH_EVENT_TAG_MAX_LEN - 1] = '\0';
event.authorized = is_tag_valid(tag);
ESP_LOGI(TAG, "Tag %s: %s", tag, event.authorized ? "AUTHORIZED" : "DENIED");
if (event_queue) {
if (xQueueSend(event_queue, &event, pdMS_TO_TICKS(100)) != pdPASS) {
ESP_LOGW(TAG, "Auth event queue full, dropping tag: %s", tag);
}
} else {
ESP_LOGW(TAG, "Auth event queue not set");
}
esp_event_post(AUTH_EVENTS, AUTH_EVENT_TAG_PROCESSED, &event, sizeof(event), portMAX_DELAY);
}

View File

@@ -0,0 +1,3 @@
#include "auth_events.h"
ESP_EVENT_DEFINE_BASE(AUTH_EVENTS);

View File

@@ -101,9 +101,11 @@ esp_err_t wiegand_reader_init(wiegand_reader_t *reader, gpio_num_t gpio_d0, gpio
{
CHECK_ARG(reader && buf_size && callback);
/*
esp_err_t res = gpio_install_isr_service(0);
if (res != ESP_OK && res != ESP_ERR_INVALID_STATE)
return res;
*/
memset(reader, 0, sizeof(wiegand_reader_t));
reader->gpio_d0 = gpio_d0;

View File

@@ -1,8 +1,3 @@
/*
* wiegand_reader.c
*/
#include <stdio.h>
#include <string.h>
#include <freertos/FreeRTOS.h>
@@ -10,8 +5,6 @@
#include <freertos/queue.h>
#include <esp_log.h>
#include <wiegand.h>
#include <evse_api.h>
#include <ocpp.h>
#include "auth.h"
#define CONFIG_EXAMPLE_BUF_SIZE 50
@@ -62,29 +55,12 @@ static void wiegand_task(void *arg) {
}
ESP_LOGI(TAG, "Tag read: %s", tag);
if (!auth_is_enabled()) {
ESP_LOGW(TAG, "Auth disabled, ignoring tag.");
continue;
}
if (auth_tag_exists(tag)) {
ESP_LOGI(TAG, "Authorized tag. Proceeding...");
evse_authorize();
if (ocpp_is_TransactionActive()) {
ocpp_end_transaction(tag);
} else {
ocpp_begin_transaction(tag);
}
} else {
ESP_LOGW(TAG, "Unauthorized tag: %s", tag);
}
auth_process_tag(tag); // agora delega toda a lógica à auth.c
}
}
}
void initWiegand(void) {
ESP_LOGI(TAG, "Initializing Wiegand reader");
xTaskCreate(wiegand_task, TAG, configMINIMAL_STACK_SIZE * 4, NULL, 5, NULL);
xTaskCreate(wiegand_task, TAG, configMINIMAL_STACK_SIZE * 4, NULL, 4, NULL);
}