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