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