Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revision Previous revision
Next revision
Previous revision
Next revisionBoth sides next revision
docs:guide-user:hardware:watchdog [2019/07/13 23:32] – [USB Watchdogs] bobafetthotmaildocs:guide-user:hardware:watchdog [2021/11/01 11:39] – [USB watchdogs] vgaetera
Line 1: Line 1:
-======Hardware Watchdog====== +====== Hardware watchdog ====== 
- +While older versions of OpenWrt used the watchdog daemon from BusyBox, all new versions implement the watchdog daemon via procd, which is the init process (PID1). 
-While older versions of OpenWrt used the watchdog daemon from busybox, all new versions implement the watchdog daemon via procd, which is the init process (PID1). Therefore on modern OpenWrt, you will never see the watchdog process running. +Therefore on modern OpenWrt, you will never see the watchdog process running.
- +
-====Supported Hardware====+
  
 +===== Supported hardware =====
 Most embedded devices will have their hardware watchdog enabled in the default kernel configuration of their target, which means it will be built-in (no kmod to install, it's always available). Most embedded devices will have their hardware watchdog enabled in the default kernel configuration of their target, which means it will be built-in (no kmod to install, it's always available).
  
-For x86 hardware you have some kmods to choose, **kmod-it87-wdt kmod-sp5100_tco** which are for AMD hardware, **kmod-itco-wdt** for Intel hardware and **kmod-w83627hf-wdt** that can work for both.+For x86 hardware you have some kmods to choose
 +  AMD: [[packages:pkgdata:kmod-it87-wdt]], [[packages:pkgdata:kmod-sp5100_tco]] 
 +  Intel: [[packages:pkgdata:kmod-itco-wdt]] 
 +  Generic: [[packages:pkgdata:kmod-w83627hf-wdt]]
  
 There are also USB-based watchdogs, which will be discussed below in their own sub-section because they are not handled by procd in any way. There are also USB-based watchdogs, which will be discussed below in their own sub-section because they are not handled by procd in any way.
  
-The procd watchdog code always uses the primary watchdog device /dev/watchdog. You can configure what watchdog that is (ie GSC Watchdog or SoC watchdog) by disabling all but the desired watchdog in the kernel configuration, or not installing watchdog kmods for x86 architecture. +The procd watchdog code always uses the primary watchdog device ''/dev/watchdog''. 
- +You can configure what watchdog that is (i.e. GSC Watchdog or SoC watchdog) by disabling all but the desired watchdog in the kernel configuration, or not installing watchdog kmods for x86 architecture.
-====Controlling the Watchdog====+
  
 +===== Controlling the watchdog =====
 You can see the current configuration of the watchdog service via ubus: You can see the current configuration of the watchdog service via ubus:
  
-If there is a watchdog available: +<code bash
-<code> +# ubus call system watchdog
-root@OpenWrt:/# ubus call system watchdog+
 { {
         "status": "running",         "status": "running",
         "timeout": 30,         "timeout": 30,
         "frequency": 5         "frequency": 5
-}</code>+} 
 +</code>
  
-If no watchdog is available+If no watchdog is available, the status parameter is ''offline''.
-<code> +
-root@OpenWrt:/# ubus call system watchdog +
-+
-        "status": "offline", +
-        "timeout": 30, +
-        "frequency":+
-}</code>+
  
 +While there is no UCI configuration available for these options, you can change them in an rc script such as ''/etc/rc.local''.
  
-While there is no uci configuration available for these options you could change them in an rc script such as rc.local if you wish:+<code bash> 
 +# Change timeout to 60s 
 +ubus call system watchdog '{"timeout":60}'
  
-change to 60s timeout +# Change frequency to 1s 
-<code> +ubus call system watchdog '{"frequency":1}'
-ubus call system watchdog '{ "timeout": 60 }'   +
 </code> </code>
  
-change to 1s frequency +To bypass procd, enable the magicclose feature, stop the service and control the watchdog manually: 
-<code> + 
-ubus call system watchdog '{ "frequency": }'   +<code bash
-</code> +ubus call system watchdog '{"magicclose":true}' 
-To stop the service: +ubus call system watchdog '{"stop":true}' 
-<code> +while :; do echo 1 > /dev/watchdog; sleep 5; done
-ubus call system watchdog '{ "stop": true }'   # watchdog will cause a reset after it expires+
 </code> </code>
  
 +Note that watchdog will cause a reset after it expires.
  
-====Manually control hardware watchdog====+===== USB watchdogs ===== 
 +USB-based watchdogs are basically a microcontroller over a USB-serial dongle, that will close/open contacts when they trigger. 
 +These contacts are usually on a header that can be connected to the Power and Reset switch of the board, or to a relay wired to interrupt the power supply to the device.
  
