diff options
author | xengineering <me@xengineering.eu> | 2025-03-21 22:23:47 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2025-03-21 22:23:47 +0100 |
commit | 2430e8557685a8af292ae82ca11ec59acb365390 (patch) | |
tree | b830cef277b4a88338342d009ead9f0519885e18 | |
parent | b912c63ca9724a31f57226a44e87954e4fbd46c4 (diff) | |
download | iot-contact-2430e8557685a8af292ae82ca11ec59acb365390.tar iot-contact-2430e8557685a8af292ae82ca11ec59acb365390.tar.zst iot-contact-2430e8557685a8af292ae82ca11ec59acb365390.zip |
fw: http: Add HTML resource /
This provides the index HTML page.
-rw-r--r-- | fw/CMakeLists.txt | 10 | ||||
-rw-r--r-- | fw/prj.conf | 9 | ||||
-rw-r--r-- | fw/sections-rom.ld | 3 | ||||
-rw-r--r-- | fw/src/html/index.html | 11 | ||||
-rw-r--r-- | fw/src/http.c | 52 |
5 files changed, 85 insertions, 0 deletions
diff --git a/fw/CMakeLists.txt b/fw/CMakeLists.txt index 32a4571..d07494b 100644 --- a/fw/CMakeLists.txt +++ b/fw/CMakeLists.txt @@ -22,4 +22,14 @@ target_sources(app "${CMAKE_CURRENT_SOURCE_DIR}/src/main.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/syslog.c" "${CMAKE_CURRENT_SOURCE_DIR}/src/mac.c" + "${CMAKE_CURRENT_SOURCE_DIR}/src/http.c" ) + +zephyr_linker_sources(SECTIONS sections-rom.ld) +zephyr_linker_section(NAME http_resource_desc_http_service + KVMA RAM_REGION GROUP RODATA_REGION + SUBALIGN ${CONFIG_LINKER_ITERABLE_SUBALIGN}) + +set(gen_dir ${ZEPHYR_BINARY_DIR}/include/generated/) +set(source_file_index src/html/index.html) +generate_inc_file_for_target(app ${source_file_index} ${gen_dir}/index.html.gz.inc --gzip) diff --git a/fw/prj.conf b/fw/prj.conf index 405e696..bd761fc 100644 --- a/fw/prj.conf +++ b/fw/prj.conf @@ -10,6 +10,7 @@ CONFIG_NET_SHELL=y CONFIG_NET_SOCKETS=y CONFIG_NET_CONNECTION_MANAGER=y CONFIG_NET_L2_ETHERNET_MGMT=y +CONFIG_NET_TCP=y CONFIG_LOG=y CONFIG_LOG_BACKEND_NET=y @@ -18,3 +19,11 @@ CONFIG_LOG_BACKEND_NET_AUTOSTART=n CONFIG_LOG_MODE_DEFERRED=y CONFIG_POSIX_C_LANG_SUPPORT_R=y + +CONFIG_HTTP_SERVER=y +CONFIG_HTTP_PARSER=y +CONFIG_HTTP_PARSER_URL=y + +CONFIG_FILE_SYSTEM=y + +CONFIG_EVENTFD=y diff --git a/fw/sections-rom.ld b/fw/sections-rom.ld new file mode 100644 index 0000000..c2f7bc4 --- /dev/null +++ b/fw/sections-rom.ld @@ -0,0 +1,3 @@ +#include <zephyr/linker/iterable_sections.h> + +ITERABLE_SECTION_ROM(http_resource_desc_http_service, Z_LINK_ITERABLE_SUBALIGN) diff --git a/fw/src/html/index.html b/fw/src/html/index.html new file mode 100644 index 0000000..a7e3ce9 --- /dev/null +++ b/fw/src/html/index.html @@ -0,0 +1,11 @@ +<!DOCTYPE html> +<html lang="en"> + <head> + <meta charset="UTF-8"> + <meta name="viewport" content="width=device-width, initial-scale=1"> + <title>iot-contact</title> + </head> + <body> + <h4>iot-contact</h4> + </body> +</html> diff --git a/fw/src/http.c b/fw/src/http.c new file mode 100644 index 0000000..36ad7c0 --- /dev/null +++ b/fw/src/http.c @@ -0,0 +1,52 @@ +/* + * This Source Code Form is subject to the terms of the Mozilla Public License, + * v. 2.0. If a copy of the MPL was not distributed with this file, You can + * obtain one at https://mozilla.org/MPL/2.0/. + */ + + +#include <stdint.h> + +#include <zephyr/init.h> +#include <zephyr/logging/log.h> +#include <zephyr/net/http/server.h> +#include <zephyr/net/http/service.h> + + +LOG_MODULE_REGISTER(http); + + +static const uint8_t index_html_gz[] = { + #include "index.html.gz.inc" +}; + +struct http_resource_detail_static index_resource_detail = { + .common = { + .type = HTTP_RESOURCE_TYPE_STATIC, + .bitmask_of_supported_http_methods = BIT(HTTP_GET), + .content_encoding = "gzip", + .content_type = "text/html", + }, + .static_data = index_html_gz, + .static_data_len = sizeof(index_html_gz), +}; + +static uint16_t http_port = 80; + +HTTP_SERVICE_DEFINE(http_service, NULL, &http_port, 1, 10, NULL, NULL); + +HTTP_RESOURCE_DEFINE(index_resource, http_service, "/", &index_resource_detail); + +int init_http_server(void) { + LOG_DBG("Starting HTTP server"); + + int ret = http_server_start(); + if (ret < 0) { + LOG_ERR("Failed to start HTTP server (%d)", ret); + return ret; + } + + LOG_INF("HTTP server was started"); + return 0; +} +SYS_INIT(init_http_server, APPLICATION, 99); |