1#!/usr/bin/env python3 2# 3# Copyright (C) 2016 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""" 17Bluetooth adapter libraries 18""" 19 20from acts_contrib.test_utils.bt.bt_constants import bt_rfcomm_uuids 21from acts_contrib.test_utils.bt.bt_test_utils import set_bt_scan_mode 22 23 24class RfcommLib(): 25 def __init__(self, log, dut, target_mac_addr=None): 26 self.advertisement_list = [] 27 self.dut = dut 28 self.log = log 29 self.target_mac_addr = target_mac_addr 30 31 def set_target_mac_addr(self, mac_addr): 32 self.target_mac_addr = mac_addr 33 34 def connect(self, line): 35 """Perform an RFCOMM connect""" 36 uuid = None 37 if len(line) > 0: 38 uuid = line 39 if uuid: 40 self.dut.droid.bluetoothRfcommBeginConnectThread( 41 self.target_mac_addr, uuid) 42 else: 43 self.dut.droid.bluetoothRfcommBeginConnectThread( 44 self.target_mac_addr) 45 46 def open_rfcomm_socket(self): 47 """Open rfcomm socket""" 48 self.dut.droid.rfcommCreateRfcommSocket(self.target_mac_addr, 1) 49 50 def open_l2cap_socket(self): 51 """Open L2CAP socket""" 52 self.dut.droid.rfcommCreateL2capSocket(self.target_mac_addr, 1) 53 54 def write(self, line): 55 """Write String data over an RFCOMM connection""" 56 self.dut.droid.bluetoothRfcommWrite(line) 57 58 def write_binary(self, line): 59 """Write String data over an RFCOMM connection""" 60 self.dut.droid.bluetoothRfcommWriteBinary(line) 61 62 def end_connect(self): 63 """End RFCOMM connection""" 64 self.dut.droid.bluetoothRfcommEndConnectThread() 65 66 def accept(self, line): 67 """Accept RFCOMM connection""" 68 uuid = None 69 if len(line) > 0: 70 uuid = line 71 if uuid: 72 self.dut.droid.bluetoothRfcommBeginAcceptThread(uuid) 73 else: 74 self.dut.droid.bluetoothRfcommBeginAcceptThread( 75 bt_rfcomm_uuids['base_uuid']) 76 77 def stop(self): 78 """Stop RFCOMM Connection""" 79 self.dut.droid.bluetoothRfcommStop() 80 81 def open_l2cap_socket(self): 82 """Open L2CAP socket""" 83 self.dut.droid.rfcommCreateL2capSocket(self.target_mac_addr, 1) 84