• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_napi_access"
17 #endif
18 
19 #include "napi_bluetooth_access.h"
20 
21 #include "bluetooth_log.h"
22 #include "bluetooth_errorcode.h"
23 #include "bluetooth_host.h"
24 
25 #include "napi_bluetooth_error.h"
26 #include "napi_bluetooth_access_observer.h"
27 #include "napi_bluetooth_utils.h"
28 #include "napi_bluetooth_spp_client.h"
29 #include "../parser/napi_parser_utils.h"
30 #include "hitrace_meter.h"
31 
32 namespace OHOS {
33 namespace Bluetooth {
34 namespace {
35 std::shared_ptr<NapiBluetoothAccessObserver> g_bluetoothAccessObserver =
36     std::make_shared<NapiBluetoothAccessObserver>();
37 }  // namespace
38 
DefineAccessJSFunction(napi_env env,napi_value exports)39 napi_value NapiAccess::DefineAccessJSFunction(napi_env env, napi_value exports)
40 {
41     HILOGD("enter");
42     RegisterAccessObserverToHost();
43     AccessPropertyValueInit(env, exports);
44     napi_property_descriptor desc[] = {
45         DECLARE_NAPI_FUNCTION("getState", GetState),
46         DECLARE_NAPI_FUNCTION("enableBluetooth", EnableBluetooth),
47         DECLARE_NAPI_FUNCTION("disableBluetooth", DisableBluetooth),
48         DECLARE_NAPI_FUNCTION("restrictBluetooth", RestrictBluetooth),
49 #ifdef BLUETOOTH_API_SINCE_10
50         DECLARE_NAPI_FUNCTION("factoryReset", FactoryReset),
51         DECLARE_NAPI_FUNCTION("getLocalAddress", GetLocalAddress),
52         DECLARE_NAPI_FUNCTION("on", RegisterAccessObserver),
53         DECLARE_NAPI_FUNCTION("off", DeregisterAccessObserver),
54         DECLARE_NAPI_FUNCTION("addPersistentDeviceId", AddPersistentDeviceId),
55         DECLARE_NAPI_FUNCTION("deletePersistentDeviceId", DeletePersistentDeviceId),
56         DECLARE_NAPI_FUNCTION("getPersistentDeviceIds", GetPersistentDeviceIds),
57         DECLARE_NAPI_FUNCTION("isValidRandomDeviceId", isValidRandomDeviceId),
58 #endif
59     };
60     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "access:napi_define_properties");
61     napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
62     return exports;
63 }
64 
RegisterAccessObserverToHost()65 void NapiAccess::RegisterAccessObserverToHost()
66 {
67     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
68     host->RegisterObserver(g_bluetoothAccessObserver);
69 }
70 
EnableBluetooth(napi_env env,napi_callback_info info)71 napi_value NapiAccess::EnableBluetooth(napi_env env, napi_callback_info info)
72 {
73     HILOGI("enter");
74     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
75     int32_t ret = host->EnableBle();
76     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
77     return NapiGetBooleanTrue(env);
78 }
79 
RestrictBluetooth(napi_env env,napi_callback_info info)80 napi_value NapiAccess::RestrictBluetooth(napi_env env, napi_callback_info info)
81 {
82     HILOGI("enter");
83     auto func = []() {
84         int32_t ret = BluetoothHost::GetDefaultHost().RestrictBluetooth();
85         return NapiAsyncWorkRet(ret);
86     };
87     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
88     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
89     asyncWork->Run();
90     return asyncWork->GetRet();
91 }
92 
DisableBluetooth(napi_env env,napi_callback_info info)93 napi_value NapiAccess::DisableBluetooth(napi_env env, napi_callback_info info)
94 {
95     HILOGI("enter");
96     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
97     int ret = host->DisableBt();
98     NAPI_BT_ASSERT_RETURN_FALSE(env, ret == BT_NO_ERROR, ret);
99     return NapiGetBooleanTrue(env);
100 }
101 
GetState(napi_env env,napi_callback_info info)102 napi_value NapiAccess::GetState(napi_env env, napi_callback_info info)
103 {
104     HILOGD("enter");
105     int32_t state = static_cast<int>(BluetoothHost::GetDefaultHost().GetBluetoothState());
106     napi_value result = nullptr;
107     napi_create_int32(env, state, &result);
108     return result;
109 }
110 
AccessPropertyValueInit(napi_env env,napi_value exports)111 napi_value NapiAccess::AccessPropertyValueInit(napi_env env, napi_value exports)
112 {
113     HILOGD("enter");
114     napi_value stateObj = StateChangeInit(env);
115     napi_property_descriptor exportFuncs[] = {
116         DECLARE_NAPI_PROPERTY("BluetoothState", stateObj),
117     };
118     HITRACE_METER_NAME(HITRACE_TAG_OHOS, "access:napi_define_properties");
119     napi_define_properties(env, exports, sizeof(exportFuncs) / sizeof(*exportFuncs), exportFuncs);
120     return exports;
121 }
122 
StateChangeInit(napi_env env)123 napi_value NapiAccess::StateChangeInit(napi_env env)
124 {
125     HILOGD("enter");
126     napi_value state = nullptr;
127     napi_create_object(env, &state);
128     SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_OFF), "STATE_OFF");
129     SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_TURNING_ON), "STATE_TURNING_ON");
130     SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_ON), "STATE_ON");
131     SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_TURNING_OFF), "STATE_TURNING_OFF");
132     SetNamedPropertyByInteger(
133         env, state, static_cast<int>(BluetoothState::STATE_BLE_TURNING_ON), "STATE_BLE_TURNING_ON");
134     SetNamedPropertyByInteger(env, state, static_cast<int>(BluetoothState::STATE_BLE_ON), "STATE_BLE_ON");
135     SetNamedPropertyByInteger(
136         env, state, static_cast<int>(BluetoothState::STATE_BLE_TURNING_OFF), "STATE_BLE_TURNING_OFF");
137     return state;
138 }
139 
RegisterAccessObserver(napi_env env,napi_callback_info info)140 napi_value NapiAccess::RegisterAccessObserver(napi_env env, napi_callback_info info)
141 {
142     if (g_bluetoothAccessObserver) {
143         auto status = g_bluetoothAccessObserver->eventSubscribe_.Register(env, info);
144         NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
145     }
146     return NapiGetUndefinedRet(env);
147 }
148 
DeregisterAccessObserver(napi_env env,napi_callback_info info)149 napi_value NapiAccess::DeregisterAccessObserver(napi_env env, napi_callback_info info)
150 {
151     if (g_bluetoothAccessObserver) {
152         auto status = g_bluetoothAccessObserver->eventSubscribe_.Deregister(env, info);
153         NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
154     }
155     return NapiGetUndefinedRet(env);
156 }
157 
158 #ifdef BLUETOOTH_API_SINCE_10
FactoryReset(napi_env env,napi_callback_info info)159 napi_value NapiAccess::FactoryReset(napi_env env, napi_callback_info info)
160 {
161     HILOGD("enter");
162     auto func = []() {
163         int32_t ret = BluetoothHost::GetDefaultHost().BluetoothFactoryReset();
164         HILOGI("factoryReset ret: %{public}d", ret);
165         return NapiAsyncWorkRet(ret);
166     };
167     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
168     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
169     asyncWork->Run();
170     return asyncWork->GetRet();
171 }
172 
GetLocalAddress(napi_env env,napi_callback_info info)173 napi_value NapiAccess::GetLocalAddress(napi_env env, napi_callback_info info)
174 {
175     napi_value result = nullptr;
176     HILOGI("enter");
177     BluetoothHost *host = &BluetoothHost::GetDefaultHost();
178     std::string localAddr = INVALID_MAC_ADDRESS;
179     int32_t err = host->GetLocalAddress(localAddr);
180     napi_create_string_utf8(env, localAddr.c_str(), localAddr.size(), &result);
181     NAPI_BT_ASSERT_RETURN(env, err == BT_NO_ERROR, err, result);
182     return result;
183 }
184 #endif
185 
AddPersistentDeviceId(napi_env env,napi_callback_info info)186 napi_value NapiAccess::AddPersistentDeviceId(napi_env env, napi_callback_info info)
187 {
188     std::string deviceId = "";
189     auto status = CheckDeviceAddressParam(env, info, deviceId);
190     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
191 
192     auto func = [deviceId]() {
193         bool isValid = false;
194         std::vector<std::string> deviceIdVec = { deviceId };
195         int32_t ret = BluetoothHost::GetDefaultHost().ProcessRandomDeviceIdCommand(
196             static_cast<int32_t>(RandomDeviceIdCommand::ADD), deviceIdVec, isValid);
197         HILOGI("AddPersistentDeviceId ret: %{public}d", ret);
198         return NapiAsyncWorkRet(ret);
199     };
200     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
201     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
202     asyncWork->Run();
203     return asyncWork->GetRet();
204 }
205 
DeletePersistentDeviceId(napi_env env,napi_callback_info info)206 napi_value NapiAccess::DeletePersistentDeviceId(napi_env env, napi_callback_info info)
207 {
208     std::string deviceId = "";
209     auto status = CheckDeviceAddressParam(env, info, deviceId);
210     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
211 
212     auto func = [deviceId]() mutable {
213         bool isValid = false;
214         std::vector<std::string> deviceIdVec = { deviceId };
215         int32_t ret = BluetoothHost::GetDefaultHost().ProcessRandomDeviceIdCommand(
216             static_cast<int32_t>(RandomDeviceIdCommand::DELETE), deviceIdVec, isValid);
217         HILOGI("DeletePersistentDeviceId ret: %{public}d", ret);
218         return NapiAsyncWorkRet(ret);
219     };
220     auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
221     NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
222     asyncWork->Run();
223     return asyncWork->GetRet();
224 }
225 
GetPersistentDeviceIds(napi_env env,napi_callback_info info)226 napi_value NapiAccess::GetPersistentDeviceIds(napi_env env, napi_callback_info info)
227 {
228     bool isValid = false;
229     std::vector<std::string> deviceIdVec;
230     int32_t ret = BluetoothHost::GetDefaultHost().ProcessRandomDeviceIdCommand(
231         static_cast<int32_t>(RandomDeviceIdCommand::GET), deviceIdVec, isValid);
232     HILOGI("GetPersistentDeviceIds ret: %{public}d", ret);
233     NAPI_BT_ASSERT_RETURN_UNDEF(env, ret == BT_NO_ERROR, ret);
234 
235     NapiNativeStringArray object(deviceIdVec);
236     return object.ToNapiValue(env);
237 }
238 
isValidRandomDeviceId(napi_env env,napi_callback_info info)239 napi_value NapiAccess::isValidRandomDeviceId(napi_env env, napi_callback_info info)
240 {
241     std::string deviceId = "";
242     auto status = CheckDeviceAddressParam(env, info, deviceId);
243     NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
244 
245     bool isValid = false;
246     std::vector<std::string> deviceIdVec = { deviceId };
247     int32_t ret = BluetoothHost::GetDefaultHost().ProcessRandomDeviceIdCommand(
248         static_cast<int32_t>(RandomDeviceIdCommand::IS_VALID), deviceIdVec, isValid);
249     HILOGI("isValidRandomDeviceId ret: %{public}d", ret);
250     NAPI_BT_ASSERT_RETURN_UNDEF(env, ret == BT_NO_ERROR, ret);
251 
252     NapiNativeBool object(isValid);
253     return object.ToNapiValue(env);
254 }
255 
256 
257 }  // namespace Bluetooth
258 }  // namespace OHOS
259