/**************************************************************************
* Qt 控制watchdog app hacking
* 声明:
* 本文主要是为了查看之前朋友留下的Qt控制watchdog软件运作机制。
*
* 2015-12-31 深圳 南山平山村 曾剑锋
*************************************************************************/ 一、参考文档:
. Qt Documentation:
http://doc.qt.io/qt-4.8/qbasictimer.html
. QTimer和QBasicTimer使用:
http://blog.csdn.net/thinkme2010/article/details/9112785
. stm32独立看门狗和窗口看门狗的区别是什么?
http://www.zhixinrui.com/thread-1192-1-1.html
. Linux下Watchdog
http://blog.csdn.net/hshl1214/article/details/6248942 二、QBasicTimer Class:
This is a fast, lightweight, and low-level class used by Qt internally. We recommend using the higher-level QTimer class rather than this class if you want to use timers in your applications. Note that this timer is a repeating timer that will send subsequent timer events unless the stop() function is called. 三、Linux下watchdog的工作原理
Watchdog在实现上可以是硬件电路也可以是软件定时器,能够在系统出现故障时自动重新启动系统。在Linux 内核下, watchdog的基本工作原理是:当watchdog启动后(即/dev/watchdog 设备被打开后),如果在某一设定的时间间隔内/dev/watchdog没有被执行写操作, 硬件watchdog电路或软件定时器就会重新启动系统。 四、cat mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/types.h>
#include <linux/watchdog.h>
#include <QMessageBox> MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this); fd = ;
times = ;
} MainWindow::~MainWindow()
{
delete ui;
::close(fd);
} void MainWindow::timerEvent(QTimerEvent *)
{
int dummy;
QString str; times++; /**
* timer每次都会执行,有点不合理
*/
if(ui->checkBox->isChecked())
::ioctl(fd,WDIOC_KEEPALIVE,&dummy); ui->times->setText(str.sprintf("%d",times));
} void MainWindow::on_pushButton_clicked()
{
// 打开watchdog设备文件,就相当于开机了watchdog,并设置了一个默认值
// 这个默认值是多少,目前不知道,去跟一下驱动,应该是可以获知的。
if(ui->pushButton->text() == QString("open watchdog"))
{
ui->pushButton->setText("close watchdog");
ui->checkBox->setDisabled(true); fd = ::open("/dev/watchdog",O_WRONLY);
if(fd < )
{
QMessageBox::about(this,"error","open watchdog failure");
exit(-);
} times = ;
ui->times->setText("");
timer.start(,this);
}
else // 关闭fd,就相当于关闭了watchdog
{
ui->pushButton->setText("open watchdog");
ui->checkBox->setEnabled(true);
timer.stop();
::close(fd);
}
} void MainWindow::moveEvent(QMoveEvent *)
{
this->move(QPoint(,));
} void MainWindow::resizeEvent(QResizeEvent *)
{
this->showMaximized();
} void MainWindow::closeEvent(QCloseEvent *)
{
exit();
}