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 #include "gatt/gatt_test.h"
20
21 #include "adapter/bluetooth_test.h"
22 #include "types/bluetooth/uuid.h"
23 #include "types/raw_address.h"
24
25 namespace bttest {
26
27 static GattTest* instance = nullptr;
28
RegisterClientCallback(int status,int clientIf,const bluetooth::Uuid &)29 void RegisterClientCallback(int status, int clientIf, const bluetooth::Uuid& /*app_uuid*/) {
30 instance->status_ = status;
31 instance->client_interface_id_ = clientIf;
32 semaphore_post(instance->register_client_callback_sem_);
33 }
34
ScanResultCallback(uint16_t,uint8_t,RawAddress *,uint8_t,uint8_t,uint8_t,int8_t,int8_t,uint16_t,std::vector<uint8_t>,RawAddress *)35 void ScanResultCallback(uint16_t /*ble_evt_type*/, uint8_t /*addr_type*/, RawAddress* /*bda*/,
36 uint8_t /*ble_primary_phy*/, uint8_t /*ble_secondary_phy*/,
37 uint8_t /*ble_advertising_sid*/, int8_t /*ble_tx_power*/, int8_t /*rssi*/,
38 uint16_t /*ble_periodic_adv_int*/, std::vector<uint8_t> /*adv_data*/,
39 RawAddress* /*original_bda*/) {
40 semaphore_post(instance->scan_result_callback_sem_);
41 }
42
43 // GATT server callbacks
RegisterServerCallback(int status,int server_if,const bluetooth::Uuid &)44 void RegisterServerCallback(int status, int server_if, const bluetooth::Uuid& /*uuid*/) {
45 instance->status_ = status;
46 instance->server_interface_id_ = server_if;
47 semaphore_post(instance->register_server_callback_sem_);
48 }
49
ServiceAddedCallback(int status,int server_if,const btgatt_db_element_t * service,size_t)50 void ServiceAddedCallback(int status, int server_if, const btgatt_db_element_t* service,
51 size_t /*service_count*/) {
52 instance->status_ = status;
53 instance->server_interface_id_ = server_if;
54 instance->service_handle_ = service[0].attribute_handle;
55 semaphore_post(instance->service_added_callback_sem_);
56 }
57
ServiceStoppedCallback(int status,int server_if,int srvc_handle)58 void ServiceStoppedCallback(int status, int server_if, int srvc_handle) {
59 instance->status_ = status;
60 instance->server_interface_id_ = server_if;
61 instance->service_handle_ = srvc_handle;
62 semaphore_post(instance->service_stopped_callback_sem_);
63 }
64
ServiceDeletedCallback(int status,int server_if,int srvc_handle)65 void ServiceDeletedCallback(int status, int server_if, int srvc_handle) {
66 instance->status_ = status;
67 instance->server_interface_id_ = server_if;
68 instance->service_handle_ = srvc_handle;
69 semaphore_post(instance->service_deleted_callback_sem_);
70 }
71
72 static const btgatt_scanner_callbacks_t scanner_callbacks = {
73 .scan_result_cb = ScanResultCallback,
74 };
75
76 static const btgatt_client_callbacks_t client_callbacks = {
77 .register_client_cb = RegisterClientCallback,
78 };
79
80 static const btgatt_server_callbacks_t server_callbacks = {
81 .register_server_cb = RegisterServerCallback,
82 .service_added_cb = ServiceAddedCallback,
83 .service_stopped_cb = ServiceStoppedCallback,
84 .service_deleted_cb = ServiceDeletedCallback,
85 };
86
87 static const btgatt_callbacks_t callbacks = {
88 sizeof(btgatt_callbacks_t),
89 &client_callbacks,
90 &server_callbacks,
91 &scanner_callbacks,
92 };
93
SetUp()94 void GattTest::SetUp() {
95 gatt_interface_ = nullptr;
96
97 client_interface_id_ = 0;
98 server_interface_id_ = 0;
99 service_handle_ = 0;
100 characteristic_handle_ = 0;
101 descriptor_handle_ = 0;
102 status_ = 0;
103
104 BluetoothTest::SetUp();
105 ASSERT_EQ(bt_interface()->enable(), BT_STATUS_SUCCESS);
106 semaphore_wait(adapter_state_changed_callback_sem_);
107 EXPECT_TRUE(GetState() == BT_STATE_ON);
108
109 gatt_interface_ = reinterpret_cast<const btgatt_interface_t*>(
110 bt_interface()->get_profile_interface(BT_PROFILE_GATT_ID));
111 ASSERT_NE(nullptr, gatt_interface_);
112 instance = this;
113 auto status = gatt_interface_->init(&callbacks);
114 ASSERT_EQ(status, BT_STATUS_SUCCESS);
115 }
116
TearDown()117 void GattTest::TearDown() {
118 instance = nullptr;
119 gatt_interface_ = nullptr;
120
121 ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS);
122 semaphore_wait(adapter_state_changed_callback_sem_);
123 BluetoothTest::TearDown();
124 }
125
gatt_scanner_interface()126 const BleScannerInterface* GattTest::gatt_scanner_interface() { return gatt_interface_->scanner; }
127
gatt_client_interface()128 const btgatt_client_interface_t* GattTest::gatt_client_interface() {
129 return gatt_interface_->client;
130 }
131
gatt_server_interface()132 const btgatt_server_interface_t* GattTest::gatt_server_interface() {
133 return gatt_interface_->server;
134 }
135
136 } // namespace bttest
137