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 
16 #include "bluetooth_connection_impl.h"
17 #include "bluetooth_connection_ffi.h"
18 #include "bluetooth_errorcode.h"
19 #include "bluetooth_connection_common.h"
20 #include "bluetooth_log.h"
21 #include "cj_lambda.h"
22 
23 namespace OHOS {
24 namespace CJSystemapi {
25 namespace CJBluetoothConnection {
26 using Bluetooth::BluetoothHost;
27 using Bluetooth::BT_ERR_INTERNAL_ERROR;
28 
29 std::shared_ptr<CjBluetoothConnectionObserver> g_connectionObserver =
30     std::make_shared<CjBluetoothConnectionObserver>();
31 std::shared_ptr<CjBluetoothRemoteDeviceObserver> g_remoteDeviceObserver =
32     std::make_shared<CjBluetoothRemoteDeviceObserver>();
33 std::mutex deviceMutex;
34 bool g_flag = false;
35 
CjBluetoothConnectionObserver()36 CjBluetoothConnectionObserver::CjBluetoothConnectionObserver()
37 {}
38 
OnDiscoveryResult(const BluetoothRemoteDevice &device, int rssi, const std::string deviceName, int deviceClass)39 void CjBluetoothConnectionObserver::OnDiscoveryResult(const BluetoothRemoteDevice &device,
40     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 {}
102 
OnPairStatusChanged(const BluetoothRemoteDevice &device, int status, int cause)103 void CjBluetoothRemoteDeviceObserver::OnPairStatusChanged(const BluetoothRemoteDevice &device,
104     int status, int cause)
105 {
106     if (bondStateFunc == nullptr) {
107         HILOGD("not register bondStateChange event failed");
108         return;
109     }
110     CBondStateParam cBondStateParam{0};
111     int bondStatus = 0;
112     DealPairStatus(status, bondStatus);
113 
114     char* deviceAddr = MallocCString(device.GetDeviceAddr());
115 
116     cBondStateParam.deviceId = deviceAddr;
117     cBondStateParam.state = bondStatus;
118     cBondStateParam.cause = cause;
119 
120     bondStateFunc(cBondStateParam);
121 
122     free(deviceAddr);
123     deviceAddr = nullptr;
124 }
125 
OnRemoteBatteryChanged(const BluetoothRemoteDevice &device, const DeviceBatteryInfo &batteryInfo)126 void CjBluetoothRemoteDeviceObserver::OnRemoteBatteryChanged(const BluetoothRemoteDevice &device,
127     const DeviceBatteryInfo &batteryInfo)
128 {
129     if (batteryChangeFunc == nullptr) {
130         HILOGD("not register batteryChange event failed");
131         return;
132     }
133     CBatteryInfo cBatteryInfo{0};
134     cBatteryInfo.batteryLevel = batteryInfo.batteryLevel_;
135     cBatteryInfo.leftEarBatteryLevel = batteryInfo.leftEarBatteryLevel_;
136     cBatteryInfo.leftEarChargeState = static_cast<int32_t>(batteryInfo.leftEarChargeState_);
137     cBatteryInfo.rightEarBatteryLevel = batteryInfo.rightEarBatteryLevel_;
138     cBatteryInfo.rightEarChargeState = static_cast<int32_t>(batteryInfo.rightEarChargeState_);
139     cBatteryInfo.boxBatteryLevel = batteryInfo.boxBatteryLevel_;
140     cBatteryInfo.boxChargeState = static_cast<int32_t>(batteryInfo.boxChargeState_);
141 
142     batteryChangeFunc(cBatteryInfo);
143 }
144 
RegisterBondStateFunc(std::function<void(CBondStateParam)> cjCallback)145 void CjBluetoothRemoteDeviceObserver::RegisterBondStateFunc(std::function<void(CBondStateParam)> cjCallback)
146 {
147     bondStateFunc = cjCallback;
148 }
149 
RegisterBatteryChangeFunc(std::function<void(CBatteryInfo)> cjCallback)150 void CjBluetoothRemoteDeviceObserver::RegisterBatteryChangeFunc(std::function<void(CBatteryInfo)> cjCallback)
151 {
152     batteryChangeFunc = cjCallback;
153 }
154 
RegisterObserverToHost()155 static void RegisterObserverToHost()
156 {
157     BluetoothHost &host = BluetoothHost::GetDefaultHost();
158     host.RegisterObserver(g_connectionObserver);
159     host.RegisterRemoteDeviceObserver(g_remoteDeviceObserver);
160 }
161 
RegisterConnectionObserver(int32_t callbackType, void (*callback)(), int32_t* errCode)162 void ConnectionImpl::RegisterConnectionObserver(int32_t callbackType, void (*callback)(), int32_t* errCode)
163 {
164     if (!g_flag) {
165         RegisterObserverToHost();
166         g_flag = true;
167     }
168 
169     if (callbackType == REGISTER_DEVICE_FIND_TYPE) {
170         auto connectionObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CArrString)>(callback));
171         if (!connectionObserverFunc) {
172             HILOGD("Register bluetoothDeviceFind event failed");
173             *errCode = BT_ERR_INTERNAL_ERROR;
174             return;
175         }
176         g_connectionObserver->RegisterDeviceFindFunc(connectionObserverFunc);
177     }
178 
179     if (callbackType == REGISTER_PIN_REQUEST_TYPE) {
180         auto connectionObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CPinRequiredParam)>(callback));
181         if (!connectionObserverFunc) {
182             HILOGD("Register pinRequired event failed");
183             *errCode = BT_ERR_INTERNAL_ERROR;
184             return;
185         }
186         g_connectionObserver->RegisterPinRequestFunc(connectionObserverFunc);
187     }
188 
189     if (callbackType == REGISTER_BOND_STATE_TYPE) {
190         auto remoteDeviceObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CBondStateParam)>(callback));
191         if (!remoteDeviceObserverFunc) {
192             HILOGD("Register bondStateChange event failed");
193             *errCode = BT_ERR_INTERNAL_ERROR;
194             return;
195         }
196         g_remoteDeviceObserver->RegisterBondStateFunc(remoteDeviceObserverFunc);
197     }
198 
199     if (callbackType == REGISTER_BATTERY_CHANGE_TYPE) {
200         auto remoteDeviceObserverFunc = CJLambda::Create(reinterpret_cast<void (*)(CBatteryInfo)>(callback));
201         if (!remoteDeviceObserverFunc) {
202             HILOGD("Register batteryChange event failed");
203             *errCode = BT_ERR_INTERNAL_ERROR;
204             return;
205         }
206         g_remoteDeviceObserver->RegisterBatteryChangeFunc(remoteDeviceObserverFunc);
207     }
208 }
209 } // namespace BluetoothConnection
210 } // namespace CJSystemapi
211 } // namespace OHOS