blob: 16fb5547f8915cfaa7c77d67e54e9b7d0ddf88b7 [file] [log] [blame]
Brad Bishop87c6b732016-08-30 19:17:57 -04001#!/usr/bin/env python
Norman Jamese2765102015-08-19 22:00:55 -05002
Norman James6f8d0422015-09-14 18:48:00 -05003import gobject
Norman Jamese2765102015-08-19 22:00:55 -05004import dbus
5import dbus.service
6import dbus.mainloop.glib
Brad Bishop84e73b52016-05-12 15:57:52 -04007from obmc.dbuslib.bindings import get_dbus, DbusProperties, DbusObjectManager
Norman Jamese2765102015-08-19 22:00:55 -05008
Norman James3f97c5d2015-08-26 17:44:14 -05009DBUS_NAME = 'org.openbmc.control.Chassis'
Norman James8fee6f22015-10-28 12:48:43 -050010OBJ_NAME = '/org/openbmc/control/chassis0'
Norman Jamesa3e47c42015-10-18 14:43:10 -050011CONTROL_INTF = 'org.openbmc.Control'
Norman James3f97c5d2015-08-26 17:44:14 -050012
Norman Jamesad8c3d32016-02-11 15:19:01 -060013MACHINE_ID = '/etc/machine-id'
14
Norman James2a3d20b2015-08-20 07:09:33 -050015POWER_OFF = 0
16POWER_ON = 1
17
Norman James9e6acf92015-09-08 07:00:04 -050018BOOTED = 100
19
Norman James90baede2015-09-02 20:32:49 -050020
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050021class ChassisControlObject(DbusProperties, DbusObjectManager):
22 def getUuid(self):
Brad Bishop7a4d77a2016-09-08 23:26:17 -040023 uuid = ""
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050024 try:
25 with open(MACHINE_ID) as f:
26 data = f.readline().rstrip('\n')
27 if (len(data) == 32):
28 uuid = data
29 else:
30 print "ERROR: UUID is not formatted correctly: " + data
31 except:
32 print "ERROR: Unable to open uuid file: " + MACHINE_ID
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050033
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050034 return uuid
Norman Jamescfc2b442015-10-31 17:31:46 -050035
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050036 def __init__(self, bus, name):
Brad Bishopf47f5fa2016-09-08 22:29:01 -040037 super(ChassisControlObject, self).__init__(
38 conn=bus,
39 object_path=name)
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050040 ## load utilized objects
41 self.dbus_objects = {
42 'power_control': {
43 'bus_name': 'org.openbmc.control.Power',
44 'object_name': '/org/openbmc/control/power0',
45 'interface_name': 'org.openbmc.control.Power'
46 },
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050047 'host_services': {
48 'bus_name': 'org.openbmc.HostServices',
49 'object_name': '/org/openbmc/HostServices',
50 'interface_name': 'org.openbmc.HostServices'
51 },
52 'settings': {
53 'bus_name': 'org.openbmc.settings.Host',
54 'object_name': '/org/openbmc/settings/host0',
55 'interface_name': 'org.freedesktop.DBus.Properties'
56 },
Brad Bishopabe0f682016-08-22 19:27:18 -040057 'systemd': {
58 'bus_name': 'org.freedesktop.systemd1',
59 'object_name': '/org/freedesktop/systemd1',
60 'interface_name': 'org.freedesktop.systemd1.Manager'
Andrew Geissler6b63e9a2016-06-30 10:45:01 -050061 },
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050062 }
Norman James72567ba2016-01-13 16:57:48 -060063
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050064 # uuid
65 self.Set(DBUS_NAME, "uuid", self.getUuid())
66 self.Set(DBUS_NAME, "reboot", 0)
Norman James72567ba2016-01-13 16:57:48 -060067
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050068 bus.add_signal_receiver(self.power_button_signal_handler,
69 dbus_interface="org.openbmc.Button",
70 signal_name="Released",
71 path="/org/openbmc/buttons/power0")
Adriana Kobylakeeb9b442016-08-15 11:12:53 -050072 bus.add_signal_receiver(self.long_power_button_signal_handler,
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050073 dbus_interface="org.openbmc.Button",
74 signal_name="PressedLong",
75 path="/org/openbmc/buttons/power0")
76 bus.add_signal_receiver(self.softreset_button_signal_handler,
77 dbus_interface="org.openbmc.Button",
78 signal_name="Released",
79 path="/org/openbmc/buttons/reset0")
80
81 bus.add_signal_receiver(self.host_watchdog_signal_handler,
82 dbus_interface="org.openbmc.Watchdog",
83 signal_name="WatchdogError")
84
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050085 bus.add_signal_receiver(self.SystemStateHandler,
86 signal_name="GotoSystemState")
Norman James471ab592015-08-30 22:29:40 -050087
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050088 def getInterface(self, name):
89 o = self.dbus_objects[name]
90 obj = bus.get_object(o['bus_name'], o['object_name'], introspect=False)
91 return dbus.Interface(obj, o['interface_name'])
Norman Jamese2765102015-08-19 22:00:55 -050092
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050093 @dbus.service.method(DBUS_NAME,
94 in_signature='', out_signature='')
Andrew Geisslerb7f40b52016-06-29 15:49:26 -050095 def powerOn(self):
96 print "Turn on power and boot"
97 self.Set(DBUS_NAME, "reboot", 0)
Brad Bishopabe0f682016-08-22 19:27:18 -040098 intf = self.getInterface('systemd')
99 f = getattr(intf, 'StartUnit')
Josh D. King2a559542017-04-11 13:48:00 -0500100 f.call_async('obmc-host-start@0.target', 'replace')
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500101 return None
Norman Jamese2765102015-08-19 22:00:55 -0500102
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500103 @dbus.service.method(DBUS_NAME,
104 in_signature='', out_signature='')
105 def powerOff(self):
106 print "Turn off power"
Brad Bishopabe0f682016-08-22 19:27:18 -0400107 intf = self.getInterface('systemd')
108 f = getattr(intf, 'StartUnit')
Andrew Geissler9903ec62017-07-30 09:36:33 -0500109 f.call_async('obmc-chassis-hard-poweroff@0.target', 'replace')
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500110 return None
Norman James362a80f2015-09-14 14:04:39 -0500111
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500112 @dbus.service.method(DBUS_NAME,
113 in_signature='', out_signature='')
114 def softPowerOff(self):
115 print "Soft off power"
Andrew Geissler9903ec62017-07-30 09:36:33 -0500116 intf = self.getInterface('systemd')
117 f = getattr(intf, 'StartUnit')
118 f.call_async('obmc-host-shutdown@0.target', 'replace')
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500119 return None
Norman James362a80f2015-09-14 14:04:39 -0500120
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500121 @dbus.service.method(DBUS_NAME,
122 in_signature='', out_signature='')
123 def reboot(self):
124 print "Rebooting"
125 if self.getPowerState() == POWER_OFF:
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400126 self.powerOn()
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500127 else:
128 self.Set(DBUS_NAME, "reboot", 1)
129 self.powerOff()
130 return None
Norman Jamesb4ef3182015-12-03 17:54:35 -0600131
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500132 @dbus.service.method(DBUS_NAME,
133 in_signature='', out_signature='')
134 def softReboot(self):
135 print "Soft Rebooting"
136 if self.getPowerState() == POWER_OFF:
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400137 self.powerOn()
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500138 else:
139 self.Set(DBUS_NAME, "reboot", 1)
140 self.softPowerOff()
141 return None
Norman Jamese2765102015-08-19 22:00:55 -0500142
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500143 @dbus.service.method(DBUS_NAME,
Michael Tritz6d2f6c32017-02-02 17:03:18 -0600144 in_signature='', out_signature='')
145 def quiesce(self):
146 intf = self.getInterface('systemd')
147 f = getattr(intf, 'StartUnit')
Josh D. Kinge02cffd2017-04-11 13:53:11 -0500148 f.call_async('obmc-host-quiesce@0.target', 'replace')
Michael Tritz6d2f6c32017-02-02 17:03:18 -0600149 return None
150
151 @dbus.service.method(DBUS_NAME,
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500152 in_signature='', out_signature='i')
153 def getPowerState(self):
154 intf = self.getInterface('power_control')
155 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500156
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500157 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500158
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500159 def SystemStateHandler(self, state_name):
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400160 if state_name in ["HOST_POWERED_OFF", "HOST_POWERED_ON"]:
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500161 intf = self.getInterface('settings')
162 intf.Set("org.openbmc.settings.Host", "system_state", state_name)
Norman Jamesf5edb9e2016-01-31 13:32:24 -0600163
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500164 if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME,
165 "reboot") == 1):
166 self.powerOn()
Norman James362a80f2015-09-14 14:04:39 -0500167
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500168 def power_button_signal_handler(self):
Adriana Kobylakeeb9b442016-08-15 11:12:53 -0500169 # toggle power, power-on / soft-power-off
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500170 state = self.getPowerState()
171 if state == POWER_OFF:
172 self.powerOn()
173 elif state == POWER_ON:
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400174 self.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500175
Adriana Kobylakeeb9b442016-08-15 11:12:53 -0500176 def long_power_button_signal_handler(self):
177 print "Long-press button, hard power off"
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400178 self.powerOff()
Kenc95eccd2015-12-19 07:02:34 +0800179
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500180 def softreset_button_signal_handler(self):
Brad Bishop7a4d77a2016-09-08 23:26:17 -0400181 self.softReboot()
Norman James72567ba2016-01-13 16:57:48 -0600182
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500183 def host_watchdog_signal_handler(self):
Michael Tritz6d2f6c32017-02-02 17:03:18 -0600184 print "Watchdog Error, Going to quiesce"
185 self.quiesce()
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500186
Norman Jamese2765102015-08-19 22:00:55 -0500187
188if __name__ == '__main__':
189 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
190
Brad Bishop84e73b52016-05-12 15:57:52 -0400191 bus = get_dbus()
Norman James3f97c5d2015-08-26 17:44:14 -0500192 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500193 mainloop = gobject.MainLoop()
Brad Bishopf0f3efe2016-06-29 23:20:24 -0400194
195 obj.unmask_signals()
Brad Bishop70852a32016-06-29 22:58:51 -0400196 name = dbus.service.BusName(DBUS_NAME, bus)
Andrew Geisslerb7f40b52016-06-29 15:49:26 -0500197
Norman Jamese2765102015-08-19 22:00:55 -0500198 print "Running ChassisControlService"
199 mainloop.run()
Brad Bishop53066752016-09-21 08:48:04 -0400200
201# vim: tabstop=8 expandtab shiftwidth=4 softtabstop=4