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     ProfileEnumInit(env, exports);
37 }
38 
SetProfile(napi_env env,ProfileId code,napi_value profile)39 void NapiProfile::SetProfile(napi_env env, ProfileId code, napi_value profile)
40 {
41     napi_ref profileRef;
42     if (napi_create_reference(env, profile, 1, &profileRef) != napi_ok) {
43         HILOGE("napi create reference failed, profileId:%{public}d", code);
44         return;
45     }
46     profiles_[code] = profileRef;
47 }
48 
GetProfile(napi_env env,napi_callback_info info)49 napi_value NapiProfile::GetProfile(napi_env env, napi_callback_info info)
50 {
51     size_t expectedArgsCount = ARGS_SIZE_ONE;
52     size_t argc = expectedArgsCount;
53     napi_value argv[ARGS_SIZE_ONE] = {0};
54     napi_value thisVar = nullptr;
55     napi_value ret = NapiGetNull(env);
56     napi_status funcRet = napi_generic_failure;
57 
58     funcRet =  napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
59     NAPI_BT_RETURN_IF(funcRet != napi_ok, "call napi_get_cb_info failed.", ret);
60     NAPI_BT_RETURN_IF(argc != expectedArgsCount, "Requires 1 argument", ret);
61 
62     int32_t profileId;
63     NAPI_BT_RETURN_IF(!ParseInt32(env, profileId, argv[PARAM0]), "False type! Int32 required.", ret);
64 
65     napi_ref profileRef = profiles_[static_cast<ProfileId>(profileId)];
66     napi_value profile = nullptr;
67     funcRet = napi_get_reference_value(env, profileRef, &profile);
68     NAPI_BT_RETURN_IF(funcRet != napi_ok, "call napi_get_reference_value failed", ret);
69     NAPI_BT_RETURN_IF(profile == nullptr, "napi get reference failed.", ret);
70 
71     HILOGI("profileId:%{public}d", profileId);
72     return profile;
73 }
74 
ProfileEnumInit(napi_env env,napi_value exports)75 void NapiProfile::ProfileEnumInit(napi_env env, napi_value exports)
76 {
77     HILOGD("enter");
78     napi_value sppTypeObj = SppTypeInit(env);
79     napi_value playingStateObj = PlayingStateInit(env);
80     napi_value profileIdObj = ProfileIdInit(env);
81     napi_property_descriptor exportFuncs[] = {
82         DECLARE_NAPI_PROPERTY("SppType", sppTypeObj),
83         DECLARE_NAPI_PROPERTY("PlayingState", playingStateObj),
84         DECLARE_NAPI_PROPERTY("ProfileId", profileIdObj),
85     };
86     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "profile:napi_define_properties");
87     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
88 }
89 
SppTypeInit(napi_env env)90 napi_value NapiProfile::SppTypeInit(napi_env env)
91 {
92     HILOGD("enter");
93     napi_value sppType = nullptr;
94     napi_create_object(env, &sppType);
95     SetNamedPropertyByInteger(env, sppType, SppType::SPP_RFCOMM, "SPP_RFCOMM");
96     return sppType;
97 }
98 
PlayingStateInit(napi_env env)99 napi_value NapiProfile::PlayingStateInit(napi_env env)
100 {
101     HILOGD("enter");
102     napi_value playingState = nullptr;
103     napi_create_object(env, &playingState);
104     SetNamedPropertyByInteger(env, playingState, PlayingState::STATE_NOT_PLAYING, "STATE_NOT_PLAYING");
105     SetNamedPropertyByInteger(env, playingState, PlayingState::STATE_PLAYING, "STATE_PLAYING");
106     return playingState;
107 }
108 
ProfileIdInit(napi_env env)109 napi_value NapiProfile::ProfileIdInit(napi_env env)
110 {
111     HILOGD("enter");
112     napi_value profileId = nullptr;
113     napi_create_object(env, &profileId);
114     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_A2DP_SINK, "PROFILE_A2DP_SINK");
115     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_A2DP_SOURCE, "PROFILE_A2DP_SOURCE");
116     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_AVRCP_CT, "PROFILE_AVRCP_CT");
117     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_AVRCP_TG, "PROFILE_AVRCP_TG");
118     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_HANDS_FREE_AUDIO_GATEWAY,
119         "PROFILE_HANDS_FREE_AUDIO_GATEWAY");
120     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_HANDS_FREE_UNIT, "PROFILE_HANDS_FREE_UNIT");
121     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_HID_HOST, "PROFILE_HID_HOST");
122     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_PAN_NETWORK, "PROFILE_PAN_NETWORK");
123     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_PBAP_CLIENT, "PROFILE_PBAP_CLIENT");
124     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_PBAP_SERVER, "PROFILE_PBAP_SERVER");
125     SetNamedPropertyByInteger(env, profileId, ProfileId::PROFILE_OPP, "PROFILE_OPP");
126     return profileId;
127 }
128 } // namespace Bluetooth
129 } // namespace OHOS
130