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_ble_utils"
17 #endif
18
19 #include "napi_bluetooth_utils.h"
20 #include "napi_bluetooth_ble_utils.h"
21 #include <algorithm>
22 #include <functional>
23 #include <optional>
24 #include "bluetooth_errorcode.h"
25 #include "bluetooth_log.h"
26 #include "napi/native_api.h"
27 #include "napi/native_node_api.h"
28 #include "napi_bluetooth_error.h"
29 #include "securec.h"
30
31 namespace OHOS {
32 namespace Bluetooth {
33 using namespace std;
ConvertGattServiceToJS(napi_env env,napi_value result,GattService & service)34 void ConvertGattServiceToJS(napi_env env, napi_value result, GattService& service)
35 {
36 napi_value serviceUuid;
37 napi_create_string_utf8(env, service.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &serviceUuid);
38 napi_set_named_property(env, result, "serviceUuid", serviceUuid);
39
40 napi_value isPrimary;
41 napi_get_boolean(env, service.IsPrimary(), &isPrimary);
42 napi_set_named_property(env, result, "isPrimary", isPrimary);
43 HILOGI("uuid: %{public}s, isPrimary: %{public}d", service.GetUuid().ToString().c_str(), service.IsPrimary());
44
45 napi_value characteristics;
46 napi_create_array(env, &characteristics);
47 ConvertBLECharacteristicVectorToJS(env, characteristics, service.GetCharacteristics());
48 napi_set_named_property(env, result, "characteristics", characteristics);
49
50 napi_value includeServices;
51 napi_create_array(env, &includeServices);
52 vector<GattService> services;
53 vector<std::reference_wrapper<GattService>> srvs = service.GetIncludedServices();
54 for (auto &srv : srvs) {
55 services.push_back(srv.get());
56 }
57 ConvertGattServiceVectorToJS(env, includeServices, services);
58 napi_set_named_property(env, result, "includeServices", includeServices);
59 }
60
ConvertGattServiceVectorToJS(napi_env env,napi_value result,vector<GattService> & services)61 void ConvertGattServiceVectorToJS(napi_env env, napi_value result, vector<GattService>& services)
62 {
63 HILOGI("enter");
64 size_t idx = 0;
65
66 if (services.empty()) {
67 return;
68 }
69 HILOGI("size: %{public}zu", services.size());
70 for (auto& service : services) {
71 napi_value obj = nullptr;
72 napi_create_object(env, &obj);
73 ConvertGattServiceToJS(env, obj, service);
74 napi_set_element(env, result, idx, obj);
75 idx++;
76 }
77 }
78
ConvertBLECharacteristicVectorToJS(napi_env env,napi_value result,vector<GattCharacteristic> & characteristics)79 void ConvertBLECharacteristicVectorToJS(napi_env env, napi_value result,
80 vector<GattCharacteristic>& characteristics)
81 {
82 HILOGI("size: %{public}zu", characteristics.size());
83 size_t idx = 0;
84 if (characteristics.empty()) {
85 return;
86 }
87
88 for (auto &characteristic : characteristics) {
89 napi_value obj = nullptr;
90 napi_create_object(env, &obj);
91 ConvertBLECharacteristicToJS(env, obj, characteristic);
92 napi_set_element(env, result, idx, obj);
93 idx++;
94 }
95 }
96
HasProperty(int properties,int propertyMask)97 bool HasProperty(int properties, int propertyMask)
98 {
99 if (properties < 0 || propertyMask < 0) {
100 HILOGE("properties or propertyMask is less than 0");
101 return false;
102 }
103 return (static_cast<unsigned int>(properties) & static_cast<unsigned int>(propertyMask)) != 0;
104 }
ConvertGattPropertiesToJs(napi_env env,int properties)105 napi_value ConvertGattPropertiesToJs(napi_env env, int properties)
106 {
107 napi_value object;
108 napi_create_object(env, &object);
109
110 napi_value value;
111 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::WRITE), &value);
112 napi_set_named_property(env, object, "write", value);
113
114 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::WRITE_WITHOUT_RESPONSE), &value);
115 napi_set_named_property(env, object, "writeNoResponse", value);
116
117 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::READ), &value);
118 napi_set_named_property(env, object, "read", value);
119
120 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::NOTIFY), &value);
121 napi_set_named_property(env, object, "notify", value);
122
123 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::INDICATE), &value);
124 napi_set_named_property(env, object, "indicate", value);
125
126 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::BROADCAST), &value);
127 napi_set_named_property(env, object, "broadcast", value);
128
129 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::AUTHENTICATED_SIGNED_WRITES), &value);
130 napi_set_named_property(env, object, "authenticatedSignedWrite", value);
131
132 napi_get_boolean(env, HasProperty(properties, GattCharacteristic::EXTENDED_PROPERTIES), &value);
133 napi_set_named_property(env, object, "extendedProperties", value);
134 return object;
135 }
136
HasPermission(int permissions,int permissionMask)137 bool HasPermission(int permissions, int permissionMask)
138 {
139 if (permissions < 0 || permissionMask < 0) {
140 HILOGE("permissions or permissionMask is less than 0");
141 return false;
142 }
143 return (static_cast<unsigned int>(permissions) & static_cast<unsigned int>(permissionMask)) != 0;
144 }
145
ConvertGattPermissionsToJs(napi_env env,int permissions)146 napi_value ConvertGattPermissionsToJs(napi_env env, int permissions)
147 {
148 napi_value object;
149 napi_create_object(env, &object);
150
151 napi_value value;
152 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::READABLE), &value);
153 napi_set_named_property(env, object, "read", value);
154
155 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::READ_ENCRYPTED), &value);
156 napi_set_named_property(env, object, "readEncrypted", value);
157
158 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::READ_ENCRYPTED_MITM), &value);
159 napi_set_named_property(env, object, "readEncryptedMitm", value);
160
161 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::WRITEABLE), &value);
162 napi_set_named_property(env, object, "write", value);
163
164 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::WRITE_ENCRYPTED), &value);
165 napi_set_named_property(env, object, "writeEncrypted", value);
166
167 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::WRITE_ENCRYPTED_MITM), &value);
168 napi_set_named_property(env, object, "writeEncryptedMitm", value);
169
170 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::WRITE_SIGNED), &value);
171 napi_set_named_property(env, object, "writeSigned", value);
172
173 napi_get_boolean(env, HasPermission(permissions, GattCharacteristic::WRITE_SIGNED_MITM), &value);
174 napi_set_named_property(env, object, "writeSignedMitm", value);
175
176 return object;
177 }
178
ConvertBLECharacteristicToJS(napi_env env,napi_value result,GattCharacteristic & characteristic)179 void ConvertBLECharacteristicToJS(napi_env env, napi_value result, GattCharacteristic& characteristic)
180 {
181 napi_value characteristicUuid;
182 HILOGI("uuid: %{public}s", characteristic.GetUuid().ToString().c_str());
183 napi_create_string_utf8(env, characteristic.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &characteristicUuid);
184 napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
185
186 if (characteristic.GetService() != nullptr) {
187 napi_value serviceUuid;
188 napi_create_string_utf8(env, characteristic.GetService()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
189 &serviceUuid);
190 napi_set_named_property(env, result, "serviceUuid", serviceUuid);
191 }
192
193 size_t valueSize = 0;
194 uint8_t* valueData = characteristic.GetValue(&valueSize).get();
195 {
196 napi_value value = nullptr;
197 uint8_t* bufferData = nullptr;
198 napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
199 if (valueSize > 0 && memcpy_s(bufferData, valueSize, valueData, valueSize) != EOK) {
200 HILOGE("memcpy_s failed");
201 return;
202 }
203 napi_set_named_property(env, result, "characteristicValue", value);
204 }
205
206 napi_value propertiesValue = ConvertGattPropertiesToJs(env, characteristic.GetProperties());
207 napi_set_named_property(env, result, "properties", propertiesValue);
208
209 napi_value permissionsValue = ConvertGattPermissionsToJs(env, characteristic.GetPermissions());
210 napi_set_named_property(env, result, "permissions", permissionsValue);
211
212 napi_value descriptors;
213 napi_create_array(env, &descriptors);
214 ConvertBLEDescriptorVectorToJS(env, descriptors, characteristic.GetDescriptors());
215 napi_set_named_property(env, result, "descriptors", descriptors);
216
217 napi_value characteristicValueHandle;
218 napi_create_uint32(env, static_cast<uint32_t>(characteristic.GetHandle()), &characteristicValueHandle);
219 napi_set_named_property(env, result, "characteristicValueHandle", characteristicValueHandle);
220 }
221
ConvertBLEDescriptorVectorToJS(napi_env env,napi_value result,vector<GattDescriptor> & descriptors)222 void ConvertBLEDescriptorVectorToJS(napi_env env, napi_value result, vector<GattDescriptor>& descriptors)
223 {
224 HILOGI("size: %{public}zu", descriptors.size());
225 size_t idx = 0;
226
227 if (descriptors.empty()) {
228 return;
229 }
230
231 for (auto& descriptor : descriptors) {
232 napi_value obj = nullptr;
233 napi_create_object(env, &obj);
234 ConvertBLEDescriptorToJS(env, obj, descriptor);
235 napi_set_element(env, result, idx, obj);
236 idx++;
237 }
238 }
239
ConvertBLEDescriptorToJS(napi_env env,napi_value result,GattDescriptor & descriptor)240 void ConvertBLEDescriptorToJS(napi_env env, napi_value result, GattDescriptor& descriptor)
241 {
242 HILOGI("uuid: %{public}s", descriptor.GetUuid().ToString().c_str());
243
244 napi_value descriptorUuid;
245 napi_create_string_utf8(env, descriptor.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &descriptorUuid);
246 napi_set_named_property(env, result, "descriptorUuid", descriptorUuid);
247
248 if (descriptor.GetCharacteristic() != nullptr) {
249 napi_value characteristicUuid;
250 napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
251 &characteristicUuid);
252 napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
253
254 if (descriptor.GetCharacteristic()->GetService() != nullptr) {
255 napi_value serviceUuid;
256 napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str(),
257 NAPI_AUTO_LENGTH, &serviceUuid);
258 napi_set_named_property(env, result, "serviceUuid", serviceUuid);
259 }
260 }
261
262 napi_value value;
263 size_t valueSize;
264 uint8_t* valueData = descriptor.GetValue(&valueSize).get();
265 uint8_t* bufferData = nullptr;
266 napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
267 if (memcpy_s(bufferData, valueSize, valueData, valueSize) != EOK) {
268 HILOGE("memcpy_s error");
269 }
270 napi_set_named_property(env, result, "descriptorValue", value);
271
272 napi_value descriptorHandle;
273 napi_create_uint32(env, static_cast<uint32_t>(descriptor.GetHandle()), &descriptorHandle);
274 napi_set_named_property(env, result, "descriptorHandle", descriptorHandle);
275
276 napi_value permissionsValue = ConvertGattPermissionsToJs(env, descriptor.GetPermissions());
277 napi_set_named_property(env, result, "permissions", permissionsValue);
278 }
279
ConvertCharacteristicReadReqToJS(napi_env env,napi_value result,const std::string & device,const GattCharacteristic & characteristic,int requestId)280 void ConvertCharacteristicReadReqToJS(napi_env env, napi_value result, const std::string &device,
281 const GattCharacteristic &characteristic, int requestId)
282 {
283 napi_value deviceId;
284 napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
285 napi_set_named_property(env, result, "deviceId", deviceId);
286
287 napi_value transId;
288 napi_create_int32(env, requestId, &transId);
289 napi_set_named_property(env, result, "transId", transId);
290
291 napi_value offset;
292 napi_create_int32(env, 0, &offset);
293 napi_set_named_property(env, result, "offset", offset);
294 HILOGI("offset is %{public}d", 0);
295
296 napi_value characteristicUuid;
297 napi_create_string_utf8(env, characteristic.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &characteristicUuid);
298 napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
299 HILOGI("characteristicUuid is %{public}s", characteristic.GetUuid().ToString().c_str());
300
301 if (characteristic.GetService() != nullptr) {
302 napi_value serviceUuid;
303 napi_create_string_utf8(env, characteristic.GetService()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
304 &serviceUuid);
305 napi_set_named_property(env, result, "serviceUuid", serviceUuid);
306 }
307 }
308
ConvertDescriptorReadReqToJS(napi_env env,napi_value result,const std::string & device,const GattDescriptor & descriptor,int requestId)309 void ConvertDescriptorReadReqToJS(napi_env env, napi_value result, const std::string &device,
310 const GattDescriptor& descriptor, int requestId)
311 {
312 napi_value deviceId;
313 napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
314 napi_set_named_property(env, result, "deviceId", deviceId);
315
316 napi_value transId;
317 napi_create_int32(env, requestId, &transId);
318 napi_set_named_property(env, result, "transId", transId);
319
320 napi_value offset;
321 napi_create_int32(env, 0, &offset);
322 napi_set_named_property(env, result, "offset", offset);
323 HILOGI("offset is %{public}d", 0);
324
325 napi_value descriptorUuid;
326 napi_create_string_utf8(env, descriptor.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &descriptorUuid);
327 napi_set_named_property(env, result, "descriptorUuid", descriptorUuid);
328 HILOGI("descriptorUuid is %{public}s", descriptor.GetUuid().ToString().c_str());
329
330 if (descriptor.GetCharacteristic() != nullptr) {
331 napi_value characteristicUuid;
332 napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
333 &characteristicUuid);
334 napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
335
336 if (descriptor.GetCharacteristic()->GetService() != nullptr) {
337 napi_value serviceUuid;
338 napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str(),
339 NAPI_AUTO_LENGTH, &serviceUuid);
340 napi_set_named_property(env, result, "serviceUuid", serviceUuid);
341 }
342 }
343 }
344
ConvertCharacteristicWriteReqToJS(napi_env env,napi_value result,const std::string & device,const GattCharacteristic & characteristic,int requestId)345 void ConvertCharacteristicWriteReqToJS(napi_env env, napi_value result, const std::string &device,
346 const GattCharacteristic& characteristic, int requestId)
347 {
348 napi_value deviceId;
349 napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
350 napi_set_named_property(env, result, "deviceId", deviceId);
351
352 napi_value transId;
353 napi_create_int32(env, requestId, &transId);
354 napi_set_named_property(env, result, "transId", transId);
355
356 napi_value offset;
357 napi_create_int32(env, 0, &offset);
358 napi_set_named_property(env, result, "offset", offset);
359 HILOGI("offset is %{public}d", 0);
360
361 napi_value isPrepared;
362 napi_get_boolean(env, false, &isPrepared);
363 napi_set_named_property(env, result, "isPrepared", isPrepared);
364
365 napi_value needRsp;
366 napi_get_boolean(env, characteristic.GetWriteType() == GattCharacteristic::WriteType::DEFAULT, &needRsp);
367 napi_set_named_property(env, result, "needRsp", needRsp);
368
369 napi_value value;
370 size_t valueSize;
371 uint8_t* valueData = characteristic.GetValue(&valueSize).get();
372 uint8_t* bufferData = nullptr;
373 napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
374 if (memcpy_s(bufferData, valueSize, valueData, valueSize) != EOK) {
375 HILOGE("memcpy_s error");
376 }
377 napi_set_named_property(env, result, "value", value);
378
379 napi_value characteristicUuid;
380 napi_create_string_utf8(env, characteristic.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &characteristicUuid);
381 napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
382 HILOGI("characteristicUuid is %{public}s", characteristic.GetUuid().ToString().c_str());
383
384 if (characteristic.GetService() != nullptr) {
385 napi_value serviceUuid;
386 napi_create_string_utf8(env, characteristic.GetService()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
387 &serviceUuid);
388 napi_set_named_property(env, result, "serviceUuid", serviceUuid);
389 }
390 }
391
ConvertDescriptorWriteReqToJS(napi_env env,napi_value result,const std::string & device,const GattDescriptor & descriptor,int requestId)392 void ConvertDescriptorWriteReqToJS(napi_env env, napi_value result, const std::string &device,
393 const GattDescriptor &descriptor, int requestId)
394 {
395 napi_value deviceId;
396 napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
397 napi_set_named_property(env, result, "deviceId", deviceId);
398
399 napi_value transId;
400 napi_create_int32(env, requestId, &transId);
401 napi_set_named_property(env, result, "transId", transId);
402
403 napi_value offset;
404 napi_create_int32(env, 0, &offset);
405 napi_set_named_property(env, result, "offset", offset);
406 HILOGI("offset is %{public}d", 0);
407
408 napi_value isPrepared;
409 napi_get_boolean(env, false, &isPrepared);
410 napi_set_named_property(env, result, "isPrepared", isPrepared);
411
412 napi_value needRsp;
413 napi_get_boolean(env, true, &needRsp);
414 napi_set_named_property(env, result, "needRsp", needRsp);
415
416 napi_value value;
417 size_t valueSize;
418 uint8_t* valueData = descriptor.GetValue(&valueSize).get();
419 uint8_t* bufferData = nullptr;
420 napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
421 if (memcpy_s(bufferData, valueSize, valueData, valueSize) != EOK) {
422 HILOGE("memcpy_s error");
423 }
424 napi_set_named_property(env, result, "value", value);
425
426 napi_value descriptorUuid;
427 napi_create_string_utf8(env, descriptor.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &descriptorUuid);
428 napi_set_named_property(env, result, "descriptorUuid", descriptorUuid);
429 HILOGI("descriptorUuid is %{public}s", descriptor.GetUuid().ToString().c_str());
430
431 if (descriptor.GetCharacteristic() != nullptr) {
432 napi_value characteristicUuid;
433 napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
434 &characteristicUuid);
435 napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
436
437 if (descriptor.GetCharacteristic()->GetService() != nullptr) {
438 napi_value serviceUuid;
439 napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str(),
440 NAPI_AUTO_LENGTH, &serviceUuid);
441 napi_set_named_property(env, result, "serviceUuid", serviceUuid);
442 }
443 }
444 }
445
ScanReportTypeInit(napi_env env)446 napi_value ScanReportTypeInit(napi_env env)
447 {
448 HILOGD("enter");
449 napi_value scanReportTypeObj = nullptr;
450 napi_create_object(env, &scanReportTypeObj);
451 SetNamedPropertyByInteger(
452 env, scanReportTypeObj, static_cast<int32_t>(ScanReportType::ON_FOUND), "ON_FOUND");
453 SetNamedPropertyByInteger(
454 env, scanReportTypeObj, static_cast<int32_t>(ScanReportType::ON_LOST), "ON_LOST");
455 SetNamedPropertyByInteger(
456 env, scanReportTypeObj, static_cast<int32_t>(ScanReportType::ON_BATCH), "ON_BATCH");
457 return scanReportTypeObj;
458 }
459
ScanDutyInit(napi_env env)460 napi_value ScanDutyInit(napi_env env)
461 {
462 HILOGD("enter");
463 napi_value scanDutyObj = nullptr;
464 napi_create_object(env, &scanDutyObj);
465 SetNamedPropertyByInteger(
466 env, scanDutyObj, static_cast<int>(ScanDuty::SCAN_MODE_LOW_POWER), "SCAN_MODE_LOW_POWER");
467 SetNamedPropertyByInteger(
468 env, scanDutyObj, static_cast<int>(ScanDuty::SCAN_MODE_BALANCED), "SCAN_MODE_BALANCED");
469 SetNamedPropertyByInteger(
470 env, scanDutyObj, static_cast<int>(ScanDuty::SCAN_MODE_LOW_LATENCY), "SCAN_MODE_LOW_LATENCY");
471 return scanDutyObj;
472 }
473
MatchModeInit(napi_env env)474 napi_value MatchModeInit(napi_env env)
475 {
476 HILOGD("enter");
477 napi_value matchModeObj = nullptr;
478 napi_create_object(env, &matchModeObj);
479 SetNamedPropertyByInteger(
480 env, matchModeObj, static_cast<int>(MatchMode::MATCH_MODE_AGGRESSIVE), "MATCH_MODE_AGGRESSIVE");
481 SetNamedPropertyByInteger(
482 env, matchModeObj, static_cast<int>(MatchMode::MATCH_MODE_STICKY), "MATCH_MODE_STICKY");
483 return matchModeObj;
484 }
485
PhyTypeInit(napi_env env)486 napi_value PhyTypeInit(napi_env env)
487 {
488 HILOGD("enter");
489 napi_value phyTypeObj = nullptr;
490 napi_create_object(env, &phyTypeObj);
491 SetNamedPropertyByInteger(
492 env, phyTypeObj, static_cast<int32_t>(PhyType::PHY_LE_1M), "PHY_LE_1M");
493 SetNamedPropertyByInteger(
494 env, phyTypeObj, static_cast<int32_t>(PhyType::PHY_LE_ALL_SUPPORTED), "PHY_LE_ALL_SUPPORTED");
495 return phyTypeObj;
496 }
497
ScanReportModeInit(napi_env env)498 napi_value ScanReportModeInit(napi_env env)
499 {
500 HILOGD("enter");
501 napi_value reportModeObj = nullptr;
502 napi_create_object(env, &reportModeObj);
503 SetNamedPropertyByInteger(
504 env, reportModeObj, static_cast<int32_t>(ScanReportMode::NORMAL), "NORMAL");
505 SetNamedPropertyByInteger(
506 env, reportModeObj, static_cast<int32_t>(ScanReportMode::BATCH), "BATCH");
507 SetNamedPropertyByInteger(
508 env, reportModeObj, static_cast<int32_t>(ScanReportMode::FENCE_SENSITIVITY_LOW), "FENCE_SENSITIVITY_LOW");
509 SetNamedPropertyByInteger(
510 env, reportModeObj, static_cast<int32_t>(ScanReportMode::FENCE_SENSITIVITY_HIGH), "FENCE_SENSITIVITY_HIGH");
511 return reportModeObj;
512 }
513
GattDisconnectReasonInit(napi_env env)514 napi_value GattDisconnectReasonInit(napi_env env)
515 {
516 napi_value gattDisconnectReasonObj = nullptr;
517 napi_create_object(env, &gattDisconnectReasonObj);
518 SetNamedPropertyByInteger(env, gattDisconnectReasonObj,
519 static_cast<int32_t>(GattDisconnectReason::CONN_TIMEOUT), "CONN_TIMEOUT");
520 SetNamedPropertyByInteger(env, gattDisconnectReasonObj,
521 static_cast<int32_t>(GattDisconnectReason::CONN_TERMINATE_PEER_USER), "CONN_TERMINATE_PEER_USER");
522 SetNamedPropertyByInteger(env, gattDisconnectReasonObj,
523 static_cast<int32_t>(GattDisconnectReason::CONN_TERMINATE_LOCAL_HOST), "CONN_TERMINATE_LOCAL_HOST");
524 SetNamedPropertyByInteger(env, gattDisconnectReasonObj,
525 static_cast<int32_t>(GattDisconnectReason::CONN_UNKNOWN), "CONN_UNKNOWN");
526 return gattDisconnectReasonObj;
527 }
528
SetGattClientDeviceId(const std::string & deviceId)529 void SetGattClientDeviceId(const std::string &deviceId)
530 {
531 deviceAddr = deviceId;
532 }
533
GetGattClientDeviceId()534 std::string GetGattClientDeviceId()
535 {
536 return deviceAddr;
537 }
538
ToNapiValue(napi_env env) const539 napi_value NapiNativeBleCharacteristic::ToNapiValue(napi_env env) const
540 {
541 napi_value object;
542 napi_create_object(env, &object);
543 ConvertBLECharacteristicToJS(env, object, const_cast<GattCharacteristic &>(character_));
544 return object;
545 }
546
ToNapiValue(napi_env env) const547 napi_value NapiNativeBleDescriptor::ToNapiValue(napi_env env) const
548 {
549 napi_value object;
550 napi_create_object(env, &object);
551 ConvertBLEDescriptorToJS(env, object, const_cast<GattDescriptor &>(descriptor_));
552 return object;
553 }
554
ToNapiValue(napi_env env) const555 napi_value NapiNativeGattServiceArray::ToNapiValue(napi_env env) const
556 {
557 napi_value object;
558 napi_create_array(env, &object);
559 ConvertGattServiceVectorToJS(env, object, const_cast<vector<GattService> &>(gattServices_));
560 return object;
561 }
562
ToNapiValue(napi_env env) const563 napi_value NapiNativeAdvertisingStateInfo::ToNapiValue(napi_env env) const
564 {
565 napi_value object;
566 napi_create_object(env, &object);
567
568 napi_value value;
569 napi_create_int32(env, advHandle_, &value);
570 napi_set_named_property(env, object, "advertisingId", value);
571
572 napi_create_int32(env, advState_, &value);
573 napi_set_named_property(env, object, "state", value);
574 return object;
575 }
576
ToNapiValue(napi_env env) const577 napi_value NapiNativeGattsCharacterReadRequest::ToNapiValue(napi_env env) const
578 {
579 napi_value result = nullptr;
580 napi_create_object(env, &result);
581
582 ConvertCharacteristicReadReqToJS(env, result, deviceAddr_, character_, transId_);
583 return result;
584 }
585
ToNapiValue(napi_env env) const586 napi_value NapiNativeGattsCharacterWriteRequest::ToNapiValue(napi_env env) const
587 {
588 napi_value result = nullptr;
589 napi_create_object(env, &result);
590
591 ConvertCharacteristicWriteReqToJS(env, result, deviceAddr_, character_, transId_);
592 return result;
593 }
594
ToNapiValue(napi_env env) const595 napi_value NapiNativeGattsDescriptorWriteRequest::ToNapiValue(napi_env env) const
596 {
597 napi_value result = nullptr;
598 napi_create_object(env, &result);
599
600 ConvertDescriptorWriteReqToJS(env, result, deviceAddr_, descriptor_, transId_);
601 return result;
602 }
603
ToNapiValue(napi_env env) const604 napi_value NapiNativeGattsDescriptorReadRequest::ToNapiValue(napi_env env) const
605 {
606 napi_value result = nullptr;
607 napi_create_object(env, &result);
608
609 ConvertDescriptorReadReqToJS(env, result, deviceAddr_, descriptor_, transId_);
610 return result;
611 }
612 } // namespace Bluetooth
613 } // namespace OHOS
614