From c602713a35501ac8610d02bc5779a82021e7a17a Mon Sep 17 00:00:00 2001 From: liuhan53 Date: Wed, 30 Oct 2024 14:39:26 +0800 Subject: [PATCH] [feat]: add clientDevice & gattServer ffi api Signed-off-by: liuhan53 --- .../ble/include/bluetooth_ble_clientDevice.h | 52 +++- .../cj/ble/include/bluetooth_ble_common.h | 26 ++ frameworks/cj/ble/include/bluetooth_ble_ffi.h | 176 ++++++++++- .../cj/ble/include/bluetooth_ble_gattServer.h | 39 ++- .../cj/ble/include/bluetooth_ble_impl.h | 6 +- .../cj/ble/src/bluetooth_ble_clientDevice.cpp | 273 ++++++++++++++++++ .../cj/ble/src/bluetooth_ble_common.cpp | 255 +++++++++++++++- frameworks/cj/ble/src/bluetooth_ble_ffi.cpp | 233 ++++++++++++++- .../bluetooth_ble_ffiAdvertiseCallback.cpp | 4 +- ...luetooth_ble_ffiCentralManagerCallback.cpp | 51 ++-- .../bluetooth_ble_ffiGattClientCallback.cpp | 149 +++++++++- .../bluetooth_ble_ffiGattServerCallback.cpp | 232 ++++++++++++++- .../cj/ble/src/bluetooth_ble_gattServer.cpp | 229 +++++++++++++++ frameworks/cj/ble/src/bluetooth_ble_impl.cpp | 5 +- 14 files changed, 1678 insertions(+), 52 deletions(-) diff --git a/frameworks/cj/ble/include/bluetooth_ble_clientDevice.h b/frameworks/cj/ble/include/bluetooth_ble_clientDevice.h index 8fd43145..42ea58db 100644 --- a/frameworks/cj/ble/include/bluetooth_ble_clientDevice.h +++ b/frameworks/cj/ble/include/bluetooth_ble_clientDevice.h @@ -16,6 +16,7 @@ #ifndef BLUETOOTH_BLE_CLIENTDEVICE_H #define BLUETOOTH_BLE_CLIENTDEVICE_H +#include "bluetooth_ble_ffi.h" #include "bluetooth_def.h" #include "bluetooth_gatt_client.h" #include "bluetooth_remote_device.h" @@ -34,17 +35,26 @@ using Bluetooth::INVALID_MAC_ADDRESS; class FfiGattClientCallback : public GattClientCallback { public: - void OnConnectionStateChanged(int connectionState, int ret) override{}; - void OnCharacteristicChanged(const GattCharacteristic &characteristic) override{}; - void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) override{}; - void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret) override{}; - void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) override{}; - void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret) override{}; - void OnMtuUpdate(int mtu, int ret) override{}; + void OnConnectionStateChanged(int connectionState, int ret) override; + void OnCharacteristicChanged(const GattCharacteristic &characteristic) override; + void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) override; + void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret) override; + void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) override; + void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret) override; + void OnMtuUpdate(int mtu, int ret) override; void OnServicesDiscovered(int status) override{}; void OnConnectionParameterChanged(int interval, int latency, int timeout, int status) override{}; void OnSetNotifyCharacteristic(const GattCharacteristic &characteristic, int status) override{}; - void OnReadRemoteRssiValueResult(int rssi, int status) override{}; + void OnReadRemoteRssiValueResult(int rssi, int status) override; + + void RegisterBLECharacteristicChangeFunc(std::function cjCallback); + void RegisterBLEConnectionStateChangeFunc(std::function cjCallback); + void RegisterBLEMtuChangeFunc(std::function cjCallback); + void RegisterGetRemoteRssicallback(std::function cjCallback); + void RegisterReadCharacteristicCallback(std::function cjCallback); + void RegisterReadDescriptorCallback(std::function cjCallback); + void RegisterWriteCharacteristicCallback(std::function cjCallback); + void RegisterWriteDescriptorCallback(std::function cjCallback); FfiGattClientCallback(); ~FfiGattClientCallback() override = default; @@ -52,6 +62,17 @@ public: private: friend class FfiClientDevice; std::string deviceAddr_ = INVALID_MAC_ADDRESS; + + int remoteRssi{-1}; + + std::function bleCharacteristicChangeFunc{nullptr}; + std::function bleConnectionStateChangeFunc{nullptr}; + std::function bleMtuChangeFunc{nullptr}; + std::function getRssiValueFunc{nullptr}; + std::function readCharacteristicFunc{nullptr}; + std::function readDescriptorFunc{nullptr}; + std::function writeCharacteristicFunc{nullptr}; + std::function writeDescriptorValueFunc{nullptr}; }; class FfiClientDevice : public OHOS::FFI::FFIData { @@ -65,6 +86,21 @@ public: callback_ = std::make_shared(); callback_->deviceAddr_ = deviceId; }; + int32_t Connect(); + int32_t Disconnect(); + int32_t Close(); + std::string GetDeviceName(int32_t *errCode); + int32_t GetServices(CArrGattService &service); + int32_t ReadCharacteristicValue(NativeBLECharacteristic &characteristic, void (*callback)()); + int32_t ReadDescriptorValue(NativeBLEDescriptor &inputDescriptor, void (*callback)()); + int32_t WriteCharacteristicValue(NativeBLECharacteristic characteristic, int32_t writeType, void (*callback)()); + int32_t WriteDescriptorValue(NativeBLEDescriptor inputDescriptor, void (*callback)()); + int32_t GetRssiValue(void (*callback)()); + int32_t SetBLEMtuSize(int32_t mut); + int32_t SetCharacteristicChangeNotification(NativeBLECharacteristic characteristic, bool enable); + int32_t SetCharacteristicChangeIndication(NativeBLECharacteristic characteristic, bool enable); + + int32_t RegisterBleGattClientDeviceObserver(int32_t callbackType, void (*callback)()); private: std::shared_ptr client_ = nullptr; diff --git a/frameworks/cj/ble/include/bluetooth_ble_common.h b/frameworks/cj/ble/include/bluetooth_ble_common.h index 7bcc2eed..27f84be9 100644 --- a/frameworks/cj/ble/include/bluetooth_ble_common.h +++ b/frameworks/cj/ble/include/bluetooth_ble_common.h @@ -16,14 +16,40 @@ #ifndef BLUETOOTH_BLE_COMMON_H #define BLUETOOTH_BLE_COMMON_H +#include "bluetooth_ble_ffi.h" +#include "bluetooth_gatt_service.h" #include "cj_common_ffi.h" namespace OHOS { namespace CJSystemapi { namespace CJBluetoothBle { +using Bluetooth::GattCharacteristic; +using Bluetooth::GattDescriptor; +using Bluetooth::GattService; + +enum ProfileConnectionState { + STATE_DISCONNECTED = 0, // the current profile is disconnected + STATE_CONNECTING = 1, // the current profile is being connected + STATE_CONNECTED = 2, // the current profile is connected + STATE_DISCONNECTING = 3 // the current profile is being disconnected +}; + char *MallocCString(const std::string &origin); CArrString Convert2CArrString(std::vector &tids); CArrUI8 Convert2CArrUI8(std::vector vec); +std::string GetGattClientDeviceId(); +CArrBLECharacteristic Convert2CArrBLECharacteristic(std::vector characteristics); +CArrGattService Convert2CArrGattService(std::vector services); +CArrBLEDescriptor Convert2CArrBLEDescriptor(std::vector &descriptors); +NativeGattProperties ConvertGattPropertiesToCJ(int properties); +NativeBLECharacteristic ConvertBLECharacteristicToCJ(GattCharacteristic &characteristic); +NativeGattService ConvertGattServiceToCJ(GattService service); +NativeBLEDescriptor ConvertBLEDescriptorToCJ(GattDescriptor &descriptor); +uint16_t ConvertGattPermissions(const NativeGattPermission &nativePermissions); +uint16_t ConvertGattProperties(const NativeGattProperties &nativeProperties); +int GetProfileConnectionState(int state); +void FreeNativeBLECharacteristic(NativeBLECharacteristic characteristic); +void FreeNativeBLEDescriptor(NativeBLEDescriptor descriptor); } // namespace CJBluetoothBle } // namespace CJSystemapi } // namespace OHOS diff --git a/frameworks/cj/ble/include/bluetooth_ble_ffi.h b/frameworks/cj/ble/include/bluetooth_ble_ffi.h index 93a654d4..6f0d8614 100644 --- a/frameworks/cj/ble/include/bluetooth_ble_ffi.h +++ b/frameworks/cj/ble/include/bluetooth_ble_ffi.h @@ -115,21 +115,191 @@ typedef struct { int64_t size; } CArrScanResult; +typedef struct { + char *serviceUuid; + char *characteristicUuid; + char *descriptorUuid; + CArrUI8 descriptorValue; +} NativeBLEDescriptor; + +typedef struct { + int32_t code; + NativeBLEDescriptor data; +} RetNativeBLEDescriptor; + +typedef struct { + NativeBLEDescriptor *head; + int64_t size; +} CArrBLEDescriptor; + +typedef struct { + bool write; + bool writeNoResponse; + bool read; + bool notify; + bool indicate; +} NativeGattProperties; + +typedef struct { + char *serviceUuid; + char *characteristicUuid; + CArrUI8 characteristicValue; + CArrBLEDescriptor descriptors; + NativeGattProperties properties; +} NativeBLECharacteristic; + +typedef struct { + int32_t code; + NativeBLECharacteristic data; +} RetNativeBLECharacteristic; + +typedef struct { + NativeBLECharacteristic *head; + int64_t size; +} CArrBLECharacteristic; + +struct NativeGattService; + +struct CArrGattService { + NativeGattService *head; + int64_t size; +}; + +struct NativeGattService { + char *serviceUuid; + bool isPrimary; + CArrBLECharacteristic characteristics; + CArrGattService includeServices; +}; + +typedef struct { + char *serviceUuid; + char *characteristicUuid; + CArrUI8 characteristicValue; + bool confirm; +} NativeNotifyCharacteristic; + +typedef struct { + char *deviceId; + int32_t transId; + int32_t status; + int32_t offset; + CArrUI8 value; + bool confirm; +} NativeServerResponse; + +typedef struct { + char *deviceId; + int32_t transId; + int32_t offset; + char *characteristicUuid; + char *serviceUuid; +} NativeCharacteristicReadRequest; + +typedef struct { + char *deviceId; + int32_t transId; + int32_t offset; + bool isPrepared; + bool needRsp; + CArrUI8 value; + char *characteristicUuid; + char *serviceUuid; +} NativeCharacteristicWriteRequest; + +typedef struct { + char *deviceId; + int32_t transId; + int32_t offset; + char *descriptorUuid; + char *characteristicUuid; + char *serviceUuid; +} NativeDescriptorReadRequest; + +typedef struct { + char *deviceId; + int32_t transId; + int32_t offset; + bool isPrepared; + bool needRsp; + CArrUI8 value; + char *descriptorUuid; + char *characteristicUuid; + char *serviceUuid; +} NativeDescriptorWriteRequest; + +typedef struct { + char *deviceId; + int32_t state; +} NativeBLEConnectionChangeState; + +const int32_t REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE = 0; +const int32_t REGISTER_BLE_FIND_DEVICE_TYPE = 1; + +const int32_t CHARACTERISTIC_READ = 0; +const int32_t CHARACTERISTIC_WRITE = 1; +const int32_t DESCRIPTOR_READ = 2; +const int32_t DESCRIPTOR_WRITE = 3; +const int32_t CONNECTION_STATE_CHANGE = 4; +const int32_t SERVER_BLE_MTU_CHANGE = 5; + +const int32_t BLE_CHARACTERISTIC_CHANGE = 0; +const int32_t BLE_CONNECTION_STATE_CHANGE = 1; +const int32_t CLIENT_BLE_MTU_CHANGE = 2; + +struct NativeGattPermission { + bool readable = false; + bool writeable = false; + bool readEncrypted = false; + bool writeEncrypted = false; +}; + FFI_EXPORT int64_t FfiBluetoothBleCreateGattServer(int32_t *errCode); FFI_EXPORT int64_t FfiBluetoothBleCreateGattClientDevice(const char *deviceId, int32_t *errCode); FFI_EXPORT CArrString FfiBluetoothBleGetConnectedBleDevices(int32_t *errCode); FFI_EXPORT void FfiBluetoothBleStartBleScan(CArrNativeScanFilter filters, NativeScanOptions *options, int32_t *errCode); FFI_EXPORT void FfiBluetoothBleStopBleScan(int32_t *errCode); FFI_EXPORT void FfiBluetoothBleStartAdvertising(NativeAdvertiseSetting setting, NativeAdvertiseData advData, - NativeAdvertiseData *advResponse, int32_t *errCode); + NativeAdvertiseData *advResponse, int32_t *errCode); FFI_EXPORT void FfiBluetoothBleStopAdvertising(int32_t *errCode); FFI_EXPORT int32_t FfiBluetoothBleStartAdvertisingWithId(NativeAdvertisingParams advertisingParams, int32_t *errCode); FFI_EXPORT void FfiBluetoothBleEnableAdvertising(NativeAdvertisingEnableParams advertisingEnableParams, - int32_t *errCode); + int32_t *errCode); FFI_EXPORT void FfiBluetoothBleDisableAdvertising(NativeAdvertisingDisableParams advertisingDisableParams, - int32_t *errCode); + int32_t *errCode); FFI_EXPORT void FfiBluetoothBleStopAdvertisingWithId(uint32_t advertisingId, int32_t *errCode); FFI_EXPORT void FfiBluetoothBleOn(int32_t callbackType, void (*callback)(), int32_t *errCode); + +FFI_EXPORT void FfiBluetoothBleGattClientDeviceConnect(int64_t id, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceDisconnect(int64_t id, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceClose(int64_t id, int32_t *errCode); +FFI_EXPORT char *FfiBluetoothBleGattClientDeviceGetDeviceName(int64_t id, int32_t *errCode); +FFI_EXPORT CArrGattService FfiBluetoothBleGattClientDeviceGetServices(int64_t id, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceReadCharacteristicValue(int64_t id, + NativeBLECharacteristic characteristic, void (*callback)(), int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceReadDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, + void (*callback)(), int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceWriteCharacteristicValue(int64_t id, + NativeBLECharacteristic characteristic, int32_t writeType, void (*callback)(), int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceWriteDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, + void (*callback)(), int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceGetRssiValue(int64_t id, void (*callback)(), int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceSetBLEMtuSize(int64_t id, int32_t mut, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeNotification(int64_t id, + NativeBLECharacteristic characteristic, bool enable, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeIndication(int64_t id, + NativeBLECharacteristic characteristic, bool enable, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattClientDeviceOn(int64_t id, int32_t callbackType, void (*callback)(), + int32_t *errCode); + +FFI_EXPORT void FfiBluetoothBleGattServerAddService(int64_t id, NativeGattService service, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattServerRemoveService(int64_t id, char *serviceUuid, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattServerClose(int64_t id, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattServerNotifyCharacteristicChanged(int64_t id, char *deviceId, + NativeNotifyCharacteristic characteristic, int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattServerSendResponse(int64_t id, NativeServerResponse serverResponse, + int32_t *errCode); +FFI_EXPORT void FfiBluetoothBleGattServerOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errCode); } #endif // BLUETOOTH_BLE_FFI_H \ No newline at end of file diff --git a/frameworks/cj/ble/include/bluetooth_ble_gattServer.h b/frameworks/cj/ble/include/bluetooth_ble_gattServer.h index 21e44c00..ed38f88c 100644 --- a/frameworks/cj/ble/include/bluetooth_ble_gattServer.h +++ b/frameworks/cj/ble/include/bluetooth_ble_gattServer.h @@ -16,6 +16,7 @@ #ifndef BLUETOOTH_BLE_GATTSERVER_H #define BLUETOOTH_BLE_GATTSERVER_H +#include "bluetooth_ble_ffi.h" #include "bluetooth_gatt_server.h" #include "cj_common_ffi.h" #include "ffi_remote_data.h" @@ -32,25 +33,38 @@ using Bluetooth::GattService; class FfiGattServerCallback : public GattServerCallback { public: - void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) override{}; + void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) override; void OnServiceAdded(GattService *Service, int ret) override{}; void OnCharacteristicReadRequest(const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, - int requestId) override{}; + int requestId) override; void OnCharacteristicWriteRequest(const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, - int requestId) override{}; + int requestId) override; void OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, - int requestId) override{}; + int requestId) override; void OnDescriptorWriteRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, - int requestId) override{}; - void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) override{}; + int requestId) override; + void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) override; void OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int result) override{}; void OnConnectionParameterChanged(const BluetoothRemoteDevice &device, int interval, int latency, int timeout, int status) override{}; + void RegisterCharacteristicReadFunc(std::function cjCallback); + void RegisterCharacteristicWriteFunc(std::function cjCallback); + void RegisterDescriptorReadFunc(std::function cjCallback); + void RegisterDescriptorWriteFunc(std::function cjCallback); + void RegisterConnectionStateChangeFunc(std::function cjCallback); + void RegisterBLEMtuChangeFunc(std::function cjCallback); + FfiGattServerCallback(); ~FfiGattServerCallback() override = default; private: + std::function characteristicReadFunc{nullptr}; + std::function characteristicWriteFunc{nullptr}; + std::function descriptorReadFunc{nullptr}; + std::function descriptorWriteFunc{nullptr}; + std::function connectionStateChangeFunc{nullptr}; + std::function bleMtuChangeFunc{nullptr}; }; class FfiGattServer : public OHOS::FFI::FFIData { @@ -62,6 +76,12 @@ public: std::shared_ptr tmp = std::static_pointer_cast(callback_); server_ = GattServer::CreateInstance(tmp); }; + int32_t AddService(NativeGattService service); + int32_t RemoveService(std::string serviceUuid); + int32_t Close(); + int32_t NotifyCharacteristicChanged(std::string deviceId, NativeNotifyCharacteristic characteristic); + int32_t SendResponse(NativeServerResponse serverResponse); + int32_t RegisterBleGattServerObserver(int32_t callbackType, void (*callback)()); std::shared_ptr &GetServer() { @@ -77,6 +97,13 @@ public: private: std::shared_ptr server_ = nullptr; std::shared_ptr callback_; + + int32_t CreateCharacteristicReadFunc(void (*callback)()); + int32_t CreateCharacteristicWriteFunc(void (*callback)()); + int32_t CreateDescriptorReadFunc(void (*callback)()); + int32_t CreateRegisterDescriptorWriteFunc(void (*callback)()); + int32_t CreateConnectionStateChangeFunc(void (*callback)()); + int32_t CreateBLEMtuChangeFunc(void (*callback)()); }; } // namespace CJBluetoothBle } // namespace CJSystemapi diff --git a/frameworks/cj/ble/include/bluetooth_ble_impl.h b/frameworks/cj/ble/include/bluetooth_ble_impl.h index adc8726e..bb9afad8 100644 --- a/frameworks/cj/ble/include/bluetooth_ble_impl.h +++ b/frameworks/cj/ble/include/bluetooth_ble_impl.h @@ -44,9 +44,9 @@ public: static FfiBluetoothBleCentralManagerCallback &GetInstance(void); - void OnScanCallback(const BleScanResult &result) override{}; + void OnScanCallback(const BleScanResult &result) override; void OnFoundOrLostCallback(const BleScanResult &result, uint8_t callbackType) override{}; - void OnBleBatchScanResultsEvent(const std::vector &results) override; + 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{}; @@ -85,7 +85,7 @@ public: static int32_t CreateGattServer(FfiGattServer *&ffiGattServer); static int32_t CreateGattClientDevice(std::string deviceId, FfiClientDevice *&ffiClientDevice); - static int32_t GetConnectedBleDevices(CArrString res); + static int32_t GetConnectedBleDevices(CArrString &res); static int32_t StartBleScan(CArrNativeScanFilter filters, NativeScanOptions *options); static int32_t StopBleScan(); static int32_t StartAdvertising(NativeAdvertiseSetting setting, NativeAdvertiseData advData, diff --git a/frameworks/cj/ble/src/bluetooth_ble_clientDevice.cpp b/frameworks/cj/ble/src/bluetooth_ble_clientDevice.cpp index dea10205..76b990d0 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_clientDevice.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_clientDevice.cpp @@ -15,9 +15,282 @@ #include "bluetooth_ble_clientDevice.h" +#include "bluetooth_ble_common.h" +#include "bluetooth_errorcode.h" +#include "bluetooth_host.h" +#include "bluetooth_log.h" +#include "cj_lambda.h" + namespace OHOS { namespace CJSystemapi { namespace CJBluetoothBle { +using Bluetooth::BluetoothHost; +using Bluetooth::BT_ERR_INTERNAL_ERROR; +using Bluetooth::BT_NO_ERROR; +using Bluetooth::BT_TRANSPORT_BLE; +using Bluetooth::GATT_TRANSPORT_TYPE_LE; +using Bluetooth::UUID; + +int32_t FfiClientDevice::Connect() +{ + if (client_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + return client_->Connect(callback_, false, GATT_TRANSPORT_TYPE_LE); +} + +int32_t FfiClientDevice::Disconnect() +{ + if (client_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + return client_->Disconnect(); +} + +int32_t FfiClientDevice::Close() +{ + if (client_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + return client_->Close(); +} + +std::string FfiClientDevice::GetDeviceName(int32_t *errCode) +{ + std::string deviceName = ""; + std::string deviceAddr = GetGattClientDeviceId(); + *errCode = BluetoothHost::GetDefaultHost().GetRemoteDevice(deviceAddr, BT_TRANSPORT_BLE).GetDeviceName(deviceName); + + HILOGI("err: %{public}d, deviceName: %{public}s", *errCode, deviceName.c_str()); + return deviceName; +} + +int32_t FfiClientDevice::GetServices(CArrGattService &service) +{ + if (client_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + int ret = client_->DiscoverServices(); + if (ret == BT_NO_ERROR) { + HILOGI("start get services"); + std::vector nativeServices = client_->GetService(); + service = Convert2CArrGattService(nativeServices); + } + return ret; +} + +static GattCharacteristic *GetCharacteristic(const std::shared_ptr &client, const UUID &serviceUuid, + const UUID &characterUuid) +{ + GattCharacteristic *character = nullptr; + if (client) { + auto service = client->GetService(serviceUuid); + if (service.has_value()) { + character = service->get().GetCharacteristic(characterUuid); + } + } + return character; +} + +static GattCharacteristic *GetGattcCharacteristic(const std::shared_ptr &client, + NativeBLECharacteristic &nativeCharacteristic) +{ + GattCharacteristic *character = GetCharacteristic(client, UUID::FromString(nativeCharacteristic.serviceUuid), + UUID::FromString(nativeCharacteristic.characteristicUuid)); + if (character) { + character->SetValue(nativeCharacteristic.characteristicValue.head, + nativeCharacteristic.characteristicValue.size); + } + return character; +} + +int32_t FfiClientDevice::ReadCharacteristicValue(NativeBLECharacteristic &characteristic, void (*callback)()) +{ + GattCharacteristic *character = GetGattcCharacteristic(client_, characteristic); + + if (character == nullptr) { + HILOGE("character is nullptr"); + return BT_ERR_INTERNAL_ERROR; + } + int ret = BT_ERR_INTERNAL_ERROR; + if (client_) { + ret = client_->ReadCharacteristic(*character); + } + if (ret != BT_NO_ERROR) { + return ret; + } + auto readCharacteristicFunc = CJLambda::Create(reinterpret_cast(callback)); + callback_->RegisterReadCharacteristicCallback(readCharacteristicFunc); + return BT_NO_ERROR; +} + +static GattDescriptor *GetGattcDescriptor(const std::shared_ptr &client, + const NativeBLEDescriptor &nativeDescriptor) +{ + GattDescriptor *descriptor = nullptr; + if (client) { + auto *character = GetCharacteristic(client, UUID::FromString(nativeDescriptor.serviceUuid), + UUID::FromString(nativeDescriptor.characteristicUuid)); + if (character == nullptr) { + HILOGE("character is nullptr"); + return nullptr; + } + descriptor = character->GetDescriptor(UUID::FromString(nativeDescriptor.descriptorUuid)); + if (descriptor) { + descriptor->SetValue(nativeDescriptor.descriptorValue.head, nativeDescriptor.descriptorValue.size); + } + } + return descriptor; +} + +int32_t FfiClientDevice::ReadDescriptorValue(NativeBLEDescriptor &inputDescriptor, void (*callback)()) +{ + GattDescriptor *descriptor = GetGattcDescriptor(client_, inputDescriptor); + + if (descriptor == nullptr) { + HILOGE("descriptor is nullptr"); + return BT_ERR_INTERNAL_ERROR; + } + int ret = BT_ERR_INTERNAL_ERROR; + if (client_) { + ret = client_->ReadDescriptor(*descriptor); + } + if (ret != BT_NO_ERROR) { + return ret; + } + auto readDescriptorFunc = CJLambda::Create(reinterpret_cast(callback)); + callback_->RegisterReadDescriptorCallback(readDescriptorFunc); + return BT_NO_ERROR; +} + +int32_t FfiClientDevice::WriteCharacteristicValue(NativeBLECharacteristic characteristic, int32_t writeType, + void (*callback)()) +{ + GattCharacteristic *character = GetGattcCharacteristic(client_, characteristic); + std::vector value{}; + + character->SetWriteType(writeType); + + if (character == nullptr) { + HILOGE("character is nullptr"); + return BT_ERR_INTERNAL_ERROR; + } + int ret = BT_ERR_INTERNAL_ERROR; + if (client_) { + ret = client_->WriteCharacteristic(*character); + } + if (ret != BT_NO_ERROR) { + return ret; + } + auto writeCharacteristicFunc = CJLambda::Create(reinterpret_cast(callback)); + callback_->RegisterWriteCharacteristicCallback(writeCharacteristicFunc); + return BT_NO_ERROR; +} + +int32_t FfiClientDevice::WriteDescriptorValue(NativeBLEDescriptor inputDescriptor, void (*callback)()) +{ + GattDescriptor *descriptor = GetGattcDescriptor(client_, inputDescriptor); + + if (descriptor == nullptr) { + HILOGE("descriptor is nullptr"); + return BT_ERR_INTERNAL_ERROR; + } + int ret = BT_ERR_INTERNAL_ERROR; + if (client_) { + ret = client_->WriteDescriptor(*descriptor); + } + if (ret != BT_NO_ERROR) { + return ret; + } + auto writeDescriptorValueFunc = CJLambda::Create(reinterpret_cast(callback)); + callback_->RegisterWriteDescriptorCallback(writeDescriptorValueFunc); + return BT_NO_ERROR; +} + +int32_t FfiClientDevice::GetRssiValue(void (*callback)()) +{ + int ret = BT_ERR_INTERNAL_ERROR; + if (client_) { + ret = client_->ReadRemoteRssiValue(); + } + if (ret != BT_NO_ERROR) { + return ret; + } + auto getRssiValueFunc = CJLambda::Create(reinterpret_cast(callback)); + callback_->RegisterGetRemoteRssicallback(getRssiValueFunc); + return BT_NO_ERROR; +} + +int32_t FfiClientDevice::SetBLEMtuSize(int32_t mut) +{ + int ret = client_->RequestBleMtuSize(mut); + return ret; +} + +int32_t FfiClientDevice::SetCharacteristicChangeNotification(NativeBLECharacteristic characteristic, bool enable) +{ + GattCharacteristic *character = GetGattcCharacteristic(client_, characteristic); + if (character == nullptr) { + HILOGE("character is nullptr"); + return BT_ERR_INTERNAL_ERROR; + } + int ret = BT_ERR_INTERNAL_ERROR; + if (client_) { + ret = client_->SetNotifyCharacteristic(*character, enable); + } + return ret; +} + +int32_t FfiClientDevice::SetCharacteristicChangeIndication(NativeBLECharacteristic characteristic, bool enable) +{ + GattCharacteristic *character = GetGattcCharacteristic(client_, characteristic); + if (character == nullptr) { + HILOGE("character is nullptr"); + return BT_ERR_INTERNAL_ERROR; + } + int ret = BT_ERR_INTERNAL_ERROR; + if (client_) { + ret = client_->SetIndicateCharacteristic(*character, enable); + } + return ret; +} + +int32_t FfiClientDevice::RegisterBleGattClientDeviceObserver(int32_t callbackType, void (*callback)()) +{ + if (callbackType == BLE_CHARACTERISTIC_CHANGE) { + auto bleCharacteristicChangeFunc = + CJLambda::Create(reinterpret_cast(callback)); + if (!bleCharacteristicChangeFunc) { + HILOGD("Register bleCharacteristicChange event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterBLECharacteristicChangeFunc(bleCharacteristicChangeFunc); + return BT_NO_ERROR; + } + + if (callbackType == BLE_CONNECTION_STATE_CHANGE) { + auto bleConnectionStateChangeFunc = + CJLambda::Create(reinterpret_cast(callback)); + if (!bleConnectionStateChangeFunc) { + HILOGD("Register bleConnectionStateChange event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterBLEConnectionStateChangeFunc(bleConnectionStateChangeFunc); + return BT_NO_ERROR; + } + + if (callbackType == CLIENT_BLE_MTU_CHANGE) { + auto bleMtuChangeFunc = CJLambda::Create(reinterpret_cast(callback)); + if (!bleMtuChangeFunc) { + HILOGD("Register bleMtuChange event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterBLEMtuChangeFunc(bleMtuChangeFunc); + return BT_NO_ERROR; + } + + return BT_NO_ERROR; +} } // namespace CJBluetoothBle } // namespace CJSystemapi } // namespace OHOS \ No newline at end of file diff --git a/frameworks/cj/ble/src/bluetooth_ble_common.cpp b/frameworks/cj/ble/src/bluetooth_ble_common.cpp index 25d93361..a68ec148 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_common.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_common.cpp @@ -15,11 +15,26 @@ #include "bluetooth_ble_common.h" +#include "bluetooth_ble_ffi.h" +#include "bluetooth_log.h" + #include +#include namespace OHOS { namespace CJSystemapi { namespace CJBluetoothBle { +using Bluetooth::BTConnectState; +using Bluetooth::GattCharacteristic; +using Bluetooth::GattDescriptor; +using Bluetooth::GattPermission; +using Bluetooth::GattService; +using namespace std; + +namespace { +std::string g_deviceAddr; +} // namespace + char *MallocCString(const std::string &origin) { if (origin.empty()) { @@ -35,7 +50,7 @@ char *MallocCString(const std::string &origin) CArrString Convert2CArrString(std::vector &tids) { - CArrString res{0}; + CArrString res{}; if (tids.empty()) { return res; } @@ -60,7 +75,7 @@ CArrString Convert2CArrString(std::vector &tids) CArrUI8 Convert2CArrUI8(std::vector vec) { - CArrUI8 res{0}; + CArrUI8 res{}; if (vec.empty()) { return res; } @@ -80,6 +95,242 @@ CArrUI8 Convert2CArrUI8(std::vector vec) return res; } +NativeBLEDescriptor ConvertBLEDescriptorToCJ(GattDescriptor &descriptor) +{ + NativeBLEDescriptor outDescriptor{}; + outDescriptor.descriptorUuid = MallocCString(descriptor.GetUuid().ToString()); + if (descriptor.GetCharacteristic() != nullptr) { + outDescriptor.characteristicUuid = MallocCString(descriptor.GetCharacteristic()->GetUuid().ToString()); + if (descriptor.GetCharacteristic()->GetService() != nullptr) { + outDescriptor.serviceUuid = + MallocCString(descriptor.GetCharacteristic()->GetService()->GetUuid().ToString()); + } + } + + size_t valueSize; + uint8_t *valueData = descriptor.GetValue(&valueSize).get(); + vector vec(valueData, valueData + valueSize); + outDescriptor.descriptorValue = Convert2CArrUI8(vec); + return outDescriptor; +} + +CArrBLEDescriptor Convert2CArrBLEDescriptor(vector &descriptors) +{ + CArrBLEDescriptor res{}; + if (descriptors.empty()) { + return res; + } + size_t size = descriptors.size(); + if (size == 0 || size > (std::numeric_limits::max() / sizeof(NativeBLEDescriptor))) { + return res; + } + res.head = static_cast(malloc(sizeof(NativeBLEDescriptor) * size)); + if (res.head == nullptr) { + return res; + } + size_t i = 0; + for (; i < descriptors.size(); i++) { + res.head[i] = ConvertBLEDescriptorToCJ(descriptors[i]); + } + res.size = static_cast(i); + return res; +} + +bool 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; +} + +NativeGattProperties ConvertGattPropertiesToCJ(int properties) +{ + NativeGattProperties outProperties{}; + outProperties.write = HasProperty(properties, GattCharacteristic::WRITE); + outProperties.writeNoResponse = HasProperty(properties, GattCharacteristic::WRITE_WITHOUT_RESPONSE); + outProperties.read = HasProperty(properties, GattCharacteristic::READ); + outProperties.notify = HasProperty(properties, GattCharacteristic::NOTIFY); + outProperties.indicate = HasProperty(properties, GattCharacteristic::INDICATE); + return outProperties; +} + +NativeBLECharacteristic ConvertBLECharacteristicToCJ(GattCharacteristic &characteristic) +{ + NativeBLECharacteristic outCharacteristic{}; + outCharacteristic.characteristicUuid = MallocCString(characteristic.GetUuid().ToString()); + if (characteristic.GetService() != nullptr) { + outCharacteristic.serviceUuid = MallocCString(characteristic.GetService()->GetUuid().ToString()); + } + + size_t valueSize = 0; + uint8_t *valueData = characteristic.GetValue(&valueSize).get(); + vector vec(valueData, valueData + valueSize); + outCharacteristic.characteristicValue = Convert2CArrUI8(vec); + + outCharacteristic.descriptors = Convert2CArrBLEDescriptor(characteristic.GetDescriptors()); + + outCharacteristic.properties = ConvertGattPropertiesToCJ(characteristic.GetProperties()); + + return outCharacteristic; +} + +CArrBLECharacteristic Convert2CArrBLECharacteristic(std::vector characteristics) +{ + CArrBLECharacteristic res{}; + if (characteristics.empty()) { + return res; + } + size_t size = characteristics.size(); + if (size == 0 || size > std::numeric_limits::max() / sizeof(NativeBLECharacteristic)) { + return res; + } + res.head = static_cast(malloc(sizeof(NativeBLECharacteristic) * size)); + if (res.head == nullptr) { + return res; + } + size_t i = 0; + for (; i < characteristics.size(); i++) { + res.head[i] = ConvertBLECharacteristicToCJ(characteristics[i]); + } + res.size = static_cast(i); + return res; +} + +NativeGattService ConvertGattServiceToCJ(GattService service) +{ + NativeGattService outService{}; + outService.serviceUuid = MallocCString(service.GetUuid().ToString()); + outService.isPrimary = service.IsPrimary(); + outService.characteristics = Convert2CArrBLECharacteristic(service.GetCharacteristics()); + vector services; + vector> srvs = service.GetIncludedServices(); + for (auto &srv : srvs) { + services.push_back(srv.get()); + } + outService.includeServices = Convert2CArrGattService(services); + return outService; +} + +CArrGattService Convert2CArrGattService(std::vector services) +{ + CArrGattService res{}; + if (services.empty()) { + return res; + } + size_t size = services.size(); + if (size == 0 || size > std::numeric_limits::max() / sizeof(NativeGattService)) { + return res; + } + res.head = static_cast(malloc(sizeof(NativeGattService) * services.size())); + if (res.head == nullptr) { + return res; + } + size_t i = 0; + for (; i < services.size(); i++) { + res.head[i] = ConvertGattServiceToCJ(services[i]); + } + res.size = static_cast(i); + return res; +} + +std::string GetGattClientDeviceId() +{ + return g_deviceAddr; +} + +uint16_t ConvertGattPermissions(const NativeGattPermission &nativePermissions) +{ + uint16_t permissions = 0; + if (nativePermissions.readable) { + permissions |= static_cast(GattPermission::READABLE); + } + if (nativePermissions.writeable) { + permissions |= static_cast(GattPermission::WRITEABLE); + } + if (nativePermissions.readEncrypted) { + permissions |= static_cast(GattPermission::READ_ENCRYPTED_MITM); + } + if (nativePermissions.writeEncrypted) { + permissions |= static_cast(GattPermission::WRITE_ENCRYPTED_MITM); + } + return permissions; +} + +uint16_t ConvertGattProperties(const NativeGattProperties &nativeProperties) +{ + uint16_t properties = 0; + if (nativeProperties.read) { + properties |= static_cast(GattCharacteristic::READ); + } + if (nativeProperties.write) { + properties |= static_cast(GattCharacteristic::WRITE); + } + if (nativeProperties.writeNoResponse) { + properties |= static_cast(GattCharacteristic::WRITE_WITHOUT_RESPONSE); + } + if (nativeProperties.notify) { + properties |= static_cast(GattCharacteristic::NOTIFY); + } + if (nativeProperties.indicate) { + properties |= static_cast(GattCharacteristic::INDICATE); + } + return properties; +} + +int GetProfileConnectionState(int state) +{ + int32_t profileConnectionState = ProfileConnectionState::STATE_DISCONNECTED; + switch (state) { + case static_cast(BTConnectState::CONNECTING): + HILOGD("STATE_CONNECTING(1)"); + profileConnectionState = ProfileConnectionState::STATE_CONNECTING; + break; + case static_cast(BTConnectState::CONNECTED): + HILOGD("STATE_CONNECTED(2)"); + profileConnectionState = ProfileConnectionState::STATE_CONNECTED; + break; + case static_cast(BTConnectState::DISCONNECTING): + HILOGD("STATE_DISCONNECTING(3)"); + profileConnectionState = ProfileConnectionState::STATE_DISCONNECTING; + break; + case static_cast(BTConnectState::DISCONNECTED): + HILOGD("STATE_DISCONNECTED(0)"); + profileConnectionState = ProfileConnectionState::STATE_DISCONNECTED; + break; + default: + break; + } + return profileConnectionState; +} + +void FreeNativeBLECharacteristic(NativeBLECharacteristic characteristic) +{ + free(characteristic.characteristicUuid); + free(characteristic.serviceUuid); + for (size_t i = 0; i < characteristic.descriptors.size; i++) { + NativeBLEDescriptor descriptor = characteristic.descriptors.head[i]; + FreeNativeBLEDescriptor(descriptor); + } + free(characteristic.descriptors.head); + characteristic.characteristicUuid = nullptr; + characteristic.serviceUuid = nullptr; + characteristic.descriptors.head = nullptr; +} + +void FreeNativeBLEDescriptor(NativeBLEDescriptor descriptor) +{ + free(descriptor.descriptorUuid); + free(descriptor.characteristicUuid); + free(descriptor.serviceUuid); + free(descriptor.descriptorValue.head); + descriptor.descriptorUuid = nullptr; + descriptor.characteristicUuid = nullptr; + descriptor.serviceUuid = nullptr; + descriptor.descriptorValue.head = nullptr; +} + } // namespace CJBluetoothBle } // namespace CJSystemapi } // namespace OHOS \ No newline at end of file diff --git a/frameworks/cj/ble/src/bluetooth_ble_ffi.cpp b/frameworks/cj/ble/src/bluetooth_ble_ffi.cpp index 9e3003f9..edb055d3 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_ffi.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_ffi.cpp @@ -15,6 +15,7 @@ #include "bluetooth_ble_ffi.h" +#include "bluetooth_ble_common.h" #include "bluetooth_ble_impl.h" #include "bluetooth_errorcode.h" #include "bluetooth_log.h" @@ -82,7 +83,7 @@ void FfiBluetoothBleStopAdvertising(int32_t *errCode) int32_t FfiBluetoothBleStartAdvertisingWithId(NativeAdvertisingParams advertisingParams, int32_t *errCode) { - int32_t id = BT_ERR_INTERNAL_ERROR; + int32_t id = -1; *errCode = BleImpl::StartAdvertising(advertisingParams, id); return id; } @@ -110,6 +111,236 @@ void FfiBluetoothBleOn(int32_t callbackType, void (*callback)(), int32_t *errCod *errCode = BleImpl::RegisterBleObserver(callbackType, callback); return; } + +void FfiBluetoothBleGattClientDeviceConnect(int64_t id, int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->Connect(); + return; +} + +void FfiBluetoothBleGattClientDeviceDisconnect(int64_t id, int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->Disconnect(); + return; +} + +void FfiBluetoothBleGattClientDeviceClose(int64_t id, int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->Close(); + return; +} + +char *FfiBluetoothBleGattClientDeviceGetDeviceName(int64_t id, int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return nullptr; + } + auto name = clientDevice->GetDeviceName(errCode); + return MallocCString(name); +} + +CArrGattService FfiBluetoothBleGattClientDeviceGetServices(int64_t id, int32_t *errCode) +{ + CArrGattService service{}; + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return service; + } + *errCode = clientDevice->GetServices(service); + return service; +} + +void FfiBluetoothBleGattClientDeviceReadCharacteristicValue(int64_t id, NativeBLECharacteristic characteristic, + void (*callback)(), int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->ReadCharacteristicValue(characteristic, callback); + return; +} + +void FfiBluetoothBleGattClientDeviceReadDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, void (*callback)(), + int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->ReadDescriptorValue(descriptor, callback); + return; +} + +void FfiBluetoothBleGattClientDeviceWriteCharacteristicValue(int64_t id, NativeBLECharacteristic characteristic, + int32_t writeType, void (*callback)(), int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->WriteCharacteristicValue(characteristic, writeType, callback); + return; +} + +void FfiBluetoothBleGattClientDeviceWriteDescriptorValue(int64_t id, NativeBLEDescriptor descriptor, void (*callback)(), + int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->WriteDescriptorValue(descriptor, callback); + return; +} + +void FfiBluetoothBleGattClientDeviceGetRssiValue(int64_t id, void (*callback)(), int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->GetRssiValue(callback); + return; +} + +void FfiBluetoothBleGattClientDeviceSetBLEMtuSize(int64_t id, int32_t mut, int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->SetBLEMtuSize(mut); + return; +} + +void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeNotification(int64_t id, + NativeBLECharacteristic characteristic, + bool enable, int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->SetCharacteristicChangeNotification(characteristic, enable); + return; +} + +void FfiBluetoothBleGattClientDeviceSetCharacteristicChangeIndication(int64_t id, + NativeBLECharacteristic characteristic, + bool enable, int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->SetCharacteristicChangeIndication(characteristic, enable); + return; +} + +void FfiBluetoothBleGattClientDeviceOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errCode) +{ + auto clientDevice = FFIData::GetData(id); + if (clientDevice == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = clientDevice->RegisterBleGattClientDeviceObserver(callbackType, callback); + return; +} + +void FfiBluetoothBleGattServerAddService(int64_t id, NativeGattService service, int32_t *errCode) +{ + auto gattServer = FFIData::GetData(id); + if (gattServer == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = gattServer->AddService(service); + return; +} + +void FfiBluetoothBleGattServerRemoveService(int64_t id, char *serviceUuid, int32_t *errCode) +{ + auto gattServer = FFIData::GetData(id); + if (gattServer == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = gattServer->RemoveService(serviceUuid); + return; +} + +void FfiBluetoothBleGattServerClose(int64_t id, int32_t *errCode) +{ + auto gattServer = FFIData::GetData(id); + if (gattServer == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = gattServer->Close(); + return; +} + +void FfiBluetoothBleGattServerNotifyCharacteristicChanged(int64_t id, char *deviceId, + NativeNotifyCharacteristic characteristic, int32_t *errCode) +{ + auto gattServer = FFIData::GetData(id); + if (gattServer == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = gattServer->NotifyCharacteristicChanged(deviceId, characteristic); + return; +} + +void FfiBluetoothBleGattServerSendResponse(int64_t id, NativeServerResponse serverResponse, int32_t *errCode) +{ + auto gattServer = FFIData::GetData(id); + if (gattServer == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = gattServer->SendResponse(serverResponse); + return; +} + +void FfiBluetoothBleGattServerOn(int64_t id, int32_t callbackType, void (*callback)(), int32_t *errCode) +{ + auto gattServer = FFIData::GetData(id); + if (gattServer == nullptr) { + *errCode = BT_ERR_INTERNAL_ERROR; + return; + } + *errCode = gattServer->RegisterBleGattServerObserver(callbackType, callback); + return; +} } } // namespace CJBluetoothBle } // namespace CJSystemapi diff --git a/frameworks/cj/ble/src/bluetooth_ble_ffiAdvertiseCallback.cpp b/frameworks/cj/ble/src/bluetooth_ble_ffiAdvertiseCallback.cpp index adffeda8..3e6557b6 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_ffiAdvertiseCallback.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_ffiAdvertiseCallback.cpp @@ -20,7 +20,9 @@ namespace CJSystemapi { namespace CJBluetoothBle { using Bluetooth::AdvertisingState; -FfiBluetoothBleAdvertiseCallback::FfiBluetoothBleAdvertiseCallback() {} +FfiBluetoothBleAdvertiseCallback::FfiBluetoothBleAdvertiseCallback() +{ +} std::shared_ptr FfiBluetoothBleAdvertiseCallback::GetInstance(void) { diff --git a/frameworks/cj/ble/src/bluetooth_ble_ffiCentralManagerCallback.cpp b/frameworks/cj/ble/src/bluetooth_ble_ffiCentralManagerCallback.cpp index 61a3db47..193cc2ab 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_ffiCentralManagerCallback.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_ffiCentralManagerCallback.cpp @@ -20,7 +20,9 @@ namespace OHOS { namespace CJSystemapi { namespace CJBluetoothBle { -FfiBluetoothBleCentralManagerCallback::FfiBluetoothBleCentralManagerCallback() {} +FfiBluetoothBleCentralManagerCallback::FfiBluetoothBleCentralManagerCallback() +{ +} FfiBluetoothBleCentralManagerCallback &FfiBluetoothBleCentralManagerCallback::GetInstance(void) { @@ -33,36 +35,41 @@ void FfiBluetoothBleCentralManagerCallback::RegisterBLEDeviceFindFunc(std::funct bleDeviceFindFunc = cjCallback; } -void FfiBluetoothBleCentralManagerCallback::OnBleBatchScanResultsEvent(const std::vector &results) +void FfiBluetoothBleCentralManagerCallback::OnScanCallback(const BleScanResult &result) { if (bleDeviceFindFunc == nullptr) { return; } CArrScanResult outResults{}; - if (results.empty()) { - return; - } - size_t size = results.size(); - if (size == 0 || size > std::numeric_limits::max() / sizeof(NativeScanResult)) { + outResults.size = 1; + NativeScanResult *resultValue = static_cast(malloc(sizeof(NativeScanResult) * outResults.size)); + if (resultValue == nullptr) { return; } - outResults.head = static_cast(malloc(sizeof(NativeScanResult) * results.size())); - if (outResults.head == nullptr) { - return; - } - size_t i = 0; - for (; i < results.size(); i++) { - BleScanResult bleScanResult = results[i]; - NativeScanResult result{}; - result.deviceId = MallocCString(bleScanResult.GetPeripheralDevice().GetDeviceAddr()); - result.rssi = bleScanResult.GetRssi(); - result.data = Convert2CArrUI8(bleScanResult.GetPayload()); - result.deviceName = MallocCString(bleScanResult.GetName()); - result.connectable = bleScanResult.IsConnectable(); - outResults.head[i] = result; + for (int i = 0; i < outResults.size; i++) { + BleScanResult bleScanResult = result; + NativeScanResult nativeResult{}; + nativeResult.deviceId = MallocCString(bleScanResult.GetPeripheralDevice().GetDeviceAddr()); + nativeResult.rssi = bleScanResult.GetRssi(); + nativeResult.data = Convert2CArrUI8(bleScanResult.GetPayload()); + nativeResult.deviceName = MallocCString(bleScanResult.GetName()); + nativeResult.connectable = bleScanResult.IsConnectable(); + resultValue[i] = nativeResult; } - outResults.size = static_cast(i); + outResults.head = resultValue; bleDeviceFindFunc(outResults); + + for (int i = 0; i < outResults.size; i++) { + NativeScanResult nativeResult = outResults.head[i]; + free(nativeResult.deviceId); + free(nativeResult.data.head); + free(nativeResult.deviceName); + nativeResult.deviceId = nullptr; + nativeResult.data.head = nullptr; + nativeResult.deviceName = nullptr; + } + free(resultValue); + resultValue = nullptr; return; } diff --git a/frameworks/cj/ble/src/bluetooth_ble_ffiGattClientCallback.cpp b/frameworks/cj/ble/src/bluetooth_ble_ffiGattClientCallback.cpp index 1e783fb8..c157153f 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_ffiGattClientCallback.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_ffiGattClientCallback.cpp @@ -15,11 +15,158 @@ #include "bluetooth_ble_clientDevice.h" +#include "bluetooth_ble_common.h" +#include "bluetooth_log.h" + namespace OHOS { namespace CJSystemapi { namespace CJBluetoothBle { -FfiGattClientCallback::FfiGattClientCallback() {} +FfiGattClientCallback::FfiGattClientCallback() +{ +} + +void FfiGattClientCallback::OnReadRemoteRssiValueResult(int rssi, int status) +{ + if (getRssiValueFunc != nullptr) { + RetDataI32 res{}; + res.code = status; + res.data = rssi; + getRssiValueFunc(res); + } +} + +void FfiGattClientCallback::OnCharacteristicChanged(const GattCharacteristic &characteristic) +{ + NativeBLECharacteristic outCharacteristic{}; + + GattCharacteristic character_ = const_cast(characteristic); + + outCharacteristic.characteristicUuid = MallocCString(character_.GetUuid().ToString().c_str()); + if (character_.GetService() != nullptr) { + outCharacteristic.serviceUuid = MallocCString(character_.GetService()->GetUuid().ToString().c_str()); + } + size_t valueSize = 0; + uint8_t *valueData = character_.GetValue(&valueSize).get(); + + CArrUI8 arr{}; + arr.head = valueData; + arr.size = valueSize; + outCharacteristic.characteristicValue = arr; + + outCharacteristic.properties = ConvertGattPropertiesToCJ(character_.GetProperties()); + outCharacteristic.descriptors = Convert2CArrBLEDescriptor(character_.GetDescriptors()); + + if (bleCharacteristicChangeFunc != nullptr) { + bleCharacteristicChangeFunc(outCharacteristic); + } + FreeNativeBLECharacteristic(outCharacteristic); +} + +void FfiGattClientCallback::OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) +{ + HILOGI("UUID: %{public}s, ret: %{public}d", characteristic.GetUuid().ToString().c_str(), ret); + RetNativeBLECharacteristic res{}; + res.data = ConvertBLECharacteristicToCJ(const_cast(characteristic)); + res.code = ret; + if (readCharacteristicFunc != nullptr) { + readCharacteristicFunc(res); + } + FreeNativeBLECharacteristic(res.data); +} + +void FfiGattClientCallback::OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret) +{ + HILOGI("UUID: %{public}s, ret: %{public}d", characteristic.GetUuid().ToString().c_str(), ret); + if (writeCharacteristicFunc != nullptr) { + writeCharacteristicFunc(ret); + } +} + +void FfiGattClientCallback::OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) +{ + HILOGI("UUID: %{public}s, ret: %{public}d", descriptor.GetUuid().ToString().c_str(), ret); + RetNativeBLEDescriptor res{}; + res.data = ConvertBLEDescriptorToCJ(const_cast(descriptor)); + res.code = ret; + if (readDescriptorFunc != nullptr) { + readDescriptorFunc(res); + } + FreeNativeBLEDescriptor(res.data); +} + +void FfiGattClientCallback::OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret) +{ + HILOGI("UUID: %{public}s, ret: %{public}d", descriptor.GetUuid().ToString().c_str(), ret); + if (writeDescriptorValueFunc != nullptr) { + writeDescriptorValueFunc(ret); + } +} + +void FfiGattClientCallback::OnConnectionStateChanged(int connectionState, int ret) +{ + int connectState_ = connectionState; + + NativeBLEConnectionChangeState outState{}; + + outState.deviceId = MallocCString(deviceAddr_.c_str()); + outState.state = GetProfileConnectionState(connectState_); + + if (bleConnectionStateChangeFunc != nullptr) { + bleConnectionStateChangeFunc(outState); + } + free(outState.deviceId); + outState.deviceId = nullptr; +} + +void FfiGattClientCallback::OnMtuUpdate(int mtu, int ret) +{ + if (bleMtuChangeFunc != nullptr) { + bleMtuChangeFunc(mtu); + } +} + +void FfiGattClientCallback::RegisterBLECharacteristicChangeFunc(std::function cjCallback) +{ + bleCharacteristicChangeFunc = cjCallback; +} + +void FfiGattClientCallback::RegisterBLEConnectionStateChangeFunc( + std::function cjCallback) +{ + bleConnectionStateChangeFunc = cjCallback; +} + +void FfiGattClientCallback::RegisterBLEMtuChangeFunc(std::function cjCallback) +{ + bleMtuChangeFunc = cjCallback; +} + +void FfiGattClientCallback::RegisterGetRemoteRssicallback(std::function cjCallback) +{ + getRssiValueFunc = cjCallback; +} + +void FfiGattClientCallback::RegisterReadCharacteristicCallback( + std::function cjCallback) +{ + readCharacteristicFunc = cjCallback; +} + +void FfiGattClientCallback::RegisterReadDescriptorCallback(std::function cjCallback) +{ + readDescriptorFunc = cjCallback; +} +void FfiGattClientCallback::RegisterWriteCharacteristicCallback(std::function cjCallback) +{ + writeCharacteristicFunc = cjCallback; +} + +void FfiGattClientCallback::RegisterWriteDescriptorCallback(std::function cjCallback) +{ + writeDescriptorValueFunc = cjCallback; +} + } // namespace CJBluetoothBle } // namespace CJSystemapi } // namespace OHOS \ No newline at end of file diff --git a/frameworks/cj/ble/src/bluetooth_ble_ffiGattServerCallback.cpp b/frameworks/cj/ble/src/bluetooth_ble_ffiGattServerCallback.cpp index 9f6fe401..4b22ef47 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_ffiGattServerCallback.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_ffiGattServerCallback.cpp @@ -15,11 +15,241 @@ #include "bluetooth_ble_gattServer.h" +#include "bluetooth_ble_common.h" +#include "bluetooth_gatt_characteristic.h" +#include "bluetooth_gatt_descriptor.h" +#include "bluetooth_log.h" + namespace OHOS { namespace CJSystemapi { namespace CJBluetoothBle { +using Bluetooth::BTConnectState; +using Bluetooth::GattCharacteristic; +using Bluetooth::GattDescriptor; + +FfiGattServerCallback::FfiGattServerCallback() +{ +} + +void FfiGattServerCallback::OnCharacteristicReadRequest(const BluetoothRemoteDevice &device, + GattCharacteristic &characteristic, int requestId) +{ + int transId_ = requestId; + std::string deviceAddr_ = device.GetDeviceAddr(); + + NativeCharacteristicReadRequest request{}; + request.deviceId = MallocCString(deviceAddr_.c_str()); + request.transId = transId_; + request.offset = 0; + request.characteristicUuid = MallocCString(characteristic.GetUuid().ToString().c_str()); + if (characteristic.GetService() != nullptr) { + request.serviceUuid = MallocCString(characteristic.GetService()->GetUuid().ToString().c_str()); + } + + if (characteristicReadFunc != nullptr) { + characteristicReadFunc(request); + } + free(request.deviceId); + free(request.characteristicUuid); + free(request.serviceUuid); + request.deviceId = nullptr; + request.characteristicUuid = nullptr; + request.serviceUuid = nullptr; +} + +void FfiGattServerCallback::OnCharacteristicWriteRequest(const BluetoothRemoteDevice &device, + GattCharacteristic &characteristic, int requestId) +{ + int transId_ = requestId; + std::string deviceAddr_ = device.GetDeviceAddr(); + + NativeCharacteristicWriteRequest request{}; + request.deviceId = MallocCString(deviceAddr_.c_str()); + request.transId = transId_; + request.offset = 0; + request.isPrepared = false; + request.needRsp = characteristic.GetWriteType() == GattCharacteristic::WriteType::DEFAULT; + + size_t valueSize; + uint8_t *valueData = characteristic.GetValue(&valueSize).get(); + + CArrUI8 arr{}; + arr.head = valueData; + arr.size = valueSize; + request.value = arr; + + request.characteristicUuid = MallocCString(characteristic.GetUuid().ToString().c_str()); + if (characteristic.GetService() != nullptr) { + request.serviceUuid = MallocCString(characteristic.GetService()->GetUuid().ToString().c_str()); + } + if (characteristicWriteFunc != nullptr) { + characteristicWriteFunc(request); + } + free(request.deviceId); + free(request.characteristicUuid); + free(request.serviceUuid); + request.deviceId = nullptr; + request.characteristicUuid = nullptr; + request.serviceUuid = nullptr; +} + +void FfiGattServerCallback::OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, + int requestId) +{ + int transId_ = requestId; + std::string deviceAddr_ = device.GetDeviceAddr(); + + NativeDescriptorReadRequest request{}; + + request.deviceId = MallocCString(deviceAddr_.c_str()); + request.transId = transId_; + request.offset = 0; + request.descriptorUuid = MallocCString(descriptor.GetUuid().ToString().c_str()); + request.characteristicUuid = MallocCString(descriptor.GetCharacteristic()->GetUuid().ToString().c_str()); + + if (descriptor.GetCharacteristic()->GetService() != nullptr) { + request.serviceUuid = MallocCString(descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str()); + } + + if (descriptorReadFunc != nullptr) { + descriptorReadFunc(request); + } + free(request.deviceId); + free(request.descriptorUuid); + free(request.characteristicUuid); + free(request.serviceUuid); + request.deviceId = nullptr; + request.descriptorUuid = nullptr; + request.characteristicUuid = nullptr; + request.serviceUuid = nullptr; +} + +void FfiGattServerCallback::OnDescriptorWriteRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, + int requestId) +{ + int transId_ = requestId; + std::string deviceAddr_ = device.GetDeviceAddr(); + + NativeDescriptorWriteRequest request{}; + + request.deviceId = MallocCString(deviceAddr_.c_str()); + request.transId = transId_; + request.offset = 0; + request.isPrepared = false; + request.needRsp = true; + + size_t valueSize; + uint8_t *valueData = descriptor.GetValue(&valueSize).get(); + + CArrUI8 arr{}; + arr.head = valueData; + arr.size = valueSize; + request.value = arr; + + request.descriptorUuid = MallocCString(descriptor.GetUuid().ToString().c_str()); + if (descriptor.GetCharacteristic() != nullptr) { + request.characteristicUuid = MallocCString(descriptor.GetCharacteristic()->GetUuid().ToString().c_str()); + if (descriptor.GetCharacteristic()->GetService() != nullptr) { + request.serviceUuid = + MallocCString(descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str()); + } + } + + if (descriptorWriteFunc != nullptr) { + descriptorWriteFunc(request); + } + free(request.deviceId); + free(request.descriptorUuid); + free(request.characteristicUuid); + free(request.serviceUuid); + request.deviceId = nullptr; + request.descriptorUuid = nullptr; + request.characteristicUuid = nullptr; + request.serviceUuid = nullptr; +} + +void FfiGattServerCallback::OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) +{ + std::lock_guard lock(FfiGattServer::deviceListMutex_); + if (state == static_cast(BTConnectState::CONNECTED)) { + HILOGI("connected"); + bool hasAddr = false; + for (auto it = FfiGattServer::deviceList_.begin(); it != FfiGattServer::deviceList_.end(); ++it) { + if (*it == device.GetDeviceAddr()) { + hasAddr = true; + break; + } + } + if (!hasAddr) { + HILOGI("add devices"); + FfiGattServer::deviceList_.push_back(device.GetDeviceAddr()); + } + } else if (state == static_cast(BTConnectState::DISCONNECTED)) { + HILOGI("disconnected"); + for (auto it = FfiGattServer::deviceList_.begin(); it != FfiGattServer::deviceList_.end(); ++it) { + if (*it == device.GetDeviceAddr()) { + HILOGI("romove device"); + FfiGattServer::deviceList_.erase(it); + break; + } + } + } + + std::string deviceAddr_ = device.GetDeviceAddr(); + int connectState_ = GetProfileConnectionState(state); + + NativeBLEConnectionChangeState changeState{}; + + changeState.deviceId = MallocCString(deviceAddr_.c_str()); + changeState.state = connectState_; + + if (connectionStateChangeFunc != nullptr) { + connectionStateChangeFunc(changeState); + } + free(changeState.deviceId); + changeState.deviceId = nullptr; +} + +void FfiGattServerCallback::OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) +{ + if (bleMtuChangeFunc != nullptr) { + bleMtuChangeFunc(mtu); + } +} + +void FfiGattServerCallback::RegisterCharacteristicReadFunc( + std::function cjCallback) +{ + characteristicReadFunc = cjCallback; +} + +void FfiGattServerCallback::RegisterCharacteristicWriteFunc( + std::function cjCallback) +{ + characteristicWriteFunc = cjCallback; +} + +void FfiGattServerCallback::RegisterDescriptorReadFunc(std::function cjCallback) +{ + descriptorReadFunc = cjCallback; +} + +void FfiGattServerCallback::RegisterDescriptorWriteFunc(std::function cjCallback) +{ + descriptorWriteFunc = cjCallback; +} + +void FfiGattServerCallback::RegisterConnectionStateChangeFunc( + std::function cjCallback) +{ + connectionStateChangeFunc = cjCallback; +} + +void FfiGattServerCallback::RegisterBLEMtuChangeFunc(std::function cjCallback) +{ + bleMtuChangeFunc = cjCallback; +} -FfiGattServerCallback::FfiGattServerCallback() {} } // namespace CJBluetoothBle } // namespace CJSystemapi } // namespace OHOS \ No newline at end of file diff --git a/frameworks/cj/ble/src/bluetooth_ble_gattServer.cpp b/frameworks/cj/ble/src/bluetooth_ble_gattServer.cpp index 7c985c05..159b6552 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_gattServer.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_gattServer.cpp @@ -15,9 +15,238 @@ #include "bluetooth_ble_gattServer.h" +#include "bluetooth_ble_common.h" +#include "bluetooth_errorcode.h" +#include "bluetooth_gatt_service.h" +#include "bluetooth_log.h" +#include "cj_lambda.h" + namespace OHOS { namespace CJSystemapi { namespace CJBluetoothBle { +using Bluetooth::BT_ERR_INTERNAL_ERROR; +using Bluetooth::BT_ERR_INVALID_PARAM; +using Bluetooth::BT_NO_ERROR; +using Bluetooth::BTTransport; +using Bluetooth::GattServiceType; +using Bluetooth::UUID; + +static int CheckGattsAddService(NativeGattService service, std::unique_ptr &outService) +{ + GattServiceType type = service.isPrimary ? GattServiceType::PRIMARY : GattServiceType::SECONDARY; + outService = std::make_unique(UUID::FromString(service.serviceUuid), type); + + CArrBLECharacteristic characteristics = service.characteristics; + for (int64_t i = 0; i < characteristics.size; i++) { + NativeBLECharacteristic nativeCharacter = characteristics.head[i]; + + NativeGattPermission permissions = { + .readable = true, + .writeable = true, + }; + int charPermissions = ConvertGattPermissions(permissions); + int charProperties = ConvertGattProperties(nativeCharacter.properties); + GattCharacteristic character(UUID::FromString(nativeCharacter.characteristicUuid), charPermissions, + charProperties); + character.SetValue(nativeCharacter.characteristicValue.head, nativeCharacter.characteristicValue.size); + + CArrBLEDescriptor descriptors = nativeCharacter.descriptors; + for (int64_t j = 0; j < descriptors.size; j++) { + NativeBLEDescriptor nativeDescriptor = descriptors.head[j]; + GattDescriptor descriptor(UUID::FromString(nativeDescriptor.descriptorUuid), 0); + descriptor.SetValue(nativeDescriptor.descriptorValue.head, nativeDescriptor.descriptorValue.size); + character.AddDescriptor(descriptor); + } + outService->AddCharacteristic(character); + } + return BT_NO_ERROR; +} + +int32_t FfiGattServer::AddService(NativeGattService service) +{ + std::unique_ptr gattService{nullptr}; + auto status = CheckGattsAddService(service, gattService); + if (status != BT_NO_ERROR) { + return status; + } + if (server_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + int ret = server_->AddService(*gattService); + return ret; +} + +int32_t FfiGattServer::RemoveService(std::string serviceUuid) +{ + UUID realServiceUuid = UUID::FromString(serviceUuid); + int ret = BT_ERR_INTERNAL_ERROR; + if (server_ == nullptr) { + return ret; + } + auto primaryService = server_->GetService(realServiceUuid, true); + if (primaryService.has_value()) { + ret = server_->RemoveGattService(*primaryService); + if (ret != BT_NO_ERROR) { + return ret; + } + } + auto secondService = server_->GetService(realServiceUuid, false); + if (secondService.has_value()) { + ret = server_->RemoveGattService(*secondService); + if (ret != BT_NO_ERROR) { + return ret; + } + } + return ret; +} + +int32_t FfiGattServer::Close() +{ + if (server_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + return server_->Close(); +} + +static GattCharacteristic *GetGattCharacteristic(const std::shared_ptr &server, const UUID &serviceUuid, + const UUID &characterUuid) +{ + auto service = server->GetService(serviceUuid, true); + if (!service.has_value()) { + service = server->GetService(serviceUuid, false); + } + if (!service.has_value()) { + HILOGE("not found service uuid: %{public}s", serviceUuid.ToString().c_str()); + return nullptr; + } + GattCharacteristic *character = service.value().get().GetCharacteristic(characterUuid); + return character; +} + +int32_t FfiGattServer::NotifyCharacteristicChanged(std::string deviceId, NativeNotifyCharacteristic characteristic) +{ + if (server_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + auto character = GetGattCharacteristic(server_, UUID::FromString(characteristic.serviceUuid), + UUID::FromString(characteristic.characteristicUuid)); + if (character == nullptr) { + return BT_ERR_INVALID_PARAM; + } + character->SetValue(characteristic.characteristicValue.head, characteristic.characteristicValue.size); + BluetoothRemoteDevice remoteDevice(deviceId, BTTransport::ADAPTER_BLE); + int ret = server_->NotifyCharacteristicChanged(remoteDevice, *character, characteristic.confirm); + return ret; +} + +int32_t FfiGattServer::SendResponse(NativeServerResponse serverResponse) +{ + BluetoothRemoteDevice remoteDevice(serverResponse.deviceId, BTTransport::ADAPTER_BLE); + if (server_ == nullptr) { + return BT_ERR_INTERNAL_ERROR; + } + return server_->SendResponse(remoteDevice, serverResponse.transId, serverResponse.status, serverResponse.offset, + serverResponse.value.head, static_cast(serverResponse.value.size)); + ; +} + +int32_t FfiGattServer::CreateCharacteristicReadFunc(void (*callback)()) +{ + auto characteristicReadFunc = + CJLambda::Create(reinterpret_cast(callback)); + if (!characteristicReadFunc) { + HILOGD("Register characteristicRead event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterCharacteristicReadFunc(characteristicReadFunc); + return BT_NO_ERROR; +} + +int32_t FfiGattServer::CreateCharacteristicWriteFunc(void (*callback)()) +{ + auto characteristicWriteFunc = + CJLambda::Create(reinterpret_cast(callback)); + if (!characteristicWriteFunc) { + HILOGD("Register characteristicWrite event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterCharacteristicWriteFunc(characteristicWriteFunc); + return BT_NO_ERROR; +} + +int32_t FfiGattServer::CreateDescriptorReadFunc(void (*callback)()) +{ + auto descriptorReadFunc = CJLambda::Create(reinterpret_cast(callback)); + if (!descriptorReadFunc) { + HILOGD("Register descriptorRead event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterDescriptorReadFunc(descriptorReadFunc); + return BT_NO_ERROR; +} + +int32_t FfiGattServer::CreateRegisterDescriptorWriteFunc(void (*callback)()) +{ + auto descriptorWriteFunc = CJLambda::Create(reinterpret_cast(callback)); + if (!descriptorWriteFunc) { + HILOGD("Register descriptorWrite event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterDescriptorWriteFunc(descriptorWriteFunc); + return BT_NO_ERROR; +} + +int32_t FfiGattServer::CreateConnectionStateChangeFunc(void (*callback)()) +{ + auto connectionStateChangeFunc = + CJLambda::Create(reinterpret_cast(callback)); + if (!connectionStateChangeFunc) { + HILOGD("Register connectionStateChange event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterConnectionStateChangeFunc(connectionStateChangeFunc); + return BT_NO_ERROR; +} + +int32_t FfiGattServer::CreateBLEMtuChangeFunc(void (*callback)()) +{ + auto bleMtuChangeFunc = CJLambda::Create(reinterpret_cast(callback)); + if (!bleMtuChangeFunc) { + HILOGD("Register bleMtuChange event failed"); + return BT_ERR_INTERNAL_ERROR; + } + callback_->RegisterBLEMtuChangeFunc(bleMtuChangeFunc); + return BT_NO_ERROR; +} + +int32_t FfiGattServer::RegisterBleGattServerObserver(int32_t callbackType, void (*callback)()) +{ + if (callbackType == CHARACTERISTIC_READ) { + return CreateCharacteristicReadFunc(callback); + } + + if (callbackType == CHARACTERISTIC_WRITE) { + return CreateCharacteristicWriteFunc(callback); + } + + if (callbackType == DESCRIPTOR_READ) { + return CreateDescriptorReadFunc(callback); + } + + if (callbackType == DESCRIPTOR_WRITE) { + return CreateRegisterDescriptorWriteFunc(callback); + } + + if (callbackType == CONNECTION_STATE_CHANGE) { + return CreateConnectionStateChangeFunc(callback); + } + + if (callbackType == SERVER_BLE_MTU_CHANGE) { + return CreateBLEMtuChangeFunc(callback); + } + + return BT_NO_ERROR; +} std::vector FfiGattServer::deviceList_; std::mutex FfiGattServer::deviceListMutex_; diff --git a/frameworks/cj/ble/src/bluetooth_ble_impl.cpp b/frameworks/cj/ble/src/bluetooth_ble_impl.cpp index a926ce73..164468c1 100644 --- a/frameworks/cj/ble/src/bluetooth_ble_impl.cpp +++ b/frameworks/cj/ble/src/bluetooth_ble_impl.cpp @@ -43,9 +43,6 @@ using Bluetooth::BT_ERR_INTERNAL_ERROR; using Bluetooth::BT_ERR_INVALID_PARAM; using Bluetooth::BT_NO_ERROR; -const int32_t REGISTER_BLE_FIND_DEVICE_TYPE = 0; -const int32_t REGISTER_BLE_ADVERTISING_STATE_INFO_TYPE = 1; - namespace { using Bluetooth::BleCentralManager; @@ -81,7 +78,7 @@ int32_t BleImpl::CreateGattClientDevice(std::string deviceId, FfiClientDevice *& return BT_NO_ERROR; } -int32_t BleImpl::GetConnectedBleDevices(CArrString res) +int32_t BleImpl::GetConnectedBleDevices(CArrString &res) { std::lock_guard lock(FfiGattServer::deviceListMutex_); std::vector devicesList = FfiGattServer::deviceList_; -- Gitee