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