• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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_bluetooth_utils.h"
17 #include <functional>
18 #include <optional>
19 #include <string>
20 #include <vector>
21 #include "bluetooth_log.h"
22 #include "bluetooth_utils.h"
23 #include "napi/native_api.h"
24 #include "napi/native_node_api.h"
25 #include "napi_bluetooth_spp_client.h"
26 #include "securec.h"
27 
28 namespace OHOS {
29 namespace Bluetooth {
30 using namespace std;
31 
GetCallbackErrorValue(napi_env env,int errCode)32 napi_value GetCallbackErrorValue(napi_env env, int errCode)
33 {
34     HILOGE("GetCallbackErrorValue");
35     napi_value result = nullptr;
36     napi_value eCode = nullptr;
37     napi_create_int32(env, errCode, &eCode);
38     napi_create_object(env, &result);
39     napi_set_named_property(env, result, "code", eCode);
40     return result;
41 }
42 
ParseString(napi_env env,string & param,napi_value args)43 bool ParseString(napi_env env, string &param, napi_value args)
44 {
45     napi_valuetype valuetype;
46     napi_typeof(env, args, &valuetype);
47 
48     HILOGI("param=%{public}d.", valuetype);
49     if (valuetype != napi_string) {
50         HILOGE("Wrong argument type. String expected.");
51         return false;
52     }
53     size_t size = 0;
54 
55     if (napi_get_value_string_utf8(env, args, nullptr, 0, &size) != napi_ok) {
56         HILOGE("can not get string size");
57         param = "";
58         return false;
59     }
60     param.reserve(size + 1);
61     param.resize(size);
62     if (napi_get_value_string_utf8(env, args, param.data(), (size + 1), &size) != napi_ok) {
63         HILOGE("can not get string value");
64         param = "";
65         return false;
66     }
67     return true;
68 }
69 
ParseInt32(napi_env env,int32_t & param,napi_value args)70 bool ParseInt32(napi_env env, int32_t &param, napi_value args)
71 {
72     napi_valuetype valuetype;
73     napi_typeof(env, args, &valuetype);
74 
75     HILOGI("param=%{public}d.", valuetype);
76     if (valuetype != napi_number) {
77         HILOGE("Wrong argument type. Int32 expected.");
78         return false;
79     }
80     napi_get_value_int32(env, args, &param);
81     return true;
82 }
83 
ParseBool(napi_env env,bool & param,napi_value args)84 bool ParseBool(napi_env env, bool &param, napi_value args)
85 {
86     napi_valuetype valuetype;
87     napi_typeof(env, args, &valuetype);
88 
89     HILOGI("param=%{public}d.", valuetype);
90     if (valuetype != napi_boolean) {
91         HILOGE("Wrong argument type. bool expected.");
92         return false;
93     }
94     napi_get_value_bool(env, args, &param);
95     return true;
96 }
97 
98 
ParseArrayBuffer(napi_env env,uint8_t ** data,size_t & size,napi_value args)99 bool ParseArrayBuffer(napi_env env, uint8_t** data, size_t &size, napi_value args)
100 {
101     napi_status status;
102     napi_valuetype valuetype;
103     napi_typeof(env, args, &valuetype);
104 
105     HILOGI("param=%{public}d.", valuetype);
106     if (valuetype != napi_object) {
107         HILOGE("Wrong argument type. object expected.");
108         return false;
109     }
110 
111     status = napi_get_arraybuffer_info(env, args, (void**)data, &size);
112     if (status != napi_ok) {
113         HILOGE("can not get arraybuffer, error is %{public}d", status);
114         (*data)[0] = -1;
115         return false;
116     }
117     HILOGE("arraybuffer size is %{public}zu", size);
118     HILOGE("arraybuffer is %{public}d", (*data)[0]);
119     return true;
120 }
121 
ConvertStringVectorToJS(napi_env env,napi_value result,std::vector<std::string> & stringVector)122 void ConvertStringVectorToJS(napi_env env, napi_value result, std::vector<std::string>& stringVector)
123 {
124     HILOGI("ConvertStringVectorToJS called");
125     size_t idx = 0;
126 
127     if (stringVector.empty()) {
128         return;
129     }
130     HILOGI("ConvertStringVectorToJS size is %{public}zu", stringVector.size());
131     for (auto& str : stringVector) {
132         napi_value obj = nullptr;
133         napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &obj);
134         napi_set_element(env, result, idx, obj);
135         idx++;
136     }
137 }
138 
ConvertGattServiceVectorToJS(napi_env env,napi_value result,vector<GattService> & services)139 void ConvertGattServiceVectorToJS(napi_env env, napi_value result, vector<GattService>& services)
140 {
141     HILOGI("ConverGattServiceVectorToJS called");
142     size_t idx = 0;
143 
144     if (services.empty()) {
145         return;
146     }
147     HILOGI("ConverGattServiceVectorToJS size is %{public}zu", services.size());
148     for (auto& service : services) {
149         napi_value obj = nullptr;
150         napi_create_object(env, &obj);
151         ConvertGattServiceToJS(env, obj, service);
152         napi_set_element(env, result, idx, obj);
153         idx++;
154     }
155 }
156 
ConvertGattServiceToJS(napi_env env,napi_value result,GattService & service)157 void ConvertGattServiceToJS(napi_env env, napi_value result, GattService& service)
158 {
159     HILOGI("ConvertGattServiceToJS called");
160 
161     napi_value serviceUuid;
162     napi_create_string_utf8(env, service.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &serviceUuid);
163     napi_set_named_property(env, result, "serviceUuid", serviceUuid);
164     HILOGI("ConvertGattServiceToJS serviceUuid is %{public}s", service.GetUuid().ToString().c_str());
165 
166     napi_value isPrimary;
167     napi_get_boolean(env, service.IsPrimary(), &isPrimary);
168     napi_set_named_property(env, result, "isPrimary", isPrimary);
169     HILOGI("ConvertGattServiceToJS isPrimary is %{public}d", service.IsPrimary());
170 
171     napi_value characteristics;
172     napi_create_array(env, &characteristics);
173     ConvertBLECharacteristicVectorToJS(env, characteristics, service.GetCharacteristics());
174     napi_set_named_property(env, result, "characteristics", characteristics);
175 
176     napi_value includedServices;
177     napi_create_array(env, &includedServices);
178     vector<GattService> services;
179     vector<std::reference_wrapper<GattService>> srvs = service.GetIncludedServices();
180     for (auto &srv : srvs) {
181         services.push_back(srv.get());
182     }
183     ConvertGattServiceVectorToJS(env, includedServices, services);
184     napi_set_named_property(env, result, "includedServices", includedServices);
185 }
186 
ConvertBLECharacteristicVectorToJS(napi_env env,napi_value result,vector<GattCharacteristic> & characteristics)187 void ConvertBLECharacteristicVectorToJS(napi_env env, napi_value result,
188     vector<GattCharacteristic>& characteristics)
189 {
190     HILOGI("ConvertBLECharacteristicVectorToJS called");
191     size_t idx = 0;
192     if (characteristics.empty()) {
193         return;
194     }
195 
196     for (auto &characteristic : characteristics) {
197         napi_value obj = nullptr;
198         napi_create_object(env, &obj);
199         ConvertBLECharacteristicToJS(env, obj, characteristic);
200         napi_set_element(env, result, idx, obj);
201         idx++;
202     }
203 }
204 
ConvertBLECharacteristicToJS(napi_env env,napi_value result,GattCharacteristic & characteristic)205 void ConvertBLECharacteristicToJS(napi_env env, napi_value result, GattCharacteristic& characteristic)
206 {
207     HILOGI("ConvertBLECharacteristicToJS called");
208 
209     napi_value characteristicUuid;
210     HILOGI("ConvertBLECharacteristicToJS uuid is %{public}s", characteristic.GetUuid().ToString().c_str());
211     napi_create_string_utf8(env, characteristic.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &characteristicUuid);
212     napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
213 
214     if (characteristic.GetService() != nullptr) {
215         napi_value serviceUuid;
216         napi_create_string_utf8(env, characteristic.GetService()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
217             &serviceUuid);
218         napi_set_named_property(env, result, "serviceUuid", serviceUuid);
219     }
220 
221     napi_value value;
222     size_t valueSize;
223     uint8_t* valueData = characteristic.GetValue(&valueSize).get();
224     uint8_t* bufferData = nullptr;
225     napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
226     if (memcpy_s(bufferData, valueSize, valueData, valueSize) != EOK) {
227         HILOGE("ConvertBLECharacteristicToJS memcpy_s failed");
228         return;
229     }
230     napi_set_named_property(env, result, "characteristicValue", value);
231 
232     napi_value descriptors;
233     napi_create_array(env, &descriptors);
234     ConvertBLEDescriptorVectorToJS(env, descriptors, characteristic.GetDescriptors());
235     napi_set_named_property(env, result, "descriptors", descriptors);
236 }
237 
238 
ConvertBLEDescriptorVectorToJS(napi_env env,napi_value result,vector<GattDescriptor> & descriptors)239 void ConvertBLEDescriptorVectorToJS(napi_env env, napi_value result, vector<GattDescriptor>& descriptors)
240 {
241     HILOGI("ConvertBLEDescriptorVectorToJS called");
242     size_t idx = 0;
243 
244     if (descriptors.empty()) {
245         return;
246     }
247     HILOGI("ConvertBLEDescriptorVectorToJS size is %{public}zu", descriptors.size());
248     for (auto& descriptor : descriptors) {
249         napi_value obj = nullptr;
250         napi_create_object(env, &obj);
251         ConvertBLEDescriptorToJS(env, obj, descriptor);
252         napi_set_element(env, result, idx, obj);
253         idx++;
254     }
255 }
256 
ConvertBLEDescriptorToJS(napi_env env,napi_value result,GattDescriptor & descriptor)257 void ConvertBLEDescriptorToJS(napi_env env, napi_value result, GattDescriptor& descriptor)
258 {
259     HILOGI("ConvertBLEDescriptorToJS called");
260 
261     napi_value descriptorUuid;
262     napi_create_string_utf8(env, descriptor.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &descriptorUuid);
263     napi_set_named_property(env, result, "descriptorUuid", descriptorUuid);
264     HILOGI("ConvertBLEDescriptorToJS descriptorUuid is %{public}s", descriptor.GetUuid().ToString().c_str());
265 
266     if (descriptor.GetCharacteristic() != nullptr) {
267         napi_value characteristicUuid;
268         napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
269             &characteristicUuid);
270         napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
271 
272         HILOGI("ConvertBLEDescriptorToJS characteristicUuid is %{public}s",
273             descriptor.GetCharacteristic()->GetUuid().ToString().c_str());
274 
275         if (descriptor.GetCharacteristic()->GetService() != nullptr) {
276             napi_value serviceUuid;
277             napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str(),
278                 NAPI_AUTO_LENGTH, &serviceUuid);
279             napi_set_named_property(env, result, "serviceUuid", serviceUuid);
280             HILOGI("ConvertBLEDescriptorToJS serviceUuid is %{public}s",
281                 descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str());
282         }
283     }
284 
285     napi_value value;
286     size_t valueSize;
287     uint8_t* valueData = descriptor.GetValue(&valueSize).get();
288     uint8_t* bufferData = nullptr;
289     napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
290     memcpy_s(bufferData, valueSize, valueData, valueSize);
291     napi_set_named_property(env, result, "descriptorValue", value);
292 }
293 
ConvertCharacteristicReadReqToJS(napi_env env,napi_value result,const std::string & device,GattCharacteristic & characteristic,int requestId)294 void ConvertCharacteristicReadReqToJS(napi_env env, napi_value result, const std::string &device,
295     GattCharacteristic &characteristic, int requestId)
296 {
297     HILOGI("ConvertCharacteristicReadReqToJS called");
298     napi_value deviceId;
299     napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
300     napi_set_named_property(env, result, "deviceId", deviceId);
301     HILOGI("ConvertCharacteristicReadReqToJS deviceId is %{public}s", GetEncryptAddr(device).c_str());
302 
303     napi_value transId;
304     napi_create_int32(env, requestId, &transId);
305     napi_set_named_property(env, result, "transId", transId);
306     HILOGI("ConvertCharacteristicReadReqToJS transId is %{public}d", requestId);
307 
308     napi_value offset;
309     napi_create_int32(env, 0, &offset);
310     napi_set_named_property(env, result, "offset", offset);
311     HILOGI("ConvertCharacteristicReadReqToJS offset is %{public}d", 0);
312 
313     napi_value characteristicUuid;
314     napi_create_string_utf8(env, characteristic.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &characteristicUuid);
315     napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
316     HILOGI("ConvertCharacteristicReadReqToJS characteristicUuid is %{public}s",
317         characteristic.GetUuid().ToString().c_str());
318 
319     if (characteristic.GetService() != nullptr) {
320         napi_value serviceUuid;
321         napi_create_string_utf8(env, characteristic.GetService()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
322             &serviceUuid);
323         napi_set_named_property(env, result, "serviceUuid", serviceUuid);
324         HILOGI("ConvertCharacteristicReadReqToJS serviceUuid is %{public}s",
325             characteristic.GetService()->GetUuid().ToString().c_str());
326     }
327 }
328 
ConvertDescriptorReadReqToJS(napi_env env,napi_value result,const std::string & device,GattDescriptor & descriptor,int requestId)329 void ConvertDescriptorReadReqToJS(napi_env env, napi_value result, const std::string &device,
330     GattDescriptor& descriptor, int requestId)
331 {
332     HILOGI("ConvertDescriptorReadReqToJS called");
333     napi_value deviceId;
334     napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
335     napi_set_named_property(env, result, "deviceId", deviceId);
336     HILOGI("ConvertDescriptorReadReqToJS deviceId is %{public}s", GetEncryptAddr(device).c_str());
337 
338     napi_value transId;
339     napi_create_int32(env, requestId, &transId);
340     napi_set_named_property(env, result, "transId", transId);
341     HILOGI("ConvertDescriptorReadReqToJS transId is %{public}d", requestId);
342 
343     napi_value offset;
344     napi_create_int32(env, 0, &offset);
345     napi_set_named_property(env, result, "offset", offset);
346     HILOGI("ConvertDescriptorReadReqToJS offset is %{public}d", 0);
347 
348     napi_value descriptorUuid;
349     napi_create_string_utf8(env, descriptor.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &descriptorUuid);
350     napi_set_named_property(env, result, "descriptorUuid", descriptorUuid);
351     HILOGI("ConvertDescriptorReadReqToJS descriptorUuid is %{public}s",
352         descriptor.GetUuid().ToString().c_str());
353 
354     if (descriptor.GetCharacteristic() != nullptr) {
355     napi_value characteristicUuid;
356     napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
357         &characteristicUuid);
358     napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
359     HILOGI("ConvertDescriptorReadReqToJS characteristicUuid is %{public}s",
360         descriptor.GetCharacteristic()->GetUuid().ToString().c_str());
361 
362         if (descriptor.GetCharacteristic()->GetService() != nullptr) {
363             napi_value serviceUuid;
364             napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str(),
365                 NAPI_AUTO_LENGTH, &serviceUuid);
366             napi_set_named_property(env, result, "serviceUuid", serviceUuid);
367             HILOGI("ConvertDescriptorReadReqToJS serviceUuid is %{public}s",
368                 descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str());
369         }
370     }
371 }
372 
ConvertCharacteristicWriteReqToJS(napi_env env,napi_value result,const std::string & device,GattCharacteristic & characteristic,int requestId)373 void ConvertCharacteristicWriteReqToJS(napi_env env, napi_value result, const std::string &device,
374     GattCharacteristic& characteristic, int requestId)
375 {
376     HILOGI("ConvertCharacteristicWriteReqToJS called");
377     napi_value deviceId;
378     napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
379     napi_set_named_property(env, result, "deviceId", deviceId);
380     HILOGI("ConvertCharacteristicWriteReqToJS deviceId is %{public}s", GetEncryptAddr(device).c_str());
381 
382     napi_value transId;
383     napi_create_int32(env, requestId, &transId);
384     napi_set_named_property(env, result, "transId", transId);
385     HILOGI("ConvertCharacteristicWriteReqToJS transId is %{public}d", requestId);
386 
387     napi_value offset;
388     napi_create_int32(env, 0, &offset);
389     napi_set_named_property(env, result, "offset", offset);
390     HILOGI("ConvertCharacteristicWriteReqToJS offset is %{public}d", 0);
391 
392 
393     napi_value isPrep;
394     napi_get_boolean(env, false, &isPrep);
395     napi_set_named_property(env, result, "isPrep", isPrep);
396 
397     napi_value needRsp;
398     napi_get_boolean(env, true, &needRsp);
399     napi_set_named_property(env, result, "needRsp", needRsp);
400 
401     napi_value value;
402     size_t valueSize;
403     uint8_t* valueData = characteristic.GetValue(&valueSize).get();
404     uint8_t* bufferData = nullptr;
405     napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
406     memcpy_s(bufferData, valueSize, valueData, valueSize);
407     napi_set_named_property(env, result, "value", value);
408 
409     napi_value characteristicUuid;
410     napi_create_string_utf8(env, characteristic.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &characteristicUuid);
411     napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
412     HILOGI("ConvertCharacteristicWriteReqToJS characteristicUuid is %{public}s",
413         characteristic.GetUuid().ToString().c_str());
414 
415     if (characteristic.GetService() != nullptr) {
416         napi_value serviceUuid;
417         napi_create_string_utf8(env, characteristic.GetService()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
418             &serviceUuid);
419         napi_set_named_property(env, result, "serviceUuid", serviceUuid);
420         HILOGI("ConvertCharacteristicWriteReqToJS serviceUuid is %{public}s",
421             characteristic.GetService()->GetUuid().ToString().c_str());
422     }
423 }
424 
ConvertDescriptorWriteReqToJS(napi_env env,napi_value result,const std::string & device,GattDescriptor & descriptor,int requestId)425 void ConvertDescriptorWriteReqToJS(napi_env env, napi_value result, const std::string &device,
426     GattDescriptor &descriptor, int requestId)
427 {
428     HILOGI("ConvertDescriptorWriteReqToJS called");
429     napi_value deviceId;
430     napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
431     napi_set_named_property(env, result, "deviceId", deviceId);
432     HILOGI("ConvertCharacteristicWriteReqToJS deviceId is %{public}s", GetEncryptAddr(device).c_str());
433 
434     napi_value transId;
435     napi_create_int32(env, requestId, &transId);
436     napi_set_named_property(env, result, "transId", transId);
437     HILOGI("ConvertCharacteristicWriteReqToJS transId is %{public}d", requestId);
438 
439     napi_value offset;
440     napi_create_int32(env, 0, &offset);
441     napi_set_named_property(env, result, "offset", offset);
442     HILOGI("ConvertCharacteristicWriteReqToJS offset is %{public}d", 0);
443 
444     napi_value isPrep;
445     napi_get_boolean(env, false, &isPrep);
446     napi_set_named_property(env, result, "isPrep", isPrep);
447 
448     napi_value needRsp;
449     napi_get_boolean(env, true, &needRsp);
450     napi_set_named_property(env, result, "needRsp", needRsp);
451 
452     napi_value value;
453     size_t valueSize;
454     uint8_t* valueData = descriptor.GetValue(&valueSize).get();
455     uint8_t* bufferData = nullptr;
456     napi_create_arraybuffer(env, valueSize, (void**)&bufferData, &value);
457     memcpy_s(bufferData, valueSize, valueData, valueSize);
458     napi_set_named_property(env, result, "value", value);
459 
460     napi_value descriptorUuid;
461     napi_create_string_utf8(env, descriptor.GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH, &descriptorUuid);
462     napi_set_named_property(env, result, "descriptorUuid", descriptorUuid);
463     HILOGI("ConvertDescriptorWriteReqToJS descriptorUuid is %{public}s",
464         descriptor.GetUuid().ToString().c_str());
465 
466     if (descriptor.GetCharacteristic() != nullptr) {
467         napi_value characteristicUuid;
468         napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetUuid().ToString().c_str(), NAPI_AUTO_LENGTH,
469             &characteristicUuid);
470         napi_set_named_property(env, result, "characteristicUuid", characteristicUuid);
471         HILOGI("ConvertDescriptorWriteReqToJS characteristicUuid is %{public}s",
472             descriptor.GetCharacteristic()->GetUuid().ToString().c_str());
473         if (descriptor.GetCharacteristic()->GetService() != nullptr) {
474             napi_value serviceUuid;
475             napi_create_string_utf8(env, descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str(),
476                 NAPI_AUTO_LENGTH, &serviceUuid);
477             napi_set_named_property(env, result, "serviceUuid", serviceUuid);
478             HILOGI("ConvertDescriptorWriteReqToJS serviceUuid is %{public}s",
479                 descriptor.GetCharacteristic()->GetService()->GetUuid().ToString().c_str());
480         }
481     }
482 }
483 
ConvertStateChangeParamToJS(napi_env env,napi_value result,const std::string & device,int state)484 void ConvertStateChangeParamToJS(napi_env env, napi_value result, const std::string &device, int state)
485 {
486     HILOGI("ConvertStateChangeParamToJS called");
487     napi_value deviceId = nullptr;
488     napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
489     napi_set_named_property(env, result, "deviceId", deviceId);
490     HILOGI("ConvertStateChangeParamToJS deviceId is %{public}s", GetEncryptAddr(device).c_str());
491 
492     napi_value profileState = nullptr;
493     napi_create_int32(env, GetProfileConnectionState(state), &profileState);
494     napi_set_named_property(env, result, "state", profileState);
495     HILOGI("ConvertStateChangeParamToJS state is %{public}d", state);
496 }
497 
ConvertScoStateChangeParamToJS(napi_env env,napi_value result,const std::string & device,int state)498 void ConvertScoStateChangeParamToJS(napi_env env, napi_value result, const std::string &device, int state)
499 {
500     HILOGI("ConvertScoStateChangeParamToJS called");
501     napi_value deviceId = nullptr;
502     napi_create_string_utf8(env, device.c_str(), NAPI_AUTO_LENGTH, &deviceId);
503     napi_set_named_property(env, result, "deviceId", deviceId);
504     HILOGI("ConvertScoStateChangeParamToJS deviceId is %{public}s", GetEncryptAddr(device).c_str());
505 
506     napi_value profileState = nullptr;
507     napi_create_int32(env, GetScoConnectionState(state), &profileState);
508     napi_set_named_property(env, result, "state", profileState);
509     HILOGI("ConvertScoStateChangeParamToJS state is %{public}d", state);
510 }
511 
GetServiceVectorFromJS(napi_env env,napi_value object,vector<GattService> & services,std::shared_ptr<GattServer> server,std::shared_ptr<GattClient> client)512 void GetServiceVectorFromJS(napi_env env, napi_value object, vector<GattService>& services,
513     std::shared_ptr<GattServer> server, std::shared_ptr<GattClient> client)
514 {
515     HILOGI("GetServiceVectorFromJS called");
516     size_t idx = 0;
517     bool hasElement = false;
518     HILOGI("GetServiceVectorFromJS size is %{public}zu", services.size());
519     napi_has_element(env, object, idx, &hasElement);
520     while (hasElement) {
521         napi_value result = nullptr;
522         napi_create_object(env, &result);
523         napi_get_element(env, object, idx, &result);
524         GattService* service = GetServiceFromJS(env, result, nullptr, nullptr);
525         services.push_back(*service);
526         delete service;
527         idx++;
528         napi_has_element(env, object, idx, &hasElement);
529     }
530 }
531 
GetServiceFromJS(napi_env env,napi_value object,std::shared_ptr<GattServer> server,std::shared_ptr<GattClient> client)532 GattService* GetServiceFromJS(napi_env env, napi_value object, std::shared_ptr<GattServer> server,
533     std::shared_ptr<GattClient> client)
534 {
535     HILOGI("GetServiceFromJS called");
536     string serviceUuid;
537     bool isPrimary = false;
538 
539     napi_value propertyNameValue = nullptr;
540     napi_value value = nullptr;
541     bool hasProperty = false;
542 
543     napi_create_string_utf8(env, "serviceUuid", NAPI_AUTO_LENGTH, &propertyNameValue);
544     napi_get_property(env, object, propertyNameValue, &value);
545     ParseString(env, serviceUuid, value);
546     HILOGI("serviceUuid is %{public}s", serviceUuid.c_str());
547 
548     napi_create_string_utf8(env, "isPrimary", NAPI_AUTO_LENGTH, &propertyNameValue);
549     napi_get_property(env, object, propertyNameValue, &value);
550     ParseBool(env, isPrimary, value);
551     HILOGI("isPrimary is %{public}d", isPrimary);
552 
553     GattServiceType serviceType = GattServiceType::PRIMARY;
554     if (!isPrimary) {
555         serviceType = GattServiceType::SECONDARY;
556     }
557 
558     GattService* service;
559     if (server == nullptr && client == nullptr) {
560         service =  new GattService(UUID::FromString(serviceUuid), serviceType);
561 
562         napi_create_string_utf8(env, "characteristics", NAPI_AUTO_LENGTH, &propertyNameValue);
563         napi_has_property(env, object, propertyNameValue, &hasProperty);
564         if (hasProperty) {
565             napi_get_property(env, object, propertyNameValue, &value);
566             vector<GattCharacteristic> characteristics;
567             GetCharacteristicVectorFromJS(env, value, characteristics, server, client);
568             for (auto& characteristic : characteristics) {
569                 service->AddCharacteristic(characteristic);
570             }
571         }
572 
573         napi_create_string_utf8(env, "includeServices", NAPI_AUTO_LENGTH, &propertyNameValue);
574         napi_has_property(env, object, propertyNameValue, &hasProperty);
575         if (hasProperty) {
576             napi_get_property(env, object, propertyNameValue, &value);
577             vector<GattService> services;
578             GetServiceVectorFromJS(env, value, services, server, client);
579             for (auto& serv : services) {
580                 service->AddService(serv);
581             }
582         }
583     } else {
584         std::optional<std::reference_wrapper<GattService>> obtainedService;
585         if (server != nullptr) {
586             obtainedService = server->GetService(UUID::FromString(serviceUuid), isPrimary);
587         } else if (client != nullptr) {
588             if (client->DiscoverServices()) {
589                 obtainedService = client->GetService(UUID::FromString(serviceUuid));
590             } else {
591                 return nullptr;
592             }
593         }
594 
595         if (obtainedService == std::nullopt) {
596             return nullptr;
597         } else {
598             service = &(obtainedService->get());
599         }
600     }
601     return service;
602 }
603 
GetCharacteristicVectorFromJS(napi_env env,napi_value object,vector<GattCharacteristic> & characteristics,std::shared_ptr<GattServer> server,std::shared_ptr<GattClient> client)604 void GetCharacteristicVectorFromJS(napi_env env, napi_value object, vector<GattCharacteristic>& characteristics,
605     std::shared_ptr<GattServer> server, std::shared_ptr<GattClient> client)
606 {
607     HILOGI("GetCharacteristicVectorFromJS called");
608     size_t idx = 0;
609 
610     bool hasElement = false;
611     napi_has_element(env, object, idx, &hasElement);
612     HILOGI("GetCharacteristicVectorFromJS size is %{public}zu", characteristics.size());
613     while(hasElement) {
614         napi_value result = nullptr;
615         napi_create_object(env, &result);
616         napi_get_element(env, object, idx, &result);
617         GattCharacteristic* characteristic = GetCharacteristicFromJS(env, result, server, client);
618         characteristics.push_back(*characteristic);
619         if (server == nullptr && client == nullptr) {
620             delete characteristic;
621         }
622         idx++;
623         napi_has_element(env, object, idx, &hasElement);
624     };
625 }
626 
GetCharacteristicFromJS(napi_env env,napi_value object,std::shared_ptr<GattServer> server,std::shared_ptr<GattClient> client)627 GattCharacteristic* GetCharacteristicFromJS(napi_env env, napi_value object, std::shared_ptr<GattServer> server,
628     std::shared_ptr<GattClient> client)
629 {
630     HILOGI("GetCharacteristicFromJS called");
631     string serviceUuid;
632     string characteristicUuid;
633     uint8_t *characteristicValue = nullptr;
634     size_t characteristicValueSize = 0;
635 
636     napi_value propertyNameValue = nullptr;
637     napi_value value = nullptr;
638     bool hasProperty = false;
639 
640     napi_create_string_utf8(env, "serviceUuid", NAPI_AUTO_LENGTH, &propertyNameValue);
641     napi_get_property(env, object, propertyNameValue, &value);
642     ParseString(env, serviceUuid, value);
643     HILOGI("serviceUuid is %{public}s", serviceUuid.c_str());
644 
645     napi_create_string_utf8(env, "characteristicUuid", NAPI_AUTO_LENGTH, &propertyNameValue);
646     napi_get_property(env, object, propertyNameValue, &value);
647     ParseString(env, characteristicUuid, value);
648     HILOGI("characteristicUuid is %{public}s", characteristicUuid.c_str());
649 
650     GattCharacteristic* characteristic = nullptr;
651     std::optional<std::reference_wrapper<GattService>> service = nullopt;
652 
653     if (server == nullptr && client == nullptr) {
654         characteristic = new GattCharacteristic(UUID::FromString(characteristicUuid),
655             (GattCharacteristic::Permission::READABLE | GattCharacteristic::Permission::WRITEABLE),
656                 GattCharacteristic::Propertie::NOTIFY);
657 
658         napi_create_string_utf8(env, "descriptors", NAPI_AUTO_LENGTH, &propertyNameValue);
659         napi_has_property(env, object, propertyNameValue, &hasProperty);
660         if (hasProperty) {
661             napi_get_property(env, object, propertyNameValue, &value);
662             vector<GattDescriptor> descriptors;
663             GetDescriptorVectorFromJS(env, value, descriptors);
664             for (auto& descriptor : descriptors) {
665                 characteristic->AddDescriptor(descriptor);
666         }
667     }
668     } else {
669         if (server != nullptr) {
670             service = server->GetService(UUID::FromString(serviceUuid), true);
671             if (service == std::nullopt) {
672                 service = server->GetService(UUID::FromString(serviceUuid), false);
673             }
674         } else if (client != nullptr) {
675             service = client->GetService(UUID::FromString(serviceUuid));
676         }
677 
678         if (service == std::nullopt) {
679             return nullptr;
680         } else {
681             characteristic = service->get().GetCharacteristic(UUID::FromString(characteristicUuid));
682         }
683     }
684 
685     if (characteristic == nullptr) {
686         return nullptr;
687     }
688     napi_create_string_utf8(env, "characteristicValue", NAPI_AUTO_LENGTH, &propertyNameValue);
689     napi_get_property(env, object, propertyNameValue, &value);
690     if (!ParseArrayBuffer(env, &characteristicValue, characteristicValueSize, value)) {
691         HILOGE("ParseArrayBuffer failed");
692     } else {
693         HILOGI("characteristicValue is %{public}d", characteristicValue[0]);
694     }
695     characteristic->SetValue(characteristicValue, characteristicValueSize);
696 
697     return characteristic;
698 }
699 
SetNamedPropertyByInteger(napi_env env,napi_value dstObj,int32_t objName,const char * propName)700 void SetNamedPropertyByInteger(napi_env env, napi_value dstObj, int32_t objName, const char *propName)
701 {
702     napi_value prop = nullptr;
703     if (napi_create_int32(env, objName, &prop) == napi_ok) {
704         napi_set_named_property(env, dstObj, propName, prop);
705     }
706 }
707 
NapiGetNull(napi_env env)708 napi_value NapiGetNull(napi_env env)
709 {
710     napi_value result = nullptr;
711     napi_get_null(env, &result);
712     return result;
713 }
714 
RegisterObserver(napi_env env,napi_callback_info info)715 napi_value RegisterObserver(napi_env env, napi_callback_info info)
716 {
717     HILOGI("RegisterObserver called");
718     size_t expectedArgsCount = ARGS_SIZE_TWO;
719     size_t argc = expectedArgsCount;
720     napi_value argv[ARGS_SIZE_TWO] = {0};
721     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
722     if (argc == expectedArgsCount + 1) {
723         NapiSppClient::On(env, info);
724     } else {
725         NAPI_ASSERT(env, argc == expectedArgsCount, "Requires 2 arguments.");
726         std::string type;
727         ParseString(env, type, argv[PARAM0]);
728         std::shared_ptr<BluetoothCallbackInfo> pCallbackInfo = std::make_shared<BluetoothCallbackInfo>();
729         pCallbackInfo->env_ = env;
730         napi_valuetype valueType = napi_undefined;
731         napi_typeof(env, argv[PARAM1], &valueType);
732         NAPI_ASSERT(env, valueType == napi_function, "Wrong argument type. Function expected.");
733         napi_create_reference(env, argv[PARAM1], 1, &pCallbackInfo->callback_);
734         g_Observer[type] = pCallbackInfo;
735 
736         HILOGI("%{public}s is registered", type.c_str());
737     }
738     napi_value ret = nullptr;
739     napi_get_undefined(env, &ret);
740     HILOGI("RegisterObserver called end");
741     return ret;
742 }
743 
DeregisterObserver(napi_env env,napi_callback_info info)744 napi_value DeregisterObserver(napi_env env, napi_callback_info info)
745 {
746     HILOGI("DeregisterObserver called");
747     size_t expectedArgsCount = ARGS_SIZE_TWO;
748     size_t argc = expectedArgsCount;
749     napi_value argv[ARGS_SIZE_TWO] = {0};
750     napi_get_cb_info(env, info, &argc, argv, nullptr, nullptr);
751     if (argc == expectedArgsCount + 1) {
752         NapiSppClient::Off(env, info);
753     } else {
754         NAPI_ASSERT(env, argc == expectedArgsCount, "Requires 2 argument.");
755         std::string type;
756         ParseString(env, type, argv[PARAM0]);
757 
758         std::shared_ptr<BluetoothCallbackInfo> pCallbackInfo = std::make_shared<BluetoothCallbackInfo>();
759         pCallbackInfo->env_ = env;
760         napi_valuetype valueType = napi_undefined;
761         napi_typeof(env, argv[PARAM1], &valueType);
762         NAPI_ASSERT(env, valueType == napi_function, "Wrong argument type. Function expected.");
763         napi_create_reference(env, argv[PARAM1], 1, &pCallbackInfo->callback_);
764         if (pCallbackInfo != nullptr) {
765             napi_value callback = 0;
766             napi_value undefined = 0;
767             napi_value callResult = 0;
768             napi_get_undefined(pCallbackInfo->env_, &undefined);
769             napi_value result = NapiGetNull(pCallbackInfo->env_);
770             napi_get_reference_value(pCallbackInfo->env_, pCallbackInfo->callback_, &callback);
771             napi_call_function(pCallbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
772         }
773 
774         if (g_Observer.find(type) != g_Observer.end()) {
775             g_Observer[type] = nullptr;
776         }
777     }
778     napi_value ret = nullptr;
779     napi_get_undefined(env, &ret);
780     HILOGI("DeregisterObserver called end");
781     return ret;
782 }
783 
GetObserver()784 std::map<std::string, std::shared_ptr<BluetoothCallbackInfo>> GetObserver()
785 {
786     return g_Observer;
787 }
788 
GetDescriptorVectorFromJS(napi_env env,napi_value object,vector<GattDescriptor> & descriptors)789 void GetDescriptorVectorFromJS(napi_env env, napi_value object, vector<GattDescriptor>& descriptors)
790 {
791     HILOGI("GetDescriptorVectorFromJS called");
792 
793     size_t idx = 0;
794     bool hasElement = false;
795     napi_has_element(env, object, idx, &hasElement);
796 
797     HILOGI("GetDescriptorVectorFromJS size is %{public}zu", descriptors.size());
798 
799     while (hasElement) {
800         napi_value result = nullptr;
801         napi_create_object(env, &result);
802         napi_get_element(env, object, idx, &result);
803         GattDescriptor* descriptor = GetDescriptorFromJS(env, result, nullptr, nullptr);
804         descriptors.push_back(*descriptor);
805         delete descriptor;
806         idx++;
807         napi_has_element(env, object, idx, &hasElement);
808     }
809 }
810 
GetSysBLEObserver()811 const sysBLEMap &GetSysBLEObserver()
812 {
813     return g_sysBLEObserver;
814 }
815 
GetDescriptorFromJS(napi_env env,napi_value object,std::shared_ptr<GattServer> server,std::shared_ptr<GattClient> client)816 GattDescriptor* GetDescriptorFromJS(napi_env env, napi_value object, std::shared_ptr<GattServer> server,
817     std::shared_ptr<GattClient> client)
818 {
819     HILOGI("GetDescriptorFromJS called");
820 
821     string serviceUuid;
822     string characteristicUuid;
823     string descriptorUuid;
824     uint8_t *descriptorValue = nullptr;
825     size_t descriptorValueSize = 0;
826 
827     napi_value propertyNameValue = nullptr;
828     napi_value value = nullptr;
829 
830     napi_create_string_utf8(env, "serviceUuid", NAPI_AUTO_LENGTH, &propertyNameValue);
831     napi_get_property(env, object, propertyNameValue, &value);
832     ParseString(env, serviceUuid, value);
833     HILOGI("serviceUuid is %{public}s", serviceUuid.c_str());
834 
835     napi_create_string_utf8(env, "characteristicUuid", NAPI_AUTO_LENGTH, &propertyNameValue);
836     napi_get_property(env, object, propertyNameValue, &value);
837     ParseString(env, characteristicUuid, value);
838     HILOGI("characteristicUuid is %{public}s", characteristicUuid.c_str());
839 
840     napi_create_string_utf8(env, "descriptorUuid", NAPI_AUTO_LENGTH, &propertyNameValue);
841     napi_get_property(env, object, propertyNameValue, &value);
842     ParseString(env, descriptorUuid, value);
843     HILOGI("descriptorUuid is %{public}s", descriptorUuid.c_str());
844 
845     GattDescriptor* descriptor = nullptr;
846     GattCharacteristic* characteristic = nullptr;
847     std::optional<std::reference_wrapper<GattService>> service = nullopt;
848 
849     if (server == nullptr && client == nullptr) {
850         descriptor = new GattDescriptor(UUID::FromString(descriptorUuid),
851             GattCharacteristic::Permission::READABLE | GattCharacteristic::Permission::WRITEABLE);
852     } else {
853         if (server != nullptr) {
854             service = server->GetService(UUID::FromString(serviceUuid), true);
855             if (service == std::nullopt) {
856                 service = server->GetService(UUID::FromString(serviceUuid), false);
857             }
858         } else if (client != nullptr) {
859             service = client->GetService(UUID::FromString(serviceUuid));
860         }
861 
862         if (service == std::nullopt) {
863             return nullptr;
864         } else {
865             characteristic = service->get().GetCharacteristic(UUID::FromString(characteristicUuid));
866         }
867 
868         if (characteristic == nullptr) {
869             return nullptr;
870         } else {
871             descriptor = characteristic->GetDescriptor(UUID::FromString(descriptorUuid));
872         }
873     }
874 
875     if (descriptor == nullptr) {
876         return nullptr;
877     }
878 
879     napi_create_string_utf8(env, "descriptorValue", NAPI_AUTO_LENGTH, &propertyNameValue);
880     napi_get_property(env, object, propertyNameValue, &value);
881     if (!ParseArrayBuffer(env, &descriptorValue, descriptorValueSize, value)) {
882         HILOGE("ParseArrayBuffer failed");
883     } else {
884         HILOGI("descriptorValue is %{public}d", descriptorValue[0]);
885     }
886     descriptor->SetValue(descriptorValue, descriptorValueSize);
887 
888     return descriptor;
889 }
890 
GetServerResponseFromJS(napi_env env,napi_value object)891 ServerResponse GetServerResponseFromJS(napi_env env, napi_value object)
892 {
893     HILOGI("GetServerResponseFromJS called");
894     ServerResponse serverResponse;
895     napi_value propertyNameValue = nullptr;
896     napi_value value = nullptr;
897     uint8_t *values = nullptr;
898     size_t valuesSize = 0;
899 
900     napi_create_string_utf8(env, "deviceId", NAPI_AUTO_LENGTH, &propertyNameValue);
901     napi_get_property(env, object, propertyNameValue, &value);
902     ParseString(env, serverResponse.deviceId, value);
903     HILOGI("deviceId is %{public}s", serverResponse.deviceId.c_str());
904 
905     napi_create_string_utf8(env, "transId", NAPI_AUTO_LENGTH, &propertyNameValue);
906     napi_get_property(env, object, propertyNameValue, &value);
907     ParseInt32(env, serverResponse.transId, value);
908     HILOGI("transId is %{public}d", serverResponse.transId);
909 
910     napi_create_string_utf8(env, "status", NAPI_AUTO_LENGTH, &propertyNameValue);
911     napi_get_property(env, object, propertyNameValue, &value);
912     ParseInt32(env, serverResponse.status, value);
913     HILOGI("status is %{public}d", serverResponse.status);
914 
915     napi_create_string_utf8(env, "offset", NAPI_AUTO_LENGTH, &propertyNameValue);
916     napi_get_property(env, object, propertyNameValue, &value);
917     ParseInt32(env, serverResponse.offset, value);
918     HILOGI("offset is %{public}d", serverResponse.offset);
919 
920     napi_create_string_utf8(env, "value", NAPI_AUTO_LENGTH, &propertyNameValue);
921     napi_get_property(env, object, propertyNameValue, &value);
922     if (!ParseArrayBuffer(env, &values, valuesSize, value)) {
923         HILOGE("ParseArrayBuffer failed");
924     } else {
925         HILOGI("value is %{public}d", values[0]);
926     }
927     serverResponse.SetValue(values, valuesSize);
928 
929     return serverResponse;
930 }
931 
GetSppOptionFromJS(napi_env env,napi_value object)932 std::shared_ptr<SppOption> GetSppOptionFromJS(napi_env env, napi_value object)
933 {
934     HILOGI("GetSppOptionFromJS called");
935     std::shared_ptr<SppOption> sppOption = std::make_shared<SppOption>();
936     napi_value propertyNameValue = nullptr;
937     napi_value value = nullptr;
938 
939     napi_create_string_utf8(env, "uuid", NAPI_AUTO_LENGTH, &propertyNameValue);
940     napi_get_property(env, object, propertyNameValue, &value);
941     ParseString(env, sppOption->uuid_, value);
942     HILOGI("uuid is %{public}s", sppOption->uuid_.c_str());
943 
944     napi_create_string_utf8(env, "secure", NAPI_AUTO_LENGTH, &propertyNameValue);
945     napi_get_property(env, object, propertyNameValue, &value);
946     ParseBool(env, sppOption->secure_, value);
947     HILOGI("secure is %{public}d", sppOption->secure_);
948 
949     int type;
950     napi_create_string_utf8(env, "type", NAPI_AUTO_LENGTH, &propertyNameValue);
951     napi_get_property(env, object, propertyNameValue, &value);
952     ParseInt32(env, type, value);
953     sppOption->type_ = SppSocketType(type);
954     HILOGI("type is %{public}d", sppOption->type_);
955 
956     return sppOption;
957 }
958 
SetGattClinetDeviceId(const std::string & deviceId)959 void SetGattClinetDeviceId(const std::string &deviceId)
960 {
961     deviceAddr = deviceId;
962 }
963 
GetGattClientDeviceId()964 std::string GetGattClientDeviceId()
965 {
966     return deviceAddr;
967 }
968 
SetRssiValueCallbackInfo(std::shared_ptr<GattGetRssiValueCallbackInfo> & callback)969 void SetRssiValueCallbackInfo(std::shared_ptr<GattGetRssiValueCallbackInfo> &callback)
970 {
971     callbackInfo = callback;
972 }
973 
GetRssiValueCallbackInfo()974 std::shared_ptr<GattGetRssiValueCallbackInfo> GetRssiValueCallbackInfo()
975 {
976     return callbackInfo;
977 }
978 
GetProfileConnectionState(int state)979 int GetProfileConnectionState(int state)
980 {
981     int32_t profileConnectionState = ProfileConnectionState::STATE_DISCONNECTED;
982     switch (state) {
983         case static_cast<int32_t>(BTConnectState::CONNECTING):
984             HILOGD("BTConnectState connecting");
985             profileConnectionState = ProfileConnectionState::STATE_CONNECTING;
986             break;
987         case static_cast<int32_t>(BTConnectState::CONNECTED):
988             HILOGD("BTConnectState connected");
989             profileConnectionState = ProfileConnectionState::STATE_CONNECTED;
990             break;
991         case static_cast<int32_t>(BTConnectState::DISCONNECTING):
992             HILOGD("BTConnectState disconnecting");
993             profileConnectionState = ProfileConnectionState::STATE_DISCONNECTING;
994             break;
995         case static_cast<int32_t>(BTConnectState::DISCONNECTED):
996             HILOGD("BTConnectState disconnected");
997             profileConnectionState = ProfileConnectionState::STATE_DISCONNECTED;
998             break;
999         default:
1000             break;
1001     }
1002     return profileConnectionState;
1003 }
1004 
GetScoConnectionState(int state)1005 int GetScoConnectionState(int state)
1006 {
1007     int32_t scoState = ScoState::SCO_DISCONNECTED;
1008     switch (state) {
1009         case static_cast<int32_t>(HfpScoConnectState::SCO_CONNECTING):
1010             HILOGD("BTConnectState connecting");
1011             scoState = ScoState::SCO_CONNECTING;
1012             break;
1013         case static_cast<int32_t>(HfpScoConnectState::SCO_CONNECTED):
1014             HILOGD("BTConnectState connected");
1015             scoState = ScoState::SCO_CONNECTED;
1016             break;
1017         case static_cast<int32_t>(HfpScoConnectState::SCO_DISCONNECTING):
1018             HILOGD("BTConnectState disconnecting");
1019             scoState = ScoState::SCO_DISCONNECTING;
1020             break;
1021         case static_cast<int32_t>(HfpScoConnectState::SCO_DISCONNECTED):
1022             HILOGD("BTConnectState disconnected");
1023             scoState = ScoState::SCO_DISCONNECTED;
1024             break;
1025         default:
1026             break;
1027     }
1028     return scoState;
1029 }
RegisterSysBLEObserver(const std::shared_ptr<BluetoothCallbackInfo> & info,int32_t callbackIndex,const std::string & type)1030 void RegisterSysBLEObserver(
1031     const std::shared_ptr<BluetoothCallbackInfo> &info, int32_t callbackIndex, const std::string &type)
1032 {
1033     if (callbackIndex >= ARGS_SIZE_THREE) {
1034         return;
1035     }
1036     HILOGI("type: %{public}s, index: %{public}d", type.c_str(), callbackIndex);
1037     g_sysBLEObserver[type][callbackIndex] = info;
1038 }
1039 
UnregisterSysBLEObserver(const std::string & type)1040 void UnregisterSysBLEObserver(const std::string &type)
1041 {
1042     auto itor = g_sysBLEObserver.find(type);
1043     if (itor != g_sysBLEObserver.end()) {
1044         g_sysBLEObserver.erase(itor);
1045     }
1046 }
1047 }  // namespace Bluetooth
1048 }  // namespace OHOS
1049