Norman James | 42c1be8 | 2015-10-22 14:34:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 2 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 3 | import sys |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 4 | import subprocess |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 5 | import gobject |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 9 | import os |
Brad Bishop | ee1b154 | 2016-05-12 16:55:00 -0400 | [diff] [blame] | 10 | import obmc.dbuslib.propertycacher as PropertyCacher |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 11 | from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus |
| 12 | import obmc.enums |
Brad Bishop | 0b380f7 | 2016-06-10 00:29:50 -0400 | [diff] [blame] | 13 | import obmc_system_config as System |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 14 | import obmc.dbuslib.introspection |
| 15 | import obmc.utils.misc |
Brad Bishop | 6173a89 | 2016-07-24 10:04:09 -0400 | [diff] [blame] | 16 | import obmc.mapper.utils |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 17 | |
| 18 | DBUS_NAME = 'org.openbmc.managers.System' |
| 19 | OBJ_NAME = '/org/openbmc/managers/System' |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 20 | INTF_SENSOR = 'org.openbmc.SensorValue' |
| 21 | INTF_ITEM = 'org.openbmc.InventoryItem' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 22 | INTF_CONTROL = 'org.openbmc.Control' |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 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): |
| 27 | DbusProperties.__init__(self) |
| 28 | DbusObjectManager.__init__(self) |
| 29 | dbus.service.Object.__init__(self, bus, obj_name) |
| 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 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 38 | self.Set(DBUS_NAME, "current_state", "") |
| 39 | self.system_states = {} |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 40 | self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
Norman James | c36372d | 2015-10-08 07:03:07 -0500 | [diff] [blame] | 41 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 42 | for name in System.APPS.keys(): |
| 43 | sys_state = System.APPS[name]['system_state'] |
| 44 | if sys_state not in self.system_states: |
| 45 | self.system_states[sys_state] = [] |
| 46 | self.system_states[sys_state].append(name) |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 47 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 48 | ## replace symbolic path in ID_LOOKUP |
| 49 | for category in System.ID_LOOKUP: |
| 50 | for key in System.ID_LOOKUP[category]: |
| 51 | val = System.ID_LOOKUP[category][key] |
| 52 | new_val = val.replace( |
| 53 | "<inventory_root>", System.INVENTORY_ROOT) |
| 54 | System.ID_LOOKUP[category][key] = new_val |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 55 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 56 | self.SystemStateHandler(System.SYSTEM_STATES[0]) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 57 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 58 | print "SystemManager Init Done" |
Brad Bishop | 4de4264 | 2016-06-29 21:55:47 -0400 | [diff] [blame] | 59 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 60 | def SystemStateHandler(self, state_name): |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 61 | print "Running System State: "+state_name |
| 62 | if state_name in self.system_states: |
| 63 | for name in self.system_states[state_name]: |
| 64 | self.start_process(name) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 65 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 66 | try: |
| 67 | cb = System.ENTER_STATE_CALLBACK[state_name] |
| 68 | for methd in cb.keys(): |
| 69 | obj = bus.get_object( |
| 70 | cb[methd]['bus_name'], |
| 71 | cb[methd]['obj_name'], |
| 72 | introspect=False) |
| 73 | method = obj.get_dbus_method( |
| 74 | methd, cb[methd]['interface_name']) |
| 75 | method() |
| 76 | except: |
| 77 | pass |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 78 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 79 | self.Set(DBUS_NAME, "current_state", state_name) |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 80 | |
Brad Bishop | 6173a89 | 2016-07-24 10:04:09 -0400 | [diff] [blame] | 81 | waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys() |
| 82 | if waitlist: |
| 83 | self.waiter = obmc.mapper.utils.Wait( |
| 84 | self.bus, waitlist, |
| 85 | callback=self.gotoNextState) |
| 86 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 87 | def gotoNextState(self): |
| 88 | s = 0 |
| 89 | current_state = self.Get(DBUS_NAME, "current_state") |
| 90 | for i in range(len(System.SYSTEM_STATES)): |
| 91 | if (System.SYSTEM_STATES[i] == current_state): |
| 92 | s = i+1 |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 93 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 94 | if (s == len(System.SYSTEM_STATES)): |
| 95 | print "ERROR SystemManager: No more system states" |
| 96 | else: |
| 97 | new_state_name = System.SYSTEM_STATES[s] |
| 98 | print "SystemManager Goto System State: "+new_state_name |
| 99 | self.SystemStateHandler(new_state_name) |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 100 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 101 | @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s') |
| 102 | def getSystemState(self): |
| 103 | return self.Get(DBUS_NAME, "current_state") |
Yi Li | 5cf1e11 | 2016-04-15 15:30:30 +0800 | [diff] [blame] | 104 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 105 | def doObjectLookup(self, category, key): |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 106 | obj_path = "" |
| 107 | intf_name = INTF_ITEM |
| 108 | try: |
| 109 | obj_path = System.ID_LOOKUP[category][key] |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 110 | parts = obj_path.split('/') |
| 111 | if (parts[3] == 'sensors'): |
| 112 | intf_name = INTF_SENSOR |
| 113 | except Exception as e: |
| 114 | print "ERROR SystemManager: "+str(e)+" not found in lookup" |
Yi Li | 5cf1e11 | 2016-04-15 15:30:30 +0800 | [diff] [blame] | 115 | |
Brad Bishop | fc38a57 | 2016-07-22 08:56:09 -0400 | [diff] [blame] | 116 | return [obj_path, intf_name] |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 117 | |
Brad Bishop | fc38a57 | 2016-07-22 08:56:09 -0400 | [diff] [blame] | 118 | @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)') |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 119 | def getObjectFromId(self, category, key): |
| 120 | return self.doObjectLookup(category, key) |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 121 | |
Brad Bishop | fc38a57 | 2016-07-22 08:56:09 -0400 | [diff] [blame] | 122 | @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)') |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 123 | def getObjectFromByteId(self, category, key): |
| 124 | byte = int(key) |
| 125 | return self.doObjectLookup(category, byte) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 126 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 127 | # Get the FRU area names defined in ID_LOOKUP table given a fru_id. |
| 128 | # If serval areas are defined for a fru_id, the areas are returned |
| 129 | # together as a string with each area name seperated with ','. |
| 130 | # If no fru area defined in ID_LOOKUP, an empty string will be returned. |
| 131 | @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s') |
| 132 | def getFRUArea(self, fru_id): |
| 133 | ret_str = '' |
| 134 | fru_id = '_' + str(fru_id) |
| 135 | area_list = [ |
| 136 | area for area in System.ID_LOOKUP['FRU_STR'].keys() |
| 137 | if area.endswith(fru_id)] |
| 138 | for area in area_list: |
| 139 | ret_str = area + ',' + ret_str |
| 140 | # remove the last ',' |
| 141 | return ret_str[:-1] |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 142 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 143 | def start_process(self, name): |
| 144 | if System.APPS[name]['start_process']: |
| 145 | app = System.APPS[name] |
| 146 | process_name = self.bin_path+"/"+app['process_name'] |
| 147 | cmdline = [] |
| 148 | cmdline.append(process_name) |
| 149 | if 'args' in app: |
| 150 | for a in app['args']: |
| 151 | cmdline.append(a) |
| 152 | try: |
| 153 | print "Starting process: "+" ".join(cmdline)+": "+name |
| 154 | if app['monitor_process']: |
| 155 | app['popen'] = subprocess.Popen(cmdline) |
| 156 | else: |
| 157 | subprocess.Popen(cmdline) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 158 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 159 | except Exception as e: |
| 160 | ## TODO: error |
| 161 | print "ERROR: starting process: "+" ".join(cmdline) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 162 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 163 | def NewObjectHandler(self, obj_path, iprops, bus_name=None): |
| 164 | current_state = self.Get(DBUS_NAME, "current_state") |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 165 | if current_state not in System.EXIT_STATE_DEPEND: |
| 166 | return |
| 167 | |
| 168 | if obj_path in System.EXIT_STATE_DEPEND[current_state]: |
| 169 | print "New object: "+obj_path+" ("+bus_name+")" |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 170 | |
| 171 | @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis') |
| 172 | def gpioInit(self, name): |
| 173 | gpio_path = '' |
| 174 | gpio_num = -1 |
| 175 | r = ['', gpio_num, ''] |
| 176 | if name not in System.GPIO_CONFIG: |
| 177 | # TODO: Error handling |
| 178 | print "ERROR: "+name+" not found in GPIO config table" |
| 179 | else: |
| 180 | |
| 181 | gpio_num = -1 |
| 182 | gpio = System.GPIO_CONFIG[name] |
| 183 | if 'gpio_num' in System.GPIO_CONFIG[name]: |
| 184 | gpio_num = gpio['gpio_num'] |
| 185 | else: |
| 186 | if 'gpio_pin' in System.GPIO_CONFIG[name]: |
| 187 | gpio_num = System.convertGpio(gpio['gpio_pin']) |
| 188 | else: |
| 189 | print "ERROR: SystemManager - GPIO lookup failed for "+name |
| 190 | |
| 191 | if (gpio_num != -1): |
| 192 | r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']] |
| 193 | return r |
| 194 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 195 | |
| 196 | if __name__ == '__main__': |
| 197 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 198 | bus = get_dbus() |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 199 | obj = SystemManager(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 200 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 201 | obj.unmask_signals() |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 202 | name = dbus.service.BusName(DBUS_NAME, bus) |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 203 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 204 | print "Running SystemManager" |
| 205 | mainloop.run() |