blob: de1d68b073dee2727712c33b1e8aaa9f01cffde6 [file] [log] [blame]
Ajay Panickerf261cf82015-12-07 14:15:33 -08001/******************************************************************************
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
Ajay Panickerf261cf82015-12-07 14:15:33 -080019#include "gatt/gatt_test.h"
Jakub Pawlowski74ef54b2016-10-06 16:52:30 -070020#include "adapter/bluetooth_test.h"
Ajay Panickerf261cf82015-12-07 14:15:33 -080021#include "btcore/include/bdaddr.h"
Ajay Panickerf261cf82015-12-07 14:15:33 -080022
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();
Ajay Panicker7b266be2016-03-17 17:09:24 -070037 ASSERT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS);
Ajay Panickerf261cf82015-12-07 14:15:33 -080038 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);
Ajay Panickerf261cf82015-12-07 14:15:33 -080043
44 register_server_callback_sem_ = semaphore_new(0);
45 service_added_callback_sem_ = semaphore_new(0);
Ajay Panickerf261cf82015-12-07 14:15:33 -080046 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_);
Ajay Panickerf261cf82015-12-07 14:15:33 -080068
69 semaphore_free(register_server_callback_sem_);
70 semaphore_free(service_added_callback_sem_);
Ajay Panickerf261cf82015-12-07 14:15:33 -080071 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
Jakub Pawlowski83f1d962016-12-14 09:49:38 -080081const BleScannerInterface* GattTest::gatt_scanner_interface() {
Jakub Pawlowskic3f6a512016-10-27 11:49:40 -070082 return gatt_scanner_interface_;
83}
84
Ajay Panickerf261cf82015-12-07 14:15:33 -080085const 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(
Myles Watson911d1ae2016-11-28 16:44:40 -080094 bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
95 int clientIf, const bt_uuid_t& app_uuid) {
Ajay Panickerf261cf82015-12-07 14:15:33 -080096 status_ = status;
97 client_interface_id_ = clientIf;
98 semaphore_post(register_client_callback_sem_);
99}
100
101void GattTest::ScanResultCallback(
Jakub Pawlowskia484a882017-06-24 17:30:18 -0700102 bluetooth::hal::BluetoothGattInterface* /* unused */, const RawAddress& bda,
103 int rssi, std::vector<uint8_t> adv_data) {
Ajay Panickerf261cf82015-12-07 14:15:33 -0800104 semaphore_post(scan_result_callback_sem_);
105}
106
Ajay Panickerf261cf82015-12-07 14:15:33 -0800107// GATT server callbacks
108void GattTest::RegisterServerCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -0800109 bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
110 int server_if, const bt_uuid_t& uuid) {
Ajay Panickerf261cf82015-12-07 14:15:33 -0800111 status_ = status;
112 server_interface_id_ = server_if;
113 semaphore_post(register_server_callback_sem_);
114}
115
116void GattTest::ServiceAddedCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -0800117 bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
Pavlin Radoslavovb324a8d2016-12-09 17:50:59 -0800118 int server_if, std::vector<btgatt_db_element_t> service) {
Ajay Panickerf261cf82015-12-07 14:15:33 -0800119 status_ = status;
120 server_interface_id_ = server_if;
Jakub Pawlowskia641b6f2016-03-26 00:47:23 -0700121 service_handle_ = service[0].attribute_handle;
Ajay Panickerf261cf82015-12-07 14:15:33 -0800122 semaphore_post(service_added_callback_sem_);
123}
124
Ajay Panickerf261cf82015-12-07 14:15:33 -0800125void GattTest::ServiceStoppedCallback(
Myles Watson911d1ae2016-11-28 16:44:40 -0800126 bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
127 int server_if, int srvc_handle) {
Ajay Panickerf261cf82015-12-07 14:15:33 -0800128 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(
Myles Watson911d1ae2016-11-28 16:44:40 -0800135 bluetooth::hal::BluetoothGattInterface* /* unused */, int status,
136 int server_if, int srvc_handle) {
Ajay Panickerf261cf82015-12-07 14:15:33 -0800137 status_ = status;
138 server_interface_id_ = server_if;
139 service_handle_ = srvc_handle;
140 semaphore_post(service_deleted_callback_sem_);
141}
142
143} // bttest