1 /*
2 * Copyright (c) 2023-2024 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 #include "errors.h"
21 #include "js_native_api.h"
22 #include "napi_edm_adapter.h"
23 #include "napi_edm_common.h"
24
25 using namespace OHOS::EDM;
26
Init(napi_env env,napi_value exports)27 napi_value BluetoothManagerAddon::Init(napi_env env, napi_value exports)
28 {
29 napi_property_descriptor property[] = {
30 DECLARE_NAPI_FUNCTION("getBluetoothInfo", GetBluetoothInfo),
31 DECLARE_NAPI_FUNCTION("setBluetoothDisabled", SetBluetoothDisabled),
32 DECLARE_NAPI_FUNCTION("isBluetoothDisabled", IsBluetoothDisabled),
33 DECLARE_NAPI_FUNCTION("addAllowedBluetoothDevices", AddAllowedBluetoothDevices),
34 DECLARE_NAPI_FUNCTION("getAllowedBluetoothDevices", GetAllowedBluetoothDevices),
35 DECLARE_NAPI_FUNCTION("removeAllowedBluetoothDevices", RemoveAllowedBluetoothDevices),
36 };
37 NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property));
38 return exports;
39 }
40
GetBluetoothInfo(napi_env env,napi_callback_info info)41 napi_value BluetoothManagerAddon::GetBluetoothInfo(napi_env env, napi_callback_info info)
42 {
43 EDMLOGI("NAPI_GetBluetoothInfo called");
44 BluetoothInfo bluetoothInfo;
45 AddonMethodSign addonMethodSign;
46 addonMethodSign.name = "GetBluetoothInfo";
47 addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT};
48 addonMethodSign.methodAttribute = MethodAttribute::GET;
49 AdapterAddonData adapterAddonData{};
50 napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
51 if (result == nullptr) {
52 return nullptr;
53 }
54 int32_t ret =
55 BluetoothManagerProxy::GetBluetoothManagerProxy()->GetBluetoothInfo(adapterAddonData.data, bluetoothInfo);
56 if (FAILED(ret)) {
57 napi_throw(env, CreateError(env, ret));
58 return nullptr;
59 }
60 return ConvertBluetoothInfo(env, bluetoothInfo);
61 }
62
ConvertBluetoothInfo(napi_env env,BluetoothInfo & bluetoothInfo)63 napi_value BluetoothManagerAddon::ConvertBluetoothInfo(napi_env env, BluetoothInfo &bluetoothInfo)
64 {
65 napi_value objBluetoothInfo = nullptr;
66 NAPI_CALL(env, napi_create_object(env, &objBluetoothInfo));
67 napi_value napi_name;
68 napi_value napi_state;
69 napi_value napi_connectionState;
70 NAPI_CALL(env, napi_create_string_utf8(env, bluetoothInfo.name.c_str(), bluetoothInfo.name.size(), &napi_name));
71 NAPI_CALL(env, napi_create_int32(env, bluetoothInfo.state, &napi_state));
72 NAPI_CALL(env, napi_create_int32(env, bluetoothInfo.connectionState, &napi_connectionState));
73 NAPI_CALL(env, napi_set_named_property(env, objBluetoothInfo, "name", napi_name));
74 NAPI_CALL(env, napi_set_named_property(env, objBluetoothInfo, "state", napi_state));
75 NAPI_CALL(env, napi_set_named_property(env, objBluetoothInfo, "connectionState", napi_connectionState));
76 return objBluetoothInfo;
77 }
78
SetBluetoothDisabled(napi_env env,napi_callback_info info)79 napi_value BluetoothManagerAddon::SetBluetoothDisabled(napi_env env, napi_callback_info info)
80 {
81 EDMLOGI("NAPI_SetBluetoothDisabled called");
82
83 AddonMethodSign addonMethodSign;
84 addonMethodSign.name = "SetBluetoothDisabled";
85 addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::BOOLEAN};
86 addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
87 addonMethodSign.apiVersionTag = EdmConstants::PERMISSION_TAG_VERSION_11;
88 AdapterAddonData adapterAddonData{};
89 napi_value result = JsObjectToData(env, info, addonMethodSign, &adapterAddonData);
90 if (result == nullptr) {
91 return nullptr;
92 }
93 int32_t retCode =
94 BluetoothManagerProxy::GetBluetoothManagerProxy()->SetBluetoothDisabled(adapterAddonData.data);
95 if (FAILED(retCode)) {
96 napi_throw(env, CreateError(env, retCode));
97 }
98 return nullptr;
99 }
100
IsBluetoothDisabled(napi_env env,napi_callback_info info)101 napi_value BluetoothManagerAddon::IsBluetoothDisabled(napi_env env, napi_callback_info info)
102 {
103 EDMLOGI("NAPI_IsBluetoothDisabled called");
104 AddonMethodSign addonMethodSign;
105 addonMethodSign.name = "IsBluetoothDisabled";
106 addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT_NULL};
107 addonMethodSign.methodAttribute = MethodAttribute::GET;
108 addonMethodSign.apiVersionTag = EdmConstants::PERMISSION_TAG_VERSION_11;
109 AdapterAddonData adapterAddonData{};
110 if (JsObjectToData(env, info, addonMethodSign, &adapterAddonData) == nullptr) {
111 return nullptr;
112 }
113 bool isDisabled = false;
114 int32_t ret =
115 BluetoothManagerProxy::GetBluetoothManagerProxy()->IsBluetoothDisabled(adapterAddonData.data, isDisabled);
116 if (FAILED(ret)) {
117 napi_throw(env, CreateError(env, ret));
118 return nullptr;
119 }
120 napi_value result = nullptr;
121 NAPI_CALL(env, napi_get_boolean(env, isDisabled, &result));
122 return result;
123 }
124
AddAllowedBluetoothDevices(napi_env env,napi_callback_info info)125 napi_value BluetoothManagerAddon::AddAllowedBluetoothDevices(napi_env env, napi_callback_info info)
126 {
127 EDMLOGI("NAPI_AddAllowedBluetoothDevices called");
128 return AddOrRemoveBluetoothDevices(env, info, "AddAllowedBluetoothDevices");
129 }
130
GetAllowedBluetoothDevices(napi_env env,napi_callback_info info)131 napi_value BluetoothManagerAddon::GetAllowedBluetoothDevices(napi_env env, napi_callback_info info)
132 {
133 EDMLOGI("NAPI_GetAllowedBluetoothDevices called");
134
135 AddonMethodSign addonMethodSign;
136 addonMethodSign.name = "GetAllowedBluetoothDevices";
137 addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT_NULL};
138 addonMethodSign.methodAttribute = MethodAttribute::GET;
139 AdapterAddonData adapterAddonData{};
140 if (JsObjectToData(env, info, addonMethodSign, &adapterAddonData) == nullptr) {
141 return nullptr;
142 }
143
144 std::vector<std::string> deviceIds;
145 int32_t retCode =
146 BluetoothManagerProxy::GetBluetoothManagerProxy()->GetAllowedBluetoothDevices(adapterAddonData.data, deviceIds);
147 if (FAILED(retCode)) {
148 napi_throw(env, CreateError(env, retCode));
149 return nullptr;
150 }
151 napi_value result = nullptr;
152 NAPI_CALL(env, napi_create_array(env, &result));
153 for (size_t i = 0; i < deviceIds.size(); i++) {
154 napi_value allowedDevices = nullptr;
155 NAPI_CALL(env, napi_create_string_utf8(env, deviceIds[i].c_str(), NAPI_AUTO_LENGTH, &allowedDevices));
156 NAPI_CALL(env, napi_set_element(env, result, i, allowedDevices));
157 }
158 return result;
159 }
160
RemoveAllowedBluetoothDevices(napi_env env,napi_callback_info info)161 napi_value BluetoothManagerAddon::RemoveAllowedBluetoothDevices(napi_env env, napi_callback_info info)
162 {
163 EDMLOGI("NAPI_RemoveAllowedBluetoothDevices called");
164 return AddOrRemoveBluetoothDevices(env, info, "RemoveAllowedBluetoothDevices");
165 }
166
AddOrRemoveBluetoothDevices(napi_env env,napi_callback_info info,std::string function)167 napi_value BluetoothManagerAddon::AddOrRemoveBluetoothDevices(napi_env env, napi_callback_info info,
168 std::string function)
169 {
170 AddonMethodSign addonMethodSign;
171 addonMethodSign.name = "AddOrRemoveBluetoothDevices";
172 addonMethodSign.argsType = {EdmAddonCommonType::ELEMENT, EdmAddonCommonType::ARRAY_STRING};
173 addonMethodSign.methodAttribute = MethodAttribute::HANDLE;
174 AdapterAddonData adapterAddonData{};
175 if (JsObjectToData(env, info, addonMethodSign, &adapterAddonData) == nullptr) {
176 return nullptr;
177 }
178 std::vector<std::string> deviceIds;
179 int32_t retCode = BluetoothManagerProxy::GetBluetoothManagerProxy()->
180 AddOrRemoveAllowedBluetoothDevices(adapterAddonData.data, function == "AddAllowedBluetoothDevices");
181 if (FAILED(retCode)) {
182 napi_throw(env, CreateError(env, retCode));
183 }
184 return nullptr;
185 }
186
187 static napi_module g_bluetoothModule = {
188 .nm_version = 1,
189 .nm_flags = 0,
190 .nm_filename = nullptr,
191 .nm_register_func = BluetoothManagerAddon::Init,
192 .nm_modname = "enterprise.bluetoothManager",
193 .nm_priv = ((void *)0),
194 .reserved = {0},
195 };
196
BluetoothManagerRegister()197 extern "C" __attribute__((constructor)) void BluetoothManagerRegister()
198 {
199 napi_module_register(&g_bluetoothModule);
200 }
201