From 93c0f3d353b10193584c2543669d4802b2a7173f Mon Sep 17 00:00:00 2001 From: BuGu <62454025+Z2Z-GuGu@users.noreply.github.com> Date: Mon, 10 Mar 2025 18:09:55 +0800 Subject: [PATCH] Add software watchdog. --- .../sg2002/kvm_system/main/include/config.h | 1 + .../main/lib/system_state/system_state.cpp | 20 ++++++++++++++++++- .../main/lib/system_state/system_state.h | 5 +++++ support/sg2002/kvm_system/main/src/main.cpp | 19 ++++++++++++++++-- 4 files changed, 42 insertions(+), 3 deletions(-) diff --git a/support/sg2002/kvm_system/main/include/config.h b/support/sg2002/kvm_system/main/include/config.h index 61aede8..0c7d5dc 100644 --- a/support/sg2002/kvm_system/main/include/config.h +++ b/support/sg2002/kvm_system/main/include/config.h @@ -36,6 +36,7 @@ #define WIFI_CONNECTION_DELAY 5000 #define OLED_SLEEP_DELAY_MIN 10 #define OLED_SLEEP_DELAY_DEFAULT 30 +#define KVM_WD_COUNT_MAX 10 typedef struct { int8_t page = 0; diff --git a/support/sg2002/kvm_system/main/lib/system_state/system_state.cpp b/support/sg2002/kvm_system/main/lib/system_state/system_state.cpp index de3b4b1..8fd3c63 100644 --- a/support/sg2002/kvm_system/main/lib/system_state/system_state.cpp +++ b/support/sg2002/kvm_system/main/lib/system_state/system_state.cpp @@ -36,13 +36,14 @@ int get_nic_state(const char* interface_name) return ret; } -int get_ping_allow_state() +int get_ping_allow_state(void) { if(access("/etc/kvm/stop_ping", F_OK) == 0) { kvm_sys_state.ping_allow = 0; } else { kvm_sys_state.ping_allow = 1; } + return kvm_sys_state.ping_allow; } // net_port @@ -466,3 +467,20 @@ uint8_t ion_free_space(void) { //cat /sys/kernel/debug/ion/cvi_carveout_heap_dump/summary | grep "usage rate:" | awk '{print $2}' } + +uint8_t watchdog_sf_is_open() +{ + if(access(watchdog_mode_path, F_OK) == 0) return 1; + else return 0; +} + +int check_watchdog() +{ + if(access(watchdog_file, F_OK) == 0) { + if (remove(watchdog_file) == 0) { + return 1; + } else { + return -1; + } + } else return 0; +} \ No newline at end of file diff --git a/support/sg2002/kvm_system/main/lib/system_state/system_state.h b/support/sg2002/kvm_system/main/lib/system_state/system_state.h index 9ebf393..7494d4f 100644 --- a/support/sg2002/kvm_system/main/lib/system_state/system_state.h +++ b/support/sg2002/kvm_system/main/lib/system_state/system_state.h @@ -13,6 +13,9 @@ enum ip_addr_t #define NIC_STATE_UNKNOWN -1 #define NIC_STATE_NO_EXIST -2 +#define watchdog_mode_path "/etc/kvm/watchdog" +#define watchdog_file "/tmp/nanokvm_wd" + // net_port int get_ip_addr(ip_addr_t ip_type); int chack_net_state(ip_addr_t use_ip_type); @@ -31,5 +34,7 @@ void kvm_update_rndis_state(void); void kvm_update_tailscale_state(void); uint8_t ion_free_space(void); int get_nic_state(const char* interface_name); +uint8_t watchdog_sf_is_open(); +int check_watchdog(); #endif // SYSTEM_STATE_H_ diff --git a/support/sg2002/kvm_system/main/src/main.cpp b/support/sg2002/kvm_system/main/src/main.cpp index 96a42a0..474b800 100644 --- a/support/sg2002/kvm_system/main/src/main.cpp +++ b/support/sg2002/kvm_system/main/src/main.cpp @@ -147,7 +147,6 @@ void* thread_sys_handle(void * arg) get_ping_allow_state(); while(kvm_sys_state.sys_thread_running) { - // printf("[kvmsys]main while start!\n"); // net if(kvm_sys_state.page == 0){ kvm_update_eth_state(); @@ -161,8 +160,8 @@ void* thread_sys_handle(void * arg) kvm_update_hdmi_res(); kvm_update_stream_type(); kvm_update_stream_qlty(); - kvm_wifi_web_config_process(); + } else if(kvm_sys_state.page == 1){ kvm_wifi_config_process(); } @@ -231,8 +230,24 @@ int main(int argc, char* argv[]) } // while(!app::need_exit()){ + uint8_t kvm_wd_count = 0; + int kvm_wd_state = 0; while(kvm_sys_state.sys_thread_running){ time::sleep_ms(1000); + if(watchdog_sf_is_open()){ + kvm_wd_state = check_watchdog(); + if(kvm_wd_state == 1) kvm_wd_count = 0; + else if(kvm_wd_state == 0) { + kvm_wd_count++; + printf("Vision service unresponsive : %d\n", kvm_wd_count); + } + if(kvm_wd_count > KVM_WD_COUNT_MAX){ + printf("Vision service unresponsive, restart now\n"); + system("reboot"); + } + } else { + kvm_wd_count = 0; + } } kvm_sys_state.sys_thread_running = 0; kvm_sys_state.oled_thread_running = 0;