发表于:2005-01-04 10:00:00
20楼
IEI 的watchdog 例程
// Company : ICP Electronics Inc.
// Description : Demostrate how to use watch dog timer we provided.
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <curses.h>
#include <signal.h>
#include <sys/ioctl.h>
#include "wdt.h"
#define SCR_WIDTH 80
#define TOTAL_TIME 20
static char prompt_str[][80] = {
"The system will reset in 20 seconds",
"If you don't reset your computer, press [Ctrl-C] key to escape",
};
static char *countdown_str = "Elapsed Time : %02d seconds";
static int ctrlc_pressed = 0;
void ctrlc_signal(int sig)
{
ctrlc_pressed = 1;
signal(SIGINT, SIG_DFL);
}
int main(int argc, char **argv)
{
int fd;
int i, row ;
int elapsed_time = 0;
char buf[128];
fd = open("/dev/wdt", O_RDWR);
if ( fd < 0 )
{
fprintf(stderr, "fatal error on opening watch dog device.");
exit(1);
}
if ( ioctl(fd, IOCTL_WDT_SET_TIMEOUT, TOTAL_TIME) < 0 )
{
printf("can not set timeout\n");
return 1;
}
initscr();
noecho();
cbreak();
row = 5;
for (i = 0 ; i < sizeof(prompt_str) / sizeof(prompt_str[0]) ; i++)
{
mvprintw(row++, (SCR_WIDTH-strlen(prompt_str[i]))/2, prompt_str[i]);
}
row++;
refresh();
// install signal handler
ctrlc_pressed = 0;
signal(SIGINT, ctrlc_signal);
ioctl(fd, IOCTL_WDT_START);
for (elapsed_time = 0 ; ; elapsed_time++)
{
move(row, 0);
clrtoeol();
sprintf(buf, countdown_str, elapsed_time);
mvprintw(row, (SCR_WIDTH-strlen(buf))/2, buf);
sleep(1);
if ( ctrlc_pressed )
{
move(row, 0);
clrtoeol();
sprintf(buf, "Return to shell");
mvprintw(row, (SCR_WIDTH-strlen(buf))/2, buf);
refresh();
break;
}
refresh();
}
ioctl(fd, IOCTL_WDT_STOP);
endwin();
close(fd);
return 0;
}