chore(format): initial formatting

This commit is contained in:
HendrikRauh 2026-03-05 23:05:35 +01:00
parent fa08fcfe65
commit 008c79852b
21 changed files with 1021 additions and 1082 deletions

View file

@ -1,6 +1,7 @@
/**
* @file web_server.h
* @brief Simple HTTP web server component for ESP32 with async FreeRTOS support.
* @brief Simple HTTP web server component for ESP32 with async FreeRTOS
* support.
*/
#pragma once
@ -8,50 +9,50 @@
#include "esp_http_server.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
/**
* @brief Web server configuration structure.
*/
typedef struct
{
uint16_t port; ///< HTTP server port (default: 80)
size_t max_uri_handlers; ///< Maximum number of URI handlers
size_t stack_size; ///< FreeRTOS task stack size
UBaseType_t task_priority; ///< FreeRTOS task priority
} webserver_config_t;
/**
* @brief Web server configuration structure.
*/
typedef struct {
uint16_t port; ///< HTTP server port (default: 80)
size_t max_uri_handlers; ///< Maximum number of URI handlers
size_t stack_size; ///< FreeRTOS task stack size
UBaseType_t task_priority; ///< FreeRTOS task priority
} webserver_config_t;
/**
* @brief Initialize and start the HTTP web server.
*
* This function creates a FreeRTOS task that manages the HTTP server.
* It serves static files from the data/ folder and supports dynamic handler registration.
*
* @param config Configuration structure. If NULL, default values are used.
* @return HTTP server handle on success, NULL on failure.
*/
httpd_handle_t webserver_start(const webserver_config_t *config);
/**
* @brief Initialize and start the HTTP web server.
*
* This function creates a FreeRTOS task that manages the HTTP server.
* It serves static files from the data/ folder and supports dynamic handler
* registration.
*
* @param config Configuration structure. If NULL, default values are used.
* @return HTTP server handle on success, NULL on failure.
*/
httpd_handle_t webserver_start(const webserver_config_t *config);
/**
* @brief Stop the web server and cleanup resources.
*
* @param server HTTP server handle returned by webserver_start().
* Safe to pass NULL.
*/
void webserver_stop(httpd_handle_t server);
/**
* @brief Stop the web server and cleanup resources.
*
* @param server HTTP server handle returned by webserver_start().
* Safe to pass NULL.
*/
void webserver_stop(httpd_handle_t server);
/**
* @brief Register a custom URI handler.
*
* This allows dynamic registration of API endpoints and other custom handlers.
*
* @param server HTTP server handle.
* @param uri_handler Pointer to httpd_uri_t structure.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t webserver_register_handler(httpd_handle_t server, const httpd_uri_t *uri_handler);
/**
* @brief Register a custom URI handler.
*
* This allows dynamic registration of API endpoints and other custom handlers.
*
* @param server HTTP server handle.
* @param uri_handler Pointer to httpd_uri_t structure.
* @return ESP_OK on success, error code otherwise.
*/
esp_err_t webserver_register_handler(httpd_handle_t server,
const httpd_uri_t *uri_handler);
#ifdef __cplusplus
}

View file

@ -3,12 +3,12 @@
#include "esp_err.h"
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel, uint8_t max_connections);
void wifi_stop_ap(void);
esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel,
uint8_t max_connections);
void wifi_stop_ap(void);
#ifdef __cplusplus
}

View file

