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