blob: d145e7422cf811bb4636581e03a3447053cb64c3 [file] [log] [blame]
Brad Bishopde6ed122016-08-30 20:14:57 -04001#!/usr/bin/env python
Norman James89de9162015-08-27 21:41:36 -05002
Norman James6f8d0422015-09-14 18:48:00 -05003import gobject
Norman James89de9162015-08-27 21:41:36 -05004import dbus
5import dbus.service
6import dbus.mainloop.glib
Norman James90baede2015-09-02 20:32:49 -05007import os
Brad Bishopee1b1542016-05-12 16:55:00 -04008import obmc.dbuslib.propertycacher as PropertyCacher
Brad Bishop84e73b52016-05-12 15:57:52 -04009from obmc.dbuslib.bindings import DbusProperties, DbusObjectManager, get_dbus
10import obmc.enums
Brad Bishop0b380f72016-06-10 00:29:50 -040011import obmc_system_config as System
Brad Bishop6173a892016-07-24 10:04:09 -040012import obmc.mapper.utils
Brad Bishop7e5ec462016-09-21 09:09:48 -040013import obmc.inventory
Brad Bishopa7ac8052016-09-21 09:17:05 -040014import obmc.system
Norman James89de9162015-08-27 21:41:36 -050015
16DBUS_NAME = 'org.openbmc.managers.System'
17OBJ_NAME = '/org/openbmc/managers/System'
Norman James66c02692015-10-15 10:23:12 -050018INTF_SENSOR = 'org.openbmc.SensorValue'
19INTF_ITEM = 'org.openbmc.InventoryItem'
Norman James89de9162015-08-27 21:41:36 -050020
Ratan Gupta520f8b02016-09-06 21:47:05 +053021SYS_STATE_FILE = '/var/lib/obmc/last-system-state'
22POWER_OFF = "0"
23
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):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040027 super(SystemManager, self).__init__(
28 conn=bus,
29 object_path=obj_name)
Brad Bishop416539d2016-07-22 07:22:42 -040030 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
Ratan Gupta520f8b02016-09-06 21:47:05 +053038 bus.add_signal_receiver(
39 self.chassisPowerStateHandler,
40 dbus_interface="org.freedesktop.DBus.Properties",
41 signal_name="PropertiesChanged",
42 path="/org/openbmc/control/power0")
Norman Jamescfc2b442015-10-31 17:31:46 -050043
Ratan Gupta520f8b02016-09-06 21:47:05 +053044 self.Set(DBUS_NAME, "current_state", "")
45 self.Set(DBUS_NAME, "system_last_state", POWER_OFF)
46 self.import_system_state_from_disk()
47 # replace symbolic path in ID_LOOKUP
Brad Bishop416539d2016-07-22 07:22:42 -040048 for category in System.ID_LOOKUP:
49 for key in System.ID_LOOKUP[category]:
50 val = System.ID_LOOKUP[category][key]
51 new_val = val.replace(
Brad Bishop7e5ec462016-09-21 09:09:48 -040052 "<inventory_root>", obmc.inventory.INVENTORY_ROOT)
Brad Bishop416539d2016-07-22 07:22:42 -040053 System.ID_LOOKUP[category][key] = new_val
Norman Jamescfc2b442015-10-31 17:31:46 -050054
Brad Bishop416539d2016-07-22 07:22:42 -040055 self.SystemStateHandler(System.SYSTEM_STATES[0])
Brad Bishopfa736492016-06-29 22:21:49 -040056
Brad Bishop416539d2016-07-22 07:22:42 -040057 print "SystemManager Init Done"
Brad Bishop4de42642016-06-29 21:55:47 -040058
Ratan Gupta520f8b02016-09-06 21:47:05 +053059 def chassisPowerStateHandler(self, interface_name, changed_properties,
60 invalidated_properties):
61 value = changed_properties.get('state')
62 if value is not None:
63 self.write_to_disk_and_update(str(value))
64
Brad Bishop416539d2016-07-22 07:22:42 -040065 def SystemStateHandler(self, state_name):
Brad Bishop416539d2016-07-22 07:22:42 -040066 print "Running System State: "+state_name
Norman James7aeefa72015-10-19 11:13:25 -050067
Brad Bishop416539d2016-07-22 07:22:42 -040068 self.Set(DBUS_NAME, "current_state", state_name)
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050069
Brad Bishop6173a892016-07-24 10:04:09 -040070 waitlist = System.EXIT_STATE_DEPEND.get(state_name, {}).keys()
71 if waitlist:
72 self.waiter = obmc.mapper.utils.Wait(
73 self.bus, waitlist,
74 callback=self.gotoNextState)
75
Brad Bishop416539d2016-07-22 07:22:42 -040076 def gotoNextState(self):
77 s = 0
78 current_state = self.Get(DBUS_NAME, "current_state")
79 for i in range(len(System.SYSTEM_STATES)):
80 if (System.SYSTEM_STATES[i] == current_state):
81 s = i+1
Norman Jamesd9f1d4e2015-10-14 09:40:18 -050082
Brad Bishop416539d2016-07-22 07:22:42 -040083 if (s == len(System.SYSTEM_STATES)):
84 print "ERROR SystemManager: No more system states"
85 else:
86 new_state_name = System.SYSTEM_STATES[s]
87 print "SystemManager Goto System State: "+new_state_name
88 self.SystemStateHandler(new_state_name)
Norman James7aeefa72015-10-19 11:13:25 -050089
Ratan Gupta520f8b02016-09-06 21:47:05 +053090 def import_system_state_from_disk(self):
91 state = str(POWER_OFF)
92 try:
93 with open(SYS_STATE_FILE, 'r+') as f:
94 state = f.readline().rstrip('\n')
95 except IOError:
96 pass
97 self.Set(DBUS_NAME, "system_last_state", state)
98 return state
99
100 def write_to_disk_and_update(self, state):
101 try:
102 with open(SYS_STATE_FILE, 'w+') as f:
103 f.write(str(state))
104 self.Set(DBUS_NAME, "system_last_state", state)
105 except IOError:
106 pass
107
Brad Bishop416539d2016-07-22 07:22:42 -0400108 @dbus.service.method(DBUS_NAME, in_signature='', out_signature='s')
109 def getSystemState(self):
110 return self.Get(DBUS_NAME, "current_state")
Yi Li5cf1e112016-04-15 15:30:30 +0800111
Brad Bishop416539d2016-07-22 07:22:42 -0400112 def doObjectLookup(self, category, key):
Brad Bishop416539d2016-07-22 07:22:42 -0400113 obj_path = ""
114 intf_name = INTF_ITEM
115 try:
116 obj_path = System.ID_LOOKUP[category][key]
Brad Bishop416539d2016-07-22 07:22:42 -0400117 parts = obj_path.split('/')
118 if (parts[3] == 'sensors'):
119 intf_name = INTF_SENSOR
120 except Exception as e:
121 print "ERROR SystemManager: "+str(e)+" not found in lookup"
Yi Li5cf1e112016-04-15 15:30:30 +0800122
Brad Bishopfc38a572016-07-22 08:56:09 -0400123 return [obj_path, intf_name]
Norman James471ab592015-08-30 22:29:40 -0500124
Brad Bishopfc38a572016-07-22 08:56:09 -0400125 @dbus.service.method(DBUS_NAME, in_signature='ss', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400126 def getObjectFromId(self, category, key):
127 return self.doObjectLookup(category, key)
Norman James89de9162015-08-27 21:41:36 -0500128
Brad Bishopfc38a572016-07-22 08:56:09 -0400129 @dbus.service.method(DBUS_NAME, in_signature='sy', out_signature='(ss)')
Brad Bishop416539d2016-07-22 07:22:42 -0400130 def getObjectFromByteId(self, category, key):
131 byte = int(key)
132 return self.doObjectLookup(category, byte)
Brad Bishopfa736492016-06-29 22:21:49 -0400133
Brad Bishop416539d2016-07-22 07:22:42 -0400134 # Get the FRU area names defined in ID_LOOKUP table given a fru_id.
135 # If serval areas are defined for a fru_id, the areas are returned
136 # together as a string with each area name seperated with ','.
137 # If no fru area defined in ID_LOOKUP, an empty string will be returned.
138 @dbus.service.method(DBUS_NAME, in_signature='y', out_signature='s')
139 def getFRUArea(self, fru_id):
140 ret_str = ''
141 fru_id = '_' + str(fru_id)
142 area_list = [
143 area for area in System.ID_LOOKUP['FRU_STR'].keys()
144 if area.endswith(fru_id)]
145 for area in area_list:
146 ret_str = area + ',' + ret_str
147 # remove the last ','
148 return ret_str[:-1]
Brad Bishopfa736492016-06-29 22:21:49 -0400149
Brad Bishop416539d2016-07-22 07:22:42 -0400150 def NewObjectHandler(self, obj_path, iprops, bus_name=None):
151 current_state = self.Get(DBUS_NAME, "current_state")
Brad Bishop416539d2016-07-22 07:22:42 -0400152 if current_state not in System.EXIT_STATE_DEPEND:
153 return
154
155 if obj_path in System.EXIT_STATE_DEPEND[current_state]:
156 print "New object: "+obj_path+" ("+bus_name+")"
Brad Bishop416539d2016-07-22 07:22:42 -0400157
158 @dbus.service.method(DBUS_NAME, in_signature='s', out_signature='sis')
159 def gpioInit(self, name):
160 gpio_path = ''
161 gpio_num = -1
162 r = ['', gpio_num, '']
163 if name not in System.GPIO_CONFIG:
Xo Wang605620d2016-09-21 12:46:29 -0700164 # TODO: Better error handling
165 msg = "ERROR: "+name+" not found in GPIO config table"
166 print msg
167 raise Exception(msg)
Brad Bishop416539d2016-07-22 07:22:42 -0400168 else:
169
170 gpio_num = -1
171 gpio = System.GPIO_CONFIG[name]
172 if 'gpio_num' in System.GPIO_CONFIG[name]:
173 gpio_num = gpio['gpio_num']
174 else:
175 if 'gpio_pin' in System.GPIO_CONFIG[name]:
Brad Bishopa7ac8052016-09-21 09:17:05 -0400176 gpio_num = obmc.system.convertGpio(gpio['gpio_pin'])
Brad Bishop416539d2016-07-22 07:22:42 -0400177 else:
Xo Wang605620d2016-09-21 12:46:29 -0700178 msg = "ERROR: SystemManager - GPIO lookup failed for "+name
179 print msg
180 raise Exception(msg)
Brad Bishop416539d2016-07-22 07:22:42 -0400181
182 if (gpio_num != -1):
183 r = [obmc.enums.GPIO_DEV, gpio_num, gpio['direction']]
184 return r
185
Xo Wang3f87de82016-09-22 11:17:01 -0700186 @dbus.service.method(DBUS_NAME, in_signature='',
Lei YU75a18a22016-11-22 01:47:47 +0800187 out_signature='ssa(sb)a(sb)a(sbb)ssssa(sb)')
188 def getGpioConfiguration(self):
189 power_config = System.GPIO_CONFIGS.get('power_config', {})
190 power_good_in = power_config.get('power_good_in', '')
191 latch_out = power_config.get('latch_out', '')
192 power_up_outs = power_config.get('power_up_outs', [])
193 reset_outs = power_config.get('reset_outs', [])
194 pci_reset_outs = System.GPIO_CONFIGS.get('pci_reset_outs', [])
195 hostctl_config = System.GPIO_CONFIGS.get('hostctl_config', {})
196 fsi_data = hostctl_config.get('fsi_data', '')
197 fsi_clk = hostctl_config.get('fsi_clk', '')
198 fsi_enable = hostctl_config.get('fsi_enable', '')
199 cronus_sel = hostctl_config.get('cronus_sel', '')
200 optionals = hostctl_config.get('optionals', [])
201 r = [power_good_in, latch_out, power_up_outs, reset_outs, pci_reset_outs,\
202 fsi_data, fsi_clk, fsi_enable, cronus_sel, optionals]
Xo Wang3f87de82016-09-22 11:17:01 -0700203 print "Power GPIO config: " + str(r)
204 return r
205
Norman James89de9162015-08-27 21:41:36 -0500206
207if __name__ == '__main__':
208 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
Brad Bishop84e73b52016-05-12 15:57:52 -0400209 bus = get_dbus()
Brad Bishop416539d2016-07-22 07:22:42 -0400210 obj = SystemManager(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500211 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400212 obj.unmask_signals()
Brad Bishop416539d2016-07-22 07:22:42 -0400213 name = dbus.service.BusName(DBUS_NAME, bus)
Norman James89de9162015-08-27 21:41:36 -0500214
Norman James89de9162015-08-27 21:41:36 -0500215 print "Running SystemManager"
216 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400217
218# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4