blob: 27705074680a6781730946726e8eef491820e797 [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
Norman James5e792e32015-10-07 17:36:17 -05009import Openbmc
Norman Jamese2765102015-08-19 22:00:55 -050010
Norman James3f97c5d2015-08-26 17:44:14 -050011DBUS_NAME = 'org.openbmc.control.Chassis'
Norman James362a80f2015-09-14 14:04:39 -050012OBJ_NAME = '/org/openbmc/control/'+sys.argv[1]
Norman James3f97c5d2015-08-26 17:44:14 -050013
Norman James2a3d20b2015-08-20 07:09:33 -050014POWER_OFF = 0
15POWER_ON = 1
16
Norman James9e6acf92015-09-08 07:00:04 -050017BOOTED = 100
18
Norman Jamese2765102015-08-19 22:00:55 -050019class ChassisControlObject(dbus.service.Object):
20 def __init__(self,bus,name):
Norman James471ab592015-08-30 22:29:40 -050021 self.dbus_objects = { }
Norman James90baede2015-09-02 20:32:49 -050022
Norman James9e6acf92015-09-08 07:00:04 -050023 dbus.service.Object.__init__(self,bus,name)
Norman James90baede2015-09-02 20:32:49 -050024 ## load utilized objects
Norman James362a80f2015-09-14 14:04:39 -050025 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 James471ab592015-08-30 22:29:40 -050036 }
Norman James362a80f2015-09-14 14:04:39 -050037 #self.power_sequence = 0
Norman James9e6acf92015-09-08 07:00:04 -050038 self.reboot = 0
39 self.last_power_state = 0
Norman James90baede2015-09-02 20:32:49 -050040
Norman James471ab592015-08-30 22:29:40 -050041 bus.add_signal_receiver(self.power_button_signal_handler,
42 dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed",
Norman James362a80f2015-09-14 14:04:39 -050043 path="/org/openbmc/buttons/PowerButton_0" )
Norman James9e6acf92015-09-08 07:00:04 -050044 bus.add_signal_receiver(self.host_watchdog_signal_handler,
45 dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError")
Norman James362a80f2015-09-14 14:04:39 -050046 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James471ab592015-08-30 22:29:40 -050047
Norman James9e6acf92015-09-08 07:00:04 -050048
Norman James362a80f2015-09-14 14:04:39 -050049 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 Jamese2765102015-08-19 22:00:55 -050053
Norman James3f97c5d2015-08-26 17:44:14 -050054 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050055 in_signature='', out_signature='s')
56 def getID(self):
57 return id
58
Norman James3f97c5d2015-08-26 17:44:14 -050059 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050060 in_signature='', out_signature='')
61 def setIdentify(self):
62 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050063 intf = self.getInterface('identify_led')
64 intf.setOn()
Norman Jamese2765102015-08-19 22:00:55 -050065 return None
66
Norman James3f97c5d2015-08-26 17:44:14 -050067 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050068 in_signature='', out_signature='')
69 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050070 print "Turn on identify"
71 intf = self.getInterface('identify_led')
72 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050073 return None
74
Norman James3f97c5d2015-08-26 17:44:14 -050075 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050076 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050077 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -050078 print "Turn on power and boot"
Norman James9e6acf92015-09-08 07:00:04 -050079 self.reboot = 0
Norman Jamese2765102015-08-19 22:00:55 -050080 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -050081 intf = self.getInterface('power_control')
82 intf.setPowerState(POWER_ON)
Norman Jamese2765102015-08-19 22:00:55 -050083 return None
84
Norman James3f97c5d2015-08-26 17:44:14 -050085 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050086 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050087 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -050088 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -050089 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 Jamese2765102015-08-19 22:00:55 -0500106 return None
107
Norman James3f97c5d2015-08-26 17:44:14 -0500108 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500109 in_signature='', out_signature='i')
110 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500111 intf = self.getInterface('power_control')
112 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500113
Norman James3f97c5d2015-08-26 17:44:14 -0500114 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500115 in_signature='', out_signature='')
116 def setDebugMode(self):
117 return None
118
Norman James3f97c5d2015-08-26 17:44:14 -0500119 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500120 in_signature='i', out_signature='')
121 def setPowerPolicy(self,policy):
122 return None
123
Norman James9e6acf92015-09-08 07:00:04 -0500124
Norman Jamese2765102015-08-19 22:00:55 -0500125 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500126
127 def SystemStateHandler(self,state_name):
128 if (state_name == "POWERED_OFF" and self.reboot==1):
129 self.powerOn()
130
131
Norman Jamese2765102015-08-19 22:00:55 -0500132 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500133 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500134 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500135 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500136 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500137 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500138 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500139
140 # TODO: handle long press and reset
141
Norman James9e6acf92015-09-08 07:00:04 -0500142 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500143 print "Watchdog Error, Hard Rebooting"
Norman James9e6acf92015-09-08 07:00:04 -0500144 self.reboot = 1
Norman James362a80f2015-09-14 14:04:39 -0500145 self.powerOff()
Norman James9e6acf92015-09-08 07:00:04 -0500146
Norman Jamese2765102015-08-19 22:00:55 -0500147
148if __name__ == '__main__':
149 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
150
Norman James5e792e32015-10-07 17:36:17 -0500151 bus = Openbmc.getDBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500152 name = dbus.service.BusName(DBUS_NAME, bus)
153 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500154 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500155
Norman Jamese2765102015-08-19 22:00:55 -0500156 print "Running ChassisControlService"
157 mainloop.run()
158