#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, "" "" "" "" "ARP Monitoring" "" "" "

ARP Monitoring

" ); } static esp_err_t send_device_table(httpd_req_t *req) { esp_err_t err; err = httpd_resp_sendstr_chunk( req, "
" "" "" "" "" "" "" "" "" ); 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, "
SelectIP addressMAC addressLast seenStatus
" "
" "" "
" ); } static esp_err_t send_device_row(httpd_req_t *req, const device_info_t *device) { char html[256]; snprintf( html, sizeof(html), "" "" "" "" "%u.%u.%u.%u" "%02X:%02X:%02X:%02X:%02X:%02X" "%lu" "%s" "", 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, "" "" ); } 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); }