diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/etc/systemd/system/femto-watchclock-dog.service b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/etc/systemd/system/femto-watchclock-dog.service new file mode 100644 index 0000000..454c957 --- /dev/null +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/etc/systemd/system/femto-watchclock-dog.service @@ -0,0 +1,12 @@ +[Unit] +Description=Monitor time jumps and restart meshtasticd +After=network.target + +[Service] +ExecStart=/usr/local/bin/femto-watchclock-dog.sh +Restart=always +User=root + +[Install] +WantedBy=multi-user.target + diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/lib/systemd/system/wifi-mesh-control.service b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/etc/systemd/system/femto-wifi-mesh-control.service similarity index 66% rename from foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/lib/systemd/system/wifi-mesh-control.service rename to foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/etc/systemd/system/femto-wifi-mesh-control.service index 3860f53..c01cff4 100644 --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/lib/systemd/system/wifi-mesh-control.service +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/etc/systemd/system/femto-wifi-mesh-control.service @@ -1,11 +1,11 @@ [Unit] Description=Meshtastic control of wifi After=meshtasticd.service -Requires=meshtasticd.service [Service] -ExecStart=/usr/local/bin/wifi-mesh-control.sh +ExecStart=/usr/local/bin/femto-wifi-mesh-control.sh Restart=always +RestartSec=5 User=root [Install] diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-watchclock-dog.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-watchclock-dog.sh new file mode 100644 index 0000000..6cefdfa --- /dev/null +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-watchclock-dog.sh @@ -0,0 +1,52 @@ +#!/usr/bin/env bash + +THRESHOLD=$((7 * 24 * 60 * 60)) # 1 week in seconds +LOGFILE="/var/log/time_change.log" +LAST_TIME_FILE="/tmp/last_time" + +# Ensure the log file exists and is writable +if [ ! -f "$LOGFILE" ]; then + touch "$LOGFILE" || { echo "ERROR: Cannot create log file $LOGFILE"; exit 1; } +fi +if [ ! -w "$LOGFILE" ]; then + echo "ERROR: Log file $LOGFILE is not writable" + exit 1 +fi + +# Ensure the last time file exists and is writable +if [ ! -f "$LAST_TIME_FILE" ]; then + date +%s > "$LAST_TIME_FILE" || { echo "ERROR: Cannot create last time file $LAST_TIME_FILE"; exit 1; } +fi +if [ ! -w "$LAST_TIME_FILE" ]; then + echo "ERROR: Last time file $LAST_TIME_FILE is not writable" + exit 1 +fi + +while true; do + NEW_TIME=$(date +%s) + + # Ensure last_time file is readable before using it + if [ ! -r "$LAST_TIME_FILE" ]; then + echo "ERROR: Last time file $LAST_TIME_FILE is not readable" | tee -a "$LOGFILE" + exit 1 + fi + + OLD_TIME=$(cat "$LAST_TIME_FILE") + TIME_DIFF=$((NEW_TIME - OLD_TIME)) + + if [ "${TIME_DIFF#-}" -ge "$THRESHOLD" ]; then + echo "$(date) - Large time change detected ($TIME_DIFF seconds), restarting meshtasticd" | tee -a "$LOGFILE" + systemctl restart meshtasticd + fi + + # Ensure last_time file is writable before updating it + if [ -w "$LAST_TIME_FILE" ]; then + echo "$NEW_TIME" > "$LAST_TIME_FILE" + else + echo "ERROR: Failed to write to $LAST_TIME_FILE" | tee -a "$LOGFILE" + exit 1 + fi + + sleep 5 +done + diff --git a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/wifi-mesh-control.sh b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-wifi-mesh-control.sh old mode 100755 new mode 100644 similarity index 93% rename from foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/wifi-mesh-control.sh rename to foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-wifi-mesh-control.sh index 9192e03..6cfa7ba --- a/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/wifi-mesh-control.sh +++ b/foxbuntu/sysdrv/out/rootfs_uclibc_rv1106/usr/local/bin/femto-wifi-mesh-control.sh @@ -17,11 +17,13 @@ get_mobile_wifi_state() { set_mobile_wifi_state() { local state="$1" local meshtastic_output + if [[ "$state" == "up" ]]; then - meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1) + meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled true 2>&1 || true) else - meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled false 2>&1) + meshtastic_output=$(meshtastic --host 127.0.0.1 --set network.wifi_enabled false 2>&1 || true) fi + log "Set Meshtastic Wi-Fi state to $state. Output: $meshtastic_output" } @@ -67,8 +69,8 @@ monitor_changes() { while true; do PID=$(ps -C meshtasticd -o pid= | tr -d ' ') - #if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID"; then - if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID" && [[ -d /sys/class/net/wlan0 ]]; then + #if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID" && [[ -d /sys/class/net/wlan0 ]]; then + if [[ -n "$PID" ]] && sudo lsof /dev/spidev0.0 | grep -q "$PID" && [[ -d /sys/class/net/wlan0 ]] && systemctl is-active --quiet meshtasticd; then local current_mobile_state current_wlan_state current_mobile_state=$(get_mobile_wifi_state) current_wlan_state=$(cat /sys/class/net/wlan0/operstate)