tturney | 1bdf77d | 2015-12-28 17:46:13 -0800 | [diff] [blame^] | 1 | #/usr/bin/env python3.4 |
| 2 | # |
| 3 | # Copyright (C) 2016 The Android Open Source Project |
Ang Li | 73697b3 | 2015-12-03 00:41:53 +0000 | [diff] [blame] | 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 | Test script to execute Bluetooth basic functionality test cases. |
| 19 | This test was designed to be run in a shield box. |
| 20 | """ |
| 21 | |
| 22 | import threading |
| 23 | import time |
| 24 | |
| 25 | from queue import Empty |
| 26 | from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest |
| 27 | from acts.test_utils.bt.bt_test_utils import get_bt_mac_address |
| 28 | from acts.test_utils.bt.bt_test_utils import log_energy_info |
| 29 | from acts.test_utils.bt.bt_test_utils import reset_bluetooth |
| 30 | from acts.test_utils.bt.bt_test_utils import rfcomm_accept |
| 31 | from acts.test_utils.bt.bt_test_utils import rfcomm_connect |
| 32 | from acts.test_utils.bt.bt_test_utils import setup_multiple_devices_for_bt_test |
| 33 | from acts.test_utils.bt.bt_test_utils import take_btsnoop_logs |
| 34 | |
| 35 | |
| 36 | class SppTest(BluetoothBaseTest): |
| 37 | default_timeout = 10 |
| 38 | scan_discovery_time = 5 |
| 39 | thread_list = [] |
| 40 | message = ("Space: the final frontier. These are the voyages of " |
| 41 | "the starship Enterprise. Its continuing mission: to explore " |
| 42 | "strange new worlds, to seek out new life and new civilizations," |
| 43 | " to boldly go where no man has gone before.") |
| 44 | |
| 45 | def __init__(self, controllers): |
| 46 | BluetoothBaseTest.__init__(self, controllers) |
| 47 | self.server_droid, self.server_ed = self.droids[0], self.eds[0] |
| 48 | self.client_droid, self.client_ed = self.droids[1], self.eds[1] |
| 49 | self.tests = ( |
| 50 | "test_spp_connection", |
| 51 | ) |
| 52 | |
| 53 | def _clear_bonded_devices(self): |
| 54 | for d in self.droids: |
| 55 | bonded_device_list = d.bluetoothGetBondedDevices() |
| 56 | for device in bonded_device_list: |
| 57 | d.bluetoothUnbond(device['address']) |
| 58 | |
| 59 | def setup_class(self): |
| 60 | return setup_multiple_devices_for_bt_test(self.droids, self.eds) |
| 61 | |
| 62 | def setup_test(self): |
| 63 | self._clear_bonded_devices() |
| 64 | self.log.debug(log_energy_info(self.droids, "Start")) |
| 65 | for e in self.eds: |
| 66 | e.clear_all_events() |
| 67 | return True |
| 68 | |
| 69 | def teardown_test(self): |
| 70 | self.log.debug(log_energy_info(self.droids, "End")) |
| 71 | return True |
| 72 | |
| 73 | def on_fail(self, test_name, begin_time): |
| 74 | take_btsnoop_logs(self.droids, self, test_name) |
| 75 | reset_bluetooth(self.droids, self.eds) |
| 76 | |
| 77 | def teardown_test(self): |
| 78 | for thread in self.thread_list: |
| 79 | thread.join() |
| 80 | |
| 81 | def orchestrate_rfcomm_connect(self, server_mac): |
| 82 | accept_thread = threading.Thread( |
| 83 | target=rfcomm_accept, args=(self.server_droid,)) |
| 84 | self.thread_list.append(accept_thread) |
| 85 | accept_thread.start() |
| 86 | connect_thread = threading.Thread( |
| 87 | target=rfcomm_connect, args=(self.client_droid, server_mac)) |
| 88 | self.thread_list.append(connect_thread) |
| 89 | connect_thread.start() |
| 90 | |
| 91 | @BluetoothBaseTest.bt_test_wrap |
| 92 | def test_spp_connection(self): |
| 93 | """Test bluetooth SPP profile. |
| 94 | |
| 95 | Test SPP profile though establishing an RFCOMM connection. |
| 96 | |
| 97 | Steps: |
| 98 | 1. Get the mac address of the server device. |
| 99 | 2. Establish an RFCOMM connection from the client to the server AD. |
| 100 | 3. Verify that the RFCOMM connection is active from both the client and |
| 101 | server. |
| 102 | 4. Disconnect the RFCOMM connection. |
| 103 | |
| 104 | Expected Result: |
| 105 | RFCOMM connection is established then disconnected succcessfully. |
| 106 | |
| 107 | Returns: |
| 108 | Pass if True |
| 109 | Fail if False |
| 110 | |
| 111 | TAGS: Classic, SPP, RFCOMM |
| 112 | Priority: 1 |
| 113 | """ |
| 114 | server_mac = get_bt_mac_address(self.client_droid, self.server_droid) |
| 115 | # temporary workaround. Need to find out why I can't connect after I do |
| 116 | # a device discovery from get_bt_mac_address |
| 117 | reset_bluetooth([self.server_droid], [self.server_ed]) |
| 118 | self.orchestrate_rfcomm_connect(server_mac) |
| 119 | self.log.info("Write message.") |
| 120 | self.client_droid.bluetoothRfcommWrite(self.message) |
| 121 | self.log.info("Read message.") |
| 122 | read_msg = self.server_droid.bluetoothRfcommRead() |
| 123 | self.log.info("Verify message.") |
| 124 | assert self.message == read_msg, "Mismatch! Read {}".format(read_msg) |
| 125 | if len(self.server_droid.bluetoothRfcommActiveConnections()) == 0: |
| 126 | self.log.info("No rfcomm connections found on server.") |
| 127 | return False |
| 128 | if len(self.client_droid.bluetoothRfcommActiveConnections()) == 0: |
| 129 | self.log.info("no rfcomm connections found on client.") |
| 130 | return False |
| 131 | self.client_droid.bluetoothRfcommStop() |
| 132 | self.server_droid.bluetoothRfcommStop() |
| 133 | return True |