1 /******************************************************************************
2  *
3  *  Copyright (C) 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 <hardware/hardware.h>
27 #include <signal.h>
28 #include <time.h>
29 #include <map>
30 #include <string>
31 #include "osi/include/semaphore.h"
32 #include "service/hal/bluetooth_interface.h"
33 
34 namespace bttest {
35 
36 // This class represents the Bluetooth testing framework and provides
37 // helpers and callbacks for GUnit to use for testing.
38 class BluetoothTest : public ::testing::Test,
39                       public bluetooth::hal::BluetoothInterface::Observer {
40  protected:
41   BluetoothTest() = default;
42   virtual ~BluetoothTest() = default;
43 
44   // Getter for the bt_interface
45   const bt_interface_t* bt_interface();
46 
47   // Gets the current state of the Bluetooth Interface
48   bt_state_t GetState();
49 
50   // Get the number of properties that have changed on the
51   // Adapter Properties callback
52   int GetPropertiesChangedCount();
53 
54   // Get the value of a specific property
55   bt_property_t* GetProperty(bt_property_type_t type);
56 
57   // Get the value of a specific remote device property
58   bt_property_t* GetRemoteDeviceProperty(const bt_bdaddr_t* addr,
59                                          bt_property_type_t type);
60 
61   // Get the current discovery state
62   bt_discovery_state_t GetDiscoveryState();
63 
64   // Get the current Acl State
65   bt_acl_state_t GetAclState();
66 
67   // Get the current Bond State
68   bt_bond_state_t GetBondState();
69 
70   // Reset a semaphores count to 0
71   void ClearSemaphore(semaphore_t* sem);
72 
73   // SetUp initializes the Bluetooth interface and registers the callbacks
74   // before running every test
75   void SetUp() override;
76 
77   // TearDown cleans up the stack and interface at the end of every test
78   void TearDown() override;
79 
80   // A callback that is called when a property changes
81   void AdapterPropertiesCallback(bt_status_t status, int num_properties,
82                                  bt_property_t* properties) override;
83 
84   // A callback that is called when the remote device's property changes
85   void RemoteDevicePropertiesCallback(bt_status_t status,
86                                       bt_bdaddr_t* remote_bd_addr,
87                                       int num_properties,
88                                       bt_property_t* properties) override;
89 
90   // A callback that is called when the adapter state changes
91   void AdapterStateChangedCallback(bt_state_t state) override;
92 
93   // A callback that is called when the Discovery state changes
94   void DiscoveryStateChangedCallback(bt_discovery_state_t state) override;
95 
96   // Semaphores used to wait for specific callback execution. Each callback
97   // has its own semaphore associated with it.
98   semaphore_t* adapter_properties_callback_sem_;
99   semaphore_t* remote_device_properties_callback_sem_;
100   semaphore_t* adapter_state_changed_callback_sem_;
101   semaphore_t* discovery_state_changed_callback_sem_;
102 
103  private:
104   // The bluetooth interface that all the tests use to interact with the HAL
105   const bt_interface_t* bt_interface_;
106 
107   bt_state_t state_;
108   int properties_changed_count_;
109   bt_property_t* last_changed_properties_;
110   bt_bdaddr_t curr_remote_device_;
111   int remote_device_properties_changed_count_;
112   bt_property_t* remote_device_last_changed_properties_;
113   bt_discovery_state_t discovery_state_;
114   bt_acl_state_t acl_state_;
115   bt_bond_state_t bond_state_;
116 
117   DISALLOW_COPY_AND_ASSIGN(BluetoothTest);
118 };
119 
120 }  // bttest
121