blob: d11ab407dbacb40840bbf82dfa96829f983ca3b6 [file] [log] [blame]
Norman Jamese2765102015-08-19 22:00:55 -05001#!/usr/bin/env python
2
3import gobject
4import dbus
5import dbus.service
6import dbus.mainloop.glib
7
Norman James3f97c5d2015-08-26 17:44:14 -05008DBUS_NAME = 'org.openbmc.control.Chassis'
9OBJ_NAME = '/org/openbmc/control/Chassis'
10
Norman James2a3d20b2015-08-20 07:09:33 -050011POWER_OFF = 0
12POWER_ON = 1
13
Norman Jamese2765102015-08-19 22:00:55 -050014class ChassisControlObject(dbus.service.Object):
15 def __init__(self,bus,name):
Norman James2a3d20b2015-08-20 07:09:33 -050016 self.power_sequence = 0
Norman Jamese2765102015-08-19 22:00:55 -050017 dbus.service.Object.__init__(self,bus,name)
18 bus = dbus.SessionBus()
19 try:
20 # Get PowerControl object
Norman James3f97c5d2015-08-26 17:44:14 -050021 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 Jamese2765102015-08-19 22:00:55 -050023 # Get ChassisIdentify object
Norman James3f97c5d2015-08-26 17:44:14 -050024 chassis_identify_service = bus.get_object('org.openbmc.leds.ChassisIdentify','/org/openbmc/leds/ChassisIdentify/0')
Norman Jamese2765102015-08-19 22:00:55 -050025 self.identify_led_iface = dbus.Interface(chassis_identify_service, 'org.openbmc.Led');
26 # Get HostControl object
Norman James3f97c5d2015-08-26 17:44:14 -050027 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 Jamese2765102015-08-19 22:00:55 -050029
Norman James2a3d20b2015-08-20 07:09:33 -050030 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 James3f97c5d2015-08-26 17:44:14 -050033 dbus_interface = "org.openbmc.control.Power", signal_name = "PowerGood")
Norman James81dbd352015-08-19 22:44:53 -050034
Norman Jamese2765102015-08-19 22:00:55 -050035
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 James3f97c5d2015-08-26 17:44:14 -050041 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050042 in_signature='', out_signature='s')
43 def getID(self):
44 return id
45
46
Norman James3f97c5d2015-08-26 17:44:14 -050047 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050048 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 James3f97c5d2015-08-26 17:44:14 -050055 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050056 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 James3f97c5d2015-08-26 17:44:14 -050062 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050063 in_signature='', out_signature='')
64 def setPowerOn(self):
65 print "Turn on power and boot"
Norman James2a3d20b2015-08-20 07:09:33 -050066 self.power_sequence = 0
Norman Jamese2765102015-08-19 22:00:55 -050067 if (self.getPowerState()==0):
Norman James2a3d20b2015-08-20 07:09:33 -050068 self.power_control_iface.setPowerState(POWER_ON)
69 self.power_sequence = 1
Norman Jamese2765102015-08-19 22:00:55 -050070 return None
71
Norman James3f97c5d2015-08-26 17:44:14 -050072 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050073 in_signature='', out_signature='')
74 def setPowerOff(self):
75 print "Turn off power"
Norman James2a3d20b2015-08-20 07:09:33 -050076 self.power_control_iface.setPowerState(POWER_OFF);
Norman Jamese2765102015-08-19 22:00:55 -050077 return None
78
Norman James3f97c5d2015-08-26 17:44:14 -050079 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050080 in_signature='', out_signature='i')
81 def getPowerState(self):
82 state = self.power_control_iface.getPowerState();
83 return state
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='')
87 def setDebugMode(self):
88 return None
89
Norman James3f97c5d2015-08-26 17:44:14 -050090 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050091 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 James2a3d20b2015-08-20 07:09:33 -050099 if state == POWER_OFF:
Norman Jamese2765102015-08-19 22:00:55 -0500100 self.setPowerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500101 elif state == POWER_ON:
Norman Jamese2765102015-08-19 22:00:55 -0500102 self.setPowerOff();
103
104 # TODO: handle long press and reset
105
106 ## Signal handler
107 def power_good_signal_handler(self):
Norman James2a3d20b2015-08-20 07:09:33 -0500108 if (self.power_sequence==1):
Norman Jamese2765102015-08-19 22:00:55 -0500109 self.host_control_iface.boot()
Norman James2a3d20b2015-08-20 07:09:33 -0500110 self.power_sequence = 2
Norman Jamese2765102015-08-19 22:00:55 -0500111
112
113
114if __name__ == '__main__':
115 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
116
117 bus = dbus.SessionBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500118 name = dbus.service.BusName(DBUS_NAME, bus)
119 obj = ChassisControlObject(bus, OBJ_NAME)
Norman Jamese2765102015-08-19 22:00:55 -0500120 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500121
Norman Jamese2765102015-08-19 22:00:55 -0500122 print "Running ChassisControlService"
123 mainloop.run()
124