1 /******************************************************************************
2 *
3 * Copyright 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 <binder/ProcessState.h>
22
23 #include <mutex>
24
25 #include "btcore/include/property.h"
26 #include "types/raw_address.h"
27
28 extern bt_interface_t bluetoothInterface;
29
semaphore_wait(btsemaphore & s)30 void semaphore_wait(btsemaphore &s) {
31 s.wait();
32 }
semaphore_post(btsemaphore & s)33 void semaphore_post(btsemaphore &s) {
34 s.post();
35 }
semaphore_try_wait(btsemaphore & s)36 void semaphore_try_wait(btsemaphore &s) {
37 s.try_wait();
38 }
39
40 namespace bttest {
41
42 static BluetoothTest* instance = nullptr;
43
AdapterStateChangedCallback(bt_state_t new_state)44 void AdapterStateChangedCallback(bt_state_t new_state) {
45 instance->state_ = new_state;
46 semaphore_post(instance->adapter_state_changed_callback_sem_);
47 }
48
AdapterPropertiesCallback(bt_status_t status,int num_properties,bt_property_t * new_properties)49 void AdapterPropertiesCallback(bt_status_t status, int num_properties,
50 bt_property_t* new_properties) {
51 property_free_array(instance->last_changed_properties_,
52 instance->properties_changed_count_);
53 instance->last_changed_properties_ =
54 property_copy_array(new_properties, num_properties);
55 instance->properties_changed_count_ = num_properties;
56 semaphore_post(instance->adapter_properties_callback_sem_);
57 }
58
RemoteDevicePropertiesCallback(bt_status_t status,RawAddress * remote_bd_addr,int num_properties,bt_property_t * properties)59 void RemoteDevicePropertiesCallback(bt_status_t status,
60 RawAddress* remote_bd_addr,
61 int num_properties,
62 bt_property_t* properties) {
63 instance->curr_remote_device_ = *remote_bd_addr;
64 property_free_array(instance->remote_device_last_changed_properties_,
65 instance->remote_device_properties_changed_count_);
66 instance->remote_device_last_changed_properties_ =
67 property_copy_array(properties, num_properties);
68 instance->remote_device_properties_changed_count_ = num_properties;
69 semaphore_post(instance->remote_device_properties_callback_sem_);
70 }
71
DiscoveryStateChangedCallback(bt_discovery_state_t state)72 void DiscoveryStateChangedCallback(bt_discovery_state_t state) {
73 instance->discovery_state_ = state;
74 semaphore_post(instance->discovery_state_changed_callback_sem_);
75 }
76
77 static bt_callbacks_t callbacks = {
78 .size = sizeof(bt_callbacks_t),
79 .adapter_state_changed_cb = AdapterStateChangedCallback,
80 .adapter_properties_cb = AdapterPropertiesCallback,
81 .remote_device_properties_cb = RemoteDevicePropertiesCallback,
82 .discovery_state_changed_cb = DiscoveryStateChangedCallback,
83 };
84
SetUp()85 void BluetoothTest::SetUp() {
86 android::ProcessState::self()->startThreadPool();
87 state_ = BT_STATE_OFF;
88 properties_changed_count_ = 0;
89 last_changed_properties_ = nullptr;
90 remote_device_properties_changed_count_ = 0;
91 remote_device_last_changed_properties_ = nullptr;
92 discovery_state_ = BT_DISCOVERY_STOPPED;
93 acl_state_ = BT_ACL_STATE_DISCONNECTED;
94 bond_state_ = BT_BOND_STATE_NONE;
95
96 remove("/data/misc/bluedroid/bt_config.conf.encrypted-checksum");
97
98 instance = this;
99 int status = bluetoothInterface.init(&callbacks, false, false, 0, nullptr,
100 false, nullptr);
101 ASSERT_EQ(status, BT_STATUS_SUCCESS);
102 }
103
TearDown()104 void BluetoothTest::TearDown() {
105 bluetoothInterface.cleanup();
106 instance = nullptr;
107 }
108
ClearSemaphore(btsemaphore & sem)109 void BluetoothTest::ClearSemaphore(btsemaphore& sem) {
110 while (sem.try_wait())
111 ;
112 }
113
bt_interface()114 const bt_interface_t* BluetoothTest::bt_interface() {
115 return &bluetoothInterface;
116 }
117
bt_callbacks()118 bt_callbacks_t* BluetoothTest::bt_callbacks() { return &callbacks; }
119
GetState()120 bt_state_t BluetoothTest::GetState() { return state_; }
121
GetPropertiesChangedCount()122 int BluetoothTest::GetPropertiesChangedCount() {
123 return properties_changed_count_;
124 }
125
GetProperty(bt_property_type_t type)126 bt_property_t* BluetoothTest::GetProperty(bt_property_type_t type) {
127 for (int i = 0; i < properties_changed_count_; ++i) {
128 if (last_changed_properties_[i].type == type) {
129 return &last_changed_properties_[i];
130 }
131 }
132 return nullptr;
133 }
134
GetRemoteDeviceProperty(const RawAddress * addr,bt_property_type_t type)135 bt_property_t* BluetoothTest::GetRemoteDeviceProperty(const RawAddress* addr,
136 bt_property_type_t type) {
137 if (curr_remote_device_ != *addr) return nullptr;
138
139 for (int i = 0; i < remote_device_properties_changed_count_; i++) {
140 if (remote_device_last_changed_properties_[i].type == type) {
141 return &remote_device_last_changed_properties_[i];
142 }
143 }
144 return nullptr;
145 }
146
GetDiscoveryState()147 bt_discovery_state_t BluetoothTest::GetDiscoveryState() {
148 return discovery_state_;
149 }
150
GetAclState()151 bt_acl_state_t BluetoothTest::GetAclState() { return acl_state_; }
152
153 // Returns the device bond state.
GetBondState()154 bt_bond_state_t BluetoothTest::GetBondState() { return bond_state_; }
155
156 } // namespace bttest
157