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