arp_monitoring/components/arp_monitor/arp_monitor.c
2026-07-15 20:32:20 +03:00

91 lines
2.0 KiB
C

#include "arp_monitor.h"
#include "device_list.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/queue.h"
#define ARP_QUEUE_LENGTH 16
#define ARP_TASK_STACK_SIZE 2048
#define ARP_TASK_PRIORITY (tskIDLE_PRIORITY + 1)
static const char *TAG = "arp_monitor";
static QueueHandle_t arp_queue = NULL;
static void process_arp_event(const arp_event_t *event);
static void arp_monitor_task(void *arg);
void arp_monitor_init(void)
{
arp_queue = xQueueCreate(ARP_QUEUE_LENGTH, sizeof(arp_event_t));
if (arp_queue == NULL)
{
ESP_LOGE(TAG, "Failde to create ARP queue");
return;
}
ESP_LOGI(TAG, "ARP queue created");
BaseType_t arp_task = xTaskCreate(
arp_monitor_task,
"arp_monitor",
ARP_TASK_STACK_SIZE,
NULL,
ARP_TASK_PRIORITY,
NULL);
if (arp_task != pdPASS)
{
ESP_LOGE(TAG, "Failed to create ARP monitor task");
vQueueDelete(arp_queue);
arp_queue = NULL;
return;
}
ESP_LOGI(TAG, "ARP monitor task created");
}
bool arp_monitor_post_event(const arp_event_t *event)
{
if (arp_queue == NULL)
{
ESP_LOGE(TAG, "ARP queue isn't initialized");
return false;
}
if (xQueueSend(arp_queue, event, 0) != pdPASS)
{
ESP_LOGW(TAG, "ARP queue is full");
return false;
}
return true;
}
static void process_arp_event(const arp_event_t *event)
{
device_info_t device = {
.ip = event->ip,
.last_seen = xTaskGetTickCount(),
.active = true
};
memcpy(device.mac.addr, event->mac, ETH_HWADDR_LEN);
if (!device_list_add_or_update(&device))
{
ESP_LOGW(TAG, "Failed to update device list");
}
}
static void arp_monitor_task(void *arg)
{
arp_event_t event;
ESP_LOGI(TAG, "ARP monitor task started");
while (true)
{
if (xQueueReceive(arp_queue, &event, portMAX_DELAY) == pdPASS)
{
process_arp_event(&event);
}
}
}