blob: 9625c45359d67eac61a674eb9ae3ab93d695066a [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 James323ed972015-12-09 09:06:37 -060070 self.InterfacesAdded(obj_name,self.properties)
Norman James362a80f2015-09-14 14:04:39 -050071 print "SystemManager Init Done"
72
Norman James7aeefa72015-10-19 11:13:25 -050073
Brad Bishop4de42642016-06-29 21:55:47 -040074 def try_next_state(self):
75 current_state = self.Get(DBUS_NAME,"current_state")
76 if current_state not in System.EXIT_STATE_DEPEND:
77 return
78
79 if all(System.EXIT_STATE_DEPEND[current_state].values()):
80 print "All required objects started for "+current_state
81 self.gotoNextState()
82
83
Norman James362a80f2015-09-14 14:04:39 -050084 def SystemStateHandler(self,state_name):
Norman Jamesa3e47c42015-10-18 14:43:10 -050085 ## clearing object started flags
Norman James98e1f7b2015-11-24 22:17:56 -060086 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamesa3e47c42015-10-18 14:43:10 -050087 try:
Norman James98e1f7b2015-11-24 22:17:56 -060088 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
89 System.EXIT_STATE_DEPEND[current_state][obj_path] = 0
Norman Jamesa3e47c42015-10-18 14:43:10 -050090 except:
91 pass
Norman James65a295a2015-09-26 22:21:10 -050092
Norman James362a80f2015-09-14 14:04:39 -050093 print "Running System State: "+state_name
94 if (self.system_states.has_key(state_name)):
Norman Jamescf74f952015-10-28 12:45:18 -050095 for name in self.system_states[state_name]:
96 self.start_process(name)
Norman James90baede2015-09-02 20:32:49 -050097
Norman Jamesa3e47c42015-10-18 14:43:10 -050098 if (state_name == "BMC_INIT"):
Norman James362a80f2015-09-14 14:04:39 -050099 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -0500100 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -0500101
Norman Jamesa3e47c42015-10-18 14:43:10 -0500102 try:
Norman James362a80f2015-09-14 14:04:39 -0500103 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman Jamese38859c2015-10-29 06:19:40 -0500104 for methd in cb.keys():
Norman James85f050b2015-12-18 14:58:20 -0600105 obj = bus.get_object(cb[methd]['bus_name'],cb[methd]['obj_name'],introspect=False)
Norman Jamese38859c2015-10-29 06:19:40 -0500106 method = obj.get_dbus_method(methd,cb[methd]['interface_name'])
107 method()
Norman Jamesa3e47c42015-10-18 14:43:10 -0500108 except:
109 pass
Norman James471ab592015-08-30 22:29:40 -0500110
Norman James98e1f7b2015-11-24 22:17:56 -0600111 self.Set(DBUS_NAME,"current_state",state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500112
113 def gotoNextState(self):
114 s = 0
Norman James98e1f7b2015-11-24 22:17:56 -0600115 current_state = self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500116 for i in range(len(System.SYSTEM_STATES)):
Norman James98e1f7b2015-11-24 22:17:56 -0600117 if (System.SYSTEM_STATES[i] == current_state):
Norman James7aeefa72015-10-19 11:13:25 -0500118 s = i+1
119
120 if (s == len(System.SYSTEM_STATES)):
121 print "ERROR SystemManager: No more system states"
122 else:
123 new_state_name = System.SYSTEM_STATES[s]
124 print "SystemManager Goto System State: "+new_state_name
125 self.SystemStateHandler(new_state_name)
126
127
Norman James19e45912015-10-04 20:19:41 -0500128 @dbus.service.method(DBUS_NAME,
Norman James7aeefa72015-10-19 11:13:25 -0500129 in_signature='', out_signature='s')
130 def getSystemState(self):
Norman James98e1f7b2015-11-24 22:17:56 -0600131 return self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500132
133 def doObjectLookup(self,category,key):
Norman James19e45912015-10-04 20:19:41 -0500134 bus_name = ""
135 obj_path = ""
Norman James66c02692015-10-15 10:23:12 -0500136 intf_name = INTF_ITEM
137 try:
138 obj_path = System.ID_LOOKUP[category][key]
Norman James19e45912015-10-04 20:19:41 -0500139 bus_name = self.bus_name_lookup[obj_path]
Norman James66c02692015-10-15 10:23:12 -0500140 parts = obj_path.split('/')
Norman James323ed972015-12-09 09:06:37 -0600141 if (parts[3] == 'sensors'):
Norman James66c02692015-10-15 10:23:12 -0500142 intf_name = INTF_SENSOR
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500143 except Exception as e:
144 print "ERROR SystemManager: "+str(e)+" not found in lookup"
145
Norman James66c02692015-10-15 10:23:12 -0500146 return [bus_name,obj_path,intf_name]
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500147
Norman James7aeefa72015-10-19 11:13:25 -0500148 @dbus.service.method(DBUS_NAME,
149 in_signature='ss', out_signature='(sss)')
150 def getObjectFromId(self,category,key):
151 return self.doObjectLookup(category,key)
152
153 @dbus.service.method(DBUS_NAME,
154 in_signature='sy', out_signature='(sss)')
155 def getObjectFromByteId(self,category,key):
156 byte = int(key)
157 return self.doObjectLookup(category,byte)
Yi Li5cf1e112016-04-15 15:30:30 +0800158
159 # 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,
164 in_signature='y', out_signature='s')
165 def getFRUArea(self,fru_id):
166 ret_str = ''
167 fru_id = '_' + str(fru_id)
168 area_list = [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]
174
Norman Jamescf74f952015-10-28 12:45:18 -0500175 def start_process(self,name):
176 if (System.APPS[name]['start_process'] == True):
177 app = System.APPS[name]
178 process_name = self.bin_path+"/"+app['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500179 cmdline = [ ]
180 cmdline.append(process_name)
Norman Jamescf74f952015-10-28 12:45:18 -0500181 if (app.has_key('args')):
182 for a in app['args']:
183 cmdline.append(a)
Norman James5d78b4d2015-09-05 13:34:34 -0500184 try:
Norman Jamescf74f952015-10-28 12:45:18 -0500185 print "Starting process: "+" ".join(cmdline)+": "+name
Norman Jamesbfa561e2015-11-01 07:38:14 -0600186 if (app['monitor_process'] == True):
187 app['popen'] = subprocess.Popen(cmdline)
188 else:
189 subprocess.Popen(cmdline)
190
Norman James5d78b4d2015-09-05 13:34:34 -0500191 except Exception as e:
192 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500193 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500194
Norman James89de9162015-08-27 21:41:36 -0500195 def heartbeat_check(self):
Norman Jamescf74f952015-10-28 12:45:18 -0500196 for name in System.APPS.keys():
197 app = System.APPS[name]
Norman Jamesbfa561e2015-11-01 07:38:14 -0600198 if (app['start_process'] == True and app.has_key('popen')):
Norman James5d78b4d2015-09-05 13:34:34 -0500199 ## make sure process is still alive
Norman Jamescf74f952015-10-28 12:45:18 -0500200 p = app['popen']
Norman James5d78b4d2015-09-05 13:34:34 -0500201 p.poll()
202 if (p.returncode != None):
Norman Jamescf74f952015-10-28 12:45:18 -0500203 print "Process for "+name+" appears to be dead"
204 self.start_process(name)
Norman James5d78b4d2015-09-05 13:34:34 -0500205
Norman James471ab592015-08-30 22:29:40 -0500206 return True
Norman James89de9162015-08-27 21:41:36 -0500207
Brad Bishopfa736492016-06-29 22:21:49 -0400208 def bus_handler(self, owned_name, old, new):
209 if obmc.dbuslib.bindings.is_unique(owned_name) or not new:
210 return
211
212 if owned_name == DBUS_NAME:
213 return
214
215 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
Brad Bishop58e694d2016-04-13 16:04:32 -0400231 def NewObjectHandler(self, obj_path, iprops, bus_name = None):
Norman James98e1f7b2015-11-24 22:17:56 -0600232 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamescfc2b442015-10-31 17:31:46 -0500233 if (self.bus_name_lookup.has_key(obj_path)):
234 if (self.bus_name_lookup[obj_path] == bus_name):
235 return
236 self.bus_name_lookup[obj_path] = bus_name
Brad Bishop4de42642016-06-29 21:55:47 -0400237 if current_state not in System.EXIT_STATE_DEPEND:
238 return
Norman Jamesa3e47c42015-10-18 14:43:10 -0500239
Brad Bishop4de42642016-06-29 21:55:47 -0400240 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()
Norman James89de9162015-08-27 21:41:36 -0500246
247 @dbus.service.method(DBUS_NAME,
248 in_signature='s', out_signature='sis')
249 def gpioInit(self,name):
250 gpio_path = ''
Norman Jamescf74f952015-10-28 12:45:18 -0500251 gpio_num = -1
252 r = ['',gpio_num,'']
Norman James471ab592015-08-30 22:29:40 -0500253 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500254 # TODO: Error handling
255 print "ERROR: "+name+" not found in GPIO config table"
Norman James89de9162015-08-27 21:41:36 -0500256 else:
Norman Jamescf74f952015-10-28 12:45:18 -0500257
258 gpio_num = -1
259 gpio = System.GPIO_CONFIG[name]
260 if (System.GPIO_CONFIG[name].has_key('gpio_num')):
261 gpio_num = gpio['gpio_num']
262 else:
263 if (System.GPIO_CONFIG[name].has_key('gpio_pin')):
264 gpio_num = System.convertGpio(gpio['gpio_pin'])
265 else:
266 print "ERROR: SystemManager - GPIO lookup failed for "+name
267
268 if (gpio_num != -1):
Brad Bishop84e73b52016-05-12 15:57:52 -0400269 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
Norman Jamescf74f952015-10-28 12:45:18 -0500270 return r
Norman James5236a8f2015-11-05 20:39:31 -0600271
Norman James88872672015-09-21 16:51:35 -0500272
Norman James89de9162015-08-27 21:41:36 -0500273
274if __name__ == '__main__':
275 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400276 bus = get_dbus()
Norman James89de9162015-08-27 21:41:36 -0500277 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500278 mainloop = gobject.MainLoop()
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