diff options
author | xengineering <me@xengineering.eu> | 2025-03-23 17:53:24 +0100 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2025-03-26 21:18:17 +0100 |
commit | b073db4017008ceb638a9c23c8cc93e60a3a7fdb (patch) | |
tree | 3359897450d1a0d187eb1fc2c47f36a6475691fd /fw/app/src/http.c | |
parent | 6ebbc6cd9876744c09547bcb4ce2c39a89ce0f6c (diff) | |
download | iot-contact-b073db4017008ceb638a9c23c8cc93e60a3a7fdb.tar iot-contact-b073db4017008ceb638a9c23c8cc93e60a3a7fdb.tar.zst iot-contact-b073db4017008ceb638a9c23c8cc93e60a3a7fdb.zip |
fw: app: Move application firmware code here
This makes the structure of the `fw` folder more clear and separates
application-related code from bootloader- or rtos-related code.
Diffstat (limited to 'fw/app/src/http.c')
-rw-r--r-- | fw/app/src/http.c | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/fw/app/src/http.c b/fw/app/src/http.c new file mode 100644 index 0000000..e206f86 --- /dev/null +++ b/fw/app/src/http.c @@ -0,0 +1,112 @@ +/* + * 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> +#include <zephyr/net/http/status.h> + +#include "ws.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 const uint8_t js_html_gz[] = { + #include "iot-contact.js.gz.inc" +}; + +struct http_resource_detail_static js_resource_detail = { + .common = { + .type = HTTP_RESOURCE_TYPE_STATIC, + .bitmask_of_supported_http_methods = BIT(HTTP_GET), + .content_encoding = "gzip", + .content_type = "text/javascript", + }, + .static_data = js_html_gz, + .static_data_len = sizeof(js_html_gz), +}; + +static int favicon_handler( + struct http_client_ctx *client, + enum http_data_status status, + const struct http_request_ctx *request_ctx, + struct http_response_ctx *response_ctx, + void *user_data +) { + LOG_DBG("Handling favicon request"); + + response_ctx->body = NULL; + response_ctx->body_len = 0; + response_ctx->final_chunk = true; + response_ctx->status = HTTP_204_NO_CONTENT; + + LOG_DBG("Favicon request handled"); + return 0; +} + +static struct http_resource_detail_dynamic favicon_resource_detail = { + .common = { + .type = HTTP_RESOURCE_TYPE_DYNAMIC, + .bitmask_of_supported_http_methods = BIT(HTTP_GET), + }, + .cb = favicon_handler, + .user_data = NULL, +}; + +static uint8_t websocket_read_buffer[1024]; + +struct http_resource_detail_websocket websocket_resource_detail = { + .common = { + .type = HTTP_RESOURCE_TYPE_WEBSOCKET, + .bitmask_of_supported_http_methods = BIT(HTTP_GET), + }, + .cb = ws_upgrade_handler, + .data_buffer = websocket_read_buffer, + .data_buffer_len = sizeof(websocket_read_buffer), +}; + +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); +HTTP_RESOURCE_DEFINE(websocket_resource, http_service, "/", &websocket_resource_detail); +HTTP_RESOURCE_DEFINE(favicon_resource, http_service, "/favicon.ico", &favicon_resource_detail); +HTTP_RESOURCE_DEFINE(js_resource, http_service, "/iot-contact.js", &js_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); |