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