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 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 22 | class ChassisControlObject(DbusProperties,DbusObjectManager): |
Norman James | ad8c3d3 | 2016-02-11 15:19:01 -0600 | [diff] [blame] | 23 | def getUuid(self): |
| 24 | uuid = ""; |
| 25 | try: |
| 26 | with open(MACHINE_ID) as f: |
| 27 | data = f.readline().rstrip('\n') |
| 28 | if (len(data) == 32): |
| 29 | uuid = data |
| 30 | else: |
| 31 | print "ERROR: UUID is not formatted correctly: "+data |
| 32 | except: |
| 33 | print "ERROR: Unable to open uuid file: "+MACHINE_ID |
| 34 | |
| 35 | return uuid |
| 36 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 37 | def __init__(self,bus,name): |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 38 | self.dbus_objects = { } |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 39 | DbusProperties.__init__(self) |
| 40 | DbusObjectManager.__init__(self) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 41 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 42 | ## load utilized objects |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 43 | self.dbus_objects = { |
| 44 | 'power_control' : { |
| 45 | 'bus_name' : 'org.openbmc.control.Power', |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 46 | 'object_name' : '/org/openbmc/control/power0', |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 47 | 'interface_name' : 'org.openbmc.control.Power' |
| 48 | }, |
| 49 | 'identify_led' : { |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 50 | 'bus_name' : 'org.openbmc.control.led', |
Adriana Kobylak | 9c75104 | 2016-02-09 13:44:32 -0600 | [diff] [blame] | 51 | 'object_name' : '/org/openbmc/control/led/identify', |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 52 | 'interface_name' : 'org.openbmc.Led' |
Norman James | c07c473 | 2015-10-26 07:12:58 -0500 | [diff] [blame] | 53 | }, |
| 54 | 'watchdog' : { |
| 55 | 'bus_name' : 'org.openbmc.watchdog.Host', |
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 56 | 'object_name' : '/org/openbmc/watchdog/host0', |
Norman James | c07c473 | 2015-10-26 07:12:58 -0500 | [diff] [blame] | 57 | 'interface_name' : 'org.openbmc.Watchdog' |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 58 | }, |
| 59 | 'host_services' : { |
| 60 | 'bus_name' : 'org.openbmc.HostServices', |
| 61 | 'object_name' : '/org/openbmc/HostServices', |
| 62 | 'interface_name' : 'org.openbmc.HostServices' |
| 63 | }, |
Norman James | f5edb9e | 2016-01-31 13:32:24 -0600 | [diff] [blame] | 64 | 'settings' : { |
| 65 | 'bus_name' : 'org.openbmc.settings.Host', |
| 66 | 'object_name' : '/org/openbmc/settings/host0', |
| 67 | 'interface_name' : 'org.freedesktop.DBus.Properties' |
| 68 | }, |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 69 | } |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 70 | |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 71 | #uuid |
Norman James | ad8c3d3 | 2016-02-11 15:19:01 -0600 | [diff] [blame] | 72 | self.Set(DBUS_NAME,"uuid",self.getUuid()) |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 73 | self.Set(DBUS_NAME,"reboot",0) |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 74 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 75 | bus.add_signal_receiver(self.power_button_signal_handler, |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 76 | dbus_interface = "org.openbmc.Button", signal_name = "Released", |
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 77 | path="/org/openbmc/buttons/power0" ) |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 78 | bus.add_signal_receiver(self.reset_button_signal_handler, |
Norman James | 807ed1f | 2015-11-09 10:53:03 -0600 | [diff] [blame] | 79 | dbus_interface = "org.openbmc.Button", signal_name = "PressedLong", |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 80 | path="/org/openbmc/buttons/power0" ) |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 81 | bus.add_signal_receiver(self.softreset_button_signal_handler, |
| 82 | dbus_interface = "org.openbmc.Button", signal_name = "Released", |
| 83 | path="/org/openbmc/buttons/reset0" ) |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 84 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 85 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 86 | dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError") |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 87 | |
| 88 | bus.add_signal_receiver(self.emergency_shutdown_signal_handler, |
| 89 | dbus_interface = "org.openbmc.SensorThresholds", signal_name = "Emergency") |
| 90 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 91 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 323ed97 | 2015-12-09 09:06:37 -0600 | [diff] [blame] | 92 | self.InterfacesAdded(name,self.properties) |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 93 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 94 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 95 | def getInterface(self,name): |
| 96 | o = self.dbus_objects[name] |
Norman James | 85f050b | 2015-12-18 14:58:20 -0600 | [diff] [blame] | 97 | obj = bus.get_object(o['bus_name'],o['object_name'],introspect=False) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 98 | return dbus.Interface(obj,o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 99 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 100 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 101 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 102 | in_signature='', out_signature='') |
| 103 | def setIdentify(self): |
| 104 | print "Turn on identify" |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 105 | intf = self.getInterface('identify_led') |
| 106 | intf.setOn() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 107 | return None |
| 108 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 109 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 110 | in_signature='', out_signature='') |
| 111 | def clearIdentify(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 112 | print "Turn on identify" |
| 113 | intf = self.getInterface('identify_led') |
| 114 | intf.setOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 115 | return None |
| 116 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 117 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 118 | in_signature='', out_signature='') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 119 | def powerOn(self): |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 120 | print "Turn on power and boot" |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 121 | self.Set(DBUS_NAME,"reboot",0) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 122 | if (self.getPowerState()==0): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 123 | intf = self.getInterface('power_control') |
| 124 | intf.setPowerState(POWER_ON) |
Norman James | c07c473 | 2015-10-26 07:12:58 -0500 | [diff] [blame] | 125 | intfwatchdog = self.getInterface('watchdog') |
Adriana Kobylak | 025d13f | 2015-10-22 12:45:24 -0500 | [diff] [blame] | 126 | #Start watchdog with 30s timeout per the OpenPower Host IPMI Spec |
| 127 | #Once the host starts booting, it'll reset and refresh the timer |
| 128 | intfwatchdog.set(30000) |
| 129 | intfwatchdog.start() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 130 | return None |
| 131 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 132 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 133 | in_signature='', out_signature='') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 134 | def powerOff(self): |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 135 | print "Turn off power" |
Adriana Kobylak | 5f7fe66 | 2016-02-04 12:39:06 -0600 | [diff] [blame] | 136 | intfwatchdog = self.getInterface('watchdog') |
| 137 | intfwatchdog.stop() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 138 | intf = self.getInterface('power_control') |
| 139 | intf.setPowerState(POWER_OFF) |
| 140 | return None |
| 141 | |
| 142 | @dbus.service.method(DBUS_NAME, |
| 143 | in_signature='', out_signature='') |
| 144 | def softPowerOff(self): |
| 145 | print "Soft off power" |
Norman James | 98e1f7b | 2015-11-24 22:17:56 -0600 | [diff] [blame] | 146 | intf = self.getInterface('host_services') |
| 147 | ## host services will call power off when ready |
| 148 | intf.SoftPowerOff() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 149 | return None |
| 150 | |
| 151 | @dbus.service.method(DBUS_NAME, |
| 152 | in_signature='', out_signature='') |
| 153 | def reboot(self): |
| 154 | print "Rebooting" |
Norman James | 8d2e3ef | 2015-11-17 19:34:25 -0600 | [diff] [blame] | 155 | if self.getPowerState() == POWER_OFF: |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 156 | self.powerOn(); |
| 157 | else: |
| 158 | self.Set(DBUS_NAME,"reboot",1) |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 159 | self.powerOff() |
| 160 | return None |
| 161 | |
Norman James | b4bd9e2 | 2015-12-18 15:47:50 -0600 | [diff] [blame] | 162 | @dbus.service.method(DBUS_NAME, |
| 163 | in_signature='', out_signature='') |
Norman James | b4ef318 | 2015-12-03 17:54:35 -0600 | [diff] [blame] | 164 | def softReboot(self): |
| 165 | print "Soft Rebooting" |
| 166 | if self.getPowerState() == POWER_OFF: |
| 167 | self.powerOn(); |
| 168 | else: |
| 169 | self.Set(DBUS_NAME,"reboot",1) |
Norman James | 8d2e3ef | 2015-11-17 19:34:25 -0600 | [diff] [blame] | 170 | self.softPowerOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 171 | return None |
| 172 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 173 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 174 | in_signature='', out_signature='i') |
| 175 | def getPowerState(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 176 | intf = self.getInterface('power_control') |
| 177 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 178 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 179 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 180 | |
| 181 | def SystemStateHandler(self,state_name): |
Norman James | f5edb9e | 2016-01-31 13:32:24 -0600 | [diff] [blame] | 182 | if (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) |
| 185 | |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 186 | if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME,"reboot")==1): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 187 | self.powerOn() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 188 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 189 | def power_button_signal_handler(self): |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 190 | # toggle power |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 191 | state = self.getPowerState() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 192 | if state == POWER_OFF: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 193 | self.powerOn() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 194 | elif state == POWER_ON: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 195 | self.powerOff(); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 196 | |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 197 | def reset_button_signal_handler(self): |
| 198 | self.reboot(); |
Ken | c95eccd | 2015-12-19 07:02:34 +0800 | [diff] [blame] | 199 | |
| 200 | def softreset_button_signal_handler(self): |
| 201 | self.softReboot(); |
Norman James | cfc2b44 | 2015-10-31 17:31:46 -0500 | [diff] [blame] | 202 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 203 | def host_watchdog_signal_handler(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 204 | print "Watchdog Error, Hard Rebooting" |
Adriana Kobylak | dbc8105 | 2016-02-08 15:48:21 -0600 | [diff] [blame] | 205 | self.Set(DBUS_NAME,"reboot",1) |
| 206 | self.powerOff() |
Norman James | 72567ba | 2016-01-13 16:57:48 -0600 | [diff] [blame] | 207 | |
| 208 | def emergency_shutdown_signal_handler(self): |
| 209 | print "Emergency Shutdown!" |
| 210 | self.powerOff() |
| 211 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 212 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 213 | |
| 214 | if __name__ == '__main__': |
| 215 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 216 | |
Brad Bishop | 84e73b5 | 2016-05-12 15:57:52 -0400 | [diff] [blame] | 217 | bus = get_dbus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 218 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 219 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 220 | mainloop = gobject.MainLoop() |
Norman James | 81dbd35 | 2015-08-19 22:44:53 -0500 | [diff] [blame] | 221 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 222 | print "Running ChassisControlService" |
| 223 | mainloop.run() |
| 224 | |