Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 3 | import sys |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 4 | #from gi.repository import GObject |
| 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 |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 9 | import Openbmc |
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 | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 12 | OBJ_NAME = '/org/openbmc/control/'+sys.argv[1] |
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 | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 15 | POWER_OFF = 0 |
| 16 | POWER_ON = 1 |
| 17 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 18 | BOOTED = 100 |
| 19 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 20 | class ChassisControlObject(dbus.service.Object): |
| 21 | def __init__(self,bus,name): |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 22 | self.dbus_objects = { } |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 23 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 24 | dbus.service.Object.__init__(self,bus,name) |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 25 | ## load utilized objects |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 26 | self.dbus_objects = { |
| 27 | 'power_control' : { |
| 28 | 'bus_name' : 'org.openbmc.control.Power', |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 29 | 'object_name' : '/org/openbmc/control/power0', |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 30 | 'interface_name' : 'org.openbmc.control.Power' |
| 31 | }, |
| 32 | 'identify_led' : { |
Norman James | a3e47c4 | 2015-10-18 14:43:10 -0500 | [diff] [blame] | 33 | 'bus_name' : 'org.openbmc.control.led', |
| 34 | 'object_name' : '/org/openbmc/led/IDENTIFY', |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 35 | 'interface_name' : 'org.openbmc.Led' |
| 36 | } |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 37 | } |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 38 | #self.power_sequence = 0 |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 39 | self.reboot = 0 |
| 40 | self.last_power_state = 0 |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 41 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 42 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 43 | dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed", |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 44 | path="/org/openbmc/buttons/PowerButton_0" ) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 45 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 46 | dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError") |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 47 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 48 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 49 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 50 | def getInterface(self,name): |
| 51 | o = self.dbus_objects[name] |
| 52 | obj = bus.get_object(o['bus_name'],o['object_name']) |
| 53 | return dbus.Interface(obj,o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 54 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 55 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 56 | in_signature='', out_signature='s') |
| 57 | def getID(self): |
| 58 | return id |
| 59 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 60 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 61 | in_signature='', out_signature='') |
| 62 | def setIdentify(self): |
| 63 | print "Turn on identify" |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 64 | intf = self.getInterface('identify_led') |
| 65 | intf.setOn() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 66 | return None |
| 67 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 68 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 69 | in_signature='', out_signature='') |
| 70 | def clearIdentify(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 71 | print "Turn on identify" |
| 72 | intf = self.getInterface('identify_led') |
| 73 | intf.setOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 74 | return None |
| 75 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 76 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 77 | in_signature='', out_signature='') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 78 | def powerOn(self): |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 79 | print "Turn on power and boot" |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 80 | self.reboot = 0 |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 81 | if (self.getPowerState()==0): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 82 | intf = self.getInterface('power_control') |
| 83 | intf.setPowerState(POWER_ON) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 84 | return None |
| 85 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 86 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 87 | in_signature='', out_signature='') |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 88 | def powerOff(self): |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 89 | print "Turn off power" |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 90 | intf = self.getInterface('power_control') |
| 91 | intf.setPowerState(POWER_OFF) |
| 92 | return None |
| 93 | |
| 94 | @dbus.service.method(DBUS_NAME, |
| 95 | in_signature='', out_signature='') |
| 96 | def softPowerOff(self): |
| 97 | print "Soft off power" |
| 98 | ## Somehow tell host to shutdown via ipmi |
| 99 | return None |
| 100 | |
| 101 | @dbus.service.method(DBUS_NAME, |
| 102 | in_signature='', out_signature='') |
| 103 | def reboot(self): |
| 104 | print "Rebooting" |
| 105 | self.reboot=1 |
| 106 | intf.softPowerOff() |
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='i') |
| 111 | def getPowerState(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 112 | intf = self.getInterface('power_control') |
| 113 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 114 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 115 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 116 | in_signature='', out_signature='') |
| 117 | def setDebugMode(self): |
| 118 | return None |
| 119 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 120 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 121 | in_signature='i', out_signature='') |
| 122 | def setPowerPolicy(self,policy): |
| 123 | return None |
| 124 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 125 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 126 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 127 | |
| 128 | def SystemStateHandler(self,state_name): |
Norman James | 0e0c788 | 2015-10-19 08:58:22 -0500 | [diff] [blame] | 129 | if (state_name == "HOST_POWERED_OFF" and self.reboot==1): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 130 | self.powerOn() |
| 131 | |
| 132 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 133 | def power_button_signal_handler(self): |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 134 | # toggle power |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 135 | state = self.getPowerState() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 136 | if state == POWER_OFF: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 137 | self.powerOn() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 138 | elif state == POWER_ON: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 139 | self.powerOff(); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 140 | |
| 141 | # TODO: handle long press and reset |
| 142 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 143 | def host_watchdog_signal_handler(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 144 | print "Watchdog Error, Hard Rebooting" |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 145 | self.reboot = 1 |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 146 | self.powerOff() |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 147 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 148 | |
| 149 | if __name__ == '__main__': |
| 150 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 151 | |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 152 | bus = Openbmc.getDBus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 153 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 154 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 155 | mainloop = gobject.MainLoop() |
Norman James | 81dbd35 | 2015-08-19 22:44:53 -0500 | [diff] [blame] | 156 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 157 | print "Running ChassisControlService" |
| 158 | mainloop.run() |
| 159 | |