1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <securec.h>
17
18 #include "bluetooth_mock.h"
19 #include "disc_log.h"
20
21 MockBluetooth *MockBluetooth::targetMocker = nullptr;
22 BtGapCallBacks *MockBluetooth::btGapCallback = nullptr;
23 BtGattCallbacks *MockBluetooth::btGattCallback = nullptr;
24 BleScanCallbacks *MockBluetooth::bleScanCallback = nullptr;
25 const SoftbusBroadcastMediumInterface *MockBluetooth::interface = nullptr;
26
ActionGapRegisterCallbacks(BtGapCallBacks * func)27 static int32_t ActionGapRegisterCallbacks(BtGapCallBacks *func)
28 {
29 MockBluetooth::btGapCallback = func;
30 return OHOS_BT_STATUS_SUCCESS;
31 }
32
ActionBleGattRegisterCallbacks(BtGattCallbacks * func)33 static int32_t ActionBleGattRegisterCallbacks(BtGattCallbacks *func)
34 {
35 MockBluetooth::btGattCallback = func;
36 return OHOS_BT_STATUS_SUCCESS;
37 }
38
ActionBleRegisterScanCallbacks(BleScanCallbacks * func,int32_t * scannerId)39 static int32_t ActionBleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId)
40 {
41 MockBluetooth::bleScanCallback = func;
42 return OHOS_BT_STATUS_SUCCESS;
43 }
44
ActionBleDeregisterScanCallbacks(int32_t scannerId)45 static int32_t ActionBleDeregisterScanCallbacks(int32_t scannerId)
46 {
47 MockBluetooth::bleScanCallback = nullptr;
48 return OHOS_BT_STATUS_SUCCESS;
49 }
50
ActionRegisterBroadcastMediumFunction(SoftbusMediumType type,const SoftbusBroadcastMediumInterface * func)51 static int32_t ActionRegisterBroadcastMediumFunction(
52 SoftbusMediumType type, const SoftbusBroadcastMediumInterface *func)
53 {
54 MockBluetooth::interface = func;
55 return OHOS_BT_STATUS_SUCCESS;
56 }
57
GetMocker()58 MockBluetooth *MockBluetooth::GetMocker()
59 {
60 return targetMocker;
61 }
62
MockBluetooth()63 MockBluetooth::MockBluetooth()
64 {
65 MockBluetooth::targetMocker = this;
66 // common callback is register global. Need to pay attention to the calling timing.
67 EXPECT_CALL(*this, GapRegisterCallbacks).WillRepeatedly(ActionGapRegisterCallbacks);
68 EXPECT_CALL(*this, BleGattRegisterCallbacks).WillRepeatedly(ActionBleGattRegisterCallbacks);
69 EXPECT_CALL(*this, BleRegisterScanCallbacks).WillRepeatedly(ActionBleRegisterScanCallbacks);
70 EXPECT_CALL(*this, BleDeregisterScanCallbacks).WillRepeatedly(ActionBleDeregisterScanCallbacks);
71 EXPECT_CALL(*this, RegisterBroadcastMediumFunction).WillRepeatedly(ActionRegisterBroadcastMediumFunction);
72 }
73
~MockBluetooth()74 MockBluetooth::~MockBluetooth()
75 {
76 MockBluetooth::targetMocker = nullptr;
77 }
78
EnableBle(void)79 bool EnableBle(void)
80 {
81 return MockBluetooth::GetMocker()->EnableBle();
82 }
83
DisableBle(void)84 bool DisableBle(void)
85 {
86 return MockBluetooth::GetMocker()->DisableBle();
87 }
88
IsBleEnabled()89 bool IsBleEnabled()
90 {
91 return MockBluetooth::GetMocker()->IsBleEnabled();
92 }
93
IsLpDeviceAvailable()94 bool IsLpDeviceAvailable()
95 {
96 return true;
97 }
98
GetLocalAddr(unsigned char * mac,unsigned int len)99 bool GetLocalAddr(unsigned char *mac, unsigned int len)
100 {
101 return MockBluetooth::GetMocker()->GetLocalAddr(mac, len);
102 }
103
SetLocalName(unsigned char * localName,unsigned char length)104 bool SetLocalName(unsigned char *localName, unsigned char length)
105 {
106 return MockBluetooth::GetMocker()->SetLocalName(localName, length);
107 }
108
GapRegisterCallbacks(BtGapCallBacks * func)109 int32_t GapRegisterCallbacks(BtGapCallBacks *func)
110 {
111 return MockBluetooth::GetMocker()->GapRegisterCallbacks(func);
112 }
113
PairRequestReply(const BdAddr * bdAddr,int32_t transport,bool accept)114 bool PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept)
115 {
116 return MockBluetooth::GetMocker()->PairRequestReply(bdAddr, transport, accept);
117 }
118
SetDevicePairingConfirmation(const BdAddr * bdAddr,int32_t transport,bool accept)119 bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept)
120 {
121 return MockBluetooth::GetMocker()->SetDevicePairingConfirmation(bdAddr, transport, accept);
122 }
123
BleGattRegisterCallbacks(BtGattCallbacks * func)124 int32_t BleGattRegisterCallbacks(BtGattCallbacks *func)
125 {
126 return MockBluetooth::GetMocker()->BleGattRegisterCallbacks(func);
127 }
128
BleRegisterScanCallbacks(BleScanCallbacks * func,int32_t * scannerId)129 int32_t BleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId)
130 {
131 return MockBluetooth::GetMocker()->BleRegisterScanCallbacks(func, scannerId);
132 }
133
BleDeregisterScanCallbacks(int32_t scannerId)134 int32_t BleDeregisterScanCallbacks(int32_t scannerId)
135 {
136 return MockBluetooth::GetMocker()->BleDeregisterScanCallbacks(scannerId);
137 }
138
BleStartScanEx(int32_t scannerId,const BleScanConfigs * configs,const BleScanNativeFilter * filter,uint32_t filterSize)139 int32_t BleStartScanEx(
140 int32_t scannerId, const BleScanConfigs *configs, const BleScanNativeFilter *filter, uint32_t filterSize)
141 {
142 return MockBluetooth::GetMocker()->BleStartScanEx(scannerId, configs, filter, filterSize);
143 }
144
BleChangeScanParams(int32_t scannerId,const BleScanConfigs * config,const BleScanNativeFilter * filter,uint32_t filterSize,uint32_t filterAction)145 int BleChangeScanParams(int32_t scannerId, const BleScanConfigs *config, const BleScanNativeFilter *filter,
146 uint32_t filterSize, uint32_t filterAction)
147 {
148 return MockBluetooth::GetMocker()->BleChangeScanParams(scannerId, config, filter,
149 filterSize, filterAction);
150 }
151
BleStopScan(int32_t scannerId)152 int32_t BleStopScan(int32_t scannerId)
153 {
154 return MockBluetooth::GetMocker()->BleStopScan(scannerId);
155 }
156
GetAdvHandle(int32_t btAdvId,int32_t * bcHandle)157 int32_t GetAdvHandle(int32_t btAdvId, int32_t *bcHandle)
158 {
159 return OHOS_BT_STATUS_SUCCESS;
160 }
161
EnableSyncDataToLpDevice()162 int32_t EnableSyncDataToLpDevice()
163 {
164 return OHOS_BT_STATUS_SUCCESS;
165 }
166
DisableSyncDataToLpDevice()167 int32_t DisableSyncDataToLpDevice()
168 {
169 return OHOS_BT_STATUS_SUCCESS;
170 }
171
SetLpDeviceAdvParam(int32_t duration,int32_t maxExtAdvEvents,int32_t window,int32_t interval,int32_t bcHandle)172 int32_t SetLpDeviceAdvParam(
173 int32_t duration, int32_t maxExtAdvEvents, int32_t window, int32_t interval, int32_t bcHandle)
174 {
175 return OHOS_BT_STATUS_SUCCESS;
176 }
177
BleStartAdvEx(int32_t * advId,const StartAdvRawData rawData,BleAdvParams advParam)178 int32_t BleStartAdvEx(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam)
179 {
180 return MockBluetooth::GetMocker()->BleStartAdvEx(advId, rawData, advParam);
181 }
182
BleStopAdv(int32_t advId)183 int32_t BleStopAdv(int32_t advId)
184 {
185 return OHOS_BT_STATUS_SUCCESS;
186 }
187
BleGattcRegister(BtUuid appUuid)188 int32_t BleGattcRegister(BtUuid appUuid)
189 {
190 return MockBluetooth::GetMocker()->BleGattcRegister(appUuid);
191 }
192
BleGattcConnect(int32_t clientId,BtGattClientCallbacks * func,const BdAddr * bdAddr,bool isAutoConnect,BtTransportType transport)193 int32_t BleGattcConnect(
194 int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)
195 {
196 return MockBluetooth::GetMocker()->BleGattcConnect(clientId, func, bdAddr, isAutoConnect, transport);
197 }
198
BleGattcDisconnect(int32_t clientId)199 int32_t BleGattcDisconnect(int32_t clientId)
200 {
201 return MockBluetooth::GetMocker()->BleGattcDisconnect(clientId);
202 }
203
BleGattcSearchServices(int32_t clientId)204 int32_t BleGattcSearchServices(int32_t clientId)
205 {
206 return MockBluetooth::GetMocker()->BleGattcSearchServices(clientId);
207 }
208
BleGattcGetService(int32_t clientId,BtUuid serviceUuid)209 bool BleGattcGetService(int32_t clientId, BtUuid serviceUuid)
210 {
211 return MockBluetooth::GetMocker()->BleGattcGetService(clientId, serviceUuid);
212 }
213
BleGattcRegisterNotification(int32_t clientId,BtGattCharacteristic characteristic,bool enable)214 int32_t BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable)
215 {
216 return MockBluetooth::GetMocker()->BleGattcRegisterNotification(clientId, characteristic, enable);
217 }
218
BleGattcConfigureMtuSize(int32_t clientId,int32_t mtuSize)219 int32_t BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize)
220 {
221 return MockBluetooth::GetMocker()->BleGattcConfigureMtuSize(clientId, mtuSize);
222 }
223
BleGattcWriteCharacteristic(int32_t clientId,BtGattCharacteristic characteristic,BtGattWriteType writeType,int32_t len,const char * value)224 int32_t BleGattcWriteCharacteristic(
225 int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value)
226 {
227 return MockBluetooth::GetMocker()->BleGattcWriteCharacteristic(clientId, characteristic, writeType, len, value);
228 }
229
BleGattcUnRegister(int32_t clientId)230 int32_t BleGattcUnRegister(int32_t clientId)
231 {
232 return MockBluetooth::GetMocker()->BleGattcUnRegister(clientId);
233 }
234
BleGattcSetFastestConn(int32_t clientId,bool fastestConnFlag)235 int32_t BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag)
236 {
237 return MockBluetooth::GetMocker()->BleGattcSetFastestConn(clientId, fastestConnFlag);
238 }
239
BleGattcSetPriority(int32_t clientId,const BdAddr * bdAddr,BtGattPriority priority)240 int32_t BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority)
241 {
242 return MockBluetooth::GetMocker()->BleGattcSetPriority(clientId, bdAddr, priority);
243 }
244
BleGattsRegisterCallbacks(BtGattServerCallbacks * func)245 int32_t BleGattsRegisterCallbacks(BtGattServerCallbacks *func)
246 {
247 return MockBluetooth::GetMocker()->BleGattsRegisterCallbacks(func);
248 }
249
BleGattsRegister(BtUuid appUuid)250 int32_t BleGattsRegister(BtUuid appUuid)
251 {
252 return MockBluetooth::GetMocker()->BleGattsRegister(appUuid);
253 }
254
BleGattsAddService(int32_t serverId,BtUuid srvcUuid,bool isPrimary,int32_t number)255 int32_t BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number)
256 {
257 return MockBluetooth::GetMocker()->BleGattsAddService(serverId, srvcUuid, isPrimary, number);
258 }
259
BleGattsUnRegister(int32_t serverId)260 int32_t BleGattsUnRegister(int32_t serverId)
261 {
262 return MockBluetooth::GetMocker()->BleGattsUnRegister(serverId);
263 }
264
BleGattsAddCharacteristic(int32_t serverId,int32_t srvcHandle,BtUuid characUuid,int32_t properties,int32_t permissions)265 int32_t BleGattsAddCharacteristic(
266 int32_t serverId, int32_t srvcHandle, BtUuid characUuid, int32_t properties, int32_t permissions)
267 {
268 return MockBluetooth::GetMocker()->BleGattsAddCharacteristic(
269 serverId, srvcHandle, characUuid, properties, permissions);
270 }
271
BleGattsAddDescriptor(int32_t serverId,int32_t srvcHandle,BtUuid descUuid,int32_t permissions)272 int32_t BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions)
273 {
274 return MockBluetooth::GetMocker()->BleGattsAddDescriptor(serverId, srvcHandle, descUuid, permissions);
275 }
276
BleGattsStartService(int32_t serverId,int32_t srvcHandle)277 int32_t BleGattsStartService(int32_t serverId, int32_t srvcHandle)
278 {
279 return MockBluetooth::GetMocker()->BleGattsStartService(serverId, srvcHandle);
280 }
281
BleGattsStopService(int32_t serverId,int32_t srvcHandle)282 int32_t BleGattsStopService(int32_t serverId, int32_t srvcHandle)
283 {
284 return MockBluetooth::GetMocker()->BleGattsStopService(serverId, srvcHandle);
285 }
286
BleGattsDeleteService(int32_t serverId,int32_t srvcHandle)287 int32_t BleGattsDeleteService(int32_t serverId, int32_t srvcHandle)
288 {
289 return MockBluetooth::GetMocker()->BleGattsDeleteService(serverId, srvcHandle);
290 }
291
BleGattsDisconnect(int32_t serverId,BdAddr bdAddr,int32_t connId)292 int32_t BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId)
293 {
294 return MockBluetooth::GetMocker()->BleGattsDisconnect(serverId, bdAddr, connId);
295 }
296
BleGattsSendResponse(int32_t serverId,GattsSendRspParam * param)297 int32_t BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param)
298 {
299 return MockBluetooth::GetMocker()->BleGattsSendResponse(serverId, param);
300 }
301
BleGattsSendIndication(int32_t serverId,GattsSendIndParam * param)302 int32_t BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param)
303 {
304 return MockBluetooth::GetMocker()->BleGattsSendIndication(serverId, param);
305 }
306
SoftBusAddBtStateListener(const SoftBusBtStateListener * listener,int32_t * listenerId)307 int32_t SoftBusAddBtStateListener(const SoftBusBtStateListener *listener, int32_t *listenerId)
308 {
309 return MockBluetooth::GetMocker()->SoftBusAddBtStateListener(listener, listenerId);
310 }
311
RegisterBroadcastMediumFunction(SoftbusMediumType type,const SoftbusBroadcastMediumInterface * interface)312 int32_t RegisterBroadcastMediumFunction(SoftbusMediumType type, const SoftbusBroadcastMediumInterface *interface)
313 {
314 DISC_LOGI(DISC_TEST, "begin to register func");
315 int32_t ret = MockBluetooth::GetMocker()->RegisterBroadcastMediumFunction(type, interface);
316 DISC_LOGI(DISC_TEST, "end to register func");
317 return ret;
318 }
319