summaryrefslogtreecommitdiff
path: root/system/test/suite/rfcomm/rfcomm_test.cc
blob: 1c38501ee41300018759763c8404a75ba56f5ea7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
/******************************************************************************
 *
 *  Copyright 2016 Google, Inc.
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at:
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 ******************************************************************************/

#include "rfcomm/rfcomm_test.h"

#include "adapter/bluetooth_test.h"
#include "types/bluetooth/uuid.h"
#include "types/raw_address.h"

using bluetooth::Uuid;

namespace bttest {

const Uuid RFCommTest::HFP_UUID = Uuid::From16Bit(0x111E);

void RFCommTest::SetUp() {
  BluetoothTest::SetUp();

  ASSERT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);
  ASSERT_TRUE(GetState() == BT_STATE_ON);
  socket_interface_ =
      (const btsock_interface_t*)bt_interface()->get_profile_interface(
          BT_PROFILE_SOCKETS_ID);
  ASSERT_NE(socket_interface_, nullptr);

  // Find a bonded device that supports HFP
  bt_remote_bdaddr_ = RawAddress::kEmpty;

  bt_property_t* bonded_devices_prop =
      GetProperty(BT_PROPERTY_ADAPTER_BONDED_DEVICES);
  RawAddress* devices = (RawAddress*)bonded_devices_prop->val;
  int num_bonded_devices = bonded_devices_prop->len / sizeof(RawAddress);

  for (int i = 0; i < num_bonded_devices && bt_remote_bdaddr_.IsEmpty(); i++) {
    ClearSemaphore(remote_device_properties_callback_sem_);
    bt_interface()->get_remote_device_property(&devices[i], BT_PROPERTY_UUIDS);
    semaphore_wait(remote_device_properties_callback_sem_);

    bt_property_t* uuid_prop =
        GetRemoteDeviceProperty(&devices[i], BT_PROPERTY_UUIDS);
    if (uuid_prop == nullptr) continue;
    Uuid* uuids = reinterpret_cast<Uuid*>(uuid_prop->val);
    int num_uuids = uuid_prop->len / sizeof(Uuid);

    for (int j = 0; j < num_uuids; j++) {
      if (!memcmp(uuids + j, &HFP_UUID, sizeof(Uuid))) {
        bt_remote_bdaddr_ = *(devices + i);
        break;
      }
    }
  }

  ASSERT_FALSE(bt_remote_bdaddr_.IsEmpty())
      << "Could not find paired device that supports HFP";
}

void RFCommTest::TearDown() {
  socket_interface_ = NULL;

  ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
  semaphore_wait(adapter_state_changed_callback_sem_);

  BluetoothTest::TearDown();
}

}  // bttest