esp32-sip-client-with-headset/main/web/web_server.c

118 lines
3.1 KiB
C

/**
* Web Server - Konfigurations-Weboberfläche
*/
#include <string.h>
#include "web_server.h"
#include "web_api.h"
#include "esp_log.h"
#include "esp_http_server.h"
static const char* TAG = "WEB_SRV";
// Embedded Files (aus CMakeLists.txt EMBED_FILES)
extern const uint8_t index_html_start[] asm("_binary_index_html_start");
extern const uint8_t index_html_end[] asm("_binary_index_html_end");
extern const uint8_t style_css_start[] asm("_binary_style_css_start");
extern const uint8_t style_css_end[] asm("_binary_style_css_end");
extern const uint8_t app_js_start[] asm("_binary_app_js_start");
extern const uint8_t app_js_end[] asm("_binary_app_js_end");
static httpd_handle_t s_server = NULL;
// Handler für statische Dateien
static esp_err_t index_handler(httpd_req_t* req)
{
httpd_resp_set_type(req, "text/html");
httpd_resp_send(req, (const char*)index_html_start, index_html_end - index_html_start);
return ESP_OK;
}
static esp_err_t style_handler(httpd_req_t* req)
{
httpd_resp_set_type(req, "text/css");
httpd_resp_send(req, (const char*)style_css_start, style_css_end - style_css_start);
return ESP_OK;
}
static esp_err_t js_handler(httpd_req_t* req)
{
httpd_resp_set_type(req, "application/javascript");
httpd_resp_send(req, (const char*)app_js_start, app_js_end - app_js_start);
return ESP_OK;
}
esp_err_t web_server_init(void)
{
if (s_server != NULL) {
ESP_LOGW(TAG, "Server bereits gestartet");
return ESP_OK;
}
ESP_LOGI(TAG, "Starte Webserver auf Port %d", CONFIG_BSC_WEB_PORT);
httpd_config_t config = HTTPD_DEFAULT_CONFIG();
config.server_port = CONFIG_BSC_WEB_PORT;
config.lru_purge_enable = true;
config.max_uri_handlers = 20;
config.stack_size = 8192;
esp_err_t err = httpd_start(&s_server, &config);
if (err != ESP_OK) {
ESP_LOGE(TAG, "Server starten fehlgeschlagen: %s", esp_err_to_name(err));
return err;
}
// Statische Routen registrieren
httpd_uri_t index_uri = {
.uri = "/",
.method = HTTP_GET,
.handler = index_handler,
};
httpd_register_uri_handler(s_server, &index_uri);
httpd_uri_t style_uri = {
.uri = "/style.css",
.method = HTTP_GET,
.handler = style_handler,
};
httpd_register_uri_handler(s_server, &style_uri);
httpd_uri_t js_uri = {
.uri = "/app.js",
.method = HTTP_GET,
.handler = js_handler,
};
httpd_register_uri_handler(s_server, &js_uri);
// API-Routen registrieren
web_api_register_handlers(s_server);
ESP_LOGI(TAG, "Webserver gestartet");
return ESP_OK;
}
esp_err_t web_server_stop(void)
{
if (s_server == NULL) {
return ESP_OK;
}
ESP_LOGI(TAG, "Stoppe Webserver");
esp_err_t err = httpd_stop(s_server);
s_server = NULL;
return err;
}
httpd_handle_t web_server_get_handle(void)
{
return s_server;
}
esp_err_t web_server_send_ws_event(const char* event_type, const char* json_data)
{
// WebSocket-Broadcast - wird in einer erweiterten Version implementiert
ESP_LOGD(TAG, "WS Event: %s", event_type);
return ESP_OK;
}