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 | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 26 | |
| 27 | class SystemManager(dbus.service.Object): |
| 28 | def __init__(self,bus,name): |
| 29 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 30 | self.property_manager = PropertyManager.PropertyManager(bus,System.CACHE_PATH) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 31 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 32 | ## Signal handlers |
| 33 | bus.add_signal_receiver(self.NewBusHandler, |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 34 | dbus_interface = 'org.freedesktop.DBus', |
| 35 | signal_name = "NameOwnerChanged") |
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 = {} |
| 41 | |
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 | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 44 | for bus_name in System.SYSTEM_CONFIG.keys(): |
| 45 | sys_state = System.SYSTEM_CONFIG[bus_name]['system_state'] |
| 46 | if (self.system_states.has_key(sys_state) == False): |
| 47 | self.system_states[sys_state] = [] |
| 48 | self.system_states[sys_state].append(bus_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 | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 57 | self.SystemStateHandler("INIT") |
| 58 | print "SystemManager Init Done" |
| 59 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 60 | def SystemStateHandler(self,state_name): |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 61 | print "Checking previous state started..." |
| 62 | i = 0 |
Norman James | 7caa10e | 2015-09-27 22:39:52 -0500 | [diff] [blame] | 63 | started = self.check_state_started() |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -0500 | [diff] [blame] | 64 | while(i<10 and started == False): |
| 65 | started = self.check_state_started() |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 66 | i=i+1 |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -0500 | [diff] [blame] | 67 | time.sleep(1) |
| 68 | |
Norman James | 9a60a7b | 2015-10-06 16:53:23 -0500 | [diff] [blame] | 69 | if (i == STATE_START_TIMEOUT): |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 70 | print "ERROR: Timeout waiting for state to finish: "+self.current_state |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -0500 | [diff] [blame] | 71 | return |
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)): |
| 75 | for bus_name in self.system_states[state_name]: |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 76 | self.start_process(bus_name) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 77 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 78 | if (state_name == "INIT"): |
| 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 | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 82 | if (System.ENTER_STATE_CALLBACK.has_key(state_name)): |
| 83 | cb = System.ENTER_STATE_CALLBACK[state_name] |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 84 | obj = bus.get_object(cb['bus_name'],cb['obj_name']) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 85 | method = obj.get_dbus_method(cb['method_name'],cb['interface_name']) |
| 86 | method() |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 87 | |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 88 | self.current_state = state_name |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 89 | |
| 90 | @dbus.service.method(DBUS_NAME, |
| 91 | in_signature='ss', out_signature='a{ss}') |
| 92 | def getObjectFromId(self,category,key): |
| 93 | bus_name = "" |
| 94 | obj_path = "" |
| 95 | |
| 96 | if (System.ID_LOOKUP.has_key(category)): |
| 97 | if (System.ID_LOOKUP[category].has_key(key)): |
| 98 | obj_path = System.ID_LOOKUP[category][key] |
| 99 | else: |
| 100 | print "ERROR: key not found: "+category+","+key |
| 101 | |
| 102 | if (self.bus_name_lookup.has_key(obj_path)): |
| 103 | bus_name = self.bus_name_lookup[obj_path] |
| 104 | else: |
| 105 | print "ERROR: bus name not found for: "+obj_path |
| 106 | r = { 'bus_name' : bus_name, 'obj_path' : obj_path } |
| 107 | return r |
Norman James | d9f1d4e | 2015-10-14 09:40:18 -0500 | [diff] [blame] | 108 | |
| 109 | @dbus.service.method(DBUS_NAME, |
| 110 | in_signature='sy', out_signature='(ss)') |
| 111 | def getObjectFromByteId(self,category,key): |
| 112 | bus_name = "" |
| 113 | obj_path = "" |
| 114 | try: |
| 115 | byte = int(key) |
| 116 | obj_path = System.ID_LOOKUP[category][byte] |
| 117 | bus_name = self.bus_name_lookup[obj_path] |
| 118 | except Exception as e: |
| 119 | print "ERROR SystemManager: "+str(e)+" not found in lookup" |
| 120 | |
| 121 | return [bus_name,obj_path] |
| 122 | |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 123 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 124 | def start_process(self,bus_name): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 125 | if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True): |
Norman James | c36372d | 2015-10-08 07:03:07 -0500 | [diff] [blame] | 126 | process_name = self.bin_path+"/"+System.SYSTEM_CONFIG[bus_name]['process_name'] |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 127 | cmdline = [ ] |
| 128 | cmdline.append(process_name) |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 129 | System.SYSTEM_CONFIG[bus_name]['popen'] = None |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 130 | for instance in System.SYSTEM_CONFIG[bus_name]['instances']: |
| 131 | cmdline.append(instance['name']) |
| 132 | try: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 133 | print "Starting process: "+" ".join(cmdline)+": "+bus_name |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 134 | System.SYSTEM_CONFIG[bus_name]['popen'] = subprocess.Popen(cmdline) |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 135 | except Exception as e: |
| 136 | ## TODO: error |
Norman James | 8c6d838 | 2015-10-06 07:47:16 -0500 | [diff] [blame] | 137 | print "ERROR: starting process: "+" ".join(cmdline) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 138 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 139 | def heartbeat_check(self): |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 140 | for bus_name in System.SYSTEM_CONFIG.keys(): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 141 | if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True and |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 142 | System.SYSTEM_CONFIG[bus_name].has_key('popen') and |
| 143 | System.SYSTEM_CONFIG[bus_name]['monitor_process'] == True): |
Norman James | 5d78b4d | 2015-09-05 13:34:34 -0500 | [diff] [blame] | 144 | ## make sure process is still alive |
| 145 | p = System.SYSTEM_CONFIG[bus_name]['popen'] |
| 146 | p.poll() |
| 147 | if (p.returncode != None): |
| 148 | print "Process for "+bus_name+" appears to be dead" |
| 149 | self.start_process(bus_name) |
| 150 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 151 | return True |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 152 | |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 153 | def check_state_started(self): |
| 154 | r = True |
| 155 | if (self.current_state == ""): |
| 156 | return True |
Norman James | c36372d | 2015-10-08 07:03:07 -0500 | [diff] [blame] | 157 | if (self.system_states.has_key(self.current_state)): |
| 158 | for bus_name in self.system_states[self.current_state]: |
| 159 | if (System.SYSTEM_CONFIG[bus_name].has_key('popen') == False and |
| 160 | System.SYSTEM_CONFIG[bus_name]['start_process'] == True): |
| 161 | r = False |
| 162 | break; |
Norman James | 65a295a | 2015-09-26 22:21:10 -0500 | [diff] [blame] | 163 | return r |
| 164 | |
| 165 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 166 | def NewBusHandler(self, bus_name, a, b): |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 167 | if (len(b) > 0 and bus_name.find(Openbmc.BUS_PREFIX) == 0): |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 168 | objects = {} |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 169 | try: |
| 170 | Openbmc.get_objs(bus,bus_name,"",objects) |
| 171 | for instance_name in objects.keys(): |
| 172 | self.bus_name_lookup[objects[instance_name]['PATH']] = bus_name |
Norman James | 1899818 | 2015-10-11 21:54:53 -0500 | [diff] [blame] | 173 | except: |
| 174 | pass |
Norman James | 19e4591 | 2015-10-04 20:19:41 -0500 | [diff] [blame] | 175 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 176 | if (System.SYSTEM_CONFIG.has_key(bus_name)): |
| 177 | System.SYSTEM_CONFIG[bus_name]['heartbeat_count'] = 0 |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 178 | for instance_name in objects.keys(): |
| 179 | obj_path = objects[instance_name]['PATH'] |
| 180 | for instance in System.SYSTEM_CONFIG[bus_name]['instances']: |
| 181 | if (instance.has_key('properties') and instance['name'] == instance_name): |
| 182 | props = instance['properties'] |
| 183 | print "Load Properties: "+obj_path |
| 184 | self.property_manager.loadProperties(bus_name,obj_path,props) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 185 | ## If object has an init method, call it |
| 186 | for init_intf in objects[instance_name]['INIT']: |
| 187 | obj = bus.get_object(bus_name,obj_path) |
| 188 | intf = dbus.Interface(obj,init_intf) |
| 189 | print "Calling init method: " +obj_path+" : "+init_intf |
| 190 | intf.init() |
Norman James | dfdaca9 | 2015-09-27 22:11:15 -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 = '' |
| 197 | gpio_num = 0 |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 198 | if (System.GPIO_CONFIG.has_key(name) == False): |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 199 | # TODO: Error handling |
| 200 | print "ERROR: "+name+" not found in GPIO config table" |
| 201 | return ['',0,''] |
| 202 | else: |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 203 | gpio_num = System.GPIO_CONFIG[name]['gpio_num'] |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 204 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 205 | return [Openbmc.GPIO_DEV, gpio_num, System.GPIO_CONFIG[name]['direction']] |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 206 | |
Norman James | 8887267 | 2015-09-21 16:51:35 -0500 | [diff] [blame] | 207 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 208 | |
| 209 | if __name__ == '__main__': |
| 210 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 211 | bus = Openbmc.getDBus() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 212 | name = dbus.service.BusName(DBUS_NAME,bus) |
| 213 | obj = SystemManager(bus,OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 214 | mainloop = gobject.MainLoop() |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 215 | |
Norman James | 89de916 | 2015-08-27 21:41:36 -0500 | [diff] [blame] | 216 | print "Running SystemManager" |
| 217 | mainloop.run() |
| 218 | |