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 24 def __init__(self, addr: str) -> None: 25 super().__init__(addr, "ble") 26 27 def _convert_human_readable_uuid_to_byte_list(self, readable_uuid): 28 """Converts a readable uuid to a byte list. 29 30 Args: 31 readable_uuid: string, A readable uuid in the format: 32 Input: "00001101-0000-1000-8000-00805f9b34fb" 33 Output: ['fb', '34', '9b', '5f', '80', '00', '00', '80', '00', 34 '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 57 return self.send_command(test_cmd, test_args) 58 59 def bleStartBleAdvertising(self, 60 advertising_data, 61 scan_response, 62 interval, 63 connectable=True): 64 """BleStartAdvertising command 65 66 Args: 67 advertising_data: dictionary, advertising data required for ble 68 advertise. 69 scan_response: dictionary, optional scan respones data to send. 70 interval: int, Advertising interval (in ms). 71 connectable: bool, whether the advertisement is connectable or not. 72 73 Returns: 74 Dictionary, None if success, error string if error. 75 """ 76 test_cmd = "ble_advertise_facade.BleAdvertise" 77 service_uuid_list = None 78 if type(advertising_data['service_uuids']) == list: 79 service_uuid_list = [] 80 for single_uuid in advertising_data['service_uuids']: 81 service_uuid_list.append( 82 self._convert_human_readable_uuid_to_byte_list( 83 single_uuid)) 84 advertising_data['service_uuids'] = service_uuid_list 85 86 service_uuid_list = None 87 if scan_response and type(scan_response['service_uuids']) == list: 88 service_uuid_list = [] 89 for single_uuid in scan_response['service_uuids']: 90 service_uuid_list.append( 91 self._convert_human_readable_uuid_to_byte_list( 92 single_uuid)) 93 scan_response['service_uuids'] = service_uuid_list 94 95 if scan_response and type(scan_response['service_data']) == list: 96 for service_data in scan_response['service_data']: 97 service_data[ 98 "uuid"] = self._convert_human_readable_uuid_to_byte_list( 99 service_data["uuid"]) 100 101 if type(advertising_data['service_data']) == list: 102 for service_data in advertising_data['service_data']: 103 service_data[ 104 "uuid"] = self._convert_human_readable_uuid_to_byte_list( 105 service_data["uuid"]) 106 107 test_args = { 108 "advertising_data": advertising_data, 109 "scan_response": scan_response, 110 "interval_ms": interval, 111 "connectable": connectable 112 } 113 return self.send_command(test_cmd, test_args) 114 115 def blePublishService(self, primary, type_, service_id): 116 """Publishes services specified by input args 117 118 Args: 119 primary: bool, Flag of service. 120 type: string, Canonical 8-4-4-4-12 uuid of service. 121 service_proxy_key: string, Unique identifier to specify where to publish service 122 123 Returns: 124 Dictionary, None if success, error if error. 125 """ 126 test_cmd = "bluetooth.BlePublishService" 127 test_args = { 128 "primary": primary, 129 "type": type_, 130 "local_service_id": service_id 131 } 132 133 return self.send_command(test_cmd, test_args) 134