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 | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 21 | class ChassisControlObject(Openbmc.DbusProperties): |
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 | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 25 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 26 | ## load utilized objects |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 27 | self.dbus_objects = { |
| 28 | 'power_control' : { |
| 29 | 'bus_name' : 'org.openbmc.control.Power', |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 30 | 'object_name' : '/org/openbmc/control/power0', |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 31 | 'interface_name' : 'org.openbmc.control.Power' |
| 32 | }, |
| 33 | 'identify_led' : { |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 34 | 'bus_name' : 'org.openbmc.control.led', |
| 35 | 'object_name' : '/org/openbmc/led/IDENTIFY', |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 36 | 'interface_name' : 'org.openbmc.Led' |
Norman James | c07c473 | 2015-10-26 07:12:58 -0500 | [diff] [blame] | 37 | }, |
| 38 | 'watchdog' : { |
| 39 | 'bus_name' : 'org.openbmc.watchdog.Host', |
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 40 | 'object_name' : '/org/openbmc/watchdog/host0', |
Norman James | c07c473 | 2015-10-26 07:12:58 -0500 | [diff] [blame] | 41 | 'interface_name' : 'org.openbmc.Watchdog' |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 42 | } |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 43 | } |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 44 | |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 45 | #uuid |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 46 | self.Set(DBUS_NAME,"uuid",str(uuid.uuid1())) |
| 47 | self.Set(DBUS_NAME,"reboot",0) |
| 48 | self.Set(DBUS_NAME,"power_policy",0) |
| 49 | self.Set(DBUS_NAME,"last_system_state","") |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 50 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 51 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 52 | dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed", |
Norman James | 8fee6f2 | 2015-10-28 12:48:43 -0500 | [diff] [blame] | 53 | path="/org/openbmc/buttons/power0" ) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 54 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 55 | dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError") |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 56 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 57 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 58 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 59 | def getInterface(self,name): |
| 60 | o = self.dbus_objects[name] |
| 61 | obj = bus.get_object(o['bus_name'],o['object_name']) |
| 62 | return dbus.Interface(obj,o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 63 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 64 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 65 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 66 | in_signature='', out_signature='') |
| 67 | def setIdentify(self): |
| 68 | print "Turn on identify" |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 69 | intf = self.getInterface('identify_led') |
| 70 | intf.setOn() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 71 | return None |
| 72 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 73 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 74 | in_signature='', out_signature='') |
| 75 | def clearIdentify(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 76 | print "Turn on identify" |
| 77 | intf = self.getInterface('identify_led') |
| 78 | intf.setOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 79 | return None |
| 80 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 81 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 82 | in_signature='', out_signature='') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 83 | def powerOn(self): |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 84 | print "Turn on power and boot" |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 85 | self.Set(DBUS_NAME,"reboot",0) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 86 | if (self.getPowerState()==0): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 87 | intf = self.getInterface('power_control') |
| 88 | intf.setPowerState(POWER_ON) |
Norman James | c07c473 | 2015-10-26 07:12:58 -0500 | [diff] [blame] | 89 | intfwatchdog = self.getInterface('watchdog') |
Adriana Kobylak | 025d13f | 2015-10-22 12:45:24 -0500 | [diff] [blame] | 90 | #Start watchdog with 30s timeout per the OpenPower Host IPMI Spec |
| 91 | #Once the host starts booting, it'll reset and refresh the timer |
| 92 | intfwatchdog.set(30000) |
| 93 | intfwatchdog.start() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 94 | return None |
| 95 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 96 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 97 | in_signature='', out_signature='') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 98 | def powerOff(self): |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 99 | print "Turn off power" |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 100 | intf = self.getInterface('power_control') |
| 101 | intf.setPowerState(POWER_OFF) |
| 102 | return None |
| 103 | |
| 104 | @dbus.service.method(DBUS_NAME, |
| 105 | in_signature='', out_signature='') |
| 106 | def softPowerOff(self): |
| 107 | print "Soft off power" |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 108 | ## TODO: Somehow tell host to shutdown via ipmi |
| 109 | ## for now hard power off |
| 110 | self.powerOff() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 111 | return None |
| 112 | |
| 113 | @dbus.service.method(DBUS_NAME, |
| 114 | in_signature='', out_signature='') |
| 115 | def reboot(self): |
| 116 | print "Rebooting" |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 117 | self.Set(DBUS_NAME,"reboot",1) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 118 | intf.softPowerOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 119 | return None |
| 120 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 121 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 122 | in_signature='', out_signature='i') |
| 123 | def getPowerState(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 124 | intf = self.getInterface('power_control') |
| 125 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 126 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 127 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 128 | in_signature='', out_signature='') |
| 129 | def setDebugMode(self): |
| 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='i', out_signature='') |
| 134 | def setPowerPolicy(self,policy): |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 135 | self.Set(DBUS_NAME,"power_policy",policy) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 136 | return None |
| 137 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 138 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 139 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 140 | |
| 141 | def SystemStateHandler(self,state_name): |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame] | 142 | self.Set(DBUS_NAME,"last_system_state",state_name) |
| 143 | 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] | 144 | self.powerOn() |
| 145 | |
| 146 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 147 | def power_button_signal_handler(self): |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 148 | # toggle power |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 149 | state = self.getPowerState() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 150 | if state == POWER_OFF: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 151 | self.powerOn() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 152 | elif state == POWER_ON: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 153 | self.powerOff(); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 154 | |
| 155 | # TODO: handle long press and reset |
| 156 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 157 | def host_watchdog_signal_handler(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 158 | print "Watchdog Error, Hard Rebooting" |
Norman James | c07c473 | 2015-10-26 07:12:58 -0500 | [diff] [blame] | 159 | #self.Set(DBUS_NAME,"reboot",1) |
| 160 | #self.powerOff() |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 161 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 162 | |
| 163 | if __name__ == '__main__': |
| 164 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 165 | |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 166 | bus = Openbmc.getDBus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 167 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 168 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 169 | mainloop = gobject.MainLoop() |
Norman James | 81dbd35 | 2015-08-19 22:44:53 -0500 | [diff] [blame] | 170 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 171 | print "Running ChassisControlService" |
| 172 | mainloop.run() |
| 173 | |