blob: b33f78f88a7e93f20e29d5c31f72bbf2ca6876d4 [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
11import json
Norman Jamescf74f952015-10-28 12:45:18 -050012import xml.etree.ElementTree as ET
13
Norman James9e6acf92015-09-08 07:00:04 -050014import Openbmc
15
Norman James471ab592015-08-30 22:29:40 -050016if (len(sys.argv) < 2):
17 print "Usage: system_manager.py [system name]"
18 exit(1)
19
20System = __import__(sys.argv[1])
21import Openbmc
Norman James89de9162015-08-27 21:41:36 -050022
23DBUS_NAME = 'org.openbmc.managers.System'
24OBJ_NAME = '/org/openbmc/managers/System'
Norman James471ab592015-08-30 22:29:40 -050025HEARTBEAT_CHECK_INTERVAL = 20000
Norman James9a60a7b2015-10-06 16:53:23 -050026STATE_START_TIMEOUT = 10
Norman James66c02692015-10-15 10:23:12 -050027INTF_SENSOR = 'org.openbmc.SensorValue'
28INTF_ITEM = 'org.openbmc.InventoryItem'
Norman Jamesa3e47c42015-10-18 14:43:10 -050029INTF_CONTROL = 'org.openbmc.Control'
Norman James89de9162015-08-27 21:41:36 -050030
Norman Jamescf74f952015-10-28 12:45:18 -050031def get_objects(bus,bus_name,path,objects):
32 tmp_path = path
33 if (path == ""):
34 tmp_path="/"
35
36 obj = bus.get_object(bus_name,tmp_path)
37 introspect_iface = dbus.Interface(obj,"org.freedesktop.DBus.Introspectable")
38 tree = ET.ElementTree(ET.fromstring(introspect_iface.Introspect()))
39 root = tree.getroot()
40 parent = True
41 ##print introspect_iface.Introspect()
42 for node in root.iter('node'):
43 for intf in node.iter('interface'):
44 objects[path] = True
45
46 if (node.attrib.has_key('name') == True):
47 node_name = node.attrib['name']
48 if (parent == False):
49 get_objects(bus,bus_name,path+"/"+node_name,objects)
50 else:
51 if (node_name != "" and node_name != path):
52 get_objects(bus,bus_name,node_name,objects)
53
54 parent = False
55
56
57
58
Norman James89de9162015-08-27 21:41:36 -050059class SystemManager(dbus.service.Object):
60 def __init__(self,bus,name):
61 dbus.service.Object.__init__(self,bus,name)
Norman James9e6acf92015-09-08 07:00:04 -050062
Norman James90baede2015-09-02 20:32:49 -050063 ## Signal handlers
64 bus.add_signal_receiver(self.NewBusHandler,
Norman James89de9162015-08-27 21:41:36 -050065 dbus_interface = 'org.freedesktop.DBus',
66 signal_name = "NameOwnerChanged")
Norman James362a80f2015-09-14 14:04:39 -050067 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050068
Norman James362a80f2015-09-14 14:04:39 -050069 self.current_state = ""
70 self.system_states = {}
Norman James19e45912015-10-04 20:19:41 -050071 self.bus_name_lookup = {}
Norman Jamesc36372d2015-10-08 07:03:07 -050072 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
73
Norman Jamescf74f952015-10-28 12:45:18 -050074 for name in System.APPS.keys():
75 sys_state = System.APPS[name]['system_state']
Norman James362a80f2015-09-14 14:04:39 -050076 if (self.system_states.has_key(sys_state) == False):
77 self.system_states[sys_state] = []
Norman Jamescf74f952015-10-28 12:45:18 -050078 self.system_states[sys_state].append(name)
Norman James19e45912015-10-04 20:19:41 -050079
80 ## replace symbolic path in ID_LOOKUP
81 for category in System.ID_LOOKUP:
82 for key in System.ID_LOOKUP[category]:
83 val = System.ID_LOOKUP[category][key]
84 new_val = val.replace("<inventory_root>",System.INVENTORY_ROOT)
85 System.ID_LOOKUP[category][key] = new_val
86
Norman James7aeefa72015-10-19 11:13:25 -050087 self.SystemStateHandler(System.SYSTEM_STATES[0])
Norman James362a80f2015-09-14 14:04:39 -050088 print "SystemManager Init Done"
89
Norman James7aeefa72015-10-19 11:13:25 -050090
Norman James362a80f2015-09-14 14:04:39 -050091 def SystemStateHandler(self,state_name):
Norman Jamesa3e47c42015-10-18 14:43:10 -050092 ## clearing object started flags
93 try:
94 for obj_path in System.EXIT_STATE_DEPEND[self.current_state]:
95 System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 0
96 except:
97 pass
Norman James65a295a2015-09-26 22:21:10 -050098
Norman James362a80f2015-09-14 14:04:39 -050099 print "Running System State: "+state_name
100 if (self.system_states.has_key(state_name)):
Norman Jamescf74f952015-10-28 12:45:18 -0500101 for name in self.system_states[state_name]:
102 self.start_process(name)
Norman James90baede2015-09-02 20:32:49 -0500103
Norman Jamesa3e47c42015-10-18 14:43:10 -0500104 if (state_name == "BMC_INIT"):
Norman James362a80f2015-09-14 14:04:39 -0500105 ## Add poll for heartbeat
Norman James6f8d0422015-09-14 18:48:00 -0500106 gobject.timeout_add(HEARTBEAT_CHECK_INTERVAL, self.heartbeat_check)
Norman James9e6acf92015-09-08 07:00:04 -0500107
Norman Jamesa3e47c42015-10-18 14:43:10 -0500108 try:
Norman James362a80f2015-09-14 14:04:39 -0500109 cb = System.ENTER_STATE_CALLBACK[state_name]
Norman James8c6d8382015-10-06 07:47:16 -0500110 obj = bus.get_object(cb['bus_name'],cb['obj_name'])
Norman James362a80f2015-09-14 14:04:39 -0500111 method = obj.get_dbus_method(cb['method_name'],cb['interface_name'])
112 method()
Norman Jamesa3e47c42015-10-18 14:43:10 -0500113 except:
114 pass
Norman James471ab592015-08-30 22:29:40 -0500115
Norman James65a295a2015-09-26 22:21:10 -0500116 self.current_state = state_name
Norman James7aeefa72015-10-19 11:13:25 -0500117
118 def gotoNextState(self):
119 s = 0
120 for i in range(len(System.SYSTEM_STATES)):
121 if (System.SYSTEM_STATES[i] == self.current_state):
122 s = i+1
123
124 if (s == len(System.SYSTEM_STATES)):
125 print "ERROR SystemManager: No more system states"
126 else:
127 new_state_name = System.SYSTEM_STATES[s]
128 print "SystemManager Goto System State: "+new_state_name
129 self.SystemStateHandler(new_state_name)
130
131
Norman James19e45912015-10-04 20:19:41 -0500132 @dbus.service.method(DBUS_NAME,
Norman James7aeefa72015-10-19 11:13:25 -0500133 in_signature='', out_signature='s')
134 def getSystemState(self):
135 return self.current_state
136
137 def doObjectLookup(self,category,key):
Norman James19e45912015-10-04 20:19:41 -0500138 bus_name = ""
139 obj_path = ""
Norman James66c02692015-10-15 10:23:12 -0500140 intf_name = INTF_ITEM
141 try:
142 obj_path = System.ID_LOOKUP[category][key]
Norman James19e45912015-10-04 20:19:41 -0500143 bus_name = self.bus_name_lookup[obj_path]
Norman James66c02692015-10-15 10:23:12 -0500144 parts = obj_path.split('/')
Norman Jamesa3e47c42015-10-18 14:43:10 -0500145 if (parts[3] == 'sensor'):
Norman James66c02692015-10-15 10:23:12 -0500146 intf_name = INTF_SENSOR
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500147 except Exception as e:
148 print "ERROR SystemManager: "+str(e)+" not found in lookup"
149
Norman James66c02692015-10-15 10:23:12 -0500150 return [bus_name,obj_path,intf_name]
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500151
Norman James7aeefa72015-10-19 11:13:25 -0500152 @dbus.service.method(DBUS_NAME,
153 in_signature='ss', out_signature='(sss)')
154 def getObjectFromId(self,category,key):
155 return self.doObjectLookup(category,key)
156
157 @dbus.service.method(DBUS_NAME,
158 in_signature='sy', out_signature='(sss)')
159 def getObjectFromByteId(self,category,key):
160 byte = int(key)
161 return self.doObjectLookup(category,byte)
Norman James19e45912015-10-04 20:19:41 -0500162
Norman Jamescf74f952015-10-28 12:45:18 -0500163 def start_process(self,name):
164 if (System.APPS[name]['start_process'] == True):
165 app = System.APPS[name]
166 process_name = self.bin_path+"/"+app['process_name']
Norman James5d78b4d2015-09-05 13:34:34 -0500167 cmdline = [ ]
168 cmdline.append(process_name)
Norman Jamescf74f952015-10-28 12:45:18 -0500169 app['popen'] = None
170 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
175 app['popen'] = subprocess.Popen(cmdline)
Norman James5d78b4d2015-09-05 13:34:34 -0500176 except Exception as e:
177 ## TODO: error
Norman James8c6d8382015-10-06 07:47:16 -0500178 print "ERROR: starting process: "+" ".join(cmdline)
Norman James471ab592015-08-30 22:29:40 -0500179
Norman James89de9162015-08-27 21:41:36 -0500180 def heartbeat_check(self):
Norman Jamescf74f952015-10-28 12:45:18 -0500181 for name in System.APPS.keys():
182 app = System.APPS[name]
183 if (app['start_process'] == True and
184 app.has_key('popen') and
185 app['monitor_process'] == True):
186
Norman James5d78b4d2015-09-05 13:34:34 -0500187 ## make sure process is still alive
Norman Jamescf74f952015-10-28 12:45:18 -0500188 p = app['popen']
Norman James5d78b4d2015-09-05 13:34:34 -0500189 p.poll()
190 if (p.returncode != None):
Norman Jamescf74f952015-10-28 12:45:18 -0500191 print "Process for "+name+" appears to be dead"
192 self.start_process(name)
Norman James5d78b4d2015-09-05 13:34:34 -0500193
Norman James471ab592015-08-30 22:29:40 -0500194 return True
Norman James89de9162015-08-27 21:41:36 -0500195
Norman James90baede2015-09-02 20:32:49 -0500196 def NewBusHandler(self, bus_name, a, b):
Norman James471ab592015-08-30 22:29:40 -0500197 if (len(b) > 0 and bus_name.find(Openbmc.BUS_PREFIX) == 0):
Norman James19e45912015-10-04 20:19:41 -0500198 objects = {}
Norman James18998182015-10-11 21:54:53 -0500199 try:
Norman Jamescf74f952015-10-28 12:45:18 -0500200 get_objects(bus,bus_name,"",objects)
201 for obj_path in objects.keys():
Norman Jamesa3e47c42015-10-18 14:43:10 -0500202 self.bus_name_lookup[obj_path] = bus_name
Norman Jamescf74f952015-10-28 12:45:18 -0500203 print "New object: "+obj_path+" ("+bus_name+")"
Norman Jamesa3e47c42015-10-18 14:43:10 -0500204 if (System.EXIT_STATE_DEPEND[self.current_state].has_key(obj_path) == True):
205 System.EXIT_STATE_DEPEND[self.current_state][obj_path] = 1
206
Norman James2656f332015-10-26 06:42:41 -0500207 except Exception as e:
Norman Jamescf74f952015-10-28 12:45:18 -0500208 ## object probably disappeared
209 #print e
Norman James18998182015-10-11 21:54:53 -0500210 pass
Norman Jamesa3e47c42015-10-18 14:43:10 -0500211
Norman Jamescf74f952015-10-28 12:45:18 -0500212 ## check if all required objects are started to move to next state
Norman Jamesa3e47c42015-10-18 14:43:10 -0500213 try:
214 state = 1
215 for obj_path in System.EXIT_STATE_DEPEND[self.current_state]:
216 if (System.EXIT_STATE_DEPEND[self.current_state][obj_path] == 0):
217 state = 0
Norman James7aeefa72015-10-19 11:13:25 -0500218 ## all required objects have started so go to next state
Norman Jamesa3e47c42015-10-18 14:43:10 -0500219 if (state == 1):
Norman James7aeefa72015-10-19 11:13:25 -0500220 self.gotoNextState()
Norman Jamesa3e47c42015-10-18 14:43:10 -0500221 except:
222 pass
223
224
Norman James89de9162015-08-27 21:41:36 -0500225
226 @dbus.service.method(DBUS_NAME,
227 in_signature='s', out_signature='sis')
228 def gpioInit(self,name):
229 gpio_path = ''
Norman Jamescf74f952015-10-28 12:45:18 -0500230 gpio_num = -1
231 r = ['',gpio_num,'']
Norman James471ab592015-08-30 22:29:40 -0500232 if (System.GPIO_CONFIG.has_key(name) == False):
Norman James89de9162015-08-27 21:41:36 -0500233 # TODO: Error handling
234 print "ERROR: "+name+" not found in GPIO config table"
Norman James89de9162015-08-27 21:41:36 -0500235 else:
Norman Jamescf74f952015-10-28 12:45:18 -0500236
237 gpio_num = -1
238 gpio = System.GPIO_CONFIG[name]
239 if (System.GPIO_CONFIG[name].has_key('gpio_num')):
240 gpio_num = gpio['gpio_num']
241 else:
242 if (System.GPIO_CONFIG[name].has_key('gpio_pin')):
243 gpio_num = System.convertGpio(gpio['gpio_pin'])
244 else:
245 print "ERROR: SystemManager - GPIO lookup failed for "+name
246
247 if (gpio_num != -1):
248 r = [Openbmc.GPIO_DEV, gpio_num, gpio['direction']]
249 return r
Norman James88872672015-09-21 16:51:35 -0500250
Norman James89de9162015-08-27 21:41:36 -0500251
252if __name__ == '__main__':
253 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Norman James5e792e32015-10-07 17:36:17 -0500254 bus = Openbmc.getDBus()
Norman James89de9162015-08-27 21:41:36 -0500255 name = dbus.service.BusName(DBUS_NAME,bus)
256 obj = SystemManager(bus,OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500257 mainloop = gobject.MainLoop()
Norman James89de9162015-08-27 21:41:36 -0500258
Norman James89de9162015-08-27 21:41:36 -0500259 print "Running SystemManager"
260 mainloop.run()
261