1 /*
2 * Copyright (C) 2021 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_fwk_remote_device"
17 #endif
18
19 #include <string>
20
21 #include "bluetooth_raw_address.h"
22 #include "bluetooth_def.h"
23 #include "bluetooth_host.h"
24 #include "bluetooth_device.h"
25 #include "bluetooth_host_proxy.h"
26 #include "bluetooth_log.h"
27 #include "bluetooth_utils.h"
28 #include "bluetooth_profile_manager.h"
29 #include "bluetooth_remote_device.h"
30 #include "iservice_registry.h"
31 #include "system_ability_definition.h"
32 #include "bluetooth_audio_manager.h"
33
34 using namespace OHOS::bluetooth;
35
36 namespace OHOS {
37 namespace Bluetooth {
BluetoothRemoteDevice(const std::string & addr,const int transport)38 BluetoothRemoteDevice::BluetoothRemoteDevice(const std::string &addr, const int transport)
39 {
40 address_ = addr;
41 transport_ = transport;
42 }
43
GetDeviceType() const44 int BluetoothRemoteDevice::GetDeviceType() const
45 {
46 HILOGI("enter");
47 int type = 0;
48 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), type, "Invalid remote device.");
49
50 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
51 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, type, "proxy is nullptr.");
52
53 return hostProxy->GetDeviceType(transport_, address_);
54 }
55
IsValidBluetoothRemoteDevice() const56 bool BluetoothRemoteDevice::IsValidBluetoothRemoteDevice() const
57 {
58 CHECK_AND_RETURN_LOG_RET(BluetoothHost::IsValidBluetoothAddr(address_), false,
59 "invalid bluetooth addr, address_: %{public}s", GetEncryptAddr(address_).c_str());
60
61 CHECK_AND_RETURN_LOG_RET(transport_ == BT_TRANSPORT_BREDR ||
62 transport_ == BT_TRANSPORT_BLE || transport_ == BT_TRANSPORT_NONE,
63 false, "invalid transport type.");
64 return true;
65 }
66
GetTransportType() const67 int BluetoothRemoteDevice::GetTransportType() const
68 {
69 return transport_;
70 }
71
GetPhonebookPermission() const72 int BluetoothRemoteDevice::GetPhonebookPermission() const
73 {
74 HILOGI("enter");
75 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
76 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
77 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
78
79 return hostProxy->GetPhonebookPermission(address_);
80 }
81
SetPhonebookPermission(int permission)82 bool BluetoothRemoteDevice::SetPhonebookPermission(int permission)
83 {
84 HILOGI("enter, permission: %{public}d", permission);
85 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device.");
86 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
87 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
88 return hostProxy->SetPhonebookPermission(address_, permission);
89 }
90
GetMessagePermission() const91 int BluetoothRemoteDevice::GetMessagePermission() const
92 {
93 HILOGI("enter");
94 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
95 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
96 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
97 return hostProxy->GetMessagePermission(address_);
98 }
99
SetMessagePermission(int permission)100 bool BluetoothRemoteDevice::SetMessagePermission(int permission)
101 {
102 HILOGI("enter, permission: %{public}d", permission);
103 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device.");
104 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
105 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
106 return hostProxy->SetMessagePermission(address_, permission);
107 }
108
GetPowerMode(void) const109 int BluetoothRemoteDevice::GetPowerMode(void) const
110 {
111 HILOGI("enter");
112 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
113 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
114 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
115 return hostProxy->GetPowerMode(address_);
116 }
117
GetDeviceName() const118 std::string BluetoothRemoteDevice::GetDeviceName() const
119 {
120 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_NAME, "Invalid remote device");
121 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), INVALID_NAME, "bluetooth is off.");
122 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
123 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_NAME, "proxy is nullptr.");
124 std::string name = INVALID_NAME;
125 hostProxy->GetDeviceName(transport_, address_, name);
126 return name;
127 }
128
GetDeviceName(std::string & name,bool alias) const129 int BluetoothRemoteDevice::GetDeviceName(std::string &name, bool alias) const
130 {
131 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
132 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
133 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
134 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
135 return hostProxy->GetDeviceName(transport_, address_, name, alias);
136 }
137
GetDeviceAlias() const138 std::string BluetoothRemoteDevice::GetDeviceAlias() const
139 {
140 HILOGI("enter");
141 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_NAME, "Invalid remote device");
142 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
143 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_NAME, "proxy is nullptr.");
144 return hostProxy->GetDeviceAlias(address_);
145 }
146
SetDeviceAlias(const std::string & aliasName)147 int32_t BluetoothRemoteDevice::SetDeviceAlias(const std::string &aliasName)
148 {
149 HILOGI("enter");
150 CHECK_AND_RETURN_LOG_RET(
151 IsValidBluetoothRemoteDevice() && aliasName != INVALID_NAME, BT_ERR_INVALID_PARAM, "Invalid remote device");
152 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
153
154 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
155 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr.");
156 return hostProxy->SetDeviceAlias(address_, aliasName);
157 }
158
GetRemoteDeviceBatteryInfo(DeviceBatteryInfo & batteryInfo) const159 int BluetoothRemoteDevice::GetRemoteDeviceBatteryInfo(DeviceBatteryInfo &batteryInfo) const
160 {
161 HILOGI("enter");
162 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device.");
163 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
164 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
165 BluetoothBatteryInfo bluetoothBatteryInfo;
166 int32_t ret = hostProxy->GetRemoteDeviceBatteryInfo(address_, bluetoothBatteryInfo);
167 CHECK_AND_RETURN_LOG_RET(ret == BT_NO_ERROR, ret, "GetRemoteDeviceBatteryInfo fail");
168 batteryInfo.deviceId_ = address_;
169 batteryInfo.batteryLevel_ = bluetoothBatteryInfo.batteryLevel_;
170 batteryInfo.leftEarBatteryLevel_ = bluetoothBatteryInfo.leftEarBatteryLevel_;
171 batteryInfo.leftEarChargeState_ = static_cast<DeviceChargeState>(bluetoothBatteryInfo.leftEarChargeState_);
172 batteryInfo.rightEarBatteryLevel_ = bluetoothBatteryInfo.rightEarBatteryLevel_;
173 batteryInfo.rightEarChargeState_ = static_cast<DeviceChargeState>(bluetoothBatteryInfo.rightEarChargeState_);
174 batteryInfo.boxBatteryLevel_ = bluetoothBatteryInfo.boxBatteryLevel_;
175 batteryInfo.boxChargeState_ = static_cast<DeviceChargeState>(bluetoothBatteryInfo.boxChargeState_);
176 return ret;
177 }
178
GetPairState(int & pairState) const179 int BluetoothRemoteDevice::GetPairState(int &pairState) const
180 {
181 HILOGI("enter");
182 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), INVALID_VALUE, "Invalid remote device.");
183 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
184 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, INVALID_VALUE, "proxy is nullptr.");
185 return hostProxy->GetPairState(transport_, address_, pairState);
186 }
187
StartPair()188 int BluetoothRemoteDevice::StartPair()
189 {
190 HILOGI("enter");
191 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
192 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
193 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
194 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
195 return hostProxy->StartPair(transport_, address_);
196 }
197
StartCrediblePair()198 int BluetoothRemoteDevice::StartCrediblePair()
199 {
200 HILOGI("enter");
201 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
202 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
203 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
204 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
205 return hostProxy->StartCrediblePair(transport_, address_);
206 }
207
CancelPairing()208 int BluetoothRemoteDevice::CancelPairing()
209 {
210 HILOGI("enter");
211 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
212 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
213 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
214 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
215 if (hostProxy->CancelPairing(transport_, address_)) {
216 return BT_NO_ERROR;
217 }
218 return BT_ERR_INTERNAL_ERROR;
219 }
220
IsBondedFromLocal() const221 bool BluetoothRemoteDevice::IsBondedFromLocal() const
222 {
223 HILOGI("enter");
224 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
225 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
226 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
227 return hostProxy->IsBondedFromLocal(transport_, address_);
228 }
229
IsAclConnected() const230 bool BluetoothRemoteDevice::IsAclConnected() const
231 {
232 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
233 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
234 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
235 return hostProxy->IsAclConnected(transport_, address_);
236 }
237
IsAclEncrypted() const238 bool BluetoothRemoteDevice::IsAclEncrypted() const
239 {
240 HILOGI("enter");
241 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
242 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
243 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
244 return hostProxy->IsAclEncrypted(transport_, address_);
245 }
246
GetDeviceClass(int & cod) const247 int BluetoothRemoteDevice::GetDeviceClass(int &cod) const
248 {
249 HILOGD("enter");
250 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
251 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
252 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
253 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
254 int ret = hostProxy->GetDeviceClass(address_, cod);
255 return ret;
256 }
257
GetDeviceProductId(std::string & prodcutId) const258 int BluetoothRemoteDevice::GetDeviceProductId(std::string &prodcutId) const
259 {
260 HILOGD("enter");
261 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
262 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
263
264 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
265 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
266 std::shared_ptr<BluetoothRemoteDeviceInfo> info;
267 int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_MODEL_ID);
268 if (exception == BT_NO_ERROR && info != nullptr) {
269 prodcutId = info->modelId_;
270 }
271 return exception;
272 }
273
GetDeviceUuids(std::vector<std::string> & uuids) const274 int BluetoothRemoteDevice::GetDeviceUuids(std::vector<std::string> &uuids) const
275 {
276 HILOGI("enter");
277 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
278 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
279 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
280 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
281 return hostProxy->GetDeviceUuids(address_, uuids);
282 }
283
SetDevicePin(const std::string & pin)284 int BluetoothRemoteDevice::SetDevicePin(const std::string &pin)
285 {
286 HILOGI("enter");
287 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
288 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
289 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
290 return hostProxy->SetDevicePin(address_, pin);
291 }
292
SetDevicePairingConfirmation(bool accept)293 int BluetoothRemoteDevice::SetDevicePairingConfirmation(bool accept)
294 {
295 HILOGI("enter, accept: %{public}d", accept);
296 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote device");
297 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
298 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
299 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr.");
300 return hostProxy->SetDevicePairingConfirmation(transport_, address_, accept);
301 }
302
SetDevicePasskey(int passkey,bool accept)303 bool BluetoothRemoteDevice::SetDevicePasskey(int passkey, bool accept)
304 {
305 HILOGI("enter, accept: %{public}d", accept);
306 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
307 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
308 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
309 return hostProxy->SetDevicePasskey(transport_, address_, passkey, accept);
310 }
311
PairRequestReply(bool accept)312 bool BluetoothRemoteDevice::PairRequestReply(bool accept)
313 {
314 HILOGI("enter, accept: %{public}d", accept);
315 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
316 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
317 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
318 return hostProxy->PairRequestReply(transport_, address_, accept);
319 }
320
ReadRemoteRssiValue()321 bool BluetoothRemoteDevice::ReadRemoteRssiValue()
322 {
323 HILOGI("enter");
324 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), false, "Invalid remote device");
325 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
326 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, false, "proxy is nullptr.");
327 return hostProxy->ReadRemoteRssiValue(address_);
328 }
329
GetDeviceProductType(int & cod,int & majorClass,int & majorMinorClass) const330 int BluetoothRemoteDevice::GetDeviceProductType(int &cod, int &majorClass, int &majorMinorClass) const
331 {
332 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "Invalid remote device");
333
334 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
335
336 int deviceCod = 0;
337 int ret = GetDeviceClass(deviceCod);
338 BluetoothDeviceClass deviceClass = BluetoothDeviceClass(deviceCod);
339 cod = deviceClass.GetClassOfDevice();
340 majorClass = deviceClass.GetMajorClass();
341 majorMinorClass = deviceClass.GetMajorMinorClass();
342 if (cod == 0) {
343 HILOGW("cod = %{public}d", cod);
344 cod = BluetoothDevice::MAJOR_UNCATEGORIZED;
345 majorClass = BluetoothDevice::MAJOR_UNCATEGORIZED;
346 majorMinorClass = BluetoothDevice::COMPUTER_UNCATEGORIZED;
347 }
348 HILOGD("device %{public}s cod = %{public}#X, majorClass = %{public}#X, majorMinorClass = %{public}#X",
349 GetEncryptAddr(address_).c_str(), cod, majorClass, majorMinorClass);
350
351 return ret;
352 }
353
SetDeviceCustomType(int32_t deviceType) const354 int32_t BluetoothRemoteDevice::SetDeviceCustomType(int32_t deviceType) const
355 {
356 HILOGI("enter");
357 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice() && deviceType >= DeviceType::DEVICE_TYPE_DEFAULT &&
358 deviceType <= DeviceType::DEVICE_TYPE_SPEAKER, BT_ERR_INVALID_PARAM,
359 "Invalid remote device or Invalid device Type");
360 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
361
362 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
363 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr.");
364 return hostProxy->SetDeviceCustomType(address_, deviceType);
365 }
366
GetDeviceCustomType(int32_t & deviceType) const367 int32_t BluetoothRemoteDevice::GetDeviceCustomType(int32_t &deviceType) const
368 {
369 HILOGI("enter");
370 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INVALID_PARAM, "Invalid remote device");
371 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off.");
372
373 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
374 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INVALID_STATE, "proxy is nullptr.");
375 std::shared_ptr<BluetoothRemoteDeviceInfo> info;
376 int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_CUSTOM_TYPE);
377 if (exception == BT_NO_ERROR && info != nullptr) {
378 deviceType = info->customType_;
379 }
380 return exception;
381 }
382
GetDeviceVendorId(uint16_t & vendorId) const383 int32_t BluetoothRemoteDevice::GetDeviceVendorId(uint16_t &vendorId) const
384 {
385 HILOGD("enter");
386 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
387 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
388 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
389 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
390 std::shared_ptr<BluetoothRemoteDeviceInfo> info;
391 int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_VENDOR_ID);
392 if (exception == BT_NO_ERROR && info != nullptr) {
393 vendorId = info->vendorId_;
394 }
395 return exception;
396 }
397
GetDeviceProductId(uint16_t & productId) const398 int32_t BluetoothRemoteDevice::GetDeviceProductId(uint16_t &productId) const
399 {
400 HILOGD("enter");
401 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
402 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
403 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
404 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
405 std::shared_ptr<BluetoothRemoteDeviceInfo> info;
406 int32_t exception = hostProxy->GetRemoteDeviceInfo(address_, info, DeviceInfoType::DEVICE_PRODUCT_ID);
407 if (exception == BT_NO_ERROR && info != nullptr) {
408 productId = info->productId_;
409 }
410 return exception;
411 }
412
IsSupportVirtualAutoConnect(bool & outSupport) const413 int32_t BluetoothRemoteDevice::IsSupportVirtualAutoConnect(bool &outSupport) const
414 {
415 HILOGD("enter");
416 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
417 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
418 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
419 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
420 int32_t exception = hostProxy->IsSupportVirtualAutoConnect(address_, outSupport);
421 return exception;
422 }
423
SetVirtualAutoConnectType(int connType,int businessType) const424 int32_t BluetoothRemoteDevice::SetVirtualAutoConnectType(int connType, int businessType) const
425 {
426 HILOGD("enter");
427 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
428 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
429 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
430 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
431 int32_t exception = hostProxy->SetVirtualAutoConnectType(address_, connType, businessType);
432 return exception;
433 }
434
ControlDeviceAction(uint32_t controlType,uint32_t controlTypeVal,uint32_t controlObject) const435 int32_t BluetoothRemoteDevice::ControlDeviceAction(uint32_t controlType, uint32_t controlTypeVal,
436 uint32_t controlObject) const
437 {
438 HILOGD("enter");
439 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
440 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
441 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
442 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
443 int32_t exception = hostProxy->ControlDeviceAction(address_, controlType, controlTypeVal, controlObject);
444 return exception;
445 }
446
GetLastConnectionTime(int64_t & connectionTime) const447 int32_t BluetoothRemoteDevice::GetLastConnectionTime(int64_t &connectionTime) const
448 {
449 HILOGD("enter");
450 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
451 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
452 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
453 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
454 return hostProxy->GetLastConnectionTime(address_, connectionTime);
455 }
456
GetCloudBondState(int32_t & cloudBondState) const457 int32_t BluetoothRemoteDevice::GetCloudBondState(int32_t &cloudBondState) const
458 {
459 HILOGD("enter");
460 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
461 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
462 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
463 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
464 return hostProxy->GetCloudBondState(address_, cloudBondState);
465 }
466
GetDeviceTransport(int32_t & transport) const467 int32_t BluetoothRemoteDevice::GetDeviceTransport(int32_t &transport) const
468 {
469 HILOGD("enter");
470 CHECK_AND_RETURN_LOG_RET(IsValidBluetoothRemoteDevice(), BT_ERR_INTERNAL_ERROR, "Invalid remote Device");
471 CHECK_AND_RETURN_LOG_RET(IS_BT_ENABLED(), BT_ERR_INVALID_STATE, "bluetooth is off");
472 sptr<IBluetoothHost> hostProxy = GetRemoteProxy<IBluetoothHost>(BLUETOOTH_HOST);
473 CHECK_AND_RETURN_LOG_RET(hostProxy != nullptr, BT_ERR_INTERNAL_ERROR, "proxy is nullptr");
474 return hostProxy->GetDeviceTransport(address_, transport);
475 }
476 } // namespace Bluetooth
477 } // namespace OHOS
478