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