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