1 /* 2 * Copyright (c) 2024 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 #ifndef BLUETOOTH_BLE_IMPL_H 16 #define BLUETOOTH_BLE_IMPL_H 17 18 #include "bluetooth_ble_advertiser.h" 19 #include "bluetooth_ble_central_manager.h" 20 #include "bluetooth_ble_clientDevice.h" 21 #include "bluetooth_ble_ffi.h" 22 #include "bluetooth_ble_gattServer.h" 23 #include "bluetooth_host.h" 24 #include "napi_bluetooth_utils.h" 25 26 #include <cstdint> 27 28 namespace OHOS { 29 namespace CJSystemapi { 30 namespace CJBluetoothBle { 31 using Bluetooth::BleCentralManagerCallback; 32 using Bluetooth::BleScanResult; 33 using Bluetooth::ScanResult; 34 using Bluetooth::UUID; 35 36 using Bluetooth::BleAdvertiseCallback; 37 using Bluetooth::BleAdvertiserData; 38 using Bluetooth::BleAdvertiserSettings; 39 40 class FfiBluetoothBleCentralManagerCallback : public BleCentralManagerCallback { 41 public: 42 FfiBluetoothBleCentralManagerCallback(); 43 ~FfiBluetoothBleCentralManagerCallback() override = default; 44 45 static FfiBluetoothBleCentralManagerCallback &GetInstance(void); 46 47 void OnScanCallback(const BleScanResult &result) override; OnFoundOrLostCallback(const BleScanResult & result,uint8_t callbackType)48 void OnFoundOrLostCallback(const BleScanResult &result, uint8_t callbackType) override{}; OnBleBatchScanResultsEvent(const std::vector<BleScanResult> & results)49 void OnBleBatchScanResultsEvent(const std::vector<BleScanResult> &results) override{}; OnStartOrStopScanEvent(int resultCode,bool isStartScan)50 void OnStartOrStopScanEvent(int resultCode, bool isStartScan) override{}; OnNotifyMsgReportFromLpDevice(const UUID & uuid,int msgType,const std::vector<uint8_t> & value)51 void OnNotifyMsgReportFromLpDevice(const UUID &uuid, int msgType, const std::vector<uint8_t> &value) override{}; 52 53 void RegisterBLEDeviceFindFunc(std::function<void(CArrScanResult)> cjCallback); 54 55 private: 56 std::function<void(CArrScanResult)> bleDeviceFindFunc{nullptr}; 57 }; 58 59 class FfiBluetoothBleAdvertiseCallback : public BleAdvertiseCallback { 60 public: 61 FfiBluetoothBleAdvertiseCallback(); 62 ~FfiBluetoothBleAdvertiseCallback() override = default; 63 64 static std::shared_ptr<FfiBluetoothBleAdvertiseCallback> GetInstance(void); 65 66 void OnStartResultEvent(int result, int advHandle) override; 67 void OnEnableResultEvent(int result, int advHandle) override; 68 void OnDisableResultEvent(int result, int advHandle) override; 69 void OnStopResultEvent(int result, int advHandle) override; OnSetAdvDataEvent(int result)70 void OnSetAdvDataEvent(int result) override{}; 71 void OnGetAdvHandleEvent(int result, int advHandle) override; 72 void OnChangeAdvResultEvent(int result, int advHandle) override; 73 74 void RegisterAdvertisingStateChangeFunc(std::function<void(CAdvertisingStateChangeInfo)> cjCallback); 75 int32_t GetAdvHandleEvent(); 76 77 private: 78 int32_t handleEvent{-1}; 79 std::function<void(CAdvertisingStateChangeInfo)> advertisingStateChangeFunc{nullptr}; 80 }; 81 82 class BleImpl { 83 public: 84 BleImpl() = default; 85 ~BleImpl() = default; 86 87 static int32_t CreateGattServer(FfiGattServer *&ffiGattServer); 88 static int32_t CreateGattClientDevice(std::string deviceId, FfiClientDevice *&ffiClientDevice); 89 static int32_t GetConnectedBleDevices(CArrString &res); 90 static int32_t StartBleScan(CArrNativeScanFilter filters, NativeScanOptions *options); 91 static int32_t StopBleScan(); 92 static int32_t StartAdvertising(NativeAdvertiseSetting setting, NativeAdvertiseData advData, 93 NativeAdvertiseData *advResponse); 94 static int32_t StopAdvertising(); 95 static int32_t StartAdvertising(NativeAdvertisingParams advertisingParams, int32_t &id); 96 static int32_t EnableAdvertising(NativeAdvertisingEnableParams advertisingEnableParams); 97 static int32_t DisableAdvertising(NativeAdvertisingDisableParams advertisingDisableParams); 98 static int32_t StopAdvertising(uint32_t advertisingId); 99 static int32_t RegisterBleObserver(int32_t callbackType, void (*callback)()); 100 }; 101 } // namespace CJBluetoothBle 102 } // namespace CJSystemapi 103 } // namespace OHOS 104 105 #endif // BLUETOOTH_BLE_IMPL_H