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 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 47 | 'host_services': { |
| 48 | 'bus_name': 'org.openbmc.HostServices', |
| 49 | 'object_name': '/org/openbmc/HostServices', |
| 50 | 'interface_name': 'org.openbmc.HostServices' |
| 51 | }, |
| 52 | 'settings': { |
| 53 | 'bus_name': 'org.openbmc.settings.Host', |
| 54 | 'object_name': '/org/openbmc/settings/host0', |
| 55 | 'interface_name': 'org.freedesktop.DBus.Properties' |
| 56 | }, |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 57 | 'systemd': { |
| 58 | 'bus_name': 'org.freedesktop.systemd1', |
| 59 | 'object_name': '/org/freedesktop/systemd1', |
| 60 | 'interface_name': 'org.freedesktop.systemd1.Manager' |
Andrew Geissler | 6b63e9a | 2016-06-30 10:45:01 -0500 | [diff] [blame] | 61 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 62 | } |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 63 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 64 | # uuid |
| 65 | self.Set(DBUS_NAME, "uuid", self.getUuid()) |
| 66 | self.Set(DBUS_NAME, "reboot", 0) |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 67 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 68 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 69 | dbus_interface="org.openbmc.Button", |
| 70 | signal_name="Released", |
| 71 | path="/org/openbmc/buttons/power0") |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 72 | bus.add_signal_receiver(self.long_power_button_signal_handler, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 73 | dbus_interface="org.openbmc.Button", |
| 74 | signal_name="PressedLong", |
| 75 | path="/org/openbmc/buttons/power0") |
| 76 | bus.add_signal_receiver(self.softreset_button_signal_handler, |
| 77 | dbus_interface="org.openbmc.Button", |
| 78 | signal_name="Released", |
| 79 | path="/org/openbmc/buttons/reset0") |
| 80 | |
| 81 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 82 | dbus_interface="org.openbmc.Watchdog", |
| 83 | signal_name="WatchdogError") |
| 84 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 85 | bus.add_signal_receiver(self.SystemStateHandler, |
| 86 | signal_name="GotoSystemState") |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 87 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 88 | def getInterface(self, name): |
| 89 | o = self.dbus_objects[name] |
| 90 | obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False) |
| 91 | return dbus.Interface(obj, o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 92 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 93 | @dbus.service.method(DBUS_NAME, |
| 94 | in_signature='', out_signature='') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 95 | def powerOn(self): |
| 96 | print "Turn on power and boot" |
| 97 | self.Set(DBUS_NAME, "reboot", 0) |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 98 | intf = self.getInterface('systemd') |
| 99 | f = getattr(intf, 'StartUnit') |
Josh D. King | 2a55954 | 2017-04-11 13:48:00 -0500 | [diff] [blame] | 100 | f.call_async('obmc-host-start@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 101 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 102 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 103 | @dbus.service.method(DBUS_NAME, |
| 104 | in_signature='', out_signature='') |
| 105 | def powerOff(self): |
| 106 | print "Turn off power" |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 107 | intf = self.getInterface('systemd') |
| 108 | f = getattr(intf, 'StartUnit') |
Andrew Geissler | 9903ec6 | 2017-07-30 09:36:33 -0500 | [diff] [blame] | 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 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 112 | @dbus.service.method(DBUS_NAME, |
| 113 | in_signature='', out_signature='') |
| 114 | def softPowerOff(self): |
| 115 | print "Soft off power" |
Andrew Geissler | 9903ec6 | 2017-07-30 09:36:33 -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 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 121 | @dbus.service.method(DBUS_NAME, |
| 122 | in_signature='', out_signature='') |
| 123 | def reboot(self): |
| 124 | print "Rebooting" |
| 125 | if self.getPowerState() == POWER_OFF: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 126 | self.powerOn() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 127 | else: |
| 128 | self.Set(DBUS_NAME, "reboot", 1) |
| 129 | self.powerOff() |
| 130 | return None |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 131 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 132 | @dbus.service.method(DBUS_NAME, |
| 133 | in_signature='', out_signature='') |
| 134 | def softReboot(self): |
| 135 | print "Soft Rebooting" |
| 136 | if self.getPowerState() == POWER_OFF: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 137 | self.powerOn() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 138 | else: |
| 139 | self.Set(DBUS_NAME, "reboot", 1) |
| 140 | self.softPowerOff() |
| 141 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 142 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 143 | @dbus.service.method(DBUS_NAME, |
Michael Tritz | 6d2f6c3 | 2017-02-02 17:03:18 -0600 | [diff] [blame] | 144 | in_signature='', out_signature='') |
| 145 | def quiesce(self): |
| 146 | intf = self.getInterface('systemd') |
| 147 | f = getattr(intf, 'StartUnit') |
Josh D. King | e02cffd | 2017-04-11 13:53:11 -0500 | [diff] [blame] | 148 | f.call_async('obmc-host-quiesce@0.target', 'replace') |
Michael Tritz | 6d2f6c3 | 2017-02-02 17:03:18 -0600 | [diff] [blame] | 149 | return None |
| 150 | |
| 151 | @dbus.service.method(DBUS_NAME, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 152 | in_signature='', out_signature='i') |
| 153 | def getPowerState(self): |
| 154 | intf = self.getInterface('power_control') |
| 155 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 156 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 157 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 158 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 159 | def SystemStateHandler(self, state_name): |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 160 | if state_name in ["HOST_POWERED_OFF", "HOST_POWERED_ON"]: |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 161 | intf = self.getInterface('settings') |
| 162 | intf.Set("org.openbmc.settings.Host", "system_state", state_name) |
Norman James | f5edb9e | 2016-01-31 13:32:24 -0600 | [diff] [blame] | 163 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 164 | if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME, |
| 165 | "reboot") == 1): |
| 166 | self.powerOn() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 167 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 168 | def power_button_signal_handler(self): |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 169 | # toggle power, power-on / soft-power-off |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 170 | state = self.getPowerState() |
| 171 | if state == POWER_OFF: |
| 172 | self.powerOn() |
| 173 | elif state == POWER_ON: |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 174 | self.softPowerOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 175 | |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 176 | def long_power_button_signal_handler(self): |
| 177 | print "Long-press button, hard power off" |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 178 | self.powerOff() |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 179 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 180 | def softreset_button_signal_handler(self): |
Brad Bishop | 7a4d77a | 2016-09-08 23:26:17 -0400 | [diff] [blame] | 181 | self.softReboot() |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 182 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 183 | def host_watchdog_signal_handler(self): |
Michael Tritz | 6d2f6c3 | 2017-02-02 17:03:18 -0600 | [diff] [blame] | 184 | print "Watchdog Error, Going to quiesce" |
| 185 | self.quiesce() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 186 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 187 | |
| 188 | if __name__ == '__main__': |
| 189 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 190 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 191 | bus = get_dbus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 192 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 193 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 194 | |
| 195 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 196 | name = dbus.service.BusName(DBUS_NAME, bus) |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 197 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 198 | print "Running ChassisControlService" |
| 199 | mainloop.run() |
Brad Bishop | 5306675 | 2016-09-21 08:48:04 -0400 | [diff] [blame] | 200 | |
| 201 | # vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 |