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