Brad Bishop | 87c6b73 | 2016-08-30 19:17:57 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 2 | |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 3 | import gobject |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 4 | import dbus |
| 5 | import dbus.service |
| 6 | import dbus.mainloop.glib |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 7 | from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 8 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 9 | DBUS_NAME = 'org.openbmc.control.Chassis' |
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 10 | OBJ_NAME = '/org/openbmc/control/chassis0' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 11 | CONTROL_INTF = 'org.openbmc.Control' |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 12 | |
Norman James | ad8c3d3 | 2016-02-11 15:19:01 -0600 | [diff] [blame] | 13 | MACHINE_ID = '/etc/machine-id' |
| 14 | |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 15 | POWER_OFF = 0 |
| 16 | POWER_ON = 1 |
| 17 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 18 | BOOTED = 100 |
| 19 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 20 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 21 | class ChassisControlObject(DbusProperties, DbusObjectManager): |
| 22 | def getUuid(self): |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 23 | uuid = "" |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 24 | 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 Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 33 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 34 | return uuid |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 35 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 36 | def __init__(self, bus, name): |
Brad Bishop | f47f5fa | 2016-09-08 22:29:01 -0400 | [diff] [blame] | 37 | super(ChassisControlObject, self).__init__( |
| 38 | conn=bus, |
| 39 | object_path=name) |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 40 | ## 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 Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 52 | '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 Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 62 | 'systemd': { |
| 63 | 'bus_name': 'org.freedesktop.systemd1', |
| 64 | 'object_name': '/org/freedesktop/systemd1', |
| 65 | 'interface_name': 'org.freedesktop.systemd1.Manager' |
Andrew Geissler | 6b63e9a | 2016-06-30 10:45:01 -0500 | [diff] [blame] | 66 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 67 | } |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 68 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 69 | # uuid |
| 70 | self.Set(DBUS_NAME, "uuid", self.getUuid()) |
| 71 | self.Set(DBUS_NAME, "reboot", 0) |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 72 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 73 | 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 Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 77 | bus.add_signal_receiver(self.long_power_button_signal_handler, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 78 | 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 James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 96 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 97 | 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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 101 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 102 | @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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 109 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 110 | @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 James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 117 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 118 | @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 Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 123 | intf = self.getInterface('systemd') |
| 124 | f = getattr(intf, 'StartUnit') |
| 125 | f.call_async('obmc-chassis-start@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 126 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 127 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 128 | @dbus.service.method(DBUS_NAME, |
| 129 | in_signature='', out_signature='') |
| 130 | def powerOff(self): |
| 131 | print "Turn off power" |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 132 | |
| 133 | intf = self.getInterface('systemd') |
| 134 | f = getattr(intf, 'StartUnit') |
| 135 | f.call_async('obmc-chassis-stop@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 136 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 137 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 138 | @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 James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 146 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 147 | @dbus.service.method(DBUS_NAME, |
| 148 | in_signature='', out_signature='') |
| 149 | def reboot(self): |
| 150 | print "Rebooting" |
| 151 | if self.getPowerState() == POWER_OFF: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 152 | self.powerOn() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 153 | else: |
| 154 | self.Set(DBUS_NAME, "reboot", 1) |
| 155 | self.powerOff() |
| 156 | return None |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 157 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 158 | @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 Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 163 | self.powerOn() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 164 | else: |
| 165 | self.Set(DBUS_NAME, "reboot", 1) |
| 166 | self.softPowerOff() |
| 167 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 168 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 169 | @dbus.service.method(DBUS_NAME, |
| 170 | in_signature='', out_signature='i') |
| 171 | def getPowerState(self): |
| 172 | intf = self.getInterface('power_control') |
| 173 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 174 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 175 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 176 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 177 | def SystemStateHandler(self, state_name): |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 178 | if state_name in ["HOST_POWERED_OFF", "HOST_POWERED_ON"]: |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 179 | intf = self.getInterface('settings') |
| 180 | intf.Set("org.openbmc.settings.Host", "system_state", state_name) |
Norman James | f5edb9e | 2016-01-31 13:32:24 -0600 | [diff] [blame] | 181 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 182 | if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME, |
| 183 | "reboot") == 1): |
| 184 | self.powerOn() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 185 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 186 | def power_button_signal_handler(self): |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 187 | # toggle power, power-on / soft-power-off |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 188 | state = self.getPowerState() |
| 189 | if state == POWER_OFF: |
| 190 | self.powerOn() |
| 191 | elif state == POWER_ON: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 192 | self.softPowerOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 193 | |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 194 | def long_power_button_signal_handler(self): |
| 195 | print "Long-press button, hard power off" |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 196 | self.powerOff() |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 197 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 198 | def softreset_button_signal_handler(self): |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 199 | self.softReboot() |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 200 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 201 | def host_watchdog_signal_handler(self): |
| 202 | print "Watchdog Error, Hard Rebooting" |
| 203 | self.Set(DBUS_NAME, "reboot", 1) |
| 204 | self.powerOff() |
| 205 | |
Yi Li | 4eab1dc | 2016-07-02 22:29:45 +0800 | [diff] [blame] | 206 | def emergency_shutdown_signal_handler(self, message): |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 207 | print "Emergency Shutdown!" |
Yi Li | 4eab1dc | 2016-07-02 22:29:45 +0800 | [diff] [blame] | 208 | # Log an event. |
| 209 | try: |
| 210 | # Exception happens or not, we need to power off. |
| 211 | obj = bus.get_object("org.openbmc.records.events", |
| 212 | "/org/openbmc/records/events", |
| 213 | introspect=False) |
| 214 | intf = dbus.Interface(obj, "org.openbmc.recordlog") |
| 215 | desc = message |
| 216 | sev = "critical error" |
| 217 | details = "Get emergency shutdown signal. Shutdown the host." |
| 218 | debug = dbus.ByteArray("") |
| 219 | intf.acceptBMCMessage(desc, sev, details, debug) |
| 220 | except Exception as e: |
| 221 | print "Emergency shutdown signal handler: log event error." |
| 222 | print e |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 223 | self.powerOff() |
| 224 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 225 | |
| 226 | if __name__ == '__main__': |
| 227 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 228 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 229 | bus = get_dbus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 230 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 231 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 232 | |
| 233 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 234 | name = dbus.service.BusName(DBUS_NAME, bus) |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 235 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 236 | print "Running ChassisControlService" |
| 237 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 238 | |
| 239 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |