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