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 <uv.h>
17 #include "napi_bluetooth_audio_manager.h"
18 #include "bluetooth_audio_manager.h"
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21
22 #include "bluetooth_log.h"
23 #include "bluetooth_errorcode.h"
24 #include "napi_bluetooth_error.h"
25 #include "napi_async_work.h"
26 #include "napi_bluetooth_utils.h"
27 #include "parser/napi_parser_utils.h"
28 #include "napi_bluetooth_utils.h"
29 #include "hitrace_meter.h"
30
31 namespace OHOS {
32 namespace Bluetooth {
33
34 const int WEARDETECTION_ENABLED = 1;
35 const int ENABLE_WEARDETECTION_UNSUPPORT = -1;
36
DefineSystemWearDetectionInterface(napi_env env,napi_value exports)37 void NapiBluetoothAudioManager::DefineSystemWearDetectionInterface(napi_env env, napi_value exports)
38 {
39 napi_property_descriptor desc[] = {
40 DECLARE_NAPI_FUNCTION("enableWearDetection", EnableWearDetection),
41 DECLARE_NAPI_FUNCTION("disableWearDetection", DisableWearDetection),
42 DECLARE_NAPI_FUNCTION("isWearDetectionEnabled", IsWearDetectionEnabled),
43 DECLARE_NAPI_FUNCTION("isWearDetectionSupported", IsWearDetectionSupported),
44 };
45 HITRACE_METER_NAME(HITRACE_TAG_OHOS, "audio:napi_define_properties");
46 napi_define_properties(env, exports, sizeof(desc) / sizeof(desc[0]), desc);
47 HILOGI("DefineSystemWearDetectionInterface init");
48 }
49
EnableWearDetection(napi_env env,napi_callback_info info)50 napi_value NapiBluetoothAudioManager::EnableWearDetection(napi_env env, napi_callback_info info)
51 {
52 HILOGD("start");
53 std::string remoteAddr{};
54 auto status = CheckDeviceAddressParam(env, info, remoteAddr);
55 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
56
57 auto func = [remoteAddr]() {
58 BluetoothAudioManager &wd = BluetoothAudioManager::GetInstance();
59 int32_t err = wd.EnableWearDetection(remoteAddr);
60 return NapiAsyncWorkRet(err);
61 };
62 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
63 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
64 asyncWork->Run();
65 return asyncWork->GetRet();
66 }
67
DisableWearDetection(napi_env env,napi_callback_info info)68 napi_value NapiBluetoothAudioManager::DisableWearDetection(napi_env env, napi_callback_info info)
69 {
70 HILOGD("start");
71 std::string remoteAddr{};
72 auto status = CheckDeviceAddressParam(env, info, remoteAddr);
73 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
74
75 auto func = [remoteAddr]() {
76 BluetoothAudioManager &wd = BluetoothAudioManager::GetInstance();
77 int32_t err = wd.DisableWearDetection(remoteAddr);
78 return NapiAsyncWorkRet(err);
79 };
80 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
81 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
82 asyncWork->Run();
83 return asyncWork->GetRet();
84 }
85
IsWearDetectionEnabled(napi_env env,napi_callback_info info)86 napi_value NapiBluetoothAudioManager::IsWearDetectionEnabled(napi_env env, napi_callback_info info)
87 {
88 HILOGD("start");
89 std::string remoteAddr{};
90 auto status = CheckDeviceAddressParam(env, info, remoteAddr);
91 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
92
93 auto func = [remoteAddr]() {
94 int32_t ability = ENABLE_WEARDETECTION_UNSUPPORT;
95 BluetoothAudioManager &wd = BluetoothAudioManager::GetInstance();
96 int32_t err = wd.GetWearDetectionState(remoteAddr, ability);
97 if (ability == WEARDETECTION_ENABLED) {
98 return NapiAsyncWorkRet(err, std::make_shared<NapiNativeBool>(true));
99 }
100 return NapiAsyncWorkRet(err, std::make_shared<NapiNativeBool>(false));
101 };
102 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
103 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
104 asyncWork->Run();
105 return asyncWork->GetRet();
106 }
107
IsWearDetectionSupported(napi_env env,napi_callback_info info)108 napi_value NapiBluetoothAudioManager::IsWearDetectionSupported(napi_env env, napi_callback_info info)
109 {
110 HILOGI("enter");
111 std::string remoteAddr{};
112 auto status = CheckDeviceAddressParam(env, info, remoteAddr);
113 NAPI_BT_ASSERT_RETURN_UNDEF(env, status == napi_ok, BT_ERR_INVALID_PARAM);
114
115 auto func = [remoteAddr]() {
116 BluetoothAudioManager &audioManager = BluetoothAudioManager::GetInstance();
117 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
118 bool isSupported = false;
119 int32_t err = audioManager.IsWearDetectionSupported(device, isSupported);
120 HILOGI("isSupported: %{public}d", isSupported);
121 return NapiAsyncWorkRet(err, std::make_shared<NapiNativeBool>(isSupported));
122 };
123 auto asyncWork = NapiAsyncWorkFactory::CreateAsyncWork(env, info, func, ASYNC_WORK_NO_NEED_CALLBACK);
124 NAPI_BT_ASSERT_RETURN_UNDEF(env, asyncWork, BT_ERR_INTERNAL_ERROR);
125 asyncWork->Run();
126 return asyncWork->GetRet();
127 }
128
129 } // namespace Bluetooth
130 } // namespace OHOS
131