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, 61 const bluetooth::Uuid& app_uuid); 62 friend void ScanResultCallback(uint16_t ble_evt_type, uint8_t addr_type, 63 RawAddress* bda, uint8_t ble_primary_phy, 64 uint8_t ble_secondary_phy, 65 uint8_t ble_advertising_sid, 66 int8_t ble_tx_power, int8_t rssi, 67 uint16_t ble_periodic_adv_int, 68 std::vector<uint8_t> adv_data, 69 RawAddress* original_bda); 70 71 friend void RegisterServerCallback(int status, int server_if, 72 const bluetooth::Uuid& uuid); 73 friend void ServiceAddedCallback(int status, int server_if, 74 const btgatt_db_element_t* service, 75 size_t service_count); 76 friend void ServiceStoppedCallback(int status, int server_if, 77 int srvc_handle); 78 friend void ServiceDeletedCallback(int status, int server_if, 79 int srvc_handle); 80 81 // Semaphores used to wait for specific callback execution. Each callback 82 // has its own semaphore associated with it 83 btsemaphore register_client_callback_sem_; 84 btsemaphore scan_result_callback_sem_; 85 btsemaphore listen_callback_sem_; 86 87 btsemaphore register_server_callback_sem_; 88 btsemaphore service_added_callback_sem_; 89 btsemaphore characteristic_added_callback_sem_; 90 btsemaphore descriptor_added_callback_sem_; 91 btsemaphore service_started_callback_sem_; 92 btsemaphore service_stopped_callback_sem_; 93 btsemaphore service_deleted_callback_sem_; 94 95 private: 96 const btgatt_interface_t* gatt_interface_; 97 98 // No mutex needed for these as the semaphores should ensure 99 // synchronous access 100 101 // An ID that is used as a handle for each gatt client. 102 int client_interface_id_; 103 104 // An ID that is used as a handle for each gatt server. 105 int server_interface_id_; 106 107 // A handle to the last used service. 108 int service_handle_; 109 110 // A handle to the last characteristic added. 111 int characteristic_handle_; 112 113 // A handle to the last descriptor added. 114 int descriptor_handle_; 115 116 // The status of the last callback. Is BT_STATUS_SUCCESS if no issues. 117 int status_; 118 }; 119 120 } // bttest 121