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 = {"name": fuchsia_name} 60 interval = 1000 61 res = True 62 63 # Start advertising 64 self.fuchsia_adv.ble_lib.bleStartBleAdvertising(adv_data, interval) 65 self.log.info("Fuchsia advertising name: {}".format(fuchsia_name)) 66 67 # Start scan 68 scan_result = le_scan_for_device_by_name( 69 self.fuchsia_scan, self.log, fuchsia_name, self.default_timeout) 70 if not scan_result: 71 res = False 72 73 # Stop advertising 74 self.fuchsia_adv.ble_lib.bleStopBleAdvertising() 75 76 return res 77 78 def test_fuchsia_gatt_fuchsia_periph(self): 79 # Create random service with id, primary, and uuid 80 service_id = 3 81 service_primary = True 82 # Random uuid 83 service_type = "0000180f-0000-1000-8000-00805fffffff" 84 85 # Generate a random key for sl4f storage of proxy key 86 service_proxy_key = "SProxy" + str(random.randint(0, 1000000)) 87 res = self.fuchsia_adv.ble_lib.blePublishService( 88 service_id, service_primary, service_type, service_proxy_key) 89 self.log.info("Publish result: {}".format(res)) 90 91 # Initialize advertising on fuchsia dveice with name and interval 92 fuchsia_name = "testADV1234" 93 adv_data = {"name": fuchsia_name} 94 interval = 1000 95 96 # Start advertising 97 self.fuchsia_adv.ble_lib.bleStartBleAdvertising(adv_data, interval) 98 self.log.info("Fuchsia advertising name: {}".format(fuchsia_name)) 99 100 # Start Scan 101 scan_result = le_scan_for_device_by_name( 102 self.fuchsia_scan, self.log, fuchsia_name, self.default_timeout) 103 if not scan_result: 104 self.fuchsia_adv.ble_lib.bleStopBleAdvertising() 105 return False 106 107 name, did, connectable = scan_result["name"], scan_result[ 108 "id"], scan_result["connectable"] 109 110 connect = self.fuchsia_scan.gattc_lib.bleConnectToPeripheral(did) 111 self.log.info("Connecting returned status: {}".format(connect)) 112 113 services = self.fuchsia_scan.gattc_lib.listServices(did) 114 self.log.info("Listing services returned: {}".format(services)) 115 116 dconnect = self.fuchsia_scan.gattc_lib.bleDisconnectPeripheral(did) 117 self.log.info("Disconnect status: {}".format(dconnect)) 118 119 # Stop fuchsia advertising 120 self.fuchsia_adv.ble_lib.bleStopBleAdvertising() 121 122 return True 123