1 /*
2 * Copyright (c) 2023 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
16 #include "bluetooth_manager_addon.h"
17
18 #include "edm_constants.h"
19 #include "edm_log.h"
20
21 using namespace OHOS::EDM;
22
Init(napi_env env,napi_value exports)23 napi_value BluetoothManagerAddon::Init(napi_env env, napi_value exports)
24 {
25 napi_property_descriptor property[] = {
26 DECLARE_NAPI_FUNCTION("getBluetoothInfo", GetBluetoothInfo),
27 DECLARE_NAPI_FUNCTION("setBluetoothDisabled", SetBluetoothDisabled),
28 DECLARE_NAPI_FUNCTION("isBluetoothDisabled", IsBluetoothDisabled),
29 };
30 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
31 return exports;
32 }
33
GetBluetoothInfo(napi_env env,napi_callback_info info)34 napi_value BluetoothManagerAddon::GetBluetoothInfo(napi_env env, napi_callback_info info)
35 {
36 EDMLOGI("NAPI_GetBluetoothInfo called");
37 size_t argc = ARGS_SIZE_ONE;
38 napi_value argv[ARGS_SIZE_ONE] = { nullptr };
39 napi_value thisArg = nullptr;
40 void* data = nullptr;
41 OHOS::AppExecFwk::ElementName elementName;
42 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
43 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "parameter count error");
44 ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object), "parameter type error");
45 ASSERT_AND_THROW_PARAM_ERROR(env, ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]),
46 "element name param error");
47 EDMLOGD(
48 "EnableAdmin: elementName.bundlename %{public}s, "
49 "elementName.abilityname:%{public}s",
50 elementName.GetBundleName().c_str(),
51 elementName.GetAbilityName().c_str());
52 BluetoothInfo bluetoothInfo;
53 auto bluetoothManagerProxy = BluetoothManagerProxy::GetBluetoothManagerProxy();
54 int32_t ret = bluetoothManagerProxy->GetBluetoothInfo(elementName, bluetoothInfo);
55 if (FAILED(ret)) {
56 napi_throw(env, CreateError(env, ret));
57 return nullptr;
58 }
59 return ConvertBluetoothInfo(env, bluetoothInfo);
60 }
61
ConvertBluetoothInfo(napi_env env,BluetoothInfo & bluetoothInfo)62 napi_value BluetoothManagerAddon::ConvertBluetoothInfo(napi_env env, BluetoothInfo &bluetoothInfo)
63 {
64 napi_value objBluetoothInfo = nullptr;
65 NAPI_CALL(env, napi_create_object(env, &objBluetoothInfo));
66 napi_value napi_name;
67 napi_value napi_state;
68 napi_value napi_connectionState;
69 NAPI_CALL(env, napi_create_string_utf8(env, bluetoothInfo.name.c_str(), bluetoothInfo.name.size(), &napi_name));
70 NAPI_CALL(env, napi_create_int32(env, bluetoothInfo.state, &napi_state));
71 NAPI_CALL(env, napi_create_int32(env, bluetoothInfo.connectionState, &napi_connectionState));
72 NAPI_CALL(env, napi_set_named_property(env, objBluetoothInfo, "name", napi_name));
73 NAPI_CALL(env, napi_set_named_property(env, objBluetoothInfo, "state", napi_state));
74 NAPI_CALL(env, napi_set_named_property(env, objBluetoothInfo, "connectionState", napi_connectionState));
75 return objBluetoothInfo;
76 }
77
SetBluetoothDisabled(napi_env env,napi_callback_info info)78 napi_value BluetoothManagerAddon::SetBluetoothDisabled(napi_env env, napi_callback_info info)
79 {
80 EDMLOGI("NAPI_SetBluetoothDisabled called");
81 size_t argc = ARGS_SIZE_TWO;
82 napi_value argv[ARGS_SIZE_TWO] = { nullptr };
83 napi_value thisArg = nullptr;
84 void* data = nullptr;
85 OHOS::AppExecFwk::ElementName elementName;
86 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
87
88 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_TWO, "parameter count error");
89 ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ZERO], napi_object),
90 "The first parameter must be want.");
91 ASSERT_AND_THROW_PARAM_ERROR(env, MatchValueType(env, argv[ARR_INDEX_ONE], napi_boolean),
92 "The second parameter must be bool.");
93
94 bool ret = ParseElementName(env, elementName, argv[ARR_INDEX_ZERO]);
95 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "param 'admin' parse error");
96 EDMLOGD("EnableAdmin: elementName.bundlename %{public}s, elementName.abilityname:%{public}s",
97 elementName.GetBundleName().c_str(), elementName.GetAbilityName().c_str());
98
99 bool disabled = false;
100 ret = ParseBool(env, disabled, argv[ARR_INDEX_ONE]);
101 ASSERT_AND_THROW_PARAM_ERROR(env, ret, "param 'disabled' parse error");
102
103 auto bluetoothManagerProxy = BluetoothManagerProxy::GetBluetoothManagerProxy();
104 int32_t retCode = bluetoothManagerProxy->SetBluetoothDisabled(elementName, disabled);
105 if (FAILED(retCode)) {
106 napi_throw(env, CreateError(env, retCode));
107 }
108 return nullptr;
109 }
110
IsBluetoothDisabled(napi_env env,napi_callback_info info)111 napi_value BluetoothManagerAddon::IsBluetoothDisabled(napi_env env, napi_callback_info info)
112 {
113 EDMLOGI("NAPI_IsBluetoothDisabled called");
114 size_t argc = ARGS_SIZE_ONE;
115 napi_value argv[ARGS_SIZE_ONE] = { nullptr };
116 napi_value thisArg = nullptr;
117 void* data = nullptr;
118 NAPI_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisArg, &data));
119 ASSERT_AND_THROW_PARAM_ERROR(env, argc >= ARGS_SIZE_ONE, "parameter count error");
120 bool hasAdmin = false;
121 OHOS::AppExecFwk::ElementName elementName;
122 ASSERT_AND_THROW_PARAM_ERROR(env, CheckGetPolicyAdminParam(env, argv[ARR_INDEX_ZERO], hasAdmin, elementName),
123 "param admin need be null or want");
124 bool isDisabled = false;
125 int32_t ret = ERR_OK;
126 if (hasAdmin) {
127 ret = BluetoothManagerProxy::GetBluetoothManagerProxy()->IsBluetoothDisabled(&elementName, isDisabled);
128 } else {
129 ret = BluetoothManagerProxy::GetBluetoothManagerProxy()->IsBluetoothDisabled(nullptr, isDisabled);
130 }
131 if (FAILED(ret)) {
132 napi_throw(env, CreateError(env, ret));
133 return nullptr;
134 }
135 napi_value result = nullptr;
136 napi_get_boolean(env, isDisabled, &result);
137 return result;
138 }
139
140 static napi_module g_bluetoothModule = {
141 .nm_version = 1,
142 .nm_flags = 0,
143 .nm_filename = nullptr,
144 .nm_register_func = BluetoothManagerAddon::Init,
145 .nm_modname = "enterprise.bluetoothManager",
146 .nm_priv = ((void *)0),
147 .reserved = {0},
148 };
149
BluetoothManagerRegister()150 extern "C" __attribute__((constructor)) void BluetoothManagerRegister()
151 {
152 napi_module_register(&g_bluetoothModule);
153 }
154