1 /******************************************************************************
2 *
3 * Copyright (C) 2015 Google, Inc.
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 *
17 ******************************************************************************/
18
19 #include "adapter/bluetooth_test.h"
20
21 #include <mutex>
22
23 extern "C" {
24 #include "btcore/include/property.h"
25 }
26
27 namespace {
28
29 // Mutex lock used by callbacks to protect |callback_semaphores_| from
30 // racey behaviour caused when Wait and Notify are called at the same time
31 std::mutex callback_lock;
32
33 } // namespace
34
35 namespace bttest {
36
SetUp()37 void BluetoothTest::SetUp() {
38 bt_interface_ = nullptr;
39 state_ = BT_STATE_OFF;
40 properties_changed_count_ = 0;
41 last_changed_properties_ = nullptr;
42 discovery_state_ = BT_DISCOVERY_STOPPED;
43 acl_state_ = BT_ACL_STATE_DISCONNECTED;
44 bond_state_ = BT_BOND_STATE_NONE;
45
46 adapter_properties_callback_sem_ = semaphore_new(0);
47 adapter_state_changed_callback_sem_ = semaphore_new(0);
48 discovery_state_changed_callback_sem_ = semaphore_new(0);
49
50 bluetooth::hal::BluetoothInterface::Initialize();
51 ASSERT_TRUE(bluetooth::hal::BluetoothInterface::IsInitialized());
52 auto bt_hal_interface = bluetooth::hal::BluetoothInterface::Get();
53 bt_hal_interface->AddObserver(this);
54 bt_interface_ = bt_hal_interface->GetHALInterface();
55 ASSERT_NE(nullptr, bt_interface_) << "bt_interface is null.";
56 }
57
TearDown()58 void BluetoothTest::TearDown() {
59 semaphore_free(adapter_properties_callback_sem_);
60 semaphore_free(adapter_state_changed_callback_sem_);
61 semaphore_free(discovery_state_changed_callback_sem_);
62
63 auto bt_hal_interface = bluetooth::hal::BluetoothInterface::Get();
64 bt_hal_interface->RemoveObserver(this);
65 bt_hal_interface->CleanUp();
66 ASSERT_FALSE(bt_hal_interface->IsInitialized());
67 }
68
ClearSemaphore(semaphore_t * sem)69 void BluetoothTest::ClearSemaphore(semaphore_t* sem) {
70 while (semaphore_try_wait(sem));
71 }
72
bt_interface()73 const bt_interface_t* BluetoothTest::bt_interface() {
74 return bt_interface_;
75 }
76
GetState()77 bt_state_t BluetoothTest::GetState() {
78 return state_;
79 }
80
GetPropertiesChangedCount()81 int BluetoothTest::GetPropertiesChangedCount() {
82 return properties_changed_count_;
83 }
84
GetProperty(bt_property_type_t type)85 bt_property_t* BluetoothTest::GetProperty(bt_property_type_t type) {
86 for (int i = 0; i < properties_changed_count_; ++i) {
87 if (last_changed_properties_[i].type == type) {
88 return &last_changed_properties_[i];
89 }
90 }
91 return nullptr;
92 }
93
GetDiscoveryState()94 bt_discovery_state_t BluetoothTest::GetDiscoveryState() {
95 return discovery_state_;
96 }
97
GetAclState()98 bt_acl_state_t BluetoothTest::GetAclState() {
99 return acl_state_;
100 }
101
102 // Returns the device bond state.
GetBondState()103 bt_bond_state_t BluetoothTest::GetBondState() {
104 return bond_state_;
105 }
106
107 // callback
AdapterStateChangedCallback(bt_state_t new_state)108 void BluetoothTest::AdapterStateChangedCallback(bt_state_t new_state) {
109 state_ = new_state;
110 semaphore_post(adapter_state_changed_callback_sem_);
111 }
112
113 // callback
AdapterPropertiesCallback(bt_status_t status,int num_properties,bt_property_t * new_properties)114 void BluetoothTest::AdapterPropertiesCallback(
115 bt_status_t status,
116 int num_properties,
117 bt_property_t* new_properties) {
118 property_free_array(last_changed_properties_, properties_changed_count_);
119 last_changed_properties_ = property_copy_array(new_properties, num_properties);
120 properties_changed_count_ = num_properties;
121 semaphore_post(adapter_properties_callback_sem_);
122 }
123
124 // callback
DiscoveryStateChangedCallback(bt_discovery_state_t state)125 void BluetoothTest::DiscoveryStateChangedCallback(bt_discovery_state_t state) {
126 discovery_state_ = state;
127 semaphore_post(discovery_state_changed_callback_sem_);
128 }
129
130 } // bttest
131