1#!/usr/bin/env python3 2# 3# Copyright 2018 - The Android Open Source Project 4# 5# Licensed under the Apache License, Version 2.0 (the "License"); 6# you may not use this file except in compliance with the License. 7# You may obtain a copy of 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, 13# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14# See the License for the specific language governing permissions and 15# limitations under the License. 16 17from acts.controllers.fuchsia_lib.base_lib import BaseLib 18 19import uuid 20 21 22class FuchsiaBleLib(BaseLib): 23 def __init__(self, addr, tc, client_id): 24 self.address = addr 25 self.test_counter = tc 26 self.client_id = client_id 27 28 def _convert_human_readable_uuid_to_byte_list(self, readable_uuid): 29 """Converts a readable uuid to a byte list. 30 31 Args: 32 readable_uuid: string, A readable uuid in the format: 33 Input: "00001101-0000-1000-8000-00805f9b34fb" 34 Output: ['fb', '34', '9b', '5f', '80', '00', '00', '80', '00', '10', '00', '00', '01', '11', '00', '00'] 35 36 Returns: 37 A byte list representing the readable uuid. 38 """ 39 hex_uuid_str = uuid.UUID(readable_uuid).hex 40 break_n_bytes = 2 41 byte_list = [ 42 hex_uuid_str[i:i + break_n_bytes] 43 for i in range(0, len(hex_uuid_str), break_n_bytes) 44 ] 45 byte_list.reverse() 46 return byte_list 47 48 def bleStopBleAdvertising(self): 49 """BleStopAdvertising command 50 51 Returns: 52 Dictionary, None if success, error string if error. 53 """ 54 test_cmd = "ble_advertise_facade.BleStopAdvertise" 55 test_args = {} 56 test_id = self.build_id(self.test_counter) 57 self.test_counter += 1 58 59 return self.send_command(test_id, test_cmd, test_args) 60 61 def bleStartBleAdvertising(self, 62 advertising_data, 63 scan_response, 64 interval, 65 connectable=True): 66 """BleStartAdvertising command 67 68 Args: 69 advertising_data: dictionary, advertising data required for ble 70 advertise. 71 scan_response: dictionary, optional scan respones data to send. 72 interval: int, Advertising interval (in ms). 73 connectable: bool, whether the advertisement is connectable or not. 74 75 Returns: 76 Dictionary, None if success, error string if error. 77 """ 78 test_cmd = "ble_advertise_facade.BleAdvertise" 79 service_uuid_list = None 80 if type(advertising_data['service_uuids']) == list: 81 service_uuid_list = [] 82 for single_uuid in advertising_data['service_uuids']: 83 service_uuid_list.append( 84 self._convert_human_readable_uuid_to_byte_list( 85 single_uuid)) 86 advertising_data['service_uuids'] = service_uuid_list 87 88 service_uuid_list = None 89 if scan_response and type(scan_response['service_uuids']) == list: 90 service_uuid_list = [] 91 for single_uuid in scan_response['service_uuids']: 92 service_uuid_list.append( 93 self._convert_human_readable_uuid_to_byte_list( 94 single_uuid)) 95 scan_response['service_uuids'] = service_uuid_list 96 97 if scan_response and type(scan_response['service_data']) == list: 98 for service_data in scan_response['service_data']: 99 service_data[ 100 "uuid"] = self._convert_human_readable_uuid_to_byte_list( 101 service_data["uuid"]) 102 103 if type(advertising_data['service_data']) == list: 104 for service_data in advertising_data['service_data']: 105 service_data[ 106 "uuid"] = self._convert_human_readable_uuid_to_byte_list( 107 service_data["uuid"]) 108 109 test_args = { 110 "advertising_data": advertising_data, 111 "scan_response": scan_response, 112 "interval_ms": interval, 113 "connectable": connectable 114 } 115 test_id = self.build_id(self.test_counter) 116 self.test_counter += 1 117 return self.send_command(test_id, test_cmd, test_args) 118 119 def blePublishService(self, id_, primary, type_, service_id): 120 """Publishes services specified by input args 121 122 Args: 123 id: string, Identifier of service. 124 primary: bool, Flag of service. 125 type: string, Canonical 8-4-4-4-12 uuid of service. 126 service_proxy_key: string, Unique identifier to specify where to publish service 127 128 Returns: 129 Dictionary, None if success, error if error. 130 """ 131 test_cmd = "bluetooth.BlePublishService" 132 test_args = { 133 "id": id_, 134 "primary": primary, 135 "type": type_, 136 "local_service_id": service_id 137 } 138 test_id = self.build_id(self.test_counter) 139 self.test_counter += 1 140 141 return self.send_command(test_id, test_cmd, test_args) 142