1060ff233Sopenharmony_ci/* 2060ff233Sopenharmony_ci * Copyright (c) 2024 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#include <securec.h> 18060ff233Sopenharmony_ci#include "disc_log.h" 19060ff233Sopenharmony_ci 20060ff233Sopenharmony_ciMockBluetooth *MockBluetooth::targetMocker = nullptr; 21060ff233Sopenharmony_ciBtGapCallBacks *MockBluetooth::btGapCallback = nullptr; 22060ff233Sopenharmony_ciBtGattCallbacks *MockBluetooth::btGattCallback = nullptr; 23060ff233Sopenharmony_ciBleScanCallbacks *MockBluetooth::bleScanCallback = nullptr; 24060ff233Sopenharmony_ciconst SoftbusBroadcastMediumInterface *MockBluetooth::interface = nullptr; 25060ff233Sopenharmony_ci 26060ff233Sopenharmony_cistatic int32_t ActionGapRegisterCallbacks(BtGapCallBacks *func) 27060ff233Sopenharmony_ci{ 28060ff233Sopenharmony_ci MockBluetooth::btGapCallback = func; 29060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 30060ff233Sopenharmony_ci} 31060ff233Sopenharmony_ci 32060ff233Sopenharmony_cistatic int32_t ActionBleGattRegisterCallbacks(BtGattCallbacks *func) 33060ff233Sopenharmony_ci{ 34060ff233Sopenharmony_ci MockBluetooth::btGattCallback = func; 35060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 36060ff233Sopenharmony_ci} 37060ff233Sopenharmony_ci 38060ff233Sopenharmony_cistatic int32_t ActionBleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId) 39060ff233Sopenharmony_ci{ 40060ff233Sopenharmony_ci MockBluetooth::bleScanCallback = func; 41060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 42060ff233Sopenharmony_ci} 43060ff233Sopenharmony_ci 44060ff233Sopenharmony_cistatic int32_t ActionBleDeregisterScanCallbacks(int32_t scannerId) 45060ff233Sopenharmony_ci{ 46060ff233Sopenharmony_ci MockBluetooth::bleScanCallback = nullptr; 47060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 48060ff233Sopenharmony_ci} 49060ff233Sopenharmony_ci 50060ff233Sopenharmony_cistatic int32_t ActionRegisterBroadcastMediumFunction(SoftbusMediumType type, 51060ff233Sopenharmony_ci const SoftbusBroadcastMediumInterface *func) 52060ff233Sopenharmony_ci{ 53060ff233Sopenharmony_ci MockBluetooth::interface = func; 54060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 55060ff233Sopenharmony_ci} 56060ff233Sopenharmony_ci 57060ff233Sopenharmony_ciMockBluetooth *MockBluetooth::GetMocker() 58060ff233Sopenharmony_ci{ 59060ff233Sopenharmony_ci return targetMocker; 60060ff233Sopenharmony_ci} 61060ff233Sopenharmony_ci 62060ff233Sopenharmony_ciMockBluetooth::MockBluetooth() 63060ff233Sopenharmony_ci{ 64060ff233Sopenharmony_ci MockBluetooth::targetMocker = this; 65060ff233Sopenharmony_ci // common callback is register global. Need to pay attention to the calling timing. 66060ff233Sopenharmony_ci EXPECT_CALL(*this, GapRegisterCallbacks).WillRepeatedly(ActionGapRegisterCallbacks); 67060ff233Sopenharmony_ci EXPECT_CALL(*this, BleGattRegisterCallbacks).WillRepeatedly(ActionBleGattRegisterCallbacks); 68060ff233Sopenharmony_ci EXPECT_CALL(*this, BleRegisterScanCallbacks).WillRepeatedly(ActionBleRegisterScanCallbacks); 69060ff233Sopenharmony_ci EXPECT_CALL(*this, BleDeregisterScanCallbacks).WillRepeatedly(ActionBleDeregisterScanCallbacks); 70060ff233Sopenharmony_ci EXPECT_CALL(*this, RegisterBroadcastMediumFunction).WillRepeatedly(ActionRegisterBroadcastMediumFunction); 71060ff233Sopenharmony_ci} 72060ff233Sopenharmony_ci 73060ff233Sopenharmony_ciMockBluetooth::~MockBluetooth() 74060ff233Sopenharmony_ci{ 75060ff233Sopenharmony_ci MockBluetooth::targetMocker = nullptr; 76060ff233Sopenharmony_ci} 77060ff233Sopenharmony_ci 78060ff233Sopenharmony_cibool EnableBle(void) 79060ff233Sopenharmony_ci{ 80060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->EnableBle(); 81060ff233Sopenharmony_ci} 82060ff233Sopenharmony_ci 83060ff233Sopenharmony_cibool DisableBle(void) 84060ff233Sopenharmony_ci{ 85060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->DisableBle(); 86060ff233Sopenharmony_ci} 87060ff233Sopenharmony_ci 88060ff233Sopenharmony_cibool IsBleEnabled() 89060ff233Sopenharmony_ci{ 90060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->IsBleEnabled(); 91060ff233Sopenharmony_ci} 92060ff233Sopenharmony_ci 93060ff233Sopenharmony_cibool GetLocalAddr(unsigned char *mac, unsigned int len) 94060ff233Sopenharmony_ci{ 95060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->GetLocalAddr(mac, len); 96060ff233Sopenharmony_ci} 97060ff233Sopenharmony_ci 98060ff233Sopenharmony_cibool SetLocalName(unsigned char *localName, unsigned char length) 99060ff233Sopenharmony_ci{ 100060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->SetLocalName(localName, length); 101060ff233Sopenharmony_ci} 102060ff233Sopenharmony_ci 103060ff233Sopenharmony_ciint32_t GapRegisterCallbacks(BtGapCallBacks *func) 104060ff233Sopenharmony_ci{ 105060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->GapRegisterCallbacks(func); 106060ff233Sopenharmony_ci} 107060ff233Sopenharmony_ci 108060ff233Sopenharmony_cibool PairRequestReply(const BdAddr *bdAddr, int32_t transport, bool accept) 109060ff233Sopenharmony_ci{ 110060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->PairRequestReply(bdAddr, transport, accept); 111060ff233Sopenharmony_ci} 112060ff233Sopenharmony_ci 113060ff233Sopenharmony_cibool SetDevicePairingConfirmation(const BdAddr *bdAddr, int32_t transport, bool accept) 114060ff233Sopenharmony_ci{ 115060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->SetDevicePairingConfirmation(bdAddr, transport, accept); 116060ff233Sopenharmony_ci} 117060ff233Sopenharmony_ci 118060ff233Sopenharmony_ciint32_t BleGattRegisterCallbacks(BtGattCallbacks *func) 119060ff233Sopenharmony_ci{ 120060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattRegisterCallbacks(func); 121060ff233Sopenharmony_ci} 122060ff233Sopenharmony_ci 123060ff233Sopenharmony_ciint32_t BleRegisterScanCallbacks(BleScanCallbacks *func, int32_t *scannerId) 124060ff233Sopenharmony_ci{ 125060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleRegisterScanCallbacks(func, scannerId); 126060ff233Sopenharmony_ci} 127060ff233Sopenharmony_ci 128060ff233Sopenharmony_ciint32_t BleDeregisterScanCallbacks(int32_t scannerId) 129060ff233Sopenharmony_ci{ 130060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleDeregisterScanCallbacks(scannerId); 131060ff233Sopenharmony_ci} 132060ff233Sopenharmony_ci 133060ff233Sopenharmony_ciint32_t BleStartScanEx(int32_t scannerId, const BleScanConfigs *configs, const BleScanNativeFilter *filter, 134060ff233Sopenharmony_ci uint32_t filterSize) 135060ff233Sopenharmony_ci{ 136060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleStartScanEx(scannerId, configs, filter, filterSize); 137060ff233Sopenharmony_ci} 138060ff233Sopenharmony_ci 139060ff233Sopenharmony_ciint32_t BleStopScan(int32_t scannerId) 140060ff233Sopenharmony_ci{ 141060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleStopScan(scannerId); 142060ff233Sopenharmony_ci} 143060ff233Sopenharmony_ci 144060ff233Sopenharmony_ciint32_t GetAdvHandle(int32_t btAdvId, int32_t *bcHandle) 145060ff233Sopenharmony_ci{ 146060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 147060ff233Sopenharmony_ci} 148060ff233Sopenharmony_ci 149060ff233Sopenharmony_ciint32_t EnableSyncDataToLpDevice() 150060ff233Sopenharmony_ci{ 151060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 152060ff233Sopenharmony_ci} 153060ff233Sopenharmony_ci 154060ff233Sopenharmony_ciint32_t DisableSyncDataToLpDevice() 155060ff233Sopenharmony_ci{ 156060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 157060ff233Sopenharmony_ci} 158060ff233Sopenharmony_ci 159060ff233Sopenharmony_ciint32_t SetLpDeviceAdvParam(int32_t duration, int32_t maxExtAdvEvents, int32_t window, 160060ff233Sopenharmony_ci int32_t interval, int32_t bcHandle) 161060ff233Sopenharmony_ci{ 162060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 163060ff233Sopenharmony_ci} 164060ff233Sopenharmony_ci 165060ff233Sopenharmony_ciint32_t BleStartAdvEx(int32_t *advId, const StartAdvRawData rawData, BleAdvParams advParam) 166060ff233Sopenharmony_ci{ 167060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleStartAdvEx(advId, rawData, advParam); 168060ff233Sopenharmony_ci} 169060ff233Sopenharmony_ci 170060ff233Sopenharmony_ciint32_t BleStopAdv(int32_t advId) 171060ff233Sopenharmony_ci{ 172060ff233Sopenharmony_ci return OHOS_BT_STATUS_SUCCESS; 173060ff233Sopenharmony_ci} 174060ff233Sopenharmony_ci 175060ff233Sopenharmony_ciint32_t BleGattcRegister(BtUuid appUuid) 176060ff233Sopenharmony_ci{ 177060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcRegister(appUuid); 178060ff233Sopenharmony_ci} 179060ff233Sopenharmony_ci 180060ff233Sopenharmony_ciint32_t BleGattcConnect( 181060ff233Sopenharmony_ci int32_t clientId, BtGattClientCallbacks *func, const BdAddr *bdAddr, bool isAutoConnect, BtTransportType transport) 182060ff233Sopenharmony_ci{ 183060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcConnect(clientId, func, bdAddr, isAutoConnect, transport); 184060ff233Sopenharmony_ci} 185060ff233Sopenharmony_ci 186060ff233Sopenharmony_ciint32_t BleGattcDisconnect(int32_t clientId) 187060ff233Sopenharmony_ci{ 188060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcDisconnect(clientId); 189060ff233Sopenharmony_ci} 190060ff233Sopenharmony_ci 191060ff233Sopenharmony_ciint32_t BleGattcSearchServices(int32_t clientId) 192060ff233Sopenharmony_ci{ 193060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcSearchServices(clientId); 194060ff233Sopenharmony_ci} 195060ff233Sopenharmony_ci 196060ff233Sopenharmony_cibool BleGattcGetService(int32_t clientId, BtUuid serviceUuid) 197060ff233Sopenharmony_ci{ 198060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcGetService(clientId, serviceUuid); 199060ff233Sopenharmony_ci} 200060ff233Sopenharmony_ci 201060ff233Sopenharmony_ciint32_t BleGattcRegisterNotification(int32_t clientId, BtGattCharacteristic characteristic, bool enable) 202060ff233Sopenharmony_ci{ 203060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcRegisterNotification(clientId, characteristic, enable); 204060ff233Sopenharmony_ci} 205060ff233Sopenharmony_ci 206060ff233Sopenharmony_ciint32_t BleGattcConfigureMtuSize(int32_t clientId, int32_t mtuSize) 207060ff233Sopenharmony_ci{ 208060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcConfigureMtuSize(clientId, mtuSize); 209060ff233Sopenharmony_ci} 210060ff233Sopenharmony_ci 211060ff233Sopenharmony_ciint32_t BleGattcWriteCharacteristic( 212060ff233Sopenharmony_ci int32_t clientId, BtGattCharacteristic characteristic, BtGattWriteType writeType, int32_t len, const char *value) 213060ff233Sopenharmony_ci{ 214060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcWriteCharacteristic(clientId, characteristic, writeType, len, value); 215060ff233Sopenharmony_ci} 216060ff233Sopenharmony_ci 217060ff233Sopenharmony_ciint32_t BleGattcUnRegister(int32_t clientId) 218060ff233Sopenharmony_ci{ 219060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcUnRegister(clientId); 220060ff233Sopenharmony_ci} 221060ff233Sopenharmony_ci 222060ff233Sopenharmony_ciint32_t BleGattcSetFastestConn(int32_t clientId, bool fastestConnFlag) 223060ff233Sopenharmony_ci{ 224060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcSetFastestConn(clientId, fastestConnFlag); 225060ff233Sopenharmony_ci} 226060ff233Sopenharmony_ci 227060ff233Sopenharmony_ciint32_t BleGattcSetPriority(int32_t clientId, const BdAddr *bdAddr, BtGattPriority priority) 228060ff233Sopenharmony_ci{ 229060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattcSetPriority(clientId, bdAddr, priority); 230060ff233Sopenharmony_ci} 231060ff233Sopenharmony_ci 232060ff233Sopenharmony_ciint32_t BleGattsRegisterCallbacks(BtGattServerCallbacks *func) 233060ff233Sopenharmony_ci{ 234060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsRegisterCallbacks(func); 235060ff233Sopenharmony_ci} 236060ff233Sopenharmony_ci 237060ff233Sopenharmony_ciint32_t BleGattsRegister(BtUuid appUuid) 238060ff233Sopenharmony_ci{ 239060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsRegister(appUuid); 240060ff233Sopenharmony_ci} 241060ff233Sopenharmony_ci 242060ff233Sopenharmony_ciint32_t BleGattsAddService(int32_t serverId, BtUuid srvcUuid, bool isPrimary, int32_t number) 243060ff233Sopenharmony_ci{ 244060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsAddService(serverId, srvcUuid, isPrimary, number); 245060ff233Sopenharmony_ci} 246060ff233Sopenharmony_ci 247060ff233Sopenharmony_ciint32_t BleGattsUnRegister(int32_t serverId) 248060ff233Sopenharmony_ci{ 249060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsUnRegister(serverId); 250060ff233Sopenharmony_ci} 251060ff233Sopenharmony_ci 252060ff233Sopenharmony_ciint32_t BleGattsAddCharacteristic(int32_t serverId, int32_t srvcHandle, 253060ff233Sopenharmony_ci BtUuid characUuid, int32_t properties, int32_t permissions) 254060ff233Sopenharmony_ci{ 255060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsAddCharacteristic( 256060ff233Sopenharmony_ci serverId, srvcHandle, characUuid, properties, permissions); 257060ff233Sopenharmony_ci} 258060ff233Sopenharmony_ci 259060ff233Sopenharmony_ciint32_t BleGattsAddDescriptor(int32_t serverId, int32_t srvcHandle, BtUuid descUuid, int32_t permissions) 260060ff233Sopenharmony_ci{ 261060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsAddDescriptor(serverId, srvcHandle, descUuid, permissions); 262060ff233Sopenharmony_ci} 263060ff233Sopenharmony_ci 264060ff233Sopenharmony_ciint32_t BleGattsStartService(int32_t serverId, int32_t srvcHandle) 265060ff233Sopenharmony_ci{ 266060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsStartService(serverId, srvcHandle); 267060ff233Sopenharmony_ci} 268060ff233Sopenharmony_ci 269060ff233Sopenharmony_ciint32_t BleGattsStopService(int32_t serverId, int32_t srvcHandle) 270060ff233Sopenharmony_ci{ 271060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsStopService(serverId, srvcHandle); 272060ff233Sopenharmony_ci} 273060ff233Sopenharmony_ci 274060ff233Sopenharmony_ciint32_t BleGattsDeleteService(int32_t serverId, int32_t srvcHandle) 275060ff233Sopenharmony_ci{ 276060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsDeleteService(serverId, srvcHandle); 277060ff233Sopenharmony_ci} 278060ff233Sopenharmony_ci 279060ff233Sopenharmony_ciint32_t BleGattsDisconnect(int32_t serverId, BdAddr bdAddr, int32_t connId) 280060ff233Sopenharmony_ci{ 281060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsDisconnect(serverId, bdAddr, connId); 282060ff233Sopenharmony_ci} 283060ff233Sopenharmony_ci 284060ff233Sopenharmony_ciint32_t BleGattsSendResponse(int32_t serverId, GattsSendRspParam *param) 285060ff233Sopenharmony_ci{ 286060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsSendResponse(serverId, param); 287060ff233Sopenharmony_ci} 288060ff233Sopenharmony_ci 289060ff233Sopenharmony_ciint32_t BleGattsSendIndication(int32_t serverId, GattsSendIndParam *param) 290060ff233Sopenharmony_ci{ 291060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->BleGattsSendIndication(serverId, param); 292060ff233Sopenharmony_ci} 293060ff233Sopenharmony_ci 294060ff233Sopenharmony_ciint32_t SoftBusAddBtStateListener(const SoftBusBtStateListener *listener) 295060ff233Sopenharmony_ci{ 296060ff233Sopenharmony_ci return MockBluetooth::GetMocker()->SoftBusAddBtStateListener(listener); 297060ff233Sopenharmony_ci} 298060ff233Sopenharmony_ci 299060ff233Sopenharmony_ciint32_t RegisterBroadcastMediumFunction(SoftbusMediumType type, const SoftbusBroadcastMediumInterface *interface) 300060ff233Sopenharmony_ci{ 301060ff233Sopenharmony_ci DISC_LOGI(DISC_TEST, "begin to register func"); 302060ff233Sopenharmony_ci int32_t ret = MockBluetooth::GetMocker()->RegisterBroadcastMediumFunction(type, interface); 303060ff233Sopenharmony_ci DISC_LOGI(DISC_TEST, "end to register func"); 304060ff233Sopenharmony_ci return ret; 305060ff233Sopenharmony_ci} 306