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