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 
18 #include "bta_gatt_api_mock.h"
19 
20 #include <bluetooth/log.h>
21 
22 #include "types/bluetooth/uuid.h"
23 #include "types/raw_address.h"
24 
25 using namespace bluetooth;
26 
27 static gatt::MockBtaGattInterface* gatt_interface = nullptr;
28 static gatt::MockBtaGattServerInterface* gatt_server_interface = nullptr;
29 
SetMockBtaGattInterface(MockBtaGattInterface * mock_bta_gatt_interface)30 void gatt::SetMockBtaGattInterface(MockBtaGattInterface* mock_bta_gatt_interface) {
31   gatt_interface = mock_bta_gatt_interface;
32 }
33 
BTA_GATTC_AppRegister(tBTA_GATTC_CBACK * p_client_cb,BtaAppRegisterCallback cb,bool eatt_support)34 void BTA_GATTC_AppRegister(tBTA_GATTC_CBACK* p_client_cb, BtaAppRegisterCallback cb,
35                            bool eatt_support) {
36   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
37   gatt_interface->AppRegister(p_client_cb, cb, eatt_support);
38 }
39 
BTA_GATTC_AppDeregister(tGATT_IF client_if)40 void BTA_GATTC_AppDeregister(tGATT_IF client_if) {
41   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
42   gatt_interface->AppDeregister(client_if);
43 }
44 
BTA_GATTC_Open(tGATT_IF client_if,const RawAddress & remote_bda,tBTM_BLE_CONN_TYPE connection_type,bool opportunistic)45 void BTA_GATTC_Open(tGATT_IF client_if, const RawAddress& remote_bda,
46                     tBTM_BLE_CONN_TYPE connection_type, bool opportunistic) {
47   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
48   gatt_interface->Open(client_if, remote_bda, connection_type, opportunistic);
49 }
50 
BTA_GATTC_CancelOpen(tGATT_IF client_if,const RawAddress & remote_bda,bool is_direct)51 void BTA_GATTC_CancelOpen(tGATT_IF client_if, const RawAddress& remote_bda, bool is_direct) {
52   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
53   gatt_interface->CancelOpen(client_if, remote_bda, is_direct);
54 }
55 
BTA_GATTC_Close(uint16_t conn_id)56 void BTA_GATTC_Close(uint16_t conn_id) {
57   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
58   gatt_interface->Close(conn_id);
59 }
60 
BTA_GATTC_ServiceSearchRequest(uint16_t conn_id,bluetooth::Uuid p_srvc_uuid)61 void BTA_GATTC_ServiceSearchRequest(uint16_t conn_id, bluetooth::Uuid p_srvc_uuid) {
62   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
63   gatt_interface->ServiceSearchRequest(conn_id, &p_srvc_uuid);
64 }
65 
BTA_GATTC_ServiceSearchAllRequest(uint16_t conn_id)66 void BTA_GATTC_ServiceSearchAllRequest(uint16_t conn_id) {
67   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
68   gatt_interface->ServiceSearchRequest(conn_id, nullptr);
69 }
70 
BTA_GATTC_SendIndConfirm(uint16_t conn_id,uint16_t cid)71 void BTA_GATTC_SendIndConfirm(uint16_t conn_id, uint16_t cid) {
72   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
73   gatt_interface->SendIndConfirm(conn_id, cid);
74 }
75 
BTA_GATTC_GetServices(uint16_t conn_id)76 const std::list<gatt::Service>* BTA_GATTC_GetServices(uint16_t conn_id) {
77   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
78   return gatt_interface->GetServices(conn_id);
79 }
80 
BTA_GATTC_GetCharacteristic(uint16_t conn_id,uint16_t handle)81 const gatt::Characteristic* BTA_GATTC_GetCharacteristic(uint16_t conn_id, uint16_t handle) {
82   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
83   return gatt_interface->GetCharacteristic(conn_id, handle);
84 }
85 
BTA_GATTC_GetOwningService(uint16_t conn_id,uint16_t handle)86 const gatt::Service* BTA_GATTC_GetOwningService(uint16_t conn_id, uint16_t handle) {
87   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
88   return gatt_interface->GetOwningService(conn_id, handle);
89 }
90 
BTA_GATTC_RegisterForNotifications(tGATT_IF client_if,const RawAddress & remote_bda,uint16_t handle)91 tGATT_STATUS BTA_GATTC_RegisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
92                                                 uint16_t handle) {
93   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
94   return gatt_interface->RegisterForNotifications(client_if, remote_bda, handle);
95 }
96 
BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if,const RawAddress & remote_bda,uint16_t handle)97 tGATT_STATUS BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
98                                                   uint16_t handle) {
99   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
100   return gatt_interface->DeregisterForNotifications(client_if, remote_bda, handle);
101 }
102 
BTA_GATTS_Disable(void)103 void BTA_GATTS_Disable(void) {
104   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
105   gatt_server_interface->Disable();
106 }
BTA_GATTS_AppDeregister(tGATT_IF server_if)107 void BTA_GATTS_AppDeregister(tGATT_IF server_if) {
108   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
109   gatt_server_interface->AppDeregister(server_if);
110 }
BTA_GATTS_AppRegister(const bluetooth::Uuid & app_uuid,tBTA_GATTS_CBACK * p_cback,bool eatt_support)111 void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid, tBTA_GATTS_CBACK* p_cback,
112                            bool eatt_support) {
113   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
114   gatt_server_interface->AppRegister(app_uuid, p_cback, eatt_support);
115 }
BTA_GATTS_CancelOpen(tGATT_IF server_if,const RawAddress & remote_bda,bool is_direct)116 void BTA_GATTS_CancelOpen(tGATT_IF server_if, const RawAddress& remote_bda, bool is_direct) {
117   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
118   gatt_server_interface->CancelOpen(server_if, remote_bda, is_direct);
119 }
BTA_GATTS_Close(uint16_t conn_id)120 void BTA_GATTS_Close(uint16_t conn_id) {
121   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
122   gatt_server_interface->Close(conn_id);
123 }
BTA_GATTS_AddService(tGATT_IF server_if,std::vector<btgatt_db_element_t> service,BTA_GATTS_AddServiceCb cb)124 void BTA_GATTS_AddService(tGATT_IF server_if, std::vector<btgatt_db_element_t> service,
125                           BTA_GATTS_AddServiceCb cb) {
126   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
127   gatt_server_interface->AddService(server_if, service, cb);
128 }
BTA_GATTS_DeleteService(uint16_t service_id)129 void BTA_GATTS_DeleteService(uint16_t service_id) {
130   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
131   gatt_server_interface->DeleteService(service_id);
132 }
BTA_GATTS_HandleValueIndication(uint16_t conn_id,uint16_t attr_id,std::vector<uint8_t> value,bool need_confirm)133 void BTA_GATTS_HandleValueIndication(uint16_t conn_id, uint16_t attr_id, std::vector<uint8_t> value,
134                                      bool need_confirm) {
135   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
136   gatt_server_interface->HandleValueIndication(conn_id, attr_id, value, need_confirm);
137 }
BTA_GATTS_Open(tGATT_IF server_if,const RawAddress & remote_bda,tBLE_ADDR_TYPE addr_type,bool is_direct,tBT_TRANSPORT transport)138 void BTA_GATTS_Open(tGATT_IF server_if, const RawAddress& remote_bda, tBLE_ADDR_TYPE addr_type,
139                     bool is_direct, tBT_TRANSPORT transport) {
140   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
141   gatt_server_interface->Open(server_if, remote_bda, addr_type, is_direct, transport);
142 }
BTA_GATTS_SendRsp(uint16_t conn_id,uint32_t trans_id,tGATT_STATUS status,tGATTS_RSP * p_msg)143 void BTA_GATTS_SendRsp(uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status,
144                        tGATTS_RSP* p_msg) {
145   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
146   gatt_server_interface->SendRsp(conn_id, trans_id, status, p_msg);
147 }
BTA_GATTS_StopService(uint16_t service_id)148 void BTA_GATTS_StopService(uint16_t service_id) {
149   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
150   gatt_server_interface->StopService(service_id);
151 }
152 
BTA_GATTS_InitBonded(void)153 void BTA_GATTS_InitBonded(void) {
154   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
155   gatt_server_interface->InitBonded();
156 }
157 
SetMockBtaGattServerInterface(MockBtaGattServerInterface * mock_bta_gatt_server_interface)158 void gatt::SetMockBtaGattServerInterface(
159         MockBtaGattServerInterface* mock_bta_gatt_server_interface) {
160   gatt_server_interface = mock_bta_gatt_server_interface;
161 }
162