Init commit
This commit is contained in:
commit
948ae56f77
13
.devcontainer/Dockerfile
Normal file
13
.devcontainer/Dockerfile
Normal file
@ -0,0 +1,13 @@
|
||||
ARG DOCKER_TAG=latest
|
||||
FROM espressif/idf:${DOCKER_TAG}
|
||||
|
||||
ENV LC_ALL=C.UTF-8
|
||||
ENV LANG=C.UTF-8
|
||||
|
||||
RUN apt-get update -y && apt-get install udev -y
|
||||
|
||||
RUN echo "source /opt/esp/idf/export.sh > /dev/null 2>&1" >> ~/.bashrc
|
||||
|
||||
ENTRYPOINT [ "/opt/esp/entrypoint.sh" ]
|
||||
|
||||
CMD ["/bin/bash", "-c"]
|
||||
19
.devcontainer/devcontainer.json
Normal file
19
.devcontainer/devcontainer.json
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"name": "ESP-IDF QEMU",
|
||||
"build": {
|
||||
"dockerfile": "Dockerfile"
|
||||
},
|
||||
"customizations": {
|
||||
"vscode": {
|
||||
"settings": {
|
||||
"terminal.integrated.defaultProfile.linux": "bash",
|
||||
"idf.gitPath": "/usr/bin/git"
|
||||
},
|
||||
"extensions": [
|
||||
"espressif.esp-idf-extension",
|
||||
"espressif.esp-idf-web"
|
||||
]
|
||||
}
|
||||
},
|
||||
"runArgs": ["--privileged"]
|
||||
}
|
||||
91
.gitignore
vendored
Normal file
91
.gitignore
vendored
Normal file
@ -0,0 +1,91 @@
|
||||
# macOS
|
||||
.DS_Store
|
||||
.AppleDouble
|
||||
.LSOverride
|
||||
|
||||
# Directory metadata
|
||||
.directory
|
||||
|
||||
# Temporary files
|
||||
*~
|
||||
*.swp
|
||||
*.swo
|
||||
*.bak
|
||||
*.tmp
|
||||
|
||||
# Log files
|
||||
*.log
|
||||
|
||||
# Build artifacts and directories
|
||||
**/build/
|
||||
build/
|
||||
*.o
|
||||
*.a
|
||||
*.out
|
||||
*.exe # For any host-side utilities compiled on Windows
|
||||
|
||||
# ESP-IDF specific build outputs
|
||||
*.bin
|
||||
*.elf
|
||||
*.map
|
||||
flasher_args.json # Generated in build directory
|
||||
sdkconfig.old
|
||||
sdkconfig
|
||||
|
||||
# ESP-IDF dependencies
|
||||
# For older versions or manual component management
|
||||
/components/.idf/
|
||||
**/components/.idf/
|
||||
# For modern ESP-IDF component manager
|
||||
managed_components/
|
||||
# If ESP-IDF tools are installed/referenced locally to the project
|
||||
.espressif/
|
||||
|
||||
# CMake generated files
|
||||
CMakeCache.txt
|
||||
CMakeFiles/
|
||||
cmake_install.cmake
|
||||
install_manifest.txt
|
||||
CTestTestfile.cmake
|
||||
|
||||
# Python environment files
|
||||
*.pyc
|
||||
*.pyo
|
||||
*.pyd
|
||||
__pycache__/
|
||||
*.egg-info/
|
||||
dist/
|
||||
|
||||
# Virtual environment folders
|
||||
venv/
|
||||
.venv/
|
||||
env/
|
||||
|
||||
# Language Servers
|
||||
.clangd/
|
||||
.ccls-cache/
|
||||
compile_commands.json
|
||||
|
||||
# Windows specific
|
||||
Thumbs.db
|
||||
ehthumbs.db
|
||||
Desktop.ini
|
||||
|
||||
# User-specific configuration files
|
||||
*.user
|
||||
*.workspace # General workspace files, can be from various tools
|
||||
*.suo # Visual Studio Solution User Options
|
||||
*.sln.docstates # Visual Studio
|
||||
|
||||
# Игнорируем весь lwip
|
||||
components/lwip/**
|
||||
|
||||
# Но разрешаем нужные папки
|
||||
!components/lwip/
|
||||
!components/lwip/lwip/
|
||||
!components/lwip/lwip/src/
|
||||
!components/lwip/lwip/src/core/
|
||||
!components/lwip/lwip/src/core/ipv4/
|
||||
|
||||
# И сам файл
|
||||
!components/lwip/lwip/src/core/ipv4/etharp.c
|
||||
19
.vscode/c_cpp_properties.json
vendored
Normal file
19
.vscode/c_cpp_properties.json
vendored
Normal file
@ -0,0 +1,19 @@
|
||||
{
|
||||
"configurations": [
|
||||
{
|
||||
"name": "ESP-IDF",
|
||||
"compilerPath": "C:\\Espressif\\tools\\xtensa-esp-elf\\esp-15.2.0_20251204\\xtensa-esp-elf\\bin\\xtensa-esp32-elf-gcc.exe",
|
||||
"compileCommands": [
|
||||
"${config:idf.buildPath}/compile_commands.json"
|
||||
],
|
||||
"intelliSenseMode": "gcc-x86",
|
||||
"browse": {
|
||||
"path": [
|
||||
"${workspaceFolder}"
|
||||
],
|
||||
"limitSymbolsToIncludedHeaders": true
|
||||
}
|
||||
}
|
||||
],
|
||||
"version": 4
|
||||
}
|
||||
10
.vscode/launch.json
vendored
Normal file
10
.vscode/launch.json
vendored
Normal file
@ -0,0 +1,10 @@
|
||||
{
|
||||
"version": "0.2.0",
|
||||
"configurations": [
|
||||
{
|
||||
"type": "gdbtarget",
|
||||
"request": "attach",
|
||||
"name": "Eclipse CDT GDB Adapter"
|
||||
}
|
||||
]
|
||||
}
|
||||
17
.vscode/settings.json
vendored
Normal file
17
.vscode/settings.json
vendored
Normal file
@ -0,0 +1,17 @@
|
||||
{
|
||||
"C_Cpp.intelliSenseEngine": "default",
|
||||
"idf.extensionActivationMode": "always",
|
||||
"idf.openOcdConfigs": [
|
||||
"board/esp32-wrover-kit-3.3v.cfg"
|
||||
],
|
||||
"idf.currentSetup": "C:\\esp\\v6.0.2\\esp-idf",
|
||||
"idf.customExtraVars": {
|
||||
"IDF_TARGET": "esp32"
|
||||
},
|
||||
"clangd.path": "C:\\Espressif\\tools\\esp-clang\\esp-20.1.1_20250829\\esp-clang\\bin\\clangd.exe",
|
||||
"clangd.arguments": [
|
||||
"--background-index",
|
||||
"--query-driver=**",
|
||||
"--compile-commands-dir=c:\\Users\\Maxpr\\monitoring\\build"
|
||||
]
|
||||
}
|
||||
6
CMakeLists.txt
Normal file
6
CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
# The following five lines of boilerplate have to be in your project's
|
||||
# CMakeLists in this exact order for cmake to work correctly
|
||||
cmake_minimum_required(VERSION 3.22)
|
||||
|
||||
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
|
||||
project(monitoring)
|
||||
34
README.md
Normal file
34
README.md
Normal file
@ -0,0 +1,34 @@
|
||||
# ARP Monitoring for ESP32
|
||||
|
||||
> Практическая работа по анализу и модификации сетевого стека **lwIP** в среде **ESP-IDF**.
|
||||
|
||||
## Описание
|
||||
|
||||
Проект представляет собой систему мониторинга устройств локальной сети на базе **ESP32**.
|
||||
|
||||
В ходе работы выполняется перехват входящих ARP-пакетов на уровне сетевого стека **lwIP**, извлекается информация об обнаруженных устройствах и формируется динамический список сетевых узлов. Полученные данные отображаются через встроенный HTTP-сервер.
|
||||
|
||||
Дополнительно реализована возможность проверки доступности устройств с помощью **ICMP Echo (Ping)**.
|
||||
|
||||
## Возможности
|
||||
|
||||
* Перехват входящих ARP-пакетов.
|
||||
* Обнаружение устройств локальной сети.
|
||||
* Хранение списка устройств в оперативной памяти.
|
||||
* Веб-интерфейс для просмотра обнаруженных устройств.
|
||||
* Проверка доступности устройств с помощью Ping.
|
||||
|
||||
## Структура проекта
|
||||
|
||||
```text
|
||||
components/
|
||||
│
|
||||
├── arp_monitor/ Перехват ARP-пакетов
|
||||
├── device_list/ Работа со списком устройств
|
||||
├── ping/ Проверка доступности устройств
|
||||
├── web_server/ HTTP-сервер и веб-интерфейс
|
||||
└── lwip/ Изменённый файл etharp.c
|
||||
|
||||
main/
|
||||
└── main.c Точка входа приложения
|
||||
```
|
||||
6
components/arp_monitor/CMakeLists.txt
Normal file
6
components/arp_monitor/CMakeLists.txt
Normal file
@ -0,0 +1,6 @@
|
||||
idf_component_register(
|
||||
SRCS "arp_monitor.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
device_list
|
||||
)
|
||||
91
components/arp_monitor/arp_monitor.c
Normal file
91
components/arp_monitor/arp_monitor.c
Normal 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
17
components/arp_monitor/include/arp_monitor.h
Normal file
17
components/arp_monitor/include/arp_monitor.h
Normal file
@ -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
components/device_list/CMakeLists.txt
Normal file
4
components/device_list/CMakeLists.txt
Normal file
@ -0,0 +1,4 @@
|
||||
idf_component_register(
|
||||
SRCS "device_list.c"
|
||||
INCLUDE_DIRS "include"
|
||||
)
|
||||
76
components/device_list/device_list.c
Normal file
76
components/device_list/device_list.c
Normal 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];
|
||||
}
|
||||
26
components/device_list/include/device_list.h
Normal file
26
components/device_list/include/device_list.h
Normal file
@ -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
|
||||
1272
components/lwip/lwip/src/core/ipv4/etharp.c
Normal file
1272
components/lwip/lwip/src/core/ipv4/etharp.c
Normal file
File diff suppressed because it is too large
Load Diff
8
components/ping/CMakeLists.txt
Normal file
8
components/ping/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
idf_component_register(
|
||||
SRCS "ping.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
lwip
|
||||
esp_netif
|
||||
device_list
|
||||
)
|
||||
6
components/ping/include/ping.h
Normal file
6
components/ping/include/ping.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef PING_H
|
||||
#define PING_H
|
||||
|
||||
void ping_selected_devices(const char *query);
|
||||
|
||||
#endif
|
||||
138
components/ping/ping.c
Normal file
138
components/ping/ping.c
Normal 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
components/web_server/CMakeLists.txt
Normal file
8
components/web_server/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
idf_component_register(
|
||||
SRCS "web_server.c"
|
||||
INCLUDE_DIRS "include"
|
||||
REQUIRES
|
||||
device_list
|
||||
esp_http_server
|
||||
ping
|
||||
)
|
||||
6
components/web_server/include/web_server.h
Normal file
6
components/web_server/include/web_server.h
Normal file
@ -0,0 +1,6 @@
|
||||
#ifndef WEB_SERVER_H
|
||||
#define WEB_SERVER_H
|
||||
|
||||
void web_server_init(void);
|
||||
|
||||
#endif
|
||||
213
components/web_server/web_server.c
Normal file
213
components/web_server/web_server.c
Normal 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);
|
||||
}
|
||||
8
main/CMakeLists.txt
Normal file
8
main/CMakeLists.txt
Normal file
@ -0,0 +1,8 @@
|
||||
idf_component_register(
|
||||
SRCS "main.c"
|
||||
INCLUDE_DIRS "."
|
||||
REQUIRES
|
||||
arp_monitor
|
||||
device_list
|
||||
web_server
|
||||
)
|
||||
20
main/main.c
Normal file
20
main/main.c
Normal file
@ -0,0 +1,20 @@
|
||||
#include "arp_monitor.h"
|
||||
#include "device_list.h"
|
||||
#include "web_server.h"
|
||||
#include "esp_log.h"
|
||||
|
||||
void app_main(void)
|
||||
{
|
||||
device_list_init();
|
||||
arp_monitor_init();
|
||||
web_server_init();
|
||||
|
||||
arp_event_t event = {0};
|
||||
IP4_ADDR(&event.ip, 192, 168, 1, 10);
|
||||
uint8_t mac[6] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF};
|
||||
memcpy(event.mac, mac, ETH_HWADDR_LEN);
|
||||
|
||||
arp_monitor_post_event(&event);
|
||||
vTaskDelay(pdMS_TO_TICKS(100));
|
||||
ESP_LOGI("TEST", "Count = %u", device_list_count());
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user