blob: 7eda9a0cb6c30958c6187b8d877792ba60d06fe3 [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 James362a80f2015-09-14 14:04:39 -050013OBJ_NAME = '/org/openbmc/control/'+sys.argv[1]
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
Adriana Kobylak025d13f2015-10-22 12:45:24 -050021def getWatchdog():
22 obj = bus.get_object('org.openbmc.watchdog.Host',
23 '/org/openbmc/watchdog/HostWatchdog_0')
24 intf = dbus.Interface(obj, 'org.openbmc.Watchdog' )
25 return intf
26
Norman James2656f332015-10-26 06:42:41 -050027class ChassisControlObject(Openbmc.DbusProperties):
Norman Jamese2765102015-08-19 22:00:55 -050028 def __init__(self,bus,name):
Norman James471ab592015-08-30 22:29:40 -050029 self.dbus_objects = { }
Norman James2656f332015-10-26 06:42:41 -050030 Openbmc.DbusProperties.__init__(self)
Norman James9e6acf92015-09-08 07:00:04 -050031 dbus.service.Object.__init__(self,bus,name)
Norman James90baede2015-09-02 20:32:49 -050032 ## load utilized objects
Norman James362a80f2015-09-14 14:04:39 -050033 self.dbus_objects = {
34 'power_control' : {
35 'bus_name' : 'org.openbmc.control.Power',
Norman Jamesa3e47c42015-10-18 14:43:10 -050036 'object_name' : '/org/openbmc/control/power0',
Norman James362a80f2015-09-14 14:04:39 -050037 'interface_name' : 'org.openbmc.control.Power'
38 },
39 'identify_led' : {
Norman Jamesa3e47c42015-10-18 14:43:10 -050040 'bus_name' : 'org.openbmc.control.led',
41 'object_name' : '/org/openbmc/led/IDENTIFY',
Norman James362a80f2015-09-14 14:04:39 -050042 'interface_name' : 'org.openbmc.Led'
43 }
Norman James471ab592015-08-30 22:29:40 -050044 }
Norman James90baede2015-09-02 20:32:49 -050045
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050046 #uuid
Norman James2656f332015-10-26 06:42:41 -050047 self.Set(DBUS_NAME,"uuid",str(uuid.uuid1()))
48 self.Set(DBUS_NAME,"reboot",0)
49 self.Set(DBUS_NAME,"power_policy",0)
50 self.Set(DBUS_NAME,"last_system_state","")
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050051
Norman James471ab592015-08-30 22:29:40 -050052 bus.add_signal_receiver(self.power_button_signal_handler,
53 dbus_interface = "org.openbmc.Button", signal_name = "ButtonPressed",
Norman James362a80f2015-09-14 14:04:39 -050054 path="/org/openbmc/buttons/PowerButton_0" )
Norman James9e6acf92015-09-08 07:00:04 -050055 bus.add_signal_receiver(self.host_watchdog_signal_handler,
56 dbus_interface = "org.openbmc.Watchdog", signal_name = "WatchdogError")
Norman James362a80f2015-09-14 14:04:39 -050057 bus.add_signal_receiver(self.SystemStateHandler,signal_name = "GotoSystemState")
Norman James471ab592015-08-30 22:29:40 -050058
Norman James9e6acf92015-09-08 07:00:04 -050059
Norman James362a80f2015-09-14 14:04:39 -050060 def getInterface(self,name):
61 o = self.dbus_objects[name]
62 obj = bus.get_object(o['bus_name'],o['object_name'])
63 return dbus.Interface(obj,o['interface_name'])
Norman Jamese2765102015-08-19 22:00:55 -050064
Norman Jamese2765102015-08-19 22:00:55 -050065
Norman James3f97c5d2015-08-26 17:44:14 -050066 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050067 in_signature='', out_signature='')
68 def setIdentify(self):
69 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050070 intf = self.getInterface('identify_led')
71 intf.setOn()
Norman Jamese2765102015-08-19 22:00:55 -050072 return None
73
Norman James3f97c5d2015-08-26 17:44:14 -050074 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050075 in_signature='', out_signature='')
76 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050077 print "Turn on identify"
78 intf = self.getInterface('identify_led')
79 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050080 return None
81
Norman James3f97c5d2015-08-26 17:44:14 -050082 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050083 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050084 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -050085 print "Turn on power and boot"
Norman James2656f332015-10-26 06:42:41 -050086 self.Set(DBUS_NAME,"reboot",0)
Norman Jamese2765102015-08-19 22:00:55 -050087 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -050088 intf = self.getInterface('power_control')
89 intf.setPowerState(POWER_ON)
Adriana Kobylak025d13f2015-10-22 12:45:24 -050090 intfwatchdog = getWatchdog()
91 #Start watchdog with 30s timeout per the OpenPower Host IPMI Spec
92 #Once the host starts booting, it'll reset and refresh the timer
93 intfwatchdog.set(30000)
94 intfwatchdog.start()
Norman Jamese2765102015-08-19 22:00:55 -050095 return None
96
Norman James3f97c5d2015-08-26 17:44:14 -050097 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050098 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050099 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -0500100 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -0500101 intf = self.getInterface('power_control')
102 intf.setPowerState(POWER_OFF)
103 return None
104
105 @dbus.service.method(DBUS_NAME,
106 in_signature='', out_signature='')
107 def softPowerOff(self):
108 print "Soft off power"
Norman James2656f332015-10-26 06:42:41 -0500109 ## TODO: Somehow tell host to shutdown via ipmi
110 ## for now hard power off
111 self.powerOff()
Norman James362a80f2015-09-14 14:04:39 -0500112 return None
113
114 @dbus.service.method(DBUS_NAME,
115 in_signature='', out_signature='')
116 def reboot(self):
117 print "Rebooting"
Norman James2656f332015-10-26 06:42:41 -0500118 self.Set(DBUS_NAME,"reboot",1)
Norman James362a80f2015-09-14 14:04:39 -0500119 intf.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500120 return None
121
Norman James3f97c5d2015-08-26 17:44:14 -0500122 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500123 in_signature='', out_signature='i')
124 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500125 intf = self.getInterface('power_control')
126 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500127
Norman James3f97c5d2015-08-26 17:44:14 -0500128 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500129 in_signature='', out_signature='')
130 def setDebugMode(self):
131 return None
132
Norman James3f97c5d2015-08-26 17:44:14 -0500133 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500134 in_signature='i', out_signature='')
135 def setPowerPolicy(self,policy):
Norman James2656f332015-10-26 06:42:41 -0500136 self.Set(DBUS_NAME,"power_policy",policy)
Norman Jamese2765102015-08-19 22:00:55 -0500137 return None
138
Norman James9e6acf92015-09-08 07:00:04 -0500139
Norman Jamese2765102015-08-19 22:00:55 -0500140 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500141
142 def SystemStateHandler(self,state_name):
Norman James2656f332015-10-26 06:42:41 -0500143 self.Set(DBUS_NAME,"last_system_state",state_name)
144 if (state_name == "HOST_POWERED_OFF" and self.Get(DBUS_NAME,"reboot")==1):
Norman James362a80f2015-09-14 14:04:39 -0500145 self.powerOn()
146
147
Norman Jamese2765102015-08-19 22:00:55 -0500148 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500149 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500150 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500151 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500152 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500153 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500154 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500155
156 # TODO: handle long press and reset
157
Norman James9e6acf92015-09-08 07:00:04 -0500158 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500159 print "Watchdog Error, Hard Rebooting"
Norman James2656f332015-10-26 06:42:41 -0500160 self.Set(DBUS_NAME,"reboot",1)
161 self.powerOff()
Norman James9e6acf92015-09-08 07:00:04 -0500162
Norman Jamese2765102015-08-19 22:00:55 -0500163
164if __name__ == '__main__':
165 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
166
Norman James5e792e32015-10-07 17:36:17 -0500167 bus = Openbmc.getDBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500168 name = dbus.service.BusName(DBUS_NAME, bus)
169 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500170 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500171
Norman Jamese2765102015-08-19 22:00:55 -0500172 print "Running ChassisControlService"
173 mainloop.run()
174