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"""
17This test script leverages the relay_lib to pair different BT devices. This
18script will be invoked from Tradefed test. The test will first setup pairing
19between BT device and DUT and wait for signal (through socket) from tradefed
20to power down the BT device
21"""
22
23import logging
24import socket
25import time
26
27from acts import base_test
28
29
30class SetupBTPairingTest(base_test.BaseTestClass):
31    def __init__(self, controllers):
32        base_test.BaseTestClass.__init__(self, controllers)
33
34    def select_device_by_mac_address(self, mac_address):
35        for device in self.relay_devices:
36            if device.mac_address == mac_address:
37                return device
38        return self.relay_devices[0]
39
40    def setup_test(self):
41        self.bt_device = self.select_device_by_mac_address(
42            self.user_params["mac_address"])
43
44    def wait_for_test_completion(self):
45        port = int(self.user_params["socket_port"])
46        timeout = float(self.user_params["socket_timeout_secs"])
47
48        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
49        sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
50
51        server_address = ('localhost', port)
52        logging.info("Starting server socket on localhost port %s", port)
53        sock.bind(('localhost', port))
54        sock.settimeout(timeout)
55        sock.listen(1)
56        logging.info("Waiting for client socket connection")
57        try:
58            connection, client_address = sock.accept()
59        except socket.timeout:
60            logging.error("Did not receive signal. Shutting down AP")
61        except socket.error:
62            logging.error("Socket connection errored out. Shutting down AP")
63        finally:
64            if connection is not None:
65                connection.close()
66            if sock is not None:
67                sock.shutdown(socket.SHUT_RDWR)
68                sock.close()
69
70    def enable_pairing_mode(self):
71        self.bt_device.setup()
72        self.bt_device.power_on()
73        # Wait for a moment between pushing buttons
74        time.sleep(2)
75        self.bt_device.enter_pairing_mode()
76
77    def test_bt_pairing(self):
78        req_params = ["RelayDevice", "socket_port", "socket_timeout_secs"]
79        opt_params = []
80        self.unpack_userparams(req_param_names=req_params,
81                               opt_param_names=opt_params)
82        # Setup BT pairing mode
83        self.enable_pairing_mode()
84        # BT pairing mode is turned on
85        self.wait_for_test_completion()
86
87    def teardown_test(self):
88        self.bt_device.power_off()
89        self.bt_device.clean_up()
90