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(tBTA_GATTC_CBACK* p_client_cb, BtaAppRegisterCallback cb,
31                            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 tGATT_STATUS RegisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
46                                                 uint16_t handle) = 0;
47   virtual tGATT_STATUS DeregisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
48                                                   uint16_t handle) = 0;
49   virtual ~BtaGattInterface() = default;
50 };
51 
52 class MockBtaGattInterface : public BtaGattInterface {
53 public:
54   MOCK_METHOD((void), AppRegister,
55               (tBTA_GATTC_CBACK * p_client_cb, BtaAppRegisterCallback cb, bool eatt_support),
56               (override));
57   MOCK_METHOD((void), AppDeregister, (tGATT_IF client_if), (override));
58   MOCK_METHOD((void), Open,
59               (tGATT_IF client_if, const RawAddress& remote_bda, tBTM_BLE_CONN_TYPE connection_type,
60                tBT_TRANSPORT transport, bool opportunistic, uint8_t initiating_phys),
61               (override));
62   MOCK_METHOD((void), Open,
63               (tGATT_IF client_if, const RawAddress& remote_bda, tBTM_BLE_CONN_TYPE connection_type,
64                bool opportunistic));
65   MOCK_METHOD((void), CancelOpen,
66               (tGATT_IF client_if, const RawAddress& remote_bda, bool is_direct));
67   MOCK_METHOD((void), Close, (uint16_t conn_id));
68   MOCK_METHOD((void), ServiceSearchRequest, (uint16_t conn_id, const bluetooth::Uuid* p_srvc_uuid));
69   MOCK_METHOD((void), SendIndConfirm, (uint16_t conn_id, uint16_t cid), (override));
70   MOCK_METHOD((std::list<Service>*), GetServices, (uint16_t conn_id));
71   MOCK_METHOD((const Characteristic*), GetCharacteristic, (uint16_t conn_id, uint16_t handle));
72   MOCK_METHOD((const Service*), GetOwningService, (uint16_t conn_id, uint16_t handle));
73   MOCK_METHOD((tGATT_STATUS), RegisterForNotifications,
74               (tGATT_IF client_if, const RawAddress& remote_bda, uint16_t handle));
75   MOCK_METHOD((tGATT_STATUS), DeregisterForNotifications,
76               (tGATT_IF client_if, const RawAddress& remote_bda, uint16_t handle));
77 };
78 
79 /**
80  * Set the {@link MockBtaGattInterface} for testing
81  *
82  * @param mock_bta_gatt_interface pointer to mock bta gatt interface,
83  * could be null
84  */
85 void SetMockBtaGattInterface(MockBtaGattInterface* mock_bta_gatt_interface);
86 
87 class BtaGattServerInterface {
88 public:
89   virtual void Disable() = 0;
90   virtual void AppRegister(const bluetooth::Uuid& /* app_uuid */, tBTA_GATTS_CBACK* /* p_cback */,
91                            bool /* eatt_support */) = 0;
92   virtual void AppDeregister(tGATT_IF server_if) = 0;
93   virtual void Open(tGATT_IF /* server_if */, const RawAddress& /* remote_bda */,
94                     tBLE_ADDR_TYPE /* addr_type */, bool /* is_direct */,
95                     tBT_TRANSPORT /* transport */) = 0;
96   virtual void CancelOpen(tGATT_IF /* server_if */, const RawAddress& /* remote_bda */,
97                           bool /* is_direct */) = 0;
98   virtual void Close(uint16_t /* conn_id */) = 0;
99   virtual void AddService(tGATT_IF /* server_if */, std::vector<btgatt_db_element_t> /* service */,
100                           BTA_GATTS_AddServiceCb /* cb */) = 0;
101   virtual void DeleteService(uint16_t /* service_id */) = 0;
102   virtual void HandleValueIndication(uint16_t /* conn_id */, uint16_t /* attr_id */,
103                                      std::vector<uint8_t> /* value */, bool /* need_confirm */) = 0;
104   virtual void SendRsp(uint16_t /* conn_id */, uint32_t /* trans_id */, tGATT_STATUS /* status */,
105                        tGATTS_RSP* /* p_msg */) = 0;
106   virtual void StopService(uint16_t /* service_id */) = 0;
107   virtual void InitBonded() = 0;
108   virtual ~BtaGattServerInterface() = default;
109 };
110 
111 class MockBtaGattServerInterface : public BtaGattServerInterface {
112 public:
113   MOCK_METHOD((void), Disable, ());
114   MOCK_METHOD((void), AppRegister,
115               (const bluetooth::Uuid& uuid, tBTA_GATTS_CBACK* cb, bool eatt_support), (override));
116   MOCK_METHOD((void), AppDeregister, (tGATT_IF server_if), (override));
117   MOCK_METHOD((void), Open,
118               (tGATT_IF server_if, const RawAddress& remote_bda, tBLE_ADDR_TYPE type,
119                bool is_direct, tBT_TRANSPORT transport),
120               (override));
121   MOCK_METHOD((void), CancelOpen,
122               (tGATT_IF server_if, const RawAddress& remote_bda, bool is_direct));
123   MOCK_METHOD((void), Close, (uint16_t conn_id));
124   MOCK_METHOD((void), AddService,
125               (tGATT_IF server_if, std::vector<btgatt_db_element_t> service,
126                BTA_GATTS_AddServiceCb cb),
127               (override));
128   MOCK_METHOD((void), DeleteService, (uint16_t service_id));
129   MOCK_METHOD((void), HandleValueIndication,
130               (uint16_t conn_id, uint16_t attr_id, std::vector<uint8_t> value, bool need_confirm));
131 
132   MOCK_METHOD((void), SendRsp,
133               (uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status, tGATTS_RSP* p_msg));
134   MOCK_METHOD((void), StopService, (uint16_t service_id));
135   MOCK_METHOD((void), InitBonded, ());
136 };
137 
138 /**
139  * Set the {@link MockBtaGattServerInterface} for testing
140  *
141  * @param mock_bta_gatt_server_interface pointer to mock bta gatt server interface,
142  * could be null
143  */
144 void SetMockBtaGattServerInterface(MockBtaGattServerInterface* mock_bta_gatt_server_interface);
145 
146 }  // namespace gatt
147