blob: 819b4c095ff12b6ace1b8f940fea9ce9854199b0 [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 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040059
Brad Bishop416539d2016-07-22 07:22:42 -040060 def SystemStateHandler(self, state_name):
Brad Bishop416539d2016-07-22 07:22:42 -040061 print "Running System State: "+state_name
62 if state_name in self.system_states:
63 for name in self.system_states[state_name]:
64 self.start_process(name)
Norman James471ab592015-08-30 22:29:40 -050065
Brad Bishop416539d2016-07-22 07:22:42 -040066 try:
67 cb = System.ENTER_STATE_CALLBACK[state_name]
68 for methd in cb.keys():
69 obj = bus.get_object(
70 cb[methd]['bus_name'],
71 cb[methd]['obj_name'],
72 introspect=False)
73 method = obj.get_dbus_method(
74 methd, cb[methd]['interface_name'])
75 method()
76 except:
77 pass
Norman James7aeefa72015-10-19 11:13:25 -050078
Brad Bishop416539d2016-07-22 07:22:42 -040079 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050080
Brad Bishop6173a892016-07-24 10:04:09 -040081 waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys()
82 if waitlist:
83 self.waiter = obmc.mapper.utils.Wait(
84 self.bus, waitlist,
85 callback=self.gotoNextState)
86
Brad Bishop416539d2016-07-22 07:22:42 -040087 def gotoNextState(self):
88 s = 0
89 current_state = self.Get(DBUS_NAME, "current_state")
90 for i in range(len(System.SYSTEM_STATES)):
91 if (System.SYSTEM_STATES[i] == current_state):
92 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050093
Brad Bishop416539d2016-07-22 07:22:42 -040094 if (s == len(System.SYSTEM_STATES)):
95 print "ERROR SystemManager: No more system states"
96 else:
97 new_state_name = System.SYSTEM_STATES[s]
98 print "SystemManager Goto System State: "+new_state_name
99 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -0500100
Brad Bishop416539d2016-07-22 07:22:42 -0400101 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
102 def getSystemState(self):
103 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +0800104
Brad Bishop416539d2016-07-22 07:22:42 -0400105 def doObjectLookup(self, category, key):
Brad Bishop416539d2016-07-22 07:22:42 -0400106 obj_path = ""
107 intf_name = INTF_ITEM
108 try:
109 obj_path = System.ID_LOOKUP[category][key]
Brad Bishop416539d2016-07-22 07:22:42 -0400110 parts = obj_path.split('/')
111 if (parts[3] == 'sensors'):
112 intf_name = INTF_SENSOR
113 except Exception as e:
114 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +0800115
Brad Bishopfc38a572016-07-22 08:56:09 -0400116 return [obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -0500117
Brad Bishopfc38a572016-07-22 08:56:09 -0400118 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400119 def getObjectFromId(self, category, key):
120 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -0500121
Brad Bishopfc38a572016-07-22 08:56:09 -0400122 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400123 def getObjectFromByteId(self, category, key):
124 byte = int(key)
125 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -0400126
Brad Bishop416539d2016-07-22 07:22:42 -0400127 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
128 # If serval areas are defined for a fru_id, the areas are returned
129 # together as a string with each area name seperated with ','.
130 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
131 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
132 def getFRUArea(self, fru_id):
133 ret_str = ''
134 fru_id = '_' + str(fru_id)
135 area_list = [
136 area for area in System.ID_LOOKUP['FRU_STR'].keys()
137 if area.endswith(fru_id)]
138 for area in area_list:
139 ret_str = area + ',' + ret_str
140 # remove the last ','
141 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400142
Brad Bishop416539d2016-07-22 07:22:42 -0400143 def start_process(self, name):
144 if System.APPS[name]['start_process']:
145 app = System.APPS[name]
146 process_name = self.bin_path+"/"+app['process_name']
147 cmdline = []
148 cmdline.append(process_name)
149 if 'args' in app:
150 for a in app['args']:
151 cmdline.append(a)
152 try:
153 print "Starting process: "+" ".join(cmdline)+": "+name
154 if app['monitor_process']:
155 app['popen'] = subprocess.Popen(cmdline)
156 else:
157 subprocess.Popen(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400158
Brad Bishop416539d2016-07-22 07:22:42 -0400159 except Exception as e:
160 ## TODO: error
161 print "ERROR: starting process: "+" ".join(cmdline)
Brad Bishopfa736492016-06-29 22:21:49 -0400162
Brad Bishop416539d2016-07-22 07:22:42 -0400163 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
164 current_state = self.Get(DBUS_NAME, "current_state")
Brad Bishop416539d2016-07-22 07:22:42 -0400165 if current_state not in System.EXIT_STATE_DEPEND:
166 return
167
168 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
169 print "New object: "+obj_path+" ("+bus_name+")"
Brad Bishop416539d2016-07-22 07:22:42 -0400170
171 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
172 def gpioInit(self, name):
173 gpio_path = ''
174 gpio_num = -1
175 r = ['', gpio_num, '']
176 if name not in System.GPIO_CONFIG:
177 # TODO: Error handling
178 print "ERROR: "+name+" not found in GPIO config table"
179 else:
180
181 gpio_num = -1
182 gpio = System.GPIO_CONFIG[name]
183 if 'gpio_num' in System.GPIO_CONFIG[name]:
184 gpio_num = gpio['gpio_num']
185 else:
186 if 'gpio_pin' in System.GPIO_CONFIG[name]:
187 gpio_num = System.convertGpio(gpio['gpio_pin'])
188 else:
189 print "ERROR: SystemManager - GPIO lookup failed for "+name
190
191 if (gpio_num != -1):
192 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
193 return r
194
Norman James89de9162015-08-27 21:41:36 -0500195
196if __name__ == '__main__':
197 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400198 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400199 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500200 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400201 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400202 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500203
Norman James89de9162015-08-27 21:41:36 -0500204 print "Running SystemManager"
205 mainloop.run()