bluetooth_test.h revision 74ef54b440dc50bb25f587783d80fe5a37070e4a
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
34namespace bttest {
35
36// This class represents the Bluetooth testing framework and provides
37// helpers and callbacks for GUnit to use for testing.
38class 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(
82      bt_status_t status,
83      int num_properties,
84      bt_property_t *properties);
85
86  // A callback that is called when the remote device's property changes
87  void RemoteDevicePropertiesCallback(
88      bt_status_t status,
89      bt_bdaddr_t *remote_bd_addr,
90      int num_properties,
91      bt_property_t *properties);
92
93  // A callback that is called when the adapter state changes
94  void AdapterStateChangedCallback(bt_state_t state);
95
96  // A callback that is called when the Discovery state changes
97  void DiscoveryStateChangedCallback(bt_discovery_state_t state);
98
99  // Semaphores used to wait for specific callback execution. Each callback
100  // has its own semaphore associated with it.
101  semaphore_t* adapter_properties_callback_sem_;
102  semaphore_t* remote_device_properties_callback_sem_;
103  semaphore_t* adapter_state_changed_callback_sem_;
104  semaphore_t* discovery_state_changed_callback_sem_;
105
106 private:
107  // The bluetooth interface that all the tests use to interact with the HAL
108  const bt_interface_t* bt_interface_;
109
110  bt_state_t state_;
111  int properties_changed_count_;
112  bt_property_t *last_changed_properties_;
113  bt_bdaddr_t curr_remote_device_;
114  int remote_device_properties_changed_count_;
115  bt_property_t *remote_device_last_changed_properties_;
116  bt_discovery_state_t discovery_state_;
117  bt_acl_state_t acl_state_;
118  bt_bond_state_t bond_state_;
119
120  DISALLOW_COPY_AND_ASSIGN(BluetoothTest);
121};
122
123}  // bttest
124