|
| 1 | +# |
| 2 | +# psuutil.py |
| 3 | +# Platform-specific PSU status interface for SONiC |
| 4 | +# |
| 5 | + |
| 6 | +import os.path |
| 7 | + |
| 8 | +try: |
| 9 | + from sonic_psu.psu_base import PsuBase |
| 10 | +except ImportError as e: |
| 11 | + raise ImportError(str(e) + "- required module not found") |
| 12 | + |
| 13 | + |
| 14 | +class PsuUtil(PsuBase): |
| 15 | + """Platform-specific PSUutil class""" |
| 16 | + |
| 17 | + def __init__(self): |
| 18 | + PsuBase.__init__(self) |
| 19 | + # DX010 PSU pin mapping |
| 20 | + self.psu = [ |
| 21 | + {'base':216}, # Reserved |
| 22 | + {'abs':27, 'power':22}, |
| 23 | + {'abs':28, 'power':25} |
| 24 | + ] |
| 25 | + |
| 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) |
| 37 | + |
| 38 | + |
| 39 | + # Get a psu status and presence |
| 40 | + def read_psu_statuses(self, pinnum): |
| 41 | + sys_gpio_dir = "/sys/class/gpio" |
| 42 | + retval = 'ERR' |
| 43 | + gpio_base = self.psu[0]['base'] |
| 44 | + |
| 45 | + gpio_dir = sys_gpio_dir + '/gpio' + str(gpio_base+pinnum) |
| 46 | + gpio_file = gpio_dir + "/value" |
| 47 | + |
| 48 | + # init gpio |
| 49 | + if (not os.path.isdir(gpio_dir)): |
| 50 | + self.init_psu_gpio(pinnum) |
| 51 | + |
| 52 | + try: |
| 53 | + with open(gpio_file, 'r') as fd: |
| 54 | + retval = fd.read() |
| 55 | + except Exception as error: |
| 56 | + logging.error("Unable to open ", gpio_file, "file !") |
| 57 | + |
| 58 | + retval = retval.rstrip('\r\n') |
| 59 | + return retval |
| 60 | + |
| 61 | + def get_num_psus(self): |
| 62 | + """ |
| 63 | + Retrieves the number of PSUs available on the device |
| 64 | + :return: An integer, the number of PSUs available on the device |
| 65 | + """ |
| 66 | + DX010_MAX_PSUS = 2 |
| 67 | + return DX010_MAX_PSUS |
| 68 | + |
| 69 | + def get_psu_status(self, index): |
| 70 | + """ |
| 71 | + Retrieves the oprational status of power supply unit (PSU) defined |
| 72 | + by index <index> |
| 73 | + :param index: An integer, index of the PSU of which to query status |
| 74 | + :return: Boolean, True if PSU is operating properly, False if PSU is\ |
| 75 | + faulty |
| 76 | + """ |
| 77 | + status = 0 |
| 78 | + psu_status = self.read_psu_statuses(self.psu[index]['power']) |
| 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 |
| 84 | + |
| 85 | + return status |
| 86 | + |
| 87 | + def get_psu_presence(self, index): |
| 88 | + """ |
| 89 | + Retrieves the presence status of power supply unit (PSU) defined |
| 90 | + by index <index> |
| 91 | + :param index: An integer, index of the PSU of which to query status |
| 92 | + :return: Boolean, True if PSU is plugged, False if not |
| 93 | + """ |
| 94 | + status = 0 |
| 95 | + psu_absence = self.read_psu_statuses(self.psu[index]['abs']) |
| 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 |
| 101 | + |
| 102 | + return status |
0 commit comments