#include "iwatchdog.h" #include #include #include #include #include #include #include #include Q_LOGGING_CATEGORY(flapp, "app.watchdog") #include "systemmanager.h" IWatchDog::IWatchDog(const QString &name, QObject *parent) :QObject(parent) ,m_dogName(name) ,m_fd(-1) ,m_feed(true), m_timer(Q_NULLPTR) { if(SystemManager::instance()->platformInfo().contains("t507")) { m_type =2; }else{ m_type =1; } } IWatchDog::~IWatchDog() { } bool IWatchDog::open(int timeoutSeconds, float feedSeconds) { m_timeoutTimes= timeoutSeconds; m_feedTimes = feedSeconds; this->checkoutTime(); m_fd = ::open(m_dogName.toLocal8Bit().constData(), O_WRONLY); if(m_fd <0) { qCDebug(flapp) << QString("open wachdog: %1, failured"); return m_fd; } if(m_timer == Q_NULLPTR) { m_timer = new QTimer(this); connect(m_timer, &QTimer::timeout, this, [=](){ feedDog(); emit feedCount(m_feedCount++); }); } m_feedCount =0; switch (m_type) { case 1: { char s ='V'; write(m_fd,(char *)&s,sizeof(s)); } break; default: break; } setTimeoutTime(m_timeoutTimes); setFeedTime(m_feedTimes); feedDog(); return m_fd; } bool IWatchDog::close() { bool bRet= true; if(m_fd > 0) { int flags = WDIOS_DISABLECARD; ioctl(m_fd, WDIOC_SETOPTIONS, &flags); this->setFeed(false); bRet = ::close(m_fd); m_fd = -1; } return bRet; } void IWatchDog::setFeed(bool bFeed) { if(bFeed) { feedDog(); m_timer->start(m_feedTimes*1000); qDebug()<<"start feed="<< m_feedTimes*1000; }else{ m_timer->stop(); } } void IWatchDog::setFeedTime(float seconds) { checkoutTime(); m_feedTimes = seconds; m_timer->start(m_feedTimes*1000); feedDog(); } void IWatchDog::setTimeoutTime(int seconds) { checkoutTime(); if(m_fd > 0) { m_timeoutTimes = seconds; int flags = m_timeoutTimes; ioctl(m_fd,WDIOC_SETTIMEOUT,&flags); } } bool IWatchDog::isOpen() { return (m_fd >0); } void IWatchDog::feedDog() { int dummy; ::ioctl(m_fd, WDIOC_KEEPALIVE,&dummy); } void IWatchDog::checkoutTime() { if(m_timeoutTimes <1) { m_timeoutTimes = 1; }else if(m_timeoutTimes >10) { m_timeoutTimes = 10; } if(m_feedTimes <0.1) { m_feedTimes = 0.1; }else if(m_feedTimes >10) { m_feedTimes = 10; } }