blob: e3f6e6a991b7d344d2a2bb2d7b25d3b600f92c9b [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 Jamese2765102015-08-19 22:00:55 -050027class ChassisControlObject(dbus.service.Object):
28 def __init__(self,bus,name):
Norman James471ab592015-08-30 22:29:40 -050029 self.dbus_objects = { }
Norman James90baede2015-09-02 20:32:49 -050030
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 James362a80f2015-09-14 14:04:39 -050045 #self.power_sequence = 0
Norman James9e6acf92015-09-08 07:00:04 -050046 self.reboot = 0
47 self.last_power_state = 0
Norman James90baede2015-09-02 20:32:49 -050048
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050049 #uuid
50 self.id = 0
51
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 James3f97c5d2015-08-26 17:44:14 -050065 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050066 in_signature='', out_signature='s')
67 def getID(self):
Adriana Kobylak08d3bdb2015-10-20 16:59:14 -050068 if (self.id==0):
69 #calculate uuuid
70 self.id = uuid.uuid1()
71 return str(self.id)
Norman Jamese2765102015-08-19 22:00:55 -050072
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 setIdentify(self):
76 print "Turn on identify"
Norman James362a80f2015-09-14 14:04:39 -050077 intf = self.getInterface('identify_led')
78 intf.setOn()
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='')
83 def clearIdentify(self):
Norman James362a80f2015-09-14 14:04:39 -050084 print "Turn on identify"
85 intf = self.getInterface('identify_led')
86 intf.setOff()
Norman Jamese2765102015-08-19 22:00:55 -050087 return None
88
Norman James3f97c5d2015-08-26 17:44:14 -050089 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -050090 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -050091 def powerOn(self):
Norman Jamese2765102015-08-19 22:00:55 -050092 print "Turn on power and boot"
Norman James9e6acf92015-09-08 07:00:04 -050093 self.reboot = 0
Norman Jamese2765102015-08-19 22:00:55 -050094 if (self.getPowerState()==0):
Norman James362a80f2015-09-14 14:04:39 -050095 intf = self.getInterface('power_control')
96 intf.setPowerState(POWER_ON)
Adriana Kobylak025d13f2015-10-22 12:45:24 -050097 intfwatchdog = getWatchdog()
98 #Start watchdog with 30s timeout per the OpenPower Host IPMI Spec
99 #Once the host starts booting, it'll reset and refresh the timer
100 intfwatchdog.set(30000)
101 intfwatchdog.start()
Norman Jamese2765102015-08-19 22:00:55 -0500102 return None
103
Norman James3f97c5d2015-08-26 17:44:14 -0500104 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500105 in_signature='', out_signature='')
Norman James362a80f2015-09-14 14:04:39 -0500106 def powerOff(self):
Norman Jamese2765102015-08-19 22:00:55 -0500107 print "Turn off power"
Norman James362a80f2015-09-14 14:04:39 -0500108 intf = self.getInterface('power_control')
109 intf.setPowerState(POWER_OFF)
110 return None
111
112 @dbus.service.method(DBUS_NAME,
113 in_signature='', out_signature='')
114 def softPowerOff(self):
115 print "Soft off power"
116 ## Somehow tell host to shutdown via ipmi
117 return None
118
119 @dbus.service.method(DBUS_NAME,
120 in_signature='', out_signature='')
121 def reboot(self):
122 print "Rebooting"
123 self.reboot=1
124 intf.softPowerOff()
Norman Jamese2765102015-08-19 22:00:55 -0500125 return None
126
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='i')
129 def getPowerState(self):
Norman James362a80f2015-09-14 14:04:39 -0500130 intf = self.getInterface('power_control')
131 return intf.getPowerState()
Norman Jamese2765102015-08-19 22:00:55 -0500132
Norman James3f97c5d2015-08-26 17:44:14 -0500133 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500134 in_signature='', out_signature='')
135 def setDebugMode(self):
136 return None
137
Norman James3f97c5d2015-08-26 17:44:14 -0500138 @dbus.service.method(DBUS_NAME,
Norman Jamese2765102015-08-19 22:00:55 -0500139 in_signature='i', out_signature='')
140 def setPowerPolicy(self,policy):
141 return None
142
Norman James9e6acf92015-09-08 07:00:04 -0500143
Norman Jamese2765102015-08-19 22:00:55 -0500144 ## Signal handler
Norman James362a80f2015-09-14 14:04:39 -0500145
146 def SystemStateHandler(self,state_name):
Norman James0e0c7882015-10-19 08:58:22 -0500147 if (state_name == "HOST_POWERED_OFF" and self.reboot==1):
Norman James362a80f2015-09-14 14:04:39 -0500148 self.powerOn()
149
150
Norman Jamese2765102015-08-19 22:00:55 -0500151 def power_button_signal_handler(self):
Norman James9e6acf92015-09-08 07:00:04 -0500152 # toggle power
Norman Jamese2765102015-08-19 22:00:55 -0500153 state = self.getPowerState()
Norman James2a3d20b2015-08-20 07:09:33 -0500154 if state == POWER_OFF:
Norman James362a80f2015-09-14 14:04:39 -0500155 self.powerOn()
Norman James2a3d20b2015-08-20 07:09:33 -0500156 elif state == POWER_ON:
Norman James362a80f2015-09-14 14:04:39 -0500157 self.powerOff();
Norman Jamese2765102015-08-19 22:00:55 -0500158
159 # TODO: handle long press and reset
160
Norman James9e6acf92015-09-08 07:00:04 -0500161 def host_watchdog_signal_handler(self):
Norman James362a80f2015-09-14 14:04:39 -0500162 print "Watchdog Error, Hard Rebooting"
Norman James42c1be82015-10-22 14:34:26 -0500163 #self.reboot = 1
164 #self.powerOff()
Norman James9e6acf92015-09-08 07:00:04 -0500165
Norman Jamese2765102015-08-19 22:00:55 -0500166
167if __name__ == '__main__':
168 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
169
Norman James5e792e32015-10-07 17:36:17 -0500170 bus = Openbmc.getDBus()
Norman James3f97c5d2015-08-26 17:44:14 -0500171 name = dbus.service.BusName(DBUS_NAME, bus)
172 obj = ChassisControlObject(bus, OBJ_NAME)
Norman James6f8d0422015-09-14 18:48:00 -0500173 mainloop = gobject.MainLoop()
Norman James81dbd352015-08-19 22:44:53 -0500174
Norman Jamese2765102015-08-19 22:00:55 -0500175 print "Running ChassisControlService"
176 mainloop.run()
177