• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SERVER_CALLBACK_H_
16 #define NAPI_BLUETOOTH_GATT_SERVER_CALLBACK_H_
17 
18 #include "bluetooth_gatt_server.h"
19 #include "napi_bluetooth_utils.h"
20 #include "napi_bluetooth_ble_utils.h"
21 #include "napi_async_callback.h"
22 
23 namespace OHOS {
24 namespace Bluetooth {
25 const std::string STR_BT_GATT_SERVER_CALLBACK_CHARACTERISTIC_READ = "characteristicRead";
26 const std::string STR_BT_GATT_SERVER_CALLBACK_CHARACTERISTIC_WRITE = "characteristicWrite";
27 const std::string STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_READ = "descriptorRead";
28 const std::string STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE = "descriptorWrite";
29 
30 #ifdef BLUETOOTH_API_SINCE_10
31 const std::string STR_BT_GATT_SERVER_CALLBACK_CONNECT_STATE_CHANGE = "connectionStateChange";
32 #else
33 const std::string STR_BT_GATT_SERVER_CALLBACK_CONNECT_STATE_CHANGE = "connectStateChange";
34 #endif
35 const std::string STR_BT_GATT_SERVER_CALLBACK_MTU_CHANGE = "BLEMtuChange";
36 
37 class NapiGattServerCallback : public GattServerCallback {
38 public:
39     void OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state) override;
OnServiceAdded(GattService * Service,int ret)40     void OnServiceAdded(GattService *Service, int ret) override {}
41     void OnCharacteristicReadRequest(const BluetoothRemoteDevice &device,
42         GattCharacteristic &characteristic, int requestId) override;
43     void OnCharacteristicWriteRequest(const BluetoothRemoteDevice &device,
44         GattCharacteristic &characteristic, int requestId) override;
45     void OnDescriptorReadRequest(const BluetoothRemoteDevice &device,
46         GattDescriptor &descriptor, int requestId) override;
47     void OnDescriptorWriteRequest(const BluetoothRemoteDevice &device,
48         GattDescriptor &descriptor, int requestId) override;
49     void OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu) override;
50     void OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device,
51         int result) override;
OnConnectionParameterChanged(const BluetoothRemoteDevice & device,int interval,int latency,int timeout,int status)52     void OnConnectionParameterChanged(const BluetoothRemoteDevice &device,
53         int interval, int latency, int timeout, int status) override {}
54 
SetCallbackInfo(const std::string & type,std::shared_ptr<BluetoothCallbackInfo> callbackInfo)55     void SetCallbackInfo(const std::string &type, std::shared_ptr<BluetoothCallbackInfo> callbackInfo)
56     {
57         HILOGI("enter, type: %{public}s", type.c_str());
58         callbackInfos_[type] = callbackInfo;
59     }
60 
61     NapiAsyncWorkMap asyncWorkMap_ {};
62     NapiGattServerCallback() = default;
63     ~NapiGattServerCallback() override = default;
64 
65 private:
66     std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> callbackInfos_ = {};
67 };
68 }  // namespace Bluetooth
69 }  // namespace OHOS
70 #endif /* NAPI_BLUETOOTH_GATT_SERVER_CALLBACK_H_ */