summaryrefslogtreecommitdiff
path: root/fw/app/src/syslog.c
diff options
context:
space:
mode:
Diffstat (limited to 'fw/app/src/syslog.c')
-rw-r--r--fw/app/src/syslog.c79
1 files changed, 78 insertions, 1 deletions
diff --git a/fw/app/src/syslog.c b/fw/app/src/syslog.c
index e19a196..22aa034 100644
--- a/fw/app/src/syslog.c
+++ b/fw/app/src/syslog.c
@@ -4,8 +4,12 @@
* obtain one at https://mozilla.org/MPL/2.0/.
*/
+#include <errno.h>
#include <stdbool.h>
+#include <stdio.h>
+#include <string.h>
+#include <string.h>
#include <zephyr/init.h>
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
@@ -14,18 +18,24 @@
#include <zephyr/logging/log_ctrl.h>
#include <zephyr/logging/log_core.h>
#include <zephyr/net/conn_mgr_connectivity.h>
+#include <zephyr/settings/settings.h>
+#include <zephyr/sys/util.h>
#ifdef CONFIG_IOT_CONTACT_NETWORK_HACK
#include <zephyr/sys/reboot.h>
#endif // CONFIG_IOT_CONTACT_NETWORK_HACK
+#include "settings.h"
+
LOG_MODULE_REGISTER(syslog);
#define NETWORK_BUG_DELAY K_MSEC(4000)
#define L4_EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)
+#define SYSLOG_TARGET_PORT 514
struct net_mgmt_event_callback l4_cb;
static K_SEM_DEFINE(network_connected, 0, 1);
+static char target_ip[IPV6_STRLEN_MAX + 1] = "2001:db8::2";
void l4_event_handler(
struct net_mgmt_event_callback *cb,
@@ -87,4 +97,71 @@ int init_syslog(void)
return 0;
}
-SYS_INIT(init_syslog, APPLICATION, 50);
+SYS_INIT(init_syslog, APPLICATION, 40);
+
+int syslog_handle_set(const char *name, size_t len, settings_read_cb read_cb,
+ void *cb_arg)
+{
+ const char* next = NULL;
+
+ if (settings_name_steq(name, "target/ip", &next) && !next) {
+ memset(target_ip, '\0', sizeof(target_ip));
+ ssize_t ret = read_cb(cb_arg, target_ip, MIN(sizeof(target_ip) - 1, len));
+ if (ret < 0) {
+ LOG_ERR("Failed to set target IP (%d)", ret);
+ return (int)ret;
+ }
+ LOG_INF("Set target IP to '%s'", target_ip);
+ return 0;
+ }
+
+ return 0;
+}
+
+int syslog_handle_commit(void)
+{
+ char target[IPV6_STRLEN_MAX + 9]; // 9 for brackets, colon, port and \0
+
+ int ret = snprintf(target, sizeof(target), "[%s]:%d", target_ip,
+ SYSLOG_TARGET_PORT);
+ if (ret < 0) {
+ LOG_ERR("Failed to format target based on ip and port (%d)", ret);
+ return ret;
+ }
+
+ if (log_backend_net_set_addr(target) == false) {
+ LOG_ERR("Could not commit settings");
+ return -EINVAL;
+ }
+ LOG_INF("Committed settings");
+
+ return 0;
+}
+
+int syslog_handle_export(int (*cb)(const char *name, const void *value,
+ size_t val_len))
+{
+ LOG_WRN("Settings export not implemented");
+ return 0;
+}
+
+int syslog_handle_get(const char *name, char *val, int val_len_max)
+{
+ 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;
+}
+
+SETTINGS_STATIC_HANDLER_DEFINE(
+ syslog_settings_handler,
+ "syslog",
+ syslog_handle_get,
+ syslog_handle_set,
+ syslog_handle_commit,
+ syslog_handle_export
+);