[点击并拖拽以移动]
内核选中红框部分,确保有/dev/watchdog
include <errno.h>
include <fcntl.h>
include <stdlib.h>
include <string.h>
include <unistd.h>
include "stdio.h"
include <unistd.h> / System V /
include <sys/ioctl.h> / BSD and Linux /
include <stropts.h> / XSI STREAMS /
include <linux/watchdog.h>
define DEV_NAME "/dev/watchdog"
int fd;
/*
- @brief 初始化看门狗
- @param interval 喂狗时间
- @param margin 喂狗间隔保留时间
- @return int
- @create at 2019.09.04 by Eric.Xi
*/
int watch_dog_init(int interval,int margin)
{
printf("started (interval %d, margin %d)!\n", interval, margin);
fd = open(DEV_NAME, O_RDWR|O_CLOEXEC);
if (fd == -1) {
printf("Failed to open %s: %s\n", DEV_NAME, strerror(errno));
return 1;
}
int timeout = interval + margin;
int ret = ioctl(fd, WDIOC_SETTIMEOUT, &timeout);
if (ret) {
printf("Failed to set timeout to %d: %s\n", timeout, strerror(errno));
ret = ioctl(fd, WDIOC_GETTIMEOUT, &timeout);
if (ret) {
printf("Failed to get timeout: %s\n", strerror(errno));
} else {
if (timeout > margin) {
interval = timeout - margin;
} else {
interval = 1;
}
printf("Adjusted interval to timeout returned by driver:"
" timeout %d, interval %d, margin %d\n",
timeout, interval, margin);
}
}
return 0;
}
/*
- @brief 喂看门狗
- @param interval 喂狗时间
- @param fd 看门狗句柄
- @return int
- @create at 2019.09.04 by Eric.Xi
*/
int watch_dog_feed(int fd, int interval)
{
write(fd, "", 1);
sleep(interval);
return 0;
}
/*
- @brief 看门狗使用demo
- @return int
- @create at 2019.09.04 by Eric.Xi
*/
int main(int argc, char **argv)
{
int interval = 1;
int margin =2;
int ret;
ret = watch_dog_init(interval,margin);
if(ret<0){
return -1;
}
while (true) {
watch_dog_feed(fd ,interval);
printf("------feed--------\n");
}
}