blob: 1d35ac9c593e5317f2ba7fedc8c9b702eacf59b0 [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 Bishop84e73b52016-05-12 15:57:52 -040027class SystemManager(DbusProperties,DbusObjectManager):
Norman James98e1f7b2015-11-24 22:17:56 -060028 def __init__(self,bus,obj_name):
Brad Bishop84e73b52016-05-12 15:57:52 -040029 DbusProperties.__init__(self)
30 DbusObjectManager.__init__(self)
Norman James98e1f7b2015-11-24 22:17:56 -060031 dbus.service.Object.__init__(self,bus,obj_name)
Brad Bishopfa736492016-06-29 22:21:49 -040032 self.bus = bus
Norman James9e6acf92015-09-08 07:00:04 -050033
Brad Bishopfa736492016-06-29 22:21:49 -040034 bus.add_signal_receiver(
35 self.bus_handler,
36 dbus_interface=dbus.BUS_DAEMON_IFACE,
37 signal_name='NameOwnerChanged')
Norman Jamescfc2b442015-10-31 17:31:46 -050038 bus.add_signal_receiver(self.NewObjectHandler,
Brad Bishop58e694d2016-04-13 16:04:32 -040039 signal_name = "InterfacesAdded", sender_keyword = 'bus_name')
Norman James362a80f2015-09-14 14:04:39 -050040 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050041
Norman James98e1f7b2015-11-24 22:17:56 -060042 self.Set(DBUS_NAME,"current_state","")
Norman James362a80f2015-09-14 14:04:39 -050043 self.system_states = {}
Norman James19e45912015-10-04 20:19:41 -050044 self.bus_name_lookup = {}
Norman Jamesc36372d2015-10-08 07:03:07 -050045 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
46
Norman Jamescf74f952015-10-28 12:45:18 -050047 for name in System.APPS.keys():
48 sys_state = System.APPS[name]['system_state']
Norman James362a80f2015-09-14 14:04:39 -050049 if (self.system_states.has_key(sys_state) == False):
50 self.system_states[sys_state] = []
Norman Jamescf74f952015-10-28 12:45:18 -050051 self.system_states[sys_state].append(name)
Norman James19e45912015-10-04 20:19:41 -050052
53 ## replace symbolic path in ID_LOOKUP
54 for category in System.ID_LOOKUP:
55 for key in System.ID_LOOKUP[category]:
56 val = System.ID_LOOKUP[category][key]
57 new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT)
58 System.ID_LOOKUP[category][key] = new_val
59
Norman James7aeefa72015-10-19 11:13:25 -050060 self.SystemStateHandler(System.SYSTEM_STATES[0])
Norman Jamescfc2b442015-10-31 17:31:46 -050061
62 if not os.path.exists(PropertyCacher.CACHE_PATH):
63 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
64 os.makedirs(PropertyCacher.CACHE_PATH)
65
Brad Bishopfa736492016-06-29 22:21:49 -040066 for s in self.bus.list_names():
67 if obmc.utils.misc.org_dot_openbmc_match(s):
68 self.bus_handler(s, '', s)
69
Norman James362a80f2015-09-14 14:04:39 -050070 print "SystemManager Init Done"
71
Norman James7aeefa72015-10-19 11:13:25 -050072
Brad Bishop4de42642016-06-29 21:55:47 -040073 def try_next_state(self):
74 current_state = self.Get(DBUS_NAME,"current_state")
75 if current_state not in System.EXIT_STATE_DEPEND:
76 return
77
78 if all(System.EXIT_STATE_DEPEND[current_state].values()):
79 print "All required objects started for "+current_state
80 self.gotoNextState()
81
82
Norman James362a80f2015-09-14 14:04:39 -050083 def SystemStateHandler(self,state_name):
Norman Jamesa3e47c42015-10-18 14:43:10 -050084 ## clearing object started flags
Norman James98e1f7b2015-11-24 22:17:56 -060085 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamesa3e47c42015-10-18 14:43:10 -050086 try:
Norman James98e1f7b2015-11-24 22:17:56 -060087 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
88 System.EXIT_STATE_DEPEND[current_state][obj_path] = 0
Norman Jamesa3e47c42015-10-18 14:43:10 -050089 except:
90 pass
Norman James65a295a2015-09-26 22:21:10 -050091
Norman James362a80f2015-09-14 14:04:39 -050092 print "Running System State: "+state_name
93 if (self.system_states.has_key(state_name)):
Norman Jamescf74f952015-10-28 12:45:18 -050094 for name in self.system_states[state_name]:
95 self.start_process(name)
Norman James90baede2015-09-02 20:32:49 -050096
Norman Jamesa3e47c42015-10-18 14:43:10 -050097 if (state_name == "BMC_INIT"):
Norman James362a80f2015-09-14 14:04:39 -050098 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -050099 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -0500100
Norman Jamesa3e47c42015-10-18 14:43:10 -0500101 try:
Norman James362a80f2015-09-14 14:04:39 -0500102 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman Jamese38859c2015-10-29 06:19:40 -0500103 for methd in cb.keys():
Norman James85f050b2015-12-18 14:58:20 -0600104 obj = bus.get_object(cb[methd]['bus_name'],cb[methd]['obj_name'],introspect=False)
Norman Jamese38859c2015-10-29 06:19:40 -0500105 method = obj.get_dbus_method(methd,cb[methd]['interface_name'])
106 method()
Norman Jamesa3e47c42015-10-18 14:43:10 -0500107 except:
108 pass
Norman James471ab592015-08-30 22:29:40 -0500109
Norman James98e1f7b2015-11-24 22:17:56 -0600110 self.Set(DBUS_NAME,"current_state",state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500111
112 def gotoNextState(self):
113 s = 0
Norman James98e1f7b2015-11-24 22:17:56 -0600114 current_state = self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500115 for i in range(len(System.SYSTEM_STATES)):
Norman James98e1f7b2015-11-24 22:17:56 -0600116 if (System.SYSTEM_STATES[i] == current_state):
Norman James7aeefa72015-10-19 11:13:25 -0500117 s = i+1
118
119 if (s == len(System.SYSTEM_STATES)):
120 print "ERROR SystemManager: No more system states"
121 else:
122 new_state_name = System.SYSTEM_STATES[s]
123 print "SystemManager Goto System State: "+new_state_name
124 self.SystemStateHandler(new_state_name)
125
126
Norman James19e45912015-10-04 20:19:41 -0500127 @dbus.service.method(DBUS_NAME,
Norman James7aeefa72015-10-19 11:13:25 -0500128 in_signature='', out_signature='s')
129 def getSystemState(self):
Norman James98e1f7b2015-11-24 22:17:56 -0600130 return self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500131
132 def doObjectLookup(self,category,key):
Norman James19e45912015-10-04 20:19:41 -0500133 bus_name = ""
134 obj_path = ""
Norman James66c02692015-10-15 10:23:12 -0500135 intf_name = INTF_ITEM
136 try:
137 obj_path = System.ID_LOOKUP[category][key]
Norman James19e45912015-10-04 20:19:41 -0500138 bus_name = self.bus_name_lookup[obj_path]
Norman James66c02692015-10-15 10:23:12 -0500139 parts = obj_path.split('/')
Norman James323ed972015-12-09 09:06:37 -0600140 if (parts[3] == 'sensors'):
Norman James66c02692015-10-15 10:23:12 -0500141 intf_name = INTF_SENSOR
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500142 except Exception as e:
143 print "ERROR SystemManager: "+str(e)+" not found in lookup"
144
Norman James66c02692015-10-15 10:23:12 -0500145 return [bus_name,obj_path,intf_name]
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500146
Norman James7aeefa72015-10-19 11:13:25 -0500147 @dbus.service.method(DBUS_NAME,
148 in_signature='ss', out_signature='(sss)')
149 def getObjectFromId(self,category,key):
150 return self.doObjectLookup(category,key)
151
152 @dbus.service.method(DBUS_NAME,
153 in_signature='sy', out_signature='(sss)')
154 def getObjectFromByteId(self,category,key):
155 byte = int(key)
156 return self.doObjectLookup(category,byte)
Yi Li5cf1e112016-04-15 15:30:30 +0800157
158 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
159 # If serval areas are defined for a fru_id, the areas are returned
160 # together as a string with each area name seperated with ','.
161 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
162 @dbus.service.method(DBUS_NAME,
163 in_signature='y', out_signature='s')
164 def getFRUArea(self,fru_id):
165 ret_str = ''
166 fru_id = '_' + str(fru_id)
167 area_list = [area for area in System.ID_LOOKUP['FRU_STR'].keys() \
168 if area.endswith(fru_id)]
169 for area in area_list:
170 ret_str = area + ',' + ret_str
171 # remove the last ','
172 return ret_str[:-1]
173
Norman Jamescf74f952015-10-28 12:45:18 -0500174 def start_process(self,name):
175 if (System.APPS[name]['start_process'] == True):
176 app = System.APPS[name]
177 process_name = self.bin_path+"/"+app['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500178 cmdline = [ ]
179 cmdline.append(process_name)
Norman Jamescf74f952015-10-28 12:45:18 -0500180 if (app.has_key('args')):
181 for a in app['args']:
182 cmdline.append(a)
Norman James5d78b4d2015-09-05 13:34:34 -0500183 try:
Norman Jamescf74f952015-10-28 12:45:18 -0500184 print "Starting process: "+" ".join(cmdline)+": "+name
Norman Jamesbfa561e2015-11-01 07:38:14 -0600185 if (app['monitor_process'] == True):
186 app['popen'] = subprocess.Popen(cmdline)
187 else:
188 subprocess.Popen(cmdline)
189
Norman James5d78b4d2015-09-05 13:34:34 -0500190 except Exception as e:
191 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500192 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500193
Norman James89de9162015-08-27 21:41:36 -0500194 def heartbeat_check(self):
Norman Jamescf74f952015-10-28 12:45:18 -0500195 for name in System.APPS.keys():
196 app = System.APPS[name]
Norman Jamesbfa561e2015-11-01 07:38:14 -0600197 if (app['start_process'] == True and app.has_key('popen')):
Norman James5d78b4d2015-09-05 13:34:34 -0500198 ## make sure process is still alive
Norman Jamescf74f952015-10-28 12:45:18 -0500199 p = app['popen']
Norman James5d78b4d2015-09-05 13:34:34 -0500200 p.poll()
201 if (p.returncode != None):
Norman Jamescf74f952015-10-28 12:45:18 -0500202 print "Process for "+name+" appears to be dead"
203 self.start_process(name)
Norman James5d78b4d2015-09-05 13:34:34 -0500204
Norman James471ab592015-08-30 22:29:40 -0500205 return True
Norman James89de9162015-08-27 21:41:36 -0500206
Brad Bishopfa736492016-06-29 22:21:49 -0400207 def bus_handler(self, owned_name, old, new):
208 if obmc.dbuslib.bindings.is_unique(owned_name) or not new:
209 return
210
211 if owned_name == DBUS_NAME:
212 return
213
214 objs = obmc.dbuslib.introspection.find_dbus_interfaces(
215 self.bus, owned_name, '/', bool)
216 current_state = self.Get(DBUS_NAME,"current_state")
217 for o in objs.keys():
218 if o in self.bus_name_lookup:
219 continue
220 self.bus_name_lookup[o] = owned_name
221
222 if current_state not in System.EXIT_STATE_DEPEND:
223 continue
224 if o in System.EXIT_STATE_DEPEND[current_state]:
225 print "New object: "+o+" ("+owned_name+")"
226 System.EXIT_STATE_DEPEND[current_state][o] = 1
227
228 self.try_next_state()
229
Brad Bishop58e694d2016-04-13 16:04:32 -0400230 def NewObjectHandler(self, obj_path, iprops, bus_name = None):
Norman James98e1f7b2015-11-24 22:17:56 -0600231 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamescfc2b442015-10-31 17:31:46 -0500232 if (self.bus_name_lookup.has_key(obj_path)):
233 if (self.bus_name_lookup[obj_path] == bus_name):
234 return
235 self.bus_name_lookup[obj_path] = bus_name
Brad Bishop4de42642016-06-29 21:55:47 -0400236 if current_state not in System.EXIT_STATE_DEPEND:
237 return
Norman Jamesa3e47c42015-10-18 14:43:10 -0500238
Brad Bishop4de42642016-06-29 21:55:47 -0400239 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
240 print "New object: "+obj_path+" ("+bus_name+")"
241 System.EXIT_STATE_DEPEND[current_state][obj_path] = 1
242 ## check if all required objects are
243 # started to move to next state
244 self.try_next_state()
Norman James89de9162015-08-27 21:41:36 -0500245
246 @dbus.service.method(DBUS_NAME,
247 in_signature='s', out_signature='sis')
248 def gpioInit(self,name):
249 gpio_path = ''
Norman Jamescf74f952015-10-28 12:45:18 -0500250 gpio_num = -1
251 r = ['',gpio_num,'']
Norman James471ab592015-08-30 22:29:40 -0500252 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500253 # TODO: Error handling
254 print "ERROR: "+name+" not found in GPIO config table"
Norman James89de9162015-08-27 21:41:36 -0500255 else:
Norman Jamescf74f952015-10-28 12:45:18 -0500256
257 gpio_num = -1
258 gpio = System.GPIO_CONFIG[name]
259 if (System.GPIO_CONFIG[name].has_key('gpio_num')):
260 gpio_num = gpio['gpio_num']
261 else:
262 if (System.GPIO_CONFIG[name].has_key('gpio_pin')):
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):
Brad Bishop84e73b52016-05-12 15:57:52 -0400268 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
Norman Jamescf74f952015-10-28 12:45:18 -0500269 return r
Norman James5236a8f2015-11-05 20:39:31 -0600270
Norman James88872672015-09-21 16:51:35 -0500271
Norman James89de9162015-08-27 21:41:36 -0500272
273if __name__ == '__main__':
274 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400275 bus = get_dbus()
Norman James89de9162015-08-27 21:41:36 -0500276 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500277 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400278 obj.unmask_signals()
Brad Bishop70852a32016-06-29 22:58:51 -0400279 name = dbus.service.BusName(DBUS_NAME,bus)
Norman James89de9162015-08-27 21:41:36 -0500280
Norman James89de9162015-08-27 21:41:36 -0500281 print "Running SystemManager"
282 mainloop.run()
283