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_connection_callback"
17 #endif
18
19 #include "bluetooth_connection_common.h"
20 #include "bluetooth_connection_ffi.h"
21 #include "bluetooth_connection_impl.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 CJBluetoothConnection {
29 using Bluetooth::BluetoothHost;
30 using Bluetooth::BT_ERR_INTERNAL_ERROR;
31
32 std::shared_ptr<CjBluetoothConnectionObserver> g_connectionObserver = std::make_shared<CjBluetoothConnectionObserver>();
33 std::shared_ptr<CjBluetoothRemoteDeviceObserver> g_remoteDeviceObserver =
34 std::make_shared<CjBluetoothRemoteDeviceObserver>();
35 bool g_flag = false;
36
CjBluetoothConnectionObserver()37 CjBluetoothConnectionObserver::CjBluetoothConnectionObserver() {}
38
OnDiscoveryResult(const BluetoothRemoteDevice & device,int rssi,const std::string deviceName,int deviceClass)39 void CjBluetoothConnectionObserver::OnDiscoveryResult(
40 const BluetoothRemoteDevice& device, int rssi, const std::string deviceName, int deviceClass)
41 {
42 if (deviceFindFunc == nullptr) {
43 HILOGD("not register bluetoothDeviceFind event failed");
44 return;
45 }
46 CArrString array { 0 };
47 std::shared_ptr<BluetoothRemoteDevice> remoteDevice = std::make_shared<BluetoothRemoteDevice>(device);
48 array.size = 1;
49 char** retValue = static_cast<char**>(malloc(sizeof(char*) * array.size));
50 if (retValue == nullptr) {
51 return;
52 }
53
54 for (int i = 0; i < array.size; i++) {
55 retValue[i] = MallocCString(remoteDevice->GetDeviceAddr());
56 }
57 array.head = retValue;
58
59 deviceFindFunc(array);
60
61 for (int i = 0; i < array.size; i++) {
62 free(array.head[i]);
63 array.head[i] = nullptr;
64 }
65 free(retValue);
66 retValue = nullptr;
67 }
68
OnPairConfirmed(const BluetoothRemoteDevice & device,int reqType,int number)69 void CjBluetoothConnectionObserver::OnPairConfirmed(const BluetoothRemoteDevice& device, int reqType, int number)
70 {
71 if (pinRequestFunc == nullptr) {
72 HILOGD("not register pinRequired event failed");
73 return;
74 }
75 CPinRequiredParam cPinRequiredParam { 0 };
76 char* deviceAddr = MallocCString(device.GetDeviceAddr());
77 cPinRequiredParam.deviceId = deviceAddr;
78
79 char* pinCodeNative = MallocCString(GetFormatPinCode(reqType, number));
80 cPinRequiredParam.pinCode = pinCodeNative;
81
82 pinRequestFunc(cPinRequiredParam);
83
84 free(deviceAddr);
85 deviceAddr = nullptr;
86 free(pinCodeNative);
87 pinCodeNative = nullptr;
88 }
89
RegisterDeviceFindFunc(std::function<void (CArrString)> cjCallback)90 void CjBluetoothConnectionObserver::RegisterDeviceFindFunc(std::function<void(CArrString)> cjCallback)
91 {
92 deviceFindFunc = cjCallback;
93 }
94
RegisterPinRequestFunc(std::function<void (CPinRequiredParam)> cjCallback)95 void CjBluetoothConnectionObserver::RegisterPinRequestFunc(std::function<void(CPinRequiredParam)> cjCallback)
96 {
97 pinRequestFunc = cjCallback;
98 }
99
CjBluetoothRemoteDeviceObserver()100 CjBluetoothRemoteDeviceObserver::CjBluetoothRemoteDeviceObserver() {}
101
OnPairStatusChanged(const BluetoothRemoteDevice & device,int status,int cause)102 void CjBluetoothRemoteDeviceObserver::OnPairStatusChanged(const BluetoothRemoteDevice& device, int status, int cause)
103 {
104 if (bondStateFunc == nullptr) {
105 HILOGD("not register bondStateChange event failed");
106 return;
107 }
108 CBondStateParam cBondStateParam { 0 };
109 int bondStatus = 0;
110 DealPairStatus(status, bondStatus);
111
112 char* deviceAddr = MallocCString(device.GetDeviceAddr());
113
114 cBondStateParam.deviceId = deviceAddr;
115 cBondStateParam.state = bondStatus;
116 cBondStateParam.cause = cause;
117
118 bondStateFunc(cBondStateParam);
119
120 free(deviceAddr);
121 deviceAddr = nullptr;
122 }
123
OnRemoteBatteryChanged(const BluetoothRemoteDevice & device,const DeviceBatteryInfo & batteryInfo)124 void CjBluetoothRemoteDeviceObserver::OnRemoteBatteryChanged(
125 const BluetoothRemoteDevice& device, const DeviceBatteryInfo& batteryInfo)
126 {
127 if (batteryChangeFunc == nullptr) {
128 HILOGD("not register batteryChange event failed");
129 return;
130 }
131 CBatteryInfo cBatteryInfo { 0 };
132 cBatteryInfo.batteryLevel = batteryInfo.batteryLevel_;
133 cBatteryInfo.leftEarBatteryLevel = batteryInfo.leftEarBatteryLevel_;
134 cBatteryInfo.leftEarChargeState = static_cast<int32_t>(batteryInfo.leftEarChargeState_);
135 cBatteryInfo.rightEarBatteryLevel = batteryInfo.rightEarBatteryLevel_;
136 cBatteryInfo.rightEarChargeState = static_cast<int32_t>(batteryInfo.rightEarChargeState_);
137 cBatteryInfo.boxBatteryLevel = batteryInfo.boxBatteryLevel_;
138 cBatteryInfo.boxChargeState = static_cast<int32_t>(batteryInfo.boxChargeState_);
139
140 batteryChangeFunc(cBatteryInfo);
141 }
142
RegisterBondStateFunc(std::function<void (CBondStateParam)> cjCallback)143 void CjBluetoothRemoteDeviceObserver::RegisterBondStateFunc(std::function<void(CBondStateParam)> cjCallback)
144 {
145 bondStateFunc = cjCallback;
146 }
147
RegisterBatteryChangeFunc(std::function<void (CBatteryInfo)> cjCallback)148 void CjBluetoothRemoteDeviceObserver::RegisterBatteryChangeFunc(std::function<void(CBatteryInfo)> cjCallback)
149 {
150 batteryChangeFunc = cjCallback;
151 }
152
RegisterObserverToHost()153 static void RegisterObserverToHost()
154 {
155 BluetoothHost& host = BluetoothHost::GetDefaultHost();
156 host.RegisterObserver(g_connectionObserver);
157 host.RegisterRemoteDeviceObserver(g_remoteDeviceObserver);
158 }
159
RegisterConnectionObserver(int32_t callbackType,void (* callback)(),int32_t * errCode)160 void ConnectionImpl::RegisterConnectionObserver(int32_t callbackType, void (*callback)(), int32_t* errCode)
161 {
162 if (!g_flag) {
163 RegisterObserverToHost();
164 g_flag = true;
165 }
166
167 if (callbackType == REGISTER_DEVICE_FIND_TYPE) {
168 auto connectionObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CArrString)>(callback));
169 if (!connectionObserverFunc) {
170 HILOGD("Register bluetoothDeviceFind event failed");
171 *errCode = BT_ERR_INTERNAL_ERROR;
172 return;
173 }
174 g_connectionObserver->RegisterDeviceFindFunc(connectionObserverFunc);
175 }
176
177 if (callbackType == REGISTER_PIN_REQUEST_TYPE) {
178 auto connectionObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CPinRequiredParam)>(callback));
179 if (!connectionObserverFunc) {
180 HILOGD("Register pinRequired event failed");
181 *errCode = BT_ERR_INTERNAL_ERROR;
182 return;
183 }
184 g_connectionObserver->RegisterPinRequestFunc(connectionObserverFunc);
185 }
186
187 if (callbackType == REGISTER_BOND_STATE_TYPE) {
188 auto remoteDeviceObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CBondStateParam)>(callback));
189 if (!remoteDeviceObserverFunc) {
190 HILOGD("Register bondStateChange event failed");
191 *errCode = BT_ERR_INTERNAL_ERROR;
192 return;
193 }
194 g_remoteDeviceObserver->RegisterBondStateFunc(remoteDeviceObserverFunc);
195 }
196
197 if (callbackType == REGISTER_BATTERY_CHANGE_TYPE) {
198 auto remoteDeviceObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CBatteryInfo)>(callback));
199 if (!remoteDeviceObserverFunc) {
200 HILOGD("Register batteryChange event failed");
201 *errCode = BT_ERR_INTERNAL_ERROR;
202 return;
203 }
204 g_remoteDeviceObserver->RegisterBatteryChangeFunc(remoteDeviceObserverFunc);
205 }
206 }
207 } // namespace CJBluetoothConnection
208 } // namespace CJSystemapi
209 } // namespace OHOS