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