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