|
| 1 | +# |
| 2 | +# psuutil.py |
| 3 | +# Platform-specific PSU status interface for SONiC |
| 4 | +# |
| 5 | + |
| 6 | + |
| 7 | +import os.path |
| 8 | + |
| 9 | +try: |
| 10 | + from sonic_psu.psu_base import PsuBase |
| 11 | +except ImportError as e: |
| 12 | + raise ImportError(str(e) + "- required module not found") |
| 13 | + |
| 14 | + |
| 15 | +class PsuUtil(PsuBase): |
| 16 | + """Platform-specific PSUutil class""" |
| 17 | + |
| 18 | + PSU_CPLD_DIR = "/sys/bus/i2c/devices/0-0033" |
| 19 | + |
| 20 | + def __init__(self): |
| 21 | + PsuBase.__init__(self) |
| 22 | + |
| 23 | + |
| 24 | + # Get sysfs attribute |
| 25 | + def get_attr_value(self, attr_path): |
| 26 | + |
| 27 | + retval = 'ERR' |
| 28 | + if (not os.path.isfile(attr_path)): |
| 29 | + return retval |
| 30 | + |
| 31 | + try: |
| 32 | + with open(attr_path, 'r') as fd: |
| 33 | + retval = fd.read() |
| 34 | + except Exception as error: |
| 35 | + logging.error("Unable to open ", attr_path, " file !") |
| 36 | + |
| 37 | + retval = retval.rstrip('\r\n') |
| 38 | + return retval |
| 39 | + |
| 40 | + def get_num_psus(self): |
| 41 | + """ |
| 42 | + Retrieves the number of PSUs available on the device |
| 43 | + :return: An integer, the number of PSUs available on the device |
| 44 | + """ |
| 45 | + MAX_PSUS = 2 |
| 46 | + return MAX_PSUS |
| 47 | + |
| 48 | + def get_psu_status(self, index): |
| 49 | + """ |
| 50 | + Retrieves the oprational status of power supply unit (PSU) defined |
| 51 | + by index <index> |
| 52 | + :param index: An integer, index of the PSU of which to query status |
| 53 | + :return: Boolean, True if PSU is operating properly, False if PSU is\ |
| 54 | + faulty |
| 55 | + """ |
| 56 | + status = 0 |
| 57 | + mask = [ 0x08, 0x10 ] |
| 58 | + attr_file = 'cpld_pw_good' |
| 59 | + attr_path = self.PSU_CPLD_DIR +'/'+ attr_file |
| 60 | + |
| 61 | + attr_value = self.get_attr_value(attr_path) |
| 62 | + |
| 63 | + if (attr_value != 'ERR'): |
| 64 | + attr_value = int(attr_value, 16) |
| 65 | + # Check for PSU status |
| 66 | + if (attr_value & mask[index-1]): |
| 67 | + status = 1 |
| 68 | + |
| 69 | + return status |
| 70 | + |
| 71 | + def get_psu_presence(self, index): |
| 72 | + """ |
| 73 | + Retrieves the presence status of power supply unit (PSU) defined |
| 74 | + by index <index> |
| 75 | + :param index: An integer, index of the PSU of which to query status |
| 76 | + :return: Boolean, True if PSU is plugged, False if not |
| 77 | + """ |
| 78 | + status = 0 |
| 79 | + mask = [ 0x01, 0x02 ] |
| 80 | + attr_file ='cpld_pw_abs' |
| 81 | + attr_path = self.PSU_CPLD_DIR +'/'+ attr_file |
| 82 | + |
| 83 | + attr_value = self.get_attr_value(attr_path) |
| 84 | + |
| 85 | + if (attr_value != 'ERR'): |
| 86 | + attr_value = int(attr_value, 16) |
| 87 | + # Check for PSU presence |
| 88 | + if (~attr_value & mask[index-1]): |
| 89 | + status = 1 |
| 90 | + |
| 91 | + return status |
| 92 | + |
0 commit comments