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 int32_t ActionGapRegisterCallbacks(BtGapCallBacks *func)
27 {
28     MockBluetooth::btGapCallback = func;
29     return OHOS_BT_STATUS_SUCCESS;
30 }
31 
ActionBleGattRegisterCallbacks(BtGattCallbacks *func)32 static int32_t ActionBleGattRegisterCallbacks(BtGattCallbacks *func)
33 {
34     MockBluetooth::btGattCallback = func;
35     return OHOS_BT_STATUS_SUCCESS;
36 }
37 
ActionBleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId)38 static int32_t 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 int32_t 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 int32_t GapRegisterCallbacks(BtGapCallBacks *func)
104 {
105     return MockBluetooth::GetMocker()->GapRegisterCallbacks(func);
106 }
107 
PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept)108 bool PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept)
109 {
110     return MockBluetooth::GetMocker()->PairRequestReply(bdAddr, transport, accept);
111 }
112 
SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept)113 bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept)
114 {
115     return MockBluetooth::GetMocker()->SetDevicePairingConfirmation(bdAddr, transport, accept);
116 }
117 
BleGattRegisterCallbacks(BtGattCallbacks *func)118 int32_t BleGattRegisterCallbacks(BtGattCallbacks *func)
119 {
120     return MockBluetooth::GetMocker()->BleGattRegisterCallbacks(func);
121 }
122 
BleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId)123 int32_t BleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId)
124 {
125     return MockBluetooth::GetMocker()->BleRegisterScanCallbacks(func, scannerId);
126 }
127 
BleDeregisterScanCallbacks(int32_t scannerId)128 int32_t 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 int32_t 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(int32_t scannerId)139 int32_t BleStopScan(int32_t scannerId)
140 {
141     return MockBluetooth::GetMocker()->BleStopScan(scannerId);
142 }
143 
GetAdvHandle(int32_t btAdvId, int32_t *bcHandle)144 int32_t GetAdvHandle(int32_t btAdvId, int32_t *bcHandle)
145 {
146     return OHOS_BT_STATUS_SUCCESS;
147 }
148 
EnableSyncDataToLpDevice()149 int32_t EnableSyncDataToLpDevice()
150 {
151     return OHOS_BT_STATUS_SUCCESS;
152 }
153 
DisableSyncDataToLpDevice()154 int32_t 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 int32_t 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(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam)165 int32_t BleStartAdvEx(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam)
166 {
167     return MockBluetooth::GetMocker()->BleStartAdvEx(advId, rawData, advParam);
168 }
169 
BleStopAdv(int32_t advId)170 int32_t BleStopAdv(int32_t advId)
171 {
172     return OHOS_BT_STATUS_SUCCESS;
173 }
174 
BleGattcRegister(BtUuid appUuid)175 int32_t BleGattcRegister(BtUuid appUuid)
176 {
177     return MockBluetooth::GetMocker()->BleGattcRegister(appUuid);
178 }
179 
BleGattcConnect( int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)180 int32_t BleGattcConnect(
181     int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)
182 {
183     return MockBluetooth::GetMocker()->BleGattcConnect(clientId, func, bdAddr, isAutoConnect, transport);
184 }
185 
BleGattcDisconnect(int32_t clientId)186 int32_t BleGattcDisconnect(int32_t clientId)
187 {
188     return MockBluetooth::GetMocker()->BleGattcDisconnect(clientId);
189 }
190 
BleGattcSearchServices(int32_t clientId)191 int32_t BleGattcSearchServices(int32_t clientId)
192 {
193     return MockBluetooth::GetMocker()->BleGattcSearchServices(clientId);
194 }
195 
BleGattcGetService(int32_t clientId, BtUuid serviceUuid)196 bool BleGattcGetService(int32_t clientId, BtUuid serviceUuid)
197 {
198     return MockBluetooth::GetMocker()->BleGattcGetService(clientId, serviceUuid);
199 }
200 
BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable)201 int32_t BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable)
202 {
203     return MockBluetooth::GetMocker()->BleGattcRegisterNotification(clientId, characteristic, enable);
204 }
205 
BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize)206 int32_t BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize)
207 {
208     return MockBluetooth::GetMocker()->BleGattcConfigureMtuSize(clientId, mtuSize);
209 }
210 
BleGattcWriteCharacteristic( int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value)211 int32_t BleGattcWriteCharacteristic(
212     int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value)
213 {
214     return MockBluetooth::GetMocker()->BleGattcWriteCharacteristic(clientId, characteristic, writeType, len, value);
215 }
216 
BleGattcUnRegister(int32_t clientId)217 int32_t BleGattcUnRegister(int32_t clientId)
218 {
219     return MockBluetooth::GetMocker()->BleGattcUnRegister(clientId);
220 }
221 
BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag)222 int32_t BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag)
223 {
224     return MockBluetooth::GetMocker()->BleGattcSetFastestConn(clientId, fastestConnFlag);
225 }
226 
BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority)227 int32_t BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority)
228 {
229     return MockBluetooth::GetMocker()->BleGattcSetPriority(clientId, bdAddr, priority);
230 }
231 
BleGattsRegisterCallbacks(BtGattServerCallbacks *func)232 int32_t BleGattsRegisterCallbacks(BtGattServerCallbacks *func)
233 {
234     return MockBluetooth::GetMocker()->BleGattsRegisterCallbacks(func);
235 }
236 
BleGattsRegister(BtUuid appUuid)237 int32_t BleGattsRegister(BtUuid appUuid)
238 {
239     return MockBluetooth::GetMocker()->BleGattsRegister(appUuid);
240 }
241 
BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number)242 int32_t BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number)
243 {
244     return MockBluetooth::GetMocker()->BleGattsAddService(serverId, srvcUuid, isPrimary, number);
245 }
246 
BleGattsUnRegister(int32_t serverId)247 int32_t BleGattsUnRegister(int32_t serverId)
248 {
249     return MockBluetooth::GetMocker()->BleGattsUnRegister(serverId);
250 }
251 
BleGattsAddCharacteristic(int32_t serverId, int32_t srvcHandle, BtUuid characUuid, int32_t properties, int32_t permissions)252 int32_t BleGattsAddCharacteristic(int32_t serverId, int32_t srvcHandle,
253                                   BtUuid characUuid, int32_t properties, int32_t permissions)
254 {
255     return MockBluetooth::GetMocker()->BleGattsAddCharacteristic(
256         serverId, srvcHandle, characUuid, properties, permissions);
257 }
258 
BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions)259 int32_t BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions)
260 {
261     return MockBluetooth::GetMocker()->BleGattsAddDescriptor(serverId, srvcHandle, descUuid, permissions);
262 }
263 
BleGattsStartService(int32_t serverId, int32_t srvcHandle)264 int32_t BleGattsStartService(int32_t serverId, int32_t srvcHandle)
265 {
266     return MockBluetooth::GetMocker()->BleGattsStartService(serverId, srvcHandle);
267 }
268 
BleGattsStopService(int32_t serverId, int32_t srvcHandle)269 int32_t BleGattsStopService(int32_t serverId, int32_t srvcHandle)
270 {
271     return MockBluetooth::GetMocker()->BleGattsStopService(serverId, srvcHandle);
272 }
273 
BleGattsDeleteService(int32_t serverId, int32_t srvcHandle)274 int32_t BleGattsDeleteService(int32_t serverId, int32_t srvcHandle)
275 {
276     return MockBluetooth::GetMocker()->BleGattsDeleteService(serverId, srvcHandle);
277 }
278 
BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId)279 int32_t BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId)
280 {
281     return MockBluetooth::GetMocker()->BleGattsDisconnect(serverId, bdAddr, connId);
282 }
283 
BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param)284 int32_t BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param)
285 {
286     return MockBluetooth::GetMocker()->BleGattsSendResponse(serverId, param);
287 }
288 
BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param)289 int32_t BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param)
290 {
291     return MockBluetooth::GetMocker()->BleGattsSendIndication(serverId, param);
292 }
293 
SoftBusAddBtStateListener(const SoftBusBtStateListener *listener)294 int32_t SoftBusAddBtStateListener(const SoftBusBtStateListener *listener)
295 {
296     return MockBluetooth::GetMocker()->SoftBusAddBtStateListener(listener);
297 }
298 
RegisterBroadcastMediumFunction(SoftbusMediumType type, const SoftbusBroadcastMediumInterface *interface)299 int32_t RegisterBroadcastMediumFunction(SoftbusMediumType type, const SoftbusBroadcastMediumInterface *interface)
300 {
301     DISC_LOGI(DISC_TEST, "begin to register func");
302     int32_t ret = MockBluetooth::GetMocker()->RegisterBroadcastMediumFunction(type, interface);
303     DISC_LOGI(DISC_TEST, "end to register func");
304     return ret;
305 }
306