xref: /OK3568_Linux_fs/app/forlinx/flapp/src/plugins/watchdog/iwatchdog.cpp (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 #include "iwatchdog.h"
2 #include <QTimer>
3 #include <fcntl.h>
4 #include <unistd.h>
5 #include <sys/ioctl.h>
6 #include <linux/watchdog.h>
7 #include <QTimerEvent>
8 #include <QTimer>
9 #include <QLoggingCategory>
10 Q_LOGGING_CATEGORY(flapp,  "app.watchdog")
11 #include "systemmanager.h"
12 
IWatchDog(const QString & name,QObject * parent)13 IWatchDog::IWatchDog(const QString &name, QObject *parent)
14     :QObject(parent)
15     ,m_dogName(name)
16     ,m_fd(-1)
17     ,m_feed(true),
18       m_timer(Q_NULLPTR)
19 {
20     if(SystemManager::instance()->platformInfo().contains("t507"))
21     {
22         m_type =2;
23     }else{
24         m_type =1;
25     }
26 }
27 
~IWatchDog()28 IWatchDog::~IWatchDog()
29 {
30 }
31 
open(int timeoutSeconds,float feedSeconds)32 bool IWatchDog::open(int timeoutSeconds, float feedSeconds)
33 {
34 
35     m_timeoutTimes= timeoutSeconds;
36     m_feedTimes = feedSeconds;
37 
38     this->checkoutTime();
39 
40 
41     m_fd = ::open(m_dogName.toLocal8Bit().constData(),  O_WRONLY);
42     if(m_fd <0)
43     {
44         qCDebug(flapp) << QString("open wachdog: %1, failured");
45         return m_fd;
46     }
47 
48     if(m_timer == Q_NULLPTR)
49     {
50         m_timer = new QTimer(this);
51         connect(m_timer, &QTimer::timeout, this, [=](){
52             feedDog();
53             emit feedCount(m_feedCount++);
54         });
55     }
56 
57     m_feedCount =0;
58     switch (m_type) {
59     case 1:
60     {
61         char s ='V';
62         write(m_fd,(char *)&s,sizeof(s));
63     }
64         break;
65     default:
66         break;
67     }
68 
69     setTimeoutTime(m_timeoutTimes);
70     setFeedTime(m_feedTimes);
71     feedDog();
72 
73     return m_fd;
74 }
75 
close()76 bool IWatchDog::close()
77 {
78     bool bRet= true;
79     if(m_fd > 0)
80     {
81         int flags = WDIOS_DISABLECARD;
82         ioctl(m_fd, WDIOC_SETOPTIONS, &flags);
83         this->setFeed(false);
84         bRet = ::close(m_fd);
85         m_fd = -1;
86     }
87     return bRet;
88 }
89 
setFeed(bool bFeed)90 void IWatchDog::setFeed(bool bFeed)
91 {
92     if(bFeed)
93     {
94         feedDog();
95         m_timer->start(m_feedTimes*1000);
96         qDebug()<<"start feed="<< m_feedTimes*1000;
97     }else{
98         m_timer->stop();
99     }
100 }
101 
setFeedTime(float seconds)102 void IWatchDog::setFeedTime(float seconds)
103 {
104     checkoutTime();
105     m_feedTimes = seconds;
106     m_timer->start(m_feedTimes*1000);
107     feedDog();
108 }
109 
setTimeoutTime(int seconds)110 void IWatchDog::setTimeoutTime(int seconds)
111 {
112     checkoutTime();
113     if(m_fd > 0)
114     {
115         m_timeoutTimes = seconds;
116         int flags = m_timeoutTimes;
117         ioctl(m_fd,WDIOC_SETTIMEOUT,&flags);
118     }
119 }
120 
121 
isOpen()122 bool IWatchDog::isOpen()
123 {
124     return (m_fd >0);
125 }
126 
feedDog()127 void IWatchDog::feedDog()
128 {
129     int dummy;
130     ::ioctl(m_fd, WDIOC_KEEPALIVE,&dummy);
131 }
132 
checkoutTime()133 void IWatchDog::checkoutTime()
134 {
135     if(m_timeoutTimes <1)
136     {
137         m_timeoutTimes = 1;
138     }else if(m_timeoutTimes >10)
139     {
140         m_timeoutTimes = 10;
141     }
142 
143     if(m_feedTimes <0.1)
144     {
145         m_feedTimes = 0.1;
146     }else if(m_feedTimes >10)
147     {
148         m_feedTimes = 10;
149     }
150 }
151