Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | import gobject |
| 4 | import dbus |
| 5 | import dbus.service |
| 6 | import dbus.mainloop.glib |
| 7 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 8 | DBUS_NAME = 'org.openbmc.control.Chassis' |
| 9 | OBJ_NAME = '/org/openbmc/control/Chassis' |
| 10 | |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 11 | POWER_OFF = 0 |
| 12 | POWER_ON = 1 |
| 13 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 14 | class ChassisControlObject(dbus.service.Object): |
| 15 | def __init__(self,bus,name): |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 16 | self.power_sequence = 0 |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 17 | dbus.service.Object.__init__(self,bus,name) |
| 18 | bus = dbus.SessionBus() |
| 19 | try: |
| 20 | # Get PowerControl object |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 21 | power_control_service = bus.get_object('org.openbmc.control.Power','/org/openbmc/control/Power/0') |
| 22 | self.power_control_iface = dbus.Interface(power_control_service, 'org.openbmc.control.Power') |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 23 | # Get ChassisIdentify object |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 24 | chassis_identify_service = bus.get_object('org.openbmc.leds.ChassisIdentify','/org/openbmc/leds/ChassisIdentify/0') |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 25 | self.identify_led_iface = dbus.Interface(chassis_identify_service, 'org.openbmc.Led'); |
| 26 | # Get HostControl object |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 27 | host_control_service = bus.get_object('org.openbmc.control.Host','/org/openbmc/control/Host/0') |
| 28 | self.host_control_iface = dbus.Interface(host_control_service, 'org.openbmc.control.Host'); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 29 | |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 30 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 31 | dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed") |
| 32 | bus.add_signal_receiver(self.power_good_signal_handler, |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 33 | dbus_interface = "org.openbmc.control.Power", signal_name = "PowerGood") |
Norman James | 81dbd35 | 2015-08-19 22:44:53 -0500 | [diff] [blame] | 34 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 35 | |
| 36 | except dbus.exceptions.DBusException, e: |
| 37 | # TODO: not sure what to do if can't find other services |
| 38 | print "Unable to find dependent services: ",e |
| 39 | |
| 40 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 41 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 42 | in_signature='', out_signature='s') |
| 43 | def getID(self): |
| 44 | return id |
| 45 | |
| 46 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 47 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 48 | in_signature='', out_signature='') |
| 49 | def setIdentify(self): |
| 50 | print "Turn on identify" |
| 51 | self.identify_led_iface.setOn() |
| 52 | return None |
| 53 | |
| 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='') |
| 57 | def clearIdentify(self): |
| 58 | print "Turn off identify" |
| 59 | r=self.identify_led_iface.setOff() |
| 60 | return None |
| 61 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 62 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 63 | in_signature='', out_signature='') |
| 64 | def setPowerOn(self): |
| 65 | print "Turn on power and boot" |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 66 | self.power_sequence = 0 |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 67 | if (self.getPowerState()==0): |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 68 | self.power_control_iface.setPowerState(POWER_ON) |
| 69 | self.power_sequence = 1 |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 70 | return None |
| 71 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 72 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 73 | in_signature='', out_signature='') |
| 74 | def setPowerOff(self): |
| 75 | print "Turn off power" |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 76 | self.power_control_iface.setPowerState(POWER_OFF); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 77 | return None |
| 78 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 79 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 80 | in_signature='', out_signature='i') |
| 81 | def getPowerState(self): |
| 82 | state = self.power_control_iface.getPowerState(); |
| 83 | return state |
| 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='') |
| 87 | def setDebugMode(self): |
| 88 | return None |
| 89 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 90 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 91 | in_signature='i', out_signature='') |
| 92 | def setPowerPolicy(self,policy): |
| 93 | return None |
| 94 | |
| 95 | ## Signal handler |
| 96 | def power_button_signal_handler(self): |
| 97 | # only power on if not currently powered on |
| 98 | state = self.getPowerState() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 99 | if state == POWER_OFF: |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 100 | self.setPowerOn() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 101 | elif state == POWER_ON: |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 102 | self.setPowerOff(); |
| 103 | |
| 104 | # TODO: handle long press and reset |
| 105 | |
| 106 | ## Signal handler |
| 107 | def power_good_signal_handler(self): |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 108 | if (self.power_sequence==1): |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 109 | self.host_control_iface.boot() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 110 | self.power_sequence = 2 |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 111 | |
| 112 | |
| 113 | |
| 114 | if __name__ == '__main__': |
| 115 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 116 | |
| 117 | bus = dbus.SessionBus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 118 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 119 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 120 | mainloop = gobject.MainLoop() |
Norman James | 81dbd35 | 2015-08-19 22:44:53 -0500 | [diff] [blame] | 121 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 122 | print "Running ChassisControlService" |
| 123 | mainloop.run() |
| 124 | |