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"
17 #endif
18 
19 #include "napi_bluetooth_connection.h"
20 
21 #include <set>
22 
23 #include "napi_bluetooth_connection_observer.h"
24 #include "napi_bluetooth_remote_device_observer.h"
25 #include "bluetooth_log.h"
26 #include "bluetooth_errorcode.h"
27 #include "napi_bluetooth_error.h"
28 #include "napi_async_work.h"
29 #include "napi_bluetooth_utils.h"
30 #include "parser/napi_parser_utils.h"
31 #include "hitrace_meter.h"
32 
33 namespace OHOS {
34 namespace Bluetooth {
35 std::shared_ptr<NapiBluetoothConnectionObserver> g_connectionObserver =
36     std::make_shared<NapiBluetoothConnectionObserver>();
37 std::shared_ptr<NapiBluetoothRemoteDeviceObserver> g_remoteDeviceObserver =
38     std::make_shared<NapiBluetoothRemoteDeviceObserver>();
39 std::mutex deviceMutex;
40 
DefineConnectionFunctions(napi_env env,napi_value exports)41 napi_value DefineConnectionFunctions(napi_env env, napi_value exports)
42 {
43     HILOGD("enter");
44     RegisterObserverToHost();
45     ConnectionPropertyValueInit(env, exports);
46     napi_property_descriptor desc[] = {
47         DECLARE_NAPI_FUNCTION("getBtConnectionState", GetBtConnectionState),
48 #ifdef BLUETOOTH_API_SINCE_10
49         DECLARE_NAPI_FUNCTION("pairDevice", PairDeviceAsync),
50         DECLARE_NAPI_FUNCTION("cancelPairedDevice", CancelPairedDeviceAsync),
51         DECLARE_NAPI_FUNCTION("getProfileConnectionState", GetProfileConnectionStateEx),
52 #else
53         DECLARE_NAPI_FUNCTION("pairDevice", PairDevice),
54         DECLARE_NAPI_FUNCTION("cancelPairedDevice", CancelPairedDevice),
55         DECLARE_NAPI_FUNCTION("getProfileConnectionState", GetProfileConnectionState),
56 #endif
57         DECLARE_NAPI_FUNCTION("getRemoteDeviceName", GetRemoteDeviceName),
58         DECLARE_NAPI_FUNCTION("getRemoteDeviceClass", GetRemoteDeviceClass),
59         DECLARE_NAPI_FUNCTION("getLocalName", GetLocalName),
60         DECLARE_NAPI_FUNCTION("getPairedDevices", GetPairedDevices),
61         DECLARE_NAPI_FUNCTION("getProfileConnState", GetProfileConnectionState),
62         DECLARE_NAPI_FUNCTION("setDevicePairingConfirmation", SetDevicePairingConfirmation),
63         DECLARE_NAPI_FUNCTION("setLocalName", SetLocalName),
64         DECLARE_NAPI_FUNCTION("setBluetoothScanMode", SetBluetoothScanMode),
65         DECLARE_NAPI_FUNCTION("getBluetoothScanMode", GetBluetoothScanMode),
66         DECLARE_NAPI_FUNCTION("startBluetoothDiscovery", StartBluetoothDiscovery),
67         DECLARE_NAPI_FUNCTION("stopBluetoothDiscovery", StopBluetoothDiscovery),
68 #ifdef BLUETOOTH_API_SINCE_10
69         DECLARE_NAPI_FUNCTION("setDevicePinCode", SetDevicePinCode),
70         DECLARE_NAPI_FUNCTION("cancelPairingDevice", CancelPairingDevice),
71         DECLARE_NAPI_FUNCTION("pairCredibleDevice", PairCredibleDevice),
72         DECLARE_NAPI_FUNCTION("getLocalProfileUuids", GetLocalProfileUuids),
73         DECLARE_NAPI_FUNCTION("getRemoteProfileUuids", GetRemoteProfileUuids),
74         DECLARE_NAPI_FUNCTION("on", RegisterConnectionObserver),
75         DECLARE_NAPI_FUNCTION("off", DeRegisterConnectionObserver),
76         DECLARE_NAPI_FUNCTION("isBluetoothDiscovering", IsBluetoothDiscovering),
77         DECLARE_NAPI_FUNCTION("getPairState", GetPairState),
78         DECLARE_NAPI_FUNCTION("connectAllowedProfiles", ConnectAllowedProfiles),
79         DECLARE_NAPI_FUNCTION("disconnectAllowedProfiles", DisconnectAllowedProfiles),
80         DECLARE_NAPI_FUNCTION("getRemoteProductId", GetRemoteProductId),
81 #endif
82         DECLARE_NAPI_FUNCTION("setRemoteDeviceName", SetRemoteDeviceName),
83         DECLARE_NAPI_FUNCTION("setRemoteDeviceType", SetRemoteDeviceType),
84         DECLARE_NAPI_FUNCTION("getRemoteDeviceType", GetRemoteDeviceType),
85         DECLARE_NAPI_FUNCTION("getRemoteDeviceBatteryInfo", GetRemoteDeviceBatteryInfo),
86     };
87 
88     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
89     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
90     return exports;
91 }
92 
93 using NapiBluetoothOnOffFunc = std::function<napi_status(napi_env env, napi_callback_info info)>;
94 
NapiConnectionOnOffExecute(napi_env env,napi_callback_info info,NapiBluetoothOnOffFunc connectionObserverFunc,NapiBluetoothOnOffFunc remoteDeviceObserverFunc)95 static napi_status NapiConnectionOnOffExecute(napi_env env, napi_callback_info info,
96     NapiBluetoothOnOffFunc connectionObserverFunc, NapiBluetoothOnOffFunc remoteDeviceObserverFunc)
97 {
98     std::string type = "";
99     NAPI_BT_CALL_RETURN(NapiGetOnOffCallbackName(env, info, type));
100 
101     napi_status status = napi_ok;
102     if (type == REGISTER_DEVICE_FIND_TYPE ||
103         type == REGISTER_DISCOVERY_RESULT_TYPE ||
104         type == REGISTER_PIN_REQUEST_TYPE) {
105         status = connectionObserverFunc(env, info);
106     } else if (type == REGISTER_BOND_STATE_TYPE || type == REGISTER_BATTERY_CHANGE_TYPE) {
107         status = remoteDeviceObserverFunc(env, info);
108     } else {
109         HILOGE("Unsupported callback: %{public}s", type.c_str());
110         status = napi_invalid_arg;
111     }
112     return status;
113 }
114 
RegisterConnectionObserver(napi_env env,napi_callback_info info)115 napi_value RegisterConnectionObserver(napi_env env, napi_callback_info info)
116 {
117     auto connectionObserverFunc = [](napi_env env, napi_callback_info info) {
118         return g_connectionObserver->eventSubscribe_.Register(env, info);
119     };
120     auto remoteDeviceObserverFunc =  [](napi_env env, napi_callback_info info) {
121         return g_remoteDeviceObserver->eventSubscribe_.Register(env, info);
122     };
123 
124     auto status = NapiConnectionOnOffExecute(env, info, connectionObserverFunc, remoteDeviceObserverFunc);
125     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
126     return NapiGetUndefinedRet(env);
127 }
128 
DeRegisterConnectionObserver(napi_env env,napi_callback_info info)129 napi_value DeRegisterConnectionObserver(napi_env env, napi_callback_info info)
130 {
131     auto connectionObserverFunc = [](napi_env env, napi_callback_info info) {
132         return g_connectionObserver->eventSubscribe_.Deregister(env, info);
133     };
134     auto remoteDeviceObserverFunc =  [](napi_env env, napi_callback_info info) {
135         return g_remoteDeviceObserver->eventSubscribe_.Deregister(env, info);
136     };
137 
138     auto status = NapiConnectionOnOffExecute(env, info, connectionObserverFunc, remoteDeviceObserverFunc);
139     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
140     return NapiGetUndefinedRet(env);
141 }
142 
GetBtConnectionState(napi_env env,napi_callback_info info)143 napi_value GetBtConnectionState(napi_env env, napi_callback_info info)
144 {
145     HILOGD("enter");
146     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
147     int state = static_cast<int>(BTConnectState::DISCONNECTED);
148     int32_t err = host->GetBtConnectionState(state);
149     HILOGD("start state %{publDc}d", state);
150     napi_value result = nullptr;
151     napi_create_int32(env, GetProfileConnectionState(state), &result);
152     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
153     return result;
154 }
155 
PairDevice(napi_env env,napi_callback_info info)156 napi_value PairDevice(napi_env env, napi_callback_info info)
157 {
158     HILOGD("enter");
159     std::string remoteAddr = INVALID_MAC_ADDRESS;
160     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
161     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
162 
163     BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
164     int32_t ret = remoteDevice.StartPair();
165     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
166     return NapiGetBooleanTrue(env);
167 }
168 
CancelPairedDevice(napi_env env,napi_callback_info info)169 napi_value CancelPairedDevice(napi_env env, napi_callback_info info)
170 {
171     HILOGD("enter");
172     std::string remoteAddr{};
173     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
174     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
175 
176     BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
177     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
178     int32_t ret = host->RemovePair(remoteDevice);
179     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
180 
181     return NapiGetBooleanTrue(env);
182 }
183 
GetRemoteDeviceName(napi_env env,napi_callback_info info)184 napi_value GetRemoteDeviceName(napi_env env, napi_callback_info info)
185 {
186     HILOGD("start");
187     std::string remoteAddr = INVALID_MAC_ADDRESS;
188     std::string name = INVALID_NAME;
189     napi_value result = nullptr;
190     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
191     napi_create_string_utf8(env, name.c_str(), name.size(), &result);
192     NAPI_BT_ASSERT_RETURN(env, checkRet == true, BT_ERR_INVALID_PARAM, result);
193 
194     BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
195     int32_t err = remoteDevice.GetDeviceName(name);
196     napi_create_string_utf8(env, name.c_str(), name.size(), &result);
197     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
198     return result;
199 }
200 
GetRemoteDeviceClass(napi_env env,napi_callback_info info)201 napi_value GetRemoteDeviceClass(napi_env env, napi_callback_info info)
202 {
203     HILOGD("start");
204     std::string remoteAddr = INVALID_MAC_ADDRESS;
205     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
206     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
207 
208     BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
209     int tmpCod = MajorClass::MAJOR_UNCATEGORIZED;
210     int tmpMajorClass = MajorClass::MAJOR_UNCATEGORIZED;
211     int tmpMajorMinorClass = MajorClass::MAJOR_UNCATEGORIZED;
212     int32_t err = remoteDevice.GetDeviceProductType(tmpCod, tmpMajorClass, tmpMajorMinorClass);
213     napi_value result = nullptr;
214     napi_create_object(env, &result);
215     napi_value majorClass = 0;
216     napi_create_int32(env, tmpMajorClass, &majorClass);
217     napi_set_named_property(env, result, "majorClass", majorClass);
218     napi_value majorMinorClass = 0;
219     napi_create_int32(env, tmpMajorMinorClass, &majorMinorClass);
220     napi_set_named_property(env, result, "majorMinorClass", majorMinorClass);
221     napi_value cod = 0;
222     napi_create_int32(env, tmpCod, &cod);
223     napi_set_named_property(env, result, "classOfDevice", cod);
224     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
225     return result;
226 }
227 
GetLocalName(napi_env env,napi_callback_info info)228 napi_value GetLocalName(napi_env env, napi_callback_info info)
229 {
230     napi_value result = nullptr;
231     HILOGD("enter");
232     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
233     std::string localName = INVALID_NAME;
234     int32_t err = host->GetLocalName(localName);
235     napi_create_string_utf8(env, localName.c_str(), localName.size(), &result);
236     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
237     HILOGI("end");
238     return result;
239 }
240 
GetPairedDevices(napi_env env,napi_callback_info info)241 napi_value GetPairedDevices(napi_env env, napi_callback_info info)
242 {
243     HILOGD("enter");
244     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
245     std::vector<BluetoothRemoteDevice> remoteDeviceLists;
246     int32_t ret = host->GetPairedDevices(BT_TRANSPORT_BREDR, remoteDeviceLists);
247     napi_value result = nullptr;
248     int count = 0;
249     napi_create_array(env, &result);
250     for (auto vec : remoteDeviceLists) {
251         napi_value remoteDeviceResult;
252         napi_create_string_utf8(env, vec.GetDeviceAddr().c_str(), vec.GetDeviceAddr().size(), &remoteDeviceResult);
253         napi_set_element(env, result, count, remoteDeviceResult);
254         count++;
255     }
256     NAPI_BT_ASSERT_RETURN(env, ret == BT_NO_ERROR, ret, result);
257     HILOGI("end");
258     return result;
259 }
260 
GetProfileConnectionState(napi_env env,napi_callback_info info)261 napi_value GetProfileConnectionState(napi_env env, napi_callback_info info)
262 {
263     HILOGD("enter");
264     int profileId = 0;
265     bool checkRet = CheckProfileIdParam(env, info, profileId);
266     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
267 
268     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
269     int state = static_cast<int>(BTConnectState::DISCONNECTED);
270     int32_t err = host->GetBtProfileConnState(GetProfileId(profileId), state);
271     int status = GetProfileConnectionState(state);
272     napi_value ret = nullptr;
273     napi_create_int32(env, status, &ret);
274     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, ret);
275     HILOGD("status: %{public}d", status);
276     return ret;
277 }
278 
GetProfileConnectionStateEx(napi_env env,napi_callback_info info)279 napi_value GetProfileConnectionStateEx(napi_env env, napi_callback_info info)
280 {
281     HILOGD("enter");
282     int profileId = 0;
283     size_t argSize = ARGS_SIZE_ONE;
284     bool checkRet = CheckProfileIdParamEx(env, info, profileId, argSize);
285     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
286 
287     napi_value ret = nullptr;
288     if (argSize == 0) {
289         ret = GetBtConnectionState(env, info);
290     } else {
291         ret = GetProfileConnectionState(env, info);
292     }
293     return ret;
294 }
295 
SetDevicePairingConfirmation(napi_env env,napi_callback_info info)296 napi_value SetDevicePairingConfirmation(napi_env env, napi_callback_info info)
297 {
298     HILOGD("enter");
299     std::string remoteAddr{};
300     bool accept = false;
301     bool checkRet = CheckSetDevicePairingConfirmationParam(env, info, remoteAddr, accept);
302     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
303 
304     HILOGI("SetDevicePairingConfirmation::accept = %{public}d", accept);
305     BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
306     int32_t ret = BT_NO_ERROR;
307     if (accept) {
308         ret = remoteDevice.SetDevicePairingConfirmation(accept);
309     } else {
310         ret = remoteDevice.CancelPairing();
311     }
312     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
313     return NapiGetBooleanTrue(env);
314 }
315 
SetLocalName(napi_env env,napi_callback_info info)316 napi_value SetLocalName(napi_env env, napi_callback_info info)
317 {
318     HILOGD("enter");
319     std::string localName = INVALID_NAME;
320     bool checkRet = CheckLocalNameParam(env, info, localName);
321     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
322 
323     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
324     int32_t ret = host->SetLocalName(localName);
325     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
326     return NapiGetBooleanTrue(env);
327 }
328 
SetBluetoothScanMode(napi_env env,napi_callback_info info)329 napi_value SetBluetoothScanMode(napi_env env, napi_callback_info info)
330 {
331     HILOGD("enter");
332     int32_t mode = 0;
333     int32_t duration = 0;
334     bool checkRet = CheckSetBluetoothScanModeParam(env, info, mode, duration);
335     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
336     HILOGI("mode = %{public}d,duration = %{public}d", mode, duration);
337 
338     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
339     int32_t ret = host->SetBtScanMode(mode, duration);
340     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
341     host->SetBondableMode(BT_TRANSPORT_BREDR, 1);
342     return NapiGetBooleanTrue(env);
343 }
344 
GetBluetoothScanMode(napi_env env,napi_callback_info info)345 napi_value GetBluetoothScanMode(napi_env env, napi_callback_info info)
346 {
347     HILOGD("enter");
348     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
349     int32_t scanMode = 0;
350     int32_t err = host->GetBtScanMode(scanMode);
351     napi_value result = nullptr;
352     napi_create_uint32(env, scanMode, &result);
353     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
354     HILOGI("end");
355     return result;
356 }
357 
StartBluetoothDiscovery(napi_env env,napi_callback_info info)358 napi_value StartBluetoothDiscovery(napi_env env, napi_callback_info info)
359 {
360     HILOGD("enter");
361     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
362     int ret = host->StartBtDiscovery();
363     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
364     return NapiGetBooleanTrue(env);
365 }
366 
StopBluetoothDiscovery(napi_env env,napi_callback_info info)367 napi_value StopBluetoothDiscovery(napi_env env, napi_callback_info info)
368 {
369     HILOGD("enter");
370     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
371     int ret = host->CancelBtDiscovery();
372     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
373     return NapiGetBooleanTrue(env);
374 }
375 
376 #ifdef BLUETOOTH_API_SINCE_10
ParseSetDevicePinCodeParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,std::string & outPinCode)377 napi_status ParseSetDevicePinCodeParameters(napi_env env, napi_callback_info info,
378     std::string &outRemoteAddr, std::string &outPinCode)
379 {
380     HILOGD("enter");
381     std::string remoteAddr{};
382     std::string pinCode{};
383     size_t argc = ARGS_SIZE_THREE;
384     napi_value argv[ARGS_SIZE_THREE] = {nullptr};
385     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
386     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO && argc != ARGS_SIZE_THREE,
387         "Requires 2 or 3 arguments.", napi_invalid_arg);
388     NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
389     NAPI_BT_RETURN_IF(!ParseString(env, pinCode, argv[PARAM1]), "pinCode ParseString failed", napi_invalid_arg);
390     outRemoteAddr = remoteAddr;
391     outPinCode = pinCode;
392     return napi_ok;
393 }
394 
SetDevicePinCode(napi_env env,napi_callback_info info)395 napi_value SetDevicePinCode(napi_env env, napi_callback_info info)
396 {
397     HILOGD("enter");
398     std::string remoteAddr = "";
399     std::string pinCode = "";
400     auto status = ParseSetDevicePinCodeParameters(env, info, remoteAddr, pinCode);
401     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
402 
403     auto func = [remoteAddr, pinCode]() {
404         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
405         int32_t err = remoteDevice.SetDevicePin(pinCode);
406         HILOGI("SetDevicePinCode err: %{public}d", err);
407         return NapiAsyncWorkRet(err);
408     };
409     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
410     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
411     asyncWork->Run();
412     return asyncWork->GetRet();
413 }
414 
CheckDeviceAsyncParam(napi_env env,napi_callback_info info,std::string & addr)415 napi_status CheckDeviceAsyncParam(napi_env env, napi_callback_info info, std::string &addr)
416 {
417     size_t argc = ARGS_SIZE_TWO;
418     napi_value argv[ARGS_SIZE_TWO] = {nullptr};
419     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
420     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO, "Requires 1 or 2 arguments", napi_invalid_arg);
421     NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], addr));
422     return napi_ok;
423 }
424 
PairDeviceAsync(napi_env env,napi_callback_info info)425 napi_value PairDeviceAsync(napi_env env, napi_callback_info info)
426 {
427     HILOGD("enter");
428     std::string remoteAddr = INVALID_MAC_ADDRESS;
429     auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
430     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
431 
432     auto func = [remoteAddr]() {
433         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
434         int32_t err = remoteDevice.StartPair();
435         HILOGI("err: %{public}d", err);
436         return NapiAsyncWorkRet(err);
437     };
438     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
439     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
440     asyncWork->Run();
441     return asyncWork->GetRet();
442 }
443 
CancelPairedDeviceAsync(napi_env env,napi_callback_info info)444 napi_value CancelPairedDeviceAsync(napi_env env, napi_callback_info info)
445 {
446     HILOGD("enter");
447     std::string remoteAddr {};
448     bool checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
449     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
450 
451     auto func = [remoteAddr]() {
452         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
453         BluetoothHost *host = &BluetoothHost::GetDefaultHost();
454         int32_t err = host->RemovePair(remoteDevice);
455         HILOGI("err: %{public}d", err);
456         return NapiAsyncWorkRet(err);
457     };
458     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
459     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
460     asyncWork->Run();
461     return asyncWork->GetRet();
462 }
463 
CancelPairingDevice(napi_env env,napi_callback_info info)464 napi_value CancelPairingDevice(napi_env env, napi_callback_info info)
465 {
466     HILOGD("enter");
467     std::string remoteAddr{};
468     bool checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
469     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
470 
471     auto func = [remoteAddr]() {
472         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
473         int32_t err = remoteDevice.CancelPairing();
474         HILOGI("err: %{public}d", err);
475         return NapiAsyncWorkRet(err);
476     };
477     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
478     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
479     asyncWork->Run();
480     return asyncWork->GetRet();
481 }
482 
CheckPairCredibleDeviceParam(napi_env env,napi_callback_info info,std::string & addr,int & transport)483 napi_status CheckPairCredibleDeviceParam(napi_env env, napi_callback_info info, std::string &addr, int &transport)
484 {
485     size_t argc = ARGS_SIZE_THREE;
486     napi_value argv[ARGS_SIZE_THREE] = {nullptr};
487     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
488     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO && argc != ARGS_SIZE_THREE, "Requires 2 or 3 arguments.", napi_invalid_arg);
489     NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], addr));
490     NAPI_BT_RETURN_IF(!ParseInt32(env, transport, argv[PARAM1]), "ParseInt32 failed", napi_invalid_arg);
491     NAPI_BT_RETURN_IF(!IsValidTransport(transport), "Invalid transport", napi_invalid_arg);
492     return napi_ok;
493 }
494 
PairCredibleDevice(napi_env env,napi_callback_info info)495 napi_value PairCredibleDevice(napi_env env, napi_callback_info info)
496 {
497     HILOGD("enter");
498     std::string remoteAddr = INVALID_MAC_ADDRESS;
499     int transport = BT_TRANSPORT_NONE;
500     auto status = CheckPairCredibleDeviceParam(env, info, remoteAddr, transport);
501     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
502 
503     auto func = [remoteAddr, transport]() {
504         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr, transport);
505         int32_t err = remoteDevice.StartCrediblePair();
506         HILOGI("err: %{public}d", err);
507         return NapiAsyncWorkRet(err);
508     };
509     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
510     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
511     asyncWork->Run();
512     return asyncWork->GetRet();
513 }
514 
CheckGetProfileUuids(napi_env env,napi_callback_info info,std::string & address)515 napi_status CheckGetProfileUuids(napi_env env, napi_callback_info info, std::string &address)
516 {
517     size_t argc = ARGS_SIZE_TWO;
518     napi_value argv[ARGS_SIZE_TWO] = {0};
519     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr));
520     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_ONE && argc != ARGS_SIZE_TWO, "Requires 1 or 2 arguments.", napi_invalid_arg);
521     NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], address));
522     return napi_ok;
523 }
524 
GetLocalProfileUuids(napi_env env,napi_callback_info info)525 napi_value GetLocalProfileUuids(napi_env env, napi_callback_info info)
526 {
527     HILOGD("enter");
528     auto func = []() {
529         std::vector<std::string> uuids{};
530         int32_t err = BluetoothHost::GetDefaultHost().GetLocalProfileUuids(uuids);
531         HILOGI("err: %{public}d", err);
532         auto object = std::make_shared<NapiNativeUuidsArray>(uuids);
533         return NapiAsyncWorkRet(err, object);
534     };
535     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
536     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
537     asyncWork->Run();
538     return asyncWork->GetRet();
539 }
540 
GetRemoteProfileUuids(napi_env env,napi_callback_info info)541 napi_value GetRemoteProfileUuids(napi_env env, napi_callback_info info)
542 {
543     HILOGD("enter");
544     std::string address;
545     auto status = CheckGetProfileUuids(env, info, address);
546     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
547     auto func = [address]() {
548         std::vector<std::string> uuids{};
549         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(address);
550         int32_t err = remoteDevice.GetDeviceUuids(uuids);
551         HILOGI("err: %{public}d", err);
552         auto object = std::make_shared<NapiNativeUuidsArray>(uuids);
553         return NapiAsyncWorkRet(err, object);
554     };
555     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
556     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
557     asyncWork->Run();
558     return asyncWork->GetRet();
559 }
560 
IsBluetoothDiscovering(napi_env env,napi_callback_info info)561 napi_value IsBluetoothDiscovering(napi_env env, napi_callback_info info)
562 {
563     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
564     bool isDiscovering = false;
565     int32_t err = host->IsBtDiscovering(isDiscovering);
566     napi_value result = nullptr;
567     NAPI_BT_ASSERT_RETURN(env, napi_get_boolean(env, isDiscovering, &result) == napi_ok, err, result);
568     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
569     HILOGE("isBluetoothDiscovering :%{public}d", isDiscovering);
570     return result;
571 }
572 
GetPairState(napi_env env,napi_callback_info info)573 napi_value GetPairState(napi_env env, napi_callback_info info)
574 {
575     std::string remoteAddr = INVALID_MAC_ADDRESS;
576     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
577     NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
578     BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
579     int state = PAIR_NONE;
580     int32_t err = remoteDevice.GetPairState(state);
581     int pairState = static_cast<int>(BondState::BOND_STATE_INVALID);
582     DealPairStatus(state, pairState);
583     napi_value result = nullptr;
584     NAPI_BT_ASSERT_RETURN(env, napi_create_int32(env, pairState, &result) == napi_ok, err, result);
585     NAPI_BT_ASSERT_RETURN(env, (err == BT_NO_ERROR || err == BT_ERR_INTERNAL_ERROR), err, result);
586     HILOGI("getPairState :%{public}d", pairState);
587     return result;
588 }
589 
ConnectAllowedProfiles(napi_env env,napi_callback_info info)590 napi_value ConnectAllowedProfiles(napi_env env, napi_callback_info info)
591 {
592     HILOGI("enter");
593     std::string remoteAddr = INVALID_MAC_ADDRESS;
594     auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
595     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
596 
597     auto func = [remoteAddr]() {
598         BluetoothHost *host = &BluetoothHost::GetDefaultHost();
599         int32_t ret = host->ConnectAllowedProfiles(remoteAddr);
600         HILOGI("ret: %{public}d", ret);
601         return NapiAsyncWorkRet(ret);
602     };
603     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
604     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
605     asyncWork->Run();
606     return asyncWork->GetRet();
607 }
608 
DisconnectAllowedProfiles(napi_env env,napi_callback_info info)609 napi_value DisconnectAllowedProfiles(napi_env env, napi_callback_info info)
610 {
611     HILOGI("enter");
612     std::string remoteAddr = INVALID_MAC_ADDRESS;
613     auto checkRet = CheckDeviceAsyncParam(env, info, remoteAddr);
614     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM);
615 
616     auto func = [remoteAddr]() {
617         BluetoothHost *host = &BluetoothHost::GetDefaultHost();
618         int32_t ret = host->DisconnectAllowedProfiles(remoteAddr);
619         HILOGI("ret: %{public}d", ret);
620         return NapiAsyncWorkRet(ret);
621     };
622     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
623     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
624     asyncWork->Run();
625     return asyncWork->GetRet();
626 }
627 
GetRemoteProductId(napi_env env,napi_callback_info info)628 napi_value GetRemoteProductId(napi_env env, napi_callback_info info)
629 {
630     HILOGD("start");
631     std::string remoteAddr = INVALID_MAC_ADDRESS;
632     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
633     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
634 
635     BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
636     std::string productId;
637     int32_t err = remoteDevice.GetDeviceProductId(productId);
638 
639     napi_value result = nullptr;
640     napi_create_string_utf8(env, productId.c_str(), productId.size(), &result);
641     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
642     HILOGI("GetRemoteProductId :%{public}s", productId.c_str());
643     return result;
644 }
645 
646 #endif
647 
ParseSetRemoteDeviceNameParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,std::string & outDeviceName)648 napi_status ParseSetRemoteDeviceNameParameters(napi_env env, napi_callback_info info,
649     std::string &outRemoteAddr, std::string &outDeviceName)
650 {
651     HILOGD("enter");
652     std::string remoteAddr{};
653     std::string deviceName{};
654     size_t argc = ARGS_SIZE_TWO;
655     napi_value argv[ARGS_SIZE_TWO] = {nullptr};
656     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
657     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO, "Requires 2 arguments.", napi_invalid_arg);
658     NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
659     NAPI_BT_RETURN_IF(!ParseString(env, deviceName, argv[PARAM1]), "deviceName ParseString failed", napi_invalid_arg);
660     outRemoteAddr = remoteAddr;
661     outDeviceName = deviceName;
662     return napi_ok;
663 }
664 
SetRemoteDeviceName(napi_env env,napi_callback_info info)665 napi_value SetRemoteDeviceName(napi_env env, napi_callback_info info)
666 {
667     HILOGD("enter");
668     std::string remoteAddr = "";
669     std::string deviceName = "";
670     auto status = ParseSetRemoteDeviceNameParameters(env, info, remoteAddr, deviceName);
671     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
672 
673     auto func = [remoteAddr, deviceName]() {
674         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
675         int32_t err = remoteDevice.SetDeviceAlias(deviceName);
676         HILOGI("SetDeviceName err: %{public}d", err);
677         return NapiAsyncWorkRet(err);
678     };
679     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
680     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
681     asyncWork->Run();
682     return asyncWork->GetRet();
683 }
684 
ParseSetRemoteDeviceTypeParameters(napi_env env,napi_callback_info info,std::string & outRemoteAddr,int32_t & outDeviceType)685 napi_status ParseSetRemoteDeviceTypeParameters(napi_env env, napi_callback_info info,
686     std::string &outRemoteAddr, int32_t &outDeviceType)
687 {
688     HILOGD("enter");
689     std::string remoteAddr{};
690     int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
691     size_t argc = ARGS_SIZE_TWO;
692     napi_value argv[ARGS_SIZE_TWO] = {nullptr};
693     NAPI_BT_CALL_RETURN(napi_get_cb_info(env, info, &argc, argv, nullptr, NULL));
694     NAPI_BT_RETURN_IF(argc != ARGS_SIZE_TWO, "Requires 2 arguments.", napi_invalid_arg);
695     NAPI_BT_CALL_RETURN(NapiParseBdAddr(env, argv[PARAM0], remoteAddr));
696     NAPI_BT_RETURN_IF(!ParseInt32(env, deviceType, argv[PARAM1]), "deviceType ParseInt32 failed", napi_invalid_arg);
697     outRemoteAddr = remoteAddr;
698     outDeviceType = deviceType;
699     return napi_ok;
700 }
701 
SetRemoteDeviceType(napi_env env,napi_callback_info info)702 napi_value SetRemoteDeviceType(napi_env env, napi_callback_info info)
703 {
704     HILOGD("enter");
705     std::string remoteAddr = INVALID_MAC_ADDRESS;
706     int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
707     auto status = ParseSetRemoteDeviceTypeParameters(env, info, remoteAddr, deviceType);
708     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
709 
710     auto func = [remoteAddr, deviceType]() {
711         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
712         int32_t err = remoteDevice.SetDeviceCustomType(deviceType);
713         HILOGI("SetRemoteDeviceType err: %{public}d", err);
714         return NapiAsyncWorkRet(err);
715     };
716     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
717     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
718     asyncWork->Run();
719     return asyncWork->GetRet();
720 }
721 
GetRemoteDeviceType(napi_env env,napi_callback_info info)722 napi_value GetRemoteDeviceType(napi_env env, napi_callback_info info)
723 {
724     HILOGD("enter");
725     std::string remoteAddr;
726     bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
727     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
728     auto func = [remoteAddr]() {
729         int32_t deviceType = DeviceType::DEVICE_TYPE_DEFAULT;
730         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
731         int32_t err = remoteDevice.GetDeviceCustomType(deviceType);
732         HILOGI("GetRemoteDeviceType err: %{public}d", err);
733         auto object = std::make_shared<NapiNativeInt>(deviceType);
734         return NapiAsyncWorkRet(err, object);
735     };
736     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
737     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
738     asyncWork->Run();
739     return asyncWork->GetRet();
740 }
741 
GetRemoteDeviceBatteryInfo(napi_env env,napi_callback_info info)742 napi_value GetRemoteDeviceBatteryInfo(napi_env env, napi_callback_info info)
743 {
744     HILOGD("enter");
745     std::string remoteAddr = INVALID_MAC_ADDRESS;
746     auto checkRet = CheckDeivceIdParam(env, info, remoteAddr);
747     NAPI_BT_ASSERT_RETURN_UNDEF(env, checkRet, BT_ERR_INVALID_PARAM);
748     auto func = [remoteAddr]() {
749         DeviceBatteryInfo batteryInfo;
750         BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(remoteAddr);
751         int32_t err = remoteDevice.GetRemoteDeviceBatteryInfo(batteryInfo);
752         HILOGI("err: %{public}d", err);
753         auto object = std::make_shared<NapiNativeBatteryInfo>(batteryInfo);
754         return NapiAsyncWorkRet(err, object);
755     };
756     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
757     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
758     asyncWork->Run();
759     return asyncWork->GetRet();
760 }
761 
ConnectionPropertyValueInit(napi_env env,napi_value exports)762 napi_value ConnectionPropertyValueInit(napi_env env, napi_value exports)
763 {
764     HILOGD("enter");
765     napi_value scanModeObj = ScanModeInit(env);
766     napi_value bondStateObj = BondStateInit(env);
767     napi_value unbondCauseObj = UnbondCauseInit(env);
768 #ifdef BLUETOOTH_API_SINCE_10
769     napi_value bluetoothTransportObject = BluetoothTransportInit(env);
770     napi_value pinTypeObject = PinTypeInit(env);
771 #endif
772     napi_value deviceTypeObject = DeviceTypeInit(env);
773     napi_value deviceChargeStateObject = DeviceChargeStateInit(env);
774     napi_property_descriptor exportProperties[] = {
775         DECLARE_NAPI_PROPERTY("ScanMode", scanModeObj),
776         DECLARE_NAPI_PROPERTY("BondState", bondStateObj),
777         DECLARE_NAPI_PROPERTY("UnbondCause", unbondCauseObj),
778 #ifdef BLUETOOTH_API_SINCE_10
779         DECLARE_NAPI_PROPERTY("BluetoothTransport", bluetoothTransportObject),
780         DECLARE_NAPI_PROPERTY("PinType", pinTypeObject),
781 #endif
782         DECLARE_NAPI_PROPERTY("DeviceType", deviceTypeObject),
783         DECLARE_NAPI_PROPERTY("DeviceChargeState", deviceChargeStateObject),
784     };
785     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "connection:napi_define_properties");
786     napi_define_properties(env, exports, sizeof(exportProperties) / sizeof(*exportProperties), exportProperties);
787     return exports;
788 }
789 
ScanModeInit(napi_env env)790 napi_value ScanModeInit(napi_env env)
791 {
792     HILOGD("enter");
793     napi_value scanMode = nullptr;
794     napi_create_object(env, &scanMode);
795     SetNamedPropertyByInteger(env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_NONE), "SCAN_MODE_NONE");
796     SetNamedPropertyByInteger(
797         env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE), "SCAN_MODE_CONNECTABLE");
798     SetNamedPropertyByInteger(
799         env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_GENERAL_DISCOVERABLE), "SCAN_MODE_GENERAL_DISCOVERABLE");
800     SetNamedPropertyByInteger(
801         env, scanMode, static_cast<int>(ScanMode::SCAN_MODE_LIMITED_DISCOVERABLE), "SCAN_MODE_LIMITED_DISCOVERABLE");
802     SetNamedPropertyByInteger(env,
803         scanMode,
804         static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE),
805         "SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE");
806     SetNamedPropertyByInteger(env,
807         scanMode,
808         static_cast<int>(ScanMode::SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE),
809         "SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE");
810     return scanMode;
811 }
812 
BondStateInit(napi_env env)813 napi_value BondStateInit(napi_env env)
814 {
815     HILOGD("enter");
816     napi_value bondState = nullptr;
817     napi_create_object(env, &bondState);
818     SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_INVALID), "BOND_STATE_INVALID");
819     SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_BONDING), "BOND_STATE_BONDING");
820     SetNamedPropertyByInteger(env, bondState, static_cast<int>(BondState::BOND_STATE_BONDED), "BOND_STATE_BONDED");
821     return bondState;
822 }
823 
UnbondCauseInit(napi_env env)824 napi_value UnbondCauseInit(napi_env env)
825 {
826     HILOGD("enter");
827     napi_value unbondCause = nullptr;
828     napi_create_object(env, &unbondCause);
829     SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_USER_REMOVED, "USER_REMOVED");
830     SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_REMOTE_DEVICE_DOWN, "REMOTE_DEVICE_DOWN");
831     SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_AUTH_FAILURE, "AUTH_FAILURE");
832     SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_AUTH_REJECTED, "AUTH_REJECTED");
833     SetNamedPropertyByInteger(env, unbondCause, UNBOND_CAUSE_INTERNAL_ERROR, "INTERNAL_ERROR");
834     return unbondCause;
835 }
836 
837 #ifdef BLUETOOTH_API_SINCE_10
BluetoothTransportInit(napi_env env)838 napi_value BluetoothTransportInit(napi_env env)
839 {
840     HILOGD("enter");
841     napi_value bluetoothTransport = nullptr;
842     napi_create_object(env, &bluetoothTransport);
843     SetNamedPropertyByInteger(
844         env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_BR_EDR), "TRANSPORT_BR_EDR");
845     SetNamedPropertyByInteger(
846         env, bluetoothTransport, static_cast<int>(BluetoothTransport::TRANSPORT_LE), "TRANSPORT_LE");
847     return bluetoothTransport;
848 }
849 
PinTypeInit(napi_env env)850 napi_value PinTypeInit(napi_env env)
851 {
852     HILOGD("enter");
853     napi_value pinType = nullptr;
854     napi_create_object(env, &pinType);
855     SetNamedPropertyByInteger(
856         env, pinType, static_cast<int>(PinType::PIN_TYPE_ENTER_PIN_CODE), "PIN_TYPE_ENTER_PIN_CODE");
857     SetNamedPropertyByInteger(
858         env, pinType, static_cast<int>(PinType::PIN_TYPE_ENTER_PASSKEY), "PIN_TYPE_ENTER_PASSKEY");
859     SetNamedPropertyByInteger(
860         env, pinType, static_cast<int>(PinType::PIN_TYPE_CONFIRM_PASSKEY), "PIN_TYPE_CONFIRM_PASSKEY");
861     SetNamedPropertyByInteger(
862         env, pinType, static_cast<int>(PinType::PIN_TYPE_NO_PASSKEY_CONSENT), "PIN_TYPE_NO_PASSKEY_CONSENT");
863     SetNamedPropertyByInteger(
864         env, pinType, static_cast<int>(PinType::PIN_TYPE_NOTIFY_PASSKEY), "PIN_TYPE_NOTIFY_PASSKEY");
865     SetNamedPropertyByInteger(
866         env, pinType, static_cast<int>(PinType::PIN_TYPE_DISPLAY_PIN_CODE), "PIN_TYPE_DISPLAY_PIN_CODE");
867     SetNamedPropertyByInteger(env, pinType, static_cast<int>(PinType::PIN_TYPE_OOB_CONSENT), "PIN_TYPE_OOB_CONSENT");
868     SetNamedPropertyByInteger(
869         env, pinType, static_cast<int>(PinType::PIN_TYPE_PIN_16_DIGITS), "PIN_TYPE_PIN_16_DIGITS");
870     return pinType;
871 }
872 #endif
873 
DeviceTypeInit(napi_env env)874 napi_value DeviceTypeInit(napi_env env)
875 {
876     HILOGD("enter");
877     napi_value deviceType = nullptr;
878     napi_create_object(env, &deviceType);
879     SetNamedPropertyByInteger(
880         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_DEFAULT), "DEVICE_TYPE_DEFAULT");
881     SetNamedPropertyByInteger(
882         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_CAR), "DEVICE_TYPE_CAR");
883     SetNamedPropertyByInteger(
884         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_HEADSET), "DEVICE_TYPE_HEADSET");
885     SetNamedPropertyByInteger(
886         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_HEARING), "DEVICE_TYPE_HEARING");
887     SetNamedPropertyByInteger(
888         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_GLASSES), "DEVICE_TYPE_GLASSES");
889     SetNamedPropertyByInteger(
890         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_WATCH), "DEVICE_TYPE_WATCH");
891     SetNamedPropertyByInteger(
892         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_SPEAKER), "DEVICE_TYPE_SPEAKER");
893     SetNamedPropertyByInteger(
894         env, deviceType, static_cast<int>(DeviceType::DEVICE_TYPE_OTHERS), "DEVICE_TYPE_OTHERS");
895     return deviceType;
896 }
897 
DeviceChargeStateInit(napi_env env)898 napi_value DeviceChargeStateInit(napi_env env)
899 {
900     HILOGD("enter");
901     napi_value deviceChargeState = nullptr;
902     napi_create_object(env, &deviceChargeState);
903     SetNamedPropertyByInteger(
904         env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_NORMAL_CHARGE_NOT_CHARGED),
905         "DEVICE_NORMAL_CHARGE_NOT_CHARGED");
906     SetNamedPropertyByInteger(
907         env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_NORMAL_CHARGE_IN_CHARGING),
908         "DEVICE_NORMAL_CHARGE_IN_CHARGING");
909     SetNamedPropertyByInteger(
910         env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_SUPER_CHARGE_NOT_CHARGED),
911         "DEVICE_SUPER_CHARGE_NOT_CHARGED");
912     SetNamedPropertyByInteger(
913         env, deviceChargeState, static_cast<int32_t>(DeviceChargeState::DEVICE_SUPER_CHARGE_IN_CHARGING),
914         "DEVICE_SUPER_CHARGE_IN_CHARGING");
915     return deviceChargeState;
916 }
917 
RegisterObserverToHost()918 void RegisterObserverToHost()
919 {
920     HILOGD("enter");
921     BluetoothHost &host = BluetoothHost::GetDefaultHost();
922     host.RegisterObserver(g_connectionObserver);
923     host.RegisterRemoteDeviceObserver(g_remoteDeviceObserver);
924 }
925 
DealPairStatus(const int & status,int & bondStatus)926 void DealPairStatus(const int &status, int &bondStatus)
927 {
928     HILOGD("status is %{public}d", status);
929     switch (status) {
930         case PAIR_NONE:
931             bondStatus = static_cast<int>(BondState::BOND_STATE_INVALID);
932             break;
933         case PAIR_PAIRING:
934             bondStatus = static_cast<int>(BondState::BOND_STATE_BONDING);
935             break;
936         case PAIR_PAIRED:
937             bondStatus = static_cast<int>(BondState::BOND_STATE_BONDED);
938             break;
939         default:
940             break;
941     }
942 }
943 }  // namespace Bluetooth
944 }  // namespace OHOS