Brad Bishop | de6ed12 | 2016-08-30 20:14:57 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 2 | |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 3 | import gobject |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 4 | import dbus |
| 5 | import dbus.service |
| 6 | import dbus.mainloop.glib |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 7 | import os |
Brad Bishop | ee1b154 | 2016-05-12 16:55:00 -0400 | [diff] [blame] | 8 | import obmc.dbuslib.propertycacher as PropertyCacher |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 9 | from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus |
| 10 | import obmc.enums |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 11 | import obmc_system_config as System |
Brad Bishop | 6173a89 | 2016-07-24 10:04:09 -0400 | [diff] [blame] | 12 | import obmc.mapper.utils |
Brad Bishop | 7e5ec46 | 2016-09-21 09:09:48 -0400 | [diff] [blame] | 13 | import obmc.inventory |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 14 | import obmc.system |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 15 | |
| 16 | DBUS_NAME = 'org.openbmc.managers.System' |
| 17 | OBJ_NAME = '/org/openbmc/managers/System' |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 18 | INTF_SENSOR = 'org.openbmc.SensorValue' |
| 19 | INTF_ITEM = 'org.openbmc.InventoryItem' |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 20 | |
Ratan Gupta | 520f8b0 | 2016-09-06 21:47:05 +0530 | [diff] [blame] | 21 | SYS_STATE_FILE = '/var/lib/obmc/last-system-state' |
| 22 | POWER_OFF = "0" |
| 23 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 24 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 25 | class SystemManager(DbusProperties, DbusObjectManager): |
| 26 | def __init__(self, bus, obj_name): |
Brad Bishop | f47f5fa | 2016-09-08 22:29:01 -0400 | [diff] [blame] | 27 | super(SystemManager, self).__init__( |
| 28 | conn=bus, |
| 29 | object_path=obj_name) |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 30 | self.bus = bus |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 31 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 32 | bus.add_signal_receiver( |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 33 | self.NewObjectHandler, |
| 34 | signal_name="InterfacesAdded", sender_keyword='bus_name') |
| 35 | bus.add_signal_receiver( |
| 36 | self.SystemStateHandler, signal_name="GotoSystemState") |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 37 | |
Ratan Gupta | 520f8b0 | 2016-09-06 21:47:05 +0530 | [diff] [blame] | 38 | bus.add_signal_receiver( |
| 39 | self.chassisPowerStateHandler, |
| 40 | dbus_interface="org.freedesktop.DBus.Properties", |
| 41 | signal_name="PropertiesChanged", |
| 42 | path="/org/openbmc/control/power0") |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 43 | |
Ratan Gupta | 520f8b0 | 2016-09-06 21:47:05 +0530 | [diff] [blame] | 44 | self.Set(DBUS_NAME, "current_state", "") |
| 45 | self.Set(DBUS_NAME, "system_last_state", POWER_OFF) |
| 46 | self.import_system_state_from_disk() |
| 47 | # replace symbolic path in ID_LOOKUP |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 48 | for category in System.ID_LOOKUP: |
| 49 | for key in System.ID_LOOKUP[category]: |
| 50 | val = System.ID_LOOKUP[category][key] |
| 51 | new_val = val.replace( |
Brad Bishop | 7e5ec46 | 2016-09-21 09:09:48 -0400 | [diff] [blame] | 52 | "<inventory_root>", obmc.inventory.INVENTORY_ROOT) |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 53 | System.ID_LOOKUP[category][key] = new_val |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 54 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 55 | self.SystemStateHandler(System.SYSTEM_STATES[0]) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 56 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 57 | print "SystemManager Init Done" |
Brad Bishop | 4de4264 | 2016-06-29 21:55:47 -0400 | [diff] [blame] | 58 | |
Ratan Gupta | 520f8b0 | 2016-09-06 21:47:05 +0530 | [diff] [blame] | 59 | def chassisPowerStateHandler(self, interface_name, changed_properties, |
| 60 | invalidated_properties): |
| 61 | value = changed_properties.get('state') |
| 62 | if value is not None: |
| 63 | self.write_to_disk_and_update(str(value)) |
| 64 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 65 | def SystemStateHandler(self, state_name): |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 66 | print "Running System State: "+state_name |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 67 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 68 | self.Set(DBUS_NAME, "current_state", state_name) |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 69 | |
Brad Bishop | 6173a89 | 2016-07-24 10:04:09 -0400 | [diff] [blame] | 70 | waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys() |
| 71 | if waitlist: |
| 72 | self.waiter = obmc.mapper.utils.Wait( |
| 73 | self.bus, waitlist, |
| 74 | callback=self.gotoNextState) |
| 75 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 76 | def gotoNextState(self): |
| 77 | s = 0 |
| 78 | current_state = self.Get(DBUS_NAME, "current_state") |
| 79 | for i in range(len(System.SYSTEM_STATES)): |
| 80 | if (System.SYSTEM_STATES[i] == current_state): |
| 81 | s = i+1 |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 82 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 83 | if (s == len(System.SYSTEM_STATES)): |
| 84 | print "ERROR SystemManager: No more system states" |
| 85 | else: |
| 86 | new_state_name = System.SYSTEM_STATES[s] |
| 87 | print "SystemManager Goto System State: "+new_state_name |
| 88 | self.SystemStateHandler(new_state_name) |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 89 | |
Ratan Gupta | 520f8b0 | 2016-09-06 21:47:05 +0530 | [diff] [blame] | 90 | def import_system_state_from_disk(self): |
| 91 | state = str(POWER_OFF) |
| 92 | try: |
| 93 | with open(SYS_STATE_FILE, 'r+') as f: |
| 94 | state = f.readline().rstrip('\n') |
| 95 | except IOError: |
| 96 | pass |
| 97 | self.Set(DBUS_NAME, "system_last_state", state) |
| 98 | return state |
| 99 | |
| 100 | def write_to_disk_and_update(self, state): |
| 101 | try: |
| 102 | with open(SYS_STATE_FILE, 'w+') as f: |
| 103 | f.write(str(state)) |
| 104 | self.Set(DBUS_NAME, "system_last_state", state) |
| 105 | except IOError: |
| 106 | pass |
| 107 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 108 | @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s') |
| 109 | def getSystemState(self): |
| 110 | return self.Get(DBUS_NAME, "current_state") |
Yi Li | 5cf1e11 | 2016-04-15 15:30:30 +0800 | [diff] [blame] | 111 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 112 | def doObjectLookup(self, category, key): |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 113 | obj_path = "" |
| 114 | intf_name = INTF_ITEM |
| 115 | try: |
| 116 | obj_path = System.ID_LOOKUP[category][key] |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 117 | parts = obj_path.split('/') |
| 118 | if (parts[3] == 'sensors'): |
| 119 | intf_name = INTF_SENSOR |
| 120 | except Exception as e: |
| 121 | print "ERROR SystemManager: "+str(e)+" not found in lookup" |
Yi Li | 5cf1e11 | 2016-04-15 15:30:30 +0800 | [diff] [blame] | 122 | |
Brad Bishop | fc38a57 | 2016-07-22 08:56:09 -0400 | [diff] [blame] | 123 | return [obj_path, intf_name] |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 124 | |
Brad Bishop | fc38a57 | 2016-07-22 08:56:09 -0400 | [diff] [blame] | 125 | @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)') |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 126 | def getObjectFromId(self, category, key): |
| 127 | return self.doObjectLookup(category, key) |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 128 | |
Brad Bishop | fc38a57 | 2016-07-22 08:56:09 -0400 | [diff] [blame] | 129 | @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)') |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 130 | def getObjectFromByteId(self, category, key): |
| 131 | byte = int(key) |
| 132 | return self.doObjectLookup(category, byte) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 133 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 134 | # Get the FRU area names defined in ID_LOOKUP table given a fru_id. |
| 135 | # If serval areas are defined for a fru_id, the areas are returned |
Gunnar Mills | aa4c310 | 2017-10-25 20:50:47 -0500 | [diff] [blame] | 136 | # together as a string with each area name separated with ','. |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 137 | # If no fru area defined in ID_LOOKUP, an empty string will be returned. |
| 138 | @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s') |
| 139 | def getFRUArea(self, fru_id): |
| 140 | ret_str = '' |
| 141 | fru_id = '_' + str(fru_id) |
| 142 | area_list = [ |
| 143 | area for area in System.ID_LOOKUP['FRU_STR'].keys() |
| 144 | if area.endswith(fru_id)] |
| 145 | for area in area_list: |
| 146 | ret_str = area + ',' + ret_str |
| 147 | # remove the last ',' |
| 148 | return ret_str[:-1] |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 149 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 150 | def NewObjectHandler(self, obj_path, iprops, bus_name=None): |
| 151 | current_state = self.Get(DBUS_NAME, "current_state") |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 152 | if current_state not in System.EXIT_STATE_DEPEND: |
| 153 | return |
| 154 | |
| 155 | if obj_path in System.EXIT_STATE_DEPEND[current_state]: |
| 156 | print "New object: "+obj_path+" ("+bus_name+")" |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 157 | |
| 158 | @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis') |
| 159 | def gpioInit(self, name): |
| 160 | gpio_path = '' |
| 161 | gpio_num = -1 |
| 162 | r = ['', gpio_num, ''] |
| 163 | if name not in System.GPIO_CONFIG: |
Xo Wang | 605620d | 2016-09-21 12:46:29 -0700 | [diff] [blame] | 164 | # TODO: Better error handling |
| 165 | msg = "ERROR: "+name+" not found in GPIO config table" |
| 166 | print msg |
| 167 | raise Exception(msg) |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 168 | else: |
| 169 | |
| 170 | gpio_num = -1 |
| 171 | gpio = System.GPIO_CONFIG[name] |
| 172 | if 'gpio_num' in System.GPIO_CONFIG[name]: |
| 173 | gpio_num = gpio['gpio_num'] |
| 174 | else: |
| 175 | if 'gpio_pin' in System.GPIO_CONFIG[name]: |
Brad Bishop | a7ac805 | 2016-09-21 09:17:05 -0400 | [diff] [blame] | 176 | gpio_num = obmc.system.convertGpio(gpio['gpio_pin']) |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 177 | else: |
Xo Wang | 605620d | 2016-09-21 12:46:29 -0700 | [diff] [blame] | 178 | msg = "ERROR: SystemManager - GPIO lookup failed for "+name |
| 179 | print msg |
| 180 | raise Exception(msg) |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 181 | |
| 182 | if (gpio_num != -1): |
| 183 | r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']] |
| 184 | return r |
| 185 | |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 186 | @dbus.service.method(DBUS_NAME, in_signature='', |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 187 | out_signature='ssa(sb)a(sb)a(sbb)ssssa(sb)') |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 188 | def getGpioConfiguration(self): |
| 189 | power_config = System.GPIO_CONFIGS.get('power_config', {}) |
| 190 | power_good_in = power_config.get('power_good_in', '') |
| 191 | latch_out = power_config.get('latch_out', '') |
| 192 | power_up_outs = power_config.get('power_up_outs', []) |
| 193 | reset_outs = power_config.get('reset_outs', []) |
Lei YU | a42400f | 2017-08-22 17:04:43 +0800 | [diff] [blame] | 194 | pci_reset_outs = power_config.get('pci_reset_outs', []) |
Lei YU | 75a18a2 | 2016-11-22 01:47:47 +0800 | [diff] [blame] | 195 | hostctl_config = System.GPIO_CONFIGS.get('hostctl_config', {}) |
| 196 | fsi_data = hostctl_config.get('fsi_data', '') |
| 197 | fsi_clk = hostctl_config.get('fsi_clk', '') |
| 198 | fsi_enable = hostctl_config.get('fsi_enable', '') |
| 199 | cronus_sel = hostctl_config.get('cronus_sel', '') |
| 200 | optionals = hostctl_config.get('optionals', []) |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 201 | r = [power_good_in, latch_out, power_up_outs, reset_outs, |
| 202 | pci_reset_outs, fsi_data, fsi_clk, fsi_enable, cronus_sel, |
| 203 | optionals] |
Xo Wang | 3f87de8 | 2016-09-22 11:17:01 -0700 | [diff] [blame] | 204 | print "Power GPIO config: " + str(r) |
| 205 | return r |
| 206 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 207 | |
| 208 | if __name__ == '__main__': |
| 209 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 210 | bus = get_dbus() |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 211 | obj = SystemManager(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 212 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 213 | obj.unmask_signals() |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 214 | name = dbus.service.BusName(DBUS_NAME, bus) |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 215 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 216 | print "Running SystemManager" |
| 217 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 218 | |
| 219 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |