diff --git a/frameworks/ets/taihe/bluetooth_ble/BUILD.gn b/frameworks/ets/taihe/bluetooth_ble/BUILD.gn index c6805532471beed935c12e9518eeff2f03c603c5..ad6205ec896ea74ef6e89dd88deb73a697763669 100644 --- a/frameworks/ets/taihe/bluetooth_ble/BUILD.gn +++ b/frameworks/ets/taihe/bluetooth_ble/BUILD.gn @@ -66,6 +66,7 @@ taihe_shared_library("bluetoothBle_taihe_native") { ] external_deps = [ "hilog:libhilog", + "c_utils:utils", ] sanitize = { cfi = true # Enable/disable control flow integrity detection diff --git a/frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe b/frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe index e97a229de5a80de9712b1c6af6d0b6948bc2448f..0272b857d78c878d0ac3237bd69ceb6f5c871f25 100644 --- a/frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe +++ b/frameworks/ets/taihe/bluetooth_ble/idl/ohos.bluetooth.ble.taihe @@ -19,13 +19,35 @@ static { loadLibrary("bluetoothBle_taihe_native.z") } """) from ohos.bluetooth.constant use ProfileConnectionState; +@!sts_inject(""" +export function on(type: string, cb: (arg_0: Object) => void) { + switch (type) { + case "advertisingStateChange": return onAdvertisingStateChange(cb); + case "BLEDeviceFind": return onBLEDeviceFind(cb); + default: throw new Error(`Unknown type: ${type}`); + } +} +export function off(type: string, cb?: (arg_0: Object) => void) { + switch (type) { + case "advertisingStateChange": return offAdvertisingStateChange(cb as (((arg_0: AdvertisingStateChangeInfo) => void) | undefined)); + case "BLEDeviceFind": return offBLEDeviceFind(cb as (((arg_0: Array) => void) | undefined)); + default: throw new Error(`Unknown type: ${type}`); + } +} +""") +function OnAdvertisingStateChange(callback: (data: AdvertisingStateChangeInfo) => void): void; +function OffAdvertisingStateChange(callback: Optional<(data: AdvertisingStateChangeInfo) => void>): void; +function OnBLEDeviceFind(callback: (data: Array) => void): void; +function OffBLEDeviceFind(callback: Optional<(data: Array) => void>): void; + struct ScanReport { reportType: ScanReportType; scanResult: Array; } enum ScanReportType: i32 { ON_FOUND = 1, - ON_LOST = 2 + ON_LOST = 2, + ON_BATCH = 3 } struct ScanResult { deviceId: String; @@ -198,12 +220,74 @@ interface GattClientDevice { Disconnect(): void; Close(): void; + + @!sts_inject_into_class(""" + on(type: string, cb: Object) { + switch (type) { + case "BLECharacteristicChange": return this.onBLECharacteristicChange(cb as (info: BLECharacteristic) => void); + case "BLEMtuChange": return this.onBLEMtuChange(cb as (info: int) => void); + case "BLEConnectionStateChange": return this.onBLEConnectionStateChange(cb as (info: BLEConnectionChangeState) => void); + default: throw new Error(`Unknown type: ${type}`); + } + } + off(type: string, cb?: Object) { + switch (type) { + case "BLECharacteristicChange": return this.offBLECharacteristicChange(cb as (((arg_0: BLECharacteristic) => void) | undefined)); + case "BLEMtuChange": return this.offBLEMtuChange(cb as (((arg_0: int) => void) | undefined)); + case "BLEConnectionStateChange": return this.offBLEConnectionStateChange(cb as (((arg_0: BLEConnectionChangeState) => void) | undefined)); + default: throw new Error(`Unknown type: ${type}`); + } + } + """) + OnBLECharacteristicChange(callback: (info: BLECharacteristic) => void): void; + OffBLECharacteristicChange(callback: Optional<(info: BLECharacteristic) => void>): void; + OnBLEMtuChange(callback: (info: i32) => void): void; + OffBLEMtuChange(callback: Optional<(info: i32) => void>): void; + OnBLEConnectionStateChange(callback: (info: BLEConnectionChangeState) => void): void; + OffBLEConnectionStateChange(callback: Optional<(info: BLEConnectionChangeState) => void>): void; } function CreateGattClientDevice(deviceId: String): GattClientDevice; interface GattServer { Close(): void; + + @!sts_inject_into_class(""" + on(type: string, cb: Object) { + switch (type) { + case "characteristicWrite": return this.onCharacteristicWrite(cb as (info: CharacteristicWriteRequest) => void); + case "BLEMtuChange": return this.onBLEMtuChange(cb as (info: int) => void); + case "characteristicRead": return this.onCharacteristicRead(cb as (info: CharacteristicReadRequest) => void); + case "descriptorRead": return this.onDescriptorRead(cb as (info: DescriptorReadRequest) => void); + case "descriptorWrite": return this.onDescriptorWrite(cb as (info: DescriptorWriteRequest) => void); + case "connectionStateChange": return this.onConnectionStateChange(cb as (info: BLEConnectionChangeState) => void); + default: throw new Error(`Unknown type: ${type}`); + } + } + off(type: string, cb?: Object) { + switch (type) { + case "characteristicWrite": return this.offCharacteristicWrite(cb as (((arg_0: CharacteristicWriteRequest) => void) | undefined)); + case "BLEMtuChange": return this.offBLEMtuChange(cb as (((arg_0: int) => void) | undefined)); + case "characteristicRead": return this.offCharacteristicRead(cb as (((arg_0: CharacteristicReadRequest) => void) | undefined)); + case "descriptorRead": return this.offDescriptorRead(cb as (((arg_0: DescriptorReadRequest) => void) | undefined)); + case "descriptorWrite": return this.offDescriptorWrite(cb as (((arg_0: DescriptorWriteRequest) => void) | undefined)); + case "connectionStateChange": return this.offConnectionStateChange(cb as (((arg_0: BLEConnectionChangeState) => void) | undefined)); + default: throw new Error(`Unknown type: ${type}`); + } + } + """) + OnCharacteristicWrite(callback: (info: CharacteristicWriteRequest) => void): void; + OffCharacteristicWrite(callback: Optional<(info: CharacteristicWriteRequest) => void>): void; + OnBLEMtuChange(callback: (info: i32) => void): void; + OffBLEMtuChange(callback: Optional<(info: i32) => void>): void; + OnCharacteristicRead(callback: (info: CharacteristicReadRequest) => void): void; + OffCharacteristicRead(callback: Optional<(info: CharacteristicReadRequest) => void>): void; + OnDescriptorRead(callback: (info: DescriptorReadRequest) => void): void; + OffDescriptorRead(callback: Optional<(info: DescriptorReadRequest) => void>): void; + OnDescriptorWrite(callback: (info: DescriptorWriteRequest) => void): void; + OffDescriptorWrite(callback: Optional<(info: DescriptorWriteRequest) => void>): void; + OnConnectionStateChange(callback: (info: BLEConnectionChangeState) => void): void; + OffConnectionStateChange(callback: Optional<(info: BLEConnectionChangeState) => void>): void; } function CreateGattServer(): GattServer; @@ -245,6 +329,22 @@ struct BLEConnectionChangeState { } interface BleScanner { + @!sts_inject_into_class(""" + on(type: string, cb: Object) { + switch (type) { + case "BLEDeviceFind": return this.onBLEDeviceFind(cb as (info: ScanReport) => void); + default: throw new Error(`Unknown type: ${type}`); + } + } + off(type: string, cb?: Object) { + switch (type) { + case "BLEDeviceFind": return this.offBLEDeviceFind(cb as (((arg_0: ScanReport) => void) | undefined)); + default: throw new Error(`Unknown type: ${type}`); + } + } + """) + OnBLEDeviceFind(callback: (info: ScanReport) => void): void; + OffBLEDeviceFind(callback: Optional<(info: ScanReport) => void>): void; } function CreateBleScanner(): BleScanner; diff --git a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_advertise_callback.h b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_advertise_callback.h index 61f63439118daf42f2d052dc3fb07314aef2feee..dad321ac6c2a6a3d15d20a9ad7df59a4737d8ec7 100644 --- a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_advertise_callback.h +++ b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_advertise_callback.h @@ -16,6 +16,7 @@ #ifndef TAIHE_BLUETOOTH_BLE_ADVERTISE_CALLBACK_H #define TAIHE_BLUETOOTH_BLE_ADVERTISE_CALLBACK_H +#include #include "bluetooth_ble_advertiser.h" #include "ohos.bluetooth.ble.proj.hpp" #include "ohos.bluetooth.ble.impl.hpp" @@ -24,6 +25,18 @@ namespace OHOS { namespace Bluetooth { +constexpr int SDK_VERSION_20 = 20; +enum class AdvertisingState { + STARTED = 1, + ENABLED = 2, + DISABLED = 3, + STOPPED = 4 +}; + +extern std::vector<::taihe::optional<::taihe::callback>> g_advertisingStateChangeVec; +extern std::shared_mutex g_advertisingStateChangeLock; + class TaiheBluetoothBleAdvertiseCallback : public BleAdvertiseCallback { public: TaiheBluetoothBleAdvertiseCallback(); diff --git a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_central_manager_callback.h b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_central_manager_callback.h index 92e97780c84173eb73c3e3c76ff0972d8420dff2..a9068ba00243f841baa6c53a5f73b42321d2d357 100644 --- a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_central_manager_callback.h +++ b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_ble_central_manager_callback.h @@ -16,14 +16,28 @@ #ifndef TAIHE_BLUETOOTH_BLE_CENTRAL_MANAGER_H #define TAIHE_BLUETOOTH_BLE_CENTRAL_MANAGER_H +#include #include "bluetooth_ble_central_manager.h" #include "taihe_bluetooth_utils.h" +#include "ohos.bluetooth.ble.proj.hpp" +#include "ohos.bluetooth.ble.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" namespace OHOS { namespace Bluetooth { + +extern std::vector<::taihe::optional<::taihe::callback data)>>> g_bleDeviceFindVec; +extern std::shared_mutex g_bleDeviceFindLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_bleScannerDeviceFindVec; +extern std::shared_mutex g_bleScannerDeviceFindLock; + class TaiheBluetoothBleCentralManagerCallback : public BleCentralManagerCallback { public: - TaiheBluetoothBleCentralManagerCallback(bool isLatestNapiBleScannerObj); + TaiheBluetoothBleCentralManagerCallback(bool isLatestTaiheBleScannerObj); ~TaiheBluetoothBleCentralManagerCallback() override = default; static TaiheBluetoothBleCentralManagerCallback &GetInstance(void); @@ -33,9 +47,8 @@ public: void OnBleBatchScanResultsEvent(const std::vector &results) override; void OnStartOrStopScanEvent(int resultCode, bool isStartScan) override; void OnNotifyMsgReportFromLpDevice(const UUID &uuid, int msgType, const std::vector &value) override {}; - - void ConvertScanReportType(ScanReportType &scanReportType, uint8_t callbackType); - bool isLatestNapiBleScannerObj_ = false; + + bool isLatestTaiheBleScannerObj_ = false; }; } // namespace Bluetooth } // namespace OHOS diff --git a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_client_callback.h b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_client_callback.h index b8d2f35bddf907da2e5ef422d5b6d2346c65d732..b9dfae480b3eb7e17a669a9ca54e50db558ca58e 100644 --- a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_client_callback.h +++ b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_client_callback.h @@ -25,6 +25,19 @@ namespace OHOS { namespace Bluetooth { + +extern std::vector<::taihe::optional<::taihe::callback>> g_characteristicChangeVec; +extern std::shared_mutex g_characteristicChangeLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_bleMtuChangeVec; +extern std::shared_mutex g_bleMtuChangeLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_bleConnectionChangeStateVec; +extern std::shared_mutex g_bleConnectionChangeStateLock; + class TaiheGattClientCallback : public GattClientCallback { public: void OnConnectionStateChanged(int connectionState, int ret) override; @@ -54,6 +67,11 @@ public: private: std::string deviceAddr_ = INVALID_MAC_ADDRESS; + void ConvertBLEDescriptorVectorToJs(std::vector& descriptors, + ::taihe::array<::ohos::bluetooth::ble::BLEDescriptor>& taiheDescriptors); + void ConvertGattPropertiesToJs(int properties, + ::ohos::bluetooth::ble::GattProperties &taiheGattProperties); + bool HasProperty(int properties, int propertyMask); }; } // namespace Bluetooth } // namespace OHOS diff --git a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_server_callback.h b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_server_callback.h index eb5b28adcdefafbef6a739cd5de7c640cf9ff461..46084b3521d2ddbc060ec30775ad4bc10c5a98f6 100644 --- a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_server_callback.h +++ b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_gatt_server_callback.h @@ -16,10 +16,40 @@ #ifndef TAIHE_BLUETOOTH_GATT_SERVER_CALLBACK_H_ #define TAIHE_BLUETOOTH_GATT_SERVER_CALLBACK_H_ +#include #include "bluetooth_gatt_server.h" +#include "ohos.bluetooth.ble.proj.hpp" +#include "ohos.bluetooth.ble.impl.hpp" +#include "taihe/runtime.hpp" +#include "stdexcept" namespace OHOS { namespace Bluetooth { + +extern std::vector<::taihe::optional<::taihe::callback>> g_characteristicWriteVec; +extern std::shared_mutex g_characteristicWriteLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_gattServerMtuChangeVec; +extern std::shared_mutex g_gattServerMtuChangeLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_characteristicReadVec; +extern std::shared_mutex g_characteristicReadLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_descriptorReadVec; +extern std::shared_mutex g_descriptorReadLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_descriptorWriteVec; +extern std::shared_mutex g_descriptorWriteLock; + +extern std::vector<::taihe::optional<::taihe::callback>> g_connectionStateChangeVec; +extern std::shared_mutex g_connectionStateChangeLock; + class TaiheGattServerCallback : public GattServerCallback { public: void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) override; diff --git a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_utils.h b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_utils.h index 09ab37d1ac7c1fc83370e40c425ae32ded83a371..1dff601069e4221af7671dbad6b1846732df1925 100644 --- a/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_utils.h +++ b/frameworks/ets/taihe/bluetooth_ble/include/taihe_bluetooth_utils.h @@ -21,7 +21,8 @@ namespace Bluetooth { enum class ScanReportType { ON_FOUND = 1, // the found of advertisement packet - ON_LOST = 2 // the lost of advertisement packet + ON_LOST = 2, // the lost of advertisement packet + ON_BATCH = 3 // Batchscan reports }; } } diff --git a/frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp b/frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp index ad2651a5fa6162b631741ef02dc85e5c00ea69d6..2c386d5ae0e2436b5cc0699e91edf0a90e02f2ac 100644 --- a/frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp +++ b/frameworks/ets/taihe/bluetooth_ble/src/ohos.bluetooth.ble.impl.cpp @@ -115,6 +115,79 @@ public: ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "Close return error"); } + + void OnBLECharacteristicChange(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_characteristicChangeLock); + auto bleCharacteristicChangeCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_characteristicChangeVec.emplace_back(bleCharacteristicChangeCb); + } + + void OffBLECharacteristicChange(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_characteristicChangeLock); + if (OHOS::Bluetooth::g_characteristicChangeVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffBLECharacteristicChange return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_characteristicChangeVec.size(); ++i) { + if (OHOS::Bluetooth::g_characteristicChangeVec[i] == callback) { + OHOS::Bluetooth::g_characteristicChangeVec.erase( + OHOS::Bluetooth::g_characteristicChangeVec.begin() + i); + } + } + } + + void OnBLEMtuChange(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_bleMtuChangeLock); + auto bleMtuChangeCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_bleMtuChangeVec.emplace_back(bleMtuChangeCb); + } + + void OffBLEMtuChange(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_bleMtuChangeLock); + if (OHOS::Bluetooth::g_bleMtuChangeVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffBLEMtuChange return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_bleMtuChangeVec.size(); ++i) { + if (OHOS::Bluetooth::g_bleMtuChangeVec[i] == callback) { + OHOS::Bluetooth::g_bleMtuChangeVec.erase( + OHOS::Bluetooth::g_bleMtuChangeVec.begin() + i); + } + } + } + + void OnBLEConnectionStateChange(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_bleConnectionChangeStateLock); + auto bleConnectionStateChangeCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_bleConnectionChangeStateVec.emplace_back(bleConnectionStateChangeCb); + } + + void OffBLEConnectionStateChange(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_bleConnectionChangeStateLock); + if (OHOS::Bluetooth::g_bleConnectionChangeStateVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffBLEConnectionStateChange return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_bleConnectionChangeStateVec.size(); ++i) { + if (OHOS::Bluetooth::g_bleConnectionChangeStateVec[i] == callback) { + OHOS::Bluetooth::g_bleConnectionChangeStateVec.erase( + OHOS::Bluetooth::g_bleConnectionChangeStateVec.begin() + i); + } + } + } private: std::shared_ptr client_ = nullptr; std::shared_ptr callback_; @@ -141,6 +214,155 @@ public: ANI_BT_ASSERT_RETURN(ret == OHOS::Bluetooth::BT_NO_ERROR, ret, "Close return error"); } + + void OnCharacteristicWrite(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_characteristicWriteLock); + auto characteristicWriteCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_characteristicWriteVec.emplace_back(characteristicWriteCb); + } + + void OffCharacteristicWrite(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_characteristicWriteLock); + if (OHOS::Bluetooth::g_characteristicWriteVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffCharacteristicWrite return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_characteristicWriteVec.size(); ++i) { + if (OHOS::Bluetooth::g_characteristicWriteVec[i] == callback) { + OHOS::Bluetooth::g_characteristicWriteVec.erase( + OHOS::Bluetooth::g_characteristicWriteVec.begin() + i); + } + } + } + + void OnBLEMtuChange(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_gattServerMtuChangeLock); + auto gattServerMtuChangeCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_gattServerMtuChangeVec.emplace_back(gattServerMtuChangeCb); + } + + void OffBLEMtuChange(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_gattServerMtuChangeLock); + if (OHOS::Bluetooth::g_gattServerMtuChangeVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffBLEMtuChange return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_gattServerMtuChangeVec.size(); ++i) { + if (OHOS::Bluetooth::g_gattServerMtuChangeVec[i] == callback) { + OHOS::Bluetooth::g_gattServerMtuChangeVec.erase( + OHOS::Bluetooth::g_gattServerMtuChangeVec.begin() + i); + } + } + } + + void OnCharacteristicRead(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_characteristicReadLock); + auto characteristicReadCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_characteristicReadVec.emplace_back(characteristicReadCb); + } + + void OffCharacteristicRead(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_characteristicReadLock); + if (OHOS::Bluetooth::g_characteristicReadVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffCharacteristicRead return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_characteristicReadVec.size(); ++i) { + if (OHOS::Bluetooth::g_characteristicReadVec[i] == callback) { + OHOS::Bluetooth::g_characteristicReadVec.erase( + OHOS::Bluetooth::g_characteristicReadVec.begin() + i); + } + } + } + + void OnDescriptorRead(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_descriptorReadLock); + auto descriptorReadCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_descriptorReadVec.emplace_back(descriptorReadCb); + } + + void OffDescriptorRead(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_descriptorReadLock); + if (OHOS::Bluetooth::g_descriptorReadVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffDescriptorRead return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_descriptorReadVec.size(); ++i) { + if (OHOS::Bluetooth::g_descriptorReadVec[i] == callback) { + OHOS::Bluetooth::g_descriptorReadVec.erase( + OHOS::Bluetooth::g_descriptorReadVec.begin() + i); + } + } + } + + void OnDescriptorWrite(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_descriptorWriteLock); + auto descriptorReadCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_descriptorWriteVec.emplace_back(descriptorReadCb); + } + + void OffDescriptorWrite(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_descriptorWriteLock); + if (OHOS::Bluetooth::g_descriptorWriteVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffDescriptorWrite return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_descriptorWriteVec.size(); ++i) { + if (OHOS::Bluetooth::g_descriptorWriteVec[i] == callback) { + OHOS::Bluetooth::g_descriptorWriteVec.erase( + OHOS::Bluetooth::g_descriptorWriteVec.begin() + i); + } + } + } + + void OnConnectionStateChange(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_connectionStateChangeLock); + auto connectionStateChangeCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_connectionStateChangeVec.emplace_back(connectionStateChangeCb); + } + + void OffConnectionStateChange(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_connectionStateChangeLock); + if (OHOS::Bluetooth::g_connectionStateChangeVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffConnectionStateChange return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_connectionStateChangeVec.size(); ++i) { + if (OHOS::Bluetooth::g_connectionStateChangeVec[i] == callback) { + OHOS::Bluetooth::g_connectionStateChangeVec.erase( + OHOS::Bluetooth::g_connectionStateChangeVec.begin() + i); + } + } + } + private: std::shared_ptr server_ = nullptr; std::shared_ptr callback_; @@ -155,6 +377,31 @@ public: } ~BleScannerImpl() = default; + void OnBLEDeviceFind(::taihe::callback_view callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_bleDeviceFindLock); + auto connectionStateChangeCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_bleScannerDeviceFindVec.emplace_back(connectionStateChangeCb); + } + + void OffBLEDeviceFind(::taihe::optional_view<::taihe::callback> callback) + { + std::unique_lock guard(OHOS::Bluetooth::g_bleDeviceFindLock); + if (OHOS::Bluetooth::g_bleScannerDeviceFindVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffBLEDeviceFind return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_bleScannerDeviceFindVec.size(); ++i) { + if (OHOS::Bluetooth::g_bleScannerDeviceFindVec[i] == callback) { + OHOS::Bluetooth::g_bleScannerDeviceFindVec.erase( + OHOS::Bluetooth::g_bleScannerDeviceFindVec.begin() + i); + } + } + } + private: std::shared_ptr bleCentralManager_ = nullptr; std::shared_ptr callback_ = nullptr; @@ -194,10 +441,65 @@ BleScanner CreateBleScanner() { return make_holder(); } + +void OnAdvertisingStateChange(::taihe::callback_view callback) +{ + std::unique_lock guard(OHOS::Bluetooth::g_advertisingStateChangeLock); + auto advertisingStateChangeCb = ::taihe::optional<::taihe::callback>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_advertisingStateChangeVec.emplace_back(advertisingStateChangeCb); +} + +void OffAdvertisingStateChange(::taihe::optional_view<::taihe::callback> callback) +{ + std::unique_lock guard(OHOS::Bluetooth::g_advertisingStateChangeLock); + if (OHOS::Bluetooth::g_advertisingStateChangeVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffAdvertisingStateChange return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_advertisingStateChangeVec.size(); ++i) { + if (OHOS::Bluetooth::g_advertisingStateChangeVec[i] == callback) { + OHOS::Bluetooth::g_advertisingStateChangeVec.erase( + OHOS::Bluetooth::g_advertisingStateChangeVec.begin() + i); + } + } +} + +void OnBLEDeviceFind(::taihe::callback_view data)> callback) +{ + std::unique_lock guard(OHOS::Bluetooth::g_bleDeviceFindLock); + auto bleDeviceFindCb = ::taihe::optional<::taihe::callback data)>>{std::in_place_t{}, callback}; + OHOS::Bluetooth::g_bleDeviceFindVec.emplace_back(bleDeviceFindCb); +} + +void OffBLEDeviceFind(::taihe::optional_view<::taihe::callback data)>> callback) +{ + std::unique_lock guard(OHOS::Bluetooth::g_bleDeviceFindLock); + if (OHOS::Bluetooth::g_bleDeviceFindVec.empty()) { + set_business_error(OHOS::Bluetooth::BT_ERR_INVALID_PARAM, + "OffBLEDeviceFind return error: callback is not registered"); + } + for (std::uint32_t i = 0; i < OHOS::Bluetooth::g_bleDeviceFindVec.size(); ++i) { + if (OHOS::Bluetooth::g_bleDeviceFindVec[i] == callback) { + OHOS::Bluetooth::g_bleDeviceFindVec.erase( + OHOS::Bluetooth::g_bleDeviceFindVec.begin() + i); + } + } +} + } // namespace // Since these macros are auto-generate, lint will cause false positive. // NOLINTBEGIN +TH_EXPORT_CPP_API_OnAdvertisingStateChange(OnAdvertisingStateChange); +TH_EXPORT_CPP_API_OffAdvertisingStateChange(OffAdvertisingStateChange); +TH_EXPORT_CPP_API_OnBLEDeviceFind(OnBLEDeviceFind); +TH_EXPORT_CPP_API_OffBLEDeviceFind(OffBLEDeviceFind); TH_EXPORT_CPP_API_CreateGattClientDevice(CreateGattClientDevice); TH_EXPORT_CPP_API_CreateGattServer(CreateGattServer); TH_EXPORT_CPP_API_CreateBleScanner(CreateBleScanner); diff --git a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_advertise_callback.cpp b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_advertise_callback.cpp index 6130566ba19030e4628dc345c3f4ee037b67ba6d..1a4817bd509dfaf12257036682520e7e58646ede 100644 --- a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_advertise_callback.cpp +++ b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_advertise_callback.cpp @@ -18,6 +18,11 @@ namespace OHOS { namespace Bluetooth { + +std::vector<::taihe::optional<::taihe::callback>> g_advertisingStateChangeVec{}; +std::shared_mutex g_advertisingStateChangeLock; + TaiheBluetoothBleAdvertiseCallback::TaiheBluetoothBleAdvertiseCallback() {} @@ -31,21 +36,53 @@ std::shared_ptr TaiheBluetoothBleAdvertiseCa void TaiheBluetoothBleAdvertiseCallback::OnStartResultEvent(int result, int advHandle) { HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle); + auto state_ = ::ohos::bluetooth::ble::AdvertisingState::key_t::STARTED; + auto taiheResult = ::ohos::bluetooth::ble::AdvertisingStateChangeInfo{ + .advertisingId = advHandle, + .state = state_, + }; + for (auto callback : g_advertisingStateChangeVec) { + (*callback)(taiheResult); + } } void TaiheBluetoothBleAdvertiseCallback::OnEnableResultEvent(int result, int advHandle) { HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle); + auto state_ = ::ohos::bluetooth::ble::AdvertisingState::key_t::ENABLED; + auto taiheResult = ::ohos::bluetooth::ble::AdvertisingStateChangeInfo{ + .advertisingId = advHandle, + .state = state_, + }; + for (auto callback : g_advertisingStateChangeVec) { + (*callback)(taiheResult); + } } void TaiheBluetoothBleAdvertiseCallback::OnDisableResultEvent(int result, int advHandle) { HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle); + auto state_ = ::ohos::bluetooth::ble::AdvertisingState::key_t::DISABLED; + auto taiheResult = ::ohos::bluetooth::ble::AdvertisingStateChangeInfo{ + .advertisingId = advHandle, + .state = state_, + }; + for (auto callback : g_advertisingStateChangeVec) { + (*callback)(taiheResult); + } } void TaiheBluetoothBleAdvertiseCallback::OnStopResultEvent(int result, int advHandle) { HILOGI("enter, result: %{public}d advHandle: %{public}d", result, advHandle); + auto state_ = ::ohos::bluetooth::ble::AdvertisingState::key_t::STOPPED; + auto taiheResult = ::ohos::bluetooth::ble::AdvertisingStateChangeInfo{ + .advertisingId = advHandle, + .state = state_, + }; + for (auto callback : g_advertisingStateChangeVec) { + (*callback)(taiheResult); + } } void TaiheBluetoothBleAdvertiseCallback::OnSetAdvDataEvent(int result) diff --git a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_central_manager_callback.cpp b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_central_manager_callback.cpp index 5ab418e07a9adab3b82ba42b5aa4b7801117d424..3ab8526c03415edcb21e1cab1feb6ddc31577b31 100644 --- a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_central_manager_callback.cpp +++ b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_ble_central_manager_callback.cpp @@ -14,16 +14,47 @@ */ #include "taihe_bluetooth_ble_central_manager_callback.h" - #include "bluetooth_log.h" #include "bluetooth_utils.h" namespace OHOS { namespace Bluetooth { -TaiheBluetoothBleCentralManagerCallback::TaiheBluetoothBleCentralManagerCallback(bool isLatestNapiBleScannerObj) +std::vector<::taihe::optional<::taihe::callback data)>>> g_bleDeviceFindVec{}; +std::shared_mutex g_bleDeviceFindLock; + +std::vector<::taihe::optional<::taihe::callback>> g_bleScannerDeviceFindVec{}; + + +void ConvertScanResult(const std::vector& results, + ::taihe::array<::ohos::bluetooth::ble::ScanResult>& scanResultArray) +{ + size_t count = 0; + for (auto result : results) { + BleScanResult result_ = result; + auto deviceId_ = static_cast<::taihe::string>(result_.GetPeripheralDevice().GetDeviceAddr()); + auto rssi_ = static_cast(result_.GetRssi()); + auto data_ = ::taihe::array( + taihe::copy_data_t{}, result_.GetPayload().data(), result_.GetPayload().size()); + auto deviceName_ = static_cast<::taihe::string>(result_.GetName()); + auto connectable_ = result_.IsConnectable(); + auto taiheScanResult = ::ohos::bluetooth::ble::ScanResult{ + .deviceId = deviceId_, + .rssi = rssi_, + .data = data_, + .deviceName = deviceName_, + .connectable = connectable_, + }; + scanResultArray[count] = taiheScanResult; + ++count; + } +} + +TaiheBluetoothBleCentralManagerCallback::TaiheBluetoothBleCentralManagerCallback(bool isLatestTaiheBleScannerObj) { - isLatestNapiBleScannerObj_ = isLatestNapiBleScannerObj; + isLatestTaiheBleScannerObj_ = isLatestTaiheBleScannerObj; } TaiheBluetoothBleCentralManagerCallback &TaiheBluetoothBleCentralManagerCallback::GetInstance(void) @@ -34,27 +65,77 @@ TaiheBluetoothBleCentralManagerCallback &TaiheBluetoothBleCentralManagerCallback void TaiheBluetoothBleCentralManagerCallback::OnScanCallback(const BleScanResult &result) { + std::shared_lock guard(g_bleDeviceFindLock); HILOGD("enter, remote device address: %{public}s", GET_ENCRYPT_ADDR(result.GetPeripheralDevice())); + + std::vector<::ohos::bluetooth::ble::ScanResult> taiheScanResultVec{}; + ::taihe::array<::ohos::bluetooth::ble::ScanResult> scanResultArray(taiheScanResultVec); + ConvertScanResult(std::vector{result}, scanResultArray); + + if (isLatestTaiheBleScannerObj_) { + auto taiheScanReportType = ::ohos::bluetooth::ble::ScanReportType::key_t::ON_FOUND; + auto taiheScanReport = ohos::bluetooth::ble::ScanReport{ + .reportType = taiheScanReportType, + .scanResult = scanResultArray, + }; + for (auto callback : g_bleScannerDeviceFindVec) { + (*callback)(taiheScanReport); + } + } else { + for (auto callback : g_bleDeviceFindVec) { + (*callback)(scanResultArray); + } + } } void TaiheBluetoothBleCentralManagerCallback::OnFoundOrLostCallback(const BleScanResult &result, uint8_t callbackType) { + std::shared_lock guard(g_bleDeviceFindLock); HILOGD("enter, remote device address: %{public}s", GET_ENCRYPT_ADDR(result.GetPeripheralDevice())); + auto taiheScanReportType = ::ohos::bluetooth::ble::ScanReportType::key_t::ON_FOUND; + if (callbackType == BLE_SCAN_CALLBACK_TYPE_FIRST_MATCH) { + taiheScanReportType = ::ohos::bluetooth::ble::ScanReportType::key_t::ON_FOUND; + } else if (callbackType == BLE_SCAN_CALLBACK_TYPE_LOST_MATCH) { + taiheScanReportType = ::ohos::bluetooth::ble::ScanReportType::key_t::ON_LOST; + } + if (isLatestTaiheBleScannerObj_) { + std::vector<::ohos::bluetooth::ble::ScanResult> taiheScanResultVec{}; + ::taihe::array<::ohos::bluetooth::ble::ScanResult> scanResultArray(taiheScanResultVec); + ConvertScanResult(std::vector{result}, scanResultArray); + auto taiheScanReport = ohos::bluetooth::ble::ScanReport{ + .reportType = taiheScanReportType, + .scanResult = scanResultArray, + }; + for (auto callback : g_bleScannerDeviceFindVec) { + (*callback)(taiheScanReport); + } + } else { + HILOGE("error callback."); + } } void TaiheBluetoothBleCentralManagerCallback::OnBleBatchScanResultsEvent(const std::vector &results) { + std::shared_lock guard(g_bleDeviceFindLock); HILOGI("enter, scan result size: %{public}zu", results.size()); + if (isLatestTaiheBleScannerObj_) { + auto taiheScanReportType = ::ohos::bluetooth::ble::ScanReportType::key_t::ON_BATCH; + std::vector<::ohos::bluetooth::ble::ScanResult> taiheScanResultVec{}; + ::taihe::array<::ohos::bluetooth::ble::ScanResult> scanResultArray(taiheScanResultVec); + ConvertScanResult(results, scanResultArray); + auto taiheScanReport = ohos::bluetooth::ble::ScanReport{ + .reportType = taiheScanReportType, + .scanResult = scanResultArray, + }; + for (auto callback : g_bleScannerDeviceFindVec) { + (*callback)(taiheScanReport); + } + } } void TaiheBluetoothBleCentralManagerCallback::OnStartOrStopScanEvent(int resultCode, bool isStartScan) { HILOGI("resultCode: %{public}d, isStartScan: %{public}d", resultCode, isStartScan); } - -void TaiheBluetoothBleCentralManagerCallback::ConvertScanReportType( - ScanReportType &scanReportType, uint8_t callbackType) -{ -} } // namespace Bluetooth } // namespace OHOS \ No newline at end of file diff --git a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_client_callback.cpp b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_client_callback.cpp index f6feb57358cca0b7f91fceed14fafc973248ac92..25c75edb87083a765bd54045c69f4cfd3c1b8d59 100644 --- a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_client_callback.cpp +++ b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_client_callback.cpp @@ -20,12 +20,60 @@ namespace OHOS { namespace Bluetooth { + +std::vector<::taihe::optional<::taihe::callback>> g_characteristicChangeVec{}; +std::shared_mutex g_characteristicChangeLock; + +std::vector<::taihe::optional<::taihe::callback>> g_bleMtuChangeVec{}; +std::shared_mutex g_bleMtuChangeLock; + +std::vector<::taihe::optional<::taihe::callback>> g_bleConnectionChangeStateVec{}; +std::shared_mutex g_bleConnectionChangeStateLock; + TaiheGattClientCallback::TaiheGattClientCallback() {} void TaiheGattClientCallback::OnCharacteristicChanged(const GattCharacteristic &characteristic) { + std::shared_lock guard(g_characteristicChangeLock); HILOGI("UUID: %{public}s", characteristic.GetUuid().ToString().c_str()); + GattCharacteristic characteristic_ = characteristic; + size_t valueSize = 0; + uint8_t* valueData = characteristic_.GetValue(&valueSize).get(); + + ::taihe::string serviceUuid_(""); + if (characteristic_.GetService() != nullptr) { + serviceUuid_ = static_cast<::taihe::string>(characteristic_.GetService()->GetUuid().ToString()); + } + ::taihe::string characteristicUuid_(characteristic_.GetUuid().ToString()); + auto characteristicValue_ = ::taihe::array(taihe::copy_data_t{}, valueData, valueSize); + + std::vector<::ohos::bluetooth::ble::BLEDescriptor> taiheDescriptorVec{}; + ::taihe::array<::ohos::bluetooth::ble::BLEDescriptor> descriptors_(taiheDescriptorVec); + ConvertBLEDescriptorVectorToJs(characteristic_.GetDescriptors(), descriptors_); + + auto optionalInit = ::taihe::optional(std::nullopt); + ::ohos::bluetooth::ble::GattProperties property_ = { + optionalInit, optionalInit, optionalInit, optionalInit, optionalInit}; + ConvertGattPropertiesToJs(characteristic_.GetProperties(), property_); + auto properties_ = ::taihe::optional<::ohos::bluetooth::ble::GattProperties>{std::in_place_t{}, property_}; + + auto characteristicValueHandle_ = ::taihe::optional{std::in_place_t{}, characteristic_.GetHandle()}; + + auto taiheResult = ::ohos::bluetooth::ble::BLECharacteristic{ + .serviceUuid = serviceUuid_, + .characteristicUuid = characteristicUuid_, + .characteristicValue = characteristicValue_, + .descriptors = descriptors_, + .properties = properties_, + .characteristicValueHandle = characteristicValueHandle_, + }; + for (auto callback : g_characteristicChangeVec) { + (*callback)(taiheResult); + } } void TaiheGattClientCallback::OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) @@ -40,7 +88,17 @@ void TaiheGattClientCallback::OnDescriptorReadResult(const GattDescriptor &descr void TaiheGattClientCallback::OnConnectionStateChanged(int connectionState, int ret) { + std::shared_lock guard(g_bleConnectionChangeStateLock); HILOGI("connectionState:%{public}d, ret:%{public}d", connectionState, ret); + ::taihe::string deviceId_(deviceAddr_); + auto state_ = ::ohos::bluetooth::constant::ProfileConnectionState::from_value(connectionState); + auto taiheResult = ::ohos::bluetooth::ble::BLEConnectionChangeState{ + .deviceId = deviceId_, + .state = state_, + }; + for (auto callback : g_bleConnectionChangeStateVec) { + (*callback)(taiheResult); + } } void TaiheGattClientCallback::OnServicesDiscovered(int status) @@ -70,7 +128,63 @@ void TaiheGattClientCallback::OnSetNotifyCharacteristic(const GattCharacteristic void TaiheGattClientCallback::OnMtuUpdate(int mtu, int ret) { +#ifdef BLUETOOTH_API_SINCE_10 + std::shared_lock guard(g_bleMtuChangeLock); HILOGI("ret: %{public}d, mtu: %{public}d", ret, mtu); + for (auto callback : g_bleMtuChangeVec) { + (*callback)(mtu); + } +#endif +} +void TaiheGattClientCallback::ConvertBLEDescriptorVectorToJs(std::vector& descriptors, + ::taihe::array<::ohos::bluetooth::ble::BLEDescriptor>& taiheDescriptors) +{ + HILOGI("size: %{public}zu", descriptors.size()); + size_t idx = 0; + if (descriptors.empty()) { + return; + } + for (auto& descriptor : descriptors) { + ::ohos::bluetooth::ble::BLEDescriptor taiheBLEDescriptor = { + "", "", "", taihe::array{}, ::taihe::optional(std::nullopt)}; + taiheBLEDescriptor.descriptorUuid = static_cast<::taihe::string>(descriptor.GetUuid().ToString()); + if (descriptor.GetCharacteristic() != nullptr) { + taiheBLEDescriptor.characteristicUuid = static_cast<::taihe::string>( + descriptor.GetCharacteristic()->GetUuid().ToString()); + if (descriptor.GetCharacteristic()->GetService() != nullptr) { + taiheBLEDescriptor.serviceUuid = static_cast<::taihe::string>( + descriptor.GetCharacteristic()->GetService()->GetUuid().ToString()); + } + } + size_t valueSize; + uint8_t* valueData = descriptor.GetValue(&valueSize).get(); + taiheBLEDescriptor.descriptorValue = ::taihe::array(taihe::copy_data_t{}, valueData, valueSize); + taiheBLEDescriptor.descriptorHandle = ::taihe::optional{std::in_place_t{}, descriptor.GetHandle()}; + taiheDescriptors[idx] = taiheBLEDescriptor; + idx++; + } +} +void TaiheGattClientCallback::ConvertGattPropertiesToJs(int properties, + ::ohos::bluetooth::ble::GattProperties &taiheGattProperties) +{ + taiheGattProperties.write = ::taihe::optional( + std::in_place_t{}, HasProperty(properties, OHOS::Bluetooth::GattCharacteristic::WRITE)); + taiheGattProperties.writeNoResponse = ::taihe::optional( + std::in_place_t{}, HasProperty(properties, OHOS::Bluetooth::GattCharacteristic::WRITE_WITHOUT_RESPONSE)); + taiheGattProperties.read = ::taihe::optional( + std::in_place_t{}, HasProperty(properties, OHOS::Bluetooth::GattCharacteristic::READ)); + taiheGattProperties.notify = ::taihe::optional( + std::in_place_t{}, HasProperty(properties, OHOS::Bluetooth::GattCharacteristic::NOTIFY)); + taiheGattProperties.indicate = ::taihe::optional( + std::in_place_t{}, HasProperty(properties, OHOS::Bluetooth::GattCharacteristic::INDICATE)); +} +bool TaiheGattClientCallback::HasProperty(int properties, int propertyMask) +{ + if (properties < 0 || propertyMask < 0) { + HILOGE("properties or propertyMask is less than 0"); + return false; + } + return (static_cast(properties) & static_cast(propertyMask)) != 0; } } // namespace Bluetooth diff --git a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_server_callback.cpp b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_server_callback.cpp index ba3eb22e951b93f6c47a963cb1eb2e16c67e9805..7b1cfc1501ce4db252debf5b29cdf154e3ff521d 100644 --- a/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_server_callback.cpp +++ b/frameworks/ets/taihe/bluetooth_ble/src/taihe_bluetooth_gatt_server_callback.cpp @@ -20,46 +20,174 @@ namespace OHOS { namespace Bluetooth { using namespace std; +std::vector<::taihe::optional<::taihe::callback>> g_characteristicWriteVec{}; +std::shared_mutex g_characteristicWriteLock; + +std::vector<::taihe::optional<::taihe::callback>> g_gattServerMtuChangeVec; +std::shared_mutex g_gattServerMtuChangeLock; + +std::vector<::taihe::optional<::taihe::callback>> g_characteristicReadVec{}; +std::shared_mutex g_characteristicReadLock; + +std::vector<::taihe::optional<::taihe::callback>> g_descriptorReadVec{}; +std::shared_mutex g_descriptorReadLock; + +std::vector<::taihe::optional<::taihe::callback>> g_descriptorWriteVec{}; +std::shared_mutex g_descriptorWriteLock; + +std::vector<::taihe::optional<::taihe::callback>> g_connectionStateChangeVec{}; +std::shared_mutex g_connectionStateChangeLock; + TaiheGattServerCallback::TaiheGattServerCallback() {} void TaiheGattServerCallback::OnCharacteristicReadRequest( const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId) { + std::shared_lock guard(g_characteristicReadLock); HILOGI("enter, remote device address: %{public}s, requestId: %{public}d", GET_ENCRYPT_ADDR(device), requestId); + ::taihe::string deviceId_(device.GetDeviceAddr()); + int transId_ = requestId; + int offset_ = 0; + ::taihe::string characteristicUuid_(characteristic.GetUuid().ToString()); + ::taihe::string serviceUuid_(""); + if (characteristic.GetService() != nullptr) { + serviceUuid_ = static_cast<::taihe::string>(characteristic.GetService()->GetUuid().ToString()); + } + ::ohos::bluetooth::ble::CharacteristicReadRequest taiheResult = { + deviceId_, transId_, offset_, characteristicUuid_, serviceUuid_}; + for (auto callback : g_characteristicReadVec) { + (*callback)(taiheResult); + } } void TaiheGattServerCallback::OnCharacteristicWriteRequest(const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId) { + std::shared_lock guard(g_characteristicWriteLock); HILOGI("enter, remote device address: %{public}s, requestId: %{public}d", GET_ENCRYPT_ADDR(device), requestId); + ::taihe::string deviceId_(device.GetDeviceAddr()); + int transId_ = requestId; + int offset_ = 0; + bool isPrepared_ = false; + bool needRsp_ = characteristic.GetWriteType() == GattCharacteristic::WriteType::DEFAULT; + size_t valueSize; + uint8_t* valueData = characteristic.GetValue(&valueSize).get(); + auto value_ = ::taihe::array(taihe::copy_data_t{}, valueData, valueSize); + ::taihe::string characteristicUuid_(characteristic.GetUuid().ToString()); + ::taihe::string serviceUuid_(""); + if (characteristic.GetService() != nullptr) { + serviceUuid_ = static_cast<::taihe::string>(characteristic.GetService()->GetUuid().ToString()); + } + ::ohos::bluetooth::ble::CharacteristicWriteRequest taiheResult = { + deviceId_, transId_, offset_, isPrepared_, needRsp_, value_, characteristicUuid_, serviceUuid_}; + for (auto callback : g_characteristicWriteVec) { + (*callback)(taiheResult); + } } void TaiheGattServerCallback::OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) { + std::shared_lock guard(g_connectionStateChangeLock); HILOGI("enter, state: %{public}d, remote device address: %{public}s", state, GET_ENCRYPT_ADDR(device)); + ::taihe::string deviceId_(device.GetDeviceAddr()); + auto state_ = ::ohos::bluetooth::constant::ProfileConnectionState::from_value(state); + ::ohos::bluetooth::ble::BLEConnectionChangeState taiheResult = {deviceId_, state_}; + for (auto callback : g_connectionStateChangeVec) { + (*callback)(taiheResult); + } } void TaiheGattServerCallback::OnDescriptorWriteRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId) { + std::shared_lock guard(g_descriptorWriteLock); HILOGI("enter, remote device address: %{public}s, requestId: %{public}d", GET_ENCRYPT_ADDR(device), requestId); + ::taihe::string deviceId_(device.GetDeviceAddr()); + int transId_ = requestId; + int offset_ = 0; + bool isPrepared_ = false; + bool needRsp_ = true; + size_t valueSize; + uint8_t* valueData = descriptor.GetValue(&valueSize).get(); + auto value_ = ::taihe::array(taihe::copy_data_t{}, valueData, valueSize); + ::taihe::string descriptorUuid_(descriptor.GetUuid().ToString()); + ::taihe::string characteristicUuid_(""); + ::taihe::string serviceUuid_(""); + if (descriptor.GetCharacteristic() != nullptr) { + characteristicUuid_ = static_cast<::taihe::string>( + descriptor.GetCharacteristic()->GetUuid().ToString()); + if (descriptor.GetCharacteristic()->GetService() != nullptr) { + serviceUuid_ = static_cast<::taihe::string>( + descriptor.GetCharacteristic()->GetService()->GetUuid().ToString()); + } + } + auto taiheResult = ::ohos::bluetooth::ble::DescriptorWriteRequest{ + .deviceId = deviceId_, + .transId = transId_, + .offset = offset_, + .isPrepared = isPrepared_, + .needRsp = needRsp_, + .value = value_, + .descriptorUuid = descriptorUuid_, + .characteristicUuid = characteristicUuid_, + .serviceUuid = serviceUuid_, + }; + for (auto callback : g_descriptorWriteVec) { + (*callback)(taiheResult); + } } void TaiheGattServerCallback::OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, int requestId) { + std::shared_lock guard(g_descriptorReadLock); HILOGI("enter, remote device address: %{public}s, requestId: %{public}d", GET_ENCRYPT_ADDR(device), requestId); + ::taihe::string deviceId_(device.GetDeviceAddr()); + int transId_ = requestId; + int offset_ = 0; + ::taihe::string descriptorUuid_(descriptor.GetUuid().ToString()); + ::taihe::string characteristicUuid_(""); + ::taihe::string serviceUuid_(""); + if (descriptor.GetCharacteristic() != nullptr) { + characteristicUuid_ = static_cast<::taihe::string>( + descriptor.GetCharacteristic()->GetUuid().ToString()); + if (descriptor.GetCharacteristic()->GetService() != nullptr) { + serviceUuid_ = static_cast<::taihe::string>( + descriptor.GetCharacteristic()->GetService()->GetUuid().ToString()); + } + } + auto taiheResult = ::ohos::bluetooth::ble::DescriptorReadRequest{ + .deviceId = deviceId_, + .transId = transId_, + .offset = offset_, + .descriptorUuid = descriptorUuid_, + .characteristicUuid = characteristicUuid_, + .serviceUuid = serviceUuid_, + }; + for (auto callback : g_descriptorReadVec) { + (*callback)(taiheResult); + } } void TaiheGattServerCallback::OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) { + std::shared_lock guard(g_gattServerMtuChangeLock); HILOGI("enter, remote device address: %{public}s, mtu: %{public}d", GET_ENCRYPT_ADDR(device), mtu); + for (auto callback : g_gattServerMtuChangeVec) { + (*callback)(mtu); + } } void TaiheGattServerCallback::OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int ret)