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