refact AUTH module
This commit is contained in:
107
components/auth/src/auth.c
Normal file → Executable file
107
components/auth/src/auth.c
Normal file → Executable file
@@ -1,11 +1,108 @@
|
||||
#include "auth.h"
|
||||
#include "wiegand_reader.h"
|
||||
#include "wiegand_handler.h"
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <esp_log.h>
|
||||
#include <string.h>
|
||||
|
||||
static bool enable = false;
|
||||
#define MAX_TAGS 50
|
||||
#define TAG_LEN 20
|
||||
|
||||
void auth_set_enable(bool value) {
|
||||
enable = value;
|
||||
static const char *TAG = "Auth";
|
||||
static bool enabled = true;
|
||||
static char valid_tags[MAX_TAGS][TAG_LEN];
|
||||
static int tag_count = 0;
|
||||
|
||||
static bool is_tag_valid(const char *tag) {
|
||||
for (int i = 0; i < tag_count; i++) {
|
||||
if (strncmp(valid_tags[i], tag, TAG_LEN) == 0) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool auth_get_enable(void) {
|
||||
return enable;
|
||||
void auth_set_enabled(bool value) {
|
||||
enabled = value;
|
||||
ESP_LOGI(TAG, "Wiegand reader %s", enabled ? "ENABLED" : "DISABLED");
|
||||
}
|
||||
|
||||
bool auth_is_enabled(void) {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
bool auth_add_tag(const char *tag) {
|
||||
if (tag_count >= MAX_TAGS) return false;
|
||||
if (auth_tag_exists(tag)) return true;
|
||||
|
||||
strncpy(valid_tags[tag_count], tag, TAG_LEN - 1);
|
||||
valid_tags[tag_count][TAG_LEN - 1] = '\0';
|
||||
tag_count++;
|
||||
ESP_LOGI(TAG, "Tag added: %s", tag);
|
||||
return true;
|
||||
}
|
||||
|
||||
bool auth_remove_tag(const char *tag) {
|
||||
for (int i = 0; i < tag_count; i++) {
|
||||
if (strncmp(valid_tags[i], tag, TAG_LEN) == 0) {
|
||||
for (int j = i; j < tag_count - 1; j++) {
|
||||
strncpy(valid_tags[j], valid_tags[j + 1], TAG_LEN);
|
||||
}
|
||||
tag_count--;
|
||||
ESP_LOGI(TAG, "Tag removed: %s", tag);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
bool auth_tag_exists(const char *tag) {
|
||||
return is_tag_valid(tag);
|
||||
}
|
||||
|
||||
void auth_list_tags(void) {
|
||||
ESP_LOGI(TAG, "Registered Tags (%d):", tag_count);
|
||||
for (int i = 0; i < tag_count; i++) {
|
||||
ESP_LOGI(TAG, "- %s", valid_tags[i]);
|
||||
}
|
||||
}
|
||||
|
||||
// Função de callback para o reader
|
||||
static void on_card_read(const wiegand_packet_t *packet) {
|
||||
if (!enabled) {
|
||||
ESP_LOGW(TAG, "Ignoring Wiegand data: reader is disabled");
|
||||
return;
|
||||
}
|
||||
|
||||
char tag[TAG_LEN];
|
||||
memset(tag, 0, TAG_LEN);
|
||||
|
||||
if (packet->bits == 26) {
|
||||
snprintf(tag, TAG_LEN, "%03d%03d%03d", packet->data[0], packet->data[1], packet->data[2]);
|
||||
} else if (packet->bits == 34) {
|
||||
snprintf(tag, TAG_LEN, "%03d%03d%03d%03d", packet->data[0], packet->data[1], packet->data[2], packet->data[3]);
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unsupported bit length: %d", packet->bits);
|
||||
return;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Tag read: %s", tag);
|
||||
|
||||
if (is_tag_valid(tag)) {
|
||||
ESP_LOGI(TAG, "Authorized tag.");
|
||||
evse_authorize();
|
||||
|
||||
if (ocpp_is_TransactionActive()) {
|
||||
ocpp_end_transaction(tag);
|
||||
} else {
|
||||
ocpp_begin_transaction(tag);
|
||||
}
|
||||
} else {
|
||||
ESP_LOGW(TAG, "Unauthorized tag.");
|
||||
}
|
||||
}
|
||||
|
||||
void auth_init(void) {
|
||||
wiegand_reader_init(19, 18, on_card_read);
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
112
components/auth/src/wiegand.c
Normal file → Executable file
112
components/auth/src/wiegand.c
Normal file → Executable file
@@ -1,112 +1,18 @@
|
||||
#include <stdio.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
#include <freertos/task.h>
|
||||
#include <freertos/queue.h>
|
||||
#include <wiegand.h>
|
||||
#include "wiegand_reader.h"
|
||||
#include "wiegand.h"
|
||||
#include <esp_log.h>
|
||||
#include <string.h>
|
||||
#include <evse_api.h>
|
||||
#include <ocpp.h>
|
||||
#include <freertos/FreeRTOS.h>
|
||||
|
||||
static const char *TAG = "Wiegand_reader";
|
||||
|
||||
#define WIEGAND_PIN_D0 19
|
||||
#define WIEGAND_PIN_D1 18
|
||||
#define WIEGAND_BUF_SIZE 50
|
||||
#define QUEUE_SIZE 10
|
||||
static const char *TAG = "WiegandReader";
|
||||
|
||||
static wiegand_reader_t reader;
|
||||
static QueueHandle_t data_queue = NULL;
|
||||
|
||||
// Single data packet
|
||||
typedef struct
|
||||
void wiegand_reader_init(int pin_d0, int pin_d1, wiegand_callback_t callback)
|
||||
{
|
||||
uint8_t data[WIEGAND_BUF_SIZE];
|
||||
size_t bits;
|
||||
} data_packet_t;
|
||||
|
||||
// callback on new data in reader
|
||||
static void reader_callback(wiegand_reader_t *r)
|
||||
{
|
||||
data_packet_t p;
|
||||
p.bits = r->bits;
|
||||
size_t data_size = (r->bits + 7) / 8;
|
||||
memcpy(p.data, r->buf, data_size);
|
||||
|
||||
if (xQueueSendToBack(data_queue, &p, portMAX_DELAY) != pdTRUE) {
|
||||
ESP_LOGE(TAG, "Failed to send data to queue");
|
||||
}
|
||||
}
|
||||
|
||||
static void wiegand_task(void *arg)
|
||||
{
|
||||
// Create queue
|
||||
data_queue = xQueueCreate(QUEUE_SIZE, sizeof(data_packet_t));
|
||||
if (!data_queue)
|
||||
{
|
||||
ESP_LOGE(TAG, "Error creating queue");
|
||||
ESP_ERROR_CHECK(ESP_ERR_NO_MEM);
|
||||
}
|
||||
|
||||
// Initialize reader
|
||||
esp_err_t err = wiegand_reader_init(&reader, WIEGAND_PIN_D0, WIEGAND_PIN_D1,
|
||||
true, WIEGAND_BUF_SIZE, reader_callback,
|
||||
WIEGAND_MSB_FIRST, WIEGAND_LSB_FIRST);
|
||||
esp_err_t err = wiegand_reader_config(&reader, pin_d0, pin_d1,
|
||||
callback,
|
||||
WIEGAND_MSB_FIRST, WIEGAND_LSB_FIRST);
|
||||
if (err != ESP_OK) {
|
||||
ESP_LOGE(TAG, "Failed to initialize Wiegand reader: %s", esp_err_to_name(err));
|
||||
return;
|
||||
}
|
||||
|
||||
data_packet_t p;
|
||||
while (1)
|
||||
{
|
||||
ESP_LOGI(TAG, "Waiting for Wiegand data...");
|
||||
if (xQueueReceive(data_queue, &p, pdMS_TO_TICKS(1000)) != pdTRUE) {
|
||||
ESP_LOGW(TAG, "No Wiegand data received within the timeout");
|
||||
continue;
|
||||
}
|
||||
|
||||
ESP_LOGI(TAG, "Bits received: %d", p.bits);
|
||||
for (size_t i = 0; i < (p.bits + 7) / 8; i++)
|
||||
ESP_LOGI(TAG, " 0x%02x", p.data[i]);
|
||||
|
||||
char str[20];
|
||||
if (p.bits == 26 || p.bits == 34)
|
||||
{
|
||||
evse_authorize();
|
||||
|
||||
|
||||
|
||||
/*----26
|
||||
sprintf(str, "%03d%03d%03d", p.data[0], p.data[1], p.data[2]);
|
||||
|
||||
if (ocpp_is_TransactionActive())
|
||||
{
|
||||
ocpp_end_transaction(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
ocpp_begin_transaction(str);
|
||||
}*/
|
||||
/*----34
|
||||
sprintf(str, "%03d%03d%03d%03d", p.data[0], p.data[1], p.data[2], p.data[3]);
|
||||
|
||||
if (ocpp_is_TransactionActive())
|
||||
{
|
||||
ocpp_end_transaction(str);
|
||||
}
|
||||
else
|
||||
{
|
||||
ocpp_begin_transaction(str);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
ESP_LOGE(TAG, "Failed to init Wiegand reader: %s", esp_err_to_name(err));
|
||||
}
|
||||
}
|
||||
|
||||
void initialize_wiegand_reader()
|
||||
{
|
||||
ESP_LOGI(TAG, "Starting Wiegand reader");
|
||||
xTaskCreate(wiegand_task, TAG, configMINIMAL_STACK_SIZE * 4, NULL, 5, NULL);
|
||||
}
|
||||
|
||||
0
components/auth/src/main_wiegand.c → components/auth/src/wiegand_reader.c
Normal file → Executable file
0
components/auth/src/main_wiegand.c → components/auth/src/wiegand_reader.c
Normal file → Executable file
Reference in New Issue
Block a user