new buzzer component
This commit is contained in:
@@ -8,74 +8,105 @@
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/// Tamanho máximo de uma tag RFID (incluindo '\0')
|
||||
#define AUTH_TAG_MAX_LEN 20
|
||||
/// Maximum length of an RFID tag (including null terminator)
|
||||
#define AUTH_TAG_MAX_LEN 30
|
||||
|
||||
/// Estrutura de evento emitida após leitura de uma tag
|
||||
/// Event structure emitted after a tag is read
|
||||
typedef struct {
|
||||
char tag[AUTH_TAG_MAX_LEN]; ///< Tag lida
|
||||
bool authorized; ///< true se a tag for reconhecida como válida
|
||||
char tag[AUTH_TAG_MAX_LEN]; ///< The tag that was read
|
||||
bool authorized; ///< true if the tag is valid
|
||||
} auth_event_t;
|
||||
|
||||
/**
|
||||
* @brief Inicializa o sistema de autenticação.
|
||||
* @brief Initializes the authentication system.
|
||||
*
|
||||
* - Carrega a configuração (enabled) da NVS
|
||||
* - Inicia o leitor Wiegand
|
||||
* - Emite evento AUTH_EVENT_INIT com estado atual
|
||||
* - Loads configuration (enabled/disabled) from NVS
|
||||
* - Starts the Wiegand reader
|
||||
* - Emits AUTH_EVENT_INIT with current status
|
||||
*/
|
||||
void auth_init(void);
|
||||
|
||||
/**
|
||||
* @brief Ativa ou desativa o uso de autenticação via RFID.
|
||||
* @brief Enables or disables RFID-based authentication.
|
||||
*
|
||||
* Esta configuração é persistida em NVS. Se desativado, o sistema
|
||||
* considerará todas as autorizações como aceitas.
|
||||
* This setting is persisted in NVS. If disabled,
|
||||
* all tags will be treated as authorized.
|
||||
*
|
||||
* @param value true para ativar, false para desativar
|
||||
* @param value true to enable authentication, false to disable
|
||||
*/
|
||||
void auth_set_enabled(bool value);
|
||||
|
||||
/**
|
||||
* @brief Verifica se o sistema de autenticação está habilitado.
|
||||
* @brief Checks whether authentication is currently enabled.
|
||||
*
|
||||
* @return true if enabled, false if disabled
|
||||
*/
|
||||
bool auth_is_enabled(void);
|
||||
|
||||
/**
|
||||
* @brief Adiciona uma nova tag RFID à lista de autorizadas.
|
||||
* @brief Adds a new RFID tag to the authorized list.
|
||||
*
|
||||
* @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
|
||||
* @param tag The RFID tag (max AUTH_TAG_MAX_LEN-1 characters)
|
||||
* @return true if the tag was added successfully,
|
||||
* false if it already exists or is invalid
|
||||
*/
|
||||
bool auth_add_tag(const char *tag);
|
||||
|
||||
/**
|
||||
* @brief Remove uma tag previamente cadastrada.
|
||||
* @brief Removes an existing RFID tag from the authorized list.
|
||||
*
|
||||
* @param tag String da tag
|
||||
* @return true se foi removida, false se não encontrada
|
||||
* @param tag The tag to remove
|
||||
* @return true if the tag was removed, false if not found
|
||||
*/
|
||||
bool auth_remove_tag(const char *tag);
|
||||
|
||||
/**
|
||||
* @brief Verifica se uma tag já está registrada como válida.
|
||||
* @brief Checks whether a tag is already registered.
|
||||
*
|
||||
* @param tag The tag to check
|
||||
* @return true if the tag exists, false otherwise
|
||||
*/
|
||||
bool auth_tag_exists(const char *tag);
|
||||
|
||||
/**
|
||||
* @brief Lista todas as tags válidas atualmente registradas (via logs).
|
||||
* @brief Logs all currently registered tags via ESP logging.
|
||||
*/
|
||||
void auth_list_tags(void);
|
||||
|
||||
/**
|
||||
* @brief Processa uma tag RFID lida (chamada normalmente pelo leitor).
|
||||
* @brief Processes a read RFID tag.
|
||||
*
|
||||
* - Verifica validade
|
||||
* - Emite evento AUTH_EVENT_TAG_PROCESSED
|
||||
* - Inicia timer de expiração se autorizada
|
||||
* - Checks whether it's authorized
|
||||
* - Emits AUTH_EVENT_TAG_PROCESSED event
|
||||
* - Starts expiration timer if authorized
|
||||
*
|
||||
* @param tag The tag that was read
|
||||
*/
|
||||
void auth_process_tag(const char *tag);
|
||||
|
||||
/**
|
||||
* @brief Enables registration mode for the next tag read.
|
||||
*
|
||||
* When registration mode is active, the next tag read
|
||||
* will be added to the authorized list automatically.
|
||||
* Mode is deactivated after one tag is registered.
|
||||
*/
|
||||
void auth_wait_for_tag_registration(void);
|
||||
|
||||
/**
|
||||
* @brief Returns the total number of registered tags.
|
||||
*
|
||||
* @return Number of valid tags
|
||||
*/
|
||||
int auth_get_tag_count(void);
|
||||
|
||||
/**
|
||||
* @brief Returns the tag string at the given index.
|
||||
*
|
||||
* @param index The index (0 ≤ index < auth_get_tag_count())
|
||||
* @return Pointer to the tag string, or NULL if index is invalid
|
||||
*/
|
||||
const char *auth_get_tag_by_index(int index);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
|
||||
@@ -7,6 +7,7 @@ ESP_EVENT_DECLARE_BASE(AUTH_EVENTS);
|
||||
|
||||
typedef enum {
|
||||
AUTH_EVENT_TAG_PROCESSED,
|
||||
AUTH_EVENT_TAG_SAVED,
|
||||
AUTH_EVENT_ENABLED_CHANGED,
|
||||
AUTH_EVENT_INIT,
|
||||
} auth_event_id_t;
|
||||
|
||||
@@ -1,37 +1,41 @@
|
||||
/*
|
||||
* auth.c
|
||||
*/
|
||||
|
||||
#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"
|
||||
#include "esp_random.h"
|
||||
|
||||
|
||||
#define MAX_TAGS 50
|
||||
|
||||
static const char *TAG = "Auth";
|
||||
|
||||
static bool enabled = false;
|
||||
static bool waiting_for_registration = false;
|
||||
static char valid_tags[MAX_TAGS][AUTH_TAG_MAX_LEN];
|
||||
static int tag_count = 0;
|
||||
|
||||
// NVS keys
|
||||
#define NVS_NAMESPACE "auth"
|
||||
#define NVS_TAG_PREFIX "tag_"
|
||||
#define NVS_TAG_COUNT_KEY "count"
|
||||
#define NVS_ENABLED_KEY "enabled"
|
||||
|
||||
// ===========================
|
||||
// Persistência em NVS
|
||||
// NVS Persistence
|
||||
// ===========================
|
||||
|
||||
static void load_auth_config(void) {
|
||||
nvs_handle_t handle;
|
||||
esp_err_t err = nvs_open("auth", NVS_READONLY, &handle);
|
||||
esp_err_t err = nvs_open(NVS_NAMESPACE, NVS_READONLY, &handle);
|
||||
if (err == ESP_OK) {
|
||||
uint8_t val;
|
||||
if (nvs_get_u8(handle, "enabled", &val) == ESP_OK) {
|
||||
if (nvs_get_u8(handle, NVS_ENABLED_KEY, &val) == ESP_OK) {
|
||||
enabled = val;
|
||||
ESP_LOGI(TAG, "Loaded auth enabled = %d", enabled);
|
||||
}
|
||||
@@ -43,8 +47,8 @@ static void load_auth_config(void) {
|
||||
|
||||
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);
|
||||
if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &handle) == ESP_OK) {
|
||||
nvs_set_u8(handle, NVS_ENABLED_KEY, enabled);
|
||||
nvs_commit(handle);
|
||||
nvs_close(handle);
|
||||
ESP_LOGI(TAG, "Auth config saved: enabled = %d", enabled);
|
||||
@@ -53,8 +57,60 @@ static void save_auth_config(void) {
|
||||
}
|
||||
}
|
||||
|
||||
static void load_tags_from_nvs(void) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(NVS_NAMESPACE, NVS_READONLY, &handle) != ESP_OK) {
|
||||
ESP_LOGW(TAG, "No stored tags in NVS");
|
||||
return;
|
||||
}
|
||||
|
||||
uint8_t count = 0;
|
||||
if (nvs_get_u8(handle, NVS_TAG_COUNT_KEY, &count) != ESP_OK) {
|
||||
nvs_close(handle);
|
||||
return;
|
||||
}
|
||||
|
||||
tag_count = 0;
|
||||
|
||||
for (int i = 0; i < count && i < MAX_TAGS; i++) {
|
||||
char key[16];
|
||||
char tag_buf[AUTH_TAG_MAX_LEN];
|
||||
size_t len = sizeof(tag_buf);
|
||||
|
||||
snprintf(key, sizeof(key), "%s%d", NVS_TAG_PREFIX, i);
|
||||
if (nvs_get_str(handle, key, tag_buf, &len) == ESP_OK) {
|
||||
strncpy(valid_tags[tag_count], tag_buf, AUTH_TAG_MAX_LEN - 1);
|
||||
valid_tags[tag_count][AUTH_TAG_MAX_LEN - 1] = '\0';
|
||||
tag_count++;
|
||||
}
|
||||
}
|
||||
|
||||
nvs_close(handle);
|
||||
ESP_LOGI(TAG, "Loaded %d tags from NVS", tag_count);
|
||||
}
|
||||
|
||||
static void save_tags_to_nvs(void) {
|
||||
nvs_handle_t handle;
|
||||
if (nvs_open(NVS_NAMESPACE, NVS_READWRITE, &handle) != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to open NVS to save tags");
|
||||
return;
|
||||
}
|
||||
|
||||
nvs_set_u8(handle, NVS_TAG_COUNT_KEY, tag_count);
|
||||
|
||||
for (int i = 0; i < tag_count; i++) {
|
||||
char key[16];
|
||||
snprintf(key, sizeof(key), "%s%d", NVS_TAG_PREFIX, i);
|
||||
nvs_set_str(handle, key, valid_tags[i]);
|
||||
}
|
||||
|
||||
nvs_commit(handle);
|
||||
nvs_close(handle);
|
||||
ESP_LOGI(TAG, "Tags saved to NVS (%d tags)", tag_count);
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// Internos
|
||||
// Internal Helpers
|
||||
// ===========================
|
||||
|
||||
static bool is_tag_valid(const char *tag) {
|
||||
@@ -63,15 +119,30 @@ static bool is_tag_valid(const char *tag) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
//TODO
|
||||
//return false;
|
||||
return false;
|
||||
}
|
||||
|
||||
// ===========================
|
||||
// API pública
|
||||
// Public API
|
||||
// ===========================
|
||||
|
||||
|
||||
void auth_init(void) {
|
||||
load_auth_config();
|
||||
load_tags_from_nvs();
|
||||
|
||||
if (enabled) {
|
||||
initWiegand();
|
||||
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, "Initial AUTH state sent (enabled = %d)", enabled);
|
||||
}
|
||||
|
||||
void auth_set_enabled(bool value) {
|
||||
enabled = value;
|
||||
save_auth_config();
|
||||
@@ -88,11 +159,13 @@ bool auth_is_enabled(void) {
|
||||
bool auth_add_tag(const char *tag) {
|
||||
if (tag_count >= MAX_TAGS) return false;
|
||||
if (!tag || strlen(tag) >= AUTH_TAG_MAX_LEN) return false;
|
||||
if (is_tag_valid(tag)) return true;
|
||||
if (is_tag_valid(tag)) return true; // Already exists
|
||||
|
||||
strncpy(valid_tags[tag_count], tag, AUTH_TAG_MAX_LEN - 1);
|
||||
valid_tags[tag_count][AUTH_TAG_MAX_LEN - 1] = '\0';
|
||||
tag_count++;
|
||||
|
||||
save_tags_to_nvs();
|
||||
ESP_LOGI(TAG, "Tag added: %s", tag);
|
||||
return true;
|
||||
}
|
||||
@@ -104,6 +177,8 @@ bool auth_remove_tag(const char *tag) {
|
||||
strncpy(valid_tags[j], valid_tags[j + 1], AUTH_TAG_MAX_LEN);
|
||||
}
|
||||
tag_count--;
|
||||
|
||||
save_tags_to_nvs();
|
||||
ESP_LOGI(TAG, "Tag removed: %s", tag);
|
||||
return true;
|
||||
}
|
||||
@@ -122,20 +197,9 @@ void auth_list_tags(void) {
|
||||
}
|
||||
}
|
||||
|
||||
void auth_init(void) {
|
||||
load_auth_config(); // carrega estado de ativação
|
||||
|
||||
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);
|
||||
void auth_wait_for_tag_registration(void) {
|
||||
waiting_for_registration = true;
|
||||
ESP_LOGI(TAG, "Tag registration mode enabled.");
|
||||
}
|
||||
|
||||
void auth_process_tag(const char *tag) {
|
||||
@@ -144,6 +208,22 @@ void auth_process_tag(const char *tag) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (waiting_for_registration) {
|
||||
if (auth_add_tag(tag)) {
|
||||
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 = true;
|
||||
|
||||
esp_event_post(AUTH_EVENTS, AUTH_EVENT_TAG_SAVED, &event, sizeof(event), portMAX_DELAY);
|
||||
ESP_LOGI(TAG, "Tag registered: %s", tag);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Failed to register tag: %s", tag);
|
||||
}
|
||||
waiting_for_registration = false;
|
||||
return;
|
||||
}
|
||||
|
||||
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';
|
||||
@@ -153,3 +233,12 @@ void auth_process_tag(const char *tag) {
|
||||
|
||||
esp_event_post(AUTH_EVENTS, AUTH_EVENT_TAG_PROCESSED, &event, sizeof(event), portMAX_DELAY);
|
||||
}
|
||||
|
||||
int auth_get_tag_count(void) {
|
||||
return tag_count;
|
||||
}
|
||||
|
||||
const char *auth_get_tag_by_index(int index) {
|
||||
if (index < 0 || index >= tag_count) return NULL;
|
||||
return valid_tags[index];
|
||||
}
|
||||
|
||||
@@ -34,7 +34,7 @@ static void wiegand_task(void *arg) {
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(wiegand_reader_init(&reader, 19, 18,
|
||||
ESP_ERROR_CHECK(wiegand_reader_init(&reader, 21, 22,
|
||||
true, CONFIG_EXAMPLE_BUF_SIZE, reader_callback, WIEGAND_MSB_FIRST, WIEGAND_LSB_FIRST));
|
||||
|
||||
data_packet_t p;
|
||||
|
||||
Reference in New Issue
Block a user