summaryrefslogtreecommitdiff
path: root/fw/app/src/http.c
diff options
context:
space:
mode:
authorxengineering <me@xengineering.eu>2025-05-07 19:03:22 +0200
committerxengineering <me@xengineering.eu>2025-05-07 19:03:22 +0200
commita5aafcbd0398ae3893a83827cf66d4352eaee8f0 (patch)
tree36506463a33aa274dc56f8332269eab096eb73c7 /fw/app/src/http.c
parent015972d873627466bd7e40f8d5dbb5951fb0f36a (diff)
parent184a41809c66868992c90ce9d420b8e4dc46253b (diff)
downloadiot-contact-a5aafcbd0398ae3893a83827cf66d4352eaee8f0.tar
iot-contact-a5aafcbd0398ae3893a83827cf66d4352eaee8f0.tar.zst
iot-contact-a5aafcbd0398ae3893a83827cf66d4352eaee8f0.zip
Merge branch 'settings'HEADmain
This adds the HTTP GET /settings.json API. Writing settings is only supported via the Zephyr shell.
Diffstat (limited to 'fw/app/src/http.c')
-rw-r--r--fw/app/src/http.c51
1 files changed, 51 insertions, 0 deletions
diff --git a/fw/app/src/http.c b/fw/app/src/http.c
index 08b59c1..f9016c1 100644
--- a/fw/app/src/http.c
+++ b/fw/app/src/http.c
@@ -5,6 +5,7 @@
*/
+#include <errno.h>
#include <stdint.h>
#include <zephyr/init.h>
@@ -13,6 +14,7 @@
#include <zephyr/net/http/service.h>
#include <zephyr/net/http/status.h>
+#include "settings.h"
#include "ws.h"
@@ -91,6 +93,54 @@ static struct http_resource_detail_dynamic favicon_resource_detail = {
.user_data = NULL,
};
+static int settings_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
+) {
+ static char buffer[SETTINGS_JSON_MAX_LEN];
+
+ int ret = settings_to_json(buffer, sizeof(buffer));
+ if (ret < 0) {
+ LOG_ERR("Could not serialize payload for settings request");
+ return ret;
+ }
+
+ size_t len = strnlen(buffer, sizeof(buffer));
+ if (len >= sizeof(buffer)) {
+ LOG_ERR("End of settings JSON string not found (strnlen returned %d)",
+ sizeof(buffer));
+ return -ENOMEM;
+ }
+
+ if (sizeof(buffer) < len + 2) {
+ LOG_ERR("Settings JSON buffer too small to hold \\n and \\0");
+ return -ENOMEM;
+ }
+ buffer[len + 0] = '\n';
+ buffer[len + 1] = '\0';
+ len += 1;
+
+ response_ctx->body = (const uint8_t *)buffer;
+ response_ctx->body_len = len;
+ response_ctx->final_chunk = true;
+ response_ctx->status = HTTP_200_OK;
+
+ return 0;
+}
+
+static struct http_resource_detail_dynamic settings_resource_detail = {
+ .common = {
+ .type = HTTP_RESOURCE_TYPE_DYNAMIC,
+ .bitmask_of_supported_http_methods = BIT(HTTP_GET),
+ .content_type = "text/json",
+ },
+ .cb = settings_handler,
+ .user_data = NULL,
+};
+
static uint8_t websocket_read_buffer[1024];
struct http_resource_detail_websocket websocket_resource_detail = {
@@ -112,6 +162,7 @@ HTTP_RESOURCE_DEFINE(websocket_resource, http_service, "/", &websocket_resource_
HTTP_RESOURCE_DEFINE(favicon_resource, http_service, "/favicon.ico", &favicon_resource_detail);
HTTP_RESOURCE_DEFINE(css_resource, http_service, "/simple.css", &css_resource_detail);
HTTP_RESOURCE_DEFINE(js_resource, http_service, "/iot-contact.js", &js_resource_detail);
+HTTP_RESOURCE_DEFINE(settings_resource, http_service, "/settings.json", &settings_resource_detail);
int init_http_server(void) {
LOG_DBG("Starting HTTP server");