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_native_object"
17 #endif
18 
19 #include "napi_native_object.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
ToNapiValue(napi_env env) const23 napi_value NapiNativeInt::ToNapiValue(napi_env env) const
24 {
25     napi_value value = nullptr;
26     napi_status status = napi_create_int32(env, value_, &value);
27     if (status != napi_ok) {
28         HILOGE("napi_create_int32 failed");
29     }
30     return value;
31 }
32 
ToNapiValue(napi_env env) const33 napi_value NapiNativeBool::ToNapiValue(napi_env env) const
34 {
35     napi_value value = nullptr;
36     napi_status status = napi_get_boolean(env, value_, &value);
37     if (status != napi_ok) {
38         HILOGE("napi_create_int32 failed");
39     }
40     return value;
41 }
42 
ToNapiValue(napi_env env) const43 napi_value NapiNativeString::ToNapiValue(napi_env env) const
44 {
45     napi_value value = nullptr;
46     napi_status status = napi_create_string_utf8(env, value_.c_str(), NAPI_AUTO_LENGTH, &value);
47     if (status != napi_ok) {
48         HILOGE("napi_create_string_utf8 failed");
49     }
50     return value;
51 }
52 
ToNapiValue(napi_env env) const53 napi_value NapiNativeUuidsArray::ToNapiValue(napi_env env) const
54 {
55     napi_value array;
56     napi_create_array(env, &array);
57     ConvertUuidsVectorToJS(env, array, uuids_);
58     return array;
59 }
60 
ToNapiValue(napi_env env) const61 napi_value NapiNativeDiscoveryResultArray::ToNapiValue(napi_env env) const
62 {
63     CHECK_AND_RETURN_LOG_RET(remoteDevice_, nullptr, "remoteDevice is nullptr");
64 
65     napi_value array;
66     napi_value value;
67     std::string addr = remoteDevice_->GetDeviceAddr();
68     napi_create_array(env, &array);
69     napi_create_string_utf8(env, addr.c_str(), addr.size(), &value);
70     napi_set_element(env, array, 0, value);
71     return array;
72 }
73 
ConvertDeviceClassToJS(napi_env env,napi_value result,int deviceClass)74 void ConvertDeviceClassToJS(napi_env env, napi_value result, int deviceClass)
75 {
76     BluetoothDeviceClass classOfDevice = BluetoothDeviceClass(deviceClass);
77     int tmpMajorClass = classOfDevice.GetMajorClass();
78     int tmpMajorMinorClass = classOfDevice.GetMajorMinorClass();
79 
80     napi_value majorClass = 0;
81     napi_create_int32(env, tmpMajorClass, &majorClass);
82     napi_set_named_property(env, result, "majorClass", majorClass);
83     napi_value majorMinorClass = 0;
84     napi_create_int32(env, tmpMajorMinorClass, &majorMinorClass);
85     napi_set_named_property(env, result, "majorMinorClass", majorMinorClass);
86     napi_value cod = 0;
87     napi_create_int32(env, deviceClass, &cod);
88     napi_set_named_property(env, result, "classOfDevice", cod);
89 }
90 
ToNapiValue(napi_env env) const91 napi_value NapiNativeDiscoveryInfoResultArray::ToNapiValue(napi_env env) const
92 {
93     CHECK_AND_RETURN_LOG_RET(remoteDevice_, nullptr, "remoteDevice is nullptr");
94 
95     napi_value array;
96     napi_value result = nullptr;
97     napi_create_array(env, &array);
98     napi_create_object(env, &result);
99 
100     napi_value device = nullptr;
101     std::string addr = remoteDevice_->GetDeviceAddr();
102     napi_create_string_utf8(env, addr.c_str(), addr.size(), &device);
103     napi_set_named_property(env, result, "deviceId", device);
104 
105     napi_value rssi = nullptr;
106     napi_create_int32(env, rssi_, &rssi);
107     napi_set_named_property(env, result, "rssi", rssi);
108 
109     napi_value deviceName = nullptr;
110     napi_create_string_utf8(env, deviceName_.c_str(), deviceName_.size(), &deviceName);
111     napi_set_named_property(env, result, "deviceName", deviceName);
112 
113     napi_value deviceClass = nullptr;
114     napi_create_object(env, &deviceClass);
115     ConvertDeviceClassToJS(env, deviceClass, deviceClass_);
116     napi_set_named_property(env, result, "deviceClass", deviceClass);
117     napi_set_element(env, array, 0, result);
118     return array;
119 }
120 
GetFormatPinCode(uint32_t pinType,uint32_t pinCode)121 static std::string GetFormatPinCode(uint32_t pinType, uint32_t pinCode)
122 {
123     std::string pinCodeStr = std::to_string(pinCode);
124     if (pinType != PIN_TYPE_CONFIRM_PASSKEY && pinType != PIN_TYPE_NOTIFY_PASSKEY) {
125         return pinCodeStr;
126     }
127 
128     const uint32_t FORMAT_PINCODE_LENGTH = 6;
129     while (pinCodeStr.length() < FORMAT_PINCODE_LENGTH) {
130         pinCodeStr = "0" + pinCodeStr;
131     }
132     return pinCodeStr;
133 }
134 
ToNapiValue(napi_env env) const135 napi_value NapiNativePinRequiredParam::ToNapiValue(napi_env env) const
136 {
137     CHECK_AND_RETURN_LOG_RET(pairConfirmInfo_, nullptr, "pairConfirmInfo is nullptr");
138 
139     napi_value result = nullptr;
140     napi_create_object(env, &result);
141 
142     napi_value device = nullptr;
143     std::string addr = pairConfirmInfo_->deviceAddr;
144     napi_create_string_utf8(env, addr.c_str(), addr.size(), &device);
145     napi_set_named_property(env, result, "deviceId", device);
146 
147     napi_value pinCode = nullptr;
148     std::string pinCodeStr = GetFormatPinCode(pairConfirmInfo_->pinType, pairConfirmInfo_->number);
149     napi_create_string_utf8(env, pinCodeStr.c_str(), pinCodeStr.size(), &pinCode);
150     napi_set_named_property(env, result, "pinCode", pinCode);
151 
152     napi_value pinType = nullptr;
153     napi_create_int32(env, pairConfirmInfo_->pinType, &pinType);
154     napi_set_named_property(env, result, "pinType", pinType);
155     return result;
156 }
157 
ToNapiValue(napi_env env) const158 napi_value NapiNativeBondStateParam::ToNapiValue(napi_env env) const
159 {
160     napi_value result = nullptr;
161     napi_create_object(env, &result);
162 
163     napi_value device = nullptr;
164     napi_create_string_utf8(env, deviceAddr_.c_str(), deviceAddr_.size(), &device);
165     napi_set_named_property(env, result, "deviceId", device);
166 
167     napi_value bondState = nullptr;
168     napi_create_int32(env, bondStatus_, &bondState);
169     napi_set_named_property(env, result, "state", bondState);
170 
171     napi_value unbondCause = nullptr;
172     napi_create_int32(env, unbondCause_, &unbondCause);
173     napi_set_named_property(env, result, "cause", unbondCause);
174     return result;
175 }
176 
ToNapiValue(napi_env env) const177 napi_value NapiNativeStateChangeParam::ToNapiValue(napi_env env) const
178 {
179     napi_value result = nullptr;
180     napi_create_object(env, &result);
181 
182     ConvertStateChangeParamToJS(env, result, deviceAddr_, connectState_, stateChangeCause_);
183     return result;
184 }
185 
ToNapiValue(napi_env env) const186 napi_value NapiNativeOppTransferInformation::ToNapiValue(napi_env env) const
187 {
188     napi_value result = nullptr;
189     napi_create_object(env, &result);
190 
191     ConvertOppTransferInformationToJS(env, result, information_);
192     return result;
193 }
194 
ToNapiValue(napi_env env) const195 napi_value NapiNativeBatteryInfo::ToNapiValue(napi_env env) const
196 {
197     napi_value result = nullptr;
198     napi_create_object(env, &result);
199 
200     napi_value value = nullptr;
201     napi_create_string_utf8(env, batteryInfo_.deviceId_.c_str(), batteryInfo_.deviceId_.size(), &value);
202     napi_set_named_property(env, result, "deviceId", value);
203     napi_create_int32(env, batteryInfo_.batteryLevel_, &value);
204     napi_set_named_property(env, result, "batteryLevel", value);
205     napi_create_int32(env, batteryInfo_.leftEarBatteryLevel_, &value);
206     napi_set_named_property(env, result, "leftEarBatteryLevel", value);
207     napi_create_int32(env, static_cast<int32_t>(batteryInfo_.leftEarChargeState_), &value);
208     napi_set_named_property(env, result, "leftEarChargeState", value);
209     napi_create_int32(env, batteryInfo_.rightEarBatteryLevel_, &value);
210     napi_set_named_property(env, result, "rightEarBatteryLevel", value);
211     napi_create_int32(env, static_cast<int32_t>(batteryInfo_.rightEarChargeState_), &value);
212     napi_set_named_property(env, result, "rightEarChargeState", value);
213     napi_create_int32(env, batteryInfo_.boxBatteryLevel_, &value);
214     napi_set_named_property(env, result, "boxBatteryLevel", value);
215     napi_create_int32(env, static_cast<int32_t>(batteryInfo_.boxChargeState_), &value);
216     napi_set_named_property(env, result, "boxChargeState", value);
217     return result;
218 }
219 
220 }  // namespace Bluetooth
221 }  // namespace OHOS