blob: faf7d1da752d5e11541210cd1a1d6681c7255c4b [file] [log] [blame]
Brad Bishop87c6b732016-08-30 19:17:57 -04001#!/usr/bin/env python
Norman Jamese2765102015-08-19 22:00:55 -05002
Norman James6f8d0422015-09-14 18:48:00 -05003import gobject
Norman Jamese2765102015-08-19 22:00:55 -05004import dbus
5import dbus.service
6import dbus.mainloop.glib
Brad Bishop84e73b52016-05-12 15:57:52 -04007from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Norman Jamese2765102015-08-19 22:00:55 -05008
Norman James3f97c5d2015-08-26 17:44:14 -05009DBUS_NAME = 'org.openbmc.control.Chassis'
Norman James8fee6f22015-10-28 12:48:43 -050010OBJ_NAME = '/org/openbmc/control/chassis0'
Norman Jamesa3e47c42015-10-18 14:43:10 -050011CONTROL_INTF = 'org.openbmc.Control'
Norman James3f97c5d2015-08-26 17:44:14 -050012
Norman Jamesad8c3d32016-02-11 15:19:01 -060013MACHINE_ID = '/etc/machine-id'
14
Norman James2a3d20b2015-08-20 07:09:33 -050015POWER_OFF = 0
16POWER_ON = 1
17
Norman James9e6acf92015-09-08 07:00:04 -050018BOOTED = 100
19
Norman James90baede2015-09-02 20:32:49 -050020
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050021class ChassisControlObject(DbusProperties, DbusObjectManager):
22 def getUuid(self):
Brad Bishop7a4d77a2016-09-08 23:26:17 -040023 uuid = ""
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050024 try:
25 with open(MACHINE_ID) as f:
26 data = f.readline().rstrip('\n')
27 if (len(data) == 32):
28 uuid = data
29 else:
30 print "ERROR: UUID is not formatted correctly: " + data
31 except:
32 print "ERROR: Unable to open uuid file: " + MACHINE_ID
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050033
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050034 return uuid
Norman Jamescfc2b442015-10-31 17:31:46 -050035
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050036 def __init__(self, bus, name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040037 super(ChassisControlObject, self).__init__(
38 conn=bus,
39 object_path=name)
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050040 ## load utilized objects
41 self.dbus_objects = {
42 'power_control': {
43 'bus_name': 'org.openbmc.control.Power',
44 'object_name': '/org/openbmc/control/power0',
45 'interface_name': 'org.openbmc.control.Power'
46 },
47 'identify_led': {
48 'bus_name': 'org.openbmc.control.led',
49 'object_name': '/org/openbmc/control/led/identify',
50 'interface_name': 'org.openbmc.Led'
51 },
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050052 'host_services': {
53 'bus_name': 'org.openbmc.HostServices',
54 'object_name': '/org/openbmc/HostServices',
55 'interface_name': 'org.openbmc.HostServices'
56 },
57 'settings': {
58 'bus_name': 'org.openbmc.settings.Host',
59 'object_name': '/org/openbmc/settings/host0',
60 'interface_name': 'org.freedesktop.DBus.Properties'
61 },
Brad Bishopabe0f682016-08-22 19:27:18 -040062 'systemd': {
63 'bus_name': 'org.freedesktop.systemd1',
64 'object_name': '/org/freedesktop/systemd1',
65 'interface_name': 'org.freedesktop.systemd1.Manager'
Andrew Geissler6b63e9a2016-06-30 10:45:01 -050066 },
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050067 }
Norman James72567ba2016-01-13 16:57:48 -060068
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050069 # uuid
70 self.Set(DBUS_NAME, "uuid", self.getUuid())
71 self.Set(DBUS_NAME, "reboot", 0)
Norman James72567ba2016-01-13 16:57:48 -060072
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050073 bus.add_signal_receiver(self.power_button_signal_handler,
74 dbus_interface="org.openbmc.Button",
75 signal_name="Released",
76 path="/org/openbmc/buttons/power0")
Adriana Kobylakeeb9b442016-08-15 11:12:53 -050077 bus.add_signal_receiver(self.long_power_button_signal_handler,
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050078 dbus_interface="org.openbmc.Button",
79 signal_name="PressedLong",
80 path="/org/openbmc/buttons/power0")
81 bus.add_signal_receiver(self.softreset_button_signal_handler,
82 dbus_interface="org.openbmc.Button",
83 signal_name="Released",
84 path="/org/openbmc/buttons/reset0")
85
86 bus.add_signal_receiver(self.host_watchdog_signal_handler,
87 dbus_interface="org.openbmc.Watchdog",
88 signal_name="WatchdogError")
89
90 bus.add_signal_receiver(self.emergency_shutdown_signal_handler,
91 dbus_interface="org.openbmc.SensorThresholds",
92 signal_name="Emergency")
93
94 bus.add_signal_receiver(self.SystemStateHandler,
95 signal_name="GotoSystemState")
Norman James471ab592015-08-30 22:29:40 -050096
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050097 def getInterface(self, name):
98 o = self.dbus_objects[name]
99 obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False)
100 return dbus.Interface(obj, o['interface_name'])
Norman Jamese2765102015-08-19 22:00:55 -0500101
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500102 @dbus.service.method(DBUS_NAME,
103 in_signature='', out_signature='')
104 def setIdentify(self):
105 print "Turn on identify"
106 intf = self.getInterface('identify_led')
107 intf.setOn()
108 return None
Norman Jamese2765102015-08-19 22:00:55 -0500109
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500110 @dbus.service.method(DBUS_NAME,
111 in_signature='', out_signature='')
112 def clearIdentify(self):
113 print "Turn on identify"
114 intf = self.getInterface('identify_led')
115 intf.setOff()
116 return None
Norman Jamese2765102015-08-19 22:00:55 -0500117
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500118 @dbus.service.method(DBUS_NAME,
119 in_signature='', out_signature='')
120 def powerOn(self):
121 print "Turn on power and boot"
122 self.Set(DBUS_NAME, "reboot", 0)
Brad Bishopabe0f682016-08-22 19:27:18 -0400123 intf = self.getInterface('systemd')
124 f = getattr(intf, 'StartUnit')
125 f.call_async('obmc-chassis-start@0.target', 'replace')
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500126 return None
Norman Jamese2765102015-08-19 22:00:55 -0500127
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500128 @dbus.service.method(DBUS_NAME,
129 in_signature='', out_signature='')
130 def powerOff(self):
131 print "Turn off power"
Brad Bishopabe0f682016-08-22 19:27:18 -0400132
133 intf = self.getInterface('systemd')
134 f = getattr(intf, 'StartUnit')
135 f.call_async('obmc-chassis-stop@0.target', 'replace')
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500136 return None
Norman James362a80f2015-09-14 14:04:39 -0500137
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500138 @dbus.service.method(DBUS_NAME,
139 in_signature='', out_signature='')
140 def softPowerOff(self):
141 print "Soft off power"
142 intf = self.getInterface('host_services')
143 ## host services will call power off when ready
144 intf.SoftPowerOff()
145 return None
Norman James362a80f2015-09-14 14:04:39 -0500146
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500147 @dbus.service.method(DBUS_NAME,
148 in_signature='', out_signature='')
149 def reboot(self):
150 print "Rebooting"
151 if self.getPowerState() == POWER_OFF:
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400152 self.powerOn()
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500153 else:
154 self.Set(DBUS_NAME, "reboot", 1)
155 self.powerOff()
156 return None
Norman Jamesb4ef3182015-12-03 17:54:35 -0600157
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500158 @dbus.service.method(DBUS_NAME,
159 in_signature='', out_signature='')
160 def softReboot(self):
161 print "Soft Rebooting"
162 if self.getPowerState() == POWER_OFF:
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400163 self.powerOn()
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500164 else:
165 self.Set(DBUS_NAME, "reboot", 1)
166 self.softPowerOff()
167 return None
Norman Jamese2765102015-08-19 22:00:55 -0500168
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500169 @dbus.service.method(DBUS_NAME,
Michael Tritz6d2f6c32017-02-02 17:03:18 -0600170 in_signature='', out_signature='')
171 def quiesce(self):
172 intf = self.getInterface('systemd')
173 f = getattr(intf, 'StartUnit')
174 f.call_async('obmc-quiesce-host@0.target', 'replace')
175 return None
176
177 @dbus.service.method(DBUS_NAME,
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500178 in_signature='', out_signature='i')
179 def getPowerState(self):
180 intf = self.getInterface('power_control')
181 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500182
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500183 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500184
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500185 def SystemStateHandler(self, state_name):
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400186 if state_name in ["HOST_POWERED_OFF", "HOST_POWERED_ON"]:
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500187 intf = self.getInterface('settings')
188 intf.Set("org.openbmc.settings.Host", "system_state", state_name)
Norman Jamesf5edb9e2016-01-31 13:32:24 -0600189
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500190 if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME,
191 "reboot") == 1):
192 self.powerOn()
Norman James362a80f2015-09-14 14:04:39 -0500193
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500194 def power_button_signal_handler(self):
Adriana Kobylakeeb9b442016-08-15 11:12:53 -0500195 # toggle power, power-on / soft-power-off
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500196 state = self.getPowerState()
197 if state == POWER_OFF:
198 self.powerOn()
199 elif state == POWER_ON:
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400200 self.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500201
Adriana Kobylakeeb9b442016-08-15 11:12:53 -0500202 def long_power_button_signal_handler(self):
203 print "Long-press button, hard power off"
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400204 self.powerOff()
Kenc95eccd2015-12-19 07:02:34 +0800205
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500206 def softreset_button_signal_handler(self):
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400207 self.softReboot()
Norman James72567ba2016-01-13 16:57:48 -0600208
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500209 def host_watchdog_signal_handler(self):
Michael Tritz6d2f6c32017-02-02 17:03:18 -0600210 print "Watchdog Error, Going to quiesce"
211 self.quiesce()
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500212
Yi Li4eab1dc2016-07-02 22:29:45 +0800213 def emergency_shutdown_signal_handler(self, message):
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500214 print "Emergency Shutdown!"
Yi Li4eab1dc2016-07-02 22:29:45 +0800215 # Log an event.
216 try:
217 # Exception happens or not, we need to power off.
218 obj = bus.get_object("org.openbmc.records.events",
219 "/org/openbmc/records/events",
220 introspect=False)
221 intf = dbus.Interface(obj, "org.openbmc.recordlog")
222 desc = message
223 sev = "critical error"
224 details = "Get emergency shutdown signal. Shutdown the host."
225 debug = dbus.ByteArray("")
226 intf.acceptBMCMessage(desc, sev, details, debug)
227 except Exception as e:
228 print "Emergency shutdown signal handler: log event error."
229 print e
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500230 self.powerOff()
231
Norman Jamese2765102015-08-19 22:00:55 -0500232
233if __name__ == '__main__':
234 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
235
Brad Bishop84e73b52016-05-12 15:57:52 -0400236 bus = get_dbus()
Norman James3f97c5d2015-08-26 17:44:14 -0500237 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500238 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400239
240 obj.unmask_signals()
Brad Bishop70852a32016-06-29 22:58:51 -0400241 name = dbus.service.BusName(DBUS_NAME, bus)
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500242
Norman Jamese2765102015-08-19 22:00:55 -0500243 print "Running ChassisControlService"
244 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400245
246# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4