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 | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 13 | OBJ_NAME = '/org/openbmc/control/'+sys.argv[1] |
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' |
| 37 | } |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 38 | } |
Norman James | 90baede | 2015-09-02 20:32:49 -0500 | [diff] [blame] | 39 | |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 40 | #uuid |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame^] | 41 | self.Set(DBUS_NAME,"uuid",str(uuid.uuid1())) |
| 42 | self.Set(DBUS_NAME,"reboot",0) |
| 43 | self.Set(DBUS_NAME,"power_policy",0) |
| 44 | self.Set(DBUS_NAME,"last_system_state","") |
Adriana Kobylak | 08d3bdb | 2015-10-20 16:59:14 -0500 | [diff] [blame] | 45 | |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 46 | bus.add_signal_receiver(self.power_button_signal_handler, |
| 47 | dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed", |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 48 | path="/org/openbmc/buttons/PowerButton_0" ) |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 49 | bus.add_signal_receiver(self.host_watchdog_signal_handler, |
| 50 | dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError") |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 51 | bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState") |
Norman James | 471ab59 | 2015-08-30 22:29:40 -0500 | [diff] [blame] | 52 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 53 | |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 54 | def getInterface(self,name): |
| 55 | o = self.dbus_objects[name] |
| 56 | obj = bus.get_object(o['bus_name'],o['object_name']) |
| 57 | return dbus.Interface(obj,o['interface_name']) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 58 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 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 | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame^] | 80 | self.Set(DBUS_NAME,"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" |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame^] | 98 | ## TODO: Somehow tell host to shutdown via ipmi |
| 99 | ## for now hard power off |
| 100 | self.powerOff() |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 101 | return None |
| 102 | |
| 103 | @dbus.service.method(DBUS_NAME, |
| 104 | in_signature='', out_signature='') |
| 105 | def reboot(self): |
| 106 | print "Rebooting" |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame^] | 107 | self.Set(DBUS_NAME,"reboot",1) |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 108 | intf.softPowerOff() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 109 | return None |
| 110 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 111 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 112 | in_signature='', out_signature='i') |
| 113 | def getPowerState(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 114 | intf = self.getInterface('power_control') |
| 115 | return intf.getPowerState() |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 116 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 117 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 118 | in_signature='', out_signature='') |
| 119 | def setDebugMode(self): |
| 120 | return None |
| 121 | |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 122 | @dbus.service.method(DBUS_NAME, |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 123 | in_signature='i', out_signature='') |
| 124 | def setPowerPolicy(self,policy): |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame^] | 125 | self.Set(DBUS_NAME,"power_policy",policy) |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 126 | return None |
| 127 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 128 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 129 | ## Signal handler |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 130 | |
| 131 | def SystemStateHandler(self,state_name): |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame^] | 132 | self.Set(DBUS_NAME,"last_system_state",state_name) |
| 133 | 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] | 134 | self.powerOn() |
| 135 | |
| 136 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 137 | def power_button_signal_handler(self): |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 138 | # toggle power |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 139 | state = self.getPowerState() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 140 | if state == POWER_OFF: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 141 | self.powerOn() |
Norman James | 2a3d20b | 2015-08-20 07:09:33 -0500 | [diff] [blame] | 142 | elif state == POWER_ON: |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 143 | self.powerOff(); |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 144 | |
| 145 | # TODO: handle long press and reset |
| 146 | |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 147 | def host_watchdog_signal_handler(self): |
Norman James | 362a80f | 2015-09-14 14:04:39 -0500 | [diff] [blame] | 148 | print "Watchdog Error, Hard Rebooting" |
Norman James | 2656f33 | 2015-10-26 06:42:41 -0500 | [diff] [blame^] | 149 | self.Set(DBUS_NAME,"reboot",1) |
| 150 | self.powerOff() |
Norman James | 9e6acf9 | 2015-09-08 07:00:04 -0500 | [diff] [blame] | 151 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 152 | |
| 153 | if __name__ == '__main__': |
| 154 | dbus.mainloop.glib.DBusGMainLoop(set_as_default=True) |
| 155 | |
Norman James | 5e792e3 | 2015-10-07 17:36:17 -0500 | [diff] [blame] | 156 | bus = Openbmc.getDBus() |
Norman James | 3f97c5d | 2015-08-26 17:44:14 -0500 | [diff] [blame] | 157 | name = dbus.service.BusName(DBUS_NAME, bus) |
| 158 | obj = ChassisControlObject(bus, OBJ_NAME) |
Norman James | 6f8d042 | 2015-09-14 18:48:00 -0500 | [diff] [blame] | 159 | mainloop = gobject.MainLoop() |
Norman James | 81dbd35 | 2015-08-19 22:44:53 -0500 | [diff] [blame] | 160 | |
Norman James | e276510 | 2015-08-19 22:00:55 -0500 | [diff] [blame] | 161 | print "Running ChassisControlService" |
| 162 | mainloop.run() |
| 163 | |