• 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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_napi_profile"
17 #endif
18 
19 #include "napi_bluetooth_profile.h"
20 #include "napi_bluetooth_utils.h"
21 #include "hitrace_meter.h"
22 
23 namespace OHOS {
24 namespace Bluetooth {
25 thread_local std::map<ProfileId, napi_ref> NapiProfile::profiles_;
26 
DefineProfileFunctions(napi_env env,napi_value exports)27 void NapiProfile::DefineProfileFunctions(napi_env env, napi_value exports)
28 {
29     napi_property_descriptor desc[] = {
30         DECLARE_NAPI_FUNCTION("getProfile", GetProfile),
31         DECLARE_NAPI_FUNCTION("getProfileInst", GetProfile),
32         DECLARE_NAPI_FUNCTION("getProfileInstance", GetProfile),
33     };
34     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "profile:napi_define_properties");
35     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
36 }
37 
SetProfile(napi_env env,ProfileId code,napi_value profile)38 void NapiProfile::SetProfile(napi_env env, ProfileId code, napi_value profile)
39 {
40     napi_ref profileRef;
41     if (napi_create_reference(env, profile, 1, &profileRef) != napi_ok) {
42         HILOGE("napi create reference failed, profileId:%{public}d", code);
43         return;
44     }
45     profiles_[code] = profileRef;
46 }
47 
GetProfile(napi_env env,napi_callback_info info)48 napi_value NapiProfile::GetProfile(napi_env env, napi_callback_info info)
49 {
50     size_t expectedArgsCount = ARGS_SIZE_ONE;
51     size_t argc = expectedArgsCount;
52     napi_value argv[ARGS_SIZE_ONE] = {0};
53     napi_value thisVar = nullptr;
54     napi_value ret = NapiGetNull(env);
55     napi_status funcRet = napi_generic_failure;
56 
57     funcRet =  napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
58     NAPI_BT_RETURN_IF(funcRet != napi_ok, "call napi_get_cb_info failed.", ret);
59     NAPI_BT_RETURN_IF(argc != expectedArgsCount, "Requires 1 argument", ret);
60 
61     int32_t profileId;
62     NAPI_BT_RETURN_IF(!ParseInt32(env, profileId, argv[PARAM0]), "False type! Int32 required.", ret);
63 
64     napi_ref profileRef = profiles_[static_cast<ProfileId>(profileId)];
65     napi_value profile = nullptr;
66     funcRet = napi_get_reference_value(env, profileRef, &profile);
67     NAPI_BT_RETURN_IF(funcRet != napi_ok, "call napi_get_reference_value failed", ret);
68     NAPI_BT_RETURN_IF(profile == nullptr, "napi get reference failed.", ret);
69 
70     HILOGI("profileId:%{public}d", profileId);
71     return profile;
72 }
73 } // namespace Bluetooth
74 } // namespace OHOS
75