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