From 8bfece93b1e8835929d3f270cb61628abf2e3aef Mon Sep 17 00:00:00 2001
From: xengineering <me@xengineering.eu>
Date: Sun, 30 Mar 2025 13:38:05 +0200
Subject: fw: app: Reboot in case of no network

Because of an issue likely related to hardware design on the
nucleo_f767zi board (see issue [1] for details) the firmware should
reboot in case network access cannot be established after 4 seconds.

This makes the firmware more robust at the moment. As soon as
iot-contact hardware without this issue exists the behavior can be
changed again.

[1]: https://github.com/zephyrproject-rtos/zephyr/issues/77794
---
 fw/app/src/syslog.c | 13 ++++++++++---
 1 file changed, 10 insertions(+), 3 deletions(-)

(limited to 'fw')

diff --git a/fw/app/src/syslog.c b/fw/app/src/syslog.c
index b1a1077..edc2839 100644
--- a/fw/app/src/syslog.c
+++ b/fw/app/src/syslog.c
@@ -14,9 +14,11 @@
 #include <zephyr/logging/log_ctrl.h>
 #include <zephyr/logging/log_core.h>
 #include <zephyr/net/conn_mgr_connectivity.h>
+#include <zephyr/sys/reboot.h>
 
 LOG_MODULE_REGISTER(syslog);
 
+#define NETWORK_BUG_DELAY K_MSEC(4000)
 #define L4_EVENT_MASK (NET_EVENT_L4_CONNECTED | NET_EVENT_L4_DISCONNECTED)
 
 struct net_mgmt_event_callback l4_cb;
@@ -55,10 +57,15 @@ int init_syslog(void)
 {
 	LOG_DBG("Initializing syslog logging backend");
 
-	LOG_DBG("Waiting for network ...");
-	k_sem_take(&network_connected, K_FOREVER);
+	LOG_INF("Waiting for network ...");
+	int ret = k_sem_take(&network_connected, NETWORK_BUG_DELAY);
+	if (ret == -EAGAIN) {
+		LOG_ERR("Could not connect to network - rebooting system");
+		log_flush();
+		sys_reboot(SYS_REBOOT_COLD);
+	}
 
-	LOG_DBG("Enabling syslog backend");
+	LOG_INF("Enabling syslog backend");
 	const struct log_backend *backend = log_backend_net_get();
 	if (log_backend_is_active(backend) == false) {
 		/* flush log messages to ensure first syslog message is reproducible */
-- 
cgit v1.2.3-70-g09d2