blob: fa3b9b6e23342083b8deda9d1eea05deff4adcf0 [file] [log] [blame]
Norman James42c1be82015-10-22 14:34:26 -05001#!/usr/bin/python -u
Norman Jamese2765102015-08-19 22:00:55 -05002
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 James8fee6f22015-10-28 12:48:43 -050013OBJ_NAME = '/org/openbmc/control/chassis0'
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 James323ed972015-12-09 09:06:37 -060021class ChassisControlObject(Openbmc.DbusProperties,Openbmc.DbusObjectManager):
Norman Jamese2765102015-08-19 22:00:55 -050022 def __init__(self,bus,name):
Norman James471ab592015-08-30 22:29:40 -050023 self.dbus_objects = { }
Norman James2656f332015-10-26 06:42:41 -050024 Openbmc.DbusProperties.__init__(self)
Norman James323ed972015-12-09 09:06:37 -060025 Openbmc.DbusObjectManager.__init__(self)
Norman James9e6acf92015-09-08 07:00:04 -050026 dbus.service.Object.__init__(self,bus,name)
Norman James90baede2015-09-02 20:32:49 -050027 ## load utilized objects
Norman James362a80f2015-09-14 14:04:39 -050028 self.dbus_objects = {
29 'power_control' : {
30 'bus_name' : 'org.openbmc.control.Power',
Norman Jamesa3e47c42015-10-18 14:43:10 -050031 'object_name' : '/org/openbmc/control/power0',
Norman James362a80f2015-09-14 14:04:39 -050032 'interface_name' : 'org.openbmc.control.Power'
33 },
34 'identify_led' : {
Norman Jamesa3e47c42015-10-18 14:43:10 -050035 'bus_name' : 'org.openbmc.control.led',
36 'object_name' : '/org/openbmc/led/IDENTIFY',
Norman James362a80f2015-09-14 14:04:39 -050037 'interface_name' : 'org.openbmc.Led'
Norman Jamesc07c4732015-10-26 07:12:58 -050038 },
39 'watchdog' : {
40 'bus_name' : 'org.openbmc.watchdog.Host',
Norman James8fee6f22015-10-28 12:48:43 -050041 'object_name' : '/org/openbmc/watchdog/host0',
Norman Jamesc07c4732015-10-26 07:12:58 -050042 'interface_name' : 'org.openbmc.Watchdog'
Norman James98e1f7b2015-11-24 22:17:56 -060043 },
44 'host_services' : {
45 'bus_name' : 'org.openbmc.HostServices',
46 'object_name' : '/org/openbmc/HostServices',
47 'interface_name' : 'org.openbmc.HostServices'
48 },
Norman James471ab592015-08-30 22:29:40 -050049 }
Norman James90baede2015-09-02 20:32:49 -050050
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050051 #uuid
Norman James2656f332015-10-26 06:42:41 -050052 self.Set(DBUS_NAME,"uuid",str(uuid.uuid1()))
53 self.Set(DBUS_NAME,"reboot",0)
54 self.Set(DBUS_NAME,"power_policy",0)
Norman James323ed972015-12-09 09:06:37 -060055 self.Set(DBUS_NAME,"last_system_state","")
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050056
Norman James471ab592015-08-30 22:29:40 -050057 bus.add_signal_receiver(self.power_button_signal_handler,
Norman Jamescfc2b442015-10-31 17:31:46 -050058 dbus_interface = "org.openbmc.Button", signal_name = "Released",
Norman James8fee6f22015-10-28 12:48:43 -050059 path="/org/openbmc/buttons/power0" )
Norman Jamescfc2b442015-10-31 17:31:46 -050060 bus.add_signal_receiver(self.reset_button_signal_handler,
Norman James807ed1f2015-11-09 10:53:03 -060061 dbus_interface = "org.openbmc.Button", signal_name = "PressedLong",
Norman Jamescfc2b442015-10-31 17:31:46 -050062 path="/org/openbmc/buttons/power0" )
Kenc95eccd2015-12-19 07:02:34 +080063 bus.add_signal_receiver(self.softreset_button_signal_handler,
64 dbus_interface = "org.openbmc.Button", signal_name = "Released",
65 path="/org/openbmc/buttons/reset0" )
Norman Jamescfc2b442015-10-31 17:31:46 -050066
Norman James9e6acf92015-09-08 07:00:04 -050067 bus.add_signal_receiver(self.host_watchdog_signal_handler,
68 dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError")
Norman James72567ba2016-01-13 16:57:48 -060069
70 bus.add_signal_receiver(self.emergency_shutdown_signal_handler,
71 dbus_interface = "org.openbmc.SensorThresholds", signal_name = "Emergency")
72
Norman James362a80f2015-09-14 14:04:39 -050073 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James323ed972015-12-09 09:06:37 -060074 self.InterfacesAdded(name,self.properties)
Norman James471ab592015-08-30 22:29:40 -050075
Norman James9e6acf92015-09-08 07:00:04 -050076
Norman James362a80f2015-09-14 14:04:39 -050077 def getInterface(self,name):
78 o = self.dbus_objects[name]
Norman James85f050b2015-12-18 14:58:20 -060079 obj = bus.get_object(o['bus_name'],o['object_name'],introspect=False)
Norman James362a80f2015-09-14 14:04:39 -050080 return dbus.Interface(obj,o['interface_name'])
Norman Jamese2765102015-08-19 22:00:55 -050081
Norman Jamese2765102015-08-19 22:00:55 -050082
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='')
85 def setIdentify(self):
86 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050087 intf = self.getInterface('identify_led')
88 intf.setOn()
Norman Jamese2765102015-08-19 22:00:55 -050089 return None
90
Norman James3f97c5d2015-08-26 17:44:14 -050091 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050092 in_signature='', out_signature='')
93 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050094 print "Turn on identify"
95 intf = self.getInterface('identify_led')
96 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050097 return None
98
Norman James3f97c5d2015-08-26 17:44:14 -050099 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500100 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -0500101 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -0500102 print "Turn on power and boot"
Norman James2656f332015-10-26 06:42:41 -0500103 self.Set(DBUS_NAME,"reboot",0)
Norman Jamese2765102015-08-19 22:00:55 -0500104 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -0500105 intf = self.getInterface('power_control')
106 intf.setPowerState(POWER_ON)
Norman Jamesc07c4732015-10-26 07:12:58 -0500107 intfwatchdog = self.getInterface('watchdog')
Adriana Kobylak025d13f2015-10-22 12:45:24 -0500108 #Start watchdog with 30s timeout per the OpenPower Host IPMI Spec
109 #Once the host starts booting, it'll reset and refresh the timer
110 intfwatchdog.set(30000)
111 intfwatchdog.start()
Norman Jamese2765102015-08-19 22:00:55 -0500112 return None
113
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='')
Norman James362a80f2015-09-14 14:04:39 -0500116 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -0500117 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -0500118 intf = self.getInterface('power_control')
119 intf.setPowerState(POWER_OFF)
120 return None
121
122 @dbus.service.method(DBUS_NAME,
123 in_signature='', out_signature='')
124 def softPowerOff(self):
125 print "Soft off power"
Norman James98e1f7b2015-11-24 22:17:56 -0600126 intf = self.getInterface('host_services')
127 ## host services will call power off when ready
128 intf.SoftPowerOff()
Norman James362a80f2015-09-14 14:04:39 -0500129 return None
130
131 @dbus.service.method(DBUS_NAME,
132 in_signature='', out_signature='')
133 def reboot(self):
134 print "Rebooting"
Norman James8d2e3ef2015-11-17 19:34:25 -0600135 if self.getPowerState() == POWER_OFF:
Norman Jamescfc2b442015-10-31 17:31:46 -0500136 self.powerOn();
137 else:
138 self.Set(DBUS_NAME,"reboot",1)
Norman Jamesb4ef3182015-12-03 17:54:35 -0600139 self.powerOff()
140 return None
141
Norman Jamesb4bd9e22015-12-18 15:47:50 -0600142 @dbus.service.method(DBUS_NAME,
143 in_signature='', out_signature='')
Norman Jamesb4ef3182015-12-03 17:54:35 -0600144 def softReboot(self):
145 print "Soft Rebooting"
146 if self.getPowerState() == POWER_OFF:
147 self.powerOn();
148 else:
149 self.Set(DBUS_NAME,"reboot",1)
Norman James8d2e3ef2015-11-17 19:34:25 -0600150 self.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500151 return None
152
Norman James3f97c5d2015-08-26 17:44:14 -0500153 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500154 in_signature='', out_signature='i')
155 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500156 intf = self.getInterface('power_control')
157 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500158
Norman James3f97c5d2015-08-26 17:44:14 -0500159 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500160 in_signature='', out_signature='')
161 def setDebugMode(self):
162 return None
163
Norman James3f97c5d2015-08-26 17:44:14 -0500164 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500165 in_signature='i', out_signature='')
166 def setPowerPolicy(self,policy):
Norman James2656f332015-10-26 06:42:41 -0500167 self.Set(DBUS_NAME,"power_policy",policy)
Norman Jamese2765102015-08-19 22:00:55 -0500168 return None
169
Norman James9e6acf92015-09-08 07:00:04 -0500170
Norman Jamese2765102015-08-19 22:00:55 -0500171 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500172
173 def SystemStateHandler(self,state_name):
Norman James2656f332015-10-26 06:42:41 -0500174 self.Set(DBUS_NAME,"last_system_state",state_name)
175 if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME,"reboot")==1):
Norman James362a80f2015-09-14 14:04:39 -0500176 self.powerOn()
177
178
Norman Jamese2765102015-08-19 22:00:55 -0500179 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500180 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500181 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500182 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500183 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500184 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500185 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500186
Norman Jamescfc2b442015-10-31 17:31:46 -0500187 def reset_button_signal_handler(self):
188 self.reboot();
Kenc95eccd2015-12-19 07:02:34 +0800189
190 def softreset_button_signal_handler(self):
191 self.softReboot();
Norman Jamescfc2b442015-10-31 17:31:46 -0500192
Norman James9e6acf92015-09-08 07:00:04 -0500193 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500194 print "Watchdog Error, Hard Rebooting"
Norman Jamesc07c4732015-10-26 07:12:58 -0500195 #self.Set(DBUS_NAME,"reboot",1)
196 #self.powerOff()
Norman James72567ba2016-01-13 16:57:48 -0600197
198 def emergency_shutdown_signal_handler(self):
199 print "Emergency Shutdown!"
200 self.powerOff()
201
Norman James9e6acf92015-09-08 07:00:04 -0500202
Norman Jamese2765102015-08-19 22:00:55 -0500203
204if __name__ == '__main__':
205 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
206
Norman James5e792e32015-10-07 17:36:17 -0500207 bus = Openbmc.getDBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500208 name = dbus.service.BusName(DBUS_NAME, bus)
209 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500210 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500211
Norman Jamese2765102015-08-19 22:00:55 -0500212 print "Running ChassisControlService"
213 mainloop.run()
214