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): |
| 23 | uuid = ""; |
| 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): |
| 37 | self.dbus_objects = {} |
| 38 | DbusProperties.__init__(self) |
| 39 | DbusObjectManager.__init__(self) |
| 40 | dbus.service.Object.__init__(self, bus, name) |
| 41 | ## load utilized objects |
| 42 | self.dbus_objects = { |
| 43 | 'power_control': { |
| 44 | 'bus_name': 'org.openbmc.control.Power', |
| 45 | 'object_name': '/org/openbmc/control/power0', |
| 46 | 'interface_name': 'org.openbmc.control.Power' |
| 47 | }, |
| 48 | 'identify_led': { |
| 49 | 'bus_name': 'org.openbmc.control.led', |
| 50 | 'object_name': '/org/openbmc/control/led/identify', |
| 51 | 'interface_name': 'org.openbmc.Led' |
| 52 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 53 | 'host_services': { |
| 54 | 'bus_name': 'org.openbmc.HostServices', |
| 55 | 'object_name': '/org/openbmc/HostServices', |
| 56 | 'interface_name': 'org.openbmc.HostServices' |
| 57 | }, |
| 58 | 'settings': { |
| 59 | 'bus_name': 'org.openbmc.settings.Host', |
| 60 | 'object_name': '/org/openbmc/settings/host0', |
| 61 | 'interface_name': 'org.freedesktop.DBus.Properties' |
| 62 | }, |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 63 | 'systemd': { |
| 64 | 'bus_name': 'org.freedesktop.systemd1', |
| 65 | 'object_name': '/org/freedesktop/systemd1', |
| 66 | 'interface_name': 'org.freedesktop.systemd1.Manager' |
Andrew Geissler | 6b63e9a | 2016-06-30 10:45:01 -0500 | [diff] [blame] | 67 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 68 | } |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 69 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 70 | # uuid |
| 71 | self.Set(DBUS_NAME, "uuid", self.getUuid()) |
| 72 | self.Set(DBUS_NAME, "reboot", 0) |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 73 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 74 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 75 | dbus_interface="org.openbmc.Button", |
| 76 | signal_name="Released", |
| 77 | path="/org/openbmc/buttons/power0") |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 78 | bus.add_signal_receiver(self.long_power_button_signal_handler, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 79 | dbus_interface="org.openbmc.Button", |
| 80 | signal_name="PressedLong", |
| 81 | path="/org/openbmc/buttons/power0") |
| 82 | bus.add_signal_receiver(self.softreset_button_signal_handler, |
| 83 | dbus_interface="org.openbmc.Button", |
| 84 | signal_name="Released", |
| 85 | path="/org/openbmc/buttons/reset0") |
| 86 | |
| 87 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 88 | dbus_interface="org.openbmc.Watchdog", |
| 89 | signal_name="WatchdogError") |
| 90 | |
| 91 | bus.add_signal_receiver(self.emergency_shutdown_signal_handler, |
| 92 | dbus_interface="org.openbmc.SensorThresholds", |
| 93 | signal_name="Emergency") |
| 94 | |
| 95 | bus.add_signal_receiver(self.SystemStateHandler, |
| 96 | signal_name="GotoSystemState") |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 97 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 98 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 99 | def getInterface(self, name): |
| 100 | o = self.dbus_objects[name] |
| 101 | obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False) |
| 102 | return dbus.Interface(obj, o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 103 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 104 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 105 | @dbus.service.method(DBUS_NAME, |
| 106 | in_signature='', out_signature='') |
| 107 | def setIdentify(self): |
| 108 | print "Turn on identify" |
| 109 | intf = self.getInterface('identify_led') |
| 110 | intf.setOn() |
| 111 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 112 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 113 | @dbus.service.method(DBUS_NAME, |
| 114 | in_signature='', out_signature='') |
| 115 | def clearIdentify(self): |
| 116 | print "Turn on identify" |
| 117 | intf = self.getInterface('identify_led') |
| 118 | intf.setOff() |
| 119 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -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 powerOn(self): |
| 124 | print "Turn on power and boot" |
| 125 | self.Set(DBUS_NAME, "reboot", 0) |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 126 | intf = self.getInterface('systemd') |
| 127 | f = getattr(intf, 'StartUnit') |
| 128 | f.call_async('obmc-chassis-start@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 129 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 130 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 131 | @dbus.service.method(DBUS_NAME, |
| 132 | in_signature='', out_signature='') |
| 133 | def powerOff(self): |
| 134 | print "Turn off power" |
Brad Bishop | abe0f68 | 2016-08-22 19:27:18 -0400 | [diff] [blame] | 135 | |
| 136 | intf = self.getInterface('systemd') |
| 137 | f = getattr(intf, 'StartUnit') |
| 138 | f.call_async('obmc-chassis-stop@0.target', 'replace') |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 139 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 140 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 141 | @dbus.service.method(DBUS_NAME, |
| 142 | in_signature='', out_signature='') |
| 143 | def softPowerOff(self): |
| 144 | print "Soft off power" |
| 145 | intf = self.getInterface('host_services') |
| 146 | ## host services will call power off when ready |
| 147 | intf.SoftPowerOff() |
| 148 | return None |
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 | @dbus.service.method(DBUS_NAME, |
| 151 | in_signature='', out_signature='') |
| 152 | def reboot(self): |
| 153 | print "Rebooting" |
| 154 | if self.getPowerState() == POWER_OFF: |
| 155 | self.powerOn(); |
| 156 | else: |
| 157 | self.Set(DBUS_NAME, "reboot", 1) |
| 158 | self.powerOff() |
| 159 | return None |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 160 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 161 | @dbus.service.method(DBUS_NAME, |
| 162 | in_signature='', out_signature='') |
| 163 | def softReboot(self): |
| 164 | print "Soft Rebooting" |
| 165 | if self.getPowerState() == POWER_OFF: |
| 166 | self.powerOn(); |
| 167 | else: |
| 168 | self.Set(DBUS_NAME, "reboot", 1) |
| 169 | self.softPowerOff() |
| 170 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 171 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 172 | @dbus.service.method(DBUS_NAME, |
| 173 | in_signature='', out_signature='i') |
| 174 | def getPowerState(self): |
| 175 | intf = self.getInterface('power_control') |
| 176 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 177 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 178 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 179 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 180 | def SystemStateHandler(self, state_name): |
| 181 | if ( |
| 182 | state_name == "HOST_POWERED_OFF" or state_name == "HOST_POWERED_ON"): |
| 183 | intf = self.getInterface('settings') |
| 184 | intf.Set("org.openbmc.settings.Host", "system_state", state_name) |
Norman James | f5edb9e | 2016-01-31 13:32:24 -0600 | [diff] [blame] | 185 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 186 | if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME, |
| 187 | "reboot") == 1): |
| 188 | self.powerOn() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 189 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 190 | def power_button_signal_handler(self): |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 191 | # toggle power, power-on / soft-power-off |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 192 | state = self.getPowerState() |
| 193 | if state == POWER_OFF: |
| 194 | self.powerOn() |
| 195 | elif state == POWER_ON: |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 196 | self.softPowerOff(); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 197 | |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame] | 198 | def long_power_button_signal_handler(self): |
| 199 | print "Long-press button, hard power off" |
| 200 | self.powerOff(); |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 201 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 202 | def softreset_button_signal_handler(self): |
| 203 | self.softReboot(); |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 204 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 205 | def host_watchdog_signal_handler(self): |
| 206 | print "Watchdog Error, Hard Rebooting" |
| 207 | self.Set(DBUS_NAME, "reboot", 1) |
| 208 | self.powerOff() |
| 209 | |
Yi Li | 4eab1dc | 2016-07-02 22:29:45 +0800 | [diff] [blame] | 210 | def emergency_shutdown_signal_handler(self, message): |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 211 | print "Emergency Shutdown!" |
Yi Li | 4eab1dc | 2016-07-02 22:29:45 +0800 | [diff] [blame] | 212 | # Log an event. |
| 213 | try: |
| 214 | # Exception happens or not, we need to power off. |
| 215 | obj = bus.get_object("org.openbmc.records.events", |
| 216 | "/org/openbmc/records/events", |
| 217 | introspect=False) |
| 218 | intf = dbus.Interface(obj, "org.openbmc.recordlog") |
| 219 | desc = message |
| 220 | sev = "critical error" |
| 221 | details = "Get emergency shutdown signal. Shutdown the host." |
| 222 | debug = dbus.ByteArray("") |
| 223 | intf.acceptBMCMessage(desc, sev, details, debug) |
| 224 | except Exception as e: |
| 225 | print "Emergency shutdown signal handler: log event error." |
| 226 | print e |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 227 | self.powerOff() |
| 228 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 229 | |
| 230 | if __name__ == '__main__': |
| 231 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 232 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 233 | bus = get_dbus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 234 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 235 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 236 | |
| 237 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 238 | name = dbus.service.BusName(DBUS_NAME, bus) |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 239 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 240 | print "Running ChassisControlService" |
| 241 | mainloop.run() |
| 242 | |