@ -25,240 +25,219 @@ static TaskHandle_t s_server_task_handle = NULL;
/**
* @brief Get MIME type based on file extension
*/
static const char *get_mime_type(const char *filename)
{
const char *dot = strrchr(filename, '.');
if (!dot)
return "application/octet-stream";
if (strcmp(dot, ".html") == 0)
return "text/html";
if (strcmp(dot, ".css") == 0)
return "text/css";
if (strcmp(dot, ".js") == 0)
return "application/javascript";
if (strcmp(dot, ".json") == 0)
return "application/json";
if (strcmp(dot, ".png") == 0)
return "image/png";
if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0)
return "image/jpeg";
if (strcmp(dot, ".gif") == 0)
return "image/gif";
if (strcmp(dot, ".svg") == 0)
return "image/svg+xml";
if (strcmp(dot, ".ico") == 0)
return "image/x-icon";
if (strcmp(dot, ".txt") == 0)
return "text/plain";
if (strcmp(dot, ".xml") == 0)
return "application/xml";
if (strcmp(dot, ".wav") == 0)
return "audio/wav";
if (strcmp(dot, ".mp3") == 0)
return "audio/mpeg";
static const char *get_mime_type(const char *filename) {
const char *dot = strrchr(filename, '.');
if (!dot)
return "application/octet-stream";
if (strcmp(dot, ".html") == 0)
return "text/html";
if (strcmp(dot, ".css") == 0)
return "text/css";
if (strcmp(dot, ".js") == 0)
return "application/javascript";
if (strcmp(dot, ".json") == 0)
return "application/json";
if (strcmp(dot, ".png") == 0)
return "image/png";
if (strcmp(dot, ".jpg") == 0 || strcmp(dot, ".jpeg") == 0)
return "image/jpeg";
if (strcmp(dot, ".gif") == 0)
return "image/gif";
if (strcmp(dot, ".svg") == 0)
return "image/svg+xml";
if (strcmp(dot, ".ico") == 0)
return "image/x-icon";
if (strcmp(dot, ".txt") == 0)
return "text/plain";
if (strcmp(dot, ".xml") == 0)
return "application/xml";
if (strcmp(dot, ".wav") == 0)
return "audio/wav";
if (strcmp(dot, ".mp3") == 0)
return "audio/mpeg";
return "application/octet-stream";
}
/**
* @brief HTTP handler for static files from LittleFS
*/
static esp_err_t static_file_handler(httpd_req_t *req)
{
// Build the file path
char filepath[1024];
snprintf(filepath, sizeof(filepath), "%s%s", storage_get_mount_point(), req->uri);
static esp_err_t static_file_handler(httpd_req_t *req) {
// Build the file path
char filepath[1024];
snprintf(filepath, sizeof(filepath), "%s%s", storage_get_mount_point(),
req->uri);
// Handle root path
if (strcmp(req->uri, "/") == 0)
{
snprintf(filepath, sizeof(filepath), "%s/index.html", storage_get_mount_point());
}
// Handle root path
if (strcmp(req->uri, "/") == 0) {
snprintf(filepath, sizeof(filepath), "%s/index.html",
storage_get_mount_point());
}
FILE *f = fopen(filepath, "r");
if (!f)
{
ESP_LOGW(TAG, "File not found: %s", filepath);
httpd_resp_send_404(req);
return ESP_OK;
}
// Get MIME type and set content type
const char *mime_type = get_mime_type(filepath);
httpd_resp_set_type(req, mime_type);
// Send file in chunks
char buf[1024];
size_t read_len;
while ((read_len = fread(buf, 1, sizeof(buf), f)) > 0)
{
if (httpd_resp_send_chunk(req, buf, read_len) != ESP_OK)
{
ESP_LOGW(TAG, "Failed to send data chunk for %s", filepath);
break;
}
}
fclose(f);
httpd_resp_send_chunk(req, NULL, 0); // Send end marker
FILE *f = fopen(filepath, "r");
if (!f) {
ESP_LOGW(TAG, "File not found: %s", filepath);
httpd_resp_send_404(req);
return ESP_OK;
}
// Get MIME type and set content type
const char *mime_type = get_mime_type(filepath);
httpd_resp_set_type(req, mime_type);
// Send file in chunks
char buf[1024];
size_t read_len;
while ((read_len = fread(buf, 1, sizeof(buf), f)) > 0) {
if (httpd_resp_send_chunk(req, buf, read_len) != ESP_OK) {
ESP_LOGW(TAG, "Failed to send data chunk for %s", filepath);
break;
}
}
fclose(f);
httpd_resp_send_chunk(req, NULL, 0); // Send end marker
return ESP_OK;
}
/**
* @brief HTTP handler for API health check (GET /api/health)
*/
static esp_err_t health_check_handler(httpd_req_t *req)
{
httpd_resp_set_type(req, "application/json");
httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
return ESP_OK;
static esp_err_t health_check_handler(httpd_req_t *req) {
httpd_resp_set_type(req, "application/json");
httpd_resp_sendstr(req, "{\"status\":\"ok\"}");
return ESP_OK;
}
/**
* @brief FreeRTOS task function for the HTTP server.
* Allows non-blocking server operation and future extensibility.
*/
static void webserver_task(void *arg)
{
(void)arg; // Unused parameter
ESP_LOGI(TAG, "Web server task started");
static void webserver_task(void *arg) {
(void)arg; // Unused parameter
ESP_LOGI(TAG, "Web server task started");
// Keep task alive - the server runs in the background
while (s_server_handle != NULL)
{
vTaskDelay(pdMS_TO_TICKS(10000)); // 10 second check interval
}
// Keep task alive - the server runs in the background
while (s_server_handle != NULL) {
vTaskDelay(pdMS_TO_TICKS(10000)); // 10 second check interval
}
ESP_LOGI(TAG, "Web server task ending");
vTaskDelete(NULL);
ESP_LOGI(TAG, "Web server task ending");
vTaskDelete(NULL);
}
httpd_handle_t webserver_start(const webserver_config_t *config)
{
if (s_server_handle != NULL)
{
ESP_LOGW(TAG, "Web server already running");
return s_server_handle;
}
// Initialize LittleFS
esp_err_t ret = storage_init();
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to initialize storage");
return NULL;
}
// Use provided config or defaults
uint16_t port = WEBSERVER_DEFAULT_PORT;
size_t max_handlers = WEBSERVER_DEFAULT_MAX_HANDLERS;
size_t stack_size = WEBSERVER_DEFAULT_STACK_SIZE;
UBaseType_t task_priority = WEBSERVER_DEFAULT_TASK_PRIORITY;
if (config)
{
port = config->port;
max_handlers = config->max_uri_handlers;
stack_size = config->stack_size;
task_priority = config->task_priority;
}
// Create HTTP server configuration
httpd_config_t http_config = HTTPD_DEFAULT_CONFIG();
http_config.server_port = port;
http_config.max_uri_handlers = max_handlers;
http_config.stack_size = stack_size;
http_config.uri_match_fn = httpd_uri_match_wildcard;
// Start HTTP server
ret = httpd_start(&s_server_handle, &http_config);
if (ret != ESP_OK)
{
ESP_LOGE(TAG, "Failed to start HTTP server: %s", esp_err_to_name(ret));
s_server_handle = NULL;
return NULL;
}
ESP_LOGI(TAG, "HTTP server started on port %d", port);
// Register default handlers
// Health check endpoint
httpd_uri_t health_uri = {
.uri = "/api/health",
.method = HTTP_GET,
.handler = health_check_handler,
.user_ctx = NULL,
};
httpd_register_uri_handler(s_server_handle, &health_uri);
// Wildcard handler for static files from LittleFS (must be last)
httpd_uri_t file_uri = {
.uri = "/*",
.method = HTTP_GET,
.handler = static_file_handler,
.user_ctx = NULL,
};
httpd_register_uri_handler(s_server_handle, &file_uri);
// Create FreeRTOS task for the server
// This allows other tasks to continue running and makes the server async-ready
BaseType_t task_ret = xTaskCreate(
webserver_task,
"webserver",
stack_size,
(void *)s_server_handle,
task_priority,
&s_server_task_handle);
if (task_ret != pdPASS)
{
ESP_LOGE(TAG, "Failed to create web server task");
httpd_stop(s_server_handle);
s_server_handle = NULL;
return NULL;
}
ESP_LOGI(TAG, "Web server initialized successfully");
httpd_handle_t webserver_start(const webserver_config_t *config) {
if (s_server_handle != NULL) {
ESP_LOGW(TAG, "Web server already running");
return s_server_handle;
}
}
void webserver_stop(httpd_handle_t server)
{
if (server == NULL)
{
return;
}
// Initialize LittleFS
esp_err_t ret = storage_init();
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to initialize storage");
return NULL;
}
httpd_stop(server);
// Use provided config or defaults
uint16_t port = WEBSERVER_DEFAULT_PORT;
size_t max_handlers = WEBSERVER_DEFAULT_MAX_HANDLERS;
size_t stack_size = WEBSERVER_DEFAULT_STACK_SIZE;
UBaseType_t task_priority = WEBSERVER_DEFAULT_TASK_PRIORITY;
if (config) {
port = config->port;
max_handlers = config->max_uri_handlers;
stack_size = config->stack_size;
task_priority = config->task_priority;
}
// Create HTTP server configuration
httpd_config_t http_config = HTTPD_DEFAULT_CONFIG();
http_config.server_port = port;
http_config.max_uri_handlers = max_handlers;
http_config.stack_size = stack_size;
http_config.uri_match_fn = httpd_uri_match_wildcard;
// Start HTTP server
ret = httpd_start(&s_server_handle, &http_config);
if (ret != ESP_OK) {
ESP_LOGE(TAG, "Failed to start HTTP server: %s", esp_err_to_name(ret));
s_server_handle = NULL;
return NULL;
}
// Wait for task to finish
if (s_server_task_handle != NULL)
{
vTaskDelay(pdMS_TO_TICKS(100));
s_server_task_handle = NULL;
}
ESP_LOGI(TAG, "HTTP server started on port %d", port);
ESP_LOGI(TAG, "Web server stopped");
// Register default handlers
// Health check endpoint
httpd_uri_t health_uri = {
.uri = "/api/health",
.method = HTTP_GET,
.handler = health_check_handler,
.user_ctx = NULL,
};
httpd_register_uri_handler(s_server_handle, &health_uri);
// Wildcard handler for static files from LittleFS (must be last)
httpd_uri_t file_uri = {
.uri = "/*",
.method = HTTP_GET,
.handler = static_file_handler,
.user_ctx = NULL,
};
httpd_register_uri_handler(s_server_handle, &file_uri);
// Create FreeRTOS task for the server
// This allows other tasks to continue running and makes the server
// async-ready
BaseType_t task_ret = xTaskCreate(webserver_task, "webserver", stack_size,
(void *)s_server_handle, task_priority,
&s_server_task_handle);
if (task_ret != pdPASS) {
ESP_LOGE(TAG, "Failed to create web server task");
httpd_stop(s_server_handle);
s_server_handle = NULL;
return NULL;
}
ESP_LOGI(TAG, "Web server initialized successfully");
return s_server_handle;
}
esp_err_t webserver_register_handler(httpd_handle_t server, const httpd_uri_t *uri_handler)
{
if (server == NULL || uri_handler == NULL)
{
return ESP_ERR_INVALID_ARG;
}
void webserver_stop(httpd_handle_t server) {
if (server == NULL) {
return;
}
esp_err_t ret = httpd_register_uri_handler(server, uri_handler);
if (ret == ESP_OK)
{
ESP_LOGI(TAG, "Registered handler: %s [%d]", uri_handler->uri, uri_handler->method);
}
else
{
ESP_LOGE(TAG, "Failed to register handler %s: %s", uri_handler->uri, esp_err_to_name(ret));
}
httpd_stop(server);
s_server_handle = NULL;
return ret;
// Wait for task to finish
if (s_server_task_handle != NULL) {
vTaskDelay(pdMS_TO_TICKS(100));
s_server_task_handle = NULL;
}
ESP_LOGI(TAG, "Web server stopped");
}
esp_err_t webserver_register_handler(httpd_handle_t server,
const httpd_uri_t *uri_handler) {
if (server == NULL || uri_handler == NULL) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t ret = httpd_register_uri_handler(server, uri_handler);
if (ret == ESP_OK) {
ESP_LOGI(TAG, "Registered handler: %s [%d]", uri_handler->uri,
uri_handler->method);
} else {
ESP_LOGE(TAG, "Failed to register handler %s: %s", uri_handler->uri,
esp_err_to_name(ret));
}
return ret;
}

View file

@ -11,32 +11,28 @@
static const char *TAG = "WIFI";
static bool s_wifi_started = false;
esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel, uint8_t max_connections)
{
if (s_wifi_started)
{
esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel,
uint8_t max_connections) {
if (s_wifi_started) {
return ESP_OK;
}
if (!ssid || strlen(ssid) == 0 || strlen(ssid) > 32)
{
if (!ssid || strlen(ssid) == 0 || strlen(ssid) > 32) {
return ESP_ERR_INVALID_ARG;
}
const bool has_password = password && strlen(password) > 0;
if (has_password && strlen(password) < 8)
{
if (has_password && strlen(password) < 8) {
return ESP_ERR_INVALID_ARG;
}
esp_err_t err = nvs_flash_init();
if (err == ESP_ERR_NVS_NO_FREE_PAGES || err == ESP_ERR_NVS_NEW_VERSION_FOUND)
{
if (err == ESP_ERR_NVS_NO_FREE_PAGES ||
err == ESP_ERR_NVS_NEW_VERSION_FOUND) {
ESP_ERROR_CHECK(nvs_flash_erase());
err = nvs_flash_init();
}
if (err != ESP_OK)
{
if (err != ESP_OK) {
return err;
}
@ -48,22 +44,24 @@ esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel,
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
wifi_config_t wifi_config = {
.ap = {
.channel = channel,
.max_connection = max_connections,
.authmode = has_password ? WIFI_AUTH_WPA2_PSK : WIFI_AUTH_OPEN,
.pmf_cfg = {
.required = false,
.ap =
{
.channel = channel,
.max_connection = max_connections,
.authmode = has_password ? WIFI_AUTH_WPA2_PSK : WIFI_AUTH_OPEN,
.pmf_cfg =
{
.required = false,
},
},
},
};
strlcpy((char *)wifi_config.ap.ssid, ssid, sizeof(wifi_config.ap.ssid));
wifi_config.ap.ssid_len = strlen(ssid);
if (has_password)
{
strlcpy((char *)wifi_config.ap.password, password, sizeof(wifi_config.ap.password));
if (has_password) {
strlcpy((char *)wifi_config.ap.password, password,
sizeof(wifi_config.ap.password));
}
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_AP));
@ -75,10 +73,8 @@ esp_err_t wifi_start_ap(const char *ssid, const char *password, uint8_t channel,
return ESP_OK;
}
void wifi_stop_ap(void)
{
if (!s_wifi_started)
{
void wifi_stop_ap(void) {
if (!s_wifi_started) {
return;
}