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