1 /*
2 * Copyright (C) 2021-2022 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 #include "bluetooth_hfp_ag.h"
16
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_utils.h"
19 #include "napi_bluetooth_error.h"
20 #include "napi_bluetooth_hfp_ag.h"
21 #include "napi_bluetooth_profile.h"
22 #include "napi_bluetooth_event.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
26 using namespace std;
27
28 NapiHandsFreeAudioGatewayObserver NapiHandsFreeAudioGateway::observer_;
29 bool NapiHandsFreeAudioGateway::isRegistered_ = false;
30
DefineHandsFreeAudioGatewayJSClass(napi_env env)31 void NapiHandsFreeAudioGateway::DefineHandsFreeAudioGatewayJSClass(napi_env env)
32 {
33 napi_value constructor;
34 napi_property_descriptor properties[] = {
35 DECLARE_NAPI_FUNCTION("getConnectionDevices", GetConnectionDevices),
36 DECLARE_NAPI_FUNCTION("getDeviceState", GetDeviceState),
37 DECLARE_NAPI_FUNCTION("connect", Connect),
38 DECLARE_NAPI_FUNCTION("disconnect", Disconnect),
39 DECLARE_NAPI_FUNCTION("getScoState", GetScoState),
40 DECLARE_NAPI_FUNCTION("connectSco", ConnectSco),
41 DECLARE_NAPI_FUNCTION("disconnectSco", DisconnectSco),
42 DECLARE_NAPI_FUNCTION("on", On),
43 DECLARE_NAPI_FUNCTION("off", Off),
44 DECLARE_NAPI_FUNCTION("openVoiceRecognition", OpenVoiceRecognition),
45 DECLARE_NAPI_FUNCTION("closeVoiceRecognition", CloseVoiceRecognition),
46 };
47
48 napi_define_class(env, "HandsFreeAudioGateway", NAPI_AUTO_LENGTH, HandsFreeAudioGatewayConstructor, nullptr,
49 sizeof(properties) / sizeof(properties[0]), properties, &constructor);
50 napi_value napiProfile;
51 napi_new_instance(env, constructor, 0, nullptr, &napiProfile);
52 NapiProfile::SetProfile(env, ProfileId::PROFILE_HANDS_FREE_AUDIO_GATEWAY, napiProfile);
53 HILOGI("finished");
54 }
55
HandsFreeAudioGatewayConstructor(napi_env env,napi_callback_info info)56 napi_value NapiHandsFreeAudioGateway::HandsFreeAudioGatewayConstructor(napi_env env, napi_callback_info info)
57 {
58 napi_value thisVar = nullptr;
59 napi_get_cb_info(env, info, nullptr, nullptr, &thisVar, nullptr);
60 return thisVar;
61 }
62
On(napi_env env,napi_callback_info info)63 napi_value NapiHandsFreeAudioGateway::On(napi_env env, napi_callback_info info)
64 {
65 HILOGI("enter");
66 std::unique_lock<std::shared_mutex> guard(NapiHandsFreeAudioGatewayObserver::g_handsFreeAudioGatewayCallbackMutex);
67
68 napi_value ret = nullptr;
69 ret = NapiEvent::OnEvent(env, info, observer_.callbackInfos_);
70 if (!isRegistered_) {
71 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
72 profile->RegisterObserver(&observer_);
73 isRegistered_ = true;
74 }
75
76 HILOGI("Hands Free Audio Gateway is registered");
77 return ret;
78 }
79
Off(napi_env env,napi_callback_info info)80 napi_value NapiHandsFreeAudioGateway::Off(napi_env env, napi_callback_info info)
81 {
82 HILOGI("enter");
83 std::unique_lock<std::shared_mutex> guard(NapiHandsFreeAudioGatewayObserver::g_handsFreeAudioGatewayCallbackMutex);
84
85 napi_value ret = nullptr;
86 ret = NapiEvent::OffEvent(env, info, observer_.callbackInfos_);
87 HILOGI("Hands Free Audio Gateway is unregistered");
88 return ret;
89 }
90
GetConnectionDevices(napi_env env,napi_callback_info info)91 napi_value NapiHandsFreeAudioGateway::GetConnectionDevices(napi_env env, napi_callback_info info)
92 {
93 HILOGI("enter");
94 napi_value ret = nullptr;
95 if (napi_create_array(env, &ret) != napi_ok) {
96 HILOGE("napi_create_array failed.");
97 }
98 napi_status checkRet = CheckEmptyParam(env, info);
99 NAPI_BT_ASSERT_RETURN(env, checkRet == napi_ok, BT_ERR_INVALID_PARAM, ret);
100
101 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
102 vector<BluetoothRemoteDevice> devices;
103 int errorCode = profile->GetConnectedDevices(devices);
104 HILOGI("errorCode:%{public}s, devices size:%{public}zu", GetErrorCode(errorCode).c_str(), devices.size());
105 NAPI_BT_ASSERT_RETURN(env, errorCode == BT_SUCCESS, errorCode, ret);
106
107 vector<string> deviceVector;
108 for (auto &device: devices) {
109 deviceVector.push_back(device.GetDeviceAddr());
110 }
111 ConvertStringVectorToJS(env, ret, deviceVector);
112 return ret;
113 }
114
GetDeviceState(napi_env env,napi_callback_info info)115 napi_value NapiHandsFreeAudioGateway::GetDeviceState(napi_env env, napi_callback_info info)
116 {
117 HILOGI("enter");
118 napi_value result = nullptr;
119 int32_t profileState = ProfileConnectionState::STATE_DISCONNECTED;
120 if (napi_create_int32(env, profileState, &result) != napi_ok) {
121 HILOGE("napi_create_int32 failed.");
122 }
123
124 std::string remoteAddr {};
125 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
126 NAPI_BT_ASSERT_RETURN(env, checkRet, BT_ERR_INVALID_PARAM, result);
127
128 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
129 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
130 int32_t state = static_cast<int32_t>(BTConnectState::DISCONNECTED);
131 int32_t errorCode = profile->GetDeviceState(device, state);
132 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
133 NAPI_BT_ASSERT_RETURN(env, errorCode == BT_SUCCESS, errorCode, result);
134
135 profileState = GetProfileConnectionState(state);
136 if (napi_create_int32(env, profileState, &result) != napi_ok) {
137 HILOGE("napi_create_int32 failed.");
138 }
139 return result;
140 }
141
GetScoState(napi_env env,napi_callback_info info)142 napi_value NapiHandsFreeAudioGateway::GetScoState(napi_env env, napi_callback_info info)
143 {
144 HILOGI("enter");
145 size_t expectedArgsCount = ARGS_SIZE_ONE;
146 size_t argc = expectedArgsCount;
147 napi_value argv[ARGS_SIZE_ONE] = {0};
148 napi_value thisVar = nullptr;
149
150 napi_value ret = nullptr;
151 napi_get_undefined(env, &ret);
152
153 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
154 if (argc != expectedArgsCount) {
155 HILOGE("Requires 1 argument.");
156 return ret;
157 }
158 string deviceId;
159 if (!ParseString(env, deviceId, argv[PARAM0])) {
160 HILOGE("string expected.");
161 return ret;
162 }
163
164 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
165 BluetoothRemoteDevice device(deviceId, 1);
166 int state = profile->GetScoState(device);
167 int status = GetScoConnectionState(state);
168 napi_value result = nullptr;
169 napi_create_int32(env, status, &result);
170 HILOGI("status: %{public}d", status);
171 return result;
172 }
173
ConnectSco(napi_env env,napi_callback_info info)174 napi_value NapiHandsFreeAudioGateway::ConnectSco(napi_env env, napi_callback_info info)
175 {
176 HILOGI("enter");
177 size_t expectedArgsCount = ARGS_SIZE_ONE;
178 size_t argc = expectedArgsCount;
179 napi_value argv[ARGS_SIZE_ONE] = {0};
180 napi_value thisVar = nullptr;
181
182 napi_value ret = nullptr;
183 napi_get_undefined(env, &ret);
184
185 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
186 if (argc != expectedArgsCount) {
187 HILOGE("Requires 1 argument.");
188 return ret;
189 }
190 string deviceId;
191 if (!ParseString(env, deviceId, argv[PARAM0])) {
192 HILOGE("string expected.");
193 return ret;
194 }
195
196 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
197 BluetoothRemoteDevice device(deviceId, 1);
198 bool isOK = profile->SetActiveDevice(device);
199 if (isOK) {
200 isOK = profile->ConnectSco();
201 }
202 napi_value result = nullptr;
203 napi_get_boolean(env, isOK, &result);
204 HILOGI("res: %{public}d", isOK);
205 return result;
206 }
207
DisconnectSco(napi_env env,napi_callback_info info)208 napi_value NapiHandsFreeAudioGateway::DisconnectSco(napi_env env, napi_callback_info info)
209 {
210 HILOGI("enter");
211 size_t expectedArgsCount = ARGS_SIZE_ONE;
212 size_t argc = expectedArgsCount;
213 napi_value argv[ARGS_SIZE_ONE] = {0};
214 napi_value thisVar = nullptr;
215
216 napi_value ret = nullptr;
217 napi_get_undefined(env, &ret);
218
219 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
220 if (argc != expectedArgsCount) {
221 HILOGE("Requires 1 argument.");
222 return ret;
223 }
224 string deviceId;
225 if (!ParseString(env, deviceId, argv[PARAM0])) {
226 HILOGE("string expected.");
227 return ret;
228 }
229
230 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
231 BluetoothRemoteDevice device(deviceId, 1);
232 bool isOK = profile->SetActiveDevice(device);
233 if (isOK) {
234 isOK = profile->DisconnectSco();
235 }
236 napi_value result = nullptr;
237 napi_get_boolean(env, isOK, &result);
238 HILOGI("res: %{public}d", isOK);
239 return result;
240 }
241
OpenVoiceRecognition(napi_env env,napi_callback_info info)242 napi_value NapiHandsFreeAudioGateway::OpenVoiceRecognition(napi_env env, napi_callback_info info)
243 {
244 HILOGI("enter");
245 size_t expectedArgsCount = ARGS_SIZE_ONE;
246 size_t argc = expectedArgsCount;
247 napi_value argv[ARGS_SIZE_ONE] = {0};
248 napi_value thisVar = nullptr;
249
250 napi_value ret = nullptr;
251 napi_get_undefined(env, &ret);
252
253 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
254 if (argc != expectedArgsCount) {
255 HILOGE("Requires 1 argument.");
256 return ret;
257 }
258 string deviceId;
259 if (!ParseString(env, deviceId, argv[PARAM0])) {
260 HILOGE("string expected.");
261 return ret;
262 }
263
264 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
265 BluetoothRemoteDevice device(deviceId, 1);
266 bool isOK = profile->OpenVoiceRecognition(device);
267 napi_value result = nullptr;
268 napi_get_boolean(env, isOK, &result);
269 HILOGI("res: %{public}d", isOK);
270 return result;
271 }
272
CloseVoiceRecognition(napi_env env,napi_callback_info info)273 napi_value NapiHandsFreeAudioGateway::CloseVoiceRecognition(napi_env env, napi_callback_info info)
274 {
275 HILOGI("enter");
276 size_t expectedArgsCount = ARGS_SIZE_ONE;
277 size_t argc = expectedArgsCount;
278 napi_value argv[ARGS_SIZE_ONE] = {0};
279 napi_value thisVar = nullptr;
280
281 napi_value ret = nullptr;
282 napi_get_undefined(env, &ret);
283
284 napi_get_cb_info(env, info, &argc, argv, &thisVar, nullptr);
285 if (argc != expectedArgsCount) {
286 HILOGE("Requires 1 argument.");
287 return ret;
288 }
289 string deviceId;
290 if (!ParseString(env, deviceId, argv[PARAM0])) {
291 HILOGE("string expected.");
292 return ret;
293 }
294
295 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
296 BluetoothRemoteDevice device(deviceId, 1);
297 bool isOK = profile->CloseVoiceRecognition(device);
298 napi_value result = nullptr;
299 napi_get_boolean(env, isOK, &result);
300 HILOGI("res: %{public}d", isOK);
301 return result;
302 }
303
Connect(napi_env env,napi_callback_info info)304 napi_value NapiHandsFreeAudioGateway::Connect(napi_env env, napi_callback_info info)
305 {
306 HILOGI("enter");
307 std::string remoteAddr {};
308 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
309 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
310
311 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
312 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
313 int32_t errorCode = profile->Connect(device);
314 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
315 NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_SUCCESS, errorCode);
316 return NapiGetBooleanTrue(env);
317 }
318
Disconnect(napi_env env,napi_callback_info info)319 napi_value NapiHandsFreeAudioGateway::Disconnect(napi_env env, napi_callback_info info)
320 {
321 HILOGI("enter");
322 std::string remoteAddr {};
323 bool checkRet = CheckDeivceIdParam(env, info, remoteAddr);
324 NAPI_BT_ASSERT_RETURN_FALSE(env, checkRet, BT_ERR_INVALID_PARAM);
325
326 HandsFreeAudioGateway *profile = HandsFreeAudioGateway::GetProfile();
327 BluetoothRemoteDevice device(remoteAddr, BT_TRANSPORT_BREDR);
328 int32_t errorCode = profile->Disconnect(device);
329 HILOGI("errorCode:%{public}s", GetErrorCode(errorCode).c_str());
330 NAPI_BT_ASSERT_RETURN_FALSE(env, errorCode == BT_SUCCESS, errorCode);
331 return NapiGetBooleanTrue(env);
332 }
333 } // namespace Bluetooth
334 } // namespace OHOS