bluetooth_test.h revision fa717c0b47bb1fed04da295eaa580a92bc08c622
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
27extern "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
39namespace bttest {
40
41// This class represents the Bluetooth testing framework and provides
42// helpers and callbacks for GUnit to use for testing.
43class 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 value of a specific remote device property
63  bt_property_t* GetRemoteDeviceProperty(const bt_bdaddr_t* addr,
64                                         bt_property_type_t type);
65
66  // Get the current discovery state
67  bt_discovery_state_t GetDiscoveryState();
68
69  // Get the current Acl State
70  bt_acl_state_t GetAclState();
71
72  // Get the current Bond State
73  bt_bond_state_t GetBondState();
74
75  // Reset a semaphores count to 0
76  void ClearSemaphore(semaphore_t* sem);
77
78  // SetUp initializes the Bluetooth interface and registers the callbacks
79  // before running every test
80  void SetUp() override;
81
82  // TearDown cleans up the stack and interface at the end of every test
83  void TearDown() override;
84
85  // A callback that is called when a property changes
86  void AdapterPropertiesCallback(
87      bt_status_t status,
88      int num_properties,
89      bt_property_t *properties);
90
91  // A callback that is called when the remote device's property changes
92  void RemoteDevicePropertiesCallback(
93      bt_status_t status,
94      bt_bdaddr_t *remote_bd_addr,
95      int num_properties,
96      bt_property_t *properties);
97
98  // A callback that is called when the adapter state changes
99  void AdapterStateChangedCallback(bt_state_t state);
100
101  // A callback that is called when the Discovery state changes
102  void DiscoveryStateChangedCallback(bt_discovery_state_t state);
103
104  // Semaphores used to wait for specific callback execution. Each callback
105  // has its own semaphore associated with it.
106  semaphore_t* adapter_properties_callback_sem_;
107  semaphore_t* remote_device_properties_callback_sem_;
108  semaphore_t* adapter_state_changed_callback_sem_;
109  semaphore_t* discovery_state_changed_callback_sem_;
110
111 private:
112  // The bluetooth interface that all the tests use to interact with the HAL
113  const bt_interface_t* bt_interface_;
114
115  bt_state_t state_;
116  int properties_changed_count_;
117  bt_property_t *last_changed_properties_;
118  bt_bdaddr_t curr_remote_device_;
119  int remote_device_properties_changed_count_;
120  bt_property_t *remote_device_last_changed_properties_;
121  bt_discovery_state_t discovery_state_;
122  bt_acl_state_t acl_state_;
123  bt_bond_state_t bond_state_;
124
125  DISALLOW_COPY_AND_ASSIGN(BluetoothTest);
126};
127
128}  // bttest
129