1 /*
2 * Copyright (c) 2024 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_cj_access_callback"
17 #endif
18
19 #include "bluetooth_access_impl.h"
20
21 #include "bluetooth_access_ffi.h"
22 #include "bluetooth_errorcode.h"
23 #include "bluetooth_log.h"
24 #include "cj_lambda.h"
25
26 namespace OHOS {
27 namespace CJSystemapi {
28 namespace CJBluetoothAccess {
29 using Bluetooth::BluetoothHost;
30 using Bluetooth::BT_ERR_INTERNAL_ERROR;
31 using Bluetooth::BluetoothState;
32 using Bluetooth::BT_TRANSPORT_BREDR;
33 using Bluetooth::BT_TRANSPORT_BLE;
34 using Bluetooth::BTStateID;
35
CjBluetoothAccessObserver()36 CjBluetoothAccessObserver::CjBluetoothAccessObserver()
37 {}
38
39 std::shared_ptr<CjBluetoothAccessObserver> g_bluetoothAccessObserver =
40 std::make_shared<CjBluetoothAccessObserver>();
41 bool g_flag = false;
42
RegisterAccessObserverToHost()43 static void RegisterAccessObserverToHost()
44 {
45 BluetoothHost *host = &BluetoothHost::GetDefaultHost();
46 host->RegisterObserver(g_bluetoothAccessObserver);
47 }
48
OnStateChanged(const int transport,const int status)49 void CjBluetoothAccessObserver::OnStateChanged(const int transport, const int status)
50 {
51 BluetoothState state = BluetoothState::STATE_OFF;
52 if (!DealStateChange(transport, status, state)) {
53 HILOGD("No need callback");
54 return;
55 }
56
57 HILOGD("state is %{public}d", state);
58 if (!stateChangeFunc) {
59 HILOGD("failed to register state change callback");
60 return;
61 }
62 stateChangeFunc(static_cast<int32_t>(state));
63 }
64
DealStateChange(const int transport,const int status,BluetoothState & state)65 bool CjBluetoothAccessObserver::DealStateChange(const int transport, const int status, BluetoothState &state)
66 {
67 HILOGD("transport is %{public}d, status is %{public}d", transport, status);
68 bool isCallback = true;
69 if (transport == BT_TRANSPORT_BREDR) {
70 GetBrStateByStatus(status, state, isCallback);
71 } else if (transport == BT_TRANSPORT_BLE) {
72 GetBleStateByStatus(status, state);
73 }
74 return isCallback;
75 }
76
GetBrStateByStatus(const int status,BluetoothState & state,bool & isCallback)77 void CjBluetoothAccessObserver::GetBrStateByStatus(const int status, BluetoothState &state, bool &isCallback)
78 {
79 switch (status) {
80 case BTStateID::STATE_TURNING_ON:
81 HILOGD("STATE_TURNING_ON(1)");
82 state = BluetoothState::STATE_TURNING_ON;
83 break;
84 case BTStateID::STATE_TURN_ON:
85 HILOGD("STATE_ON(2)");
86 state = BluetoothState::STATE_ON;
87 break;
88 case BTStateID::STATE_TURNING_OFF:
89 HILOGD("STATE_TURNING_OFF(3)");
90 state = BluetoothState::STATE_TURNING_OFF;
91 break;
92 case BTStateID::STATE_TURN_OFF: {
93 HILOGD("STATE_OFF(0)");
94 break;
95 }
96 default:
97 break;
98 }
99 }
100
GetBleStateByStatus(const int status,BluetoothState & state)101 void CjBluetoothAccessObserver::GetBleStateByStatus(const int status, BluetoothState &state)
102 {
103 switch (status) {
104 case BTStateID::STATE_TURNING_ON:
105 HILOGD("STATE_BLE_TURNING_ON(4)");
106 state = BluetoothState::STATE_BLE_TURNING_ON;
107 break;
108 case BTStateID::STATE_TURN_ON:
109 HILOGD("STATE_BLE_ON(5)");
110 state = BluetoothState::STATE_BLE_ON;
111 break;
112 case BTStateID::STATE_TURNING_OFF:
113 HILOGD("STATE_BLE_TURNING_OFF(6)");
114 state = BluetoothState::STATE_BLE_TURNING_OFF;
115 break;
116 case BTStateID::STATE_TURN_OFF:
117 HILOGD("STATE_OFF(0)");
118 state = BluetoothState::STATE_OFF;
119 break;
120 default:
121 break;
122 }
123 }
124
RegisterStateChangeFunc(std::function<void (int32_t)> cjCallback)125 void CjBluetoothAccessObserver::RegisterStateChangeFunc(std::function<void(int32_t)> cjCallback)
126 {
127 stateChangeFunc = cjCallback;
128 }
129
RegisterAccessObserver(int32_t callbackType,void (* callback)(),int32_t * errCode)130 void AccessImpl::RegisterAccessObserver(int32_t callbackType, void (*callback)(), int32_t* errCode)
131 {
132 if (!g_flag) {
133 RegisterAccessObserverToHost();
134 g_flag = true;
135 }
136
137 if (callbackType == REGISTER_STATE_CHANGE_TYPE) {
138 auto AccessObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(int32_t)>(callback));
139 if (!AccessObserverFunc) {
140 HILOGD("Register state change event failed");
141 *errCode = BT_ERR_INTERNAL_ERROR;
142 return;
143 }
144 g_bluetoothAccessObserver->RegisterStateChangeFunc(AccessObserverFunc);
145 }
146 return;
147 }
148
149 } // namespace BluetoothAccess
150 } // namespace CJSystemapi
151 } // namespace OHOS