blob: 8bcb239e9ce7e308dfcb9bd94f5b28f612b7cdd1 [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
Norman James89de9162015-08-27 21:41:36 -050015
16DBUS_NAME = 'org.openbmc.managers.System'
17OBJ_NAME = '/org/openbmc/managers/System'
Norman James471ab592015-08-30 22:29:40 -050018HEARTBEAT_CHECK_INTERVAL = 20000
Norman James9a60a7b2015-10-06 16:53:23 -050019STATE_START_TIMEOUT = 10
Norman James66c02692015-10-15 10:23:12 -050020INTF_SENSOR = 'org.openbmc.SensorValue'
21INTF_ITEM = 'org.openbmc.InventoryItem'
Norman Jamesa3e47c42015-10-18 14:43:10 -050022INTF_CONTROL = 'org.openbmc.Control'
Norman James89de9162015-08-27 21:41:36 -050023
Norman Jamescf74f952015-10-28 12:45:18 -050024
Brad Bishop84e73b52016-05-12 15:57:52 -040025class SystemManager(DbusProperties,DbusObjectManager):
Norman James98e1f7b2015-11-24 22:17:56 -060026 def __init__(self,bus,obj_name):
Brad Bishop84e73b52016-05-12 15:57:52 -040027 DbusProperties.__init__(self)
28 DbusObjectManager.__init__(self)
Norman James98e1f7b2015-11-24 22:17:56 -060029 dbus.service.Object.__init__(self,bus,obj_name)
Norman James9e6acf92015-09-08 07:00:04 -050030
Norman Jamescfc2b442015-10-31 17:31:46 -050031 bus.add_signal_receiver(self.NewObjectHandler,
Brad Bishop58e694d2016-04-13 16:04:32 -040032 signal_name = "InterfacesAdded", sender_keyword = 'bus_name')
Norman James362a80f2015-09-14 14:04:39 -050033 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050034
Norman James98e1f7b2015-11-24 22:17:56 -060035 self.Set(DBUS_NAME,"current_state","")
Norman James362a80f2015-09-14 14:04:39 -050036 self.system_states = {}
Norman James19e45912015-10-04 20:19:41 -050037 self.bus_name_lookup = {}
Norman Jamesc36372d2015-10-08 07:03:07 -050038 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
39
Norman Jamescf74f952015-10-28 12:45:18 -050040 for name in System.APPS.keys():
41 sys_state = System.APPS[name]['system_state']
Norman James362a80f2015-09-14 14:04:39 -050042 if (self.system_states.has_key(sys_state) == False):
43 self.system_states[sys_state] = []
Norman Jamescf74f952015-10-28 12:45:18 -050044 self.system_states[sys_state].append(name)
Norman James19e45912015-10-04 20:19:41 -050045
46 ## replace symbolic path in ID_LOOKUP
47 for category in System.ID_LOOKUP:
48 for key in System.ID_LOOKUP[category]:
49 val = System.ID_LOOKUP[category][key]
50 new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT)
51 System.ID_LOOKUP[category][key] = new_val
52
Norman James7aeefa72015-10-19 11:13:25 -050053 self.SystemStateHandler(System.SYSTEM_STATES[0])
Norman Jamescfc2b442015-10-31 17:31:46 -050054
55 if not os.path.exists(PropertyCacher.CACHE_PATH):
56 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
57 os.makedirs(PropertyCacher.CACHE_PATH)
58
Norman James323ed972015-12-09 09:06:37 -060059 self.InterfacesAdded(obj_name,self.properties)
Norman James362a80f2015-09-14 14:04:39 -050060 print "SystemManager Init Done"
61
Norman James7aeefa72015-10-19 11:13:25 -050062
Brad Bishop4de42642016-06-29 21:55:47 -040063 def try_next_state(self):
64 current_state = self.Get(DBUS_NAME,"current_state")
65 if current_state not in System.EXIT_STATE_DEPEND:
66 return
67
68 if all(System.EXIT_STATE_DEPEND[current_state].values()):
69 print "All required objects started for "+current_state
70 self.gotoNextState()
71
72
Norman James362a80f2015-09-14 14:04:39 -050073 def SystemStateHandler(self,state_name):
Norman Jamesa3e47c42015-10-18 14:43:10 -050074 ## clearing object started flags
Norman James98e1f7b2015-11-24 22:17:56 -060075 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamesa3e47c42015-10-18 14:43:10 -050076 try:
Norman James98e1f7b2015-11-24 22:17:56 -060077 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
78 System.EXIT_STATE_DEPEND[current_state][obj_path] = 0
Norman Jamesa3e47c42015-10-18 14:43:10 -050079 except:
80 pass
Norman James65a295a2015-09-26 22:21:10 -050081
Norman James362a80f2015-09-14 14:04:39 -050082 print "Running System State: "+state_name
83 if (self.system_states.has_key(state_name)):
Norman Jamescf74f952015-10-28 12:45:18 -050084 for name in self.system_states[state_name]:
85 self.start_process(name)
Norman James90baede2015-09-02 20:32:49 -050086
Norman Jamesa3e47c42015-10-18 14:43:10 -050087 if (state_name == "BMC_INIT"):
Norman James362a80f2015-09-14 14:04:39 -050088 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -050089 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -050090
Norman Jamesa3e47c42015-10-18 14:43:10 -050091 try:
Norman James362a80f2015-09-14 14:04:39 -050092 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman Jamese38859c2015-10-29 06:19:40 -050093 for methd in cb.keys():
Norman James85f050b2015-12-18 14:58:20 -060094 obj = bus.get_object(cb[methd]['bus_name'],cb[methd]['obj_name'],introspect=False)
Norman Jamese38859c2015-10-29 06:19:40 -050095 method = obj.get_dbus_method(methd,cb[methd]['interface_name'])
96 method()
Norman Jamesa3e47c42015-10-18 14:43:10 -050097 except:
98 pass
Norman James471ab592015-08-30 22:29:40 -050099
Norman James98e1f7b2015-11-24 22:17:56 -0600100 self.Set(DBUS_NAME,"current_state",state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500101
102 def gotoNextState(self):
103 s = 0
Norman James98e1f7b2015-11-24 22:17:56 -0600104 current_state = self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500105 for i in range(len(System.SYSTEM_STATES)):
Norman James98e1f7b2015-11-24 22:17:56 -0600106 if (System.SYSTEM_STATES[i] == current_state):
Norman James7aeefa72015-10-19 11:13:25 -0500107 s = i+1
108
109 if (s == len(System.SYSTEM_STATES)):
110 print "ERROR SystemManager: No more system states"
111 else:
112 new_state_name = System.SYSTEM_STATES[s]
113 print "SystemManager Goto System State: "+new_state_name
114 self.SystemStateHandler(new_state_name)
115
116
Norman James19e45912015-10-04 20:19:41 -0500117 @dbus.service.method(DBUS_NAME,
Norman James7aeefa72015-10-19 11:13:25 -0500118 in_signature='', out_signature='s')
119 def getSystemState(self):
Norman James98e1f7b2015-11-24 22:17:56 -0600120 return self.Get(DBUS_NAME,"current_state")
Norman James7aeefa72015-10-19 11:13:25 -0500121
122 def doObjectLookup(self,category,key):
Norman James19e45912015-10-04 20:19:41 -0500123 bus_name = ""
124 obj_path = ""
Norman James66c02692015-10-15 10:23:12 -0500125 intf_name = INTF_ITEM
126 try:
127 obj_path = System.ID_LOOKUP[category][key]
Norman James19e45912015-10-04 20:19:41 -0500128 bus_name = self.bus_name_lookup[obj_path]
Norman James66c02692015-10-15 10:23:12 -0500129 parts = obj_path.split('/')
Norman James323ed972015-12-09 09:06:37 -0600130 if (parts[3] == 'sensors'):
Norman James66c02692015-10-15 10:23:12 -0500131 intf_name = INTF_SENSOR
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500132 except Exception as e:
133 print "ERROR SystemManager: "+str(e)+" not found in lookup"
134
Norman James66c02692015-10-15 10:23:12 -0500135 return [bus_name,obj_path,intf_name]
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500136
Norman James7aeefa72015-10-19 11:13:25 -0500137 @dbus.service.method(DBUS_NAME,
138 in_signature='ss', out_signature='(sss)')
139 def getObjectFromId(self,category,key):
140 return self.doObjectLookup(category,key)
141
142 @dbus.service.method(DBUS_NAME,
143 in_signature='sy', out_signature='(sss)')
144 def getObjectFromByteId(self,category,key):
145 byte = int(key)
146 return self.doObjectLookup(category,byte)
Yi Li5cf1e112016-04-15 15:30:30 +0800147
148 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
149 # If serval areas are defined for a fru_id, the areas are returned
150 # together as a string with each area name seperated with ','.
151 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
152 @dbus.service.method(DBUS_NAME,
153 in_signature='y', out_signature='s')
154 def getFRUArea(self,fru_id):
155 ret_str = ''
156 fru_id = '_' + str(fru_id)
157 area_list = [area for area in System.ID_LOOKUP['FRU_STR'].keys() \
158 if area.endswith(fru_id)]
159 for area in area_list:
160 ret_str = area + ',' + ret_str
161 # remove the last ','
162 return ret_str[:-1]
163
Norman Jamescf74f952015-10-28 12:45:18 -0500164 def start_process(self,name):
165 if (System.APPS[name]['start_process'] == True):
166 app = System.APPS[name]
167 process_name = self.bin_path+"/"+app['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500168 cmdline = [ ]
169 cmdline.append(process_name)
Norman Jamescf74f952015-10-28 12:45:18 -0500170 if (app.has_key('args')):
171 for a in app['args']:
172 cmdline.append(a)
Norman James5d78b4d2015-09-05 13:34:34 -0500173 try:
Norman Jamescf74f952015-10-28 12:45:18 -0500174 print "Starting process: "+" ".join(cmdline)+": "+name
Norman Jamesbfa561e2015-11-01 07:38:14 -0600175 if (app['monitor_process'] == True):
176 app['popen'] = subprocess.Popen(cmdline)
177 else:
178 subprocess.Popen(cmdline)
179
Norman James5d78b4d2015-09-05 13:34:34 -0500180 except Exception as e:
181 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500182 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500183
Norman James89de9162015-08-27 21:41:36 -0500184 def heartbeat_check(self):
Norman Jamescf74f952015-10-28 12:45:18 -0500185 for name in System.APPS.keys():
186 app = System.APPS[name]
Norman Jamesbfa561e2015-11-01 07:38:14 -0600187 if (app['start_process'] == True and app.has_key('popen')):
Norman James5d78b4d2015-09-05 13:34:34 -0500188 ## make sure process is still alive
Norman Jamescf74f952015-10-28 12:45:18 -0500189 p = app['popen']
Norman James5d78b4d2015-09-05 13:34:34 -0500190 p.poll()
191 if (p.returncode != None):
Norman Jamescf74f952015-10-28 12:45:18 -0500192 print "Process for "+name+" appears to be dead"
193 self.start_process(name)
Norman James5d78b4d2015-09-05 13:34:34 -0500194
Norman James471ab592015-08-30 22:29:40 -0500195 return True
Norman James89de9162015-08-27 21:41:36 -0500196
Brad Bishop58e694d2016-04-13 16:04:32 -0400197 def NewObjectHandler(self, obj_path, iprops, bus_name = None):
Norman James98e1f7b2015-11-24 22:17:56 -0600198 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamescfc2b442015-10-31 17:31:46 -0500199 if (self.bus_name_lookup.has_key(obj_path)):
200 if (self.bus_name_lookup[obj_path] == bus_name):
201 return
202 self.bus_name_lookup[obj_path] = bus_name
Brad Bishop4de42642016-06-29 21:55:47 -0400203 if current_state not in System.EXIT_STATE_DEPEND:
204 return
Norman Jamesa3e47c42015-10-18 14:43:10 -0500205
Brad Bishop4de42642016-06-29 21:55:47 -0400206 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
207 print "New object: "+obj_path+" ("+bus_name+")"
208 System.EXIT_STATE_DEPEND[current_state][obj_path] = 1
209 ## check if all required objects are
210 # started to move to next state
211 self.try_next_state()
Norman James89de9162015-08-27 21:41:36 -0500212
213 @dbus.service.method(DBUS_NAME,
214 in_signature='s', out_signature='sis')
215 def gpioInit(self,name):
216 gpio_path = ''
Norman Jamescf74f952015-10-28 12:45:18 -0500217 gpio_num = -1
218 r = ['',gpio_num,'']
Norman James471ab592015-08-30 22:29:40 -0500219 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500220 # TODO: Error handling
221 print "ERROR: "+name+" not found in GPIO config table"
Norman James89de9162015-08-27 21:41:36 -0500222 else:
Norman Jamescf74f952015-10-28 12:45:18 -0500223
224 gpio_num = -1
225 gpio = System.GPIO_CONFIG[name]
226 if (System.GPIO_CONFIG[name].has_key('gpio_num')):
227 gpio_num = gpio['gpio_num']
228 else:
229 if (System.GPIO_CONFIG[name].has_key('gpio_pin')):
230 gpio_num = System.convertGpio(gpio['gpio_pin'])
231 else:
232 print "ERROR: SystemManager - GPIO lookup failed for "+name
233
234 if (gpio_num != -1):
Brad Bishop84e73b52016-05-12 15:57:52 -0400235 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
Norman Jamescf74f952015-10-28 12:45:18 -0500236 return r
Norman James5236a8f2015-11-05 20:39:31 -0600237
Norman James88872672015-09-21 16:51:35 -0500238
Norman James89de9162015-08-27 21:41:36 -0500239
240if __name__ == '__main__':
241 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400242 bus = get_dbus()
Norman James89de9162015-08-27 21:41:36 -0500243 name = dbus.service.BusName(DBUS_NAME,bus)
244 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500245 mainloop = gobject.MainLoop()
Norman James89de9162015-08-27 21:41:36 -0500246
Norman James89de9162015-08-27 21:41:36 -0500247 print "Running SystemManager"
248 mainloop.run()
249