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