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
BleStartAdvEx(int * advId,const StartAdvRawData rawData,BleAdvParams advParam)144 int BleStartAdvEx(int *advId, const StartAdvRawData rawData, BleAdvParams advParam)
145 {
146 return MockBluetooth::GetMocker()->BleStartAdvEx(advId, rawData, advParam);
147 }
148
BleStopAdv(int advId)149 int BleStopAdv(int advId)
150 {
151 return OHOS_BT_STATUS_SUCCESS;
152 }
153
BleGattcRegister(BtUuid appUuid)154 int BleGattcRegister(BtUuid appUuid)
155 {
156 return MockBluetooth::GetMocker()->BleGattcRegister(appUuid);
157 }
158
BleGattcConnect(int clientId,BtGattClientCallbacks * func,const BdAddr * bdAddr,bool isAutoConnect,BtTransportType transport)159 int BleGattcConnect(
160 int clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)
161 {
162 return MockBluetooth::GetMocker()->BleGattcConnect(clientId, func, bdAddr, isAutoConnect, transport);
163 }
164
BleGattcDisconnect(int clientId)165 int BleGattcDisconnect(int clientId)
166 {
167 return MockBluetooth::GetMocker()->BleGattcDisconnect(clientId);
168 }
169
BleGattcSearchServices(int clientId)170 int BleGattcSearchServices(int clientId)
171 {
172 return MockBluetooth::GetMocker()->BleGattcSearchServices(clientId);
173 }
174
BleGattcGetService(int clientId,BtUuid serviceUuid)175 bool BleGattcGetService(int clientId, BtUuid serviceUuid)
176 {
177 return MockBluetooth::GetMocker()->BleGattcGetService(clientId, serviceUuid);
178 }
179
BleGattcRegisterNotification(int clientId,BtGattCharacteristic characteristic,bool enable)180 int BleGattcRegisterNotification(int clientId, BtGattCharacteristic characteristic, bool enable)
181 {
182 return MockBluetooth::GetMocker()->BleGattcRegisterNotification(clientId, characteristic, enable);
183 }
184
BleGattcConfigureMtuSize(int clientId,int mtuSize)185 int BleGattcConfigureMtuSize(int clientId, int mtuSize)
186 {
187 return MockBluetooth::GetMocker()->BleGattcConfigureMtuSize(clientId, mtuSize);
188 }
189
BleGattcWriteCharacteristic(int clientId,BtGattCharacteristic characteristic,BtGattWriteType writeType,int len,const char * value)190 int BleGattcWriteCharacteristic(
191 int clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int len, const char *value)
192 {
193 return MockBluetooth::GetMocker()->BleGattcWriteCharacteristic(clientId, characteristic, writeType, len, value);
194 }
195
BleGattcUnRegister(int clientId)196 int BleGattcUnRegister(int clientId)
197 {
198 return MockBluetooth::GetMocker()->BleGattcUnRegister(clientId);
199 }
200
BleGattcSetFastestConn(int clientId,bool fastestConnFlag)201 int BleGattcSetFastestConn(int clientId, bool fastestConnFlag)
202 {
203 return MockBluetooth::GetMocker()->BleGattcSetFastestConn(clientId, fastestConnFlag);
204 }
205
BleGattcSetPriority(int clientId,const BdAddr * bdAddr,BtGattPriority priority)206 int BleGattcSetPriority(int clientId, const BdAddr *bdAddr, BtGattPriority priority)
207 {
208 return MockBluetooth::GetMocker()->BleGattcSetPriority(clientId, bdAddr, priority);
209 }
210
BleGattsRegisterCallbacks(BtGattServerCallbacks * func)211 int BleGattsRegisterCallbacks(BtGattServerCallbacks *func)
212 {
213 return MockBluetooth::GetMocker()->BleGattsRegisterCallbacks(func);
214 }
215
BleGattsRegister(BtUuid appUuid)216 int BleGattsRegister(BtUuid appUuid)
217 {
218 return MockBluetooth::GetMocker()->BleGattsRegister(appUuid);
219 }
220
BleGattsAddService(int serverId,BtUuid srvcUuid,bool isPrimary,int number)221 int BleGattsAddService(int serverId, BtUuid srvcUuid, bool isPrimary, int number)
222 {
223 return MockBluetooth::GetMocker()->BleGattsAddService(serverId, srvcUuid, isPrimary, number);
224 }
225
BleGattsUnRegister(int serverId)226 int BleGattsUnRegister(int serverId)
227 {
228 return MockBluetooth::GetMocker()->BleGattsUnRegister(serverId);
229 }
230
BleGattsAddCharacteristic(int serverId,int srvcHandle,BtUuid characUuid,int properties,int permissions)231 int BleGattsAddCharacteristic(int serverId, int srvcHandle, BtUuid characUuid, int properties, int permissions)
232 {
233 return MockBluetooth::GetMocker()->BleGattsAddCharacteristic(
234 serverId, srvcHandle, characUuid, properties, permissions);
235 }
236
BleGattsAddDescriptor(int serverId,int srvcHandle,BtUuid descUuid,int permissions)237 int BleGattsAddDescriptor(int serverId, int srvcHandle, BtUuid descUuid, int permissions)
238 {
239 return MockBluetooth::GetMocker()->BleGattsAddDescriptor(serverId, srvcHandle, descUuid, permissions);
240 }
241
BleGattsStartService(int serverId,int srvcHandle)242 int BleGattsStartService(int serverId, int srvcHandle)
243 {
244 return MockBluetooth::GetMocker()->BleGattsStartService(serverId, srvcHandle);
245 }
246
BleGattsStopService(int serverId,int srvcHandle)247 int BleGattsStopService(int serverId, int srvcHandle)
248 {
249 return MockBluetooth::GetMocker()->BleGattsStopService(serverId, srvcHandle);
250 }
251
BleGattsDeleteService(int serverId,int srvcHandle)252 int BleGattsDeleteService(int serverId, int srvcHandle)
253 {
254 return MockBluetooth::GetMocker()->BleGattsDeleteService(serverId, srvcHandle);
255 }
256
BleGattsDisconnect(int serverId,BdAddr bdAddr,int connId)257 int BleGattsDisconnect(int serverId, BdAddr bdAddr, int connId)
258 {
259 return MockBluetooth::GetMocker()->BleGattsDisconnect(serverId, bdAddr, connId);
260 }
261
BleGattsSendResponse(int serverId,GattsSendRspParam * param)262 int BleGattsSendResponse(int serverId, GattsSendRspParam *param)
263 {
264 return MockBluetooth::GetMocker()->BleGattsSendResponse(serverId, param);
265 }
266
BleGattsSendIndication(int serverId,GattsSendIndParam * param)267 int BleGattsSendIndication(int serverId, GattsSendIndParam *param)
268 {
269 return MockBluetooth::GetMocker()->BleGattsSendIndication(serverId, param);
270 }
271
RegisterBroadcastMediumFunction(SoftbusMediumType type,const SoftbusBroadcastMediumInterface * interface)272 int32_t RegisterBroadcastMediumFunction(SoftbusMediumType type, const SoftbusBroadcastMediumInterface *interface)
273 {
274 DISC_LOGI(DISC_BLE_ADAPTER, "begin to register func");
275 int32_t ret = MockBluetooth::GetMocker()->RegisterBroadcastMediumFunction(type, interface);
276 DISC_LOGI(DISC_BLE_ADAPTER, "end to register func");
277 return ret;
278 }
279