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 #include "napi_bluetooth_access_observer.h"
16
17 #include "bluetooth_log.h"
18 #include "bluetooth_utils.h"
19 #include "napi/native_api.h"
20 #include "napi/native_node_api.h"
21 #include "napi_event_subscribe_module.h"
22
23 #include <uv.h>
24
25 namespace OHOS {
26 namespace Bluetooth {
NapiBluetoothAccessObserver()27 NapiBluetoothAccessObserver::NapiBluetoothAccessObserver()
28 : eventSubscribe_(REGISTER_STATE_CHANGE_TYPE, BT_MODULE_NAME)
29 {}
30
OnStateChanged(const int transport,const int status)31 void NapiBluetoothAccessObserver::OnStateChanged(const int transport, const int status)
32 {
33 BluetoothState state = BluetoothState::STATE_OFF;
34 if (!DealStateChange(transport, status, state)) {
35 HILOGD("No need callback");
36 return;
37 }
38
39 HILOGD("state is %{public}d", state);
40 auto nativeObject = std::make_shared<NapiNativeInt>(static_cast<int>(state));
41 eventSubscribe_.PublishEvent(REGISTER_STATE_CHANGE_TYPE, nativeObject);
42 }
43
EnableBt()44 void NapiBluetoothAccessObserver::EnableBt()
45 {
46 HILOGD("start");
47 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
48 int ret = host->EnableBt();
49 if (ret != BT_NO_ERROR) {
50 HILOGE("failed");
51 }
52 }
53
DisableBle()54 void NapiBluetoothAccessObserver::DisableBle()
55 {
56 HILOGD("start");
57 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
58 int ret = host->DisableBle();
59 if (ret != BT_NO_ERROR) {
60 HILOGE("failed");
61 }
62 }
63
DealStateChange(const int transport,const int status,BluetoothState & state)64 bool NapiBluetoothAccessObserver::DealStateChange(const int transport, const int status, BluetoothState &state)
65 {
66 HILOGD("transport is %{public}d, status is %{public}d", transport, status);
67 bool isCallback = true;
68 if (transport == BT_TRANSPORT_BREDR) {
69 GetBrStateByStatus(status, state, isCallback);
70 } else if (transport == BT_TRANSPORT_BLE) {
71 GetBleStateByStatus(status, state);
72 }
73 return isCallback;
74 }
75
GetBrStateByStatus(const int status,BluetoothState & state,bool & isCallback)76 void NapiBluetoothAccessObserver::GetBrStateByStatus(const int status, BluetoothState &state, bool &isCallback)
77 {
78 switch (status) {
79 case BTStateID::STATE_TURNING_ON:
80 HILOGD("STATE_TURNING_ON(1)");
81 state = BluetoothState::STATE_TURNING_ON;
82 break;
83 case BTStateID::STATE_TURN_ON:
84 HILOGD("STATE_ON(2)");
85 state = BluetoothState::STATE_ON;
86 break;
87 case BTStateID::STATE_TURNING_OFF:
88 HILOGD("STATE_TURNING_OFF(3)");
89 state = BluetoothState::STATE_TURNING_OFF;
90 break;
91 case BTStateID::STATE_TURN_OFF: {
92 HILOGD("STATE_OFF(0)");
93 break;
94 }
95 default:
96 break;
97 }
98 }
99
GetBleStateByStatus(const int status,BluetoothState & state)100 void NapiBluetoothAccessObserver::GetBleStateByStatus(const int status, BluetoothState &state)
101 {
102 switch (status) {
103 case BTStateID::STATE_TURNING_ON:
104 HILOGD("STATE_BLE_TURNING_ON(4)");
105 state = BluetoothState::STATE_BLE_TURNING_ON;
106 break;
107 case BTStateID::STATE_TURN_ON:
108 HILOGD("STATE_BLE_ON(5)");
109 state = BluetoothState::STATE_BLE_ON;
110 break;
111 case BTStateID::STATE_TURNING_OFF:
112 HILOGD("STATE_BLE_TURNING_OFF(6)");
113 state = BluetoothState::STATE_BLE_TURNING_OFF;
114 break;
115 case BTStateID::STATE_TURN_OFF:
116 HILOGD("STATE_OFF(0)");
117 state = BluetoothState::STATE_OFF;
118 break;
119 default:
120 break;
121 }
122 }
123 } // namespace Bluetooth
124 } // namespace OHOS
125