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 scrip tests various BLE apis for Fuchsia devices. 17""" 18 19import random 20 21from acts.base_test import BaseTestClass 22from acts_contrib.test_utils.fuchsia.bt_test_utils import le_scan_for_device_by_name 23 24 25class BleFuchsiaTest(BaseTestClass): 26 default_timeout = 10 27 active_scan_callback_list = [] 28 active_adv_callback_list = [] 29 droid = None 30 31 def setup_class(self): 32 super().setup_class() 33 34 if (len(self.fuchsia_devices) < 2): 35 self.log.error("BleFuchsiaTest Init: Not enough fuchsia devices.") 36 self.log.info("Running testbed setup with two fuchsia devices") 37 self.fuchsia_adv = self.fuchsia_devices[0] 38 self.fuchsia_scan = self.fuchsia_devices[1] 39 40 def test_fuchsia_publish_service(self): 41 service_primary = True 42 # Random uuid 43 service_type = "0000180f-0000-1000-8000-00805fffffff" 44 45 # Generate a random key for sl4f storage of proxy key 46 service_proxy_key = "SProxy" + str(random.randint(0, 1000000)) 47 res = self.fuchsia_adv.sl4f.ble_lib.blePublishService( 48 service_primary, service_type, service_proxy_key) 49 self.log.info("Publish result: {}".format(res)) 50 51 return True 52 53 def test_fuchsia_scan_fuchsia_adv(self): 54 # Initialize advertising on fuchsia dveice with name and interval 55 fuchsia_name = "testADV1234" 56 adv_data = { 57 "name": fuchsia_name, 58 "appearance": None, 59 "service_data": None, 60 "tx_power_level": None, 61 "service_uuids": None, 62 "manufacturer_data": None, 63 "uris": None, 64 } 65 scan_response = None 66 connectable = True 67 interval = 1000 68 res = True 69 70 # Start advertising 71 self.fuchsia_adv.sl4f.ble_lib.bleStartBleAdvertising( 72 adv_data, scan_response, interval, connectable) 73 self.log.info("Fuchsia advertising name: {}".format(fuchsia_name)) 74 75 # Start scan 76 scan_result = le_scan_for_device_by_name(self.fuchsia_scan, self.log, 77 fuchsia_name, 78 self.default_timeout) 79 if not scan_result: 80 res = False 81 82 # Stop advertising 83 self.fuchsia_adv.sl4f.ble_lib.bleStopBleAdvertising() 84 85 return res 86 87 def test_fuchsia_gatt_fuchsia_periph(self): 88 # Create random service with primary, and uuid 89 service_primary = True 90 # Random uuid 91 service_type = "0000180f-0000-1000-8000-00805fffffff" 92 93 # Generate a random key for sl4f storage of proxy key 94 service_proxy_key = "SProxy" + str(random.randint(0, 1000000)) 95 res = self.fuchsia_adv.sl4f.ble_lib.blePublishService( 96 service_primary, service_type, service_proxy_key) 97 self.log.info("Publish result: {}".format(res)) 98 99 # Initialize advertising on fuchsia dveice with name and interval 100 fuchsia_name = "testADV1234" 101 adv_data = { 102 "name": fuchsia_name, 103 "appearance": None, 104 "service_data": None, 105 "tx_power_level": None, 106 "service_uuids": None, 107 "manufacturer_data": None, 108 "uris": None, 109 } 110 scan_response = None 111 connectable = True 112 interval = 1000 113 114 # Start advertising 115 self.fuchsia_adv.sl4f.ble_lib.bleStartBleAdvertising( 116 adv_data, scan_response, interval, connectable) 117 self.log.info("Fuchsia advertising name: {}".format(fuchsia_name)) 118 119 # Start Scan 120 scan_result = le_scan_for_device_by_name(self.fuchsia_scan, self.log, 121 fuchsia_name, 122 self.default_timeout) 123 if not scan_result: 124 self.fuchsia_adv.sl4f.ble_lib.bleStopBleAdvertising() 125 return False 126 127 name, did, connectable = scan_result["name"], scan_result[ 128 "id"], scan_result["connectable"] 129 130 connect = self.fuchsia_scan.sl4f.gattc_lib.bleConnectToPeripheral(did) 131 self.log.info("Connecting returned status: {}".format(connect)) 132 133 services = self.fuchsia_scan.sl4f.gattc_lib.listServices(did) 134 self.log.info("Listing services returned: {}".format(services)) 135 136 dconnect = self.fuchsia_scan.sl4f.gattc_lib.bleDisconnectPeripheral( 137 did) 138 self.log.info("Disconnect status: {}".format(dconnect)) 139 140 # Stop fuchsia advertising 141 self.fuchsia_adv.sl4f.ble_lib.bleStopBleAdvertising() 142 143 return True 144