blob: d6f9fe719cbf75b551929625fe42980fb8301f8b [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
Brad Bishop6173a892016-07-24 10:04:09 -040016import obmc.mapper.utils
Norman James89de9162015-08-27 21:41:36 -050017
18DBUS_NAME = 'org.openbmc.managers.System'
19OBJ_NAME = '/org/openbmc/managers/System'
Norman James66c02692015-10-15 10:23:12 -050020INTF_SENSOR = 'org.openbmc.SensorValue'
21INTF_ITEM = 'org.openbmc.InventoryItem'
Norman Jamesa3e47c42015-10-18 14:43:10 -050022INTF_CONTROL = 'org.openbmc.Control'
Norman James89de9162015-08-27 21:41:36 -050023
Norman Jamescf74f952015-10-28 12:45:18 -050024
Brad Bishop416539d2016-07-22 07:22:42 -040025class SystemManager(DbusProperties, DbusObjectManager):
26 def __init__(self, bus, obj_name):
27 DbusProperties.__init__(self)
28 DbusObjectManager.__init__(self)
29 dbus.service.Object.__init__(self, bus, obj_name)
30 self.bus = bus
Norman James9e6acf92015-09-08 07:00:04 -050031
Brad Bishop416539d2016-07-22 07:22:42 -040032 bus.add_signal_receiver(
Brad Bishop416539d2016-07-22 07:22:42 -040033 self.NewObjectHandler,
34 signal_name="InterfacesAdded", sender_keyword='bus_name')
35 bus.add_signal_receiver(
36 self.SystemStateHandler, signal_name="GotoSystemState")
Norman James88872672015-09-21 16:51:35 -050037
Brad Bishop416539d2016-07-22 07:22:42 -040038 self.Set(DBUS_NAME, "current_state", "")
39 self.system_states = {}
Brad Bishop416539d2016-07-22 07:22:42 -040040 self.bin_path = os.path.dirname(os.path.realpath(sys.argv[0]))
Norman Jamesc36372d2015-10-08 07:03:07 -050041
Brad Bishop416539d2016-07-22 07:22:42 -040042 for name in System.APPS.keys():
43 sys_state = System.APPS[name]['system_state']
44 if sys_state not in self.system_states:
45 self.system_states[sys_state] = []
46 self.system_states[sys_state].append(name)
Norman Jamescfc2b442015-10-31 17:31:46 -050047
Brad Bishop416539d2016-07-22 07:22:42 -040048 ## replace symbolic path in ID_LOOKUP
49 for category in System.ID_LOOKUP:
50 for key in System.ID_LOOKUP[category]:
51 val = System.ID_LOOKUP[category][key]
52 new_val = val.replace(
53 "<inventory_root>", System.INVENTORY_ROOT)
54 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050055
Brad Bishop416539d2016-07-22 07:22:42 -040056 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040057
Brad Bishop416539d2016-07-22 07:22:42 -040058 if not os.path.exists(PropertyCacher.CACHE_PATH):
59 print "Creating cache directory: "+PropertyCacher.CACHE_PATH
60 os.makedirs(PropertyCacher.CACHE_PATH)
Norman James362a80f2015-09-14 14:04:39 -050061
Brad Bishop416539d2016-07-22 07:22:42 -040062 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040063
Brad Bishop416539d2016-07-22 07:22:42 -040064 def SystemStateHandler(self, state_name):
Brad Bishop416539d2016-07-22 07:22:42 -040065 print "Running System State: "+state_name
66 if state_name in self.system_states:
67 for name in self.system_states[state_name]:
68 self.start_process(name)
Norman James471ab592015-08-30 22:29:40 -050069
Brad Bishop416539d2016-07-22 07:22:42 -040070 try:
71 cb = System.ENTER_STATE_CALLBACK[state_name]
72 for methd in cb.keys():
73 obj = bus.get_object(
74 cb[methd]['bus_name'],
75 cb[methd]['obj_name'],
76 introspect=False)
77 method = obj.get_dbus_method(
78 methd, cb[methd]['interface_name'])
79 method()
80 except:
81 pass
Norman James7aeefa72015-10-19 11:13:25 -050082
Brad Bishop416539d2016-07-22 07:22:42 -040083 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050084
Brad Bishop6173a892016-07-24 10:04:09 -040085 waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys()
86 if waitlist:
87 self.waiter = obmc.mapper.utils.Wait(
88 self.bus, waitlist,
89 callback=self.gotoNextState)
90
Brad Bishop416539d2016-07-22 07:22:42 -040091 def gotoNextState(self):
92 s = 0
93 current_state = self.Get(DBUS_NAME, "current_state")
94 for i in range(len(System.SYSTEM_STATES)):
95 if (System.SYSTEM_STATES[i] == current_state):
96 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050097
Brad Bishop416539d2016-07-22 07:22:42 -040098 if (s == len(System.SYSTEM_STATES)):
99 print "ERROR SystemManager: No more system states"
100 else:
101 new_state_name = System.SYSTEM_STATES[s]
102 print "SystemManager Goto System State: "+new_state_name
103 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500104
Brad Bishop416539d2016-07-22 07:22:42 -0400105 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
106 def getSystemState(self):
107 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +0800108
Brad Bishop416539d2016-07-22 07:22:42 -0400109 def doObjectLookup(self, category, key):
Brad Bishop416539d2016-07-22 07:22:42 -0400110 obj_path = ""
111 intf_name = INTF_ITEM
112 try:
113 obj_path = System.ID_LOOKUP[category][key]
Brad Bishop416539d2016-07-22 07:22:42 -0400114 parts = obj_path.split('/')
115 if (parts[3] == 'sensors'):
116 intf_name = INTF_SENSOR
117 except Exception as e:
118 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +0800119
Brad Bishopfc38a572016-07-22 08:56:09 -0400120 return [obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -0500121
Brad Bishopfc38a572016-07-22 08:56:09 -0400122 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400123 def getObjectFromId(self, category, key):
124 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -0500125
Brad Bishopfc38a572016-07-22 08:56:09 -0400126 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400127 def getObjectFromByteId(self, category, key):
128 byte = int(key)
129 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -0400130
Brad Bishop416539d2016-07-22 07:22:42 -0400131 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
132 # If serval areas are defined for a fru_id, the areas are returned
133 # together as a string with each area name seperated with ','.
134 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
135 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
136 def getFRUArea(self, fru_id):
137 ret_str = ''
138 fru_id = '_' + str(fru_id)
139 area_list = [
140 area for area in System.ID_LOOKUP['FRU_STR'].keys()
141 if area.endswith(fru_id)]
142 for area in area_list:
143 ret_str = area + ',' + ret_str
144 # remove the last ','
145 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400146
Brad Bishop416539d2016-07-22 07:22:42 -0400147 def start_process(self, name):
148 if System.APPS[name]['start_process']:
149 app = System.APPS[name]
150 process_name = self.bin_path+"/"+app['process_name']
151 cmdline = []
152 cmdline.append(process_name)
153 if 'args' in app:
154 for a in app['args']:
155 cmdline.append(a)
156 try:
157 print "Starting process: "+" ".join(cmdline)+": "+name
158 if app['monitor_process']:
159 app['popen'] = subprocess.Popen(cmdline)
160 else:
161 subprocess.Popen(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400162
Brad Bishop416539d2016-07-22 07:22:42 -0400163 except Exception as e:
164 ## TODO: error
165 print "ERROR: starting process: "+" ".join(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400166
Brad Bishop416539d2016-07-22 07:22:42 -0400167 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
168 current_state = self.Get(DBUS_NAME, "current_state")
Brad Bishop416539d2016-07-22 07:22:42 -0400169 if current_state not in System.EXIT_STATE_DEPEND:
170 return
171
172 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
173 print "New object: "+obj_path+" ("+bus_name+")"
Brad Bishop416539d2016-07-22 07:22:42 -0400174
175 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
176 def gpioInit(self, name):
177 gpio_path = ''
178 gpio_num = -1
179 r = ['', gpio_num, '']
180 if name not in System.GPIO_CONFIG:
181 # TODO: Error handling
182 print "ERROR: "+name+" not found in GPIO config table"
183 else:
184
185 gpio_num = -1
186 gpio = System.GPIO_CONFIG[name]
187 if 'gpio_num' in System.GPIO_CONFIG[name]:
188 gpio_num = gpio['gpio_num']
189 else:
190 if 'gpio_pin' in System.GPIO_CONFIG[name]:
191 gpio_num = System.convertGpio(gpio['gpio_pin'])
192 else:
193 print "ERROR: SystemManager - GPIO lookup failed for "+name
194
195 if (gpio_num != -1):
196 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
197 return r
198
Norman James89de9162015-08-27 21:41:36 -0500199
200if __name__ == '__main__':
201 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400202 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400203 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500204 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400205 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400206 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500207
Norman James89de9162015-08-27 21:41:36 -0500208 print "Running SystemManager"
209 mainloop.run()