blob: 6969485972c0c235860a15bc3639fc0db4679a5b [file] [log] [blame]
Norman James42c1be82015-10-22 14:34:26 -05001#!/usr/bin/python -u
Norman James89de9162015-08-27 21:41:36 -05002
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
Norman James88872672015-09-21 16:51:35 -050010import time
Brad Bishopee1b1542016-05-12 16:55:00 -040011import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040012from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
13import obmc.enums
Brad Bishop0b380f72016-06-10 00:29:50 -040014import obmc_system_config as System
Brad Bishopfa736492016-06-29 22:21:49 -040015import obmc.dbuslib.introspection
16import obmc.utils.misc
Norman James89de9162015-08-27 21:41:36 -050017
18DBUS_NAME = 'org.openbmc.managers.System'
19OBJ_NAME = '/org/openbmc/managers/System'
Norman James471ab592015-08-30 22:29:40 -050020HEARTBEAT_CHECK_INTERVAL = 20000
Norman James9a60a7b2015-10-06 16:53:23 -050021STATE_START_TIMEOUT = 10
Norman James66c02692015-10-15 10:23:12 -050022INTF_SENSOR = 'org.openbmc.SensorValue'
23INTF_ITEM = 'org.openbmc.InventoryItem'
Norman Jamesa3e47c42015-10-18 14:43:10 -050024INTF_CONTROL = 'org.openbmc.Control'
Norman James89de9162015-08-27 21:41:36 -050025
Norman Jamescf74f952015-10-28 12:45:18 -050026
Brad Bishop416539d2016-07-22 07:22:42 -040027class SystemManager(DbusProperties, DbusObjectManager):
28 def __init__(self, bus, obj_name):
29 DbusProperties.__init__(self)
30 DbusObjectManager.__init__(self)
31 dbus.service.Object.__init__(self, bus, obj_name)
32 self.bus = bus
Norman James9e6acf92015-09-08 07:00:04 -050033
Brad Bishop416539d2016-07-22 07:22:42 -040034 bus.add_signal_receiver(
35 self.bus_handler,
36 dbus_interface=dbus.BUS_DAEMON_IFACE,
37 signal_name='NameOwnerChanged')
38 bus.add_signal_receiver(
39 self.NewObjectHandler,
40 signal_name="InterfacesAdded", sender_keyword='bus_name')
41 bus.add_signal_receiver(
42 self.SystemStateHandler, signal_name="GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050043
Brad Bishop416539d2016-07-22 07:22:42 -040044 self.Set(DBUS_NAME, "current_state", "")
45 self.system_states = {}
46 self.bus_name_lookup = {}
47 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
Norman Jamesc36372d2015-10-08 07:03:07 -050048
Brad Bishop416539d2016-07-22 07:22:42 -040049 for name in System.APPS.keys():
50 sys_state = System.APPS[name]['system_state']
51 if sys_state not in self.system_states:
52 self.system_states[sys_state] = []
53 self.system_states[sys_state].append(name)
Norman Jamescfc2b442015-10-31 17:31:46 -050054
Brad Bishop416539d2016-07-22 07:22:42 -040055 ## replace symbolic path in ID_LOOKUP
56 for category in System.ID_LOOKUP:
57 for key in System.ID_LOOKUP[category]:
58 val = System.ID_LOOKUP[category][key]
59 new_val = val.replace(
60 "<inventory_root>", System.INVENTORY_ROOT)
61 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050062
Brad Bishop416539d2016-07-22 07:22:42 -040063 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040064
Brad Bishop416539d2016-07-22 07:22:42 -040065 if not os.path.exists(PropertyCacher.CACHE_PATH):
66 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
67 os.makedirs(PropertyCacher.CACHE_PATH)
Norman James362a80f2015-09-14 14:04:39 -050068
Brad Bishop416539d2016-07-22 07:22:42 -040069 for s in self.bus.list_names():
70 if obmc.utils.misc.org_dot_openbmc_match(s):
71 self.bus_handler(s, '', s)
Norman James7aeefa72015-10-19 11:13:25 -050072
Brad Bishop416539d2016-07-22 07:22:42 -040073 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040074
Brad Bishop416539d2016-07-22 07:22:42 -040075 def try_next_state(self):
76 current_state = self.Get(DBUS_NAME, "current_state")
77 if current_state not in System.EXIT_STATE_DEPEND:
78 return
Brad Bishop4de42642016-06-29 21:55:47 -040079
Brad Bishop416539d2016-07-22 07:22:42 -040080 if all(System.EXIT_STATE_DEPEND[current_state].values()):
81 print "All required objects started for "+current_state
82 self.gotoNextState()
Brad Bishop4de42642016-06-29 21:55:47 -040083
Brad Bishop416539d2016-07-22 07:22:42 -040084 def SystemStateHandler(self, state_name):
85 ## clearing object started flags
86 current_state = self.Get(DBUS_NAME, "current_state")
87 try:
88 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
89 System.EXIT_STATE_DEPEND[current_state][obj_path] = 0
90 except:
91 pass
Norman James65a295a2015-09-26 22:21:10 -050092
Brad Bishop416539d2016-07-22 07:22:42 -040093 print "Running System State: "+state_name
94 if state_name in self.system_states:
95 for name in self.system_states[state_name]:
96 self.start_process(name)
Norman James471ab592015-08-30 22:29:40 -050097
Brad Bishop416539d2016-07-22 07:22:42 -040098 if (state_name == "BMC_INIT"):
99 ## Add poll for heartbeat
100 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James7aeefa72015-10-19 11:13:25 -0500101
Brad Bishop416539d2016-07-22 07:22:42 -0400102 try:
103 cb = System.ENTER_STATE_CALLBACK[state_name]
104 for methd in cb.keys():
105 obj = bus.get_object(
106 cb[methd]['bus_name'],
107 cb[methd]['obj_name'],
108 introspect=False)
109 method = obj.get_dbus_method(
110 methd, cb[methd]['interface_name'])
111 method()
112 except:
113 pass
Norman James7aeefa72015-10-19 11:13:25 -0500114
Brad Bishop416539d2016-07-22 07:22:42 -0400115 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500116
Brad Bishop416539d2016-07-22 07:22:42 -0400117 def gotoNextState(self):
118 s = 0
119 current_state = self.Get(DBUS_NAME, "current_state")
120 for i in range(len(System.SYSTEM_STATES)):
121 if (System.SYSTEM_STATES[i] == current_state):
122 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500123
Brad Bishop416539d2016-07-22 07:22:42 -0400124 if (s == len(System.SYSTEM_STATES)):
125 print "ERROR SystemManager: No more system states"
126 else:
127 new_state_name = System.SYSTEM_STATES[s]
128 print "SystemManager Goto System State: "+new_state_name
129 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500130
Brad Bishop416539d2016-07-22 07:22:42 -0400131 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
132 def getSystemState(self):
133 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +0800134
Brad Bishop416539d2016-07-22 07:22:42 -0400135 def doObjectLookup(self, category, key):
136 bus_name = ""
137 obj_path = ""
138 intf_name = INTF_ITEM
139 try:
140 obj_path = System.ID_LOOKUP[category][key]
141 bus_name = self.bus_name_lookup[obj_path]
142 parts = obj_path.split('/')
143 if (parts[3] == 'sensors'):
144 intf_name = INTF_SENSOR
145 except Exception as e:
146 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +0800147
Brad Bishop416539d2016-07-22 07:22:42 -0400148 return [bus_name, obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -0500149
Brad Bishop416539d2016-07-22 07:22:42 -0400150 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(sss)')
151 def getObjectFromId(self, category, key):
152 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -0500153
Brad Bishop416539d2016-07-22 07:22:42 -0400154 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(sss)')
155 def getObjectFromByteId(self, category, key):
156 byte = int(key)
157 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -0400158
Brad Bishop416539d2016-07-22 07:22:42 -0400159 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
160 # If serval areas are defined for a fru_id, the areas are returned
161 # together as a string with each area name seperated with ','.
162 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
163 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
164 def getFRUArea(self, fru_id):
165 ret_str = ''
166 fru_id = '_' + str(fru_id)
167 area_list = [
168 area for area in System.ID_LOOKUP['FRU_STR'].keys()
169 if area.endswith(fru_id)]
170 for area in area_list:
171 ret_str = area + ',' + ret_str
172 # remove the last ','
173 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400174
Brad Bishop416539d2016-07-22 07:22:42 -0400175 def start_process(self, name):
176 if System.APPS[name]['start_process']:
177 app = System.APPS[name]
178 process_name = self.bin_path+"/"+app['process_name']
179 cmdline = []
180 cmdline.append(process_name)
181 if 'args' in app:
182 for a in app['args']:
183 cmdline.append(a)
184 try:
185 print "Starting process: "+" ".join(cmdline)+": "+name
186 if app['monitor_process']:
187 app['popen'] = subprocess.Popen(cmdline)
188 else:
189 subprocess.Popen(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400190
Brad Bishop416539d2016-07-22 07:22:42 -0400191 except Exception as e:
192 ## TODO: error
193 print "ERROR: starting process: "+" ".join(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400194
Brad Bishop416539d2016-07-22 07:22:42 -0400195 def heartbeat_check(self):
196 for name in System.APPS.keys():
197 app = System.APPS[name]
198 if app['start_process'] and 'popen' in app:
199 ## make sure process is still alive
200 p = app['popen']
201 p.poll()
202 if p.returncode is None:
203 print "Process for "+name+" appears to be dead"
204 self.start_process(name)
Brad Bishopfa736492016-06-29 22:21:49 -0400205
Brad Bishop416539d2016-07-22 07:22:42 -0400206 return True
Norman Jamesa3e47c42015-10-18 14:43:10 -0500207
Brad Bishop416539d2016-07-22 07:22:42 -0400208 def bus_handler(self, owned_name, old, new):
209 if obmc.dbuslib.bindings.is_unique(owned_name) or not new:
210 return
Norman James89de9162015-08-27 21:41:36 -0500211
Brad Bishop416539d2016-07-22 07:22:42 -0400212 if owned_name == DBUS_NAME:
213 return
Norman James5236a8f2015-11-05 20:39:31 -0600214
Brad Bishop416539d2016-07-22 07:22:42 -0400215 objs = obmc.dbuslib.introspection.find_dbus_interfaces(
216 self.bus, owned_name, '/', bool)
217 current_state = self.Get(DBUS_NAME, "current_state")
218 for o in objs.keys():
219 if o in self.bus_name_lookup:
220 continue
221 self.bus_name_lookup[o] = owned_name
222
223 if current_state not in System.EXIT_STATE_DEPEND:
224 continue
225 if o in System.EXIT_STATE_DEPEND[current_state]:
226 print "New object: "+o+" ("+owned_name+")"
227 System.EXIT_STATE_DEPEND[current_state][o] = 1
228
229 self.try_next_state()
230
231 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
232 current_state = self.Get(DBUS_NAME, "current_state")
233 if obj_path in self.bus_name_lookup:
234 if (self.bus_name_lookup[obj_path] == bus_name):
235 return
236 self.bus_name_lookup[obj_path] = bus_name
237 if current_state not in System.EXIT_STATE_DEPEND:
238 return
239
240 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
241 print "New object: "+obj_path+" ("+bus_name+")"
242 System.EXIT_STATE_DEPEND[current_state][obj_path] = 1
243 ## check if all required objects are
244 # started to move to next state
245 self.try_next_state()
246
247 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
248 def gpioInit(self, name):
249 gpio_path = ''
250 gpio_num = -1
251 r = ['', gpio_num, '']
252 if name not in System.GPIO_CONFIG:
253 # TODO: Error handling
254 print "ERROR: "+name+" not found in GPIO config table"
255 else:
256
257 gpio_num = -1
258 gpio = System.GPIO_CONFIG[name]
259 if 'gpio_num' in System.GPIO_CONFIG[name]:
260 gpio_num = gpio['gpio_num']
261 else:
262 if 'gpio_pin' in System.GPIO_CONFIG[name]:
263 gpio_num = System.convertGpio(gpio['gpio_pin'])
264 else:
265 print "ERROR: SystemManager - GPIO lookup failed for "+name
266
267 if (gpio_num != -1):
268 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
269 return r
270
Norman James89de9162015-08-27 21:41:36 -0500271
272if __name__ == '__main__':
273 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400274 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400275 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500276 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400277 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400278 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500279
Norman James89de9162015-08-27 21:41:36 -0500280 print "Running SystemManager"
281 mainloop.run()