blob: cfbb0f4fdac488a6438fd874e0ef4875ca9ba40d [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 = {}
Brad Bishop416539d2016-07-22 07:22:42 -040043 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
Norman Jamesc36372d2015-10-08 07:03:07 -050044
Brad Bishop416539d2016-07-22 07:22:42 -040045 for name in System.APPS.keys():
46 sys_state = System.APPS[name]['system_state']
47 if sys_state not in self.system_states:
48 self.system_states[sys_state] = []
49 self.system_states[sys_state].append(name)
Norman Jamescfc2b442015-10-31 17:31:46 -050050
Brad Bishop416539d2016-07-22 07:22:42 -040051 ## 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(
56 "<inventory_root>", System.INVENTORY_ROOT)
57 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050058
Brad Bishop416539d2016-07-22 07:22:42 -040059 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040060
Brad Bishop416539d2016-07-22 07:22:42 -040061 if not os.path.exists(PropertyCacher.CACHE_PATH):
62 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
63 os.makedirs(PropertyCacher.CACHE_PATH)
Norman James362a80f2015-09-14 14:04:39 -050064
Brad Bishop416539d2016-07-22 07:22:42 -040065 for s in self.bus.list_names():
66 if obmc.utils.misc.org_dot_openbmc_match(s):
67 self.bus_handler(s, '', s)
Norman James7aeefa72015-10-19 11:13:25 -050068
Brad Bishop416539d2016-07-22 07:22:42 -040069 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040070
Brad Bishop416539d2016-07-22 07:22:42 -040071 def try_next_state(self):
72 current_state = self.Get(DBUS_NAME, "current_state")
73 if current_state not in System.EXIT_STATE_DEPEND:
74 return
Brad Bishop4de42642016-06-29 21:55:47 -040075
Brad Bishop416539d2016-07-22 07:22:42 -040076 if all(System.EXIT_STATE_DEPEND[current_state].values()):
77 print "All required objects started for "+current_state
78 self.gotoNextState()
Brad Bishop4de42642016-06-29 21:55:47 -040079
Brad Bishop416539d2016-07-22 07:22:42 -040080 def SystemStateHandler(self, state_name):
81 ## clearing object started flags
82 current_state = self.Get(DBUS_NAME, "current_state")
83 try:
84 for obj_path in System.EXIT_STATE_DEPEND[current_state]:
85 System.EXIT_STATE_DEPEND[current_state][obj_path] = 0
86 except:
87 pass
Norman James65a295a2015-09-26 22:21:10 -050088
Brad Bishop416539d2016-07-22 07:22:42 -040089 print "Running System State: "+state_name
90 if state_name in self.system_states:
91 for name in self.system_states[state_name]:
92 self.start_process(name)
Norman James471ab592015-08-30 22:29:40 -050093
Brad Bishop416539d2016-07-22 07:22:42 -040094 try:
95 cb = System.ENTER_STATE_CALLBACK[state_name]
96 for methd in cb.keys():
97 obj = bus.get_object(
98 cb[methd]['bus_name'],
99 cb[methd]['obj_name'],
100 introspect=False)
101 method = obj.get_dbus_method(
102 methd, cb[methd]['interface_name'])
103 method()
104 except:
105 pass
Norman James7aeefa72015-10-19 11:13:25 -0500106
Brad Bishop416539d2016-07-22 07:22:42 -0400107 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500108
Brad Bishop416539d2016-07-22 07:22:42 -0400109 def gotoNextState(self):
110 s = 0
111 current_state = self.Get(DBUS_NAME, "current_state")
112 for i in range(len(System.SYSTEM_STATES)):
113 if (System.SYSTEM_STATES[i] == current_state):
114 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -0500115
Brad Bishop416539d2016-07-22 07:22:42 -0400116 if (s == len(System.SYSTEM_STATES)):
117 print "ERROR SystemManager: No more system states"
118 else:
119 new_state_name = System.SYSTEM_STATES[s]
120 print "SystemManager Goto System State: "+new_state_name
121 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500122
Brad Bishop416539d2016-07-22 07:22:42 -0400123 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
124 def getSystemState(self):
125 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +0800126
Brad Bishop416539d2016-07-22 07:22:42 -0400127 def doObjectLookup(self, category, key):
Brad Bishop416539d2016-07-22 07:22:42 -0400128 obj_path = ""
129 intf_name = INTF_ITEM
130 try:
131 obj_path = System.ID_LOOKUP[category][key]
Brad Bishop416539d2016-07-22 07:22:42 -0400132 parts = obj_path.split('/')
133 if (parts[3] == 'sensors'):
134 intf_name = INTF_SENSOR
135 except Exception as e:
136 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +0800137
Brad Bishopfc38a572016-07-22 08:56:09 -0400138 return [obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -0500139
Brad Bishopfc38a572016-07-22 08:56:09 -0400140 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400141 def getObjectFromId(self, category, key):
142 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -0500143
Brad Bishopfc38a572016-07-22 08:56:09 -0400144 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400145 def getObjectFromByteId(self, category, key):
146 byte = int(key)
147 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -0400148
Brad Bishop416539d2016-07-22 07:22:42 -0400149 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
150 # If serval areas are defined for a fru_id, the areas are returned
151 # together as a string with each area name seperated with ','.
152 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
153 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
154 def getFRUArea(self, fru_id):
155 ret_str = ''
156 fru_id = '_' + str(fru_id)
157 area_list = [
158 area for area in System.ID_LOOKUP['FRU_STR'].keys()
159 if area.endswith(fru_id)]
160 for area in area_list:
161 ret_str = area + ',' + ret_str
162 # remove the last ','
163 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400164
Brad Bishop416539d2016-07-22 07:22:42 -0400165 def start_process(self, name):
166 if System.APPS[name]['start_process']:
167 app = System.APPS[name]
168 process_name = self.bin_path+"/"+app['process_name']
169 cmdline = []
170 cmdline.append(process_name)
171 if 'args' in app:
172 for a in app['args']:
173 cmdline.append(a)
174 try:
175 print "Starting process: "+" ".join(cmdline)+": "+name
176 if app['monitor_process']:
177 app['popen'] = subprocess.Popen(cmdline)
178 else:
179 subprocess.Popen(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400180
Brad Bishop416539d2016-07-22 07:22:42 -0400181 except Exception as e:
182 ## TODO: error
183 print "ERROR: starting process: "+" ".join(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400184
Brad Bishop416539d2016-07-22 07:22:42 -0400185 def bus_handler(self, owned_name, old, new):
186 if obmc.dbuslib.bindings.is_unique(owned_name) or not new:
187 return
Norman James89de9162015-08-27 21:41:36 -0500188
Brad Bishop416539d2016-07-22 07:22:42 -0400189 if owned_name == DBUS_NAME:
190 return
Norman James5236a8f2015-11-05 20:39:31 -0600191
Brad Bishop416539d2016-07-22 07:22:42 -0400192 objs = obmc.dbuslib.introspection.find_dbus_interfaces(
193 self.bus, owned_name, '/', bool)
194 current_state = self.Get(DBUS_NAME, "current_state")
195 for o in objs.keys():
Brad Bishop416539d2016-07-22 07:22:42 -0400196 if current_state not in System.EXIT_STATE_DEPEND:
197 continue
198 if o in System.EXIT_STATE_DEPEND[current_state]:
199 print "New object: "+o+" ("+owned_name+")"
200 System.EXIT_STATE_DEPEND[current_state][o] = 1
201
202 self.try_next_state()
203
204 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
205 current_state = self.Get(DBUS_NAME, "current_state")
Brad Bishop416539d2016-07-22 07:22:42 -0400206 if current_state not in System.EXIT_STATE_DEPEND:
207 return
208
209 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
210 print "New object: "+obj_path+" ("+bus_name+")"
211 System.EXIT_STATE_DEPEND[current_state][obj_path] = 1
212 ## check if all required objects are
213 # started to move to next state
214 self.try_next_state()
215
216 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
217 def gpioInit(self, name):
218 gpio_path = ''
219 gpio_num = -1
220 r = ['', gpio_num, '']
221 if name not in System.GPIO_CONFIG:
222 # TODO: Error handling
223 print "ERROR: "+name+" not found in GPIO config table"
224 else:
225
226 gpio_num = -1
227 gpio = System.GPIO_CONFIG[name]
228 if 'gpio_num' in System.GPIO_CONFIG[name]:
229 gpio_num = gpio['gpio_num']
230 else:
231 if 'gpio_pin' in System.GPIO_CONFIG[name]:
232 gpio_num = System.convertGpio(gpio['gpio_pin'])
233 else:
234 print "ERROR: SystemManager - GPIO lookup failed for "+name
235
236 if (gpio_num != -1):
237 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
238 return r
239
Norman James89de9162015-08-27 21:41:36 -0500240
241if __name__ == '__main__':
242 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400243 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400244 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500245 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400246 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400247 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500248
Norman James89de9162015-08-27 21:41:36 -0500249 print "Running SystemManager"
250 mainloop.run()