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_bluetooth_utils.h" 21 22 namespace OHOS { 23 namespace Bluetooth { 24 25 const std::string STR_BT_GATT_CLIENT_CALLBACK_BLE_CHARACTERISTIC_CHANGE = "BLECharacteristicChange"; 26 const std::string STR_BT_GATT_CLIENT_CALLBACK_BLE_CONNECTIION_STATE_CHANGE = "BLEConnectionStateChange"; 27 class NapiGattClient; 28 29 class NapiGattClientCallback : public GattClientCallback { 30 public: 31 void OnConnectionStateChanged(int connectionState, int ret) override; 32 void OnCharacteristicChanged(const GattCharacteristic &characteristic) override; 33 void OnCharacteristicReadResult(const GattCharacteristic &characteristic, int ret) override; OnCharacteristicWriteResult(const GattCharacteristic & characteristic,int ret)34 void OnCharacteristicWriteResult(const GattCharacteristic &characteristic, int ret) override {} 35 void OnDescriptorReadResult(const GattDescriptor &descriptor, int ret) override; OnDescriptorWriteResult(const GattDescriptor & descriptor,int ret)36 void OnDescriptorWriteResult(const GattDescriptor &descriptor, int ret) override {} OnMtuUpdate(int mtu,int ret)37 void OnMtuUpdate(int mtu, int ret) override {} 38 void OnServicesDiscovered(int status) override; OnConnectionParameterChanged(int interval,int latency,int timeout,int status)39 void OnConnectionParameterChanged(int interval, int latency, int timeout, int status) override {} OnSetNotifyCharacteristic(int status)40 void OnSetNotifyCharacteristic(int status) override {} 41 SetCallbackInfo(const std::string & type,std::shared_ptr<BluetoothCallbackInfo> callbackInfo)42 void SetCallbackInfo(const std::string &type, std::shared_ptr<BluetoothCallbackInfo> callbackInfo) 43 { 44 callbackInfos_[type] = callbackInfo; 45 } SetClient(NapiGattClient * client)46 void SetClient(NapiGattClient *client) 47 { 48 client_ = client; 49 } GetCallbackInfo(const std::string & type)50 std::shared_ptr<BluetoothCallbackInfo> GetCallbackInfo(const std::string &type) 51 { 52 return callbackInfos_[type]; 53 } 54 NapiGattClientCallback() = default; 55 virtual ~NapiGattClientCallback() = default; 56 static std::shared_mutex g_gattClientCallbackInfosMutex; 57 58 private: 59 std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> callbackInfos_ = {}; 60 NapiGattClient *client_ = nullptr; 61 }; 62 } // namespace Bluetooth 63 } // namespace OHOS 64 #endif /* NAPI_BLUETOOTH_GATT_CLIENT_CALLBACK_H_ */