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 <uv.h>
16 #include "napi_bluetooth_hfp_ag_observer.h"
17
18 namespace OHOS {
19 namespace Bluetooth {
OnConnectionStateChanged(const BluetoothRemoteDevice & device,int state)20 void NapiHandsFreeAudioGatewayObserver::OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state)
21 {
22 HILOGI("NapiHandsFreeAudioGatewayObserver::OnConnectionStateChanged called");
23 if (!callbackInfos_[STR_BT_HANDS_FREE_AUDIO_GATEWAY_OBSERVER_CONNECTION_STATE_CHANGE]) {
24 HILOGW("NapiHandsFreeAudioGatewayObserver::OnConnectionStateChanged: This callback is not registered by ability.");
25 return;
26 }
27 HILOGI("NapiHandsFreeAudioGatewayObserver::OnConnectionStateChanged: %{public}s is registered by ability",
28 STR_BT_HANDS_FREE_AUDIO_GATEWAY_OBSERVER_CONNECTION_STATE_CHANGE.c_str());
29
30 std::shared_ptr<BluetoothCallbackInfo> callbackInfo =
31 callbackInfos_[STR_BT_HANDS_FREE_AUDIO_GATEWAY_OBSERVER_CONNECTION_STATE_CHANGE];
32
33 callbackInfo->state_ = state;
34 callbackInfo->deviceId_ = device.GetDeviceAddr();
35 uv_loop_s *loop = nullptr;
36 napi_get_uv_event_loop(callbackInfo->env_, &loop);
37 uv_work_t *work = new uv_work_t;
38 work->data = (void*)callbackInfo.get();
39
40 uv_queue_work(
41 loop,
42 work,
43 [](uv_work_t *work) {},
44 [](uv_work_t *work, int status) {
45 BluetoothCallbackInfo *callbackInfo = (BluetoothCallbackInfo *)work->data;
46 napi_value result = nullptr;
47 napi_create_object(callbackInfo->env_, &result);
48 ConvertStateChangeParamToJS(callbackInfo->env_, result, callbackInfo->deviceId_, callbackInfo->state_);
49 napi_value callback = nullptr;
50 napi_value undefined = nullptr;
51 napi_value callResult = nullptr;
52 napi_get_undefined(callbackInfo->env_, &undefined);
53 napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
54 napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
55 delete work;
56 work = nullptr;
57 }
58 );
59 }
60
OnScoStateChanged(const BluetoothRemoteDevice & device,int state)61 void NapiHandsFreeAudioGatewayObserver::OnScoStateChanged(const BluetoothRemoteDevice &device, int state)
62 {
63 HILOGI("NapiHandsFreeAudioGatewayObserver::OnScoStateChanged called");
64 if (!callbackInfos_[STR_BT_HANDS_FREE_AUDIO_GATEWAY_OBSERVER_SCO_STATE_CHANGE]) {
65 HILOGW("NapiHandsFreeAudioGatewayObserver::OnScoStateChanged: This callback is not registered by ability.");
66 return;
67 }
68 HILOGI("NapiHandsFreeAudioGatewayObserver::OnScoStateChanged: %{public}s is registered by ability",
69 STR_BT_HANDS_FREE_AUDIO_GATEWAY_OBSERVER_SCO_STATE_CHANGE.c_str());
70 std::shared_ptr<BluetoothCallbackInfo> callbackInfo =
71 callbackInfos_[STR_BT_HANDS_FREE_AUDIO_GATEWAY_OBSERVER_SCO_STATE_CHANGE];
72
73 callbackInfo->state_ = state;
74 callbackInfo->deviceId_ = device.GetDeviceAddr();
75 HILOGI("deviceId = %{public}s", (callbackInfo->deviceId_).c_str());
76 HILOGI("state = %{public}d", state);
77
78 uv_loop_s *loop = nullptr;
79 napi_get_uv_event_loop(callbackInfo->env_, &loop);
80 uv_work_t *work = new uv_work_t;
81 work->data = (void*)callbackInfo.get();
82
83 uv_queue_work(
84 loop,
85 work,
86 [](uv_work_t *work) {},
87 [](uv_work_t *work, int status) {
88 BluetoothCallbackInfo *callbackInfo = (BluetoothCallbackInfo *)work->data;
89 napi_value result = nullptr;
90 napi_create_object(callbackInfo->env_, &result);
91 ConvertScoStateChangeParamToJS(callbackInfo->env_, result, callbackInfo->deviceId_, callbackInfo->state_);
92 napi_value callback = nullptr;
93 napi_value undefined = nullptr;
94 napi_value callResult = nullptr;
95 napi_get_undefined(callbackInfo->env_, &undefined);
96 napi_get_reference_value(callbackInfo->env_, callbackInfo->callback_, &callback);
97 napi_call_function(callbackInfo->env_, undefined, callback, ARGS_SIZE_ONE, &result, &callResult);
98 delete work;
99 work = nullptr;
100 }
101 );
102 }
103
104 } // namespace Bluetooth
105 } // namespace OHOS