Skip to content

Commit 50cfa77

Browse files
Wirut Getbamrunglguohan
Wirut Getbamrung
authored andcommitted
[device/celestica]: update psuutil follow new platform api (#3537)
Fixes #3518 Update psuutil API to detect PSU GPIO base from label
1 parent e9a0c57 commit 50cfa77

File tree

1 file changed

+34
-43
lines changed
  • device/celestica/x86_64-cel_seastone-r0/plugins

1 file changed

+34
-43
lines changed

device/celestica/x86_64-cel_seastone-r0/plugins/psuutil.py

+34-43
Original file line numberDiff line numberDiff line change
@@ -12,49 +12,51 @@
1212
raise ImportError(str(e) + "- required module not found")
1313

1414

15+
GPIO_DIR = "/sys/class/gpio"
16+
GPIO_LABEL = "pca9505"
17+
DX010_MAX_PSUS = 2
18+
19+
1520
class PsuUtil(PsuBase):
1621
"""Platform-specific PSUutil class"""
1722

1823
def __init__(self):
1924
PsuBase.__init__(self)
2025
# DX010 PSU pin mapping
21-
self.psu = [
22-
{'base': self.get_gpio_base()},
23-
{'abs':27, 'power':22},
24-
{'abs':28, 'power':25}
26+
self.dx010_psu_gpio = [
27+
{'base': self.__get_gpio_base()},
28+
{'prs': 27, 'status': 22},
29+
{'prs': 28, 'status': 25}
2530
]
2631

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
33-
34-
35-
# Get a psu status and presence
36-
def read_psu_statuses(self, pinnum):
37-
sys_gpio_dir = "/sys/class/gpio"
38-
gpio_base = self.psu[0]['base']
39-
40-
gpio_dir = sys_gpio_dir + '/gpio' + str(gpio_base+pinnum)
41-
gpio_file = gpio_dir + "/value"
42-
32+
def __read_txt_file(self, file_path):
4333
try:
44-
with open(gpio_file, 'r') as fd:
45-
retval = fd.read()
34+
with open(file_path, 'r') as fd:
35+
data = fd.read()
36+
return data.strip()
4637
except IOError:
47-
raise IOError("Unable to open " + gpio_file + "file !")
48-
49-
retval = retval.rstrip('\r\n')
50-
return retval
38+
pass
39+
return ""
40+
41+
def __get_gpio_base(self):
42+
for r in os.listdir(GPIO_DIR):
43+
label_path = os.path.join(GPIO_DIR, r, "label")
44+
if "gpiochip" in r and GPIO_LABEL in self.__read_txt_file(label_path):
45+
return int(r[8:], 10)
46+
return 216 # Reserve
47+
48+
def __get_gpio_value(self, pinnum):
49+
gpio_base = self.dx010_psu_gpio[0]['base']
50+
gpio_dir = GPIO_DIR + '/gpio' + str(gpio_base+pinnum)
51+
gpio_file = gpio_dir + "/value"
52+
retval = self.__read_txt_file(gpio_file)
53+
return retval.rstrip('\r\n')
5154

5255
def get_num_psus(self):
5356
"""
5457
Retrieves the number of PSUs available on the device
5558
:return: An integer, the number of PSUs available on the device
5659
"""
57-
DX010_MAX_PSUS = 2
5860
return DX010_MAX_PSUS
5961

6062
def get_psu_status(self, index):
@@ -65,14 +67,9 @@ def get_psu_status(self, index):
6567
:return: Boolean, True if PSU is operating properly, False if PSU is\
6668
faulty
6769
"""
68-
status = 0
69-
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
74-
75-
return status
70+
raw = self.__get_gpio_value(
71+
self.dx010_psu_gpio[index]['status'])
72+
return int(raw, 10) == 1
7673

7774
def get_psu_presence(self, index):
7875
"""
@@ -81,11 +78,5 @@ def get_psu_presence(self, index):
8178
:param index: An integer, index of the PSU of which to query status
8279
:return: Boolean, True if PSU is plugged, False if not
8380
"""
84-
status = 0
85-
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
90-
91-
return status
81+
raw = self.__get_gpio_value(self.dx010_psu_gpio[index]['prs'])
82+
return int(raw, 10) == 0

0 commit comments

Comments
 (0)