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