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