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