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 "adapter/bluetooth_test.h" 20 #include "service/hal/bluetooth_gatt_interface.h" 21 22 namespace bttest { 23 24 // This class represents the Bluetooth GATT testing framework and provides 25 // helpers and callbacks for GUnit to use for testing gatt. 26 class GattTest : public BluetoothTest, 27 public bluetooth::hal::BluetoothGattInterface::ClientObserver, 28 public bluetooth::hal::BluetoothGattInterface::ScannerObserver, 29 public bluetooth::hal::BluetoothGattInterface::ServerObserver { 30 protected: 31 GattTest() = default; 32 virtual ~GattTest() = default; 33 34 // Gets the gatt_scanner_interface 35 const BleScannerInterface* gatt_scanner_interface(); 36 37 // Gets the gatt_client_interface 38 const btgatt_client_interface_t* gatt_client_interface(); 39 40 // Gets the gatt_server_interface 41 const btgatt_server_interface_t* gatt_server_interface(); 42 43 // Getters for variables that track GATT-related state client_interface_id()44 int client_interface_id() const { return client_interface_id_; } server_interface_id()45 int server_interface_id() const { return server_interface_id_; } service_handle()46 int service_handle() const { return service_handle_; } characteristic_handle()47 int characteristic_handle() const { return characteristic_handle_; } descriptor_handle()48 int descriptor_handle() const { return descriptor_handle_; } status()49 int status() const { return status_; } 50 51 // SetUp initializes the Bluetooth interfaces and the GATT Interface as well 52 // as registers the callbacks and initializes the semaphores before every test 53 virtual void SetUp(); 54 55 // TearDown cleans up the Bluetooth and GATT interfaces and destroys the 56 // callback semaphores at the end of every test 57 virtual void TearDown(); 58 59 // bluetooth::hal::BluetoothGattInterface::ClientObserver overrides 60 void RegisterClientCallback( 61 bluetooth::hal::BluetoothGattInterface* /* unused */, int status, 62 int clientIf, const bluetooth::Uuid& app_uuid) override; 63 void ScanResultCallback(bluetooth::hal::BluetoothGattInterface* /* unused */, 64 const RawAddress& bda, int rssi, 65 std::vector<uint8_t> adv_data) override; 66 67 // bluetooth::hal::BluetoothGattInterface::ServerObserver overrides 68 void RegisterServerCallback( 69 bluetooth::hal::BluetoothGattInterface* /* unused */, int status, 70 int server_if, const bluetooth::Uuid& uuid) override; 71 void ServiceAddedCallback( 72 bluetooth::hal::BluetoothGattInterface* /* unused */, int status, 73 int server_if, std::vector<btgatt_db_element_t> service) override; 74 void ServiceStoppedCallback( 75 bluetooth::hal::BluetoothGattInterface* /* unused */, int status, 76 int server_if, int srvc_handle) override; 77 void ServiceDeletedCallback( 78 bluetooth::hal::BluetoothGattInterface* /* unused */, int status, 79 int server_if, int srvc_handle) override; 80 81 // Semaphores used to wait for specific callback execution. Each callback 82 // has its own semaphore associated with it 83 semaphore_t* register_client_callback_sem_; 84 semaphore_t* scan_result_callback_sem_; 85 semaphore_t* listen_callback_sem_; 86 87 semaphore_t* register_server_callback_sem_; 88 semaphore_t* service_added_callback_sem_; 89 semaphore_t* characteristic_added_callback_sem_; 90 semaphore_t* descriptor_added_callback_sem_; 91 semaphore_t* service_started_callback_sem_; 92 semaphore_t* service_stopped_callback_sem_; 93 semaphore_t* service_deleted_callback_sem_; 94 95 private: 96 // The btgatt_scanner_interface_t that all the tests use to interact with the 97 // HAL 98 const BleScannerInterface* gatt_scanner_interface_; 99 100 // The gatt_client_interface that all the tests use to interact with the HAL 101 const btgatt_client_interface_t* gatt_client_interface_; 102 103 // The gatt_server_interface that all the tests use to interact with the HAL 104 const btgatt_server_interface_t* gatt_server_interface_; 105 106 // No mutex needed for these as the semaphores should ensure 107 // synchronous access 108 109 // An ID that is used as a handle for each gatt client. 110 int client_interface_id_; 111 112 // An ID that is used as a handle for each gatt server. 113 int server_interface_id_; 114 115 // A handle to the last used service. 116 int service_handle_; 117 118 // A handle to the last characteristic added. 119 int characteristic_handle_; 120 121 // A handle to the last descriptor added. 122 int descriptor_handle_; 123 124 // The status of the last callback. Is BT_STATUS_SUCCESS if no issues. 125 int status_; 126 127 DISALLOW_COPY_AND_ASSIGN(GattTest); 128 }; 129 130 } // bttest 131