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