1 /*
2 * Copyright (C) 2021-2022 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 <map>
16 #include <memory>
17 #include <string>
18
19 #include "bluetooth_log.h"
20 #include "bluetooth_utils.h"
21 #include "napi_bluetooth_remote_device_observer.h"
22 #include "napi_bluetooth_utils.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
UvQueueWorkOnPairStatusChanged(uv_work_t * work,std::pair<std::string,int> & data)26 void NapiBluetoothRemoteDeviceObserver::UvQueueWorkOnPairStatusChanged(
27 uv_work_t *work, std::pair<std::string, int> &data)
28 {
29 HILOGI("OnPairStatusChanged uv_work_t start");
30
31 if (work == nullptr) {
32 HILOGE("work is null");
33 return;
34 }
35 auto callbackData = (AfterWorkCallbackData<NapiBluetoothRemoteDeviceObserver,
36 decltype(&NapiBluetoothRemoteDeviceObserver::UvQueueWorkOnPairStatusChanged),
37 std::pair<std::string, int>> *)work->data;
38 if (callbackData == nullptr) {
39 HILOGE("callbackData is null");
40 return;
41 }
42
43 napi_value result = 0;
44 napi_value callback = 0;
45 napi_value undefined = 0;
46 napi_value callResult = 0;
47
48 napi_get_undefined(callbackData->env, &undefined);
49 HILOGD("UvQueueWorkOnPairStatusChanged Status is %{public}d", callbackData->data.second);
50 napi_create_object(callbackData->env, &result);
51 napi_value device = 0;
52 napi_create_string_utf8(
53 callbackData->env, callbackData->data.first.data(), callbackData->data.first.size(), &device);
54 napi_set_named_property(callbackData->env, result, "deviceId", device);
55 napi_value bondState = 0;
56 napi_create_int32(callbackData->env, callbackData->data.second, &bondState);
57 napi_set_named_property(callbackData->env, result, "state", bondState);
58 napi_get_reference_value(callbackData->env, callbackData->callback, &callback);
59 napi_call_function(callbackData->env, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
60 }
61
OnPairStatusChanged(const BluetoothRemoteDevice & device,int status)62 void NapiBluetoothRemoteDeviceObserver::OnPairStatusChanged(const BluetoothRemoteDevice &device, int status)
63 {
64 HILOGI("NapiBluetoothRemoteDeviceObserver::OnPairStatusChanged called");
65 std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> observers = GetObserver();
66 if (!observers[REGISTER_BONE_STATE_TYPE]) {
67 HILOGW("NapiBluetoothHostObserver::OnPairStatusChanged: This callback is not registered by ability.");
68 return;
69 }
70 HILOGD("NapiBluetoothRemoteDeviceObserver::OnPairStatusChanged: %{public}s is registered by ability",
71 REGISTER_BONE_STATE_TYPE.c_str());
72 std::shared_ptr<BluetoothCallbackInfo> callbackInfo = observers[REGISTER_BONE_STATE_TYPE];
73 uv_loop_s *loop = nullptr;
74 napi_get_uv_event_loop(callbackInfo->env_, &loop);
75 if (loop == nullptr) {
76 HILOGE("loop instance is nullptr");
77 return;
78 }
79
80 int bondStatus = 0;
81 DealPairStatus(status, bondStatus);
82
83 auto callbackData = new (std::nothrow) AfterWorkCallbackData<NapiBluetoothRemoteDeviceObserver,
84 decltype(&NapiBluetoothRemoteDeviceObserver::UvQueueWorkOnPairStatusChanged), std::pair<std::string, int>>();
85 if (callbackData == nullptr) {
86 HILOGE("new callbackData failed");
87 return;
88 }
89
90 std::string deviceAddr = device.GetDeviceAddr();
91 callbackData->object = this;
92 callbackData->function = &NapiBluetoothRemoteDeviceObserver::UvQueueWorkOnPairStatusChanged;
93 callbackData->env = callbackInfo->env_;
94 callbackData->callback = callbackInfo->callback_;
95 callbackData->data = std::pair<std::string, int>(deviceAddr, bondStatus);
96
97 uv_work_t *work = new (std::nothrow) uv_work_t;
98 if (work == nullptr) {
99 HILOGE("new work failed");
100 delete callbackData;
101 callbackData = nullptr;
102 return;
103 }
104
105 work->data = (void *)callbackData;
106
107 int ret = uv_queue_work(
108 loop, work, [](uv_work_t *work) {}, AfterWorkCallback<decltype(callbackData)>);
109 if (ret != 0) {
110 delete callbackData;
111 callbackData = nullptr;
112 delete work;
113 work = nullptr;
114 }
115 }
116
OnRemoteUuidChanged(const BluetoothRemoteDevice & device,const std::vector<ParcelUuid> & uuids)117 void NapiBluetoothRemoteDeviceObserver ::OnRemoteUuidChanged(
118 const BluetoothRemoteDevice &device, const std::vector<ParcelUuid> &uuids)
119 {
120 HILOGI("NapiBluetoothRemoteDeviceObserver::OnRemoteUuidChanged called");
121 }
122
OnRemoteNameChanged(const BluetoothRemoteDevice & device,const std::string & deviceName)123 void NapiBluetoothRemoteDeviceObserver ::OnRemoteNameChanged(
124 const BluetoothRemoteDevice &device, const std::string &deviceName)
125 {
126 HILOGI("NapiBluetoothRemoteDeviceObserver::OnRemoteNameChanged called, address is %{public}s, deviceName is "
127 "%{public}s",
128 GetEncryptAddr(device.GetDeviceAddr()).c_str(),
129 deviceName.c_str());
130 }
131
OnRemoteAliasChanged(const BluetoothRemoteDevice & device,const std::string & alias)132 void NapiBluetoothRemoteDeviceObserver ::OnRemoteAliasChanged(
133 const BluetoothRemoteDevice &device, const std::string &alias)
134 {
135 HILOGI("NapiBluetoothRemoteDeviceObserver::OnRemoteAliasChanged called, address is %{public}s, alias is "
136 "%{public}s",
137 GetEncryptAddr(device.GetDeviceAddr()).c_str(),
138 alias.c_str());
139 }
140
OnRemoteCodChanged(const BluetoothRemoteDevice & device,const BluetoothDeviceClass & cod)141 void NapiBluetoothRemoteDeviceObserver ::OnRemoteCodChanged(
142 const BluetoothRemoteDevice &device, const BluetoothDeviceClass &cod)
143 {
144 HILOGI("NapiBluetoothRemoteDeviceObserver::OnRemoteCodChanged called, address is %{public}s, cod is "
145 "%{public}d",
146 GetEncryptAddr(device.GetDeviceAddr()).c_str(),
147 cod.GetClassOfDevice());
148 }
149
OnRemoteBatteryLevelChanged(const BluetoothRemoteDevice & device,int batteryLevel)150 void NapiBluetoothRemoteDeviceObserver ::OnRemoteBatteryLevelChanged(
151 const BluetoothRemoteDevice &device, int batteryLevel)
152 {
153 HILOGI(
154 "NapiBluetoothRemoteDeviceObserver::OnRemoteBatteryLevelChanged called, address is %{public}s, batteryLevel is "
155 "%{public}d",
156 GetEncryptAddr(device.GetDeviceAddr()).c_str(),
157 batteryLevel);
158 }
159
OnReadRemoteRssiEvent(const BluetoothRemoteDevice & device,int rssi,int status)160 void NapiBluetoothRemoteDeviceObserver ::OnReadRemoteRssiEvent(
161 const BluetoothRemoteDevice &device, int rssi, int status)
162 {
163 HILOGI("NapiBluetoothRemoteDeviceObserver::OnReadRemoteRssiEvent called, address is %{public}s, rssi is "
164 "%{public}d, status is %{public}d",
165 GetEncryptAddr(device.GetDeviceAddr()).c_str(),
166 rssi,
167 status);
168 std::shared_ptr<GattGetRssiValueCallbackInfo> callbackInfo = GetRssiValueCallbackInfo();
169 if (callbackInfo == nullptr) {
170 return;
171 }
172 std::unique_lock<std::mutex> lock(callbackInfo->mutexRssi);
173 if (status == 0) {
174 callbackInfo->promise.errorCode = CODE_SUCCESS;
175 callbackInfo->rssi = rssi;
176 } else {
177 callbackInfo->promise.errorCode = CODE_FAILED;
178 }
179 callbackInfo->cvfull.notify_all();
180 }
181
DealPairStatus(const int & status,int & boneStatus)182 void NapiBluetoothRemoteDeviceObserver::DealPairStatus(const int &status, int &boneStatus)
183 {
184 HILOGI("NapiBluetoothRemoteDeviceObserver::DealPairStatus called, status is %{public}d", status);
185 switch (status) {
186 case PAIR_NONE:
187 boneStatus = BondState::BOND_STATE_INVALID;
188 break;
189 case PAIR_PAIRING:
190 boneStatus = BondState::BOND_STATE_BONDING;
191 break;
192 case PAIR_PAIRED:
193 boneStatus = BondState::BOND_STATE_BONDED;
194 break;
195 default:
196 break;
197 }
198 }
199 } // namespace Bluetooth
200 } // namespace OHOS