1#!/usr/bin/env python 2 3import gobject 4import dbus 5import dbus.service 6import dbus.mainloop.glib 7from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager 8 9DBUS_NAME = 'org.openbmc.control.Chassis' 10OBJ_NAME = '/org/openbmc/control/chassis0' 11CONTROL_INTF = 'org.openbmc.Control' 12 13MACHINE_ID = '/etc/machine-id' 14 15POWER_OFF = 0 16POWER_ON = 1 17 18BOOTED = 100 19 20 21class ChassisControlObject(DbusProperties, DbusObjectManager): 22 def getUuid(self): 23 uuid = "" 24 try: 25 with open(MACHINE_ID) as f: 26 data = f.readline().rstrip('\n') 27 if (len(data) == 32): 28 uuid = data 29 else: 30 print "ERROR: UUID is not formatted correctly: " + data 31 except: 32 print "ERROR: Unable to open uuid file: " + MACHINE_ID 33 34 return uuid 35 36 def __init__(self, bus, name): 37 super(ChassisControlObject, self).__init__( 38 conn=bus, 39 object_path=name) 40 ## load utilized objects 41 self.dbus_objects = { 42 'power_control': { 43 'bus_name': 'org.openbmc.control.Power', 44 'object_name': '/org/openbmc/control/power0', 45 'interface_name': 'org.openbmc.control.Power' 46 }, 47 'host_services': { 48 'bus_name': 'org.openbmc.HostServices', 49 'object_name': '/org/openbmc/HostServices', 50 'interface_name': 'org.openbmc.HostServices' 51 }, 52 'settings': { 53 'bus_name': 'org.openbmc.settings.Host', 54 'object_name': '/org/openbmc/settings/host0', 55 'interface_name': 'org.freedesktop.DBus.Properties' 56 }, 57 'systemd': { 58 'bus_name': 'org.freedesktop.systemd1', 59 'object_name': '/org/freedesktop/systemd1', 60 'interface_name': 'org.freedesktop.systemd1.Manager' 61 }, 62 } 63 64 # uuid 65 self.Set(DBUS_NAME, "uuid", self.getUuid()) 66 self.Set(DBUS_NAME, "reboot", 0) 67 68 bus.add_signal_receiver(self.power_button_signal_handler, 69 dbus_interface="org.openbmc.Button", 70 signal_name="Released", 71 path="/org/openbmc/buttons/power0") 72 bus.add_signal_receiver(self.long_power_button_signal_handler, 73 dbus_interface="org.openbmc.Button", 74 signal_name="PressedLong", 75 path="/org/openbmc/buttons/power0") 76 bus.add_signal_receiver(self.softreset_button_signal_handler, 77 dbus_interface="org.openbmc.Button", 78 signal_name="Released", 79 path="/org/openbmc/buttons/reset0") 80 81 bus.add_signal_receiver(self.host_watchdog_signal_handler, 82 dbus_interface="org.openbmc.Watchdog", 83 signal_name="WatchdogError") 84 85 bus.add_signal_receiver(self.SystemStateHandler, 86 signal_name="GotoSystemState") 87 88 def getInterface(self, name): 89 o = self.dbus_objects[name] 90 obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False) 91 return dbus.Interface(obj, o['interface_name']) 92 93 @dbus.service.method(DBUS_NAME, 94 in_signature='', out_signature='') 95 def powerOn(self): 96 print "Turn on power and boot" 97 self.Set(DBUS_NAME, "reboot", 0) 98 intf = self.getInterface('systemd') 99 f = getattr(intf, 'StartUnit') 100 f.call_async('obmc-host-start@0.target', 'replace') 101 return None 102 103 @dbus.service.method(DBUS_NAME, 104 in_signature='', out_signature='') 105 def powerOff(self): 106 print "Turn off power" 107 intf = self.getInterface('systemd') 108 f = getattr(intf, 'StartUnit') 109 f.call_async('obmc-chassis-hard-poweroff@0.target', 'replace') 110 return None 111 112 @dbus.service.method(DBUS_NAME, 113 in_signature='', out_signature='') 114 def softPowerOff(self): 115 print "Soft off power" 116 intf = self.getInterface('systemd') 117 f = getattr(intf, 'StartUnit') 118 f.call_async('obmc-host-shutdown@0.target', 'replace') 119 return None 120 121 @dbus.service.method(DBUS_NAME, 122 in_signature='', out_signature='') 123 def reboot(self): 124 print "Rebooting" 125 if self.getPowerState() == POWER_OFF: 126 self.powerOn() 127 else: 128 self.Set(DBUS_NAME, "reboot", 1) 129 self.powerOff() 130 return None 131 132 @dbus.service.method(DBUS_NAME, 133 in_signature='', out_signature='') 134 def softReboot(self): 135 print "Soft Rebooting" 136 if self.getPowerState() == POWER_OFF: 137 self.powerOn() 138 else: 139 self.Set(DBUS_NAME, "reboot", 1) 140 self.softPowerOff() 141 return None 142 143 @dbus.service.method(DBUS_NAME, 144 in_signature='', out_signature='') 145 def quiesce(self): 146 intf = self.getInterface('systemd') 147 f = getattr(intf, 'StartUnit') 148 f.call_async('obmc-host-quiesce@0.target', 'replace') 149 return None 150 151 @dbus.service.method(DBUS_NAME, 152 in_signature='', out_signature='i') 153 def getPowerState(self): 154 intf = self.getInterface('power_control') 155 return intf.getPowerState() 156 157 ## Signal handler 158 159 def SystemStateHandler(self, state_name): 160 if state_name in ["HOST_POWERED_OFF", "HOST_POWERED_ON"]: 161 intf = self.getInterface('settings') 162 intf.Set("org.openbmc.settings.Host", "system_state", state_name) 163 164 if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME, 165 "reboot") == 1): 166 self.powerOn() 167 168 def power_button_signal_handler(self): 169 # toggle power, power-on / soft-power-off 170 state = self.getPowerState() 171 if state == POWER_OFF: 172 self.powerOn() 173 elif state == POWER_ON: 174 self.softPowerOff() 175 176 def long_power_button_signal_handler(self): 177 print "Long-press button, hard power off" 178 self.powerOff() 179 180 def softreset_button_signal_handler(self): 181 self.softReboot() 182 183 def host_watchdog_signal_handler(self): 184 print "Watchdog Error, Going to quiesce" 185 self.quiesce() 186 187 188if __name__ == '__main__': 189 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) 190 191 bus = get_dbus() 192 obj = ChassisControlObject(bus, OBJ_NAME) 193 mainloop = gobject.MainLoop() 194 195 obj.unmask_signals() 196 name = dbus.service.BusName(DBUS_NAME, bus) 197 198 print "Running ChassisControlService" 199 mainloop.run() 200 201# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4 202