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