1 /* 2 * Copyright (C) 2021 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 #ifndef NAPI_BLUETOOTH_GATT_CLIENT_CALLBACK_H_ 16 #define NAPI_BLUETOOTH_GATT_CLIENT_CALLBACK_H_ 17 18 #include <shared_mutex> 19 #include "bluetooth_gatt_client.h" 20 #include "napi_async_callback.h" 21 #include "napi_bluetooth_utils.h" 22 #include "napi_bluetooth_ble_utils.h" 23 24 namespace OHOS { 25 namespace Bluetooth { 26 27 const std::string STR_BT_GATT_CLIENT_CALLBACK_BLE_CHARACTERISTIC_CHANGE = "BLECharacteristicChange"; 28 const std::string STR_BT_GATT_CLIENT_CALLBACK_BLE_CONNECTIION_STATE_CHANGE = "BLEConnectionStateChange"; 29 const std::string STR_BT_GATT_CLIENT_CALLBACK_BLE_MTU_CHANGE = "BLEMtuChange"; 30 class NapiGattClient; 31 32 class NapiGattClientCallback : public GattClientCallback { 33 public: 34 void OnConnectionStateChanged(int connectionState, int ret) override; 35 void OnCharacteristicChanged(const GattCharacteristic &characteristic) override; 36 void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) override; 37 void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret) override; 38 void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) override; 39 void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret) override; 40 void OnMtuUpdate(int mtu, int ret) override; 41 void OnServicesDiscovered(int status) override; OnConnectionParameterChanged(int interval,int latency,int timeout,int status)42 void OnConnectionParameterChanged(int interval, int latency, int timeout, int status) override {} 43 void OnSetNotifyCharacteristic(const GattCharacteristic &characteristic, int status) override; 44 void OnReadRemoteRssiValueResult(int rssi, int status) override; SetCallbackInfo(const std::string & type,std::shared_ptr<BluetoothCallbackInfo> callbackInfo)45 void SetCallbackInfo(const std::string &type, std::shared_ptr<BluetoothCallbackInfo> callbackInfo) 46 { 47 callbackInfos_[type] = callbackInfo; 48 } SetClient(NapiGattClient * client)49 void SetClient(NapiGattClient *client) 50 { 51 client_ = client; 52 } GetCallbackInfo(const std::string & type)53 std::shared_ptr<BluetoothCallbackInfo> GetCallbackInfo(const std::string &type) 54 { 55 return callbackInfos_[type]; 56 } 57 NapiGattClientCallback() = default; 58 ~NapiGattClientCallback() override = default; 59 static std::shared_mutex g_gattClientCallbackInfosMutex; 60 61 NapiAsyncWorkMap asyncWorkMap_ {}; 62 napi_threadsafe_function onBleCharacterChangedThreadSafeFunc_ = nullptr; 63 64 private: 65 std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> callbackInfos_ = {}; 66 NapiGattClient *client_ = nullptr; 67 }; 68 } // namespace Bluetooth 69 } // namespace OHOS 70 #endif /* NAPI_BLUETOOTH_GATT_CLIENT_CALLBACK_H_ */