blob: 289c6ad17b73285eaa4e49dbfdd7487a8b2d0f84 [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
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -05004import uuid
Norman James6f8d0422015-09-14 18:48:00 -05005#from gi.repository import GObject
6import gobject
Norman Jamese2765102015-08-19 22:00:55 -05007import dbus
8import dbus.service
9import dbus.mainloop.glib
Norman James5e792e32015-10-07 17:36:17 -050010import Openbmc
Norman Jamese2765102015-08-19 22:00:55 -050011
Norman James3f97c5d2015-08-26 17:44:14 -050012DBUS_NAME = 'org.openbmc.control.Chassis'
Norman James362a80f2015-09-14 14:04:39 -050013OBJ_NAME = '/org/openbmc/control/'+sys.argv[1]
Norman Jamesa3e47c42015-10-18 14:43:10 -050014CONTROL_INTF = 'org.openbmc.Control'
Norman James3f97c5d2015-08-26 17:44:14 -050015
Norman James2a3d20b2015-08-20 07:09:33 -050016POWER_OFF = 0
17POWER_ON = 1
18
Norman James9e6acf92015-09-08 07:00:04 -050019BOOTED = 100
20
Norman Jamese2765102015-08-19 22:00:55 -050021class ChassisControlObject(dbus.service.Object):
22 def __init__(self,bus,name):
Norman James471ab592015-08-30 22:29:40 -050023 self.dbus_objects = { }
Norman James90baede2015-09-02 20:32:49 -050024
Norman James9e6acf92015-09-08 07:00:04 -050025 dbus.service.Object.__init__(self,bus,name)
Norman James90baede2015-09-02 20:32:49 -050026 ## load utilized objects
Norman James362a80f2015-09-14 14:04:39 -050027 self.dbus_objects = {
28 'power_control' : {
29 'bus_name' : 'org.openbmc.control.Power',
Norman Jamesa3e47c42015-10-18 14:43:10 -050030 'object_name' : '/org/openbmc/control/power0',
Norman James362a80f2015-09-14 14:04:39 -050031 'interface_name' : 'org.openbmc.control.Power'
32 },
33 'identify_led' : {
Norman Jamesa3e47c42015-10-18 14:43:10 -050034 'bus_name' : 'org.openbmc.control.led',
35 'object_name' : '/org/openbmc/led/IDENTIFY',
Norman James362a80f2015-09-14 14:04:39 -050036 'interface_name' : 'org.openbmc.Led'
37 }
Norman James471ab592015-08-30 22:29:40 -050038 }
Norman James362a80f2015-09-14 14:04:39 -050039 #self.power_sequence = 0
Norman James9e6acf92015-09-08 07:00:04 -050040 self.reboot = 0
41 self.last_power_state = 0
Norman James90baede2015-09-02 20:32:49 -050042
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050043 #uuid
44 self.id = 0
45
Norman James471ab592015-08-30 22:29:40 -050046 bus.add_signal_receiver(self.power_button_signal_handler,
47 dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed",
Norman James362a80f2015-09-14 14:04:39 -050048 path="/org/openbmc/buttons/PowerButton_0" )
Norman James9e6acf92015-09-08 07:00:04 -050049 bus.add_signal_receiver(self.host_watchdog_signal_handler,
50 dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError")
Norman James362a80f2015-09-14 14:04:39 -050051 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James471ab592015-08-30 22:29:40 -050052
Norman James9e6acf92015-09-08 07:00:04 -050053
Norman James362a80f2015-09-14 14:04:39 -050054 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 Jamese2765102015-08-19 22:00:55 -050058
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='s')
61 def getID(self):
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050062 if (self.id==0):
63 #calculate uuuid
64 self.id = uuid.uuid1()
65 return str(self.id)
Norman Jamese2765102015-08-19 22:00:55 -050066
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 setIdentify(self):
70 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050071 intf = self.getInterface('identify_led')
72 intf.setOn()
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='')
77 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050078 print "Turn on identify"
79 intf = self.getInterface('identify_led')
80 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050081 return None
82
Norman James3f97c5d2015-08-26 17:44:14 -050083 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050084 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050085 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -050086 print "Turn on power and boot"
Norman James9e6acf92015-09-08 07:00:04 -050087 self.reboot = 0
Norman Jamese2765102015-08-19 22:00:55 -050088 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -050089 intf = self.getInterface('power_control')
90 intf.setPowerState(POWER_ON)
Norman Jamese2765102015-08-19 22:00:55 -050091 return None
92
Norman James3f97c5d2015-08-26 17:44:14 -050093 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050094 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050095 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -050096 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -050097 intf = self.getInterface('power_control')
98 intf.setPowerState(POWER_OFF)
99 return None
100
101 @dbus.service.method(DBUS_NAME,
102 in_signature='', out_signature='')
103 def softPowerOff(self):
104 print "Soft off power"
105 ## Somehow tell host to shutdown via ipmi
106 return None
107
108 @dbus.service.method(DBUS_NAME,
109 in_signature='', out_signature='')
110 def reboot(self):
111 print "Rebooting"
112 self.reboot=1
113 intf.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500114 return None
115
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='i')
118 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500119 intf = self.getInterface('power_control')
120 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500121
Norman James3f97c5d2015-08-26 17:44:14 -0500122 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500123 in_signature='', out_signature='')
124 def setDebugMode(self):
125 return None
126
Norman James3f97c5d2015-08-26 17:44:14 -0500127 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500128 in_signature='i', out_signature='')
129 def setPowerPolicy(self,policy):
130 return None
131
Norman James9e6acf92015-09-08 07:00:04 -0500132
Norman Jamese2765102015-08-19 22:00:55 -0500133 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500134
135 def SystemStateHandler(self,state_name):
Norman James0e0c7882015-10-19 08:58:22 -0500136 if (state_name == "HOST_POWERED_OFF" and self.reboot==1):
Norman James362a80f2015-09-14 14:04:39 -0500137 self.powerOn()
138
139
Norman Jamese2765102015-08-19 22:00:55 -0500140 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500141 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500142 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500143 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500144 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500145 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500146 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500147
148 # TODO: handle long press and reset
149
Norman James9e6acf92015-09-08 07:00:04 -0500150 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500151 print "Watchdog Error, Hard Rebooting"
Norman James9e6acf92015-09-08 07:00:04 -0500152 self.reboot = 1
Norman James362a80f2015-09-14 14:04:39 -0500153 self.powerOff()
Norman James9e6acf92015-09-08 07:00:04 -0500154
Norman Jamese2765102015-08-19 22:00:55 -0500155
156if __name__ == '__main__':
157 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
158
Norman James5e792e32015-10-07 17:36:17 -0500159 bus = Openbmc.getDBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500160 name = dbus.service.BusName(DBUS_NAME, bus)
161 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500162 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500163
Norman Jamese2765102015-08-19 22:00:55 -0500164 print "Running ChassisControlService"
165 mainloop.run()
166