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