• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <binder/ProcessState.h>
21 #include <stdio.h>
22 #include <mutex>
23 #include "btcore/include/property.h"
24 
25 namespace {
26 
27 // Mutex lock used by callbacks to protect |callback_semaphores_| from
28 // racey behaviour caused when Wait and Notify are called at the same time
29 std::mutex callback_lock;
30 
31 }  // namespace
32 
33 namespace bttest {
34 
SetUp()35 void BluetoothTest::SetUp() {
36   android::ProcessState::self()->startThreadPool();
37   bt_interface_ = nullptr;
38   state_ = BT_STATE_OFF;
39   properties_changed_count_ = 0;
40   last_changed_properties_ = nullptr;
41   remote_device_properties_changed_count_ = 0;
42   remote_device_last_changed_properties_ = nullptr;
43   discovery_state_ = BT_DISCOVERY_STOPPED;
44   acl_state_ = BT_ACL_STATE_DISCONNECTED;
45   bond_state_ = BT_BOND_STATE_NONE;
46 
47   adapter_properties_callback_sem_ = semaphore_new(0);
48   remote_device_properties_callback_sem_ = semaphore_new(0);
49   adapter_state_changed_callback_sem_ = semaphore_new(0);
50   discovery_state_changed_callback_sem_ = semaphore_new(0);
51 
52   remove("/data/misc/bluedroid/bt_config.conf.encrypted-checksum");
53   remove("/data/misc/bluedroid/bt_config.bak.encrypted-checksum");
54 
55   bluetooth::hal::BluetoothInterface::Initialize();
56   ASSERT_TRUE(bluetooth::hal::BluetoothInterface::IsInitialized());
57   auto bt_hal_interface = bluetooth::hal::BluetoothInterface::Get();
58   bt_hal_interface->AddObserver(this);
59   bt_interface_ = bt_hal_interface->GetHALInterface();
60   ASSERT_NE(nullptr, bt_interface_) << "bt_interface is null.";
61 }
62 
TearDown()63 void BluetoothTest::TearDown() {
64   semaphore_free(adapter_properties_callback_sem_);
65   semaphore_free(remote_device_properties_callback_sem_);
66   semaphore_free(adapter_state_changed_callback_sem_);
67   semaphore_free(discovery_state_changed_callback_sem_);
68 
69   auto bt_hal_interface = bluetooth::hal::BluetoothInterface::Get();
70   bt_hal_interface->RemoveObserver(this);
71   bt_hal_interface->CleanUp();
72   ASSERT_FALSE(bt_hal_interface->IsInitialized());
73 }
74 
ClearSemaphore(semaphore_t * sem)75 void BluetoothTest::ClearSemaphore(semaphore_t* sem) {
76   while (semaphore_try_wait(sem))
77     ;
78 }
79 
bt_interface()80 const bt_interface_t* BluetoothTest::bt_interface() { return bt_interface_; }
81 
GetState()82 bt_state_t BluetoothTest::GetState() { return state_; }
83 
GetPropertiesChangedCount()84 int BluetoothTest::GetPropertiesChangedCount() {
85   return properties_changed_count_;
86 }
87 
GetProperty(bt_property_type_t type)88 bt_property_t* BluetoothTest::GetProperty(bt_property_type_t type) {
89   for (int i = 0; i < properties_changed_count_; ++i) {
90     if (last_changed_properties_[i].type == type) {
91       return &last_changed_properties_[i];
92     }
93   }
94   return nullptr;
95 }
96 
GetRemoteDeviceProperty(const RawAddress * addr,bt_property_type_t type)97 bt_property_t* BluetoothTest::GetRemoteDeviceProperty(const RawAddress* addr,
98                                                       bt_property_type_t type) {
99   if (curr_remote_device_ != *addr) return nullptr;
100 
101   for (int i = 0; i < remote_device_properties_changed_count_; i++) {
102     if (remote_device_last_changed_properties_[i].type == type) {
103       return &remote_device_last_changed_properties_[i];
104     }
105   }
106   return nullptr;
107 }
108 
GetDiscoveryState()109 bt_discovery_state_t BluetoothTest::GetDiscoveryState() {
110   return discovery_state_;
111 }
112 
GetAclState()113 bt_acl_state_t BluetoothTest::GetAclState() { return acl_state_; }
114 
115 // Returns the device bond state.
GetBondState()116 bt_bond_state_t BluetoothTest::GetBondState() { return bond_state_; }
117 
118 // callback
AdapterStateChangedCallback(bt_state_t new_state)119 void BluetoothTest::AdapterStateChangedCallback(bt_state_t new_state) {
120   state_ = new_state;
121   semaphore_post(adapter_state_changed_callback_sem_);
122 }
123 
124 // callback
AdapterPropertiesCallback(bt_status_t status,int num_properties,bt_property_t * new_properties)125 void BluetoothTest::AdapterPropertiesCallback(bt_status_t status,
126                                               int num_properties,
127                                               bt_property_t* new_properties) {
128   property_free_array(last_changed_properties_, properties_changed_count_);
129   last_changed_properties_ =
130       property_copy_array(new_properties, num_properties);
131   properties_changed_count_ = num_properties;
132   semaphore_post(adapter_properties_callback_sem_);
133 }
134 
135 // callback
RemoteDevicePropertiesCallback(bt_status_t status,RawAddress * remote_bd_addr,int num_properties,bt_property_t * properties)136 void BluetoothTest::RemoteDevicePropertiesCallback(bt_status_t status,
137                                                    RawAddress* remote_bd_addr,
138                                                    int num_properties,
139                                                    bt_property_t* properties) {
140   curr_remote_device_ = *remote_bd_addr;
141   property_free_array(remote_device_last_changed_properties_,
142                       remote_device_properties_changed_count_);
143   remote_device_last_changed_properties_ =
144       property_copy_array(properties, num_properties);
145   remote_device_properties_changed_count_ = num_properties;
146   semaphore_post(remote_device_properties_callback_sem_);
147 }
148 
149 // callback
DiscoveryStateChangedCallback(bt_discovery_state_t state)150 void BluetoothTest::DiscoveryStateChangedCallback(bt_discovery_state_t state) {
151   discovery_state_ = state;
152   semaphore_post(discovery_state_changed_callback_sem_);
153 }
154 
155 }  // bttest
156