8
8
import os
9
9
import fcntl
10
10
import array
11
-
11
+ import time
12
12
from sonic_platform_base .watchdog_base import WatchdogBase
13
13
14
14
""" ioctl constants """
35
35
WDIOS_ENABLECARD = 0x0002
36
36
37
37
""" watchdog sysfs """
38
- WD_SYSFS_PATH = "/sys/class/watchdog/"
38
+ WD_SYSFS_PATH = "/sys/class/watchdog/watchdog0/ "
39
39
40
40
WD_COMMON_ERROR = - 1
41
41
@@ -52,16 +52,32 @@ def __init__(self, wd_device_path):
52
52
@param wd_device_path Path to watchdog device
53
53
"""
54
54
super (WatchdogImplBase , self ).__init__ ()
55
-
55
+
56
+ self .watchdog = ""
56
57
self .watchdog_path = wd_device_path
57
- self .watchdog = os .open (self .watchdog_path , os .O_WRONLY )
58
-
59
- # Opening a watchdog descriptor starts
60
- # watchdog timer; by default it should be stopped
61
- self ._disablewatchdog ()
62
- self .armed = False
58
+ self .wd_state_reg = WD_SYSFS_PATH + "state"
59
+ self .wd_timeout_reg = WD_SYSFS_PATH + "timeout"
60
+ self .wd_timeleft_reg = WD_SYSFS_PATH + "timeleft"
61
+
63
62
self .timeout = self ._gettimeout ()
64
63
64
+ def _read_sysfs_file (self , sysfs_file ):
65
+ # On successful read, returns the value read from given
66
+ # reg_name and on failure returns 'ERR'
67
+ rv = 'ERR'
68
+
69
+ if (not os .path .isfile (sysfs_file )):
70
+ return rv
71
+ try :
72
+ with open (sysfs_file , 'r' ) as fd :
73
+ rv = fd .read ()
74
+ except Exception as e :
75
+ rv = 'ERR'
76
+
77
+ rv = rv .rstrip ('\r \n ' )
78
+ rv = rv .lstrip (" " )
79
+ return rv
80
+
65
81
def _disablewatchdog (self ):
66
82
"""
67
83
Turn off the watchdog timer
@@ -102,11 +118,10 @@ def _gettimeout(self):
102
118
Get watchdog timeout
103
119
@return watchdog timeout
104
120
"""
121
+ timeout = 0
122
+ timeout = self ._read_sysfs_file (self .wd_timeout_reg )
105
123
106
- req = array .array ('I' , [0 ])
107
- fcntl .ioctl (self .watchdog , WDIOC_GETTIMEOUT , req , True )
108
-
109
- return int (req [0 ])
124
+ return timeout
110
125
111
126
def _gettimeleft (self ):
112
127
"""
@@ -127,15 +142,20 @@ def arm(self, seconds):
127
142
ret = WD_COMMON_ERROR
128
143
if seconds < 0 :
129
144
return ret
130
-
145
+
146
+ # Stop the watchdog service to gain access of watchdog file pointer
147
+ if self .is_armed ():
148
+ os .popen ("systemctl stop cpu_wdt.service" )
149
+ time .sleep (2 )
150
+ if not self .watchdog :
151
+ self .watchdog = os .open (self .watchdog_path , os .O_WRONLY )
131
152
try :
132
153
if self .timeout != seconds :
133
154
self .timeout = self ._settimeout (seconds )
134
- if self .armed :
155
+ if self .is_armed () :
135
156
self ._keepalive ()
136
157
else :
137
158
self ._enablewatchdog ()
138
- self .armed = True
139
159
ret = self .timeout
140
160
except IOError :
141
161
pass
@@ -150,22 +170,31 @@ def disarm(self):
150
170
A boolean, True if watchdog is disarmed successfully, False
151
171
if not
152
172
"""
153
-
154
- try :
155
- self ._disablewatchdog ()
156
- self .armed = False
157
- self .timeout = 0
158
- except IOError :
159
- return False
173
+
174
+ if self .is_armed ():
175
+ os .popen ("systemctl stop cpu_wdt.service" )
176
+ time .sleep (2 )
177
+ if not self .watchdog :
178
+ self .watchdog = os .open (self .watchdog_path , os .O_WRONLY )
179
+ try :
180
+ self ._disablewatchdog ()
181
+ self .timeout = 0
182
+ except IOError :
183
+ return False
160
184
161
185
return True
162
186
163
187
def is_armed (self ):
164
188
"""
165
189
Implements is_armed WatchdogBase API
166
190
"""
191
+ status = False
192
+
193
+ state = self ._read_sysfs_file (self .wd_state_reg )
194
+ if (state != 'inactive' ):
195
+ status = True
167
196
168
- return self . armed
197
+ return status
169
198
170
199
def get_remaining_time (self ):
171
200
"""
@@ -174,10 +203,7 @@ def get_remaining_time(self):
174
203
175
204
timeleft = WD_COMMON_ERROR
176
205
177
- if self .armed :
178
- try :
179
- timeleft = self ._gettimeleft ()
180
- except IOError :
181
- pass
206
+ if self .is_armed ():
207
+ timeleft = self ._read_sysfs_file (self .wd_timeleft_reg )
182
208
183
- return timeleft
209
+ return int ( timeleft )
0 commit comments