-If you just stop procd from tickling the hardware watchdog you still can't manually tickle watchdog: +These are some photos of the device I am using:
- +
-<code> +
-root@OpenWrt:~# ubus call system watchdog '{"stop": true}'   +
-+
-        "status": "stopped", +
-        "timeout": 30, +
-        "frequency": 5, +
-        "magicclose": false +
-+
-root@OpenWrt:~# echo 1 > /dev/watchdog   +
--ash: can't create /dev/watchdog: Resource busy +
-</code> +
-But if you enable magicclose feature then you have control: +
-<code> +
-root@OpenWrt:~# ubus call system watchdog '{"magicclose": true}'   +
-+
-        "status": "running", +
-        "timeout": 30, +
-        "frequency": 5, +
-        "magicclose": true +
-+
- +
-root@OpenWrt:~# ubus call system watchdog '{"stop": true}'   +
-+
-        "status": "offline", +
-        "timeout": 0, +
-        "frequency": 0, +
-        "magicclose": true +
-+
-root@OpenWrt:~# echo 1 > /dev/watchdog   +
-root@OpenWrt:~# +
-</code> +
  
 +{{:docs:guide-user:hardware:usb_watchdog.jpg?direct&400|}}
 +{{:docs:guide-user:hardware:usb_watchdog2.jpg?direct&400|}}
  
-======USB Watchdogs====== +This kind of task is trivially easy to also do with an Arduino or Attiny board and some relays.
-USB-based watchdogs are basically a microcontroller over a USB-serial dongle, that will close/open contacts when they trigger. These contacts are usually on a header that can be connected to the Power and Reset switch of the board, or to a relay wired to interrupt the power supply to the device. +
-This is trivially easy to also do with an Arduino or Attiny board.+
  
 Some USB-based watchdogs are not using a serial dongle, but show up as USB HID devices and I don't know what is their protocol. Some USB-based watchdogs are not using a serial dongle, but show up as USB HID devices and I don't know what is their protocol.
  
-This is a script that will generate a Procd service to control one of such USB-serial watchdogs+This is a script that will generate a procd service to control one of such USB-serial watchdogs.
  
-<code bash usb_serial>+<code bash> 
 +cat << "EOF" > /etc/usb_watchdog
 #!/bin/sh #!/bin/sh
  
-#watchdog control script for USB-serial based watchdogs+Watchdog control script for USB-serial based watchdogs
  
-the command to run the watchdog 160 seconds is +The command to run the watchdog 160 seconds is: 
-#echo -en '\x10> /dev/ttyUSB0 +# echo -n -e "\x10> /dev/ttyUSB0 
-#the "x10" is 16 in hexadecimal +The "x10" is 16 in hexadecimal and will tell the watchdog to run for 160 seconds, setting a higher (or lower) time is possible, 
-and will tell the watchdog to run for 160 seconds, setting a higher (or lower) time is possible, +# just write a different hexadecimal number, and adjust the number of seconds in the sleep command below.
-# just write a different hexadecimal number, and adjust the number of seconds in the sleep command below+
 # DO NOT use a too small time though or the system will not have enough time to fully reboot before the watchdog triggers again. # DO NOT use a too small time though or the system will not have enough time to fully reboot before the watchdog triggers again.
  
-#first we are detecting what usb serial we need to use, to avoid sending commands to the wrong device, like LTE modems +First we are detecting what usb serial we need to use, to avoid sending commands to the wrong device, like LTE modems. 
-#the device I used appears like this in dmesg when I connect it +The device I used appears like this in dmesg when I connect it: 
-#[11705.304457] usb 4-1: new full-speed USB device number 2 using uhci_hcd +# [11705.304457] usb 4-1: new full-speed USB device number 2 using uhci_hcd 
-#[11705.538687] ch341 4-1:1.0: ch341-uart converter detected +# [11705.538687] ch341 4-1:1.0: ch341-uart converter detected 
-#[11705.549626] usb 4-1: ch341-uart converter now attached to ttyUSB0 +# [11705.549626] usb 4-1: ch341-uart converter now attached to ttyUSB0 
-#so I filter for ch341 serial chips+So I filter for ch341 serial chips.
  
-usb_serial=$(dmesg | grep ch341 | grep tty | awk '{print $NF}' )+usb_serial="$(dmesg | sed -n -e "/\sch341.*\stty/s/^.*\s//p")" 
 +cmd="${1:-start}"
  
-if [ "$usb_serial" = '' ]then +if [ -z "${usb_serial}" ] 
- echo no ch341 serial dongle detected, no watchdog available +then 
- exit 1+    echo "no ch341 serial dongle detected, no watchdog available" 
 +    exit 1
 fi fi
  
-case "$1" in+case "${cmd}" in
  
