• 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_napi_native_object"
17 #endif
18 
19 #include "napi_native_object.h"
20 #include "securec.h"
21 
22 namespace OHOS {
23 namespace Bluetooth {
ToNapiValue(napi_env env) const24 napi_value NapiNativeInt::ToNapiValue(napi_env env) const
25 {
26     napi_value value = nullptr;
27     napi_status status = napi_create_int32(env, value_, &value);
28     if (status != napi_ok) {
29         HILOGE("napi_create_int32 failed");
30     }
31     return value;
32 }
33 
ToNapiValue(napi_env env) const34 napi_value NapiNativeBool::ToNapiValue(napi_env env) const
35 {
36     napi_value value = nullptr;
37     napi_status status = napi_get_boolean(env, value_, &value);
38     if (status != napi_ok) {
39         HILOGE("napi_create_int32 failed");
40     }
41     return value;
42 }
43 
ToNapiValue(napi_env env) const44 napi_value NapiNativeString::ToNapiValue(napi_env env) const
45 {
46     napi_value value = nullptr;
47     napi_status status = napi_create_string_utf8(env, value_.c_str(), NAPI_AUTO_LENGTH, &value);
48     if (status != napi_ok) {
49         HILOGE("napi_create_string_utf8 failed");
50     }
51     return value;
52 }
53 
ToNapiValue(napi_env env) const54 napi_value NapiNativeUuidsArray::ToNapiValue(napi_env env) const
55 {
56     napi_value array;
57     napi_create_array(env, &array);
58     ConvertUuidsVectorToJS(env, array, uuids_);
59     return array;
60 }
61 
ToNapiValue(napi_env env) const62 napi_value NapiNativeDiscoveryResultArray::ToNapiValue(napi_env env) const
63 {
64     CHECK_AND_RETURN_LOG_RET(remoteDevice_, nullptr, "remoteDevice is nullptr");
65 
66     napi_value array;
67     napi_value value;
68     std::string addr = remoteDevice_->GetDeviceAddr();
69     napi_create_array(env, &array);
70     napi_create_string_utf8(env, addr.c_str(), addr.size(), &value);
71     napi_set_element(env, array, 0, value);
72     return array;
73 }
74 
ConvertDeviceClassToJS(napi_env env,napi_value result,int deviceClass)75 void ConvertDeviceClassToJS(napi_env env, napi_value result, int deviceClass)
76 {
77     BluetoothDeviceClass classOfDevice = BluetoothDeviceClass(deviceClass);
78     int tmpMajorClass = classOfDevice.GetMajorClass();
79     int tmpMajorMinorClass = classOfDevice.GetMajorMinorClass();
80 
81     napi_value majorClass = 0;
82     napi_create_int32(env, tmpMajorClass, &majorClass);
83     napi_set_named_property(env, result, "majorClass", majorClass);
84     napi_value majorMinorClass = 0;
85     napi_create_int32(env, tmpMajorMinorClass, &majorMinorClass);
86     napi_set_named_property(env, result, "majorMinorClass", majorMinorClass);
87     napi_value cod = 0;
88     napi_create_int32(env, deviceClass, &cod);
89     napi_set_named_property(env, result, "classOfDevice", cod);
90 }
91 
ToNapiValue(napi_env env) const92 napi_value NapiNativeDiscoveryInfoResultArray::ToNapiValue(napi_env env) const
93 {
94     CHECK_AND_RETURN_LOG_RET(remoteDevice_, nullptr, "remoteDevice is nullptr");
95 
96     napi_value array;
97     napi_value result = nullptr;
98     napi_create_array(env, &array);
99     napi_create_object(env, &result);
100 
101     napi_value device = nullptr;
102     std::string addr = remoteDevice_->GetDeviceAddr();
103     napi_create_string_utf8(env, addr.c_str(), addr.size(), &device);
104     napi_set_named_property(env, result, "deviceId", device);
105 
106     napi_value rssi = nullptr;
107     napi_create_int32(env, rssi_, &rssi);
108     napi_set_named_property(env, result, "rssi", rssi);
109 
110     napi_value deviceName = nullptr;
111     napi_create_string_utf8(env, deviceName_.c_str(), deviceName_.size(), &deviceName);
112     napi_set_named_property(env, result, "deviceName", deviceName);
113 
114     napi_value deviceClass = nullptr;
115     napi_create_object(env, &deviceClass);
116     ConvertDeviceClassToJS(env, deviceClass, deviceClass_);
117     napi_set_named_property(env, result, "deviceClass", deviceClass);
118     napi_set_element(env, array, 0, result);
119     return array;
120 }
121 
GetFormatPinCode(uint32_t pinType,uint32_t pinCode)122 static std::string GetFormatPinCode(uint32_t pinType, uint32_t pinCode)
123 {
124     std::string pinCodeStr = std::to_string(pinCode);
125     if (pinType != PIN_TYPE_CONFIRM_PASSKEY && pinType != PIN_TYPE_NOTIFY_PASSKEY) {
126         return pinCodeStr;
127     }
128 
129     const uint32_t FORMAT_PINCODE_LENGTH = 6;
130     while (pinCodeStr.length() < FORMAT_PINCODE_LENGTH) {
131         pinCodeStr = "0" + pinCodeStr;
132     }
133     return pinCodeStr;
134 }
135 
ToNapiValue(napi_env env) const136 napi_value NapiNativePinRequiredParam::ToNapiValue(napi_env env) const
137 {
138     CHECK_AND_RETURN_LOG_RET(pairConfirmInfo_, nullptr, "pairConfirmInfo is nullptr");
139 
140     napi_value result = nullptr;
141     napi_create_object(env, &result);
142 
143     napi_value device = nullptr;
144     std::string addr = pairConfirmInfo_->deviceAddr;
145     napi_create_string_utf8(env, addr.c_str(), addr.size(), &device);
146     napi_set_named_property(env, result, "deviceId", device);
147 
148     napi_value pinCode = nullptr;
149     std::string pinCodeStr = GetFormatPinCode(pairConfirmInfo_->pinType, pairConfirmInfo_->number);
150     napi_create_string_utf8(env, pinCodeStr.c_str(), pinCodeStr.size(), &pinCode);
151     napi_set_named_property(env, result, "pinCode", pinCode);
152 
153     napi_value pinType = nullptr;
154     napi_create_int32(env, pairConfirmInfo_->pinType, &pinType);
155     napi_set_named_property(env, result, "pinType", pinType);
156     return result;
157 }
158 
ToNapiValue(napi_env env) const159 napi_value NapiNativeBondStateParam::ToNapiValue(napi_env env) const
160 {
161     napi_value result = nullptr;
162     napi_create_object(env, &result);
163 
164     napi_value device = nullptr;
165     napi_create_string_utf8(env, deviceAddr_.c_str(), deviceAddr_.size(), &device);
166     napi_set_named_property(env, result, "deviceId", device);
167 
168     napi_value bondState = nullptr;
169     napi_create_int32(env, bondStatus_, &bondState);
170     napi_set_named_property(env, result, "state", bondState);
171 
172     napi_value unbondCause = nullptr;
173     napi_create_int32(env, unbondCause_, &unbondCause);
174     napi_set_named_property(env, result, "cause", unbondCause);
175     return result;
176 }
177 
ToNapiValue(napi_env env) const178 napi_value NapiNativeStateChangeParam::ToNapiValue(napi_env env) const
179 {
180     napi_value result = nullptr;
181     napi_create_object(env, &result);
182 
183     ConvertStateChangeParamToJS(env, result, deviceAddr_, connectState_, stateChangeCause_);
184     return result;
185 }
186 
ToNapiValue(napi_env env) const187 napi_value NapiNativeOppTransferInformation::ToNapiValue(napi_env env) const
188 {
189     napi_value result = nullptr;
190     napi_create_object(env, &result);
191 
192     ConvertOppTransferInformationToJS(env, result, information_);
193     return result;
194 }
195 
ToNapiValue(napi_env env) const196 napi_value NapiNativeInt64::ToNapiValue(napi_env env) const
197 {
198     napi_value value = nullptr;
199     napi_status status = napi_create_int64(env, value_, &value);
200     if (status != napi_ok) {
201         HILOGE("napi_create_int64 failed");
202     }
203     return value;
204 }
205 
ToNapiValue(napi_env env) const206 napi_value NapiNativeBatteryInfo::ToNapiValue(napi_env env) const
207 {
208     napi_value result = nullptr;
209     napi_create_object(env, &result);
210 
211     napi_value value = nullptr;
212     napi_create_string_utf8(env, batteryInfo_.deviceId_.c_str(), batteryInfo_.deviceId_.size(), &value);
213     napi_set_named_property(env, result, "deviceId", value);
214     napi_create_int32(env, batteryInfo_.batteryLevel_, &value);
215     napi_set_named_property(env, result, "batteryLevel", value);
216     napi_create_int32(env, batteryInfo_.leftEarBatteryLevel_, &value);
217     napi_set_named_property(env, result, "leftEarBatteryLevel", value);
218     napi_create_int32(env, static_cast<int32_t>(batteryInfo_.leftEarChargeState_), &value);
219     napi_set_named_property(env, result, "leftEarChargeState", value);
220     napi_create_int32(env, batteryInfo_.rightEarBatteryLevel_, &value);
221     napi_set_named_property(env, result, "rightEarBatteryLevel", value);
222     napi_create_int32(env, static_cast<int32_t>(batteryInfo_.rightEarChargeState_), &value);
223     napi_set_named_property(env, result, "rightEarChargeState", value);
224     napi_create_int32(env, batteryInfo_.boxBatteryLevel_, &value);
225     napi_set_named_property(env, result, "boxBatteryLevel", value);
226     napi_create_int32(env, static_cast<int32_t>(batteryInfo_.boxChargeState_), &value);
227     napi_set_named_property(env, result, "boxChargeState", value);
228     return result;
229 }
230 
ToNapiValue(napi_env env) const231 napi_value NapiNativeArrayBuffer::ToNapiValue(napi_env env) const
232 {
233     napi_value object = nullptr;
234     uint8_t* bufferData = nullptr;
235     napi_create_arraybuffer(env, sppBuffer_.len_, (void**)&bufferData, &object);
236     if (sppBuffer_.len_ <= 0) {
237         HILOGE("bufferSize_ < 0, bufferSize_: %{public}zu", sppBuffer_.len_);
238         return object;
239     }
240     if (memset_s(bufferData, sppBuffer_.len_, 0, sppBuffer_.len_) != EOK) {
241         HILOGE("memset_s error");
242     }
243     if (memcpy_s(bufferData, sppBuffer_.len_, sppBuffer_.data_, sppBuffer_.len_) != EOK) {
244         HILOGE("memcpy_s error");
245     }
246     return object;
247 }
248 
ToNapiValue(napi_env env) const249 napi_value NapiNativeStringArray::ToNapiValue(napi_env env) const
250 {
251     napi_value object = nullptr;
252     napi_create_array(env, &object);
253     ConvertStringVectorToJS(env, object, value_);
254     return object;
255 }
256 }  // namespace Bluetooth
257 }  // namespace OHOS