Norman James | 42c1be8 | 2015-10-22 14:34:26 -0500 | [diff] [blame] | 1 | #!/usr/bin/python -u |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 2 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 3 | import sys |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 4 | import uuid |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 5 | import gobject |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 6 | import dbus |
| 7 | import dbus.service |
| 8 | import dbus.mainloop.glib |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 9 | from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 10 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 11 | DBUS_NAME = 'org.openbmc.control.Chassis' |
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 12 | OBJ_NAME = '/org/openbmc/control/chassis0' |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 13 | CONTROL_INTF = 'org.openbmc.Control' |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 14 | |
Norman James | ad8c3d3 | 2016-02-11 15:19:01 -0600 | [diff] [blame] | 15 | MACHINE_ID = '/etc/machine-id' |
| 16 | |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 17 | POWER_OFF = 0 |
| 18 | POWER_ON = 1 |
| 19 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 20 | BOOTED = 100 |
| 21 | |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 22 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 23 | class ChassisControlObject(DbusProperties, DbusObjectManager): |
| 24 | def getUuid(self): |
| 25 | uuid = ""; |
| 26 | try: |
| 27 | with open(MACHINE_ID) as f: |
| 28 | data = f.readline().rstrip('\n') |
| 29 | if (len(data) == 32): |
| 30 | uuid = data |
| 31 | else: |
| 32 | print "ERROR: UUID is not formatted correctly: " + data |
| 33 | except: |
| 34 | print "ERROR: Unable to open uuid file: " + MACHINE_ID |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 35 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 36 | return uuid |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 37 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 38 | def __init__(self, bus, name): |
| 39 | self.dbus_objects = {} |
| 40 | DbusProperties.__init__(self) |
| 41 | DbusObjectManager.__init__(self) |
| 42 | dbus.service.Object.__init__(self, bus, name) |
| 43 | ## load utilized objects |
| 44 | self.dbus_objects = { |
| 45 | 'power_control': { |
| 46 | 'bus_name': 'org.openbmc.control.Power', |
| 47 | 'object_name': '/org/openbmc/control/power0', |
| 48 | 'interface_name': 'org.openbmc.control.Power' |
| 49 | }, |
| 50 | 'identify_led': { |
| 51 | 'bus_name': 'org.openbmc.control.led', |
| 52 | 'object_name': '/org/openbmc/control/led/identify', |
| 53 | 'interface_name': 'org.openbmc.Led' |
| 54 | }, |
| 55 | 'watchdog': { |
| 56 | 'bus_name': 'org.openbmc.watchdog.Host', |
| 57 | 'object_name': '/org/openbmc/watchdog/host0', |
| 58 | 'interface_name': 'org.openbmc.Watchdog' |
| 59 | }, |
| 60 | 'host_services': { |
| 61 | 'bus_name': 'org.openbmc.HostServices', |
| 62 | 'object_name': '/org/openbmc/HostServices', |
| 63 | 'interface_name': 'org.openbmc.HostServices' |
| 64 | }, |
| 65 | 'settings': { |
| 66 | 'bus_name': 'org.openbmc.settings.Host', |
| 67 | 'object_name': '/org/openbmc/settings/host0', |
| 68 | 'interface_name': 'org.freedesktop.DBus.Properties' |
| 69 | }, |
Andrew Geissler | 6b63e9a | 2016-06-30 10:45:01 -0500 | [diff] [blame] | 70 | 'host_control': { |
| 71 | 'bus_name': 'org.openbmc.control.Host', |
| 72 | 'object_name': '/org/openbmc/control/host0', |
| 73 | 'interface_name': 'org.openbmc.control.Host' |
| 74 | }, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 75 | } |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 76 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 77 | # uuid |
| 78 | self.Set(DBUS_NAME, "uuid", self.getUuid()) |
| 79 | self.Set(DBUS_NAME, "reboot", 0) |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 80 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 81 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 82 | dbus_interface="org.openbmc.Button", |
| 83 | signal_name="Released", |
| 84 | path="/org/openbmc/buttons/power0") |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame^] | 85 | bus.add_signal_receiver(self.long_power_button_signal_handler, |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 86 | dbus_interface="org.openbmc.Button", |
| 87 | signal_name="PressedLong", |
| 88 | path="/org/openbmc/buttons/power0") |
| 89 | bus.add_signal_receiver(self.softreset_button_signal_handler, |
| 90 | dbus_interface="org.openbmc.Button", |
| 91 | signal_name="Released", |
| 92 | path="/org/openbmc/buttons/reset0") |
| 93 | |
| 94 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 95 | dbus_interface="org.openbmc.Watchdog", |
| 96 | signal_name="WatchdogError") |
| 97 | |
| 98 | bus.add_signal_receiver(self.emergency_shutdown_signal_handler, |
| 99 | dbus_interface="org.openbmc.SensorThresholds", |
| 100 | signal_name="Emergency") |
| 101 | |
| 102 | bus.add_signal_receiver(self.SystemStateHandler, |
| 103 | signal_name="GotoSystemState") |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 104 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 105 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 106 | def getInterface(self, name): |
| 107 | o = self.dbus_objects[name] |
| 108 | obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False) |
| 109 | return dbus.Interface(obj, o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 110 | |
Norman James | e276510 | 2015-08-19 22:00:55 -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 setIdentify(self): |
| 115 | print "Turn on identify" |
| 116 | intf = self.getInterface('identify_led') |
| 117 | intf.setOn() |
| 118 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 119 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 120 | @dbus.service.method(DBUS_NAME, |
| 121 | in_signature='', out_signature='') |
| 122 | def clearIdentify(self): |
| 123 | print "Turn on identify" |
| 124 | intf = self.getInterface('identify_led') |
| 125 | intf.setOff() |
| 126 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 127 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 128 | @dbus.service.method(DBUS_NAME, |
| 129 | in_signature='', out_signature='') |
| 130 | def powerOn(self): |
| 131 | print "Turn on power and boot" |
| 132 | self.Set(DBUS_NAME, "reboot", 0) |
| 133 | if (self.getPowerState() == 0): |
| 134 | intf = self.getInterface('power_control') |
| 135 | intf.setPowerState(POWER_ON) |
Andrew Geissler | 6b63e9a | 2016-06-30 10:45:01 -0500 | [diff] [blame] | 136 | |
| 137 | # Determine if debug_mode is set. If it is then we don't |
| 138 | # want to start the watchdog since debug mode |
| 139 | intfcontrol = self.getInterface('host_control') |
| 140 | intfproperties = dbus.Interface(intfcontrol, |
| 141 | "org.freedesktop.DBus.Properties") |
| 142 | debug_mode = intfproperties.Get('org.openbmc.control.Host', |
| 143 | 'debug_mode') |
| 144 | if(not debug_mode): |
| 145 | intfwatchdog = self.getInterface('watchdog') |
| 146 | # Start watchdog with 30s timeout per the OpenPower Host IPMI Spec |
| 147 | #Once the host starts booting, it'll reset and refresh the timer |
| 148 | intfwatchdog.set(30000) |
| 149 | intfwatchdog.start() |
| 150 | else: |
| 151 | print "Debug mode is on, no watchdog" |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 152 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 153 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 154 | @dbus.service.method(DBUS_NAME, |
| 155 | in_signature='', out_signature='') |
| 156 | def powerOff(self): |
| 157 | print "Turn off power" |
| 158 | intfwatchdog = self.getInterface('watchdog') |
| 159 | intfwatchdog.stop() |
| 160 | intf = self.getInterface('power_control') |
| 161 | intf.setPowerState(POWER_OFF) |
| 162 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 163 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 164 | @dbus.service.method(DBUS_NAME, |
| 165 | in_signature='', out_signature='') |
| 166 | def softPowerOff(self): |
| 167 | print "Soft off power" |
| 168 | intf = self.getInterface('host_services') |
| 169 | ## host services will call power off when ready |
| 170 | intf.SoftPowerOff() |
| 171 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 172 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 173 | @dbus.service.method(DBUS_NAME, |
| 174 | in_signature='', out_signature='') |
| 175 | def reboot(self): |
| 176 | print "Rebooting" |
| 177 | if self.getPowerState() == POWER_OFF: |
| 178 | self.powerOn(); |
| 179 | else: |
| 180 | self.Set(DBUS_NAME, "reboot", 1) |
| 181 | self.powerOff() |
| 182 | return None |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 183 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 184 | @dbus.service.method(DBUS_NAME, |
| 185 | in_signature='', out_signature='') |
| 186 | def softReboot(self): |
| 187 | print "Soft Rebooting" |
| 188 | if self.getPowerState() == POWER_OFF: |
| 189 | self.powerOn(); |
| 190 | else: |
| 191 | self.Set(DBUS_NAME, "reboot", 1) |
| 192 | self.softPowerOff() |
| 193 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 194 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 195 | @dbus.service.method(DBUS_NAME, |
| 196 | in_signature='', out_signature='i') |
| 197 | def getPowerState(self): |
| 198 | intf = self.getInterface('power_control') |
| 199 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 200 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 201 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 202 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 203 | def SystemStateHandler(self, state_name): |
| 204 | if ( |
| 205 | state_name == "HOST_POWERED_OFF" or state_name == "HOST_POWERED_ON"): |
| 206 | intf = self.getInterface('settings') |
| 207 | intf.Set("org.openbmc.settings.Host", "system_state", state_name) |
Norman James | f5edb9e | 2016-01-31 13:32:24 -0600 | [diff] [blame] | 208 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 209 | if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME, |
| 210 | "reboot") == 1): |
| 211 | self.powerOn() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 212 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 213 | def power_button_signal_handler(self): |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame^] | 214 | # toggle power, power-on / soft-power-off |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 215 | state = self.getPowerState() |
| 216 | if state == POWER_OFF: |
| 217 | self.powerOn() |
| 218 | elif state == POWER_ON: |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame^] | 219 | self.softPowerOff(); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 220 | |
Adriana Kobylak | eeb9b44 | 2016-08-15 11:12:53 -0500 | [diff] [blame^] | 221 | def long_power_button_signal_handler(self): |
| 222 | print "Long-press button, hard power off" |
| 223 | self.powerOff(); |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 224 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 225 | def softreset_button_signal_handler(self): |
| 226 | self.softReboot(); |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 227 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 228 | def host_watchdog_signal_handler(self): |
| 229 | print "Watchdog Error, Hard Rebooting" |
| 230 | self.Set(DBUS_NAME, "reboot", 1) |
| 231 | self.powerOff() |
| 232 | |
Yi Li | 4eab1dc | 2016-07-02 22:29:45 +0800 | [diff] [blame] | 233 | def emergency_shutdown_signal_handler(self, message): |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 234 | print "Emergency Shutdown!" |
Yi Li | 4eab1dc | 2016-07-02 22:29:45 +0800 | [diff] [blame] | 235 | # Log an event. |
| 236 | try: |
| 237 | # Exception happens or not, we need to power off. |
| 238 | obj = bus.get_object("org.openbmc.records.events", |
| 239 | "/org/openbmc/records/events", |
| 240 | introspect=False) |
| 241 | intf = dbus.Interface(obj, "org.openbmc.recordlog") |
| 242 | desc = message |
| 243 | sev = "critical error" |
| 244 | details = "Get emergency shutdown signal. Shutdown the host." |
| 245 | debug = dbus.ByteArray("") |
| 246 | intf.acceptBMCMessage(desc, sev, details, debug) |
| 247 | except Exception as e: |
| 248 | print "Emergency shutdown signal handler: log event error." |
| 249 | print e |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 250 | self.powerOff() |
| 251 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 252 | |
| 253 | if __name__ == '__main__': |
| 254 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 255 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 256 | bus = get_dbus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 257 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 258 | mainloop = gobject.MainLoop() |
Brad Bishop | f0f3efe | 2016-06-29 23:20:24 -0400 | [diff] [blame] | 259 | |
| 260 | obj.unmask_signals() |
Brad Bishop | 70852a3 | 2016-06-29 22:58:51 -0400 | [diff] [blame] | 261 | name = dbus.service.BusName(DBUS_NAME, bus) |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame] | 262 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 263 | print "Running ChassisControlService" |
| 264 | mainloop.run() |
| 265 | |