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