blob: 086679f7b3f673566c447d928d4836d423e21b95 [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 James89de9162015-08-27 21:41:36 -050030class SystemManager(dbus.service.Object):
31 def __init__(self,bus,name):
32 dbus.service.Object.__init__(self,bus,name)
Norman James9e6acf92015-09-08 07:00:04 -050033
Norman Jamescfc2b442015-10-31 17:31:46 -050034 bus.add_signal_receiver(self.NewObjectHandler,
35 signal_name = "ObjectAdded", sender_keyword = 'bus_name')
Norman James362a80f2015-09-14 14:04:39 -050036 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050037
Norman James362a80f2015-09-14 14:04:39 -050038 self.current_state = ""
39 self.system_states = {}
Norman James19e45912015-10-04 20:19:41 -050040 self.bus_name_lookup = {}
Norman Jamesc36372d2015-10-08 07:03:07 -050041 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
42
Norman Jamescf74f952015-10-28 12:45:18 -050043 for name in System.APPS.keys():
44 sys_state = System.APPS[name]['system_state']
Norman James362a80f2015-09-14 14:04:39 -050045 if (self.system_states.has_key(sys_state) == False):
46 self.system_states[sys_state] = []
Norman Jamescf74f952015-10-28 12:45:18 -050047 self.system_states[sys_state].append(name)
Norman James19e45912015-10-04 20:19:41 -050048
49 ## replace symbolic path in ID_LOOKUP
50 for category in System.ID_LOOKUP:
51 for key in System.ID_LOOKUP[category]:
52 val = System.ID_LOOKUP[category][key]
53 new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT)
54 System.ID_LOOKUP[category][key] = new_val
55
Norman James7aeefa72015-10-19 11:13:25 -050056 self.SystemStateHandler(System.SYSTEM_STATES[0])
Norman Jamescfc2b442015-10-31 17:31:46 -050057
58 if not os.path.exists(PropertyCacher.CACHE_PATH):
59 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
60 os.makedirs(PropertyCacher.CACHE_PATH)
61
Norman James362a80f2015-09-14 14:04:39 -050062 print "SystemManager Init Done"
63
Norman James7aeefa72015-10-19 11:13:25 -050064
Norman James362a80f2015-09-14 14:04:39 -050065 def SystemStateHandler(self,state_name):
Norman Jamesa3e47c42015-10-18 14:43:10 -050066 ## clearing object started flags
67 try:
68 for obj_path in System.EXIT_STATE_DEPEND[self.current_state]:
69 System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 0
70 except:
71 pass
Norman James65a295a2015-09-26 22:21:10 -050072
Norman James362a80f2015-09-14 14:04:39 -050073 print "Running System State: "+state_name
74 if (self.system_states.has_key(state_name)):
Norman Jamescf74f952015-10-28 12:45:18 -050075 for name in self.system_states[state_name]:
76 self.start_process(name)
Norman James90baede2015-09-02 20:32:49 -050077
Norman Jamesa3e47c42015-10-18 14:43:10 -050078 if (state_name == "BMC_INIT"):
Norman James362a80f2015-09-14 14:04:39 -050079 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -050080 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -050081
Norman Jamesa3e47c42015-10-18 14:43:10 -050082 try:
Norman James362a80f2015-09-14 14:04:39 -050083 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman Jamese38859c2015-10-29 06:19:40 -050084 for methd in cb.keys():
85 obj = bus.get_object(cb[methd]['bus_name'],cb[methd]['obj_name'])
86 method = obj.get_dbus_method(methd,cb[methd]['interface_name'])
87 method()
Norman Jamesa3e47c42015-10-18 14:43:10 -050088 except:
89 pass
Norman James471ab592015-08-30 22:29:40 -050090
Norman James65a295a2015-09-26 22:21:10 -050091 self.current_state = state_name
Norman James7aeefa72015-10-19 11:13:25 -050092
93 def gotoNextState(self):
94 s = 0
95 for i in range(len(System.SYSTEM_STATES)):
96 if (System.SYSTEM_STATES[i] == self.current_state):
97 s = i+1
98
99 if (s == len(System.SYSTEM_STATES)):
100 print "ERROR SystemManager: No more system states"
101 else:
102 new_state_name = System.SYSTEM_STATES[s]
103 print "SystemManager Goto System State: "+new_state_name
104 self.SystemStateHandler(new_state_name)
105
106
Norman James19e45912015-10-04 20:19:41 -0500107 @dbus.service.method(DBUS_NAME,
Norman James7aeefa72015-10-19 11:13:25 -0500108 in_signature='', out_signature='s')
109 def getSystemState(self):
110 return self.current_state
111
112 def doObjectLookup(self,category,key):
Norman James19e45912015-10-04 20:19:41 -0500113 bus_name = ""
114 obj_path = ""
Norman James66c02692015-10-15 10:23:12 -0500115 intf_name = INTF_ITEM
116 try:
117 obj_path = System.ID_LOOKUP[category][key]
Norman James19e45912015-10-04 20:19:41 -0500118 bus_name = self.bus_name_lookup[obj_path]
Norman James66c02692015-10-15 10:23:12 -0500119 parts = obj_path.split('/')
Norman Jamesa3e47c42015-10-18 14:43:10 -0500120 if (parts[3] == 'sensor'):
Norman James66c02692015-10-15 10:23:12 -0500121 intf_name = INTF_SENSOR
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500122 except Exception as e:
123 print "ERROR SystemManager: "+str(e)+" not found in lookup"
124
Norman James66c02692015-10-15 10:23:12 -0500125 return [bus_name,obj_path,intf_name]
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500126
Norman James7aeefa72015-10-19 11:13:25 -0500127 @dbus.service.method(DBUS_NAME,
128 in_signature='ss', out_signature='(sss)')
129 def getObjectFromId(self,category,key):
130 return self.doObjectLookup(category,key)
131
132 @dbus.service.method(DBUS_NAME,
133 in_signature='sy', out_signature='(sss)')
134 def getObjectFromByteId(self,category,key):
135 byte = int(key)
136 return self.doObjectLookup(category,byte)
Norman James19e45912015-10-04 20:19:41 -0500137
Norman Jamescf74f952015-10-28 12:45:18 -0500138 def start_process(self,name):
139 if (System.APPS[name]['start_process'] == True):
140 app = System.APPS[name]
141 process_name = self.bin_path+"/"+app['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500142 cmdline = [ ]
143 cmdline.append(process_name)
Norman Jamescf74f952015-10-28 12:45:18 -0500144 app['popen'] = None
145 if (app.has_key('args')):
146 for a in app['args']:
147 cmdline.append(a)
Norman James5d78b4d2015-09-05 13:34:34 -0500148 try:
Norman Jamescf74f952015-10-28 12:45:18 -0500149 print "Starting process: "+" ".join(cmdline)+": "+name
150 app['popen'] = subprocess.Popen(cmdline)
Norman James5d78b4d2015-09-05 13:34:34 -0500151 except Exception as e:
152 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500153 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500154
Norman James89de9162015-08-27 21:41:36 -0500155 def heartbeat_check(self):
Norman Jamescf74f952015-10-28 12:45:18 -0500156 for name in System.APPS.keys():
157 app = System.APPS[name]
158 if (app['start_process'] == True and
159 app.has_key('popen') and
160 app['monitor_process'] == True):
161
Norman James5d78b4d2015-09-05 13:34:34 -0500162 ## make sure process is still alive
Norman Jamescf74f952015-10-28 12:45:18 -0500163 p = app['popen']
Norman James5d78b4d2015-09-05 13:34:34 -0500164 p.poll()
165 if (p.returncode != None):
Norman Jamescf74f952015-10-28 12:45:18 -0500166 print "Process for "+name+" appears to be dead"
167 self.start_process(name)
Norman James5d78b4d2015-09-05 13:34:34 -0500168
Norman James471ab592015-08-30 22:29:40 -0500169 return True
Norman James89de9162015-08-27 21:41:36 -0500170
Norman Jamescfc2b442015-10-31 17:31:46 -0500171 def NewObjectHandler(self,obj_path, interface_name, bus_name = None):
172 if (self.bus_name_lookup.has_key(obj_path)):
173 if (self.bus_name_lookup[obj_path] == bus_name):
174 return
175 self.bus_name_lookup[obj_path] = bus_name
176 print "New object: "+obj_path+" ("+bus_name+")"
177 try:
178 if (System.EXIT_STATE_DEPEND[self.current_state].has_key(obj_path) == True):
179 System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 1
Norman Jamescf74f952015-10-28 12:45:18 -0500180 ## check if all required objects are started to move to next state
Norman Jamescfc2b442015-10-31 17:31:46 -0500181 state = 1
182 for obj_path in System.EXIT_STATE_DEPEND[self.current_state]:
183 if (System.EXIT_STATE_DEPEND[self.current_state][obj_path] == 0):
184 state = 0
185 ## all required objects have started so go to next state
186 if (state == 1):
187 print "All required objects started for "+self.current_state
188 self.gotoNextState()
189 except:
190 pass
Norman Jamesa3e47c42015-10-18 14:43:10 -0500191
Norman James89de9162015-08-27 21:41:36 -0500192
193 @dbus.service.method(DBUS_NAME,
194 in_signature='s', out_signature='sis')
195 def gpioInit(self,name):
196 gpio_path = ''
Norman Jamescf74f952015-10-28 12:45:18 -0500197 gpio_num = -1
198 r = ['',gpio_num,'']
Norman James471ab592015-08-30 22:29:40 -0500199 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500200 # TODO: Error handling
201 print "ERROR: "+name+" not found in GPIO config table"
Norman James89de9162015-08-27 21:41:36 -0500202 else:
Norman Jamescf74f952015-10-28 12:45:18 -0500203
204 gpio_num = -1
205 gpio = System.GPIO_CONFIG[name]
206 if (System.GPIO_CONFIG[name].has_key('gpio_num')):
207 gpio_num = gpio['gpio_num']
208 else:
209 if (System.GPIO_CONFIG[name].has_key('gpio_pin')):
210 gpio_num = System.convertGpio(gpio['gpio_pin'])
211 else:
212 print "ERROR: SystemManager - GPIO lookup failed for "+name
213
214 if (gpio_num != -1):
215 r = [Openbmc.GPIO_DEV, gpio_num, gpio['direction']]
216 return r
Norman James88872672015-09-21 16:51:35 -0500217
Norman James89de9162015-08-27 21:41:36 -0500218
219if __name__ == '__main__':
220 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -0500221 bus = Openbmc.getDBus()
Norman James89de9162015-08-27 21:41:36 -0500222 name = dbus.service.BusName(DBUS_NAME,bus)
223 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500224 mainloop = gobject.MainLoop()
Norman James89de9162015-08-27 21:41:36 -0500225
Norman James89de9162015-08-27 21:41:36 -0500226 print "Running SystemManager"
227 mainloop.run()
228