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 |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 11 | import PropertyCacher |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 12 | import Openbmc |
| 13 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 14 | if (len(sys.argv) < 2): |
| 15 | print "Usage: system_manager.py [system name]" |
| 16 | exit(1) |
| 17 | |
| 18 | System = __import__(sys.argv[1]) |
| 19 | import Openbmc |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 20 | |
| 21 | DBUS_NAME = 'org.openbmc.managers.System' |
| 22 | OBJ_NAME = '/org/openbmc/managers/System' |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 23 | HEARTBEAT_CHECK_INTERVAL = 20000 |
Norman James | 9a60a7b | 2015-10-06 16:53:23 -0500 | [diff] [blame] | 24 | STATE_START_TIMEOUT = 10 |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 25 | INTF_SENSOR = 'org.openbmc.SensorValue' |
| 26 | INTF_ITEM = 'org.openbmc.InventoryItem' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 27 | INTF_CONTROL = 'org.openbmc.Control' |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 28 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 29 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 30 | class SystemManager(dbus.service.Object): |
| 31 | def __init__(self,bus,name): |
| 32 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 33 | |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 34 | bus.add_signal_receiver(self.NewObjectHandler, |
| 35 | signal_name = "ObjectAdded", sender_keyword = 'bus_name') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 36 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 37 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 38 | self.current_state = "" |
| 39 | self.system_states = {} |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 40 | self.bus_name_lookup = {} |
Norman James | c36372d | 2015-10-08 07:03:07 -0500 | [diff] [blame] | 41 | self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0])) |
| 42 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 43 | for name in System.APPS.keys(): |
| 44 | sys_state = System.APPS[name]['system_state'] |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 45 | if (self.system_states.has_key(sys_state) == False): |
| 46 | self.system_states[sys_state] = [] |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 47 | self.system_states[sys_state].append(name) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 48 | |
| 49 | ## replace symbolic path in ID_LOOKUP |
| 50 | for category in System.ID_LOOKUP: |
| 51 | for key in System.ID_LOOKUP[category]: |
| 52 | val = System.ID_LOOKUP[category][key] |
| 53 | new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT) |
| 54 | System.ID_LOOKUP[category][key] = new_val |
| 55 | |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 56 | self.SystemStateHandler(System.SYSTEM_STATES[0]) |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 57 | |
| 58 | if not os.path.exists(PropertyCacher.CACHE_PATH): |
| 59 | print "Creating cache directory: "+PropertyCacher.CACHE_PATH |
| 60 | os.makedirs(PropertyCacher.CACHE_PATH) |
| 61 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 62 | print "SystemManager Init Done" |
| 63 | |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 64 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 65 | def SystemStateHandler(self,state_name): |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 66 | ## clearing object started flags |
| 67 | try: |
| 68 | for obj_path in System.EXIT_STATE_DEPEND[self.current_state]: |
| 69 | System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 0 |
| 70 | except: |
| 71 | pass |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 72 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 73 | print "Running System State: "+state_name |
| 74 | if (self.system_states.has_key(state_name)): |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 75 | for name in self.system_states[state_name]: |
| 76 | self.start_process(name) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 77 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 78 | if (state_name == "BMC_INIT"): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 79 | ## Add poll for heartbeat |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 80 | gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 81 | |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 82 | try: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 83 | cb = System.ENTER_STATE_CALLBACK[state_name] |
Norman James | e38859c | 2015-10-29 06:19:40 -0500 | [diff] [blame] | 84 | for methd in cb.keys(): |
| 85 | obj = bus.get_object(cb[methd]['bus_name'],cb[methd]['obj_name']) |
| 86 | method = obj.get_dbus_method(methd,cb[methd]['interface_name']) |
| 87 | method() |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 88 | except: |
| 89 | pass |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 90 | |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 91 | self.current_state = state_name |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 92 | |
| 93 | def gotoNextState(self): |
| 94 | s = 0 |
| 95 | for i in range(len(System.SYSTEM_STATES)): |
| 96 | if (System.SYSTEM_STATES[i] == self.current_state): |
| 97 | s = i+1 |
| 98 | |
| 99 | if (s == len(System.SYSTEM_STATES)): |
| 100 | print "ERROR SystemManager: No more system states" |
| 101 | else: |
| 102 | new_state_name = System.SYSTEM_STATES[s] |
| 103 | print "SystemManager Goto System State: "+new_state_name |
| 104 | self.SystemStateHandler(new_state_name) |
| 105 | |
| 106 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 107 | @dbus.service.method(DBUS_NAME, |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 108 | in_signature='', out_signature='s') |
| 109 | def getSystemState(self): |
| 110 | return self.current_state |
| 111 | |
| 112 | def doObjectLookup(self,category,key): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 113 | bus_name = "" |
| 114 | obj_path = "" |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 115 | intf_name = INTF_ITEM |
| 116 | try: |
| 117 | obj_path = System.ID_LOOKUP[category][key] |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 118 | bus_name = self.bus_name_lookup[obj_path] |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 119 | parts = obj_path.split('/') |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 120 | if (parts[3] == 'sensor'): |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 121 | intf_name = INTF_SENSOR |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 122 | except Exception as e: |
| 123 | print "ERROR SystemManager: "+str(e)+" not found in lookup" |
| 124 | |
Norman James | 66c0269 | 2015-10-15 10:23:12 -0500 | [diff] [blame] | 125 | return [bus_name,obj_path,intf_name] |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 126 | |
Norman James | 7aeefa7 | 2015-10-19 11:13:25 -0500 | [diff] [blame] | 127 | @dbus.service.method(DBUS_NAME, |
| 128 | in_signature='ss', out_signature='(sss)') |
| 129 | def getObjectFromId(self,category,key): |
| 130 | return self.doObjectLookup(category,key) |
| 131 | |
| 132 | @dbus.service.method(DBUS_NAME, |
| 133 | in_signature='sy', out_signature='(sss)') |
| 134 | def getObjectFromByteId(self,category,key): |
| 135 | byte = int(key) |
| 136 | return self.doObjectLookup(category,byte) |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 137 | |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 138 | def start_process(self,name): |
| 139 | if (System.APPS[name]['start_process'] == True): |
| 140 | app = System.APPS[name] |
| 141 | process_name = self.bin_path+"/"+app['process_name'] |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 142 | cmdline = [ ] |
| 143 | cmdline.append(process_name) |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 144 | if (app.has_key('args')): |
| 145 | for a in app['args']: |
| 146 | cmdline.append(a) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 147 | try: |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 148 | print "Starting process: "+" ".join(cmdline)+": "+name |
Norman James | bfa561e | 2015-11-01 07:38:14 -0600 | [diff] [blame] | 149 | if (app['monitor_process'] == True): |
| 150 | app['popen'] = subprocess.Popen(cmdline) |
| 151 | else: |
| 152 | subprocess.Popen(cmdline) |
| 153 | |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 154 | except Exception as e: |
| 155 | ## TODO: error |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 156 | print "ERROR: starting process: "+" ".join(cmdline) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 157 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 158 | def heartbeat_check(self): |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 159 | for name in System.APPS.keys(): |
| 160 | app = System.APPS[name] |
Norman James | bfa561e | 2015-11-01 07:38:14 -0600 | [diff] [blame] | 161 | if (app['start_process'] == True and app.has_key('popen')): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 162 | ## make sure process is still alive |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 163 | p = app['popen'] |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 164 | p.poll() |
| 165 | if (p.returncode != None): |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 166 | print "Process for "+name+" appears to be dead" |
| 167 | self.start_process(name) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 168 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 169 | return True |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 170 | |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 171 | def NewObjectHandler(self,obj_path, interface_name, bus_name = None): |
| 172 | if (self.bus_name_lookup.has_key(obj_path)): |
| 173 | if (self.bus_name_lookup[obj_path] == bus_name): |
| 174 | return |
| 175 | self.bus_name_lookup[obj_path] = bus_name |
| 176 | print "New object: "+obj_path+" ("+bus_name+")" |
| 177 | try: |
| 178 | if (System.EXIT_STATE_DEPEND[self.current_state].has_key(obj_path) == True): |
| 179 | System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 1 |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 180 | ## check if all required objects are started to move to next state |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 181 | state = 1 |
| 182 | for obj_path in System.EXIT_STATE_DEPEND[self.current_state]: |
| 183 | if (System.EXIT_STATE_DEPEND[self.current_state][obj_path] == 0): |
| 184 | state = 0 |
| 185 | ## all required objects have started so go to next state |
| 186 | if (state == 1): |
| 187 | print "All required objects started for "+self.current_state |
| 188 | self.gotoNextState() |
| 189 | except: |
| 190 | pass |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 191 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 192 | |
| 193 | @dbus.service.method(DBUS_NAME, |
| 194 | in_signature='s', out_signature='sis') |
| 195 | def gpioInit(self,name): |
| 196 | gpio_path = '' |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 197 | gpio_num = -1 |
| 198 | r = ['',gpio_num,''] |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 199 | if (System.GPIO_CONFIG.has_key(name) == False): |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 200 | # TODO: Error handling |
| 201 | print "ERROR: "+name+" not found in GPIO config table" |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 202 | else: |
Norman James | cf74f95 | 2015-10-28 12:45:18 -0500 | [diff] [blame] | 203 | |
| 204 | gpio_num = -1 |
| 205 | gpio = System.GPIO_CONFIG[name] |
| 206 | if (System.GPIO_CONFIG[name].has_key('gpio_num')): |
| 207 | gpio_num = gpio['gpio_num'] |
| 208 | else: |
| 209 | if (System.GPIO_CONFIG[name].has_key('gpio_pin')): |
| 210 | gpio_num = System.convertGpio(gpio['gpio_pin']) |
| 211 | else: |
| 212 | print "ERROR: SystemManager - GPIO lookup failed for "+name |
| 213 | |
| 214 | if (gpio_num != -1): |
| 215 | r = [Openbmc.GPIO_DEV, gpio_num, gpio['direction']] |
| 216 | return r |
Norman James | 5236a8f | 2015-11-05 20:39:31 -0600 | [diff] [blame] | 217 | |
| 218 | @dbus.service.method(DBUS_NAME, |
| 219 | in_signature='s', out_signature='si') |
| 220 | def hwmonInit(self,name): |
| 221 | dbus_suffix = "" |
| 222 | poll_interval = 0 |
| 223 | r = ['',0] |
| 224 | if (System.HWMON_CONFIG.has_key(name) == False): |
| 225 | # TODO: Error handling |
| 226 | print "ERROR: "+name+" not found in HWMON config table" |
| 227 | else: |
| 228 | |
| 229 | hwmon = System.HWMON_CONFIG[name] |
| 230 | print hwmon |
| 231 | r = [hwmon['dbus_suffix'], hwmon['poll_interval']] |
| 232 | return r |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 233 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 234 | |
| 235 | if __name__ == '__main__': |
| 236 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 237 | bus = Openbmc.getDBus() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 238 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 239 | obj = SystemManager(bus,OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 240 | mainloop = gobject.MainLoop() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 241 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 242 | print "Running SystemManager" |
| 243 | mainloop.run() |
| 244 | |