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   uint64_t 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, bt_property_type_t type);
100 
101   // Get the current discovery state
102   bt_discovery_state_t GetDiscoveryState();
103 
104   // Get the current Acl State
105   bt_acl_state_t GetAclState();
106 
107   // Get the current Bond State
108   bt_bond_state_t GetBondState();
109 
110   // Reset a semaphores count to 0
111   void ClearSemaphore(btsemaphore& sem);
112 
113   // SetUp initializes the Bluetooth interface and registers the callbacks
114   // before running every test
115   void SetUp() override;
116 
117   // TearDown cleans up the stack and interface at the end of every test
118   void TearDown() override;
119 
120   // A callback that is called when a property changes
121   friend void AdapterPropertiesCallback(bt_status_t status, int num_properties,
122                                         bt_property_t* properties);
123 
124   // A callback that is called when the remote device's property changes
125   friend void RemoteDevicePropertiesCallback(bt_status_t status, RawAddress* remote_bd_addr,
126                                              int num_properties, bt_property_t* properties);
127 
128   // A callback that is called when the adapter state changes
129   friend void AdapterStateChangedCallback(bt_state_t state);
130 
131   // A callback that is called when the Discovery state changes
132   friend void DiscoveryStateChangedCallback(bt_discovery_state_t state);
133 
134   // Semaphores used to wait for specific callback execution. Each callback
135   // has its own semaphore associated with it.
136   btsemaphore adapter_properties_callback_sem_;
137   btsemaphore remote_device_properties_callback_sem_;
138   btsemaphore adapter_state_changed_callback_sem_;
139   btsemaphore discovery_state_changed_callback_sem_;
140 
141 private:
142   bt_state_t state_;
143   int properties_changed_count_;
144   bt_property_t* last_changed_properties_;
145   RawAddress curr_remote_device_;
146   int remote_device_properties_changed_count_;
147   bt_property_t* remote_device_last_changed_properties_;
148   bt_discovery_state_t discovery_state_;
149   bt_acl_state_t acl_state_;
150   bt_bond_state_t bond_state_;
151 };
152 
153 }  // namespace bttest
154