• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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(const std::string & name,tBTA_GATTC_CBACK * p_client_cb,BtaAppRegisterCallback cb,bool eatt_support)34 void BTA_GATTC_AppRegister(const std::string& name, tBTA_GATTC_CBACK* p_client_cb,
35                            BtaAppRegisterCallback cb, bool eatt_support) {
36   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
37   gatt_interface->AppRegister(name, 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_ReadCharacteristic(tCONN_ID conn_id,uint16_t handle,tGATT_AUTH_REQ auth_req,GATT_READ_OP_CB callback,void * cb_data)91 void BTA_GATTC_ReadCharacteristic(tCONN_ID conn_id, uint16_t handle, tGATT_AUTH_REQ auth_req,
92                                   GATT_READ_OP_CB callback, void* cb_data) {
93   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
94   gatt_interface->ReadCharacteristic(conn_id, handle, auth_req, callback, cb_data);
95 }
96 
BTA_GATTC_WriteCharValue(tCONN_ID conn_id,uint16_t handle,tGATT_WRITE_TYPE write_type,std::vector<uint8_t> value,tGATT_AUTH_REQ auth_req,GATT_WRITE_OP_CB callback,void * cb_data)97 void BTA_GATTC_WriteCharValue(tCONN_ID conn_id, uint16_t handle, tGATT_WRITE_TYPE write_type,
98                               std::vector<uint8_t> value, tGATT_AUTH_REQ auth_req,
99                               GATT_WRITE_OP_CB callback, void* cb_data) {
100   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
101   gatt_interface->WriteCharValue(conn_id, handle, write_type, value, auth_req, callback, cb_data);
102 }
103 
BTA_GATTC_WriteCharDescr(tCONN_ID conn_id,uint16_t handle,std::vector<uint8_t> value,tGATT_AUTH_REQ auth_req,GATT_WRITE_OP_CB callback,void * cb_data)104 void BTA_GATTC_WriteCharDescr(tCONN_ID conn_id, uint16_t handle, std::vector<uint8_t> value,
105                               tGATT_AUTH_REQ auth_req, GATT_WRITE_OP_CB callback, void* cb_data) {
106   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
107   gatt_interface->WriteCharDescr(conn_id, handle, value, auth_req, callback, cb_data);
108 }
109 
BTA_GATTC_RegisterForNotifications(tGATT_IF client_if,const RawAddress & remote_bda,uint16_t handle)110 tGATT_STATUS BTA_GATTC_RegisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
111                                                 uint16_t handle) {
112   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
113   return gatt_interface->RegisterForNotifications(client_if, remote_bda, handle);
114 }
115 
BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if,const RawAddress & remote_bda,uint16_t handle)116 tGATT_STATUS BTA_GATTC_DeregisterForNotifications(tGATT_IF client_if, const RawAddress& remote_bda,
117                                                   uint16_t handle) {
118   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
119   return gatt_interface->DeregisterForNotifications(client_if, remote_bda, handle);
120 }
121 
BTA_GATTC_ConfigureMTU(tCONN_ID conn_id,uint16_t mtu)122 void BTA_GATTC_ConfigureMTU(tCONN_ID conn_id, uint16_t mtu) {
123   log::assert_that(gatt_interface != nullptr, "Mock GATT interface not set!");
124   gatt_interface->ConfigureMTU(conn_id, mtu);
125 }
126 
BTA_GATTS_Disable(void)127 void BTA_GATTS_Disable(void) {
128   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
129   gatt_server_interface->Disable();
130 }
BTA_GATTS_AppDeregister(tGATT_IF server_if)131 void BTA_GATTS_AppDeregister(tGATT_IF server_if) {
132   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
133   gatt_server_interface->AppDeregister(server_if);
134 }
BTA_GATTS_AppRegister(const bluetooth::Uuid & app_uuid,tBTA_GATTS_CBACK * p_cback,bool eatt_support)135 void BTA_GATTS_AppRegister(const bluetooth::Uuid& app_uuid, tBTA_GATTS_CBACK* p_cback,
136                            bool eatt_support) {
137   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
138   gatt_server_interface->AppRegister(app_uuid, p_cback, eatt_support);
139 }
BTA_GATTS_CancelOpen(tGATT_IF server_if,const RawAddress & remote_bda,bool is_direct)140 void BTA_GATTS_CancelOpen(tGATT_IF server_if, const RawAddress& remote_bda, bool is_direct) {
141   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
142   gatt_server_interface->CancelOpen(server_if, remote_bda, is_direct);
143 }
BTA_GATTS_Close(uint16_t conn_id)144 void BTA_GATTS_Close(uint16_t conn_id) {
145   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
146   gatt_server_interface->Close(conn_id);
147 }
BTA_GATTS_AddService(tGATT_IF server_if,std::vector<btgatt_db_element_t> service,BTA_GATTS_AddServiceCb cb)148 void BTA_GATTS_AddService(tGATT_IF server_if, std::vector<btgatt_db_element_t> service,
149                           BTA_GATTS_AddServiceCb cb) {
150   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
151   gatt_server_interface->AddService(server_if, service, cb);
152 }
BTA_GATTS_DeleteService(uint16_t service_id)153 void BTA_GATTS_DeleteService(uint16_t service_id) {
154   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
155   gatt_server_interface->DeleteService(service_id);
156 }
BTA_GATTS_HandleValueIndication(uint16_t conn_id,uint16_t attr_id,std::vector<uint8_t> value,bool need_confirm)157 void BTA_GATTS_HandleValueIndication(uint16_t conn_id, uint16_t attr_id, std::vector<uint8_t> value,
158                                      bool need_confirm) {
159   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
160   gatt_server_interface->HandleValueIndication(conn_id, attr_id, value, need_confirm);
161 }
BTA_GATTS_Open(tGATT_IF server_if,const RawAddress & remote_bda,tBLE_ADDR_TYPE addr_type,bool is_direct,tBT_TRANSPORT transport)162 void BTA_GATTS_Open(tGATT_IF server_if, const RawAddress& remote_bda, tBLE_ADDR_TYPE addr_type,
163                     bool is_direct, tBT_TRANSPORT transport) {
164   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
165   gatt_server_interface->Open(server_if, remote_bda, addr_type, is_direct, transport);
166 }
BTA_GATTS_SendRsp(uint16_t conn_id,uint32_t trans_id,tGATT_STATUS status,tGATTS_RSP * p_msg)167 void BTA_GATTS_SendRsp(uint16_t conn_id, uint32_t trans_id, tGATT_STATUS status,
168                        tGATTS_RSP* p_msg) {
169   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
170   gatt_server_interface->SendRsp(conn_id, trans_id, status, p_msg);
171 }
BTA_GATTS_StopService(uint16_t service_id)172 void BTA_GATTS_StopService(uint16_t service_id) {
173   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
174   gatt_server_interface->StopService(service_id);
175 }
176 
BTA_GATTS_InitBonded(void)177 void BTA_GATTS_InitBonded(void) {
178   log::assert_that(gatt_server_interface != nullptr, "Mock GATT server interface not set!");
179   gatt_server_interface->InitBonded();
180 }
181 
SetMockBtaGattServerInterface(MockBtaGattServerInterface * mock_bta_gatt_server_interface)182 void gatt::SetMockBtaGattServerInterface(
183         MockBtaGattServerInterface* mock_bta_gatt_server_interface) {
184   gatt_server_interface = mock_bta_gatt_server_interface;
185 }
186