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 std::map<ProfileCode, napi_value> NapiProfile::profiles_;
21
DefineProfileFunctions(napi_env env,napi_value exports)22 void DefineProfileFunctions(napi_env env, napi_value exports)
23 {
24 napi_property_descriptor desc[] = {
25 DECLARE_NAPI_FUNCTION("getProfile", NapiProfile::GetProfile),
26 };
27 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
28
29 }
30
SetProfile(ProfileCode code,napi_value profile)31 void NapiProfile::SetProfile(ProfileCode code, napi_value profile)
32 {
33 profiles_[code] = profile;
34 }
35
GetProfile(napi_env env,napi_callback_info info)36 napi_value NapiProfile::GetProfile(napi_env env, napi_callback_info info)
37 {
38 HILOGI("GetProfile called");
39 size_t expectedArgsCount = ARGS_SIZE_ONE;
40 size_t argc = expectedArgsCount;
41 napi_value argv[ARGS_SIZE_ONE] = {0};
42 napi_value thisVar = nullptr;
43 bool isOK = false;
44 napi_value ret = nullptr;
45 napi_get_boolean(env, isOK, &ret);
46
47 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
48 if (argc != expectedArgsCount) {
49 HILOGE("Requires 1 argument.");
50 return ret;
51 }
52 int profile_code;
53 if (!ParseInt32(env, profile_code, argv[PARAM0])) {
54 HILOGE("False type! Int32 required.");
55 return ret;
56 }
57
58 napi_value profile = profiles_[static_cast<ProfileCode>(profile_code)];
59 if (!profile) {
60 return ret;
61 }
62 return profile;
63 }
64
65 } // namespace Bluetooth
66 } // namespace OHOS
67