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