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