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_connection_observer"
17 #endif
18 
19 #include "napi_bluetooth_connection_observer.h"
20 
21 #include "bluetooth_host.h"
22 #include "bluetooth_log.h"
23 #include "bluetooth_utils.h"
24 
25 #include "napi/native_api.h"
26 #include "napi/native_node_api.h"
27 #include "napi_bluetooth_connection.h"
28 #include "napi_event_subscribe_module.h"
29 
30 #include <uv.h>
31 
32 namespace OHOS {
33 namespace Bluetooth {
NapiBluetoothConnectionObserver()34 NapiBluetoothConnectionObserver::NapiBluetoothConnectionObserver()
35     : eventSubscribe_({REGISTER_DEVICE_FIND_TYPE,
36         REGISTER_DISCOVERY_RESULT_TYPE,
37         REGISTER_PIN_REQUEST_TYPE},
38         BT_MODULE_NAME)
39 {}
40 
OnDiscoveryStateChanged(int status)41 void NapiBluetoothConnectionObserver::OnDiscoveryStateChanged(int status)
42 {
43     switch (status) {
44         case DISCOVERY_STARTED:
45             HILOGD("DISCOVERY_STARTED(1)");
46             break;
47         case DISCOVERYING:
48             HILOGD("DISCOVERYING(2)");
49             break;
50         case DISCOVERY_STOPED:
51             HILOGD("DISCOVERY_STOPED(3)");
52             break;
53         default:
54             HILOGE("invaild status is %{public}d", status);
55             break;
56     }
57 }
58 
OnDiscoveryResult(const BluetoothRemoteDevice & device,int rssi,const std::string deviceName,int deviceClass)59 void NapiBluetoothConnectionObserver::OnDiscoveryResult(
60     const BluetoothRemoteDevice &device, int rssi, const std::string deviceName, int deviceClass)
61 {
62     OnDiscoveryResultCallBack(device);
63     OnDiscoveryResultCallBack(device, rssi, deviceName, deviceClass);
64 }
65 
OnPairRequested(const BluetoothRemoteDevice & device)66 void NapiBluetoothConnectionObserver::OnPairRequested(const BluetoothRemoteDevice &device)
67 {
68     HILOGD("start");
69     BluetoothRemoteDevice remoteDevice;
70     if (device.GetTransportType() == BT_TRANSPORT_BREDR) {
71         remoteDevice = BluetoothHost::GetDefaultHost().GetRemoteDevice(device.GetDeviceAddr(), BT_TRANSPORT_BREDR);
72     } else if (device.GetTransportType() == BT_TRANSPORT_BLE) {
73         remoteDevice = BluetoothHost::GetDefaultHost().GetRemoteDevice(device.GetDeviceAddr(), BT_TRANSPORT_BLE);
74     }
75     remoteDevice.PairRequestReply(true);
76 }
77 
OnPairConfirmed(const BluetoothRemoteDevice & device,int reqType,int number)78 void NapiBluetoothConnectionObserver::OnPairConfirmed(const BluetoothRemoteDevice &device, int reqType, int number)
79 {
80     HILOGD("OnPairConfirmed");
81     std::shared_ptr<PairConfirmedCallBackInfo> pairConfirmInfo =
82         std::make_shared<PairConfirmedCallBackInfo>(number, reqType, device.GetDeviceAddr());
83     OnPairConfirmedCallBack(pairConfirmInfo);
84 }
85 
OnScanModeChanged(int mode)86 void NapiBluetoothConnectionObserver::OnScanModeChanged(int mode)
87 {
88     HILOGI("mode is %{public}d", mode);
89 }
90 
OnDeviceNameChanged(const std::string & deviceName)91 void NapiBluetoothConnectionObserver::OnDeviceNameChanged(const std::string &deviceName)
92 {
93     HILOGI("name is %{public}s", deviceName.c_str());
94 }
95 
OnDeviceAddrChanged(const std::string & address)96 void NapiBluetoothConnectionObserver::OnDeviceAddrChanged(const std::string &address)
97 {
98     HILOGI("address is %{public}s", GetEncryptAddr(address).c_str());
99 }
100 
OnPairConfirmedCallBack(const std::shared_ptr<PairConfirmedCallBackInfo> & pairConfirmInfo)101 void NapiBluetoothConnectionObserver::OnPairConfirmedCallBack(
102     const std::shared_ptr<PairConfirmedCallBackInfo> &pairConfirmInfo)
103 {
104     CHECK_AND_RETURN_LOG(pairConfirmInfo, "pairConfirmInfo is nullptr");
105     HILOGI("Addr: %{public}s", GetEncryptAddr(pairConfirmInfo->deviceAddr).c_str());
106 
107     auto nativeObject = std::make_shared<NapiNativePinRequiredParam>(pairConfirmInfo);
108     eventSubscribe_.PublishEvent(REGISTER_PIN_REQUEST_TYPE, nativeObject);
109 }
110 
OnDiscoveryResultCallBack(const BluetoothRemoteDevice & device)111 void NapiBluetoothConnectionObserver::OnDiscoveryResultCallBack(const BluetoothRemoteDevice &device)
112 {
113     std::shared_ptr<BluetoothRemoteDevice> remoteDevice = std::make_shared<BluetoothRemoteDevice>(device);
114     auto nativeObject = std::make_shared<NapiNativeDiscoveryResultArray>(remoteDevice);
115     eventSubscribe_.PublishEvent(REGISTER_DEVICE_FIND_TYPE, nativeObject);
116 }
117 
OnDiscoveryResultCallBack(const BluetoothRemoteDevice & device,int rssi,const std::string & deviceName,int deviceClass)118 void NapiBluetoothConnectionObserver::OnDiscoveryResultCallBack(
119     const BluetoothRemoteDevice &device, int rssi, const std::string &deviceName, int deviceClass)
120 {
121     std::shared_ptr<BluetoothRemoteDevice> remoteDevice = std::make_shared<BluetoothRemoteDevice>(device);
122     auto nativeObject =
123         std::make_shared<NapiNativeDiscoveryInfoResultArray>(remoteDevice, rssi, deviceName, deviceClass);
124     eventSubscribe_.PublishEvent(REGISTER_DISCOVERY_RESULT_TYPE, nativeObject);
125 }
126 }  // namespace Bluetooth
127 }  // namespace OHOS
128