1 #!/usr/bin/python 2 3 # Copyright 2015 The Chromium OS Authors. All rights reserved. 4 # Use of this source code is governed by a BSD-style license that can be 5 # found in the LICENSE file. 6 7 import logging 8 import logging.handlers 9 10 import common 11 from autotest_lib.client.cros import constants 12 from autotest_lib.client.cros import dark_resume_listener 13 from autotest_lib.client.cros import xmlrpc_server 14 from autotest_lib.client.cros.power import sys_power 15 from autotest_lib.client.cros.power import power_utils 16 17 class DarkResumeXmlRpcDelegate(xmlrpc_server.XmlRpcDelegate): 18 """Exposes methods called remotely during dark resume autotests. 19 20 All instance methods of this object without a preceding '_' are exposed via 21 an XMLRPC server. This is not a stateless handler object, which means that 22 if you store state inside the delegate, that state will remain around for 23 future calls. 24 25 """ 26 27 def __init__(self): 28 super(DarkResumeXmlRpcDelegate, self).__init__() 29 self._listener = dark_resume_listener.DarkResumeListener() 30 31 32 @xmlrpc_server.dbus_safe(None) 33 def suspend_bg_for_dark_resume(self, suspend_for_secs): 34 """ 35 Suspends the system for dark resume. 36 @param suspend_for_secs : If not 0, sets a rtc alarm to 37 wake the system after|suspend_for_secs| secs. 38 """ 39 if suspend_for_secs == 0 : 40 sys_power.suspend_bg_for_dark_resume() 41 else: 42 sys_power.do_suspend(suspend_for_secs) 43 44 45 @xmlrpc_server.dbus_safe(None) 46 def set_stop_resuspend(self, stop_resuspend): 47 """ 48 Stops resuspend on seeing a dark resume. 49 @param stop_resuspend: Stops resuspend of the device on seeing a 50 a dark resume if |stop_resuspend| is true. 51 """ 52 self._listener.stop_resuspend(stop_resuspend) 53 54 55 @xmlrpc_server.dbus_safe(0) 56 def get_dark_resume_count(self): 57 """Gets the number of dark resumes that have occurred since 58 this listener was created.""" 59 return self._listener.count 60 61 62 @xmlrpc_server.dbus_safe(False) 63 def has_lid(self): 64 """ 65 Checks whether the DUT has lid. 66 67 @return: Returns True if the device has a lid, False otherwise. 68 """ 69 70 return power_utils.has_lid() 71 72 if __name__ == '__main__': 73 logging.basicConfig(level=logging.DEBUG) 74 handler = logging.handlers.SysLogHandler(address = '/dev/log') 75 formatter = logging.Formatter( 76 'dark_resume_xmlrpc_server: [%(levelname)s] %(message)s') 77 handler.setFormatter(formatter) 78 logging.getLogger().addHandler(handler) 79 logging.debug('dark_resume_xmlrpc_server main...') 80 server = xmlrpc_server.XmlRpcServer( 81 'localhost', constants.DARK_RESUME_XMLRPC_SERVER_PORT) 82 server.register_delegate(DarkResumeXmlRpcDelegate()) 83 server.run() 84