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
OnConnectionStateUpdate(const BluetoothRemoteDevice & device,int state)60 void NapiGattServerCallback::OnConnectionStateUpdate(const BluetoothRemoteDevice &device, int state)
61 {
62 HILOGI("enter, state: %{public}d, remote device address: %{public}s", state, GET_ENCRYPT_ADDR(device));
63 std::lock_guard<std::mutex> lock(NapiGattServer::deviceListMutex_);
64 if (state == static_cast<int>(BTConnectState::CONNECTED)) {
65 HILOGI("connected");
66 bool hasAddr = false;
67 for (auto it = NapiGattServer::deviceList_.begin(); it != NapiGattServer::deviceList_.end(); ++it) {
68 if (*it == device.GetDeviceAddr()) {
69 hasAddr = true;
70 break;
71 }
72 }
73 if (!hasAddr) {
74 HILOGI("add devices");
75 NapiGattServer::deviceList_.push_back(device.GetDeviceAddr());
76 }
77 } else if (state == static_cast<int>(BTConnectState::DISCONNECTED)) {
78 HILOGI("disconnected");
79 for (auto it = NapiGattServer::deviceList_.begin(); it != NapiGattServer::deviceList_.end(); ++it) {
80 if (*it == device.GetDeviceAddr()) {
81 HILOGI("romove device");
82 NapiGattServer::deviceList_.erase(it);
83 break;
84 }
85 }
86 }
87
88 auto nativeObject =
89 std::make_shared<NapiNativeBleConnectionStateChangeParam>(device.GetDeviceAddr(), state);
90 eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_CONNECT_STATE_CHANGE, nativeObject);
91 }
92
OnDescriptorWriteRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)93 void NapiGattServerCallback::OnDescriptorWriteRequest(const BluetoothRemoteDevice &device,
94 GattDescriptor &descriptor, int requestId)
95 {
96 HILOGI("enter, remote device address: %{public}s, requestId: %{public}d",
97 GET_ENCRYPT_ADDR(device), requestId);
98 auto nativeObject =
99 std::make_shared<NapiNativeGattsDescriptorWriteRequest>(requestId, device.GetDeviceAddr(), descriptor);
100 eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_WRITE, nativeObject);
101 }
102
OnDescriptorReadRequest(const BluetoothRemoteDevice & device,GattDescriptor & descriptor,int requestId)103 void NapiGattServerCallback::OnDescriptorReadRequest(const BluetoothRemoteDevice &device,
104 GattDescriptor &descriptor, int requestId)
105 {
106 HILOGI("enter, remote device address: %{public}s, requestId: %{public}d",
107 GET_ENCRYPT_ADDR(device), requestId);
108 auto nativeObject =
109 std::make_shared<NapiNativeGattsDescriptorReadRequest>(requestId, device.GetDeviceAddr(), descriptor);
110 eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_DESCRIPTOR_READ, nativeObject);
111 }
112
OnMtuUpdate(const BluetoothRemoteDevice & device,int mtu)113 void NapiGattServerCallback::OnMtuUpdate(const BluetoothRemoteDevice &device, int mtu)
114 {
115 HILOGI("enter, remote device address: %{public}s, mtu: %{public}d",
116 GET_ENCRYPT_ADDR(device), mtu);
117 auto nativeObject = std::make_shared<NapiNativeInt>(mtu);
118 eventSubscribe_.PublishEvent(STR_BT_GATT_SERVER_CALLBACK_MTU_CHANGE, nativeObject);
119 }
120
OnNotificationCharacteristicChanged(const BluetoothRemoteDevice & device,int ret)121 void NapiGattServerCallback::OnNotificationCharacteristicChanged(const BluetoothRemoteDevice &device, int ret)
122 {
123 HILOGI("ret: %{public}d", ret);
124 auto napiRet = std::make_shared<NapiNativeInt>(ret);
125 AsyncWorkCallFunction(asyncWorkMap_, NapiAsyncType::GATT_SERVER_NOTIFY_CHARACTERISTIC, napiRet, ret);
126 }
127 } // namespace Bluetooth
128 } // namespace OHOS
129