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 | }, |
| 70 | } |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 71 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 72 | # uuid |
| 73 | self.Set(DBUS_NAME, "uuid", self.getUuid()) |
| 74 | self.Set(DBUS_NAME, "reboot", 0) |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 75 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 76 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 77 | dbus_interface="org.openbmc.Button", |
| 78 | signal_name="Released", |
| 79 | path="/org/openbmc/buttons/power0") |
| 80 | bus.add_signal_receiver(self.reset_button_signal_handler, |
| 81 | dbus_interface="org.openbmc.Button", |
| 82 | signal_name="PressedLong", |
| 83 | path="/org/openbmc/buttons/power0") |
| 84 | bus.add_signal_receiver(self.softreset_button_signal_handler, |
| 85 | dbus_interface="org.openbmc.Button", |
| 86 | signal_name="Released", |
| 87 | path="/org/openbmc/buttons/reset0") |
| 88 | |
| 89 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 90 | dbus_interface="org.openbmc.Watchdog", |
| 91 | signal_name="WatchdogError") |
| 92 | |
| 93 | bus.add_signal_receiver(self.emergency_shutdown_signal_handler, |
| 94 | dbus_interface="org.openbmc.SensorThresholds", |
| 95 | signal_name="Emergency") |
| 96 | |
| 97 | bus.add_signal_receiver(self.SystemStateHandler, |
| 98 | signal_name="GotoSystemState") |
| 99 | self.InterfacesAdded(name, self.properties) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 100 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 101 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 102 | def getInterface(self, name): |
| 103 | o = self.dbus_objects[name] |
| 104 | obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False) |
| 105 | return dbus.Interface(obj, o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 106 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 107 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 108 | @dbus.service.method(DBUS_NAME, |
| 109 | in_signature='', out_signature='') |
| 110 | def setIdentify(self): |
| 111 | print "Turn on identify" |
| 112 | intf = self.getInterface('identify_led') |
| 113 | intf.setOn() |
| 114 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 115 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 116 | @dbus.service.method(DBUS_NAME, |
| 117 | in_signature='', out_signature='') |
| 118 | def clearIdentify(self): |
| 119 | print "Turn on identify" |
| 120 | intf = self.getInterface('identify_led') |
| 121 | intf.setOff() |
| 122 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 123 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 124 | @dbus.service.method(DBUS_NAME, |
| 125 | in_signature='', out_signature='') |
| 126 | def powerOn(self): |
| 127 | print "Turn on power and boot" |
| 128 | self.Set(DBUS_NAME, "reboot", 0) |
| 129 | if (self.getPowerState() == 0): |
| 130 | intf = self.getInterface('power_control') |
| 131 | intf.setPowerState(POWER_ON) |
| 132 | intfwatchdog = self.getInterface('watchdog') |
| 133 | # Start watchdog with 30s timeout per the OpenPower Host IPMI Spec |
| 134 | #Once the host starts booting, it'll reset and refresh the timer |
| 135 | intfwatchdog.set(30000) |
| 136 | intfwatchdog.start() |
| 137 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 138 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 139 | @dbus.service.method(DBUS_NAME, |
| 140 | in_signature='', out_signature='') |
| 141 | def powerOff(self): |
| 142 | print "Turn off power" |
| 143 | intfwatchdog = self.getInterface('watchdog') |
| 144 | intfwatchdog.stop() |
| 145 | intf = self.getInterface('power_control') |
| 146 | intf.setPowerState(POWER_OFF) |
| 147 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 148 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 149 | @dbus.service.method(DBUS_NAME, |
| 150 | in_signature='', out_signature='') |
| 151 | def softPowerOff(self): |
| 152 | print "Soft off power" |
| 153 | intf = self.getInterface('host_services') |
| 154 | ## host services will call power off when ready |
| 155 | intf.SoftPowerOff() |
| 156 | return None |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 157 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 158 | @dbus.service.method(DBUS_NAME, |
| 159 | in_signature='', out_signature='') |
| 160 | def reboot(self): |
| 161 | print "Rebooting" |
| 162 | if self.getPowerState() == POWER_OFF: |
| 163 | self.powerOn(); |
| 164 | else: |
| 165 | self.Set(DBUS_NAME, "reboot", 1) |
| 166 | self.powerOff() |
| 167 | return None |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 168 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 169 | @dbus.service.method(DBUS_NAME, |
| 170 | in_signature='', out_signature='') |
| 171 | def softReboot(self): |
| 172 | print "Soft Rebooting" |
| 173 | if self.getPowerState() == POWER_OFF: |
| 174 | self.powerOn(); |
| 175 | else: |
| 176 | self.Set(DBUS_NAME, "reboot", 1) |
| 177 | self.softPowerOff() |
| 178 | return None |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 179 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 180 | @dbus.service.method(DBUS_NAME, |
| 181 | in_signature='', out_signature='i') |
| 182 | def getPowerState(self): |
| 183 | intf = self.getInterface('power_control') |
| 184 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 185 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 186 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 187 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 188 | def SystemStateHandler(self, state_name): |
| 189 | if ( |
| 190 | state_name == "HOST_POWERED_OFF" or state_name == "HOST_POWERED_ON"): |
| 191 | intf = self.getInterface('settings') |
| 192 | intf.Set("org.openbmc.settings.Host", "system_state", state_name) |
Norman James | f5edb9e | 2016-01-31 13:32:24 -0600 | [diff] [blame] | 193 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 194 | if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME, |
| 195 | "reboot") == 1): |
| 196 | self.powerOn() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 197 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 198 | def power_button_signal_handler(self): |
| 199 | # toggle power |
| 200 | state = self.getPowerState() |
| 201 | if state == POWER_OFF: |
| 202 | self.powerOn() |
| 203 | elif state == POWER_ON: |
| 204 | self.powerOff(); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 205 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 206 | def reset_button_signal_handler(self): |
| 207 | self.reboot(); |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 208 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 209 | def softreset_button_signal_handler(self): |
| 210 | self.softReboot(); |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 211 | |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 212 | def host_watchdog_signal_handler(self): |
| 213 | print "Watchdog Error, Hard Rebooting" |
| 214 | self.Set(DBUS_NAME, "reboot", 1) |
| 215 | self.powerOff() |
| 216 | |
| 217 | def emergency_shutdown_signal_handler(self): |
| 218 | print "Emergency Shutdown!" |
| 219 | self.powerOff() |
| 220 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 221 | |
| 222 | if __name__ == '__main__': |
| 223 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 224 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 225 | bus = get_dbus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 226 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 227 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 228 | mainloop = gobject.MainLoop() |
Andrew Geissler | b7f40b5 | 2016-06-29 15:49:26 -0500 | [diff] [blame^] | 229 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 230 | print "Running ChassisControlService" |
| 231 | mainloop.run() |
| 232 | |