Home | History | Annotate | Download | only in power
      1 #!/usr/bin/env python3.4
      2 #
      3 # Copyright (C) 2017 The Android Open Source Project
      4 #
      5 # Licensed under the Apache License, Version 2.0 (the "License"); you may not
      6 # use this file except in compliance with the License. You may obtain a copy of
      7 # the License at
      8 #
      9 # http://www.apache.org/licenses/LICENSE-2.0
     10 #
     11 # Unless required by applicable law or agreed to in writing, software
     12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
     14 # License for the specific language governing permissions and limitations under
     15 # the License.
     16 
     17 """
     18 This test script leverages the relay_lib to pair different BT devices. This
     19 script will be invoked from Tradefed test. The test will first setup pairing
     20 between BT device and DUT and wait for signal (through socket) from tradefed
     21 to power down the BT device
     22 """
     23 
     24 import logging
     25 import socket
     26 import sys
     27 import time
     28 
     29 from acts import base_test
     30 
     31 class SetupBTPairingTest(base_test.BaseTestClass):
     32 
     33     def __init__(self, controllers):
     34         base_test.BaseTestClass.__init__(self, controllers)
     35 
     36     def setup_test(self):
     37         self.bt_device = self.relay_devices[0]
     38 
     39     def wait_for_test_completion(self):
     40         port = int(self.user_params["socket_port"])
     41         timeout = float(self.user_params["socket_timeout_secs"])
     42 
     43         sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
     44         sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
     45 
     46         server_address = ('localhost', port)
     47         logging.info("Starting server socket on localhost port %s", port)
     48         sock.bind(('localhost', port))
     49         sock.settimeout(timeout)
     50         sock.listen(1)
     51         logging.info("Waiting for client socket connection")
     52         try:
     53             connection, client_address = sock.accept()
     54         except socket.timeout:
     55             logging.error("Did not receive signal. Shutting down AP")
     56         except socket.error:
     57             logging.error("Socket connection errored out. Shutting down AP")
     58         finally:
     59             if connection is not None:
     60                 connection.close()
     61             if sock is not None:
     62                 sock.shutdown(socket.SHUT_RDWR)
     63                 sock.close()
     64 
     65 
     66     def enable_pairing_mode(self):
     67         self.bt_device.setup()
     68         self.bt_device.power_on()
     69         # Wait for a moment between pushing buttons
     70         time.sleep(0.25)
     71         self.bt_device.enter_pairing_mode()
     72 
     73     def test_bt_pairing(self):
     74         req_params = [
     75             "RelayDevice", "socket_port", "socket_timeout_secs"
     76         ]
     77         opt_params = []
     78         self.unpack_userparams(
     79             req_param_names=req_params, opt_param_names=opt_params)
     80         # Setup BT pairing mode
     81         self.enable_pairing_mode()
     82         # BT pairing mode is turned on
     83         self.wait_for_test_completion()
     84 
     85     def teardown_test(self):
     86         self.bt_device.power_off()
     87         self.bt_device.clean_up()
     88