diff options
author | xengineering <me@xengineering.eu> | 2025-04-15 22:12:54 +0200 |
---|---|---|
committer | xengineering <me@xengineering.eu> | 2025-04-16 18:03:40 +0200 |
commit | f4faa103108356bcf53fb7c8829e95f012b21a4a (patch) | |
tree | f0f8e40c0a66090fd027ac31bf5ce213aaf97b63 /fw/app/src | |
parent | d25c8e08ce6a977977e38365ace9af16acf09195 (diff) | |
download | iot-contact-f4faa103108356bcf53fb7c8829e95f012b21a4a.tar iot-contact-f4faa103108356bcf53fb7c8829e95f012b21a4a.tar.zst iot-contact-f4faa103108356bcf53fb7c8829e95f012b21a4a.zip |
fw: app: http: Add working GET /settings.json
Diffstat (limited to 'fw/app/src')
-rw-r--r-- | fw/app/src/http.c | 14 | ||||
-rw-r--r-- | fw/app/src/settings.c | 29 | ||||
-rw-r--r-- | fw/app/src/settings.h | 11 | ||||
-rw-r--r-- | fw/app/src/syslog.c | 11 |
4 files changed, 60 insertions, 5 deletions
diff --git a/fw/app/src/http.c b/fw/app/src/http.c index 081629b..83a3568 100644 --- a/fw/app/src/http.c +++ b/fw/app/src/http.c @@ -13,6 +13,7 @@ #include <zephyr/net/http/service.h> #include <zephyr/net/http/status.h> +#include "settings.h" #include "ws.h" @@ -98,9 +99,16 @@ static int settings_handler( struct http_response_ctx *response_ctx, void *user_data ) { - static const char dummy[] = "{\"syslog\":{\"target\":{\"ip\":\"fe80::1\"}}}\n"; - response_ctx->body = (const uint8_t *)dummy; - response_ctx->body_len = sizeof(dummy) - 1; + static char buffer[100]; + + int ret = settings_to_json(buffer, sizeof(buffer)); + if (ret < 0) { + LOG_ERR("Could not serialize payload for settings request"); + return ret; + } + + response_ctx->body = (const uint8_t *)buffer; + response_ctx->body_len = ret; response_ctx->final_chunk = true; response_ctx->status = HTTP_200_OK; diff --git a/fw/app/src/settings.c b/fw/app/src/settings.c index 799322e..89f2f88 100644 --- a/fw/app/src/settings.c +++ b/fw/app/src/settings.c @@ -5,10 +5,14 @@ */ +#include <stdio.h> + #include <zephyr/init.h> #include <zephyr/logging/log.h> #include <zephyr/settings/settings.h> +#include "settings.h" + LOG_MODULE_DECLARE(settings); @@ -31,3 +35,28 @@ int init_settings(void) { return 0; } SYS_INIT(init_settings, APPLICATION, 50); + +int settings_to_json(void *buffer, size_t len) +{ + static const char format[] = "{\"syslog\":{\"target\":{\"ip\":\"%s\"}}}\n"; + + char ip[100]; + int ret = settings_runtime_get("syslog/target/ip", ip, sizeof(ip)); + if (ret < 0) { + LOG_ERR("Failed to get runtime setting syslog/target/ip (%d)", ret); + return ret; + } + + ret = snprintf(buffer, len, format, ip); + + if (ret >= len) { + LOG_ERR("Buffer too small to serialize settings as JSON"); + return -ENOMEM; + } + + if (ret < 0) { + LOG_ERR("Failed to serialize settings as JSON"); + } + + return ret; +} diff --git a/fw/app/src/settings.h b/fw/app/src/settings.h new file mode 100644 index 0000000..a5cf2ac --- /dev/null +++ b/fw/app/src/settings.h @@ -0,0 +1,11 @@ +/* + * 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 <stddef.h> + + +int settings_to_json(void *buffer, size_t data); diff --git a/fw/app/src/syslog.c b/fw/app/src/syslog.c index 47b2edb..700547b 100644 --- a/fw/app/src/syslog.c +++ b/fw/app/src/syslog.c @@ -7,6 +7,7 @@ #include <errno.h> #include <stdbool.h> #include <stdio.h> +#include <string.h> #include <string.h> #include <zephyr/init.h> @@ -33,7 +34,7 @@ LOG_MODULE_REGISTER(syslog); struct net_mgmt_event_callback l4_cb; static K_SEM_DEFINE(network_connected, 0, 1); -static char target_ip[IPV6_STRLEN_MAX] = "2001:db8::2"; +static char target_ip[IPV6_STRLEN_MAX + 1] = "2001:db8::2"; void l4_event_handler( struct net_mgmt_event_callback *cb, @@ -145,7 +146,13 @@ int syslog_handle_export(int (*cb)(const char *name, const void *value, int syslog_handle_get(const char *name, char *val, int val_len_max) { - LOG_WRN("Settings get not implemented"); + const char* next = NULL; + + if (settings_name_steq(name, "target/ip", &next) && !next) { + size_t len = strnlen(target_ip, sizeof(target_ip)); + memcpy(val, target_ip, MIN(len, val_len_max)); + } + return 0; } |