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 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 3 | # TODO: openbmc/openbmc#2994 remove python 2 support |
| 4 | try: # python 2 |
| 5 | import gobject |
| 6 | except ImportError: # python 3 |
| 7 | from gi.repository import GObject as gobject |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 8 | import dbus |
| 9 | import dbus.service |
| 10 | import dbus.mainloop.glib |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 11 | from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 12 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 13 | DBUS_NAME = 'org.openbmc.control.Chassis' |
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 14 | OBJ_NAME = '/org/openbmc/control/chassis0' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 15 | CONTROL_INTF = 'org.openbmc.Control' |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 16 | |
Norman James | ad8c3d3 | 2016-02-11 15:19:01 -0600 | [diff] [blame] | 17 | MACHINE_ID = '/etc/machine-id' |
| 18 | |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 19 | POWER_OFF = 0 |
| 20 | POWER_ON = 1 |
| 21 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 22 | BOOTED = 100 |
| 23 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 24 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 25 | class ChassisControlObject(DbusProperties, DbusObjectManager): |
| 26 | def getUuid(self): |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 27 | uuid = "" |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 28 | try: |
| 29 | with open(MACHINE_ID) as f: |
| 30 | data = f.readline().rstrip('\n') |
| 31 | if (len(data) == 32): |
| 32 | uuid = data |
| 33 | else: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 34 | print("ERROR: UUID is not formatted correctly: " + data) |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 35 | except Exception: |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 36 | print("ERROR: Unable to open uuid file: " + MACHINE_ID) |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 37 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 38 | return uuid |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 39 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 40 | def __init__(self, bus, name): |
Brad Bishop | f47f5fa | 2016-09-08 22:29:01 -0400 | [diff] [blame] | 41 | super(ChassisControlObject, self).__init__( |
| 42 | conn=bus, |
| 43 | object_path=name) |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 44 | # load utilized objects |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 45 | self.dbus_objects = { |
| 46 | 'power_control': { |
| 47 | 'bus_name': 'org.openbmc.control.Power', |
| 48 | 'object_name': '/org/openbmc/control/power0', |
| 49 | 'interface_name': 'org.openbmc.control.Power' |
| 50 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 51 | 'host_services': { |
| 52 | 'bus_name': 'org.openbmc.HostServices', |
| 53 | 'object_name': '/org/openbmc/HostServices', |
| 54 | 'interface_name': 'org.openbmc.HostServices' |
| 55 | }, |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 56 | 'systemd': { |
| 57 | 'bus_name': 'org.freedesktop.systemd1', |
| 58 | 'object_name': '/org/freedesktop/systemd1', |
| 59 | 'interface_name': 'org.freedesktop.systemd1.Manager' |
Andrew Geissler | 6b63e9a | 2016-06-30 10:45:01 -0500 | [diff] [blame] | 60 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 61 | } |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 62 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 63 | # uuid |
| 64 | self.Set(DBUS_NAME, "uuid", self.getUuid()) |
| 65 | self.Set(DBUS_NAME, "reboot", 0) |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 66 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 67 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 68 | dbus_interface="org.openbmc.Button", |
| 69 | signal_name="Released", |
| 70 | path="/org/openbmc/buttons/power0") |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 71 | bus.add_signal_receiver(self.long_power_button_signal_handler, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 72 | dbus_interface="org.openbmc.Button", |
| 73 | signal_name="PressedLong", |
| 74 | path="/org/openbmc/buttons/power0") |
| 75 | bus.add_signal_receiver(self.softreset_button_signal_handler, |
| 76 | dbus_interface="org.openbmc.Button", |
| 77 | signal_name="Released", |
| 78 | path="/org/openbmc/buttons/reset0") |
| 79 | |
| 80 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 81 | dbus_interface="org.openbmc.Watchdog", |
| 82 | signal_name="WatchdogError") |
| 83 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 84 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 85 | def getInterface(self, name): |
| 86 | o = self.dbus_objects[name] |
| 87 | obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False) |
| 88 | return dbus.Interface(obj, o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 89 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 90 | @dbus.service.method(DBUS_NAME, |
| 91 | in_signature='', out_signature='') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 92 | def powerOn(self): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 93 | print("Turn on power and boot") |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 94 | self.Set(DBUS_NAME, "reboot", 0) |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 95 | intf = self.getInterface('systemd') |
| 96 | f = getattr(intf, 'StartUnit') |
Josh D. King | 2a55954 | 2017-04-11 13:48:00 -0500 | [diff] [blame] | 97 | f.call_async('obmc-host-start@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 98 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 99 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 100 | @dbus.service.method(DBUS_NAME, |
| 101 | in_signature='', out_signature='') |
| 102 | def powerOff(self): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 103 | print("Turn off power") |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 104 | intf = self.getInterface('systemd') |
| 105 | f = getattr(intf, 'StartUnit') |
Andrew Geissler | 9903ec6 | 2017-07-30 09:36:33 -0500 | [diff] [blame] | 106 | f.call_async('obmc-chassis-hard-poweroff@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 107 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 108 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 109 | @dbus.service.method(DBUS_NAME, |
| 110 | in_signature='', out_signature='') |
| 111 | def softPowerOff(self): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 112 | print("Soft off power") |
Andrew Geissler | 9903ec6 | 2017-07-30 09:36:33 -0500 | [diff] [blame] | 113 | intf = self.getInterface('systemd') |
| 114 | f = getattr(intf, 'StartUnit') |
| 115 | f.call_async('obmc-host-shutdown@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 116 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -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 reboot(self): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 121 | print("Rebooting") |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 122 | if self.getPowerState() == POWER_OFF: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 123 | self.powerOn() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 124 | else: |
| 125 | self.Set(DBUS_NAME, "reboot", 1) |
| 126 | self.powerOff() |
| 127 | return None |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 128 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 129 | @dbus.service.method(DBUS_NAME, |
| 130 | in_signature='', out_signature='') |
| 131 | def softReboot(self): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 132 | print("Soft Rebooting") |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 133 | if self.getPowerState() == POWER_OFF: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 134 | self.powerOn() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 135 | else: |
| 136 | self.Set(DBUS_NAME, "reboot", 1) |
| 137 | self.softPowerOff() |
| 138 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 139 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 140 | @dbus.service.method(DBUS_NAME, |
Michael Tritz | 6d2f6c3 | 2017-02-02 17:03:18 -0600 | [diff] [blame] | 141 | in_signature='', out_signature='') |
| 142 | def quiesce(self): |
| 143 | intf = self.getInterface('systemd') |
| 144 | f = getattr(intf, 'StartUnit') |
Josh D. King | e02cffd | 2017-04-11 13:53:11 -0500 | [diff] [blame] | 145 | f.call_async('obmc-host-quiesce@0.target', 'replace') |
Michael Tritz | 6d2f6c3 | 2017-02-02 17:03:18 -0600 | [diff] [blame] | 146 | return None |
| 147 | |
| 148 | @dbus.service.method(DBUS_NAME, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 149 | in_signature='', out_signature='i') |
| 150 | def getPowerState(self): |
| 151 | intf = self.getInterface('power_control') |
| 152 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 153 | |
Adriana Kobylak | 24341f9 | 2018-01-26 15:07:23 -0600 | [diff] [blame] | 154 | # Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 155 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 156 | def power_button_signal_handler(self): |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 157 | # toggle power, power-on / soft-power-off |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 158 | state = self.getPowerState() |
| 159 | if state == POWER_OFF: |
| 160 | self.powerOn() |
| 161 | elif state == POWER_ON: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 162 | self.softPowerOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 163 | |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 164 | def long_power_button_signal_handler(self): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 165 | print("Long-press button, hard power off") |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 166 | self.powerOff() |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 167 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 168 | def softreset_button_signal_handler(self): |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 169 | self.softReboot() |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 170 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 171 | def host_watchdog_signal_handler(self): |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 172 | print("Watchdog Error, Going to quiesce") |
Michael Tritz | 6d2f6c3 | 2017-02-02 17:03:18 -0600 | [diff] [blame] | 173 | self.quiesce() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 174 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 175 | |
| 176 | if __name__ == '__main__': |
| 177 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 178 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 179 | bus = get_dbus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 180 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 181 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 182 | |
| 183 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 184 | name = dbus.service.BusName(DBUS_NAME, bus) |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 185 | |
CamVan Nguyen | d65b2d5 | 2018-02-27 15:14:41 -0600 | [diff] [blame] | 186 | print("Running ChassisControlService") |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 187 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 188 | |
| 189 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |