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