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