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