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