|
| 1 | +# |
| 2 | +# led_control.py |
| 3 | +# |
| 4 | +# Platform-specific LED control functionality for SONiC |
| 5 | +# |
| 6 | + |
| 7 | +try: |
| 8 | + from sonic_led.led_control_base import LedControlBase |
| 9 | + import os |
| 10 | + import time |
| 11 | + import syslog |
| 12 | + import sonic_platform.platform |
| 13 | + import sonic_platform.chassis |
| 14 | +except ImportError as e: |
| 15 | + raise ImportError(str(e) + " - required module not found") |
| 16 | + |
| 17 | +smbus_present = 1 |
| 18 | + |
| 19 | +try: |
| 20 | + import smbus |
| 21 | +except ImportError as e: |
| 22 | + smbus_present = 0 |
| 23 | + |
| 24 | + |
| 25 | +def DBG_PRINT(str): |
| 26 | + syslog.openlog("nokia-led") |
| 27 | + syslog.syslog(syslog.LOG_INFO, str) |
| 28 | + syslog.closelog() |
| 29 | + |
| 30 | + |
| 31 | +class LedControl(LedControlBase): |
| 32 | + """Platform specific LED control class""" |
| 33 | + |
| 34 | + # Constructor |
| 35 | + def __init__(self): |
| 36 | + self.chassis = sonic_platform.platform.Platform().get_chassis() |
| 37 | + self._initDefaultConfig() |
| 38 | + |
| 39 | + def _initDefaultConfig(self): |
| 40 | + # For the D1 box the port leds are controlled by Trident3 LED program |
| 41 | + # The fan tray leds will be managed with the new thermalctl daemon / chassis class based API |
| 42 | + # leaving only the system leds to be done old style |
| 43 | + DBG_PRINT("starting system leds") |
| 44 | + self._initSystemLed() |
| 45 | + DBG_PRINT(" led done") |
| 46 | + |
| 47 | + def _set_i2c_register(self, reg_file, value): |
| 48 | + # On successful write, the value read will be written on |
| 49 | + # reg_name and on failure returns 'ERR' |
| 50 | + rv = 'ERR' |
| 51 | + |
| 52 | + if (not os.path.isfile(reg_file)): |
| 53 | + return rv |
| 54 | + try: |
| 55 | + with open(reg_file, 'w') as fd: |
| 56 | + rv = fd.write(str(value)) |
| 57 | + except Exception as e: |
| 58 | + rv = 'ERR' |
| 59 | + |
| 60 | + return rv |
| 61 | + |
| 62 | + def _initSystemLed(self): |
| 63 | + # Front Panel System LEDs setting |
| 64 | + oldfan = 0xf |
| 65 | + oldpsu = 0xf |
| 66 | + |
| 67 | + # Write sys led |
| 68 | + if smbus_present == 0: |
| 69 | + DBG_PRINT(" PMON LED SET ERROR -> smbus present = 0 ") |
| 70 | + else: |
| 71 | + bus = smbus.SMBus(0) |
| 72 | + DEVICE_ADDRESS = 0x41 |
| 73 | + DEVICEREG = 0x7 |
| 74 | + bus.write_byte_data(DEVICE_ADDRESS, DEVICEREG, 0x02) |
| 75 | + DBG_PRINT(" System LED set O.K. ") |
| 76 | + |
| 77 | + while True: |
| 78 | + # Front Panel FAN Panel LED setting in register 0x08 |
| 79 | + if (self.chassis.get_fan(0).get_status() == self.chassis.get_fan(1).get_status() == True): |
| 80 | + if oldfan != 0x1: |
| 81 | + if (os.path.isfile("/sys/class/gpio/fanLedAmber/value")): |
| 82 | + self._set_i2c_register("/sys/class/gpio/fanLedAmber/value", 0) |
| 83 | + self._set_i2c_register("/sys/class/gpio/fanLedGreen/value", 1) |
| 84 | + oldfan = 0x1 |
| 85 | + else: |
| 86 | + if oldfan != 0x0: |
| 87 | + if (os.path.isfile("/sys/class/gpio/fanLedGreen/value")): |
| 88 | + self._set_i2c_register("/sys/class/gpio/fanLedGreen/value", 0) |
| 89 | + self._set_i2c_register("/sys/class/gpio/fanLedAmber/value", 1) |
| 90 | + oldfan = 0x0 |
| 91 | + |
| 92 | + # Front Panel PSU Panel LED setting in register 0x09 |
| 93 | + if (self.chassis.get_psu(0).get_status() == self.chassis.get_psu(1).get_status() == True): |
| 94 | + if oldpsu != 0x1: |
| 95 | + if (os.path.isfile("/sys/class/gpio/psuLedAmber/value")): |
| 96 | + self._set_i2c_register("/sys/class/gpio/psuLedAmber/value", 0) |
| 97 | + self._set_i2c_register("/sys/class/gpio/psuLedGreen/value", 1) |
| 98 | + oldpsu = 0x1 |
| 99 | + else: |
| 100 | + if oldpsu != 0x0: |
| 101 | + if (os.path.isfile("/sys/class/gpio/psuLedGreen/value")): |
| 102 | + self._set_i2c_register("/sys/class/gpio/psuLedGreen/value", 0) |
| 103 | + self._set_i2c_register("/sys/class/gpio/psuLedAmber/value", 1) |
| 104 | + oldpsu = 0x0 |
| 105 | + time.sleep(6) |
| 106 | + |
| 107 | + # Helper method to map SONiC port name to index |
| 108 | + def _port_name_to_index(self, port_name): |
| 109 | + # Strip "Ethernet" off port name |
| 110 | + if not port_name.startswith(self.SONIC_PORT_NAME_PREFIX): |
| 111 | + return -1 |
| 112 | + |
| 113 | + port_idx = int(port_name[len(self.SONIC_PORT_NAME_PREFIX):]) |
| 114 | + return port_idx |
| 115 | + |
| 116 | + def _port_state_to_mode(self, port_idx, state): |
| 117 | + DBG_PRINT("_port_state_to_mode") |
| 118 | + |
| 119 | + def _port_led_mode_update(self, port_idx, ledMode): |
| 120 | + DBG_PRINT("_port_led_mode_update") |
| 121 | + |
| 122 | + # called when port states change- implementation of port_link_state_change() method if needed |
| 123 | + def port_link_state_change(self, portname, state): |
| 124 | + # DBG_PRINT("port_link_state_change ") |
| 125 | + return |
0 commit comments