1 /*
2 * Copyright (c) 2022 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 "softbus_common.h"
20 #include "softbus_error_code.h"
21 #include "softbus_utils.h"
22
23 MockBluetooth *MockBluetooth::targetMocker = nullptr;
24 BtGapCallBacks *MockBluetooth::btGapCallback = nullptr;
25 BtGattCallbacks *MockBluetooth::btGattCallback = nullptr;
26 BleScanCallbacks *MockBluetooth::bleScanCallback = nullptr;
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
GetMocker()40 MockBluetooth *MockBluetooth::GetMocker()
41 {
42 return targetMocker;
43 }
44
MockBluetooth()45 MockBluetooth::MockBluetooth()
46 {
47 MockBluetooth::targetMocker = this;
48 // common callback is register glabal
49 EXPECT_CALL(*this, GapRegisterCallbacks).WillRepeatedly(ActionGapRegisterCallbacks);
50 EXPECT_CALL(*this, BleGattRegisterCallbacks).WillRepeatedly(ActionBleGattRegisterCallbacks);
51 }
52
~MockBluetooth()53 MockBluetooth::~MockBluetooth()
54 {
55 MockBluetooth::targetMocker = nullptr;
56 }
57
EnableBle(void)58 bool EnableBle(void)
59 {
60 return MockBluetooth::GetMocker()->EnableBle();
61 }
62
DisableBle(void)63 bool DisableBle(void)
64 {
65 return MockBluetooth::GetMocker()->DisableBle();
66 }
67
IsBleEnabled()68 bool IsBleEnabled()
69 {
70 return MockBluetooth::GetMocker()->IsBleEnabled();
71 }
72
GetLocalAddr(unsigned char * mac,unsigned int len)73 bool GetLocalAddr(unsigned char *mac, unsigned int len)
74 {
75 return MockBluetooth::GetMocker()->GetLocalAddr(mac, len);
76 }
77
SetLocalName(unsigned char * localName,unsigned char length)78 bool SetLocalName(unsigned char *localName, unsigned char length)
79 {
80 return MockBluetooth::GetMocker()->SetLocalName(localName, length);
81 }
82
GapRegisterCallbacks(BtGapCallBacks * func)83 int32_t GapRegisterCallbacks(BtGapCallBacks *func)
84 {
85 return MockBluetooth::GetMocker()->GapRegisterCallbacks(func);
86 }
87
PairRequestReply(const BdAddr * bdAddr,int32_t transport,bool accept)88 bool PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept)
89 {
90 return MockBluetooth::GetMocker()->PairRequestReply(bdAddr, transport, accept);
91 }
92
SetDevicePairingConfirmation(const BdAddr * bdAddr,int32_t transport,bool accept)93 bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept)
94 {
95 return MockBluetooth::GetMocker()->SetDevicePairingConfirmation(bdAddr, transport, accept);
96 }
97
BleGattRegisterCallbacks(BtGattCallbacks * func)98 int32_t BleGattRegisterCallbacks(BtGattCallbacks *func)
99 {
100 return MockBluetooth::GetMocker()->BleGattRegisterCallbacks(func);
101 }
102
BleStartScanEx(int32_t scannerId,BleScanConfigs * configs,BleScanNativeFilter * filter,unsigned int filterSize)103 int32_t BleStartScanEx(int32_t scannerId, BleScanConfigs *configs, BleScanNativeFilter *filter, unsigned int filterSize)
104 {
105 return MockBluetooth::GetMocker()->BleStartScanEx(scannerId, configs, filter, filterSize);
106 }
107
BleStopScan(int32_t scannerId)108 int32_t BleStopScan(int32_t scannerId)
109 {
110 return MockBluetooth::GetMocker()->BleStopScan(scannerId);
111 }
112
BleStartAdvEx(int32_t * advId,const StartAdvRawData rawData,BleAdvParams advParam)113 int32_t BleStartAdvEx(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam)
114 {
115 return MockBluetooth::GetMocker()->BleStartAdvEx(advId, rawData, advParam);
116 }
117
BleStopAdv(int32_t advId)118 int32_t BleStopAdv(int32_t advId)
119 {
120 return MockBluetooth::GetMocker()->BleStopAdv(advId);
121 }
122
BleGattcRegister(BtUuid appUuid)123 int32_t BleGattcRegister(BtUuid appUuid)
124 {
125 return MockBluetooth::GetMocker()->BleGattcRegister(appUuid);
126 }
127
BleGattcConnect(int32_t clientId,BtGattClientCallbacks * func,const BdAddr * bdAddr,bool isAutoConnect,BtTransportType transport)128 int32_t BleGattcConnect(
129 int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)
130 {
131 return MockBluetooth::GetMocker()->BleGattcConnect(clientId, func, bdAddr, isAutoConnect, transport);
132 }
133
BleGattcDisconnect(int32_t clientId)134 int32_t BleGattcDisconnect(int32_t clientId)
135 {
136 return MockBluetooth::GetMocker()->BleGattcDisconnect(clientId);
137 }
138
BleGattcSearchServices(int32_t clientId)139 int32_t BleGattcSearchServices(int32_t clientId)
140 {
141 return MockBluetooth::GetMocker()->BleGattcSearchServices(clientId);
142 }
143
BleGattcGetService(int32_t clientId,BtUuid serviceUuid)144 bool BleGattcGetService(int32_t clientId, BtUuid serviceUuid)
145 {
146 return MockBluetooth::GetMocker()->BleGattcGetService(clientId, serviceUuid);
147 }
148
BleGattcRegisterNotification(int32_t clientId,BtGattCharacteristic characteristic,bool enable)149 int32_t BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable)
150 {
151 return MockBluetooth::GetMocker()->BleGattcRegisterNotification(clientId, characteristic, enable);
152 }
153
BleGattcConfigureMtuSize(int32_t clientId,int32_t mtuSize)154 int32_t BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize)
155 {
156 return MockBluetooth::GetMocker()->BleGattcConfigureMtuSize(clientId, mtuSize);
157 }
158
BleGattcWriteCharacteristic(int32_t clientId,BtGattCharacteristic characteristic,BtGattWriteType writeType,int32_t len,const char * value)159 int32_t BleGattcWriteCharacteristic(
160 int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value)
161 {
162 return MockBluetooth::GetMocker()->BleGattcWriteCharacteristic(clientId, characteristic, writeType, len, value);
163 }
164
BleGattcUnRegister(int32_t clientId)165 int32_t BleGattcUnRegister(int32_t clientId)
166 {
167 return MockBluetooth::GetMocker()->BleGattcUnRegister(clientId);
168 }
169
BleGattcSetFastestConn(int32_t clientId,bool fastestConnFlag)170 int32_t BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag)
171 {
172 return MockBluetooth::GetMocker()->BleGattcSetFastestConn(clientId, fastestConnFlag);
173 }
174
BleGattcSetPriority(int32_t clientId,const BdAddr * bdAddr,BtGattPriority priority)175 int32_t BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority)
176 {
177 return MockBluetooth::GetMocker()->BleGattcSetPriority(clientId, bdAddr, priority);
178 }
179
BleGattsRegisterCallbacks(BtGattServerCallbacks * func)180 int32_t BleGattsRegisterCallbacks(BtGattServerCallbacks *func)
181 {
182 return MockBluetooth::GetMocker()->BleGattsRegisterCallbacks(func);
183 }
184
BleGattsRegister(BtUuid appUuid)185 int32_t BleGattsRegister(BtUuid appUuid)
186 {
187 return MockBluetooth::GetMocker()->BleGattsRegister(appUuid);
188 }
189
BleGattsAddService(int32_t serverId,BtUuid srvcUuid,bool isPrimary,int32_t number)190 int32_t BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number)
191 {
192 return MockBluetooth::GetMocker()->BleGattsAddService(serverId, srvcUuid, isPrimary, number);
193 }
194
BleGattsUnRegister(int32_t serverId)195 int32_t BleGattsUnRegister(int32_t serverId)
196 {
197 return MockBluetooth::GetMocker()->BleGattsUnRegister(serverId);
198 }
199
BleGattsAddCharacteristic(int32_t serverId,int32_t srvcHandle,BtUuid characUuid,int32_t properties,int32_t permissions)200 int32_t BleGattsAddCharacteristic(
201 int32_t serverId, int32_t srvcHandle, BtUuid characUuid, int32_t properties, int32_t permissions)
202 {
203 return MockBluetooth::GetMocker()->BleGattsAddCharacteristic(
204 serverId, srvcHandle, characUuid, properties, permissions);
205 }
206
BleGattsAddDescriptor(int32_t serverId,int32_t srvcHandle,BtUuid descUuid,int32_t permissions)207 int32_t BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions)
208 {
209 return MockBluetooth::GetMocker()->BleGattsAddDescriptor(serverId, srvcHandle, descUuid, permissions);
210 }
211
BleGattsStartService(int32_t serverId,int32_t srvcHandle)212 int32_t BleGattsStartService(int32_t serverId, int32_t srvcHandle)
213 {
214 return MockBluetooth::GetMocker()->BleGattsStartService(serverId, srvcHandle);
215 }
216
BleGattsStopService(int32_t serverId,int32_t srvcHandle)217 int32_t BleGattsStopService(int32_t serverId, int32_t srvcHandle)
218 {
219 return MockBluetooth::GetMocker()->BleGattsStopService(serverId, srvcHandle);
220 }
221
BleGattsDeleteService(int32_t serverId,int32_t srvcHandle)222 int32_t BleGattsDeleteService(int32_t serverId, int32_t srvcHandle)
223 {
224 return MockBluetooth::GetMocker()->BleGattsDeleteService(serverId, srvcHandle);
225 }
226
BleGattsConnect(int32_t serverId,BdAddr bdAddr)227 int BleGattsConnect(int32_t serverId, BdAddr bdAddr)
228 {
229 return MockBluetooth::GetMocker()->BleGattsConnect(serverId, bdAddr);
230 }
231
BleGattsDisconnect(int32_t serverId,BdAddr bdAddr,int32_t connId)232 int32_t BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId)
233 {
234 return MockBluetooth::GetMocker()->BleGattsDisconnect(serverId, bdAddr, connId);
235 }
236
BleGattsSendResponse(int32_t serverId,GattsSendRspParam * param)237 int32_t BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param)
238 {
239 return MockBluetooth::GetMocker()->BleGattsSendResponse(serverId, param);
240 }
241
BleGattsSendIndication(int32_t serverId,GattsSendIndParam * param)242 int32_t BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param)
243 {
244 return MockBluetooth::GetMocker()->BleGattsSendIndication(serverId, param);
245 }
246