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