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
ConvertDeviceClassToJS(napi_env env, napi_value result, int deviceClass)71 void ConvertDeviceClassToJS(napi_env env, napi_value result, int deviceClass)
72 {
73 BluetoothDeviceClass classOfDevice = BluetoothDeviceClass(deviceClass);
74 int tmpMajorClass = classOfDevice.GetMajorClass();
75 int tmpMajorMinorClass = classOfDevice.GetMajorMinorClass();
76
77 napi_value majorClass = 0;
78 napi_create_int32(env, tmpMajorClass, &majorClass);
79 napi_set_named_property(env, result, "majorClass", majorClass);
80 napi_value majorMinorClass = 0;
81 napi_create_int32(env, tmpMajorMinorClass, &majorMinorClass);
82 napi_set_named_property(env, result, "majorMinorClass", majorMinorClass);
83 napi_value cod = 0;
84 napi_create_int32(env, deviceClass, &cod);
85 napi_set_named_property(env, result, "classOfDevice", cod);
86 }
87
ToNapiValue(napi_env env) const88 napi_value NapiNativeDiscoveryInfoResultArray::ToNapiValue(napi_env env) const
89 {
90 CHECK_AND_RETURN_LOG_RET(remoteDevice_, nullptr, "remoteDevice is nullptr");
91
92 napi_value array;
93 napi_value result = nullptr;
94 napi_create_array(env, &array);
95 napi_create_object(env, &result);
96
97 napi_value device = nullptr;
98 std::string addr = remoteDevice_->GetDeviceAddr();
99 napi_create_string_utf8(env, addr.c_str(), addr.size(), &device);
100 napi_set_named_property(env, result, "deviceId", device);
101
102 napi_value rssi = nullptr;
103 napi_create_int32(env, rssi_, &rssi);
104 napi_set_named_property(env, result, "rssi", rssi);
105
106 napi_value deviceName = nullptr;
107 napi_create_string_utf8(env, deviceName_.c_str(), deviceName_.size(), &deviceName);
108 napi_set_named_property(env, result, "deviceName", deviceName);
109
110 napi_value deviceClass = nullptr;
111 napi_create_object(env, &deviceClass);
112 ConvertDeviceClassToJS(env, deviceClass, deviceClass_);
113 napi_set_named_property(env, result, "deviceClass", deviceClass);
114 napi_set_element(env, array, 0, result);
115 return array;
116 }
117
GetFormatPinCode(uint32_t pinType, uint32_t pinCode)118 static std::string GetFormatPinCode(uint32_t pinType, uint32_t pinCode)
119 {
120 std::string pinCodeStr = std::to_string(pinCode);
121 if (pinType != PIN_TYPE_CONFIRM_PASSKEY && pinType != PIN_TYPE_NOTIFY_PASSKEY) {
122 return pinCodeStr;
123 }
124
125 const uint32_t FORMAT_PINCODE_LENGTH = 6;
126 while (pinCodeStr.length() < FORMAT_PINCODE_LENGTH) {
127 pinCodeStr = "0" + pinCodeStr;
128 }
129 return pinCodeStr;
130 }
131
ToNapiValue(napi_env env) const132 napi_value NapiNativePinRequiredParam::ToNapiValue(napi_env env) const
133 {
134 CHECK_AND_RETURN_LOG_RET(pairConfirmInfo_, nullptr, "pairConfirmInfo is nullptr");
135
136 napi_value result = nullptr;
137 napi_create_object(env, &result);
138
139 napi_value device = nullptr;
140 std::string addr = pairConfirmInfo_->deviceAddr;
141 napi_create_string_utf8(env, addr.c_str(), addr.size(), &device);
142 napi_set_named_property(env, result, "deviceId", device);
143
144 napi_value pinCode = nullptr;
145 std::string pinCodeStr = GetFormatPinCode(pairConfirmInfo_->pinType, pairConfirmInfo_->number);
146 napi_create_string_utf8(env, pinCodeStr.c_str(), pinCodeStr.size(), &pinCode);
147 napi_set_named_property(env, result, "pinCode", pinCode);
148
149 napi_value pinType = nullptr;
150 napi_create_int32(env, pairConfirmInfo_->pinType, &pinType);
151 napi_set_named_property(env, result, "pinType", pinType);
152 return result;
153 }
154
ToNapiValue(napi_env env) const155 napi_value NapiNativeBondStateParam::ToNapiValue(napi_env env) const
156 {
157 napi_value result = nullptr;
158 napi_create_object(env, &result);
159
160 napi_value device = nullptr;
161 napi_create_string_utf8(env, deviceAddr_.c_str(), deviceAddr_.size(), &device);
162 napi_set_named_property(env, result, "deviceId", device);
163
164 napi_value bondState = nullptr;
165 napi_create_int32(env, bondStatus_, &bondState);
166 napi_set_named_property(env, result, "state", bondState);
167
168 napi_value unbondCause = nullptr;
169 napi_create_int32(env, unbondCause_, &unbondCause);
170 napi_set_named_property(env, result, "cause", unbondCause);
171 return result;
172 }
173
ToNapiValue(napi_env env) const174 napi_value NapiNativeStateChangeParam::ToNapiValue(napi_env env) const
175 {
176 napi_value result = nullptr;
177 napi_create_object(env, &result);
178
179 ConvertStateChangeParamToJS(env, result, deviceAddr_, connectState_, stateChangeCause_);
180 return result;
181 }
182
ToNapiValue(napi_env env) const183 napi_value NapiNativeOppTransferInformation::ToNapiValue(napi_env env) const
184 {
185 napi_value result = nullptr;
186 napi_create_object(env, &result);
187
188 ConvertOppTransferInformationToJS(env, result, information_);
189 return result;
190 }
191
ToNapiValue(napi_env env) const192 napi_value NapiNativeBatteryInfo::ToNapiValue(napi_env env) const
193 {
194 napi_value result = nullptr;
195 napi_create_object(env, &result);
196
197 napi_value value = nullptr;
198 napi_create_string_utf8(env, batteryInfo_.deviceId_.c_str(), batteryInfo_.deviceId_.size(), &value);
199 napi_set_named_property(env, result, "deviceId", value);
200 napi_create_int32(env, batteryInfo_.batteryLevel_, &value);
201 napi_set_named_property(env, result, "batteryLevel", value);
202 napi_create_int32(env, batteryInfo_.leftEarBatteryLevel_, &value);
203 napi_set_named_property(env, result, "leftEarBatteryLevel", value);
204 napi_create_int32(env, static_cast<int32_t>(batteryInfo_.leftEarChargeState_), &value);
205 napi_set_named_property(env, result, "leftEarChargeState", value);
206 napi_create_int32(env, batteryInfo_.rightEarBatteryLevel_, &value);
207 napi_set_named_property(env, result, "rightEarBatteryLevel", value);
208 napi_create_int32(env, static_cast<int32_t>(batteryInfo_.rightEarChargeState_), &value);
209 napi_set_named_property(env, result, "rightEarChargeState", value);
210 napi_create_int32(env, batteryInfo_.boxBatteryLevel_, &value);
211 napi_set_named_property(env, result, "boxBatteryLevel", value);
212 napi_create_int32(env, static_cast<int32_t>(batteryInfo_.boxChargeState_), &value);
213 napi_set_named_property(env, result, "boxChargeState", value);
214 return result;
215 }
216
217 } // namespace Bluetooth
218 } // namespace OHOS