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