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_impl"
17 #endif
18
19 #include "bluetooth_connection_impl.h"
20
21 #include "bluetooth_connection_common.h"
22 #include "bluetooth_connection_ffi.h"
23 #include "bluetooth_errorcode.h"
24 #include "bluetooth_host.h"
25 #include "napi_bluetooth_utils.h"
26 #include "xcollie/xcollie.h"
27 #include "xcollie/xcollie_define.h"
28
29 namespace OHOS {
30 namespace CJSystemapi {
31 namespace CJBluetoothConnection {
32 using Bluetooth::BluetoothHost;
33 using Bluetooth::BT_ERR_INVALID_PARAM;
34 using Bluetooth::BT_NO_ERROR;
35 using Bluetooth::BT_TRANSPORT_BREDR;
36 using Bluetooth::BTConnectState;
37 using Bluetooth::DeviceBatteryInfo;
38 using Bluetooth::GetProfileConnectionState;
39 using Bluetooth::INVALID_NAME;
40 using Bluetooth::MajorClass;
41 using Bluetooth::PAIR_NONE;
42 using OHOS::Bluetooth::BluetoothRemoteDevice;
43
PairDevice(std::string deviceId,int32_t * errCode)44 void ConnectionImpl::PairDevice(std::string deviceId, int32_t* errCode)
45 {
46 if (!IsValidAddress(deviceId)) {
47 *errCode = BT_ERR_INVALID_PARAM;
48 return;
49 }
50 BluetoothRemoteDevice remoteDevice = Bluetooth::BluetoothRemoteDevice(deviceId);
51 *errCode = remoteDevice.StartPair();
52 return;
53 }
54
GetRemoteDeviceName(std::string deviceId,int32_t * errCode)55 char* ConnectionImpl::GetRemoteDeviceName(std::string deviceId, int32_t* errCode)
56 {
57 std::string name = INVALID_NAME;
58 if (!IsValidAddress(deviceId)) {
59 *errCode = BT_ERR_INVALID_PARAM;
60 return nullptr;
61 }
62 BluetoothRemoteDevice remoteDevice = Bluetooth::BluetoothRemoteDevice(deviceId);
63 *errCode = remoteDevice.GetDeviceName(name);
64 return MallocCString(name);
65 }
66
GetRemoteDeviceClass(std::string deviceId,int32_t * errCode)67 DeviceClass ConnectionImpl::GetRemoteDeviceClass(std::string deviceId, int32_t* errCode)
68 {
69 DeviceClass ret { 0 };
70 if (!IsValidAddress(deviceId)) {
71 *errCode = BT_ERR_INVALID_PARAM;
72 return ret;
73 }
74 BluetoothRemoteDevice remoteDevice = Bluetooth::BluetoothRemoteDevice(deviceId);
75 int tmpCod = MajorClass::MAJOR_UNCATEGORIZED;
76 int tmpMajorClass = MajorClass::MAJOR_UNCATEGORIZED;
77 int tmpMajorMinorClass = MajorClass::MAJOR_UNCATEGORIZED;
78 int timerId = HiviewDFX::XCollie::GetInstance().SetTimer(
79 "GetDeviceProductType", 10, nullptr, nullptr, HiviewDFX::XCOLLIE_FLAG_LOG); // 10 表示超时时间为10s
80 *errCode = remoteDevice.GetDeviceProductType(tmpCod, tmpMajorClass, tmpMajorMinorClass);
81 HiviewDFX::XCollie::GetInstance().CancelTimer(timerId);
82 ret.majorClass = tmpMajorClass;
83 ret.majorMinorClass = tmpMajorMinorClass;
84 ret.classOfDevice = tmpCod;
85 return ret;
86 }
87
GetRemoteProfileUuids(std::string deviceId,int32_t * errCode)88 CArrString ConnectionImpl::GetRemoteProfileUuids(std::string deviceId, int32_t* errCode)
89 {
90 CArrString ret { 0 };
91 if (!IsValidAddress(deviceId)) {
92 *errCode = BT_ERR_INVALID_PARAM;
93 return ret;
94 }
95 std::vector<std::string> uuids {};
96 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(deviceId);
97 *errCode = remoteDevice.GetDeviceUuids(uuids);
98 ret = Convert2CArrString(uuids);
99 return ret;
100 }
101
GetLocalName(int32_t * errCode)102 char* ConnectionImpl::GetLocalName(int32_t* errCode)
103 {
104 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
105 std::string localName = INVALID_NAME;
106 *errCode = host->GetLocalName(localName);
107 return MallocCString(localName);
108 }
109
GetPairedDevices(int32_t * errCode)110 CArrString ConnectionImpl::GetPairedDevices(int32_t* errCode)
111 {
112 CArrString ret { 0 };
113 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
114 std::vector<BluetoothRemoteDevice> remoteDeviceLists;
115 *errCode = host->GetPairedDevices(BT_TRANSPORT_BREDR, remoteDeviceLists);
116 std::vector<std::string> deviceAddr {};
117 size_t size = remoteDeviceLists.size();
118 if (size == 0 || size > std::numeric_limits<size_t>::max() / sizeof(char*)) {
119 return ret;
120 }
121 ret.head = static_cast<char**>(malloc(sizeof(char*) * size));
122 if (!ret.head) {
123 return ret;
124 }
125
126 size_t i = 0;
127 for (; i < size; ++i) {
128 ret.head[i] = MallocCString(remoteDeviceLists[i].GetDeviceAddr());
129 }
130 ret.size = static_cast<int64_t>(i);
131 return ret;
132 }
133
GetPairState(std::string deviceId,int32_t * errCode)134 int32_t ConnectionImpl::GetPairState(std::string deviceId, int32_t* errCode)
135 {
136 if (!IsValidAddress(deviceId)) {
137 *errCode = BT_ERR_INVALID_PARAM;
138 return PAIR_NONE;
139 }
140 BluetoothRemoteDevice remoteDevice = Bluetooth::BluetoothRemoteDevice(deviceId);
141 int state = PAIR_NONE;
142 *errCode = remoteDevice.GetPairState(state);
143 int pairState = static_cast<int>(BondState::BOND_STATE_INVALID);
144 DealPairStatus(state, pairState);
145 return pairState;
146 }
147
GetProfileConnectionState(int32_t profileId,int32_t * errCode)148 int32_t ConnectionImpl::GetProfileConnectionState(int32_t profileId, int32_t* errCode)
149 {
150 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
151 int state = static_cast<int>(BTConnectState::DISCONNECTED);
152 *errCode = host->GetBtProfileConnState(Bluetooth::GetProfileId(profileId), state);
153 int status = Bluetooth::GetProfileConnectionState(state);
154 return status;
155 }
156
SetDevicePairingConfirmation(std::string deviceId,bool accept,int32_t * errCode)157 void ConnectionImpl::SetDevicePairingConfirmation(std::string deviceId, bool accept, int32_t* errCode)
158 {
159 if (!IsValidAddress(deviceId)) {
160 *errCode = BT_ERR_INVALID_PARAM;
161 return;
162 }
163 BluetoothRemoteDevice remoteDevice = Bluetooth::BluetoothRemoteDevice(deviceId);
164 if (accept) {
165 *errCode = remoteDevice.SetDevicePairingConfirmation(accept);
166 } else {
167 *errCode = remoteDevice.CancelPairing();
168 }
169 return;
170 }
171
SetDevicePinCode(std::string deviceId,std::string code,int32_t * errCode)172 void ConnectionImpl::SetDevicePinCode(std::string deviceId, std::string code, int32_t* errCode)
173 {
174 BluetoothRemoteDevice remoteDevice = Bluetooth::BluetoothRemoteDevice(deviceId);
175 *errCode = remoteDevice.SetDevicePin(code);
176 return;
177 }
178
SetLocalName(std::string localName,int32_t * errCode)179 void ConnectionImpl::SetLocalName(std::string localName, int32_t* errCode)
180 {
181 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
182 *errCode = host->SetLocalName(localName);
183 return;
184 }
185
SetBluetoothScanMode(int32_t mode,int32_t duration,int32_t * errCode)186 void ConnectionImpl::SetBluetoothScanMode(int32_t mode, int32_t duration, int32_t* errCode)
187 {
188 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
189 *errCode = host->SetBtScanMode(mode, duration);
190 if (*errCode == BT_NO_ERROR) {
191 return;
192 }
193 host->SetBondableMode(BT_TRANSPORT_BREDR, 1);
194 return;
195 }
196
GetBluetoothScanMode(int32_t * errCode)197 int32_t ConnectionImpl::GetBluetoothScanMode(int32_t* errCode)
198 {
199 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
200 int32_t scanMode = 0;
201 *errCode = host->GetBtScanMode(scanMode);
202 return scanMode;
203 }
204
StartBluetoothDiscovery(int32_t * errCode)205 void ConnectionImpl::StartBluetoothDiscovery(int32_t* errCode)
206 {
207 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
208 *errCode = host->StartBtDiscovery();
209 return;
210 }
211
StopBluetoothDiscovery(int32_t * errCode)212 void ConnectionImpl::StopBluetoothDiscovery(int32_t* errCode)
213 {
214 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
215 *errCode = host->CancelBtDiscovery();
216 return;
217 }
218
IsBluetoothDiscovering(int32_t * errCode)219 bool ConnectionImpl::IsBluetoothDiscovering(int32_t* errCode)
220 {
221 BluetoothHost* host = &BluetoothHost::GetDefaultHost();
222 bool isDiscovering = false;
223 *errCode = host->IsBtDiscovering(isDiscovering);
224 return isDiscovering;
225 }
226
SetRemoteDeviceName(std::string deviceId,std::string name,int32_t * errCode)227 void ConnectionImpl::SetRemoteDeviceName(std::string deviceId, std::string name, int32_t* errCode)
228 {
229 if (!IsValidAddress(deviceId)) {
230 *errCode = BT_ERR_INVALID_PARAM;
231 return;
232 }
233 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(deviceId);
234 *errCode = remoteDevice.SetDeviceAlias(name);
235 return;
236 }
237
GetRemoteDeviceBatteryInfo(std::string deviceId,int32_t * errCode)238 CBatteryInfo ConnectionImpl::GetRemoteDeviceBatteryInfo(std::string deviceId, int32_t* errCode)
239 {
240 CBatteryInfo info { 0 };
241 if (!IsValidAddress(deviceId)) {
242 *errCode = BT_ERR_INVALID_PARAM;
243 return info;
244 }
245 DeviceBatteryInfo batteryInfo;
246 BluetoothRemoteDevice remoteDevice = BluetoothRemoteDevice(deviceId);
247 int32_t err = remoteDevice.GetRemoteDeviceBatteryInfo(batteryInfo);
248 HILOGI("err: %{public}d", err);
249 info.batteryLevel = batteryInfo.batteryLevel_;
250 info.leftEarBatteryLevel = batteryInfo.leftEarBatteryLevel_;
251 info.leftEarChargeState = static_cast<int>(batteryInfo.leftEarChargeState_);
252 info.rightEarBatteryLevel = batteryInfo.rightEarBatteryLevel_;
253 info.rightEarChargeState = static_cast<int>(batteryInfo.rightEarChargeState_);
254 info.boxBatteryLevel = batteryInfo.boxBatteryLevel_;
255 info.boxChargeState = static_cast<int>(batteryInfo.boxChargeState_);
256 return info;
257 }
258
259 } // namespace CJBluetoothConnection
260 } // namespace CJSystemapi
261 } // namespace OHOS