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_GATTSERVER_H 17 #define BLUETOOTH_BLE_GATTSERVER_H 18 19 #include "bluetooth_ble_ffi.h" 20 #include "bluetooth_gatt_server.h" 21 #include "cj_common_ffi.h" 22 #include "ffi_remote_data.h" 23 24 namespace OHOS { 25 namespace CJSystemapi { 26 namespace CJBluetoothBle { 27 using Bluetooth::BluetoothRemoteDevice; 28 using Bluetooth::GattCharacteristic; 29 using Bluetooth::GattDescriptor; 30 using Bluetooth::GattServer; 31 using Bluetooth::GattServerCallback; 32 using Bluetooth::GattService; 33 34 class FfiGattServerCallback : public GattServerCallback { 35 public: 36 void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) override; OnServiceAdded(GattService service,int ret)37 void OnServiceAdded(GattService service, int ret) override{}; 38 void OnCharacteristicReadRequest(const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, 39 int requestId) override; 40 void OnCharacteristicWriteRequest(const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, 41 int requestId) override; 42 void OnDescriptorReadRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, 43 int requestId) override; 44 void OnDescriptorWriteRequest(const BluetoothRemoteDevice &device, GattDescriptor &descriptor, 45 int requestId) override; 46 void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) override; OnNotificationCharacteristicChanged(const BluetoothRemoteDevice & device,int result)47 void OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int result) override{}; OnConnectionParameterChanged(const BluetoothRemoteDevice & device,int interval,int latency,int timeout,int status)48 void OnConnectionParameterChanged(const BluetoothRemoteDevice &device, int interval, int latency, int timeout, 49 int status) override{}; 50 51 void RegisterCharacteristicReadFunc(std::function<void(NativeCharacteristicReadRequest)> cjCallback); 52 void RegisterCharacteristicWriteFunc(std::function<void(NativeCharacteristicWriteRequest)> cjCallback); 53 void RegisterDescriptorReadFunc(std::function<void(NativeDescriptorReadRequest)> cjCallback); 54 void RegisterDescriptorWriteFunc(std::function<void(NativeDescriptorWriteRequest)> cjCallback); 55 void RegisterConnectionStateChangeFunc(std::function<void(NativeBLEConnectionChangeState)> cjCallback); 56 void RegisterBLEMtuChangeFunc(std::function<void(int32_t)> cjCallback); 57 58 FfiGattServerCallback(); 59 ~FfiGattServerCallback() override = default; 60 61 private: 62 std::function<void(NativeCharacteristicReadRequest)> characteristicReadFunc{nullptr}; 63 std::function<void(NativeCharacteristicWriteRequest)> characteristicWriteFunc{nullptr}; 64 std::function<void(NativeDescriptorReadRequest)> descriptorReadFunc{nullptr}; 65 std::function<void(NativeDescriptorWriteRequest)> descriptorWriteFunc{nullptr}; 66 std::function<void(NativeBLEConnectionChangeState)> connectionStateChangeFunc{nullptr}; 67 std::function<void(int32_t)> bleMtuChangeFunc{nullptr}; 68 }; 69 70 class FfiGattServer : public OHOS::FFI::FFIData { DECL_TYPE(FfiGattServer,OHOS::FFI::FFIData)71 DECL_TYPE(FfiGattServer, OHOS::FFI::FFIData) 72 public: 73 explicit FfiGattServer() 74 { 75 callback_ = std::make_shared<FfiGattServerCallback>(); 76 std::shared_ptr<GattServerCallback> tmp = std::static_pointer_cast<GattServerCallback>(callback_); 77 server_ = GattServer::CreateInstance(tmp); 78 }; 79 int32_t AddService(NativeGattService service); 80 int32_t RemoveService(std::string serviceUuid); 81 int32_t Close(); 82 int32_t NotifyCharacteristicChanged(std::string deviceId, NativeNotifyCharacteristic characteristic); 83 int32_t SendResponse(NativeServerResponse serverResponse); 84 int32_t RegisterBleGattServerObserver(int32_t callbackType, void (*callback)()); 85 GetServer()86 std::shared_ptr<GattServer> &GetServer() 87 { 88 return server_; 89 } GetCallback()90 std::shared_ptr<FfiGattServerCallback> GetCallback() 91 { 92 return callback_; 93 } 94 static std::vector<std::string> deviceList_; 95 static std::mutex deviceListMutex_; 96 97 private: 98 std::shared_ptr<GattServer> server_ = nullptr; 99 std::shared_ptr<FfiGattServerCallback> callback_; 100 101 int32_t CreateCharacteristicReadFunc(void (*callback)()); 102 int32_t CreateCharacteristicWriteFunc(void (*callback)()); 103 int32_t CreateDescriptorReadFunc(void (*callback)()); 104 int32_t CreateRegisterDescriptorWriteFunc(void (*callback)()); 105 int32_t CreateConnectionStateChangeFunc(void (*callback)()); 106 int32_t CreateBLEMtuChangeFunc(void (*callback)()); 107 }; 108 } // namespace CJBluetoothBle 109 } // namespace CJSystemapi 110 } // namespace OHOS 111 112 #endif // BLUETOOTH_BLE_GATTSERVER_H