blob: 1956860ce10ce1fb472a9b682e5bfb67bf30a29f [file] [log] [blame]
Norman Jamese2765102015-08-19 22:00:55 -05001#!/usr/bin/env python
2
Norman James471ab592015-08-30 22:29:40 -05003import sys
Norman James6f8d0422015-09-14 18:48:00 -05004#from gi.repository import GObject
5import gobject
Norman Jamese2765102015-08-19 22:00:55 -05006import dbus
7import dbus.service
8import dbus.mainloop.glib
9
Norman James3f97c5d2015-08-26 17:44:14 -050010DBUS_NAME = 'org.openbmc.control.Chassis'
Norman James362a80f2015-09-14 14:04:39 -050011OBJ_NAME = '/org/openbmc/control/'+sys.argv[1]
Norman James3f97c5d2015-08-26 17:44:14 -050012
Norman James2a3d20b2015-08-20 07:09:33 -050013POWER_OFF = 0
14POWER_ON = 1
15
Norman James9e6acf92015-09-08 07:00:04 -050016BOOTED = 100
17
Norman Jamese2765102015-08-19 22:00:55 -050018class ChassisControlObject(dbus.service.Object):
19 def __init__(self,bus,name):
Norman James471ab592015-08-30 22:29:40 -050020 self.dbus_objects = { }
Norman James90baede2015-09-02 20:32:49 -050021
Norman James9e6acf92015-09-08 07:00:04 -050022 dbus.service.Object.__init__(self,bus,name)
Norman James90baede2015-09-02 20:32:49 -050023 ## load utilized objects
Norman James362a80f2015-09-14 14:04:39 -050024 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 James471ab592015-08-30 22:29:40 -050035 }
Norman James362a80f2015-09-14 14:04:39 -050036 #self.power_sequence = 0
Norman James9e6acf92015-09-08 07:00:04 -050037 self.reboot = 0
38 self.last_power_state = 0
Norman James90baede2015-09-02 20:32:49 -050039
Norman James9e6acf92015-09-08 07:00:04 -050040 bus = dbus.SessionBus()
Norman James90baede2015-09-02 20:32:49 -050041
Norman Jamese2765102015-08-19 22:00:55 -050042
Norman James471ab592015-08-30 22:29:40 -050043 bus.add_signal_receiver(self.power_button_signal_handler,
44 dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed",
Norman James362a80f2015-09-14 14:04:39 -050045 path="/org/openbmc/buttons/PowerButton_0" )
Norman James9e6acf92015-09-08 07:00:04 -050046 bus.add_signal_receiver(self.host_watchdog_signal_handler,
47 dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError")
Norman James362a80f2015-09-14 14:04:39 -050048 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James471ab592015-08-30 22:29:40 -050049
Norman James9e6acf92015-09-08 07:00:04 -050050
Norman James362a80f2015-09-14 14:04:39 -050051 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 Jamese2765102015-08-19 22:00:55 -050055
Norman James3f97c5d2015-08-26 17:44:14 -050056 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050057 in_signature='', out_signature='s')
58 def getID(self):
59 return id
60
Norman James3f97c5d2015-08-26 17:44:14 -050061 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050062 in_signature='', out_signature='')
63 def setIdentify(self):
64 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050065 intf = self.getInterface('identify_led')
66 intf.setOn()
Norman Jamese2765102015-08-19 22:00:55 -050067 return None
68
Norman James3f97c5d2015-08-26 17:44:14 -050069 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050070 in_signature='', out_signature='')
71 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050072 print "Turn on identify"
73 intf = self.getInterface('identify_led')
74 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050075 return None
76
Norman James3f97c5d2015-08-26 17:44:14 -050077 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050078 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050079 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -050080 print "Turn on power and boot"
Norman James9e6acf92015-09-08 07:00:04 -050081 self.reboot = 0
Norman Jamese2765102015-08-19 22:00:55 -050082 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -050083 intf = self.getInterface('power_control')
84 intf.setPowerState(POWER_ON)
Norman Jamese2765102015-08-19 22:00:55 -050085 return None
86
Norman James3f97c5d2015-08-26 17:44:14 -050087 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050088 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050089 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -050090 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -050091 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 Jamese2765102015-08-19 22:00:55 -0500108 return None
109
Norman James3f97c5d2015-08-26 17:44:14 -0500110 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500111 in_signature='', out_signature='i')
112 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500113 intf = self.getInterface('power_control')
114 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500115
Norman James3f97c5d2015-08-26 17:44:14 -0500116 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500117 in_signature='', out_signature='')
118 def setDebugMode(self):
119 return None
120
Norman James3f97c5d2015-08-26 17:44:14 -0500121 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500122 in_signature='i', out_signature='')
123 def setPowerPolicy(self,policy):
124 return None
125
Norman James9e6acf92015-09-08 07:00:04 -0500126
Norman Jamese2765102015-08-19 22:00:55 -0500127 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500128
129 def SystemStateHandler(self,state_name):
130 if (state_name == "POWERED_OFF" and self.reboot==1):
131 self.powerOn()
132
133
Norman Jamese2765102015-08-19 22:00:55 -0500134 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500135 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500136 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500137 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500138 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500139 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500140 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500141
142 # TODO: handle long press and reset
143
Norman James9e6acf92015-09-08 07:00:04 -0500144 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500145 print "Watchdog Error, Hard Rebooting"
Norman James9e6acf92015-09-08 07:00:04 -0500146 self.reboot = 1
Norman James362a80f2015-09-14 14:04:39 -0500147 self.powerOff()
Norman James9e6acf92015-09-08 07:00:04 -0500148
Norman Jamese2765102015-08-19 22:00:55 -0500149
150if __name__ == '__main__':
151 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
152
153 bus = dbus.SessionBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500154 name = dbus.service.BusName(DBUS_NAME, bus)
155 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500156 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500157
Norman Jamese2765102015-08-19 22:00:55 -0500158 print "Running ChassisControlService"
159 mainloop.run()
160