blob: 754700b305df61b35e6669d5f590b9054b151a87 [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
Norman James9e6acf92015-09-08 07:00:04 -050014
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])
Norman James89de9162015-08-27 21:41:36 -050020
21DBUS_NAME = 'org.openbmc.managers.System'
22OBJ_NAME = '/org/openbmc/managers/System'
Norman James471ab592015-08-30 22:29:40 -050023HEARTBEAT_CHECK_INTERVAL = 20000
Norman James9a60a7b2015-10-06 16:53:23 -050024STATE_START_TIMEOUT = 10
Norman James66c02692015-10-15 10:23:12 -050025INTF_SENSOR = 'org.openbmc.SensorValue'
26INTF_ITEM = 'org.openbmc.InventoryItem'
Norman Jamesa3e47c42015-10-18 14:43:10 -050027INTF_CONTROL = 'org.openbmc.Control'
Norman James89de9162015-08-27 21:41:36 -050028
Norman Jamescf74f952015-10-28 12:45:18 -050029
Brad Bishop84e73b52016-05-12 15:57:52 -040030class SystemManager(DbusProperties,DbusObjectManager):
Norman James98e1f7b2015-11-24 22:17:56 -060031 def __init__(self,bus,obj_name):
Brad Bishop84e73b52016-05-12 15:57:52 -040032 DbusProperties.__init__(self)
33 DbusObjectManager.__init__(self)
Norman James98e1f7b2015-11-24 22:17:56 -060034 dbus.service.Object.__init__(self,bus,obj_name)
Norman James9e6acf92015-09-08 07:00:04 -050035
Norman Jamescfc2b442015-10-31 17:31:46 -050036 bus.add_signal_receiver(self.NewObjectHandler,
Brad Bishop58e694d2016-04-13 16:04:32 -040037 signal_name = "InterfacesAdded", sender_keyword = 'bus_name')
Norman James362a80f2015-09-14 14:04:39 -050038 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050039
Norman James98e1f7b2015-11-24 22:17:56 -060040 self.Set(DBUS_NAME,"current_state","")
Norman James362a80f2015-09-14 14:04:39 -050041 self.system_states = {}
Norman James19e45912015-10-04 20:19:41 -050042 self.bus_name_lookup = {}
Norman Jamesc36372d2015-10-08 07:03:07 -050043 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
44
Norman Jamescf74f952015-10-28 12:45:18 -050045 for name in System.APPS.keys():
46 sys_state = System.APPS[name]['system_state']
Norman James362a80f2015-09-14 14:04:39 -050047 if (self.system_states.has_key(sys_state) == False):
48 self.system_states[sys_state] = []
Norman Jamescf74f952015-10-28 12:45:18 -050049 self.system_states[sys_state].append(name)
Norman James19e45912015-10-04 20:19:41 -050050
51 ## replace symbolic path in ID_LOOKUP
52 for category in System.ID_LOOKUP:
53 for key in System.ID_LOOKUP[category]:
54 val = System.ID_LOOKUP[category][key]
55 new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT)
56 System.ID_LOOKUP[category][key] = new_val
57
Norman James7aeefa72015-10-19 11:13:25 -050058 self.SystemStateHandler(System.SYSTEM_STATES[0])
Norman Jamescfc2b442015-10-31 17:31:46 -050059
60 if not os.path.exists(PropertyCacher.CACHE_PATH):
61 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
62 os.makedirs(PropertyCacher.CACHE_PATH)
63
Norman James323ed972015-12-09 09:06:37 -060064 self.InterfacesAdded(obj_name,self.properties)
Norman James362a80f2015-09-14 14:04:39 -050065 print "SystemManager Init Done"
66
Norman James7aeefa72015-10-19 11:13:25 -050067
Norman James362a80f2015-09-14 14:04:39 -050068 def SystemStateHandler(self,state_name):
Norman Jamesa3e47c42015-10-18 14:43:10 -050069 ## clearing object started flags
Norman James98e1f7b2015-11-24 22:17:56 -060070 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamesa3e47c42015-10-18 14:43:10 -050071 try:
Norman James98e1f7b2015-11-24 22:17:56 -060072 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
73 System.EXIT_STATE_DEPEND[current_state][obj_path] = 0
Norman Jamesa3e47c42015-10-18 14:43:10 -050074 except:
75 pass
Norman James65a295a2015-09-26 22:21:10 -050076
Norman James362a80f2015-09-14 14:04:39 -050077 print "Running System State: "+state_name
78 if (self.system_states.has_key(state_name)):
Norman Jamescf74f952015-10-28 12:45:18 -050079 for name in self.system_states[state_name]:
80 self.start_process(name)
Norman James90baede2015-09-02 20:32:49 -050081
Norman Jamesa3e47c42015-10-18 14:43:10 -050082 if (state_name == "BMC_INIT"):
Norman James362a80f2015-09-14 14:04:39 -050083 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -050084 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -050085
Norman Jamesa3e47c42015-10-18 14:43:10 -050086 try:
Norman James362a80f2015-09-14 14:04:39 -050087 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman Jamese38859c2015-10-29 06:19:40 -050088 for methd in cb.keys():
Norman James85f050b2015-12-18 14:58:20 -060089 obj = bus.get_object(cb[methd]['bus_name'],cb[methd]['obj_name'],introspect=False)
Norman Jamese38859c2015-10-29 06:19:40 -050090 method = obj.get_dbus_method(methd,cb[methd]['interface_name'])
91 method()
Norman Jamesa3e47c42015-10-18 14:43:10 -050092 except:
93 pass
Norman James471ab592015-08-30 22:29:40 -050094
Norman James98e1f7b2015-11-24 22:17:56 -060095 self.Set(DBUS_NAME,"current_state",state_name)
Norman James7aeefa72015-10-19 11:13:25 -050096
97 def gotoNextState(self):
98 s = 0
Norman James98e1f7b2015-11-24 22:17:56 -060099 current_state = self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500100 for i in range(len(System.SYSTEM_STATES)):
Norman James98e1f7b2015-11-24 22:17:56 -0600101 if (System.SYSTEM_STATES[i] == current_state):
Norman James7aeefa72015-10-19 11:13:25 -0500102 s = i+1
103
104 if (s == len(System.SYSTEM_STATES)):
105 print "ERROR SystemManager: No more system states"
106 else:
107 new_state_name = System.SYSTEM_STATES[s]
108 print "SystemManager Goto System State: "+new_state_name
109 self.SystemStateHandler(new_state_name)
110
111
Norman James19e45912015-10-04 20:19:41 -0500112 @dbus.service.method(DBUS_NAME,
Norman James7aeefa72015-10-19 11:13:25 -0500113 in_signature='', out_signature='s')
114 def getSystemState(self):
Norman James98e1f7b2015-11-24 22:17:56 -0600115 return self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500116
117 def doObjectLookup(self,category,key):
Norman James19e45912015-10-04 20:19:41 -0500118 bus_name = ""
119 obj_path = ""
Norman James66c02692015-10-15 10:23:12 -0500120 intf_name = INTF_ITEM
121 try:
122 obj_path = System.ID_LOOKUP[category][key]
Norman James19e45912015-10-04 20:19:41 -0500123 bus_name = self.bus_name_lookup[obj_path]
Norman James66c02692015-10-15 10:23:12 -0500124 parts = obj_path.split('/')
Norman James323ed972015-12-09 09:06:37 -0600125 if (parts[3] == 'sensors'):
Norman James66c02692015-10-15 10:23:12 -0500126 intf_name = INTF_SENSOR
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500127 except Exception as e:
128 print "ERROR SystemManager: "+str(e)+" not found in lookup"
129
Norman James66c02692015-10-15 10:23:12 -0500130 return [bus_name,obj_path,intf_name]
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500131
Norman James7aeefa72015-10-19 11:13:25 -0500132 @dbus.service.method(DBUS_NAME,
133 in_signature='ss', out_signature='(sss)')
134 def getObjectFromId(self,category,key):
135 return self.doObjectLookup(category,key)
136
137 @dbus.service.method(DBUS_NAME,
138 in_signature='sy', out_signature='(sss)')
139 def getObjectFromByteId(self,category,key):
140 byte = int(key)
141 return self.doObjectLookup(category,byte)
Yi Li5cf1e112016-04-15 15:30:30 +0800142
143 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
144 # If serval areas are defined for a fru_id, the areas are returned
145 # together as a string with each area name seperated with ','.
146 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
147 @dbus.service.method(DBUS_NAME,
148 in_signature='y', out_signature='s')
149 def getFRUArea(self,fru_id):
150 ret_str = ''
151 fru_id = '_' + str(fru_id)
152 area_list = [area for area in System.ID_LOOKUP['FRU_STR'].keys() \
153 if area.endswith(fru_id)]
154 for area in area_list:
155 ret_str = area + ',' + ret_str
156 # remove the last ','
157 return ret_str[:-1]
158
Norman Jamescf74f952015-10-28 12:45:18 -0500159 def start_process(self,name):
160 if (System.APPS[name]['start_process'] == True):
161 app = System.APPS[name]
162 process_name = self.bin_path+"/"+app['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500163 cmdline = [ ]
164 cmdline.append(process_name)
Norman Jamescf74f952015-10-28 12:45:18 -0500165 if (app.has_key('args')):
166 for a in app['args']:
167 cmdline.append(a)
Norman James5d78b4d2015-09-05 13:34:34 -0500168 try:
Norman Jamescf74f952015-10-28 12:45:18 -0500169 print "Starting process: "+" ".join(cmdline)+": "+name
Norman Jamesbfa561e2015-11-01 07:38:14 -0600170 if (app['monitor_process'] == True):
171 app['popen'] = subprocess.Popen(cmdline)
172 else:
173 subprocess.Popen(cmdline)
174
Norman James5d78b4d2015-09-05 13:34:34 -0500175 except Exception as e:
176 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500177 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500178
Norman James89de9162015-08-27 21:41:36 -0500179 def heartbeat_check(self):
Norman Jamescf74f952015-10-28 12:45:18 -0500180 for name in System.APPS.keys():
181 app = System.APPS[name]
Norman Jamesbfa561e2015-11-01 07:38:14 -0600182 if (app['start_process'] == True and app.has_key('popen')):
Norman James5d78b4d2015-09-05 13:34:34 -0500183 ## make sure process is still alive
Norman Jamescf74f952015-10-28 12:45:18 -0500184 p = app['popen']
Norman James5d78b4d2015-09-05 13:34:34 -0500185 p.poll()
186 if (p.returncode != None):
Norman Jamescf74f952015-10-28 12:45:18 -0500187 print "Process for "+name+" appears to be dead"
188 self.start_process(name)
Norman James5d78b4d2015-09-05 13:34:34 -0500189
Norman James471ab592015-08-30 22:29:40 -0500190 return True
Norman James89de9162015-08-27 21:41:36 -0500191
Brad Bishop58e694d2016-04-13 16:04:32 -0400192 def NewObjectHandler(self, obj_path, iprops, bus_name = None):
Norman James98e1f7b2015-11-24 22:17:56 -0600193 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamescfc2b442015-10-31 17:31:46 -0500194 if (self.bus_name_lookup.has_key(obj_path)):
195 if (self.bus_name_lookup[obj_path] == bus_name):
196 return
197 self.bus_name_lookup[obj_path] = bus_name
198 print "New object: "+obj_path+" ("+bus_name+")"
199 try:
Norman James98e1f7b2015-11-24 22:17:56 -0600200 if (System.EXIT_STATE_DEPEND[current_state].has_key(obj_path) == True):
201 System.EXIT_STATE_DEPEND[current_state][obj_path] = 1
Norman Jamescf74f952015-10-28 12:45:18 -0500202 ## check if all required objects are started to move to next state
Norman Jamescfc2b442015-10-31 17:31:46 -0500203 state = 1
Norman James98e1f7b2015-11-24 22:17:56 -0600204 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
205 if (System.EXIT_STATE_DEPEND[current_state][obj_path] == 0):
Norman Jamescfc2b442015-10-31 17:31:46 -0500206 state = 0
207 ## all required objects have started so go to next state
208 if (state == 1):
Norman James98e1f7b2015-11-24 22:17:56 -0600209 print "All required objects started for "+current_state
Norman Jamescfc2b442015-10-31 17:31:46 -0500210 self.gotoNextState()
211 except:
212 pass
Norman Jamesa3e47c42015-10-18 14:43:10 -0500213
Norman James89de9162015-08-27 21:41:36 -0500214
215 @dbus.service.method(DBUS_NAME,
216 in_signature='s', out_signature='sis')
217 def gpioInit(self,name):
218 gpio_path = ''
Norman Jamescf74f952015-10-28 12:45:18 -0500219 gpio_num = -1
220 r = ['',gpio_num,'']
Norman James471ab592015-08-30 22:29:40 -0500221 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500222 # TODO: Error handling
223 print "ERROR: "+name+" not found in GPIO config table"
Norman James89de9162015-08-27 21:41:36 -0500224 else:
Norman Jamescf74f952015-10-28 12:45:18 -0500225
226 gpio_num = -1
227 gpio = System.GPIO_CONFIG[name]
228 if (System.GPIO_CONFIG[name].has_key('gpio_num')):
229 gpio_num = gpio['gpio_num']
230 else:
231 if (System.GPIO_CONFIG[name].has_key('gpio_pin')):
232 gpio_num = System.convertGpio(gpio['gpio_pin'])
233 else:
234 print "ERROR: SystemManager - GPIO lookup failed for "+name
235
236 if (gpio_num != -1):
Brad Bishop84e73b52016-05-12 15:57:52 -0400237 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
Norman Jamescf74f952015-10-28 12:45:18 -0500238 return r
Norman James5236a8f2015-11-05 20:39:31 -0600239
Norman James88872672015-09-21 16:51:35 -0500240
Norman James89de9162015-08-27 21:41:36 -0500241
242if __name__ == '__main__':
243 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400244 bus = get_dbus()
Norman James89de9162015-08-27 21:41:36 -0500245 name = dbus.service.BusName(DBUS_NAME,bus)
246 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500247 mainloop = gobject.MainLoop()
Norman James89de9162015-08-27 21:41:36 -0500248
Norman James89de9162015-08-27 21:41:36 -0500249 print "Running SystemManager"
250 mainloop.run()
251