1 /* 2 * Copyright 2021 HIMSA II K/S - www.himsa.com. 3 * Represented by EHIMA - www.ehima.com 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 #pragma once 18 19 #include <base/functional/callback.h> 20 #include <gmock/gmock.h> 21 22 #include "bta_gatt_api.h" 23 #include "types/bluetooth/uuid.h" 24 #include "types/raw_address.h" 25 26 namespace gatt { 27 28 class BtaGattInterface { 29 public: 30 virtual void AppRegister(const std::string& name, tBTA_GATTC_CBACK* p_client_cb, 31 BtaAppRegisterCallback cb, bool eatt_support) = 0; 32 virtual void AppDeregister(tGATT_IF client_if) = 0; 33 virtual void Open(tGATT_IF client_if, const RawAddress& remote_bda, 34 tBTM_BLE_CONN_TYPE connection_type, tBT_TRANSPORT transport, bool opportunistic, 35 uint8_t initiating_phys) = 0; 36 virtual void Open(tGATT_IF client_if, const RawAddress& remote_bda, 37 tBTM_BLE_CONN_TYPE connection_type, bool opportunistic) = 0; 38 virtual void CancelOpen(tGATT_IF client_if, const RawAddress& remote_bda, bool is_direct) = 0; 39 virtual void Close(uint16_t conn_id) = 0; 40 virtual void ServiceSearchRequest(uint16_t conn_id, const bluetooth::Uuid* p_srvc_uuid) = 0; 41 virtual void SendIndConfirm(uint16_t conn_id, uint16_t cid) = 0; 42 virtual const std::list<Service>* GetServices(uint16_t conn_id) = 0; 43 virtual const Characteristic* GetCharacteristic(uint16_t conn_id, uint16_t handle) = 0; 44 virtual const Service* GetOwningService(uint16_t conn_id, uint16_t handle) = 0; 45 virtual void ReadCharacteristic(tCONN_ID conn_id, uint16_t handle, tGATT_AUTH_REQ auth_req, 46 GATT_READ_OP_CB callback, void* cb_data) = 0; 47 virtual void WriteCharValue(tCONN_ID conn_id, uint16_t handle, tGATT_WRITE_TYPE write_type, 48 std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req, 49 GATT_WRITE_OP_CB callback, void* cb_data) = 0; 50 virtual void WriteCharDescr(tCONN_ID conn_id, uint16_t handle, std::vector<uint8_t> value, 51 tGATT_AUTH_REQ auth_req, GATT_WRITE_OP_CB callback, 52 void* cb_data) = 0; 53 virtual tGATT_STATUS RegisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda, 54 uint16_t handle) = 0; 55 virtual tGATT_STATUS DeregisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda, 56 uint16_t handle) = 0; 57 virtual void ConfigureMTU(tCONN_ID conn_id, uint16_t mtu) = 0; 58 virtual ~BtaGattInterface() = default; 59 }; 60 61 class MockBtaGattInterface : public BtaGattInterface { 62 public: 63 MOCK_METHOD((void), AppRegister, 64 (const std::string& name, tBTA_GATTC_CBACK* p_client_cb, BtaAppRegisterCallback cb, 65 bool eatt_support), 66 (override)); 67 MOCK_METHOD((void), AppDeregister, (tGATT_IF client_if), (override)); 68 MOCK_METHOD((void), Open, 69 (tGATT_IF client_if, const RawAddress& remote_bda, tBTM_BLE_CONN_TYPE connection_type, 70 tBT_TRANSPORT transport, bool opportunistic, uint8_t initiating_phys), 71 (override)); 72 MOCK_METHOD((void), Open, 73 (tGATT_IF client_if, const RawAddress& remote_bda, tBTM_BLE_CONN_TYPE connection_type, 74 bool opportunistic)); 75 MOCK_METHOD((void), CancelOpen, 76 (tGATT_IF client_if, const RawAddress& remote_bda, bool is_direct)); 77 MOCK_METHOD((void), Close, (uint16_t conn_id)); 78 MOCK_METHOD((void), ServiceSearchRequest, (uint16_t conn_id, const bluetooth::Uuid* p_srvc_uuid)); 79 MOCK_METHOD((void), SendIndConfirm, (uint16_t conn_id, uint16_t cid), (override)); 80 MOCK_METHOD((std::list<Service>*), GetServices, (uint16_t conn_id)); 81 MOCK_METHOD((const Characteristic*), GetCharacteristic, (uint16_t conn_id, uint16_t handle)); 82 MOCK_METHOD((const Service*), GetOwningService, (uint16_t conn_id, uint16_t handle)); 83 MOCK_METHOD((void), ReadCharacteristic, 84 (tCONN_ID conn_id, uint16_t handle, tGATT_AUTH_REQ auth_req, GATT_READ_OP_CB callback, 85 void* cb_data)); 86 MOCK_METHOD((void), WriteCharValue, 87 (tCONN_ID conn_id, uint16_t handle, tGATT_WRITE_TYPE write_type, 88 std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req, GATT_WRITE_OP_CB callback, 89 void* cb_data)); 90 MOCK_METHOD((void), WriteCharDescr, 91 (tCONN_ID conn_id, uint16_t handle, std::vector<uint8_t> value, 92 tGATT_AUTH_REQ auth_req, GATT_WRITE_OP_CB callback, void* cb_data)); 93 MOCK_METHOD((tGATT_STATUS), RegisterForNotifications, 94 (tGATT_IF client_if, const RawAddress& remote_bda, uint16_t handle)); 95 MOCK_METHOD((tGATT_STATUS), DeregisterForNotifications, 96 (tGATT_IF client_if, const RawAddress& remote_bda, uint16_t handle)); 97 MOCK_METHOD((void), ConfigureMTU, (tCONN_ID conn_id, uint16_t mtu)); 98 }; 99 100 /** 101 * Set the {@link MockBtaGattInterface} for testing 102 * 103 * @param mock_bta_gatt_interface pointer to mock bta gatt interface, 104 * could be null 105 */ 106 void SetMockBtaGattInterface(MockBtaGattInterface* mock_bta_gatt_interface); 107 108 class BtaGattServerInterface { 109 public: 110 virtual void Disable() = 0; 111 virtual void AppRegister(const bluetooth::Uuid& /* app_uuid */, tBTA_GATTS_CBACK* /* p_cback */, 112 bool /* eatt_support */) = 0; 113 virtual void AppDeregister(tGATT_IF server_if) = 0; 114 virtual void Open(tGATT_IF /* server_if */, const RawAddress& /* remote_bda */, 115 tBLE_ADDR_TYPE /* addr_type */, bool /* is_direct */, 116 tBT_TRANSPORT /* transport */) = 0; 117 virtual void CancelOpen(tGATT_IF /* server_if */, const RawAddress& /* remote_bda */, 118 bool /* is_direct */) = 0; 119 virtual void Close(uint16_t /* conn_id */) = 0; 120 virtual void AddService(tGATT_IF /* server_if */, std::vector<btgatt_db_element_t> /* service */, 121 BTA_GATTS_AddServiceCb /* cb */) = 0; 122 virtual void DeleteService(uint16_t /* service_id */) = 0; 123 virtual void HandleValueIndication(uint16_t /* conn_id */, uint16_t /* attr_id */, 124 std::vector<uint8_t> /* value */, bool /* need_confirm */) = 0; 125 virtual void SendRsp(uint16_t /* conn_id */, uint32_t /* trans_id */, tGATT_STATUS /* status */, 126 tGATTS_RSP* /* p_msg */) = 0; 127 virtual void StopService(uint16_t /* service_id */) = 0; 128 virtual void InitBonded() = 0; 129 virtual ~BtaGattServerInterface() = default; 130 }; 131 132 class MockBtaGattServerInterface : public BtaGattServerInterface { 133 public: 134 MOCK_METHOD((void), Disable, ()); 135 MOCK_METHOD((void), AppRegister, 136 (const bluetooth::Uuid& uuid, tBTA_GATTS_CBACK* cb, bool eatt_support), (override)); 137 MOCK_METHOD((void), AppDeregister, (tGATT_IF server_if), (override)); 138 MOCK_METHOD((void), Open, 139 (tGATT_IF server_if, const RawAddress& remote_bda, tBLE_ADDR_TYPE type, 140 bool is_direct, tBT_TRANSPORT transport), 141 (override)); 142 MOCK_METHOD((void), CancelOpen, 143 (tGATT_IF server_if, const RawAddress& remote_bda, bool is_direct)); 144 MOCK_METHOD((void), Close, (uint16_t conn_id)); 145 MOCK_METHOD((void), AddService, 146 (tGATT_IF server_if, std::vector<btgatt_db_element_t> service, 147 BTA_GATTS_AddServiceCb cb), 148 (override)); 149 MOCK_METHOD((void), DeleteService, (uint16_t service_id)); 150 MOCK_METHOD((void), HandleValueIndication, 151 (uint16_t conn_id, uint16_t attr_id, std::vector<uint8_t> value, bool need_confirm)); 152 153 MOCK_METHOD((void), SendRsp, 154 (uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status, tGATTS_RSP* p_msg)); 155 MOCK_METHOD((void), StopService, (uint16_t service_id)); 156 MOCK_METHOD((void), InitBonded, ()); 157 }; 158 159 /** 160 * Set the {@link MockBtaGattServerInterface} for testing 161 * 162 * @param mock_bta_gatt_server_interface pointer to mock bta gatt server interface, 163 * could be null 164 */ 165 void SetMockBtaGattServerInterface(MockBtaGattServerInterface* mock_bta_gatt_server_interface); 166 167 } // namespace gatt 168