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#include "gatt/gatt_test.h"
20#include "adapter/bluetooth_test.h"
21#include "btcore/include/bdaddr.h"
22
23namespace bttest {
24
25void GattTest::SetUp() {
26  gatt_client_interface_ = nullptr;
27  gatt_server_interface_ = nullptr;
28
29  client_interface_id_ = 0;
30  server_interface_id_ = 0;
31  service_handle_ = 0;
32  characteristic_handle_ = 0;
33  descriptor_handle_ = 0;
34  status_ = 0;
35
36  BluetoothTest::SetUp();
37  ASSERT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
38  semaphore_wait(adapter_state_changed_callback_sem_);
39  EXPECT_TRUE(GetState() == BT_STATE_ON);
40
41  register_client_callback_sem_ = semaphore_new(0);
42  scan_result_callback_sem_ = semaphore_new(0);
43
44  register_server_callback_sem_ = semaphore_new(0);
45  service_added_callback_sem_ = semaphore_new(0);
46  service_stopped_callback_sem_ = semaphore_new(0);
47  service_deleted_callback_sem_ = semaphore_new(0);
48
49  bluetooth::hal::BluetoothGattInterface::Initialize();
50  ASSERT_TRUE(bluetooth::hal::BluetoothGattInterface::IsInitialized());
51  auto gatt_interface = bluetooth::hal::BluetoothGattInterface::Get();
52  gatt_interface->AddClientObserver(this);
53  gatt_interface->AddServerObserver(this);
54
55  gatt_client_interface_ = gatt_interface->GetClientHALInterface();
56  gatt_server_interface_ = gatt_interface->GetServerHALInterface();
57
58  ASSERT_NE(nullptr, gatt_client_interface_);
59  ASSERT_NE(nullptr, gatt_server_interface_);
60}
61
62void GattTest::TearDown() {
63  gatt_client_interface_ = nullptr;
64  gatt_server_interface_ = nullptr;
65
66  semaphore_free(register_client_callback_sem_);
67  semaphore_free(scan_result_callback_sem_);
68
69  semaphore_free(register_server_callback_sem_);
70  semaphore_free(service_added_callback_sem_);
71  semaphore_free(service_stopped_callback_sem_);
72  semaphore_free(service_deleted_callback_sem_);
73
74  bluetooth::hal::BluetoothGattInterface::CleanUp();
75
76  ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
77  semaphore_wait(adapter_state_changed_callback_sem_);
78  BluetoothTest::TearDown();
79}
80
81const BleScannerInterface* GattTest::gatt_scanner_interface() {
82  return gatt_scanner_interface_;
83}
84
85const btgatt_client_interface_t* GattTest::gatt_client_interface() {
86  return gatt_client_interface_;
87}
88
89const btgatt_server_interface_t* GattTest::gatt_server_interface() {
90  return gatt_server_interface_;
91}
92
93void GattTest::RegisterClientCallback(
94    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
95    int clientIf, const bt_uuid_t& app_uuid) {
96  status_ = status;
97  client_interface_id_ = clientIf;
98  semaphore_post(register_client_callback_sem_);
99}
100
101void GattTest::ScanResultCallback(
102    bluetooth::hal::BluetoothGattInterface* /* unused */,
103    const bt_bdaddr_t& bda, int rssi, std::vector<uint8_t> adv_data) {
104  semaphore_post(scan_result_callback_sem_);
105}
106
107// GATT server callbacks
108void GattTest::RegisterServerCallback(
109    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
110    int server_if, const bt_uuid_t& uuid) {
111  status_ = status;
112  server_interface_id_ = server_if;
113  semaphore_post(register_server_callback_sem_);
114}
115
116void GattTest::ServiceAddedCallback(
117    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
118    int server_if, std::vector<btgatt_db_element_t> service) {
119  status_ = status;
120  server_interface_id_ = server_if;
121  service_handle_ = service[0].attribute_handle;
122  semaphore_post(service_added_callback_sem_);
123}
124
125void GattTest::ServiceStoppedCallback(
126    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
127    int server_if, int srvc_handle) {
128  status_ = status;
129  server_interface_id_ = server_if;
130  service_handle_ = srvc_handle;
131  semaphore_post(service_stopped_callback_sem_);
132}
133
134void GattTest::ServiceDeletedCallback(
135    bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
136    int server_if, int srvc_handle) {
137  status_ = status;
138  server_interface_id_ = server_if;
139  service_handle_ = srvc_handle;
140  semaphore_post(service_deleted_callback_sem_);
141}
142
143}  // bttest
144