blob: dc5dee40072cd057311f55a4837f6c3b351e31e5 [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 James2656f332015-10-26 06:42:41 -050021class ChassisControlObject(Openbmc.DbusProperties):
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 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'
Norman Jamesc07c4732015-10-26 07:12:58 -050037 },
38 'watchdog' : {
39 'bus_name' : 'org.openbmc.watchdog.Host',
Norman James8fee6f22015-10-28 12:48:43 -050040 'object_name' : '/org/openbmc/watchdog/host0',
Norman Jamesc07c4732015-10-26 07:12:58 -050041 'interface_name' : 'org.openbmc.Watchdog'
Norman James362a80f2015-09-14 14:04:39 -050042 }
Norman James471ab592015-08-30 22:29:40 -050043 }
Norman James90baede2015-09-02 20:32:49 -050044
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050045 #uuid
Norman James2656f332015-10-26 06:42:41 -050046 self.Set(DBUS_NAME,"uuid",str(uuid.uuid1()))
47 self.Set(DBUS_NAME,"reboot",0)
48 self.Set(DBUS_NAME,"power_policy",0)
49 self.Set(DBUS_NAME,"last_system_state","")
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050050
Norman James471ab592015-08-30 22:29:40 -050051 bus.add_signal_receiver(self.power_button_signal_handler,
Norman Jamescfc2b442015-10-31 17:31:46 -050052 dbus_interface = "org.openbmc.Button", signal_name = "Released",
Norman James8fee6f22015-10-28 12:48:43 -050053 path="/org/openbmc/buttons/power0" )
Norman Jamescfc2b442015-10-31 17:31:46 -050054 bus.add_signal_receiver(self.reset_button_signal_handler,
55 dbus_interface = "org.openbmc.Button", signal_name = "Pressed_Long",
56 path="/org/openbmc/buttons/power0" )
57
Norman James9e6acf92015-09-08 07:00:04 -050058 bus.add_signal_receiver(self.host_watchdog_signal_handler,
59 dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError")
Norman James362a80f2015-09-14 14:04:39 -050060 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman Jamescfc2b442015-10-31 17:31:46 -050061 self.ObjectAdded(name,CONTROL_INTF)
Norman James471ab592015-08-30 22:29:40 -050062
Norman James9e6acf92015-09-08 07:00:04 -050063
Norman James362a80f2015-09-14 14:04:39 -050064 def getInterface(self,name):
65 o = self.dbus_objects[name]
66 obj = bus.get_object(o['bus_name'],o['object_name'])
67 return dbus.Interface(obj,o['interface_name'])
Norman Jamese2765102015-08-19 22:00:55 -050068
Norman Jamese2765102015-08-19 22:00:55 -050069
Norman James3f97c5d2015-08-26 17:44:14 -050070 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050071 in_signature='', out_signature='')
72 def setIdentify(self):
73 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050074 intf = self.getInterface('identify_led')
75 intf.setOn()
Norman Jamese2765102015-08-19 22:00:55 -050076 return None
77
Norman James3f97c5d2015-08-26 17:44:14 -050078 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050079 in_signature='', out_signature='')
80 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050081 print "Turn on identify"
82 intf = self.getInterface('identify_led')
83 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050084 return None
85
Norman James3f97c5d2015-08-26 17:44:14 -050086 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050087 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050088 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -050089 print "Turn on power and boot"
Norman James2656f332015-10-26 06:42:41 -050090 self.Set(DBUS_NAME,"reboot",0)
Norman Jamese2765102015-08-19 22:00:55 -050091 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -050092 intf = self.getInterface('power_control')
93 intf.setPowerState(POWER_ON)
Norman Jamesc07c4732015-10-26 07:12:58 -050094 intfwatchdog = self.getInterface('watchdog')
Adriana Kobylak025d13f2015-10-22 12:45:24 -050095 #Start watchdog with 30s timeout per the OpenPower Host IPMI Spec
96 #Once the host starts booting, it'll reset and refresh the timer
97 intfwatchdog.set(30000)
98 intfwatchdog.start()
Norman Jamese2765102015-08-19 22:00:55 -050099 return None
100
Norman James3f97c5d2015-08-26 17:44:14 -0500101 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500102 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -0500103 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -0500104 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -0500105 intf = self.getInterface('power_control')
106 intf.setPowerState(POWER_OFF)
107 return None
108
109 @dbus.service.method(DBUS_NAME,
110 in_signature='', out_signature='')
111 def softPowerOff(self):
112 print "Soft off power"
Norman James2656f332015-10-26 06:42:41 -0500113 ## TODO: Somehow tell host to shutdown via ipmi
114 ## for now hard power off
115 self.powerOff()
Norman James362a80f2015-09-14 14:04:39 -0500116 return None
117
118 @dbus.service.method(DBUS_NAME,
119 in_signature='', out_signature='')
120 def reboot(self):
121 print "Rebooting"
Norman Jamescfc2b442015-10-31 17:31:46 -0500122 if state == POWER_OFF:
123 self.powerOn();
124 else:
125 self.Set(DBUS_NAME,"reboot",1)
126 intf.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500127 return None
128
Norman James3f97c5d2015-08-26 17:44:14 -0500129 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500130 in_signature='', out_signature='i')
131 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500132 intf = self.getInterface('power_control')
133 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500134
Norman James3f97c5d2015-08-26 17:44:14 -0500135 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500136 in_signature='', out_signature='')
137 def setDebugMode(self):
138 return None
139
Norman James3f97c5d2015-08-26 17:44:14 -0500140 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500141 in_signature='i', out_signature='')
142 def setPowerPolicy(self,policy):
Norman James2656f332015-10-26 06:42:41 -0500143 self.Set(DBUS_NAME,"power_policy",policy)
Norman Jamese2765102015-08-19 22:00:55 -0500144 return None
145
Norman James9e6acf92015-09-08 07:00:04 -0500146
Norman Jamese2765102015-08-19 22:00:55 -0500147 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500148
149 def SystemStateHandler(self,state_name):
Norman James2656f332015-10-26 06:42:41 -0500150 self.Set(DBUS_NAME,"last_system_state",state_name)
151 if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME,"reboot")==1):
Norman James362a80f2015-09-14 14:04:39 -0500152 self.powerOn()
153
154
Norman Jamese2765102015-08-19 22:00:55 -0500155 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500156 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500157 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500158 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500159 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500160 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500161 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500162
Norman Jamescfc2b442015-10-31 17:31:46 -0500163 def reset_button_signal_handler(self):
164 self.reboot();
165
Norman James9e6acf92015-09-08 07:00:04 -0500166 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500167 print "Watchdog Error, Hard Rebooting"
Norman Jamesc07c4732015-10-26 07:12:58 -0500168 #self.Set(DBUS_NAME,"reboot",1)
169 #self.powerOff()
Norman James9e6acf92015-09-08 07:00:04 -0500170
Norman Jamese2765102015-08-19 22:00:55 -0500171
172if __name__ == '__main__':
173 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
174
Norman James5e792e32015-10-07 17:36:17 -0500175 bus = Openbmc.getDBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500176 name = dbus.service.BusName(DBUS_NAME, bus)
177 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500178 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500179
Norman Jamese2765102015-08-19 22:00:55 -0500180 print "Running ChassisControlService"
181 mainloop.run()
182