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 "adapter/bluetooth_test.h" 20#include "gatt/gatt_test.h" 21 22extern "C" { 23#include "btcore/include/bdaddr.h" 24} 25 26namespace bttest { 27 28void GattTest::SetUp() { 29 gatt_client_interface_ = nullptr; 30 gatt_server_interface_ = nullptr; 31 32 client_interface_id_ = 0; 33 server_interface_id_ = 0; 34 service_handle_ = 0; 35 characteristic_handle_ = 0; 36 descriptor_handle_ = 0; 37 status_ = 0; 38 39 BluetoothTest::SetUp(); 40 ASSERT_EQ(bt_interface()->enable(false), BT_STATUS_SUCCESS); 41 semaphore_wait(adapter_state_changed_callback_sem_); 42 EXPECT_TRUE(GetState() == BT_STATE_ON); 43 44 register_client_callback_sem_ = semaphore_new(0); 45 scan_result_callback_sem_ = semaphore_new(0); 46 listen_callback_sem_ = semaphore_new(0); 47 48 register_server_callback_sem_ = semaphore_new(0); 49 service_added_callback_sem_ = semaphore_new(0); 50 characteristic_added_callback_sem_ = semaphore_new(0); 51 descriptor_added_callback_sem_ = semaphore_new(0); 52 service_started_callback_sem_ = semaphore_new(0); 53 service_stopped_callback_sem_ = semaphore_new(0); 54 service_deleted_callback_sem_ = semaphore_new(0); 55 56 bluetooth::hal::BluetoothGattInterface::Initialize(); 57 ASSERT_TRUE(bluetooth::hal::BluetoothGattInterface::IsInitialized()); 58 auto gatt_interface = bluetooth::hal::BluetoothGattInterface::Get(); 59 gatt_interface->AddClientObserver(this); 60 gatt_interface->AddServerObserver(this); 61 62 gatt_client_interface_ = gatt_interface->GetClientHALInterface(); 63 gatt_server_interface_ = gatt_interface->GetServerHALInterface(); 64 65 ASSERT_NE(nullptr, gatt_client_interface_); 66 ASSERT_NE(nullptr, gatt_server_interface_); 67} 68 69void GattTest::TearDown() { 70 gatt_client_interface_ = nullptr; 71 gatt_server_interface_ = nullptr; 72 73 semaphore_free(register_client_callback_sem_); 74 semaphore_free(scan_result_callback_sem_); 75 semaphore_free(listen_callback_sem_); 76 77 semaphore_free(register_server_callback_sem_); 78 semaphore_free(service_added_callback_sem_); 79 semaphore_free(characteristic_added_callback_sem_); 80 semaphore_free(descriptor_added_callback_sem_); 81 semaphore_free(service_started_callback_sem_); 82 semaphore_free(service_stopped_callback_sem_); 83 semaphore_free(service_deleted_callback_sem_); 84 85 bluetooth::hal::BluetoothGattInterface::CleanUp(); 86 87 ASSERT_EQ(bt_interface()->disable(), BT_STATUS_SUCCESS); 88 semaphore_wait(adapter_state_changed_callback_sem_); 89 BluetoothTest::TearDown(); 90} 91 92const btgatt_client_interface_t* GattTest::gatt_client_interface() { 93 return gatt_client_interface_; 94} 95 96const btgatt_server_interface_t* GattTest::gatt_server_interface() { 97 return gatt_server_interface_; 98} 99 100void GattTest::RegisterClientCallback( 101 bluetooth::hal::BluetoothGattInterface* /* unused */, 102 int status, int clientIf, const bt_uuid_t& app_uuid) { 103 status_ = status; 104 client_interface_id_ = clientIf; 105 semaphore_post(register_client_callback_sem_); 106} 107 108void GattTest::ScanResultCallback( 109 bluetooth::hal::BluetoothGattInterface* /* unused */, 110 const bt_bdaddr_t& bda, int rssi, uint8_t* adv_data) { 111 semaphore_post(scan_result_callback_sem_); 112} 113 114void GattTest::ListenCallback( 115 bluetooth::hal::BluetoothGattInterface* /* unused */, 116 int status, int client_if) { 117 status_ = status; 118 client_interface_id_ = client_if; 119 semaphore_post(listen_callback_sem_); 120} 121 122// GATT server callbacks 123void GattTest::RegisterServerCallback( 124 bluetooth::hal::BluetoothGattInterface* /* unused */, 125 int status, int server_if, const bt_uuid_t& uuid) { 126 status_ = status; 127 server_interface_id_ = server_if; 128 semaphore_post(register_server_callback_sem_); 129} 130 131void GattTest::ServiceAddedCallback( 132 bluetooth::hal::BluetoothGattInterface* /* unused */, 133 int status, int server_if, const btgatt_srvc_id_t& srvc_id, 134 int srvc_handle) { 135 status_ = status; 136 server_interface_id_ = server_if; 137 service_handle_ = srvc_handle; 138 semaphore_post(service_added_callback_sem_); 139} 140 141void GattTest::CharacteristicAddedCallback( 142 bluetooth::hal::BluetoothGattInterface* /* unused */, 143 int status, int server_if, const bt_uuid_t& char_id, 144 int srvc_handle, int char_handle) { 145 status_ = status; 146 server_interface_id_ = server_if; 147 service_handle_ = srvc_handle; 148 characteristic_handle_ = char_handle; 149 semaphore_post(characteristic_added_callback_sem_); 150} 151 152void GattTest::DescriptorAddedCallback( 153 bluetooth::hal::BluetoothGattInterface* /* unused */, 154 int status, int server_if, const bt_uuid_t& descr_id, 155 int srvc_handle, int descr_handle) { 156 status_ = status; 157 server_interface_id_ = server_if; 158 service_handle_ = srvc_handle; 159 descriptor_handle_ = descr_handle; 160 semaphore_post(descriptor_added_callback_sem_); 161} 162 163void GattTest::ServiceStartedCallback( 164 bluetooth::hal::BluetoothGattInterface* /* unused */, 165 int status, int server_if, int srvc_handle) { 166 status_ = status; 167 server_interface_id_ = server_if; 168 service_handle_ = srvc_handle; 169 semaphore_post(service_started_callback_sem_); 170} 171 172void GattTest::ServiceStoppedCallback( 173 bluetooth::hal::BluetoothGattInterface* /* unused */, 174 int status, int server_if, int srvc_handle) { 175 status_ = status; 176 server_interface_id_ = server_if; 177 service_handle_ = srvc_handle; 178 semaphore_post(service_stopped_callback_sem_); 179} 180 181void GattTest::ServiceDeletedCallback( 182 bluetooth::hal::BluetoothGattInterface* /* unused */, 183 int status, int server_if, int srvc_handle) { 184 status_ = status; 185 server_interface_id_ = server_if; 186 service_handle_ = srvc_handle; 187 semaphore_post(service_deleted_callback_sem_); 188} 189 190} // bttest 191