1 // 2 // Copyright (C) 2015 Google, Inc. 3 // 4 // Licensed under the Apache License, Version 2.0 (the "License"); 5 // you may not use this file except in compliance with the License. 6 // You may obtain a copy of the License at: 7 // 8 // http://www.apache.org/licenses/LICENSE-2.0 9 // 10 // Unless required by applicable law or agreed to in writing, software 11 // distributed under the License is distributed on an "AS IS" BASIS, 12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 // See the License for the specific language governing permissions and 14 // limitations under the License. 15 // 16 17 #include "service/hal/fake_bluetooth_gatt_interface.h" 18 19 namespace bluetooth { 20 namespace hal { 21 namespace { 22 23 // The global test handler instances. We have to have globals since the HAL 24 // interface methods all have to be global and their signatures don't allow us 25 // to pass in user_data. 26 std::shared_ptr<BleAdvertiserInterface> g_advertiser_handler; 27 std::shared_ptr<BleScannerInterface> g_scanner_handler; 28 std::shared_ptr<FakeBluetoothGattInterface::TestClientHandler> g_client_handler; 29 std::shared_ptr<FakeBluetoothGattInterface::TestServerHandler> g_server_handler; 30 31 bt_status_t FakeRegisterClient(bt_uuid_t* app_uuid) { 32 if (g_client_handler) return g_client_handler->RegisterClient(app_uuid); 33 34 return BT_STATUS_FAIL; 35 } 36 37 bt_status_t FakeUnregisterClient(int client_if) { 38 if (g_client_handler) return g_client_handler->UnregisterClient(client_if); 39 40 return BT_STATUS_FAIL; 41 } 42 43 bt_status_t FakeConnect(int client_if, const bt_bdaddr_t* bd_addr, 44 bool is_direct, int transport, int phy) { 45 if (g_client_handler) 46 return g_client_handler->Connect(client_if, bd_addr, is_direct, transport); 47 48 return BT_STATUS_FAIL; 49 } 50 51 bt_status_t FakeDisconnect(int client_if, const bt_bdaddr_t* bd_addr, 52 int conn_id) { 53 if (g_client_handler) 54 return g_client_handler->Disconnect(client_if, bd_addr, conn_id); 55 56 return BT_STATUS_FAIL; 57 } 58 59 bt_status_t FakeRegisterServer(bt_uuid_t* app_uuid) { 60 if (g_server_handler) return g_server_handler->RegisterServer(app_uuid); 61 62 return BT_STATUS_FAIL; 63 } 64 65 bt_status_t FakeUnregisterServer(int server_if) { 66 if (g_server_handler) return g_server_handler->UnregisterServer(server_if); 67 68 return BT_STATUS_FAIL; 69 } 70 71 bt_status_t FakeAddService(int server_if, 72 std::vector<btgatt_db_element_t> service) { 73 if (g_server_handler) 74 return g_server_handler->AddService(server_if, std::move(service)); 75 76 return BT_STATUS_FAIL; 77 } 78 79 bt_status_t FakeDeleteService(int server_if, int srvc_handle) { 80 if (g_server_handler) 81 return g_server_handler->DeleteService(server_if, srvc_handle); 82 83 return BT_STATUS_FAIL; 84 } 85 86 bt_status_t FakeSendIndication(int server_if, int attribute_handle, int conn_id, 87 int confirm, std::vector<uint8_t> value) { 88 if (g_server_handler) 89 return g_server_handler->SendIndication(server_if, attribute_handle, 90 conn_id, confirm, std::move(value)); 91 92 return BT_STATUS_FAIL; 93 } 94 95 bt_status_t FakeSendResponse(int conn_id, int trans_id, int status, 96 btgatt_response_t* response) { 97 if (g_server_handler) 98 return g_server_handler->SendResponse(conn_id, trans_id, status, response); 99 100 return BT_STATUS_FAIL; 101 } 102 103 btgatt_client_interface_t fake_btgattc_iface = { 104 FakeRegisterClient, 105 FakeUnregisterClient, 106 FakeConnect, 107 FakeDisconnect, 108 nullptr, // refresh 109 nullptr, // search_service 110 nullptr, // discover_service_by_uuid 111 nullptr, // read_characteristic 112 nullptr, // read_using_characteristic_uuid 113 nullptr, // write_characteristic 114 nullptr, // read_descriptor 115 nullptr, // write_descriptor 116 nullptr, // execute_write 117 nullptr, // register_for_notification 118 nullptr, // deregister_for_notification 119 nullptr, // read_remote_rssi 120 nullptr, // get_device_type 121 nullptr, // configure_mtu 122 nullptr, // conn_parameter_update 123 nullptr, // set_phy 124 nullptr, // read_phy 125 nullptr, // test_command 126 nullptr, // get_gatt_db 127 }; 128 129 btgatt_server_interface_t fake_btgatts_iface = { 130 FakeRegisterServer, 131 FakeUnregisterServer, 132 nullptr, // connect 133 nullptr, // disconnect 134 FakeAddService, 135 nullptr, // stop_service 136 FakeDeleteService, 137 FakeSendIndication, 138 FakeSendResponse, 139 nullptr, // set_phy 140 nullptr, // read_phy 141 }; 142 143 } // namespace 144 145 FakeBluetoothGattInterface::FakeBluetoothGattInterface( 146 std::shared_ptr<BleAdvertiserInterface> advertiser_handler, 147 std::shared_ptr<BleScannerInterface> scanner_handler, 148 std::shared_ptr<TestClientHandler> client_handler, 149 std::shared_ptr<TestServerHandler> server_handler) 150 : client_handler_(client_handler) { 151 CHECK(!g_advertiser_handler); 152 CHECK(!g_scanner_handler); 153 CHECK(!g_client_handler); 154 CHECK(!g_server_handler); 155 156 // We allow passing NULL. In this case all calls we fail by default. 157 if (advertiser_handler) g_advertiser_handler = advertiser_handler; 158 159 if (scanner_handler) g_scanner_handler = scanner_handler; 160 161 if (client_handler) g_client_handler = client_handler; 162 163 if (server_handler) g_server_handler = server_handler; 164 } 165 166 FakeBluetoothGattInterface::~FakeBluetoothGattInterface() { 167 if (g_advertiser_handler) g_advertiser_handler = nullptr; 168 169 if (g_scanner_handler) g_scanner_handler = nullptr; 170 171 if (g_client_handler) g_client_handler = nullptr; 172 173 if (g_server_handler) g_server_handler = nullptr; 174 } 175 176 // The methods below can be used to notify observers with certain events and 177 // given parameters. 178 void FakeBluetoothGattInterface::NotifyScanResultCallback( 179 const bt_bdaddr_t& bda, int rssi, std::vector<uint8_t> adv_data) { 180 FOR_EACH_OBSERVER(ScannerObserver, scanner_observers_, 181 ScanResultCallback(this, bda, rssi, adv_data)); 182 } 183 184 void FakeBluetoothGattInterface::NotifyRegisterClientCallback( 185 int status, int client_if, const bt_uuid_t& app_uuid) { 186 FOR_EACH_OBSERVER(ClientObserver, client_observers_, 187 RegisterClientCallback(this, status, client_if, app_uuid)); 188 } 189 190 void FakeBluetoothGattInterface::NotifyConnectCallback(int conn_id, int status, 191 int client_if, 192 const bt_bdaddr_t& bda) { 193 FOR_EACH_OBSERVER(ClientObserver, client_observers_, 194 ConnectCallback(this, conn_id, status, client_if, bda)); 195 } 196 197 void FakeBluetoothGattInterface::NotifyDisconnectCallback( 198 int conn_id, int status, int client_if, const bt_bdaddr_t& bda) { 199 FOR_EACH_OBSERVER(ClientObserver, client_observers_, 200 DisconnectCallback(this, conn_id, status, client_if, bda)); 201 } 202 203 void FakeBluetoothGattInterface::NotifyRegisterServerCallback( 204 int status, int server_if, const bt_uuid_t& app_uuid) { 205 FOR_EACH_OBSERVER(ServerObserver, server_observers_, 206 RegisterServerCallback(this, status, server_if, app_uuid)); 207 } 208 209 void FakeBluetoothGattInterface::NotifyServerConnectionCallback( 210 int conn_id, int server_if, int connected, const bt_bdaddr_t& bda) { 211 FOR_EACH_OBSERVER( 212 ServerObserver, server_observers_, 213 ConnectionCallback(this, conn_id, server_if, connected, bda)); 214 } 215 216 void FakeBluetoothGattInterface::NotifyServiceAddedCallback( 217 int status, int server_if, std::vector<btgatt_db_element_t> service) { 218 FOR_EACH_OBSERVER(ServerObserver, server_observers_, 219 ServiceAddedCallback(this, status, server_if, service)); 220 } 221 222 void FakeBluetoothGattInterface::NotifyRequestReadCharacteristicCallback( 223 int conn_id, int trans_id, const bt_bdaddr_t& bda, int attr_handle, 224 int offset, bool is_long) { 225 FOR_EACH_OBSERVER( 226 ServerObserver, server_observers_, 227 RequestReadCharacteristicCallback(this, conn_id, trans_id, bda, 228 attr_handle, offset, is_long)); 229 } 230 231 void FakeBluetoothGattInterface::NotifyRequestReadDescriptorCallback( 232 int conn_id, int trans_id, const bt_bdaddr_t& bda, int attr_handle, 233 int offset, bool is_long) { 234 FOR_EACH_OBSERVER( 235 ServerObserver, server_observers_, 236 RequestReadDescriptorCallback(this, conn_id, trans_id, bda, attr_handle, 237 offset, is_long)); 238 } 239 240 void FakeBluetoothGattInterface::NotifyRequestWriteCharacteristicCallback( 241 int conn_id, int trans_id, const bt_bdaddr_t& bda, int attr_handle, 242 int offset, bool need_rsp, bool is_prep, std::vector<uint8_t> value) { 243 FOR_EACH_OBSERVER(ServerObserver, server_observers_, 244 RequestWriteCharacteristicCallback( 245 this, conn_id, trans_id, bda, attr_handle, offset, 246 need_rsp, is_prep, value)); 247 } 248 249 void FakeBluetoothGattInterface::NotifyRequestWriteDescriptorCallback( 250 int conn_id, int trans_id, const bt_bdaddr_t& bda, int attr_handle, 251 int offset, bool need_rsp, bool is_prep, std::vector<uint8_t> value) { 252 FOR_EACH_OBSERVER( 253 ServerObserver, server_observers_, 254 RequestWriteDescriptorCallback(this, conn_id, trans_id, bda, attr_handle, 255 offset, need_rsp, is_prep, value)); 256 } 257 258 void FakeBluetoothGattInterface::NotifyRequestExecWriteCallback( 259 int conn_id, int trans_id, const bt_bdaddr_t& bda, int exec_write) { 260 FOR_EACH_OBSERVER( 261 ServerObserver, server_observers_, 262 RequestExecWriteCallback(this, conn_id, trans_id, bda, exec_write)); 263 } 264 265 void FakeBluetoothGattInterface::NotifyIndicationSentCallback(int conn_id, 266 int status) { 267 FOR_EACH_OBSERVER(ServerObserver, server_observers_, 268 IndicationSentCallback(this, conn_id, status)); 269 } 270 271 void FakeBluetoothGattInterface::AddScannerObserver(ScannerObserver* observer) { 272 CHECK(observer); 273 scanner_observers_.AddObserver(observer); 274 } 275 276 void FakeBluetoothGattInterface::RemoveScannerObserver( 277 ScannerObserver* observer) { 278 CHECK(observer); 279 scanner_observers_.RemoveObserver(observer); 280 } 281 282 void FakeBluetoothGattInterface::AddClientObserver(ClientObserver* observer) { 283 CHECK(observer); 284 client_observers_.AddObserver(observer); 285 } 286 287 void FakeBluetoothGattInterface::RemoveClientObserver( 288 ClientObserver* observer) { 289 CHECK(observer); 290 client_observers_.RemoveObserver(observer); 291 } 292 293 void FakeBluetoothGattInterface::AddServerObserver(ServerObserver* observer) { 294 CHECK(observer); 295 server_observers_.AddObserver(observer); 296 } 297 298 void FakeBluetoothGattInterface::RemoveServerObserver( 299 ServerObserver* observer) { 300 CHECK(observer); 301 server_observers_.RemoveObserver(observer); 302 } 303 304 BleAdvertiserInterface* FakeBluetoothGattInterface::GetAdvertiserHALInterface() 305 const { 306 return g_advertiser_handler.get(); 307 } 308 309 BleScannerInterface* FakeBluetoothGattInterface::GetScannerHALInterface() 310 const { 311 return g_scanner_handler.get(); 312 } 313 314 const btgatt_client_interface_t* 315 FakeBluetoothGattInterface::GetClientHALInterface() const { 316 return &fake_btgattc_iface; 317 } 318 319 const btgatt_server_interface_t* 320 FakeBluetoothGattInterface::GetServerHALInterface() const { 321 return &fake_btgatts_iface; 322 } 323 324 } // namespace hal 325 } // namespace bluetooth 326