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 |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 16 | |
| 17 | DBUS_NAME = 'org.openbmc.managers.System' |
| 18 | OBJ_NAME = '/org/openbmc/managers/System' |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 19 | INTF_SENSOR = 'org.openbmc.SensorValue' |
| 20 | INTF_ITEM = 'org.openbmc.InventoryItem' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 21 | INTF_CONTROL = 'org.openbmc.Control' |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 22 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 23 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 24 | class SystemManager(DbusProperties, DbusObjectManager): |
| 25 | def __init__(self, bus, obj_name): |
| 26 | DbusProperties.__init__(self) |
| 27 | DbusObjectManager.__init__(self) |
| 28 | dbus.service.Object.__init__(self, bus, obj_name) |
| 29 | self.bus = bus |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 30 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 31 | bus.add_signal_receiver( |
| 32 | self.bus_handler, |
| 33 | dbus_interface=dbus.BUS_DAEMON_IFACE, |
| 34 | signal_name='NameOwnerChanged') |
| 35 | bus.add_signal_receiver( |
| 36 | self.NewObjectHandler, |
| 37 | signal_name="InterfacesAdded", sender_keyword='bus_name') |
| 38 | bus.add_signal_receiver( |
| 39 | self.SystemStateHandler, signal_name="GotoSystemState") |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 40 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 41 | self.Set(DBUS_NAME, "current_state", "") |
| 42 | self.system_states = {} |
| 43 | self.bus_name_lookup = {} |
| 44 | self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
Norman James | c36372d | 2015-10-08 07:03:07 -0500 | [diff] [blame] | 45 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 46 | for name in System.APPS.keys(): |
| 47 | sys_state = System.APPS[name]['system_state'] |
| 48 | if sys_state not in self.system_states: |
| 49 | self.system_states[sys_state] = [] |
| 50 | self.system_states[sys_state].append(name) |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 51 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 52 | ## replace symbolic path in ID_LOOKUP |
| 53 | for category in System.ID_LOOKUP: |
| 54 | for key in System.ID_LOOKUP[category]: |
| 55 | val = System.ID_LOOKUP[category][key] |
| 56 | new_val = val.replace( |
| 57 | "<inventory_root>", System.INVENTORY_ROOT) |
| 58 | System.ID_LOOKUP[category][key] = new_val |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 59 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 60 | self.SystemStateHandler(System.SYSTEM_STATES[0]) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 61 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 62 | if not os.path.exists(PropertyCacher.CACHE_PATH): |
| 63 | print "Creating cache directory: "+PropertyCacher.CACHE_PATH |
| 64 | os.makedirs(PropertyCacher.CACHE_PATH) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 65 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 66 | for s in self.bus.list_names(): |
| 67 | if obmc.utils.misc.org_dot_openbmc_match(s): |
| 68 | self.bus_handler(s, '', s) |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 69 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 70 | print "SystemManager Init Done" |
Brad Bishop | 4de4264 | 2016-06-29 21:55:47 -0400 | [diff] [blame] | 71 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 72 | def try_next_state(self): |
| 73 | current_state = self.Get(DBUS_NAME, "current_state") |
| 74 | if current_state not in System.EXIT_STATE_DEPEND: |
| 75 | return |
Brad Bishop | 4de4264 | 2016-06-29 21:55:47 -0400 | [diff] [blame] | 76 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 77 | if all(System.EXIT_STATE_DEPEND[current_state].values()): |
| 78 | print "All required objects started for "+current_state |
| 79 | self.gotoNextState() |
Brad Bishop | 4de4264 | 2016-06-29 21:55:47 -0400 | [diff] [blame] | 80 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 81 | def SystemStateHandler(self, state_name): |
| 82 | ## clearing object started flags |
| 83 | current_state = self.Get(DBUS_NAME, "current_state") |
| 84 | try: |
| 85 | for obj_path in System.EXIT_STATE_DEPEND[current_state]: |
| 86 | System.EXIT_STATE_DEPEND[current_state][obj_path] = 0 |
| 87 | except: |
| 88 | pass |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 89 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 90 | print "Running System State: "+state_name |
| 91 | if state_name in self.system_states: |
| 92 | for name in self.system_states[state_name]: |
| 93 | self.start_process(name) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 94 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 95 | try: |
| 96 | cb = System.ENTER_STATE_CALLBACK[state_name] |
| 97 | for methd in cb.keys(): |
| 98 | obj = bus.get_object( |
| 99 | cb[methd]['bus_name'], |
| 100 | cb[methd]['obj_name'], |
| 101 | introspect=False) |
| 102 | method = obj.get_dbus_method( |
| 103 | methd, cb[methd]['interface_name']) |
| 104 | method() |
| 105 | except: |
| 106 | pass |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 107 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 108 | self.Set(DBUS_NAME, "current_state", state_name) |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 109 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 110 | def gotoNextState(self): |
| 111 | s = 0 |
| 112 | current_state = self.Get(DBUS_NAME, "current_state") |
| 113 | for i in range(len(System.SYSTEM_STATES)): |
| 114 | if (System.SYSTEM_STATES[i] == current_state): |
| 115 | s = i+1 |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 116 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 117 | if (s == len(System.SYSTEM_STATES)): |
| 118 | print "ERROR SystemManager: No more system states" |
| 119 | else: |
| 120 | new_state_name = System.SYSTEM_STATES[s] |
| 121 | print "SystemManager Goto System State: "+new_state_name |
| 122 | self.SystemStateHandler(new_state_name) |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 123 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 124 | @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s') |
| 125 | def getSystemState(self): |
| 126 | return self.Get(DBUS_NAME, "current_state") |
Yi Li | 5cf1e11 | 2016-04-15 15:30:30 +0800 | [diff] [blame] | 127 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 128 | def doObjectLookup(self, category, key): |
| 129 | bus_name = "" |
| 130 | obj_path = "" |
| 131 | intf_name = INTF_ITEM |
| 132 | try: |
| 133 | obj_path = System.ID_LOOKUP[category][key] |
| 134 | bus_name = self.bus_name_lookup[obj_path] |
| 135 | parts = obj_path.split('/') |
| 136 | if (parts[3] == 'sensors'): |
| 137 | intf_name = INTF_SENSOR |
| 138 | except Exception as e: |
| 139 | print "ERROR SystemManager: "+str(e)+" not found in lookup" |
Yi Li | 5cf1e11 | 2016-04-15 15:30:30 +0800 | [diff] [blame] | 140 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 141 | return [bus_name, obj_path, intf_name] |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 142 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 143 | @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(sss)') |
| 144 | def getObjectFromId(self, category, key): |
| 145 | return self.doObjectLookup(category, key) |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 146 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 147 | @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(sss)') |
| 148 | def getObjectFromByteId(self, category, key): |
| 149 | byte = int(key) |
| 150 | return self.doObjectLookup(category, byte) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 151 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 152 | # Get the FRU area names defined in ID_LOOKUP table given a fru_id. |
| 153 | # If serval areas are defined for a fru_id, the areas are returned |
| 154 | # together as a string with each area name seperated with ','. |
| 155 | # If no fru area defined in ID_LOOKUP, an empty string will be returned. |
| 156 | @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s') |
| 157 | def getFRUArea(self, fru_id): |
| 158 | ret_str = '' |
| 159 | fru_id = '_' + str(fru_id) |
| 160 | area_list = [ |
| 161 | area for area in System.ID_LOOKUP['FRU_STR'].keys() |
| 162 | if area.endswith(fru_id)] |
| 163 | for area in area_list: |
| 164 | ret_str = area + ',' + ret_str |
| 165 | # remove the last ',' |
| 166 | return ret_str[:-1] |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 167 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 168 | def start_process(self, name): |
| 169 | if System.APPS[name]['start_process']: |
| 170 | app = System.APPS[name] |
| 171 | process_name = self.bin_path+"/"+app['process_name'] |
| 172 | cmdline = [] |
| 173 | cmdline.append(process_name) |
| 174 | if 'args' in app: |
| 175 | for a in app['args']: |
| 176 | cmdline.append(a) |
| 177 | try: |
| 178 | print "Starting process: "+" ".join(cmdline)+": "+name |
| 179 | if app['monitor_process']: |
| 180 | app['popen'] = subprocess.Popen(cmdline) |
| 181 | else: |
| 182 | subprocess.Popen(cmdline) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 183 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 184 | except Exception as e: |
| 185 | ## TODO: error |
| 186 | print "ERROR: starting process: "+" ".join(cmdline) |
Brad Bishop | fa73649 | 2016-06-29 22:21:49 -0400 | [diff] [blame] | 187 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 188 | def bus_handler(self, owned_name, old, new): |
| 189 | if obmc.dbuslib.bindings.is_unique(owned_name) or not new: |
| 190 | return |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 191 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 192 | if owned_name == DBUS_NAME: |
| 193 | return |
Norman James | 5236a8f | 2015-11-05 20:39:31 -0600 | [diff] [blame] | 194 | |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 195 | objs = obmc.dbuslib.introspection.find_dbus_interfaces( |
| 196 | self.bus, owned_name, '/', bool) |
| 197 | current_state = self.Get(DBUS_NAME, "current_state") |
| 198 | for o in objs.keys(): |
| 199 | if o in self.bus_name_lookup: |
| 200 | continue |
| 201 | self.bus_name_lookup[o] = owned_name |
| 202 | |
| 203 | if current_state not in System.EXIT_STATE_DEPEND: |
| 204 | continue |
| 205 | if o in System.EXIT_STATE_DEPEND[current_state]: |
| 206 | print "New object: "+o+" ("+owned_name+")" |
| 207 | System.EXIT_STATE_DEPEND[current_state][o] = 1 |
| 208 | |
| 209 | self.try_next_state() |
| 210 | |
| 211 | def NewObjectHandler(self, obj_path, iprops, bus_name=None): |
| 212 | current_state = self.Get(DBUS_NAME, "current_state") |
| 213 | if obj_path in self.bus_name_lookup: |
| 214 | if (self.bus_name_lookup[obj_path] == bus_name): |
| 215 | return |
| 216 | self.bus_name_lookup[obj_path] = bus_name |
| 217 | if current_state not in System.EXIT_STATE_DEPEND: |
| 218 | return |
| 219 | |
| 220 | if obj_path in System.EXIT_STATE_DEPEND[current_state]: |
| 221 | print "New object: "+obj_path+" ("+bus_name+")" |
| 222 | System.EXIT_STATE_DEPEND[current_state][obj_path] = 1 |
| 223 | ## check if all required objects are |
| 224 | # started to move to next state |
| 225 | self.try_next_state() |
| 226 | |
| 227 | @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis') |
| 228 | def gpioInit(self, name): |
| 229 | gpio_path = '' |
| 230 | gpio_num = -1 |
| 231 | r = ['', gpio_num, ''] |
| 232 | if name not in System.GPIO_CONFIG: |
| 233 | # TODO: Error handling |
| 234 | print "ERROR: "+name+" not found in GPIO config table" |
| 235 | else: |
| 236 | |
| 237 | gpio_num = -1 |
| 238 | gpio = System.GPIO_CONFIG[name] |
| 239 | if 'gpio_num' in System.GPIO_CONFIG[name]: |
| 240 | gpio_num = gpio['gpio_num'] |
| 241 | else: |
| 242 | if 'gpio_pin' in System.GPIO_CONFIG[name]: |
| 243 | gpio_num = System.convertGpio(gpio['gpio_pin']) |
| 244 | else: |
| 245 | print "ERROR: SystemManager - GPIO lookup failed for "+name |
| 246 | |
| 247 | if (gpio_num != -1): |
| 248 | r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']] |
| 249 | return r |
| 250 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 251 | |
| 252 | if __name__ == '__main__': |
| 253 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 254 | bus = get_dbus() |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 255 | obj = SystemManager(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 256 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 257 | obj.unmask_signals() |
Brad Bishop | 416539d | 2016-07-22 07:22:42 -0400 | [diff] [blame] | 258 | name = dbus.service.BusName(DBUS_NAME, bus) |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 259 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 260 | print "Running SystemManager" |
| 261 | mainloop.run() |