Init commit

This commit is contained in:
2026-07-15 20:32:20 +03:00
commit 948ae56f77
23 changed files with 2108 additions and 0 deletions
+6
View File
@@ -0,0 +1,6 @@
idf_component_register(
SRCS "arp_monitor.c"
INCLUDE_DIRS "include"
REQUIRES
device_list
)
+91
View File
@@ -0,0 +1,91 @@
#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);
}
}
}
@@ -0,0 +1,17 @@
#ifndef ARP_MONITOR_H
#define ARP_MONITOR_H
#include <stdbool.h>
#include "lwip/ip4_addr.h"
#include "lwip/prot/ethernet.h"
typedef struct
{
ip4_addr_t ip;
uint8_t mac[6];
} arp_event_t;
void arp_monitor_init(void);
bool arp_monitor_post_event(const arp_event_t *event);
#endif
+4
View File
@@ -0,0 +1,4 @@
idf_component_register(
SRCS "device_list.c"
INCLUDE_DIRS "include"
)
+76
View File
@@ -0,0 +1,76 @@
#include "device_list.h"
#include <string.h>
#include "esp_log.h"
static device_info_t device_list[DEVICE_LIST_MAX_SIZE];
static size_t device_count = 0;
static const char *TAG = "device_list";
void device_list_init(void)
{
memset(device_list, 0, sizeof(device_list));
device_count = 0;
ESP_LOGI(TAG, "Device list initialized");
}
device_info_t *device_list_find_by_mac(const struct eth_addr *mac)
{
if (mac == NULL)
{
return NULL;
}
for (size_t i = 0; i < device_count; i++)
{
if (memcmp(device_list[i].mac.addr, mac->addr, ETH_HWADDR_LEN) == 0)
{
return &device_list[i];
}
}
return NULL;
}
bool device_list_add_or_update(const device_info_t *device)
{
device_info_t *existing_device = device_list_find_by_mac(&device->mac);
if (existing_device != NULL)
{
ip4_addr_copy(existing_device->ip, device->ip);
existing_device->last_seen = device->last_seen;
existing_device->active = device->active;
ESP_LOGI(TAG, "Device updated");
//ESP_LOGI(TAG, "Device updated: " MACSTR, MAC2STR(device->mac.addr));
return true;
}
if (device_count >= DEVICE_LIST_MAX_SIZE)
{
ESP_LOGW(TAG, "Device list is full");
return false;
}
device_list[device_count] = *device;
device_count++;
ESP_LOGI(TAG, "Device added (%u/%u)", (unsigned)device_count, DEVICE_LIST_MAX_SIZE);
return true;
}
size_t device_list_count(void)
{
return device_count;
}
const device_info_t *device_list_get_by_index(size_t index)
{
if (index >= device_count)
{
return NULL;
}
return &device_list[index];
}
@@ -0,0 +1,26 @@
#ifndef DEVICE_LIST_H
#define DEVICE_LIST_H
#include <stdbool.h>
#include <stddef.h>
#include "freertos/FreeRTOS.h"
#include "lwip/ip4_addr.h"
#include "lwip/prot/ethernet.h"
#define DEVICE_LIST_MAX_SIZE 64
typedef struct
{
ip4_addr_t ip;
struct eth_addr mac;
TickType_t last_seen;
bool active;
} device_info_t;
void device_list_init(void);
device_info_t *device_list_find_by_mac(const struct eth_addr *mac);
bool device_list_add_or_update(const device_info_t *device);
size_t device_list_count(void);
const device_info_t *device_list_get_by_index(size_t index);
#endif
File diff suppressed because it is too large Load Diff
+8
View File
@@ -0,0 +1,8 @@
idf_component_register(
SRCS "ping.c"
INCLUDE_DIRS "include"
REQUIRES
lwip
esp_netif
device_list
)
+6
View File
@@ -0,0 +1,6 @@
#ifndef PING_H
#define PING_H
void ping_selected_devices(const char *query);
#endif
+138
View File
@@ -0,0 +1,138 @@
#include "ping.h"
#include "device_list.h"
#include <string.h>
#include <stdio.h>
#include "ping/ping_sock.h"
#include "esp_netif.h"
#include "esp_log.h"
#include "freertos/FreeRTOS.h"
#include "freertos/semphr.h"
static const char *TAG = "ping";
typedef struct
{
bool reply_received;
SemaphoreHandle_t finished;
} ping_context_t;
static bool parse_mac_address(const char *text, struct eth_addr * mac);
static void ping_device(device_info_t *device);
void ping_selected_devices(const char *query);
static void ping_success_callback(esp_ping_handle_t handle, void *arguments);
static void ping_end_callback(esp_ping_handle_t handle, void *arguments);
static bool parse_mac_address(const char *text, struct eth_addr *mac)
{
if ((text == NULL) || (mac == NULL))
return false;
return (sscanf(text,
"%2hhX:%2hhX:%2hhX:%2hhX:%2hhX:%2hhX",
&mac->addr[0],
&mac->addr[1],
&mac->addr[2],
&mac->addr[3],
&mac->addr[4],
&mac->addr[5]) == 6);
}
void ping_selected_devices(const char *query)
{
const char *current = query;
while ((current = strstr(current, "mac=")) != NULL)
{
struct eth_addr mac;
device_info_t *device;
char mac_string[18];
size_t length;
current += 4;
const char *end = strchr(current, '&');
if (end != NULL)
length = end - current;
else
length = strlen(current);
if (length >= sizeof(mac_string))
break;
memcpy(mac_string, current, length);
mac_string[length] = '\0';
if (parse_mac_address(mac_string, &mac))
{
device = device_list_find_by_mac(&mac);
if (device != NULL)
{
ping_device(device);
}
}
if (end == NULL)
break;
current = end + 1;
}
}
static void ping_device(device_info_t *device)
{
ping_context_t context = {
.reply_received = false,
.finished = xSemaphoreCreateBinary()
};
if (context.finished == NULL)
return;
esp_ping_config_t config = ESP_PING_DEFAULT_CONFIG();
config.target_addr.type = ESP_IPADDR_TYPE_V4;
config.target_addr.u_addr.ip4.addr = device->ip.addr;
config.count = 1;
config.interval_ms = 1000;
config.timeout_ms = 1000;
esp_ping_callbacks_t callbacks = {
.on_ping_success = ping_success_callback,
.on_ping_end = ping_end_callback,
.cb_args = &context
};
esp_ping_handle_t ping;
if (esp_ping_new_session(&config, &callbacks, &ping) != ESP_OK)
{
vSemaphoreDelete(context.finished);
return;
}
esp_ping_start(ping);
xSemaphoreTake(context.finished, portMAX_DELAY);
esp_ping_stop(ping);
esp_ping_delete_session(ping);
device->active = context.reply_received;
vSemaphoreDelete(context.finished);
}
static void ping_success_callback(esp_ping_handle_t handle, void *args)
{
ping_context_t *context = (ping_context_t *)args;
context->reply_received = true;
}
static void ping_end_callback(esp_ping_handle_t handle, void *args)
{
ping_context_t *context = (ping_context_t *)args;
xSemaphoreGive(context->finished);
}
+8
View File
@@ -0,0 +1,8 @@
idf_component_register(
SRCS "web_server.c"
INCLUDE_DIRS "include"
REQUIRES
device_list
esp_http_server
ping
)
@@ -0,0 +1,6 @@
#ifndef WEB_SERVER_H
#define WEB_SERVER_H
void web_server_init(void);
#endif
+213
View File
@@ -0,0 +1,213 @@
#include "web_server.h"
#include "device_list.h"
#include "ping.h"
#include "esp_http_server.h"
#include "esp_log.h"
static const char *TAG = "web_server";
static esp_err_t main_page_handler(httpd_req_t *req);
static esp_err_t ping_handler(httpd_req_t *req);
static esp_err_t send_page_header(httpd_req_t *req);
static esp_err_t send_device_table(httpd_req_t *req);
static esp_err_t send_device_row(httpd_req_t *req, const device_info_t *device);
static esp_err_t send_page_footer(httpd_req_t *req);
static httpd_handle_t start_webserver(void);
static void register_http_handlers(httpd_handle_t server);
static const httpd_uri_t main_uri = {.uri = "/", .method = HTTP_GET, .handler = main_page_handler, .user_ctx = NULL};
static const httpd_uri_t ping_uri = {.uri = "/ping", .method = HTTP_GET, .handler = ping_handler, .user_ctx = NULL};
void web_server_init(void)
{
httpd_handle_t server_handle = start_webserver();
if (server_handle != NULL)
{
register_http_handlers(server_handle);
}
}
static httpd_handle_t start_webserver(void)
{
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
httpd_handle_t server = NULL;
if (httpd_start(&server, &config) == ESP_OK)
{
ESP_LOGI(TAG, "HTTP server started");
}
else
{
ESP_LOGE(TAG, "Failed to start http server");
}
return server;
}
static void register_http_handlers(httpd_handle_t server)
{
esp_err_t err = httpd_register_uri_handler(server, &main_uri);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to register main URI");
}
err = httpd_register_uri_handler(server, &ping_uri);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Failed to register ping URI");
}
}
static esp_err_t main_page_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "text/html");
esp_err_t err;
err = send_page_header(req);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Page header isn't created");
return err;
}
err = send_device_table(req);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Table isn't created");
return err;
}
err = send_page_footer(req);
if (err != ESP_OK)
{
ESP_LOGE(TAG, "Page footer isn't created");
return err;
}
return httpd_resp_sendstr_chunk(req, NULL);;
}
static esp_err_t send_page_header(httpd_req_t *req)
{
return httpd_resp_sendstr_chunk(
req,
"<!DOCTYPE html>"
"<html>"
"<head>"
"<meta charset=\"UTF-8\">"
"<title>ARP Monitoring</title>"
"</head>"
"<body>"
"<h1>ARP Monitoring</h1>"
);
}
static esp_err_t send_device_table(httpd_req_t *req)
{
esp_err_t err;
err = httpd_resp_sendstr_chunk(
req,
"<form action=\"/ping\" method=\"get\">"
"<table border=\"1\">"
"<tr>"
"<th>Select</th>"
"<th>IP address</th>"
"<th>MAC address</th>"
"<th>Last seen</th>"
"<th>Status</th>"
"</tr>"
);
for (size_t index = 0; index < device_list_count(); index++)
{
const device_info_t *current_device;
current_device = device_list_get_by_index(index);
if (current_device == NULL)
continue;
err = send_device_row(req, current_device);
if (err != ESP_OK)
return err;
}
return httpd_resp_sendstr_chunk(
req,
"</table>"
"<br>"
"<input type=\"submit\" value=\"Ping selected devices\">"
"</form>"
);
}
static esp_err_t send_device_row(httpd_req_t *req, const device_info_t *device)
{
char html[256];
snprintf(
html,
sizeof(html),
"<tr>"
"<td>"
"<input type=\"checkbox\" name=\"mac\" value=\"%02X:%02X:%02X:%02X:%02X:%02X\">"
"</td>"
"<td>%u.%u.%u.%u</td>"
"<td>%02X:%02X:%02X:%02X:%02X:%02X</td>"
"<td>%lu</td>"
"<td>%s</td>"
"</tr>",
device->mac.addr[0],
device->mac.addr[1],
device->mac.addr[2],
device->mac.addr[3],
device->mac.addr[4],
device->mac.addr[5],
ip4_addr1(&device->ip),
ip4_addr2(&device->ip),
ip4_addr3(&device->ip),
ip4_addr4(&device->ip),
device->mac.addr[0],
device->mac.addr[1],
device->mac.addr[2],
device->mac.addr[3],
device->mac.addr[4],
device->mac.addr[5],
(unsigned long)device->last_seen,
device->active ? "Online" : "Offline"
);
return httpd_resp_sendstr_chunk(req, html);
}
static esp_err_t send_page_footer(httpd_req_t *req)
{
return httpd_resp_sendstr_chunk(
req,
"</body>"
"</html>"
);
}
static esp_err_t ping_handler(httpd_req_t *req)
{
char query[256];
if (httpd_req_get_url_query_str(req, query, sizeof(query)) == ESP_OK)
{
ping_selected_devices(query);
}
return main_page_handler(req);
}