1#!/usr/bin/env python3 2# 3# Copyright (C) 2018 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"""This script shows simple examples of how to get started with bluetooth 17 low energy testing in acts. 18""" 19 20import pprint 21import random 22import time 23 24from acts.controllers import android_device 25from acts.test_utils.fuchsia.bt_test_utils import le_scan_for_device_by_name 26from acts.test_utils.bt.BluetoothBaseTest import BluetoothBaseTest 27from acts.test_utils.bt.bt_constants import ble_advertise_settings_modes 28from acts.test_utils.bt.bt_constants import adv_succ 29from acts.test_utils.bt.bt_constants import ble_scan_settings_modes 30from acts.test_utils.bt.bt_constants import scan_result 31from acts.test_utils.bt.bt_test_utils import cleanup_scanners_and_advertisers 32from acts.test_utils.bt.bt_test_utils import reset_bluetooth 33 34 35class BleFuchsiaAndroidTest(BluetoothBaseTest): 36 default_timeout = 10 37 active_adv_callback_list = [] 38 droid = None 39 40 def __init__(self, controllers): 41 BluetoothBaseTest.__init__(self, controllers) 42 43 # Android device under test 44 self.ad = self.android_devices[0] 45 # Fuchsia device under test 46 self.fd = self.fuchsia_devices[0] 47 self.log.info("There are: {} fuchsia and {} android devices.".format( 48 len(self.fuchsia_devices), len(self.android_devices))) 49 50 def teardown_test(self): 51 self.fd.clean_up() 52 53 def _start_generic_advertisement_include_device_name(self): 54 self.ad.droid.bleSetAdvertiseDataIncludeDeviceName(True) 55 self.ad.droid.bleSetAdvertiseSettingsAdvertiseMode( 56 ble_advertise_settings_modes['low_latency']) 57 advertise_data = self.ad.droid.bleBuildAdvertiseData() 58 advertise_settings = self.ad.droid.bleBuildAdvertiseSettings() 59 advertise_callback = self.ad.droid.bleGenBleAdvertiseCallback() 60 self.ad.droid.bleStartBleAdvertising( 61 advertise_callback, advertise_data, advertise_settings) 62 self.ad.ed.pop_event( 63 adv_succ.format(advertise_callback), self.default_timeout) 64 self.active_adv_callback_list.append(advertise_callback) 65 return advertise_callback 66 67 # Basic test for android device as advertiser and fuchsia device as scanner 68 # Returns True if scan result has an entry corresponding to sample_android_name 69 @BluetoothBaseTest.bt_test_wrap 70 def test_fuchsia_scan_android_adv(self): 71 sample_android_name = "Pixel1234" 72 self.ad.droid.bluetoothSetLocalName(sample_android_name) 73 adv_callback = self._start_generic_advertisement_include_device_name() 74 droid_name = self.ad.droid.bluetoothGetLocalName() 75 self.log.info("Android device name: {}".format(droid_name)) 76 res = True 77 if not le_scan_for_device_by_name( 78 self.fd, self.log, sample_android_name, self.default_timeout): 79 res = False 80 81 #Print clients to validate results are saved 82 self.fd.print_clients() 83 84 #Stop android advertising 85 self.ad.droid.bleStopBleAdvertising(adv_callback) 86 87 return res 88 89 # Test for fuchsia device attempting to connect to android device (peripheral) 90 # Also tests the list_services and discconect to a peripheral 91 @BluetoothBaseTest.bt_test_wrap 92 def test_fuchsia_connect_android_periph(self): 93 sample_android_name = "Pixel1234" 94 self.ad.droid.bluetoothStartPairingHelper() 95 self.ad.droid.bluetoothSetLocalName(sample_android_name) 96 adv_callback = self._start_generic_advertisement_include_device_name() 97 droid_name = self.ad.droid.bluetoothGetLocalName() 98 self.log.info("Android device name: {}".format(droid_name)) 99 100 scan_result = le_scan_for_device_by_name( 101 self.fd, self.log, sample_android_name, self.default_timeout) 102 if not scan_result: 103 return False 104 105 name, did, connectable = scan_result["name"], scan_result[ 106 "id"], scan_result["connectable"] 107 108 connect = self.fd.gattc_lib.bleConnectToPeripheral(did) 109 self.log.info("Connecting returned status: {}".format(connect)) 110 111 services = self.fd.gattc_lib.listServices(did) 112 self.log.info("Listing services returned: {}".format(services)) 113 114 dconnect = self.fd.gattc_lib.bleDisconnectPeripheral(did) 115 self.log.info("Disconnect status: {}".format(dconnect)) 116 117 #Print clients to validate results are saved 118 self.fd.print_clients() 119 120 #Stop android advertising + cleanup sl4f 121 self.ad.droid.bleStopBleAdvertising(adv_callback) 122 123 return True 124 125 # Currently, this test doesn't work. The android device does not scan 126 # TODO(): Debug android scan 127 @BluetoothBaseTest.bt_test_wrap 128 def test_fuchsia_adv_android_scan(self): 129 #Initialize advertising on fuchsia device with name and interval 130 fuchsia_name = "testADV123" 131 adv_data = {"name": fuchsia_name} 132 interval = 1000 133 134 #Start advertising 135 self.fd.ble_lib.bleStartBleAdvertising(adv_data, interval) 136 137 # Initialize scan on android device which scan settings + callback 138 filter_list = self.ad.droid.bleGenFilterList() 139 self.ad.droid.bleSetScanFilterDeviceName(fuchsia_name) 140 self.ad.droid.bleSetScanSettingsScanMode( 141 ble_scan_settings_modes['low_latency']) 142 scan_settings = self.ad.droid.bleBuildScanSetting() 143 scan_callback = self.ad.droid.bleGenScanCallback() 144 self.ad.droid.bleBuildScanFilter(filter_list) 145 self.ad.droid.bleStartBleScan(filter_list, scan_settings, 146 scan_callback) 147 event_name = scan_result.format(scan_callback) 148 try: 149 event = self.ad.ed.pop_event(event_name, self.default_timeout) 150 self.log.info("Found scan result: {}".format( 151 pprint.pformat(event))) 152 except Exception: 153 self.log.error("Didn't find any scan results.") 154 return False 155 finally: 156 self.fd.ble_lib.bleStopBleAdvertising() 157 self.ad.droid.bleStopBleScan(scan_callback) 158 # TODO(): Validate result 159 return True