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