fake_bluetooth_gatt_interface.h revision c3f6a51615c4f26bce0b8b6e16757610dbd0b45b
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#pragma once 18 19#include <base/macros.h> 20#include <base/observer_list.h> 21 22#include "service/hal/bluetooth_gatt_interface.h" 23 24namespace bluetooth { 25namespace hal { 26 27class FakeBluetoothGattInterface : public BluetoothGattInterface { 28 public: 29 30 // Handles HAL LE scanner API calls for testing. Test code can 31 // provide a fake or mock implementation of this and all calls will be routed 32 // to it. 33 class TestScannerHandler { 34 public: 35 virtual ~TestScannerHandler() = default; 36 37 virtual bt_status_t RegisterScanner(bt_uuid_t* app_uuid) = 0; 38 virtual bt_status_t UnregisterScanner(int client_if) = 0; 39 40 virtual bt_status_t Scan(bool start) = 0; 41 }; 42 43 // Handles HAL Bluetooth GATT client API calls for testing. Test code can 44 // provide a fake or mock implementation of this and all calls will be routed 45 // to it. 46 class TestClientHandler { 47 public: 48 virtual ~TestClientHandler() = default; 49 50 virtual bt_status_t RegisterClient(bt_uuid_t* app_uuid) = 0; 51 virtual bt_status_t UnregisterClient(int client_if) = 0; 52 53 virtual bt_status_t Connect(int client_if, const bt_bdaddr_t *bd_addr, 54 bool is_direct, int transport) = 0; 55 virtual bt_status_t Disconnect(int client_if, const bt_bdaddr_t *bd_addr, 56 int conn_id) = 0; 57 }; 58 59 // Handles HAL Bluetooth GATT server API calls for testing. Test code can 60 // provide a fake or mock implementation of this and all calls will be routed 61 // to it. 62 class TestServerHandler { 63 public: 64 virtual ~TestServerHandler() = default; 65 66 virtual bt_status_t RegisterServer(bt_uuid_t* app_uuid) = 0; 67 virtual bt_status_t UnregisterServer(int server_if) = 0; 68 virtual bt_status_t AddService( 69 int server_if, vector<btgatt_db_element_t> service) = 0; 70 virtual bt_status_t DeleteService(int server_if, int srvc_handle) = 0; 71 virtual bt_status_t SendIndication(int server_if, int attribute_handle, 72 int conn_id, int confirm, 73 vector<uint8_t> value) = 0; 74 virtual bt_status_t SendResponse(int conn_id, int trans_id, int status, 75 btgatt_response_t* response) = 0; 76 }; 77 78 // Constructs the fake with the given handlers. Implementations can 79 // provide their own handlers or simply pass "nullptr" for the default 80 // behavior in which BT_STATUS_FAIL will be returned from all calls. 81 FakeBluetoothGattInterface(std::shared_ptr<BleAdvertiserInterface> advertiser_handler, 82 std::shared_ptr<TestScannerHandler> scanner_handler, 83 std::shared_ptr<TestClientHandler> client_handler, 84 std::shared_ptr<TestServerHandler> server_handler); 85 ~FakeBluetoothGattInterface(); 86 87 // The methods below can be used to notify observers with certain events and 88 // given parameters. 89 90 void NotifyRegisterScannerCallback(int status, int client_if, 91 const bt_uuid_t& app_uuid); 92 void NotifyScanResultCallback(const bt_bdaddr_t& bda, int rssi, 93 vector<uint8_t> adv_data); 94 95 // Client callbacks: 96 void NotifyRegisterClientCallback(int status, int client_if, 97 const bt_uuid_t& app_uuid); 98 void NotifyConnectCallback(int conn_id, int status, int client_if, 99 const bt_bdaddr_t& bda); 100 void NotifyDisconnectCallback(int conn_id, int status, int client_if, 101 const bt_bdaddr_t& bda); 102 103 // Server callbacks: 104 void NotifyRegisterServerCallback(int status, int server_if, 105 const bt_uuid_t& app_uuid); 106 void NotifyServerConnectionCallback(int conn_id, int server_if, 107 int connected, 108 const bt_bdaddr_t& bda); 109 void NotifyServiceAddedCallback(int status, int server_if, 110 vector<btgatt_db_element_t> srvc); 111 void NotifyCharacteristicAddedCallback(int status, int server_if, 112 const bt_uuid_t& uuid, 113 int srvc_handle, int char_handle); 114 void NotifyDescriptorAddedCallback(int status, int server_if, 115 const bt_uuid_t& uuid, 116 int srvc_handle, int desc_handle); 117 void NotifyServiceStartedCallback(int status, int server_if, int srvc_handle); 118 void NotifyRequestReadCharacteristicCallback(int conn_id, int trans_id, 119 const bt_bdaddr_t& bda, int attr_handle, 120 int offset, bool is_long); 121 void NotifyRequestReadDescriptorCallback(int conn_id, int trans_id, 122 const bt_bdaddr_t& bda, int attr_handle, 123 int offset, bool is_long); 124 void NotifyRequestWriteCharacteristicCallback(int conn_id, int trans_id, 125 const bt_bdaddr_t& bda, int attr_handle, 126 int offset, bool need_rsp, bool is_prep, 127 vector<uint8_t> value); 128 void NotifyRequestWriteDescriptorCallback(int conn_id, int trans_id, 129 const bt_bdaddr_t& bda, int attr_handle, 130 int offset, bool need_rsp, bool is_prep, 131 vector<uint8_t> value); 132 void NotifyRequestExecWriteCallback(int conn_id, int trans_id, 133 const bt_bdaddr_t& bda, int exec_write); 134 void NotifyIndicationSentCallback(int conn_id, int status); 135 136 // BluetoothGattInterface overrides: 137 void AddScannerObserver(ScannerObserver* observer) override; 138 void RemoveScannerObserver(ScannerObserver* observer) override; 139 void AddClientObserver(ClientObserver* observer) override; 140 void RemoveClientObserver(ClientObserver* observer) override; 141 void AddServerObserver(ServerObserver* observer) override; 142 void RemoveServerObserver(ServerObserver* observer) override; 143 BleAdvertiserInterface* GetAdvertiserHALInterface() const override; 144 const btgatt_scanner_interface_t* GetScannerHALInterface() const override; 145 const btgatt_client_interface_t* GetClientHALInterface() const override; 146 const btgatt_server_interface_t* GetServerHALInterface() const override; 147 148 private: 149 base::ObserverList<ScannerObserver> scanner_observers_; 150 base::ObserverList<ClientObserver> client_observers_; 151 base::ObserverList<ServerObserver> server_observers_; 152 std::shared_ptr<TestScannerHandler> scanner_handler_; 153 std::shared_ptr<TestClientHandler> client_handler_; 154 std::shared_ptr<TestServerHandler> server_handler_; 155 156 157 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattInterface); 158}; 159 160} // namespace hal 161} // namespace bluetooth 162