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 #pragma once 20 21 #include <gtest/gtest.h> 22 #include <hardware/bluetooth.h> 23 #include <hardware/bt_gatt.h> 24 #include <hardware/bt_pan.h> 25 #include <hardware/bt_sock.h> 26 #include <signal.h> 27 #include <time.h> 28 29 #include <condition_variable> 30 #include <map> 31 #include <mutex> 32 #include <string> 33 34 #include "types/raw_address.h" 35 36 class btsemaphore { 37 public: post()38 void post() { 39 std::lock_guard<std::mutex> lock(mMutex); 40 ++mCount; 41 mCondition.notify_one(); 42 } 43 wait()44 void wait() { 45 std::unique_lock<std::mutex> lock(mMutex); 46 while (!mCount) { 47 mCondition.wait(lock); 48 } 49 --mCount; 50 } 51 try_wait()52 bool try_wait() { 53 std::lock_guard<std::mutex> lock(mMutex); 54 if (mCount) { 55 --mCount; 56 return true; 57 } 58 return false; 59 } 60 61 private: 62 std::mutex mMutex; 63 std::condition_variable mCondition; 64 unsigned long mCount = 0; 65 }; 66 void semaphore_wait(btsemaphore &s); 67 void semaphore_post(btsemaphore &s); 68 void semaphore_try_wait(btsemaphore &s); 69 70 namespace bttest { 71 72 // This class represents the Bluetooth testing framework and provides 73 // helpers and callbacks for GUnit to use for testing. 74 class BluetoothTest : public ::testing::Test { 75 protected: 76 BluetoothTest() = default; 77 BluetoothTest(const BluetoothTest&) = delete; 78 BluetoothTest& operator=(const BluetoothTest&) = delete; 79 80 virtual ~BluetoothTest() = default; 81 82 // Getter for the bt_interface 83 const bt_interface_t* bt_interface(); 84 85 // Getter for the bt_callbacks 86 bt_callbacks_t* bt_callbacks(); 87 88 // Gets the current state of the Bluetooth Interface 89 bt_state_t GetState(); 90 91 // Get the number of properties that have changed on the 92 // Adapter Properties callback 93 int GetPropertiesChangedCount(); 94 95 // Get the value of a specific property 96 bt_property_t* GetProperty(bt_property_type_t type); 97 98 // Get the value of a specific remote device property 99 bt_property_t* GetRemoteDeviceProperty(const RawAddress* addr, 100 bt_property_type_t type); 101 102 // Get the current discovery state 103 bt_discovery_state_t GetDiscoveryState(); 104 105 // Get the current Acl State 106 bt_acl_state_t GetAclState(); 107 108 // Get the current Bond State 109 bt_bond_state_t GetBondState(); 110 111 // Reset a semaphores count to 0 112 void ClearSemaphore(btsemaphore& sem); 113 114 // SetUp initializes the Bluetooth interface and registers the callbacks 115 // before running every test 116 void SetUp() override; 117 118 // TearDown cleans up the stack and interface at the end of every test 119 void TearDown() override; 120 121 // A callback that is called when a property changes 122 friend void AdapterPropertiesCallback(bt_status_t status, int num_properties, 123 bt_property_t* properties); 124 125 // A callback that is called when the remote device's property changes 126 friend void RemoteDevicePropertiesCallback(bt_status_t status, 127 RawAddress* remote_bd_addr, 128 int num_properties, 129 bt_property_t* properties); 130 131 // A callback that is called when the adapter state changes 132 friend void AdapterStateChangedCallback(bt_state_t state); 133 134 // A callback that is called when the Discovery state changes 135 friend void DiscoveryStateChangedCallback(bt_discovery_state_t state); 136 137 // Semaphores used to wait for specific callback execution. Each callback 138 // has its own semaphore associated with it. 139 btsemaphore adapter_properties_callback_sem_; 140 btsemaphore remote_device_properties_callback_sem_; 141 btsemaphore adapter_state_changed_callback_sem_; 142 btsemaphore discovery_state_changed_callback_sem_; 143 144 private: 145 bt_state_t state_; 146 int properties_changed_count_; 147 bt_property_t* last_changed_properties_; 148 RawAddress curr_remote_device_; 149 int remote_device_properties_changed_count_; 150 bt_property_t* remote_device_last_changed_properties_; 151 bt_discovery_state_t discovery_state_; 152 bt_acl_state_t acl_state_; 153 bt_bond_state_t bond_state_; 154 }; 155 156 } // bttest 157