• 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 
16 #include "napi_native_object.h"
17 
18 namespace OHOS {
19 namespace Bluetooth {
ToNapiValue(napi_env env) const20 napi_value NapiNativeInt::ToNapiValue(napi_env env) const
21 {
22     napi_value value = nullptr;
23     napi_status status = napi_create_int32(env, value_, &value);
24     if (status != napi_ok) {
25         HILOGE("napi_create_int32 failed");
26     }
27     return value;
28 }
29 
ToNapiValue(napi_env env) const30 napi_value NapiNativeBool::ToNapiValue(napi_env env) const
31 {
32     napi_value value = nullptr;
33     napi_status status = napi_get_boolean(env, value_, &value);
34     if (status != napi_ok) {
35         HILOGE("napi_create_int32 failed");
36     }
37     return value;
38 }
39 
ToNapiValue(napi_env env) const40 napi_value NapiNativeString::ToNapiValue(napi_env env) const
41 {
42     napi_value value = nullptr;
43     napi_status status = napi_create_string_utf8(env, value_.c_str(), NAPI_AUTO_LENGTH, &value);
44     if (status != napi_ok) {
45         HILOGE("napi_create_string_utf8 failed");
46     }
47     return value;
48 }
49 
ToNapiValue(napi_env env) const50 napi_value NapiNativeUuidsArray::ToNapiValue(napi_env env) const
51 {
52     napi_value array;
53     napi_create_array(env, &array);
54     ConvertUuidsVectorToJS(env, array, uuids_);
55     return array;
56 }
57 
ToNapiValue(napi_env env) const58 napi_value NapiNativeDiscoveryResultArray::ToNapiValue(napi_env env) const
59 {
60     CHECK_AND_RETURN_LOG_RET(remoteDevice_, nullptr, "remoteDevice is nullptr");
61 
62     napi_value array;
63     napi_value value;
64     std::string addr = remoteDevice_->GetDeviceAddr();
65     napi_create_array(env, &array);
66     napi_create_string_utf8(env, addr.c_str(), addr.size(), &value);
67     napi_set_element(env, array, 0, value);
68     return array;
69 }
70 
GetFormatPinCode(uint32_t pinType,uint32_t pinCode)71 static std::string GetFormatPinCode(uint32_t pinType, uint32_t pinCode)
72 {
73     std::string pinCodeStr = std::to_string(pinCode);
74     if (pinType != PIN_TYPE_CONFIRM_PASSKEY && pinType != PIN_TYPE_NOTIFY_PASSKEY) {
75         return pinCodeStr;
76     }
77 
78     const uint32_t FORMAT_PINCODE_LENGTH = 6;
79     while (pinCodeStr.length() < FORMAT_PINCODE_LENGTH) {
80         pinCodeStr = "0" + pinCodeStr;
81     }
82     return pinCodeStr;
83 }
84 
ToNapiValue(napi_env env) const85 napi_value NapiNativePinRequiredParam::ToNapiValue(napi_env env) const
86 {
87     CHECK_AND_RETURN_LOG_RET(pairConfirmInfo_, nullptr, "pairConfirmInfo is nullptr");
88 
89     napi_value result = nullptr;
90     napi_create_object(env, &result);
91 
92     napi_value device = nullptr;
93     std::string addr = pairConfirmInfo_->deviceAddr;
94     napi_create_string_utf8(env, addr.c_str(), addr.size(), &device);
95     napi_set_named_property(env, result, "deviceId", device);
96 
97     napi_value pinCode = nullptr;
98     std::string pinCodeStr = GetFormatPinCode(pairConfirmInfo_->pinType, pairConfirmInfo_->number);
99     napi_create_string_utf8(env, pinCodeStr.c_str(), pinCodeStr.size(), &pinCode);
100     napi_set_named_property(env, result, "pinCode", pinCode);
101 
102     napi_value pinType = nullptr;
103     napi_create_int32(env, pairConfirmInfo_->pinType, &pinType);
104     napi_set_named_property(env, result, "pinType", pinType);
105     return result;
106 }
107 
ToNapiValue(napi_env env) const108 napi_value NapiNativeBondStateParam::ToNapiValue(napi_env env) const
109 {
110     napi_value result = nullptr;
111     napi_create_object(env, &result);
112 
113     napi_value device = nullptr;
114     napi_create_string_utf8(env, deviceAddr_.c_str(), deviceAddr_.size(), &device);
115     napi_set_named_property(env, result, "deviceId", device);
116 
117     napi_value bondState = nullptr;
118     napi_create_int32(env, bondStatus_, &bondState);
119     napi_set_named_property(env, result, "state", bondState);
120     return result;
121 }
122 }  // namespace Bluetooth
123 }  // namespace OHOS