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

213 lines
4.9 KiB
C

#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);
}