• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2023 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 LOG_TAG
16 #define LOG_TAG "bt_napi_gatt_server_callback"
17 #endif
18 
19 #include <uv.h>
20 #include "bluetooth_log.h"
21 #include "bluetooth_utils.h"
22 #include "napi_bluetooth_gatt_server.h"
23 #include "napi_bluetooth_gatt_server_callback.h"
24 #include "napi_event_subscribe_module.h"
25 
26 namespace OHOS {
27 namespace Bluetooth {
28 using namespace std;
29 
NapiGattServerCallback()30 NapiGattServerCallback::NapiGattServerCallback()
31     : eventSubscribe_({STR_BT_GATT_SERVER_CALLBACK_CHARACTERISTIC_READ,
32         STR_BT_GATT_SERVER_CALLBACK_CHARACTERISTIC_WRITE,
33         STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_READ,
34         STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE,
35         STR_BT_GATT_SERVER_CALLBACK_CONNECT_STATE_CHANGE,
36         STR_BT_GATT_SERVER_CALLBACK_MTU_CHANGE},
37         BT_MODULE_NAME)
38 {}
39 
OnCharacteristicReadRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)40 void NapiGattServerCallback::OnCharacteristicReadRequest(
41     const BluetoothRemoteDevice &device, GattCharacteristic &characteristic, int requestId)
42 {
43     HILOGI("enter, remote device address: %{public}s, requestId: %{public}d",
44         GET_ENCRYPT_ADDR(device), requestId);
45     auto nativeObject =
46         std::make_shared<NapiNativeGattsCharacterReadRequest>(requestId, device.GetDeviceAddr(), characteristic);
47     eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_CHARACTERISTIC_READ, nativeObject);
48 }
49 
OnCharacteristicWriteRequest(const BluetoothRemoteDevice & device,GattCharacteristic & characteristic,int requestId)50 void NapiGattServerCallback::OnCharacteristicWriteRequest(const BluetoothRemoteDevice &device,
51     GattCharacteristic &characteristic, int requestId)
52 {
53     HILOGI("enter, remote device address: %{public}s, requestId: %{public}d",
54         GET_ENCRYPT_ADDR(device), requestId);
55     auto nativeObject =
56         std::make_shared<NapiNativeGattsCharacterWriteRequest>(requestId, device.GetDeviceAddr(), characteristic);
57     eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_CHARACTERISTIC_WRITE, nativeObject);
58 }
59 
OnConnectionStateUpdateWithReason(const BluetoothRemoteDevice & device,int state,int disconnectReason)60 void NapiGattServerCallback::OnConnectionStateUpdateWithReason(
61     const BluetoothRemoteDevice &device, int state, int disconnectReason)
62 {
63     HILOGI("enter, state: %{public}d, remote device address: %{public}s", state, GET_ENCRYPT_ADDR(device));
64     std::lock_guard<std::mutex> lock(NapiGattServer::deviceListMutex_);
65     if (state == static_cast<int>(BTConnectState::CONNECTED)) {
66         HILOGI("connected");
67         bool hasAddr = false;
68         for (auto it = NapiGattServer::deviceList_.begin(); it != NapiGattServer::deviceList_.end(); ++it) {
69             if (*it == device.GetDeviceAddr()) {
70                 hasAddr = true;
71                 break;
72             }
73         }
74         if (!hasAddr) {
75             HILOGI("add devices");
76             NapiGattServer::deviceList_.push_back(device.GetDeviceAddr());
77         }
78         auto nativeObject = std::make_shared<NapiNativeBleConnectionStateChangeParam>(
79             device.GetDeviceAddr(), state);
80         eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_CONNECT_STATE_CHANGE, nativeObject);
81     } else if (state == static_cast<int>(BTConnectState::DISCONNECTED)) {
82         HILOGI("disconnected");
83         for (auto it = NapiGattServer::deviceList_.begin(); it != NapiGattServer::deviceList_.end(); ++it) {
84             if (*it == device.GetDeviceAddr()) {
85                 HILOGI("romove device");
86                 NapiGattServer::deviceList_.erase(it);
87                 break;
88             }
89         }
90         auto nativeObject = std::make_shared<NapiNativeBleConnectionStateChangeParam>(
91             device.GetDeviceAddr(), state, 0, true, disconnectReason);
92         eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_CONNECT_STATE_CHANGE, nativeObject);
93     }
94 }
95 
OnDescriptorWriteRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)96 void NapiGattServerCallback::OnDescriptorWriteRequest(const BluetoothRemoteDevice &device,
97     GattDescriptor &descriptor, int requestId)
98 {
99     HILOGI("enter, remote device address: %{public}s, requestId: %{public}d",
100         GET_ENCRYPT_ADDR(device), requestId);
101     auto nativeObject =
102         std::make_shared<NapiNativeGattsDescriptorWriteRequest>(requestId, device.GetDeviceAddr(), descriptor);
103     eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE, nativeObject);
104 }
105 
OnDescriptorReadRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)106 void NapiGattServerCallback::OnDescriptorReadRequest(const BluetoothRemoteDevice &device,
107     GattDescriptor &descriptor, int requestId)
108 {
109     HILOGI("enter, remote device address: %{public}s, requestId: %{public}d",
110         GET_ENCRYPT_ADDR(device), requestId);
111     auto nativeObject =
112         std::make_shared<NapiNativeGattsDescriptorReadRequest>(requestId, device.GetDeviceAddr(), descriptor);
113     eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_READ, nativeObject);
114 }
115 
OnMtuUpdate(const BluetoothRemoteDevice & device,int mtu)116 void NapiGattServerCallback::OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu)
117 {
118     HILOGI("enter, remote device address: %{public}s, mtu: %{public}d",
119         GET_ENCRYPT_ADDR(device), mtu);
120     auto nativeObject = std::make_shared<NapiNativeInt>(mtu);
121     eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_MTU_CHANGE, nativeObject);
122 }
123 
OnNotificationCharacteristicChanged(const BluetoothRemoteDevice & device,int ret)124 void NapiGattServerCallback::OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int ret)
125 {
126     HILOGI("ret: %{public}d", ret);
127     auto napiRet = std::make_shared<NapiNativeInt>(ret);
128     AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_SERVER_NOTIFY_CHARACTERISTIC, napiRet, ret);
129 }
130 } // namespace Bluetooth
131 } // namespace OHOS
132