blob: 004d978c4fc70a374d371f87e998c021523d97c9 [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
Norman Jamescfc2b442015-10-31 17:31:46 -050011import PropertyCacher
Norman James9e6acf92015-09-08 07:00:04 -050012import Openbmc
13
Norman James471ab592015-08-30 22:29:40 -050014if (len(sys.argv) < 2):
15 print "Usage: system_manager.py [system name]"
16 exit(1)
17
18System = __import__(sys.argv[1])
19import Openbmc
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
Norman James323ed972015-12-09 09:06:37 -060030class SystemManager(Openbmc.DbusProperties,Openbmc.DbusObjectManager):
Norman James98e1f7b2015-11-24 22:17:56 -060031 def __init__(self,bus,obj_name):
32 Openbmc.DbusProperties.__init__(self)
Norman James323ed972015-12-09 09:06:37 -060033 Openbmc.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,
37 signal_name = "ObjectAdded", 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)
Norman James19e45912015-10-04 20:19:41 -0500142
Norman Jamescf74f952015-10-28 12:45:18 -0500143 def start_process(self,name):
144 if (System.APPS[name]['start_process'] == True):
145 app = System.APPS[name]
146 process_name = self.bin_path+"/"+app['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500147 cmdline = [ ]
148 cmdline.append(process_name)
Norman Jamescf74f952015-10-28 12:45:18 -0500149 if (app.has_key('args')):
150 for a in app['args']:
151 cmdline.append(a)
Norman James5d78b4d2015-09-05 13:34:34 -0500152 try:
Norman Jamescf74f952015-10-28 12:45:18 -0500153 print "Starting process: "+" ".join(cmdline)+": "+name
Norman Jamesbfa561e2015-11-01 07:38:14 -0600154 if (app['monitor_process'] == True):
155 app['popen'] = subprocess.Popen(cmdline)
156 else:
157 subprocess.Popen(cmdline)
158
Norman James5d78b4d2015-09-05 13:34:34 -0500159 except Exception as e:
160 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500161 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500162
Norman James89de9162015-08-27 21:41:36 -0500163 def heartbeat_check(self):
Norman Jamescf74f952015-10-28 12:45:18 -0500164 for name in System.APPS.keys():
165 app = System.APPS[name]
Norman Jamesbfa561e2015-11-01 07:38:14 -0600166 if (app['start_process'] == True and app.has_key('popen')):
Norman James5d78b4d2015-09-05 13:34:34 -0500167 ## make sure process is still alive
Norman Jamescf74f952015-10-28 12:45:18 -0500168 p = app['popen']
Norman James5d78b4d2015-09-05 13:34:34 -0500169 p.poll()
170 if (p.returncode != None):
Norman Jamescf74f952015-10-28 12:45:18 -0500171 print "Process for "+name+" appears to be dead"
172 self.start_process(name)
Norman James5d78b4d2015-09-05 13:34:34 -0500173
Norman James471ab592015-08-30 22:29:40 -0500174 return True
Norman James89de9162015-08-27 21:41:36 -0500175
Norman Jamescfc2b442015-10-31 17:31:46 -0500176 def NewObjectHandler(self,obj_path, interface_name, bus_name = None):
Norman James98e1f7b2015-11-24 22:17:56 -0600177 current_state = self.Get(DBUS_NAME,"current_state")
Norman Jamescfc2b442015-10-31 17:31:46 -0500178 if (self.bus_name_lookup.has_key(obj_path)):
179 if (self.bus_name_lookup[obj_path] == bus_name):
180 return
181 self.bus_name_lookup[obj_path] = bus_name
182 print "New object: "+obj_path+" ("+bus_name+")"
183 try:
Norman James98e1f7b2015-11-24 22:17:56 -0600184 if (System.EXIT_STATE_DEPEND[current_state].has_key(obj_path) == True):
185 System.EXIT_STATE_DEPEND[current_state][obj_path] = 1
Norman Jamescf74f952015-10-28 12:45:18 -0500186 ## check if all required objects are started to move to next state
Norman Jamescfc2b442015-10-31 17:31:46 -0500187 state = 1
Norman James98e1f7b2015-11-24 22:17:56 -0600188 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
189 if (System.EXIT_STATE_DEPEND[current_state][obj_path] == 0):
Norman Jamescfc2b442015-10-31 17:31:46 -0500190 state = 0
191 ## all required objects have started so go to next state
192 if (state == 1):
Norman James98e1f7b2015-11-24 22:17:56 -0600193 print "All required objects started for "+current_state
Norman Jamescfc2b442015-10-31 17:31:46 -0500194 self.gotoNextState()
195 except:
196 pass
Norman Jamesa3e47c42015-10-18 14:43:10 -0500197
Norman James89de9162015-08-27 21:41:36 -0500198
199 @dbus.service.method(DBUS_NAME,
200 in_signature='s', out_signature='sis')
201 def gpioInit(self,name):
202 gpio_path = ''
Norman Jamescf74f952015-10-28 12:45:18 -0500203 gpio_num = -1
204 r = ['',gpio_num,'']
Norman James471ab592015-08-30 22:29:40 -0500205 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500206 # TODO: Error handling
207 print "ERROR: "+name+" not found in GPIO config table"
Norman James89de9162015-08-27 21:41:36 -0500208 else:
Norman Jamescf74f952015-10-28 12:45:18 -0500209
210 gpio_num = -1
211 gpio = System.GPIO_CONFIG[name]
212 if (System.GPIO_CONFIG[name].has_key('gpio_num')):
213 gpio_num = gpio['gpio_num']
214 else:
215 if (System.GPIO_CONFIG[name].has_key('gpio_pin')):
216 gpio_num = System.convertGpio(gpio['gpio_pin'])
217 else:
218 print "ERROR: SystemManager - GPIO lookup failed for "+name
219
220 if (gpio_num != -1):
221 r = [Openbmc.GPIO_DEV, gpio_num, gpio['direction']]
222 return r
Norman James5236a8f2015-11-05 20:39:31 -0600223
Norman James88872672015-09-21 16:51:35 -0500224
Norman James89de9162015-08-27 21:41:36 -0500225
226if __name__ == '__main__':
227 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -0500228 bus = Openbmc.getDBus()
Norman James89de9162015-08-27 21:41:36 -0500229 name = dbus.service.BusName(DBUS_NAME,bus)
230 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500231 mainloop = gobject.MainLoop()
Norman James89de9162015-08-27 21:41:36 -0500232
Norman James89de9162015-08-27 21:41:36 -0500233 print "Running SystemManager"
234 mainloop.run()
235