1 /* 2 * Copyright (c) 2024 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef BLUETOOTH_BLE_CLIENTDEVICE_H 17 #define BLUETOOTH_BLE_CLIENTDEVICE_H 18 19 #include "bluetooth_ble_ffi.h" 20 #include "bluetooth_def.h" 21 #include "bluetooth_gatt_client.h" 22 #include "bluetooth_remote_device.h" 23 #include "cj_common_ffi.h" 24 #include "ffi_remote_data.h" 25 26 namespace OHOS { 27 namespace CJSystemapi { 28 namespace CJBluetoothBle { 29 using Bluetooth::BluetoothRemoteDevice; 30 using Bluetooth::GattCharacteristic; 31 using Bluetooth::GattClient; 32 using Bluetooth::GattClientCallback; 33 using Bluetooth::GattDescriptor; 34 using Bluetooth::INVALID_MAC_ADDRESS; 35 36 class FfiGattClientCallback : public GattClientCallback { 37 public: 38 void OnConnectionStateChanged(int connectionState, int ret) override; 39 void OnCharacteristicChanged(const GattCharacteristic &characteristic) override; 40 void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) override; 41 void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret) override; 42 void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) override; 43 void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret) override; 44 void OnMtuUpdate(int mtu, int ret) override; OnServicesDiscovered(int status)45 void OnServicesDiscovered(int status) override{}; OnConnectionParameterChanged(int interval,int latency,int timeout,int status)46 void OnConnectionParameterChanged(int interval, int latency, int timeout, int status) override{}; OnSetNotifyCharacteristic(const GattCharacteristic & characteristic,int status)47 void OnSetNotifyCharacteristic(const GattCharacteristic &characteristic, int status) override{}; 48 void OnReadRemoteRssiValueResult(int rssi, int status) override; 49 50 void RegisterBLECharacteristicChangeFunc(std::function<void(NativeBLECharacteristic)> cjCallback); 51 void RegisterBLEConnectionStateChangeFunc(std::function<void(NativeBLEConnectionChangeState)> cjCallback); 52 void RegisterBLEMtuChangeFunc(std::function<void(int32_t)> cjCallback); 53 void RegisterGetRemoteRssicallback(std::function<void(RetDataI32)> cjCallback); 54 void RegisterReadCharacteristicCallback(std::function<void(RetNativeBLECharacteristic)> cjCallback); 55 void RegisterReadDescriptorCallback(std::function<void(RetNativeBLEDescriptor)> cjCallback); 56 void RegisterWriteCharacteristicCallback(std::function<void(int32_t)> cjCallback); 57 void RegisterWriteDescriptorCallback(std::function<void(int32_t)> cjCallback); 58 59 FfiGattClientCallback(); 60 ~FfiGattClientCallback() override = default; 61 62 private: 63 friend class FfiClientDevice; 64 std::string deviceAddr_ = INVALID_MAC_ADDRESS; 65 66 int remoteRssi{-1}; 67 68 std::function<void(NativeBLECharacteristic)> bleCharacteristicChangeFunc{nullptr}; 69 std::function<void(NativeBLEConnectionChangeState)> bleConnectionStateChangeFunc{nullptr}; 70 std::function<void(int32_t)> bleMtuChangeFunc{nullptr}; 71 std::function<void(RetDataI32)> getRssiValueFunc{nullptr}; 72 std::function<void(RetNativeBLECharacteristic)> readCharacteristicFunc{nullptr}; 73 std::function<void(RetNativeBLEDescriptor)> readDescriptorFunc{nullptr}; 74 std::function<void(int32_t)> writeCharacteristicFunc{nullptr}; 75 std::function<void(int32_t)> writeDescriptorValueFunc{nullptr}; 76 }; 77 78 class FfiClientDevice : public OHOS::FFI::FFIData { DECL_TYPE(FfiClientDevice,OHOS::FFI::FFIData)79 DECL_TYPE(FfiClientDevice, OHOS::FFI::FFIData) 80 public: 81 explicit FfiClientDevice(std::string deviceId) 82 { 83 device_ = std::make_shared<BluetoothRemoteDevice>(deviceId, 1); 84 client_ = std::make_shared<GattClient>(*device_); 85 client_->Init(); 86 callback_ = std::make_shared<FfiGattClientCallback>(); 87 callback_->deviceAddr_ = deviceId; 88 }; 89 int32_t Connect(); 90 int32_t Disconnect(); 91 int32_t Close(); 92 std::string GetDeviceName(int32_t *errCode); 93 int32_t GetServices(CArrGattService &service); 94 int32_t ReadCharacteristicValue(NativeBLECharacteristic &characteristic, void (*callback)()); 95 int32_t ReadDescriptorValue(NativeBLEDescriptor &inputDescriptor, void (*callback)()); 96 int32_t WriteCharacteristicValue(NativeBLECharacteristic characteristic, int32_t writeType, void (*callback)()); 97 int32_t WriteDescriptorValue(NativeBLEDescriptor inputDescriptor, void (*callback)()); 98 int32_t GetRssiValue(void (*callback)()); 99 int32_t SetBLEMtuSize(int32_t mut); 100 int32_t SetCharacteristicChangeNotification(NativeBLECharacteristic characteristic, bool enable); 101 int32_t SetCharacteristicChangeIndication(NativeBLECharacteristic characteristic, bool enable); 102 103 int32_t RegisterBleGattClientDeviceObserver(int32_t callbackType, void (*callback)()); 104 105 private: 106 std::shared_ptr<GattClient> client_ = nullptr; 107 std::shared_ptr<FfiGattClientCallback> callback_; 108 std::shared_ptr<BluetoothRemoteDevice> device_ = nullptr; 109 }; 110 } // namespace CJBluetoothBle 111 } // namespace CJSystemapi 112 } // namespace OHOS 113 114 #endif // BLUETOOTH_BLE_CLIENTDEVICE_H