-stop)+(start) 
 +echo "starting usb watchdog service"
  
 +while [ ! -f /tmp/stop_usb_watchdog ]
 +do
 +    echo -n -e "\x10" > /dev/"${usb_serial}"
 +    sleep 30
 +done
 +rm /tmp/stop_usb_watchdog
 +
 +echo "stopping usb watchdog service"
 +;;
 +
 +(stop)
 touch /tmp/stop_usb_watchdog touch /tmp/stop_usb_watchdog
-exit 0 
 ;; ;;
  
-install) +(install) 
- +cat << "EOI" > /etc/init.d/usb_watchdog
-cat > /etc/init.d/usb_watchdog <<'END_OF_MESSAGE'+
 #!/bin/sh /etc/rc.common #!/bin/sh /etc/rc.common
 +
 USE_PROCD=1 USE_PROCD=1
 START=95 START=95
 STOP=01 STOP=01
 +
 start_service() { start_service() {
     procd_open_instance     procd_open_instance
-    procd_set_param command /bin/sh "/etc/usb_watchdog" +    procd_set_param command /bin/sh /etc/usb_watchdog 
-    #if process dies sooner than respawn_threshold, it is considered crashed and after 5 retries the service is stopped+    # If process dies sooner than respawn_threshold, it is considered crashed and after 5 retries the service is stopped
     procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}     procd_set_param respawn ${respawn_threshold:-3600} ${respawn_timeout:-5} ${respawn_retry:-5}
     procd_close_instance     procd_close_instance
 } }
  
-stop_service() +stop_service() { 
-+    /etc/usb_watchdog stop
-        /etc/usb_watchdog stop+
 } }
 +EOI
  
-END_OF_MESSAGE +cat << "EOI" > /lib/upgrade/keep.d/usb_watchdog
- +
-chmod +x /etc/init.d/usb_watchdog +
- +
-cat > /lib/upgrade/keep.d/usb_watchdog <<'END_OF_MESSAGE' +
-/etc/usb_watchdog+
 /etc/init.d/usb_watchdog /etc/init.d/usb_watchdog
 /etc/rc.d/ /etc/rc.d/
-END_OF_MESSAGE+/etc/usb_watchdog 
 +EOI
  
 +chmod +x /etc/init.d/usb_watchdog
 /etc/init.d/usb_watchdog enable /etc/init.d/usb_watchdog enable
 /etc/init.d/usb_watchdog start /etc/init.d/usb_watchdog start
- 
-grep -q "usb_watchdog" /etc/rc.local 
- 
-if [ "$?" = 1 ]; then 
- sed -i '/exit 0/i /etc/init.d/usb_watchdog enable' /etc/rc.local 
-fi 
-exit 0 
 ;; ;;
  
-remove) +(remove) 
-rm /lib/upgrade/keep.d/usb_watchdog +/etc/init.d/usb_watchdog disable 
-rm /etc/usb_watchdog +/etc/init.d/usb_watchdog stop 
-rm /etc/init.d/usb_watchdog +rm -f /etc/init.d/usb_watchdog 
-sed -i 's|/etc/init.d/usb_watchdog enable||g' /etc/rc.local +rm -/etc/usb_watchdog 
-exit 0+rm -f /lib/upgrade/keep.d/usb_watchdog
 ;; ;;
  
 esac esac
 +EOF
 +chmod +x /etc/usb_watchdog
 +/etc/usb_watchdog install
 +</code>
  
-echo starting usb watchdog service+To install it, write the following commands in your device's command line interface:
  
-while [ ! -/tmp/stop_usb_watchdog ]; do+<code bash> 
 +uclient-fetch -O usb-watchdog.sh "https://openwrt.org/_export/code/docs/guide-user/hardware/watchdog?codeblock=3" 
 +. ./usb-watchdog.sh 
 +</code>
  
- echo -en '\x10' > /dev/"$usb_serial" +You can then control it as normal for a procd service:
- sleep 30+
  
-done +<code bash> 
- +/etc/init.d/usb_watchdog
-rm /tmp/stop_usb_watchdog +
- +
-echo stopping usb watchdog service+
 </code> </code>
  
-To install it, write the following commands in your device's command line interface: +This script also writes the system configuration to survive a system upgrade, so you shouldn't need to reinstall it when you upgrade.
-<code>wget -O /etc/usb_watchdog http://openwrt.org/_export/code/docs/guide-user/hardware/watchdog?codeblock=7 +
-chmod +x /etc/usb_watchdog +
-/etc/usb_watchdog install +
-</code>+
  
-This script also writes the system configuration to survive a system upgrade, so you shouldn't need to reinstall it when you upgrade. 
  • Last modified: 2023/08/21 09:20
  • by vgaetera