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 int32_t ActionGapRegisterCallbacks(BtGapCallBacks *func)
30 {
31     MockBluetooth::btGapCallback = func;
32     return OHOS_BT_STATUS_SUCCESS;
33 }
34 
ActionBleGattRegisterCallbacks(BtGattCallbacks *func)35 static int32_t 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 int32_t GapRegisterCallbacks(BtGapCallBacks *func)
85 {
86     return MockBluetooth::GetMocker()->GapRegisterCallbacks(func);
87 }
88 
PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept)89 bool PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept)
90 {
91     return MockBluetooth::GetMocker()->PairRequestReply(bdAddr, transport, accept);
92 }
93 
SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept)94 bool SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept)
95 {
96     return MockBluetooth::GetMocker()->SetDevicePairingConfirmation(bdAddr, transport, accept);
97 }
98 
BleGattRegisterCallbacks(BtGattCallbacks *func)99 int32_t BleGattRegisterCallbacks(BtGattCallbacks *func)
100 {
101     return MockBluetooth::GetMocker()->BleGattRegisterCallbacks(func);
102 }
103 
BleStartScanEx(int32_t scannerId, BleScanConfigs *configs, BleScanNativeFilter *filter, unsigned int filterSize)104 int32_t BleStartScanEx(int32_t scannerId, BleScanConfigs *configs, BleScanNativeFilter *filter, unsigned int filterSize)
105 {
106     return MockBluetooth::GetMocker()->BleStartScanEx(scannerId, configs, filter, filterSize);
107 }
108 
BleStopScan(int32_t scannerId)109 int32_t BleStopScan(int32_t scannerId)
110 {
111     return MockBluetooth::GetMocker()->BleStopScan(scannerId);
112 }
113 
BleStartAdvEx(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam)114 int32_t BleStartAdvEx(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam)
115 {
116     return MockBluetooth::GetMocker()->BleStartAdvEx(advId, rawData, advParam);
117 }
118 
BleStopAdv(int32_t advId)119 int32_t BleStopAdv(int32_t advId)
120 {
121     return MockBluetooth::GetMocker()->BleStopAdv(advId);
122 }
123 
BleGattcRegister(BtUuid appUuid)124 int32_t BleGattcRegister(BtUuid appUuid)
125 {
126     return MockBluetooth::GetMocker()->BleGattcRegister(appUuid);
127 }
128 
BleGattcConnect( int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)129 int32_t BleGattcConnect(
130     int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport)
131 {
132     return MockBluetooth::GetMocker()->BleGattcConnect(clientId, func, bdAddr, isAutoConnect, transport);
133 }
134 
BleGattcDisconnect(int32_t clientId)135 int32_t BleGattcDisconnect(int32_t clientId)
136 {
137     return MockBluetooth::GetMocker()->BleGattcDisconnect(clientId);
138 }
139 
BleGattcSearchServices(int32_t clientId)140 int32_t BleGattcSearchServices(int32_t clientId)
141 {
142     return MockBluetooth::GetMocker()->BleGattcSearchServices(clientId);
143 }
144 
BleGattcGetService(int32_t clientId, BtUuid serviceUuid)145 bool BleGattcGetService(int32_t clientId, BtUuid serviceUuid)
146 {
147     return MockBluetooth::GetMocker()->BleGattcGetService(clientId, serviceUuid);
148 }
149 
BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable)150 int32_t BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable)
151 {
152     return MockBluetooth::GetMocker()->BleGattcRegisterNotification(clientId, characteristic, enable);
153 }
154 
BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize)155 int32_t BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize)
156 {
157     return MockBluetooth::GetMocker()->BleGattcConfigureMtuSize(clientId, mtuSize);
158 }
159 
BleGattcWriteCharacteristic( int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value)160 int32_t BleGattcWriteCharacteristic(
161     int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value)
162 {
163     return MockBluetooth::GetMocker()->BleGattcWriteCharacteristic(clientId, characteristic, writeType, len, value);
164 }
165 
BleGattcUnRegister(int32_t clientId)166 int32_t BleGattcUnRegister(int32_t clientId)
167 {
168     return MockBluetooth::GetMocker()->BleGattcUnRegister(clientId);
169 }
170 
BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag)171 int32_t BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag)
172 {
173     return MockBluetooth::GetMocker()->BleGattcSetFastestConn(clientId, fastestConnFlag);
174 }
175 
BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority)176 int32_t BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority)
177 {
178     return MockBluetooth::GetMocker()->BleGattcSetPriority(clientId, bdAddr, priority);
179 }
180 
BleGattsRegisterCallbacks(BtGattServerCallbacks *func)181 int32_t BleGattsRegisterCallbacks(BtGattServerCallbacks *func)
182 {
183     return MockBluetooth::GetMocker()->BleGattsRegisterCallbacks(func);
184 }
185 
BleGattsRegister(BtUuid appUuid)186 int32_t BleGattsRegister(BtUuid appUuid)
187 {
188     return MockBluetooth::GetMocker()->BleGattsRegister(appUuid);
189 }
190 
BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number)191 int32_t BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number)
192 {
193     return MockBluetooth::GetMocker()->BleGattsAddService(serverId, srvcUuid, isPrimary, number);
194 }
195 
BleGattsUnRegister(int32_t serverId)196 int32_t BleGattsUnRegister(int32_t serverId)
197 {
198     return MockBluetooth::GetMocker()->BleGattsUnRegister(serverId);
199 }
200 
BleGattsAddCharacteristic(int32_t serverId, int32_t srvcHandle, BtUuid characUuid, int32_t properties, int32_t permissions)201 int32_t BleGattsAddCharacteristic(int32_t serverId, int32_t srvcHandle,
202                                   BtUuid characUuid, int32_t properties, int32_t permissions)
203 {
204     return MockBluetooth::GetMocker()->BleGattsAddCharacteristic(
205         serverId, srvcHandle, characUuid, properties, permissions);
206 }
207 
BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions)208 int32_t BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions)
209 {
210     return MockBluetooth::GetMocker()->BleGattsAddDescriptor(serverId, srvcHandle, descUuid, permissions);
211 }
212 
BleGattsStartService(int32_t serverId, int32_t srvcHandle)213 int32_t BleGattsStartService(int32_t serverId, int32_t srvcHandle)
214 {
215     return MockBluetooth::GetMocker()->BleGattsStartService(serverId, srvcHandle);
216 }
217 
BleGattsStopService(int32_t serverId, int32_t srvcHandle)218 int32_t BleGattsStopService(int32_t serverId, int32_t srvcHandle)
219 {
220     return MockBluetooth::GetMocker()->BleGattsStopService(serverId, srvcHandle);
221 }
222 
BleGattsDeleteService(int32_t serverId, int32_t srvcHandle)223 int32_t BleGattsDeleteService(int32_t serverId, int32_t srvcHandle)
224 {
225     return MockBluetooth::GetMocker()->BleGattsDeleteService(serverId, srvcHandle);
226 }
227 
BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId)228 int32_t BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId)
229 {
230     return MockBluetooth::GetMocker()->BleGattsDisconnect(serverId, bdAddr, connId);
231 }
232 
BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param)233 int32_t BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param)
234 {
235     return MockBluetooth::GetMocker()->BleGattsSendResponse(serverId, param);
236 }
237 
BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param)238 int32_t BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param)
239 {
240     return MockBluetooth::GetMocker()->BleGattsSendIndication(serverId, param);
241 }
242