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