blob: 72fa59c5bab30eb218429993517e04a2ac29dba2 [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
Brad Bishopee1b1542016-05-12 16:55:00 -040010import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -040011from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
12import obmc.enums
Brad Bishop0b380f72016-06-10 00:29:50 -040013import obmc_system_config as System
Brad Bishopfa736492016-06-29 22:21:49 -040014import obmc.dbuslib.introspection
15import obmc.utils.misc
Norman James89de9162015-08-27 21:41:36 -050016
17DBUS_NAME = 'org.openbmc.managers.System'
18OBJ_NAME = '/org/openbmc/managers/System'
Norman James66c02692015-10-15 10:23:12 -050019INTF_SENSOR = 'org.openbmc.SensorValue'
20INTF_ITEM = 'org.openbmc.InventoryItem'
Norman Jamesa3e47c42015-10-18 14:43:10 -050021INTF_CONTROL = 'org.openbmc.Control'
Norman James89de9162015-08-27 21:41:36 -050022
Norman Jamescf74f952015-10-28 12:45:18 -050023
Brad Bishop416539d2016-07-22 07:22:42 -040024class SystemManager(DbusProperties, DbusObjectManager):
25 def __init__(self, bus, obj_name):
26 DbusProperties.__init__(self)
27 DbusObjectManager.__init__(self)
28 dbus.service.Object.__init__(self, bus, obj_name)
29 self.bus = bus
Norman James9e6acf92015-09-08 07:00:04 -050030
Brad Bishop416539d2016-07-22 07:22:42 -040031 bus.add_signal_receiver(
32 self.bus_handler,
33 dbus_interface=dbus.BUS_DAEMON_IFACE,
34 signal_name='NameOwnerChanged')
35 bus.add_signal_receiver(
36 self.NewObjectHandler,
37 signal_name="InterfacesAdded", sender_keyword='bus_name')
38 bus.add_signal_receiver(
39 self.SystemStateHandler, signal_name="GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050040
Brad Bishop416539d2016-07-22 07:22:42 -040041 self.Set(DBUS_NAME, "current_state", "")
42 self.system_states = {}
43 self.bus_name_lookup = {}
44 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
Norman Jamesc36372d2015-10-08 07:03:07 -050045
Brad Bishop416539d2016-07-22 07:22:42 -040046 for name in System.APPS.keys():
47 sys_state = System.APPS[name]['system_state']
48 if sys_state not in self.system_states:
49 self.system_states[sys_state] = []
50 self.system_states[sys_state].append(name)
Norman Jamescfc2b442015-10-31 17:31:46 -050051
Brad Bishop416539d2016-07-22 07:22:42 -040052 ## replace symbolic path in ID_LOOKUP
53 for category in System.ID_LOOKUP:
54 for key in System.ID_LOOKUP[category]:
55 val = System.ID_LOOKUP[category][key]
56 new_val = val.replace(
57 "<inventory_root>", System.INVENTORY_ROOT)
58 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050059
Brad Bishop416539d2016-07-22 07:22:42 -040060 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040061
Brad Bishop416539d2016-07-22 07:22:42 -040062 if not os.path.exists(PropertyCacher.CACHE_PATH):
63 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
64 os.makedirs(PropertyCacher.CACHE_PATH)
Norman James362a80f2015-09-14 14:04:39 -050065
Brad Bishop416539d2016-07-22 07:22:42 -040066 for s in self.bus.list_names():
67 if obmc.utils.misc.org_dot_openbmc_match(s):
68 self.bus_handler(s, '', s)
Norman James7aeefa72015-10-19 11:13:25 -050069
Brad Bishop416539d2016-07-22 07:22:42 -040070 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040071
Brad Bishop416539d2016-07-22 07:22:42 -040072 def try_next_state(self):
73 current_state = self.Get(DBUS_NAME, "current_state")
74 if current_state not in System.EXIT_STATE_DEPEND:
75 return
Brad Bishop4de42642016-06-29 21:55:47 -040076
Brad Bishop416539d2016-07-22 07:22:42 -040077 if all(System.EXIT_STATE_DEPEND[current_state].values()):
78 print "All required objects started for "+current_state
79 self.gotoNextState()
Brad Bishop4de42642016-06-29 21:55:47 -040080
Brad Bishop416539d2016-07-22 07:22:42 -040081 def SystemStateHandler(self, state_name):
82 ## clearing object started flags
83 current_state = self.Get(DBUS_NAME, "current_state")
84 try:
85 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
86 System.EXIT_STATE_DEPEND[current_state][obj_path] = 0
87 except:
88 pass
Norman James65a295a2015-09-26 22:21:10 -050089
Brad Bishop416539d2016-07-22 07:22:42 -040090 print "Running System State: "+state_name
91 if state_name in self.system_states:
92 for name in self.system_states[state_name]:
93 self.start_process(name)
Norman James471ab592015-08-30 22:29:40 -050094
Brad Bishop416539d2016-07-22 07:22:42 -040095 try:
96 cb = System.ENTER_STATE_CALLBACK[state_name]
97 for methd in cb.keys():
98 obj = bus.get_object(
99 cb[methd]['bus_name'],
100 cb[methd]['obj_name'],
101 introspect=False)
102 method = obj.get_dbus_method(
103 methd, cb[methd]['interface_name'])
104 method()
105 except:
106 pass
Norman James7aeefa72015-10-19 11:13:25 -0500107
Brad Bishop416539d2016-07-22 07:22:42 -0400108 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500109
Brad Bishop416539d2016-07-22 07:22:42 -0400110 def gotoNextState(self):
111 s = 0
112 current_state = self.Get(DBUS_NAME, "current_state")
113 for i in range(len(System.SYSTEM_STATES)):
114 if (System.SYSTEM_STATES[i] == current_state):
115 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500116
Brad Bishop416539d2016-07-22 07:22:42 -0400117 if (s == len(System.SYSTEM_STATES)):
118 print "ERROR SystemManager: No more system states"
119 else:
120 new_state_name = System.SYSTEM_STATES[s]
121 print "SystemManager Goto System State: "+new_state_name
122 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500123
Brad Bishop416539d2016-07-22 07:22:42 -0400124 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
125 def getSystemState(self):
126 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +0800127
Brad Bishop416539d2016-07-22 07:22:42 -0400128 def doObjectLookup(self, category, key):
129 bus_name = ""
130 obj_path = ""
131 intf_name = INTF_ITEM
132 try:
133 obj_path = System.ID_LOOKUP[category][key]
134 bus_name = self.bus_name_lookup[obj_path]
135 parts = obj_path.split('/')
136 if (parts[3] == 'sensors'):
137 intf_name = INTF_SENSOR
138 except Exception as e:
139 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +0800140
Brad Bishop416539d2016-07-22 07:22:42 -0400141 return [bus_name, obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -0500142
Brad Bishop416539d2016-07-22 07:22:42 -0400143 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(sss)')
144 def getObjectFromId(self, category, key):
145 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -0500146
Brad Bishop416539d2016-07-22 07:22:42 -0400147 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(sss)')
148 def getObjectFromByteId(self, category, key):
149 byte = int(key)
150 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -0400151
Brad Bishop416539d2016-07-22 07:22:42 -0400152 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
153 # If serval areas are defined for a fru_id, the areas are returned
154 # together as a string with each area name seperated with ','.
155 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
156 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
157 def getFRUArea(self, fru_id):
158 ret_str = ''
159 fru_id = '_' + str(fru_id)
160 area_list = [
161 area for area in System.ID_LOOKUP['FRU_STR'].keys()
162 if area.endswith(fru_id)]
163 for area in area_list:
164 ret_str = area + ',' + ret_str
165 # remove the last ','
166 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400167
Brad Bishop416539d2016-07-22 07:22:42 -0400168 def start_process(self, name):
169 if System.APPS[name]['start_process']:
170 app = System.APPS[name]
171 process_name = self.bin_path+"/"+app['process_name']
172 cmdline = []
173 cmdline.append(process_name)
174 if 'args' in app:
175 for a in app['args']:
176 cmdline.append(a)
177 try:
178 print "Starting process: "+" ".join(cmdline)+": "+name
179 if app['monitor_process']:
180 app['popen'] = subprocess.Popen(cmdline)
181 else:
182 subprocess.Popen(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400183
Brad Bishop416539d2016-07-22 07:22:42 -0400184 except Exception as e:
185 ## TODO: error
186 print "ERROR: starting process: "+" ".join(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400187
Brad Bishop416539d2016-07-22 07:22:42 -0400188 def bus_handler(self, owned_name, old, new):
189 if obmc.dbuslib.bindings.is_unique(owned_name) or not new:
190 return
Norman James89de9162015-08-27 21:41:36 -0500191
Brad Bishop416539d2016-07-22 07:22:42 -0400192 if owned_name == DBUS_NAME:
193 return
Norman James5236a8f2015-11-05 20:39:31 -0600194
Brad Bishop416539d2016-07-22 07:22:42 -0400195 objs = obmc.dbuslib.introspection.find_dbus_interfaces(
196 self.bus, owned_name, '/', bool)
197 current_state = self.Get(DBUS_NAME, "current_state")
198 for o in objs.keys():
199 if o in self.bus_name_lookup:
200 continue
201 self.bus_name_lookup[o] = owned_name
202
203 if current_state not in System.EXIT_STATE_DEPEND:
204 continue
205 if o in System.EXIT_STATE_DEPEND[current_state]:
206 print "New object: "+o+" ("+owned_name+")"
207 System.EXIT_STATE_DEPEND[current_state][o] = 1
208
209 self.try_next_state()
210
211 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
212 current_state = self.Get(DBUS_NAME, "current_state")
213 if obj_path in self.bus_name_lookup:
214 if (self.bus_name_lookup[obj_path] == bus_name):
215 return
216 self.bus_name_lookup[obj_path] = bus_name
217 if current_state not in System.EXIT_STATE_DEPEND:
218 return
219
220 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
221 print "New object: "+obj_path+" ("+bus_name+")"
222 System.EXIT_STATE_DEPEND[current_state][obj_path] = 1
223 ## check if all required objects are
224 # started to move to next state
225 self.try_next_state()
226
227 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
228 def gpioInit(self, name):
229 gpio_path = ''
230 gpio_num = -1
231 r = ['', gpio_num, '']
232 if name not in System.GPIO_CONFIG:
233 # TODO: Error handling
234 print "ERROR: "+name+" not found in GPIO config table"
235 else:
236
237 gpio_num = -1
238 gpio = System.GPIO_CONFIG[name]
239 if 'gpio_num' in System.GPIO_CONFIG[name]:
240 gpio_num = gpio['gpio_num']
241 else:
242 if 'gpio_pin' in System.GPIO_CONFIG[name]:
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 = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
249 return r
250
Norman James89de9162015-08-27 21:41:36 -0500251
252if __name__ == '__main__':
253 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400254 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400255 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500256 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400257 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400258 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500259
Norman James89de9162015-08-27 21:41:36 -0500260 print "Running SystemManager"
261 mainloop.run()