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 24 namespace bluetooth { 25 namespace hal { 26 27 class FakeBluetoothGattInterface : public BluetoothGattInterface { 28 public: 29 // Handles HAL Bluetooth GATT client API calls for testing. Test code can 30 // provide a fake or mock implementation of this and all calls will be routed 31 // to it. 32 class TestClientHandler { 33 public: 34 virtual ~TestClientHandler() = default; 35 36 virtual bt_status_t RegisterClient(bt_uuid_t* app_uuid) = 0; 37 virtual bt_status_t UnregisterClient(int client_if) = 0; 38 39 virtual bt_status_t Scan(bool start) = 0; 40 virtual bt_status_t Connect(int client_if, const bt_bdaddr_t *bd_addr, 41 bool is_direct, int transport) = 0; 42 virtual bt_status_t Disconnect(int client_if, const bt_bdaddr_t *bd_addr, 43 int conn_id) = 0; 44 45 virtual bt_status_t MultiAdvEnable( 46 int client_if, int min_interval, int max_interval, int adv_type, 47 int chnl_map, int tx_power, int timeout_s) = 0; 48 virtual bt_status_t MultiAdvSetInstData( 49 int client_if, bool set_scan_rsp, bool include_name, 50 bool incl_txpower, int appearance, 51 int manufacturer_len, char* manufacturer_data, 52 int service_data_len, char* service_data, 53 int service_uuid_len, char* service_uuid) = 0; 54 virtual bt_status_t MultiAdvDisable(int client_if) = 0; 55 }; 56 57 // Handles HAL Bluetooth GATT server API calls for testing. Test code can 58 // provide a fake or mock implementation of this and all calls will be routed 59 // to it. 60 class TestServerHandler { 61 public: 62 virtual ~TestServerHandler() = default; 63 64 virtual bt_status_t RegisterServer(bt_uuid_t* app_uuid) = 0; 65 virtual bt_status_t UnregisterServer(int server_if) = 0; 66 virtual bt_status_t AddService( 67 int server_if, btgatt_srvc_id_t* srvc_id, int num_handles) = 0; 68 virtual bt_status_t AddCharacteristic(int server_if, int srvc_handle, 69 bt_uuid_t *uuid, 70 int properties, int permissions) = 0; 71 virtual bt_status_t AddDescriptor(int server_if, int srvc_handle, 72 bt_uuid_t* uuid, 73 int permissions) = 0; 74 virtual bt_status_t StartService( 75 int server_if, int srvc_handle, int transport) = 0; 76 virtual bt_status_t DeleteService(int server_if, int srvc_handle) = 0; 77 virtual bt_status_t SendIndication(int server_if, int attribute_handle, 78 int conn_id, int len, int confirm, 79 char* value) = 0; 80 virtual bt_status_t SendResponse(int conn_id, int trans_id, int status, 81 btgatt_response_t* response) = 0; 82 }; 83 84 // Constructs the fake with the given handlers. Implementations can 85 // provide their own handlers or simply pass "nullptr" for the default 86 // behavior in which BT_STATUS_FAIL will be returned from all calls. 87 FakeBluetoothGattInterface(std::shared_ptr<TestClientHandler> client_handler, 88 std::shared_ptr<TestServerHandler> server_handler); 89 ~FakeBluetoothGattInterface(); 90 91 // The methods below can be used to notify observers with certain events and 92 // given parameters. 93 94 // Client callbacks: 95 void NotifyRegisterClientCallback(int status, int client_if, 96 const bt_uuid_t& app_uuid); 97 void NotifyConnectCallback(int conn_id, int status, int client_if, 98 const bt_bdaddr_t& bda); 99 void NotifyDisconnectCallback(int conn_id, int status, int client_if, 100 const bt_bdaddr_t& bda); 101 void NotifyScanResultCallback(const bt_bdaddr_t& bda, int rssi, 102 uint8_t* adv_data); 103 void NotifyMultiAdvEnableCallback(int client_if, int status); 104 void NotifyMultiAdvDataCallback(int client_if, int status); 105 void NotifyMultiAdvDisableCallback(int client_if, int status); 106 107 // Server callbacks: 108 void NotifyRegisterServerCallback(int status, int server_if, 109 const bt_uuid_t& app_uuid); 110 void NotifyServerConnectionCallback(int conn_id, int server_if, 111 int connected, 112 const bt_bdaddr_t& bda); 113 void NotifyServiceAddedCallback(int status, int server_if, 114 const btgatt_srvc_id_t& srvc_id, 115 int srvc_handle); 116 void NotifyCharacteristicAddedCallback(int status, int server_if, 117 const bt_uuid_t& uuid, 118 int srvc_handle, int char_handle); 119 void NotifyDescriptorAddedCallback(int status, int server_if, 120 const bt_uuid_t& uuid, 121 int srvc_handle, int desc_handle); 122 void NotifyServiceStartedCallback(int status, int server_if, int srvc_handle); 123 void NotifyRequestReadCallback(int conn_id, int trans_id, 124 const bt_bdaddr_t& bda, int attr_handle, 125 int offset, bool is_long); 126 void NotifyRequestWriteCallback(int conn_id, int trans_id, 127 const bt_bdaddr_t& bda, int attr_handle, 128 int offset, int length, 129 bool need_rsp, bool is_prep, uint8_t* value); 130 void NotifyRequestExecWriteCallback(int conn_id, int trans_id, 131 const bt_bdaddr_t& bda, int exec_write); 132 void NotifyIndicationSentCallback(int conn_id, int status); 133 134 // BluetoothGattInterface overrides: 135 void AddClientObserver(ClientObserver* observer) override; 136 void RemoveClientObserver(ClientObserver* observer) override; 137 void AddServerObserver(ServerObserver* observer) override; 138 void RemoveServerObserver(ServerObserver* observer) override; 139 const btgatt_client_interface_t* GetClientHALInterface() const override; 140 const btgatt_server_interface_t* GetServerHALInterface() const override; 141 142 private: 143 base::ObserverList<ClientObserver> client_observers_; 144 base::ObserverList<ServerObserver> server_observers_; 145 std::shared_ptr<TestClientHandler> client_handler_; 146 std::shared_ptr<TestServerHandler> server_handler_; 147 148 149 DISALLOW_COPY_AND_ASSIGN(FakeBluetoothGattInterface); 150 }; 151 152 } // namespace hal 153 } // namespace bluetooth 154