3
3
# Platform-specific PSU status interface for SONiC
4
4
#
5
5
6
- import os
7
-
6
+ import os .path
8
7
9
8
try :
10
9
from sonic_psu .psu_base import PsuBase
@@ -19,32 +18,42 @@ def __init__(self):
19
18
PsuBase .__init__ (self )
20
19
# DX010 PSU pin mapping
21
20
self .psu = [
22
- {'base' : self . get_gpio_base ()},
21
+ {'base' :216 }, # Reserved
23
22
{'abs' :27 , 'power' :22 },
24
23
{'abs' :28 , 'power' :25 }
25
24
]
26
25
27
- def get_gpio_base (self ):
28
- sys_gpio_dir = "/sys/class/gpio"
29
- for r in os .listdir (sys_gpio_dir ):
30
- if "gpiochip" in r :
31
- return int (r [8 :],10 )
32
- return 216 #Reserve
26
+ def init_psu_gpio (self , pinnum ):
27
+ # export pin, input as default
28
+ gpio_base = self .psu [0 ]['base' ]
29
+ export_file = "/sys/class/gpio/export"
30
+ direction_file = '/sys/class/gpio/gpio' + str (gpio_base + pinnum ) + '/direction'
31
+
32
+ try :
33
+ with open (export_file , 'w' ) as fd :
34
+ fd .write (str (gpio_base + pinnum ))
35
+ except Exception as error :
36
+ logging .error ("Unable to export gpio " , pinnum )
33
37
34
38
35
39
# Get a psu status and presence
36
40
def read_psu_statuses (self , pinnum ):
37
41
sys_gpio_dir = "/sys/class/gpio"
42
+ retval = 'ERR'
38
43
gpio_base = self .psu [0 ]['base' ]
39
44
40
45
gpio_dir = sys_gpio_dir + '/gpio' + str (gpio_base + pinnum )
41
46
gpio_file = gpio_dir + "/value"
47
+
48
+ # init gpio
49
+ if (not os .path .isdir (gpio_dir )):
50
+ self .init_psu_gpio (pinnum )
42
51
43
52
try :
44
53
with open (gpio_file , 'r' ) as fd :
45
54
retval = fd .read ()
46
- except IOError :
47
- raise IOError ("Unable to open " + gpio_file + "file !" )
55
+ except Exception as error :
56
+ logging . error ("Unable to open " , gpio_file , "file !" )
48
57
49
58
retval = retval .rstrip ('\r \n ' )
50
59
return retval
@@ -67,10 +76,11 @@ def get_psu_status(self, index):
67
76
"""
68
77
status = 0
69
78
psu_status = self .read_psu_statuses (self .psu [index ]['power' ])
70
- psu_status = int (psu_status , 10 )
71
- # Check for PSU status
72
- if (psu_status == 1 ):
73
- status = 1
79
+ if (psu_status != 'ERR' ):
80
+ psu_status = int (psu_status , 10 )
81
+ # Check for PSU status
82
+ if (psu_status == 1 ):
83
+ status = 1
74
84
75
85
return status
76
86
@@ -83,9 +93,10 @@ def get_psu_presence(self, index):
83
93
"""
84
94
status = 0
85
95
psu_absence = self .read_psu_statuses (self .psu [index ]['abs' ])
86
- psu_absence = (int (psu_absence , 10 ))
87
- # Check for PSU presence
88
- if (psu_absence == 0 ):
89
- status = 1
96
+ if (psu_absence != 'ERR' ):
97
+ psu_absence = (int (psu_absence , 10 ))
98
+ # Check for PSU presence
99
+ if (psu_absence == 0 ):
100
+ status = 1
90
101
91
102
return status
0 commit comments