blob: 0cac5799a95ce183b46dcd77d168082c4d0e316b [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,
52 dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed",
Norman James8fee6f22015-10-28 12:48:43 -050053 path="/org/openbmc/buttons/power0" )
Norman James9e6acf92015-09-08 07:00:04 -050054 bus.add_signal_receiver(self.host_watchdog_signal_handler,
55 dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError")
Norman James362a80f2015-09-14 14:04:39 -050056 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James471ab592015-08-30 22:29:40 -050057
Norman James9e6acf92015-09-08 07:00:04 -050058
Norman James362a80f2015-09-14 14:04:39 -050059 def getInterface(self,name):
60 o = self.dbus_objects[name]
61 obj = bus.get_object(o['bus_name'],o['object_name'])
62 return dbus.Interface(obj,o['interface_name'])
Norman Jamese2765102015-08-19 22:00:55 -050063
Norman Jamese2765102015-08-19 22:00:55 -050064
Norman James3f97c5d2015-08-26 17:44:14 -050065 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050066 in_signature='', out_signature='')
67 def setIdentify(self):
68 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050069 intf = self.getInterface('identify_led')
70 intf.setOn()
Norman Jamese2765102015-08-19 22:00:55 -050071 return None
72
Norman James3f97c5d2015-08-26 17:44:14 -050073 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050074 in_signature='', out_signature='')
75 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050076 print "Turn on identify"
77 intf = self.getInterface('identify_led')
78 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050079 return None
80
Norman James3f97c5d2015-08-26 17:44:14 -050081 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050082 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050083 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -050084 print "Turn on power and boot"
Norman James2656f332015-10-26 06:42:41 -050085 self.Set(DBUS_NAME,"reboot",0)
Norman Jamese2765102015-08-19 22:00:55 -050086 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -050087 intf = self.getInterface('power_control')
88 intf.setPowerState(POWER_ON)
Norman Jamesc07c4732015-10-26 07:12:58 -050089 intfwatchdog = self.getInterface('watchdog')
Adriana Kobylak025d13f2015-10-22 12:45:24 -050090 #Start watchdog with 30s timeout per the OpenPower Host IPMI Spec
91 #Once the host starts booting, it'll reset and refresh the timer
92 intfwatchdog.set(30000)
93 intfwatchdog.start()
Norman Jamese2765102015-08-19 22:00:55 -050094 return None
95
Norman James3f97c5d2015-08-26 17:44:14 -050096 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050097 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050098 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -050099 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -0500100 intf = self.getInterface('power_control')
101 intf.setPowerState(POWER_OFF)
102 return None
103
104 @dbus.service.method(DBUS_NAME,
105 in_signature='', out_signature='')
106 def softPowerOff(self):
107 print "Soft off power"
Norman James2656f332015-10-26 06:42:41 -0500108 ## TODO: Somehow tell host to shutdown via ipmi
109 ## for now hard power off
110 self.powerOff()
Norman James362a80f2015-09-14 14:04:39 -0500111 return None
112
113 @dbus.service.method(DBUS_NAME,
114 in_signature='', out_signature='')
115 def reboot(self):
116 print "Rebooting"
Norman James2656f332015-10-26 06:42:41 -0500117 self.Set(DBUS_NAME,"reboot",1)
Norman James362a80f2015-09-14 14:04:39 -0500118 intf.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500119 return None
120
Norman James3f97c5d2015-08-26 17:44:14 -0500121 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500122 in_signature='', out_signature='i')
123 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500124 intf = self.getInterface('power_control')
125 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500126
Norman James3f97c5d2015-08-26 17:44:14 -0500127 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500128 in_signature='', out_signature='')
129 def setDebugMode(self):
130 return None
131
Norman James3f97c5d2015-08-26 17:44:14 -0500132 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500133 in_signature='i', out_signature='')
134 def setPowerPolicy(self,policy):
Norman James2656f332015-10-26 06:42:41 -0500135 self.Set(DBUS_NAME,"power_policy",policy)
Norman Jamese2765102015-08-19 22:00:55 -0500136 return None
137
Norman James9e6acf92015-09-08 07:00:04 -0500138
Norman Jamese2765102015-08-19 22:00:55 -0500139 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500140
141 def SystemStateHandler(self,state_name):
Norman James2656f332015-10-26 06:42:41 -0500142 self.Set(DBUS_NAME,"last_system_state",state_name)
143 if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME,"reboot")==1):
Norman James362a80f2015-09-14 14:04:39 -0500144 self.powerOn()
145
146
Norman Jamese2765102015-08-19 22:00:55 -0500147 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500148 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500149 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500150 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500151 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500152 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500153 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500154
155 # TODO: handle long press and reset
156
Norman James9e6acf92015-09-08 07:00:04 -0500157 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500158 print "Watchdog Error, Hard Rebooting"
Norman Jamesc07c4732015-10-26 07:12:58 -0500159 #self.Set(DBUS_NAME,"reboot",1)
160 #self.powerOff()
Norman James9e6acf92015-09-08 07:00:04 -0500161
Norman Jamese2765102015-08-19 22:00:55 -0500162
163if __name__ == '__main__':
164 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
165
Norman James5e792e32015-10-07 17:36:17 -0500166 bus = Openbmc.getDBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500167 name = dbus.service.BusName(DBUS_NAME, bus)
168 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500169 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500170
Norman Jamese2765102015-08-19 22:00:55 -0500171 print "Running ChassisControlService"
172 mainloop.run()
173