blob: 7f945787045fab1795ef008018ade7886753e4b2 [file] [log] [blame]
Norman James89de9162015-08-27 21:41:36 -05001#!/usr/bin/env python
2
Norman James471ab592015-08-30 22:29:40 -05003import sys
Norman James89de9162015-08-27 21:41:36 -05004import subprocess
Norman James6f8d0422015-09-14 18:48:00 -05005import gobject
Norman James89de9162015-08-27 21:41:36 -05006import dbus
7import dbus.service
8import dbus.mainloop.glib
Norman James90baede2015-09-02 20:32:49 -05009import os
10import PropertyManager
Norman James88872672015-09-21 16:51:35 -050011import time
12import json
Norman James9e6acf92015-09-08 07:00:04 -050013import Openbmc
14
Norman James471ab592015-08-30 22:29:40 -050015if (len(sys.argv) < 2):
16 print "Usage: system_manager.py [system name]"
17 exit(1)
18
19System = __import__(sys.argv[1])
20import Openbmc
Norman James89de9162015-08-27 21:41:36 -050021
22DBUS_NAME = 'org.openbmc.managers.System'
23OBJ_NAME = '/org/openbmc/managers/System'
Norman James471ab592015-08-30 22:29:40 -050024HEARTBEAT_CHECK_INTERVAL = 20000
Norman James9a60a7b2015-10-06 16:53:23 -050025STATE_START_TIMEOUT = 10
Norman James66c02692015-10-15 10:23:12 -050026INTF_SENSOR = 'org.openbmc.SensorValue'
27INTF_ITEM = 'org.openbmc.InventoryItem'
Norman Jamesa3e47c42015-10-18 14:43:10 -050028INTF_CONTROL = 'org.openbmc.Control'
Norman James89de9162015-08-27 21:41:36 -050029
30class SystemManager(dbus.service.Object):
31 def __init__(self,bus,name):
32 dbus.service.Object.__init__(self,bus,name)
Norman James5d78b4d2015-09-05 13:34:34 -050033 self.property_manager = PropertyManager.PropertyManager(bus,System.CACHE_PATH)
Norman James9e6acf92015-09-08 07:00:04 -050034
Norman James90baede2015-09-02 20:32:49 -050035 ## Signal handlers
36 bus.add_signal_receiver(self.NewBusHandler,
Norman James89de9162015-08-27 21:41:36 -050037 dbus_interface = 'org.freedesktop.DBus',
38 signal_name = "NameOwnerChanged")
Norman James362a80f2015-09-14 14:04:39 -050039 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050040
Norman James362a80f2015-09-14 14:04:39 -050041 self.current_state = ""
42 self.system_states = {}
Norman James19e45912015-10-04 20:19:41 -050043 self.bus_name_lookup = {}
Norman Jamesc36372d2015-10-08 07:03:07 -050044 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
45
Norman James362a80f2015-09-14 14:04:39 -050046 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 James19e45912015-10-04 20:19:41 -050051
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 James7aeefa72015-10-19 11:13:25 -050059 self.SystemStateHandler(System.SYSTEM_STATES[0])
Norman James362a80f2015-09-14 14:04:39 -050060 print "SystemManager Init Done"
61
Norman James7aeefa72015-10-19 11:13:25 -050062
Norman James362a80f2015-09-14 14:04:39 -050063 def SystemStateHandler(self,state_name):
Norman James65a295a2015-09-26 22:21:10 -050064 print "Checking previous state started..."
65 i = 0
Norman James7caa10e2015-09-27 22:39:52 -050066 started = self.check_state_started()
Norman Jamesdfdaca92015-09-27 22:11:15 -050067 while(i<10 and started == False):
68 started = self.check_state_started()
Norman James65a295a2015-09-26 22:21:10 -050069 i=i+1
Norman Jamesdfdaca92015-09-27 22:11:15 -050070 time.sleep(1)
71
Norman James9a60a7b2015-10-06 16:53:23 -050072 if (i == STATE_START_TIMEOUT):
Norman James8c6d8382015-10-06 07:47:16 -050073 print "ERROR: Timeout waiting for state to finish: "+self.current_state
Norman Jamesdfdaca92015-09-27 22:11:15 -050074 return
Norman Jamesa3e47c42015-10-18 14:43:10 -050075
76 ## clearing object started flags
77 try:
78 for obj_path in System.EXIT_STATE_DEPEND[self.current_state]:
79 System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 0
80 except:
81 pass
Norman James65a295a2015-09-26 22:21:10 -050082
Norman James362a80f2015-09-14 14:04:39 -050083 print "Running System State: "+state_name
84 if (self.system_states.has_key(state_name)):
85 for bus_name in self.system_states[state_name]:
Norman James90baede2015-09-02 20:32:49 -050086 self.start_process(bus_name)
Norman James90baede2015-09-02 20:32:49 -050087
Norman Jamesa3e47c42015-10-18 14:43:10 -050088 if (state_name == "BMC_INIT"):
Norman James362a80f2015-09-14 14:04:39 -050089 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -050090 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -050091
Norman Jamesa3e47c42015-10-18 14:43:10 -050092 try:
Norman James362a80f2015-09-14 14:04:39 -050093 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman James8c6d8382015-10-06 07:47:16 -050094 obj = bus.get_object(cb['bus_name'],cb['obj_name'])
Norman James362a80f2015-09-14 14:04:39 -050095 method = obj.get_dbus_method(cb['method_name'],cb['interface_name'])
96 method()
Norman Jamesa3e47c42015-10-18 14:43:10 -050097 except:
98 pass
Norman James471ab592015-08-30 22:29:40 -050099
Norman James65a295a2015-09-26 22:21:10 -0500100 self.current_state = state_name
Norman James7aeefa72015-10-19 11:13:25 -0500101
102 def gotoNextState(self):
103 s = 0
104 for i in range(len(System.SYSTEM_STATES)):
105 if (System.SYSTEM_STATES[i] == self.current_state):
106 s = i+1
107
108 if (s == len(System.SYSTEM_STATES)):
109 print "ERROR SystemManager: No more system states"
110 else:
111 new_state_name = System.SYSTEM_STATES[s]
112 print "SystemManager Goto System State: "+new_state_name
113 self.SystemStateHandler(new_state_name)
114
115
Norman James19e45912015-10-04 20:19:41 -0500116 @dbus.service.method(DBUS_NAME,
Norman James7aeefa72015-10-19 11:13:25 -0500117 in_signature='', out_signature='s')
118 def getSystemState(self):
119 return self.current_state
120
121 def doObjectLookup(self,category,key):
Norman James19e45912015-10-04 20:19:41 -0500122 bus_name = ""
123 obj_path = ""
Norman James66c02692015-10-15 10:23:12 -0500124 intf_name = INTF_ITEM
125 try:
126 obj_path = System.ID_LOOKUP[category][key]
Norman James19e45912015-10-04 20:19:41 -0500127 bus_name = self.bus_name_lookup[obj_path]
Norman James66c02692015-10-15 10:23:12 -0500128 parts = obj_path.split('/')
Norman Jamesa3e47c42015-10-18 14:43:10 -0500129 if (parts[3] == 'sensor'):
Norman James66c02692015-10-15 10:23:12 -0500130 intf_name = INTF_SENSOR
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500131 except Exception as e:
132 print "ERROR SystemManager: "+str(e)+" not found in lookup"
133
Norman James66c02692015-10-15 10:23:12 -0500134 return [bus_name,obj_path,intf_name]
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500135
Norman James7aeefa72015-10-19 11:13:25 -0500136 @dbus.service.method(DBUS_NAME,
137 in_signature='ss', out_signature='(sss)')
138 def getObjectFromId(self,category,key):
139 return self.doObjectLookup(category,key)
140
141 @dbus.service.method(DBUS_NAME,
142 in_signature='sy', out_signature='(sss)')
143 def getObjectFromByteId(self,category,key):
144 byte = int(key)
145 return self.doObjectLookup(category,byte)
Norman James19e45912015-10-04 20:19:41 -0500146
Norman James471ab592015-08-30 22:29:40 -0500147 def start_process(self,bus_name):
Norman James5d78b4d2015-09-05 13:34:34 -0500148 if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True):
Norman Jamesc36372d2015-10-08 07:03:07 -0500149 process_name = self.bin_path+"/"+System.SYSTEM_CONFIG[bus_name]['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500150 cmdline = [ ]
151 cmdline.append(process_name)
Norman James65a295a2015-09-26 22:21:10 -0500152 System.SYSTEM_CONFIG[bus_name]['popen'] = None
Norman James5d78b4d2015-09-05 13:34:34 -0500153 for instance in System.SYSTEM_CONFIG[bus_name]['instances']:
154 cmdline.append(instance['name'])
155 try:
Norman James362a80f2015-09-14 14:04:39 -0500156 print "Starting process: "+" ".join(cmdline)+": "+bus_name
Norman James65a295a2015-09-26 22:21:10 -0500157 System.SYSTEM_CONFIG[bus_name]['popen'] = subprocess.Popen(cmdline)
Norman James5d78b4d2015-09-05 13:34:34 -0500158 except Exception as e:
159 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500160 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500161
Norman James89de9162015-08-27 21:41:36 -0500162 def heartbeat_check(self):
Norman James471ab592015-08-30 22:29:40 -0500163 for bus_name in System.SYSTEM_CONFIG.keys():
Norman James362a80f2015-09-14 14:04:39 -0500164 if (System.SYSTEM_CONFIG[bus_name]['start_process'] == True and
Norman James6f8d0422015-09-14 18:48:00 -0500165 System.SYSTEM_CONFIG[bus_name].has_key('popen') and
166 System.SYSTEM_CONFIG[bus_name]['monitor_process'] == True):
Norman James5d78b4d2015-09-05 13:34:34 -0500167 ## make sure process is still alive
168 p = System.SYSTEM_CONFIG[bus_name]['popen']
169 p.poll()
170 if (p.returncode != None):
171 print "Process for "+bus_name+" appears to be dead"
172 self.start_process(bus_name)
173
Norman James471ab592015-08-30 22:29:40 -0500174 return True
Norman James89de9162015-08-27 21:41:36 -0500175
Norman James65a295a2015-09-26 22:21:10 -0500176 def check_state_started(self):
177 r = True
178 if (self.current_state == ""):
179 return True
Norman Jamesc36372d2015-10-08 07:03:07 -0500180 if (self.system_states.has_key(self.current_state)):
181 for bus_name in self.system_states[self.current_state]:
182 if (System.SYSTEM_CONFIG[bus_name].has_key('popen') == False and
183 System.SYSTEM_CONFIG[bus_name]['start_process'] == True):
184 r = False
185 break;
Norman James65a295a2015-09-26 22:21:10 -0500186 return r
187
Norman James90baede2015-09-02 20:32:49 -0500188 def NewBusHandler(self, bus_name, a, b):
Norman James471ab592015-08-30 22:29:40 -0500189 if (len(b) > 0 and bus_name.find(Openbmc.BUS_PREFIX) == 0):
Norman James19e45912015-10-04 20:19:41 -0500190 objects = {}
Norman James18998182015-10-11 21:54:53 -0500191 try:
192 Openbmc.get_objs(bus,bus_name,"",objects)
193 for instance_name in objects.keys():
Norman Jamesa3e47c42015-10-18 14:43:10 -0500194 obj_path = objects[instance_name]['PATH']
195 self.bus_name_lookup[obj_path] = bus_name
196 if (System.EXIT_STATE_DEPEND[self.current_state].has_key(obj_path) == True):
197 System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 1
198
Norman James18998182015-10-11 21:54:53 -0500199 except:
200 pass
Norman Jamesa3e47c42015-10-18 14:43:10 -0500201
Norman James471ab592015-08-30 22:29:40 -0500202 if (System.SYSTEM_CONFIG.has_key(bus_name)):
Norman James362a80f2015-09-14 14:04:39 -0500203 for instance_name in objects.keys():
204 obj_path = objects[instance_name]['PATH']
205 for instance in System.SYSTEM_CONFIG[bus_name]['instances']:
206 if (instance.has_key('properties') and instance['name'] == instance_name):
207 props = instance['properties']
208 print "Load Properties: "+obj_path
209 self.property_manager.loadProperties(bus_name,obj_path,props)
Norman James362a80f2015-09-14 14:04:39 -0500210 ## If object has an init method, call it
211 for init_intf in objects[instance_name]['INIT']:
212 obj = bus.get_object(bus_name,obj_path)
213 intf = dbus.Interface(obj,init_intf)
214 print "Calling init method: " +obj_path+" : "+init_intf
215 intf.init()
Norman Jamesdfdaca92015-09-27 22:11:15 -0500216
Norman Jamesa3e47c42015-10-18 14:43:10 -0500217 ## check if all objects are started to move to next state
218 try:
219 state = 1
220 for obj_path in System.EXIT_STATE_DEPEND[self.current_state]:
221 if (System.EXIT_STATE_DEPEND[self.current_state][obj_path] == 0):
222 state = 0
Norman James7aeefa72015-10-19 11:13:25 -0500223 ## all required objects have started so go to next state
Norman Jamesa3e47c42015-10-18 14:43:10 -0500224 if (state == 1):
Norman James7aeefa72015-10-19 11:13:25 -0500225 self.gotoNextState()
Norman Jamesa3e47c42015-10-18 14:43:10 -0500226 except:
227 pass
228
229
Norman James89de9162015-08-27 21:41:36 -0500230
231 @dbus.service.method(DBUS_NAME,
232 in_signature='s', out_signature='sis')
233 def gpioInit(self,name):
234 gpio_path = ''
235 gpio_num = 0
Norman James471ab592015-08-30 22:29:40 -0500236 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500237 # TODO: Error handling
238 print "ERROR: "+name+" not found in GPIO config table"
239 return ['',0,'']
240 else:
Norman James471ab592015-08-30 22:29:40 -0500241 gpio_num = System.GPIO_CONFIG[name]['gpio_num']
Norman James89de9162015-08-27 21:41:36 -0500242
Norman James471ab592015-08-30 22:29:40 -0500243 return [Openbmc.GPIO_DEV, gpio_num, System.GPIO_CONFIG[name]['direction']]
Norman James89de9162015-08-27 21:41:36 -0500244
Norman James88872672015-09-21 16:51:35 -0500245
Norman James89de9162015-08-27 21:41:36 -0500246
247if __name__ == '__main__':
248 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -0500249 bus = Openbmc.getDBus()
Norman James89de9162015-08-27 21:41:36 -0500250 name = dbus.service.BusName(DBUS_NAME,bus)
251 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500252 mainloop = gobject.MainLoop()
Norman James89de9162015-08-27 21:41:36 -0500253
Norman James89de9162015-08-27 21:41:36 -0500254 print "Running SystemManager"
255 mainloop.run()
256