1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3060ff233Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4060ff233Sopenharmony_ci * you may not use this file except in compliance with the License. 5060ff233Sopenharmony_ci * You may obtain a copy of the License at 6060ff233Sopenharmony_ci * 7060ff233Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8060ff233Sopenharmony_ci * 9060ff233Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10060ff233Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11060ff233Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12060ff233Sopenharmony_ci * See the License for the specific language governing permissions and 13060ff233Sopenharmony_ci * limitations under the License. 14060ff233Sopenharmony_ci */ 15060ff233Sopenharmony_ci 16060ff233Sopenharmony_ci#include "bluetooth_mock.h" 17060ff233Sopenharmony_ci 18060ff233Sopenharmony_ci#include <securec.h> 19060ff233Sopenharmony_ci 20060ff233Sopenharmony_ci#include "softbus_common.h" 21060ff233Sopenharmony_ci#include "softbus_errcode.h" 22060ff233Sopenharmony_ci#include "softbus_utils.h" 23060ff233Sopenharmony_ci 24060ff233Sopenharmony_ciMockBluetooth *MockBluetooth::targetMocker = nullptr; 25060ff233Sopenharmony_ciBtGapCallBacks *MockBluetooth::btGapCallback = nullptr; 26060ff233Sopenharmony_ciBtGattCallbacks *MockBluetooth::btGattCallback = nullptr; 27060ff233Sopenharmony_ciBleScanCallbacks *MockBluetooth::bleScanCallback = nullptr; 28060ff233Sopenharmony_ci 29060ff233Sopenharmony_cistatic int32_t ActionGapRegisterCallbacks(BtGapCallBacks *func) 30060ff233Sopenharmony_ci{ 31060ff233Sopenharmony_ci MockBluetooth::btGapCallback = func; 32060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 33060ff233Sopenharmony_ci} 34060ff233Sopenharmony_ci 35060ff233Sopenharmony_cistatic int32_t ActionBleGattRegisterCallbacks(BtGattCallbacks *func) 36060ff233Sopenharmony_ci{ 37060ff233Sopenharmony_ci MockBluetooth::btGattCallback = func; 38060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 39060ff233Sopenharmony_ci} 40060ff233Sopenharmony_ci 41060ff233Sopenharmony_ciMockBluetooth *MockBluetooth::GetMocker() 42060ff233Sopenharmony_ci{ 43060ff233Sopenharmony_ci return targetMocker; 44060ff233Sopenharmony_ci} 45060ff233Sopenharmony_ci 46060ff233Sopenharmony_ciMockBluetooth::MockBluetooth() 47060ff233Sopenharmony_ci{ 48060ff233Sopenharmony_ci MockBluetooth::targetMocker = this; 49060ff233Sopenharmony_ci // common callback is register glabal 50060ff233Sopenharmony_ci EXPECT_CALL(*this, GapRegisterCallbacks).WillRepeatedly(ActionGapRegisterCallbacks); 51060ff233Sopenharmony_ci EXPECT_CALL(*this, BleGattRegisterCallbacks).WillRepeatedly(ActionBleGattRegisterCallbacks); 52060ff233Sopenharmony_ci} 53060ff233Sopenharmony_ci 54060ff233Sopenharmony_ciMockBluetooth::~MockBluetooth() 55060ff233Sopenharmony_ci{ 56060ff233Sopenharmony_ci MockBluetooth::targetMocker = nullptr; 57060ff233Sopenharmony_ci} 58060ff233Sopenharmony_ci 59060ff233Sopenharmony_cibool EnableBle(void) 60060ff233Sopenharmony_ci{ 61060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->EnableBle(); 62060ff233Sopenharmony_ci} 63060ff233Sopenharmony_ci 64060ff233Sopenharmony_cibool DisableBle(void) 65060ff233Sopenharmony_ci{ 66060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->DisableBle(); 67060ff233Sopenharmony_ci} 68060ff233Sopenharmony_ci 69060ff233Sopenharmony_cibool IsBleEnabled() 70060ff233Sopenharmony_ci{ 71060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->IsBleEnabled(); 72060ff233Sopenharmony_ci} 73060ff233Sopenharmony_ci 74060ff233Sopenharmony_cibool GetLocalAddr(unsigned char *mac, unsigned int len) 75060ff233Sopenharmony_ci{ 76060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->GetLocalAddr(mac, len); 77060ff233Sopenharmony_ci} 78060ff233Sopenharmony_ci 79060ff233Sopenharmony_cibool SetLocalName(unsigned char *localName, unsigned char length) 80060ff233Sopenharmony_ci{ 81060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->SetLocalName(localName, length); 82060ff233Sopenharmony_ci} 83060ff233Sopenharmony_ci 84060ff233Sopenharmony_ciint32_t GapRegisterCallbacks(BtGapCallBacks *func) 85060ff233Sopenharmony_ci{ 86060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->GapRegisterCallbacks(func); 87060ff233Sopenharmony_ci} 88060ff233Sopenharmony_ci 89060ff233Sopenharmony_cibool PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept) 90060ff233Sopenharmony_ci{ 91060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->PairRequestReply(bdAddr, transport, accept); 92060ff233Sopenharmony_ci} 93060ff233Sopenharmony_ci 94060ff233Sopenharmony_cibool SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept) 95060ff233Sopenharmony_ci{ 96060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->SetDevicePairingConfirmation(bdAddr, transport, accept); 97060ff233Sopenharmony_ci} 98060ff233Sopenharmony_ci 99060ff233Sopenharmony_ciint32_t BleGattRegisterCallbacks(BtGattCallbacks *func) 100060ff233Sopenharmony_ci{ 101060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattRegisterCallbacks(func); 102060ff233Sopenharmony_ci} 103060ff233Sopenharmony_ci 104060ff233Sopenharmony_ciint32_t BleStartScanEx(int32_t scannerId, BleScanConfigs *configs, BleScanNativeFilter *filter, unsigned int filterSize) 105060ff233Sopenharmony_ci{ 106060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleStartScanEx(scannerId, configs, filter, filterSize); 107060ff233Sopenharmony_ci} 108060ff233Sopenharmony_ci 109060ff233Sopenharmony_ciint32_t BleStopScan(int32_t scannerId) 110060ff233Sopenharmony_ci{ 111060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleStopScan(scannerId); 112060ff233Sopenharmony_ci} 113060ff233Sopenharmony_ci 114060ff233Sopenharmony_ciint32_t BleStartAdvEx(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam) 115060ff233Sopenharmony_ci{ 116060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleStartAdvEx(advId, rawData, advParam); 117060ff233Sopenharmony_ci} 118060ff233Sopenharmony_ci 119060ff233Sopenharmony_ciint32_t BleStopAdv(int32_t advId) 120060ff233Sopenharmony_ci{ 121060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleStopAdv(advId); 122060ff233Sopenharmony_ci} 123060ff233Sopenharmony_ci 124060ff233Sopenharmony_ciint32_t BleGattcRegister(BtUuid appUuid) 125060ff233Sopenharmony_ci{ 126060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcRegister(appUuid); 127060ff233Sopenharmony_ci} 128060ff233Sopenharmony_ci 129060ff233Sopenharmony_ciint32_t BleGattcConnect( 130060ff233Sopenharmony_ci int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport) 131060ff233Sopenharmony_ci{ 132060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcConnect(clientId, func, bdAddr, isAutoConnect, transport); 133060ff233Sopenharmony_ci} 134060ff233Sopenharmony_ci 135060ff233Sopenharmony_ciint32_t BleGattcDisconnect(int32_t clientId) 136060ff233Sopenharmony_ci{ 137060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcDisconnect(clientId); 138060ff233Sopenharmony_ci} 139060ff233Sopenharmony_ci 140060ff233Sopenharmony_ciint32_t BleGattcSearchServices(int32_t clientId) 141060ff233Sopenharmony_ci{ 142060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcSearchServices(clientId); 143060ff233Sopenharmony_ci} 144060ff233Sopenharmony_ci 145060ff233Sopenharmony_cibool BleGattcGetService(int32_t clientId, BtUuid serviceUuid) 146060ff233Sopenharmony_ci{ 147060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcGetService(clientId, serviceUuid); 148060ff233Sopenharmony_ci} 149060ff233Sopenharmony_ci 150060ff233Sopenharmony_ciint32_t BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable) 151060ff233Sopenharmony_ci{ 152060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcRegisterNotification(clientId, characteristic, enable); 153060ff233Sopenharmony_ci} 154060ff233Sopenharmony_ci 155060ff233Sopenharmony_ciint32_t BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize) 156060ff233Sopenharmony_ci{ 157060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcConfigureMtuSize(clientId, mtuSize); 158060ff233Sopenharmony_ci} 159060ff233Sopenharmony_ci 160060ff233Sopenharmony_ciint32_t BleGattcWriteCharacteristic( 161060ff233Sopenharmony_ci int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value) 162060ff233Sopenharmony_ci{ 163060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcWriteCharacteristic(clientId, characteristic, writeType, len, value); 164060ff233Sopenharmony_ci} 165060ff233Sopenharmony_ci 166060ff233Sopenharmony_ciint32_t BleGattcUnRegister(int32_t clientId) 167060ff233Sopenharmony_ci{ 168060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcUnRegister(clientId); 169060ff233Sopenharmony_ci} 170060ff233Sopenharmony_ci 171060ff233Sopenharmony_ciint32_t BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag) 172060ff233Sopenharmony_ci{ 173060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcSetFastestConn(clientId, fastestConnFlag); 174060ff233Sopenharmony_ci} 175060ff233Sopenharmony_ci 176060ff233Sopenharmony_ciint32_t BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority) 177060ff233Sopenharmony_ci{ 178060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcSetPriority(clientId, bdAddr, priority); 179060ff233Sopenharmony_ci} 180060ff233Sopenharmony_ci 181060ff233Sopenharmony_ciint32_t BleGattsRegisterCallbacks(BtGattServerCallbacks *func) 182060ff233Sopenharmony_ci{ 183060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsRegisterCallbacks(func); 184060ff233Sopenharmony_ci} 185060ff233Sopenharmony_ci 186060ff233Sopenharmony_ciint32_t BleGattsRegister(BtUuid appUuid) 187060ff233Sopenharmony_ci{ 188060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsRegister(appUuid); 189060ff233Sopenharmony_ci} 190060ff233Sopenharmony_ci 191060ff233Sopenharmony_ciint32_t BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number) 192060ff233Sopenharmony_ci{ 193060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsAddService(serverId, srvcUuid, isPrimary, number); 194060ff233Sopenharmony_ci} 195060ff233Sopenharmony_ci 196060ff233Sopenharmony_ciint32_t BleGattsUnRegister(int32_t serverId) 197060ff233Sopenharmony_ci{ 198060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsUnRegister(serverId); 199060ff233Sopenharmony_ci} 200060ff233Sopenharmony_ci 201060ff233Sopenharmony_ciint32_t BleGattsAddCharacteristic(int32_t serverId, int32_t srvcHandle, 202060ff233Sopenharmony_ci BtUuid characUuid, int32_t properties, int32_t permissions) 203060ff233Sopenharmony_ci{ 204060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsAddCharacteristic( 205060ff233Sopenharmony_ci serverId, srvcHandle, characUuid, properties, permissions); 206060ff233Sopenharmony_ci} 207060ff233Sopenharmony_ci 208060ff233Sopenharmony_ciint32_t BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions) 209060ff233Sopenharmony_ci{ 210060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsAddDescriptor(serverId, srvcHandle, descUuid, permissions); 211060ff233Sopenharmony_ci} 212060ff233Sopenharmony_ci 213060ff233Sopenharmony_ciint32_t BleGattsStartService(int32_t serverId, int32_t srvcHandle) 214060ff233Sopenharmony_ci{ 215060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsStartService(serverId, srvcHandle); 216060ff233Sopenharmony_ci} 217060ff233Sopenharmony_ci 218060ff233Sopenharmony_ciint32_t BleGattsStopService(int32_t serverId, int32_t srvcHandle) 219060ff233Sopenharmony_ci{ 220060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsStopService(serverId, srvcHandle); 221060ff233Sopenharmony_ci} 222060ff233Sopenharmony_ci 223060ff233Sopenharmony_ciint32_t BleGattsDeleteService(int32_t serverId, int32_t srvcHandle) 224060ff233Sopenharmony_ci{ 225060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsDeleteService(serverId, srvcHandle); 226060ff233Sopenharmony_ci} 227060ff233Sopenharmony_ci 228060ff233Sopenharmony_ciint32_t BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId) 229060ff233Sopenharmony_ci{ 230060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsDisconnect(serverId, bdAddr, connId); 231060ff233Sopenharmony_ci} 232060ff233Sopenharmony_ci 233060ff233Sopenharmony_ciint32_t BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param) 234060ff233Sopenharmony_ci{ 235060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsSendResponse(serverId, param); 236060ff233Sopenharmony_ci} 237060ff233Sopenharmony_ci 238060ff233Sopenharmony_ciint32_t BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param) 239060ff233Sopenharmony_ci{ 240060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsSendIndication(serverId, param); 241060ff233Sopenharmony_ci} 242