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