1 /*
2 * Copyright (C) 2021-2022 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_host_proxy.h"
17 #include "bluetooth_log.h"
18 #include "bluetooth_errorcode.h"
19 namespace OHOS {
20 namespace Bluetooth {
RegisterObserver(const sptr<IBluetoothHostObserver> & observer)21 void BluetoothHostProxy::RegisterObserver(const sptr<IBluetoothHostObserver> &observer)
22 {
23 HILOGI("BluetoothHostProxy::RegisterObserver start");
24 MessageParcel data;
25 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
26 HILOGE("BluetoothHostProxy::RegisterObserver WriteInterfaceToken error");
27 return;
28 }
29 if (!data.WriteRemoteObject(observer->AsObject())) {
30 HILOGE("BluetoothHostProxy::RegisterObserver error");
31 return;
32 }
33 MessageParcel reply;
34 MessageOption option = {MessageOption::TF_SYNC};
35 int32_t error = InnerTransact(IBluetoothHost::Code::BT_REGISTER_OBSERVER, option, data, reply);
36 if (error != NO_ERROR) {
37 HILOGE("BluetoothHostProxy::RegisterObserver done fail, error: %{public}d", error);
38 return;
39 }
40 HILOGI("BluetoothHostProxy::RegisterObserver success");
41 }
42
DeregisterObserver(const sptr<IBluetoothHostObserver> & observer)43 void BluetoothHostProxy::DeregisterObserver(const sptr<IBluetoothHostObserver> &observer)
44 {
45 MessageParcel data;
46 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
47 HILOGE("BluetoothHostProxy::DeregisterObserver WriteInterfaceToken error");
48 return;
49 }
50 if (!data.WriteRemoteObject(observer->AsObject())) {
51 HILOGE("BluetoothHostProxy::DeregisterObserver error");
52 return;
53 }
54 MessageParcel reply;
55 MessageOption option = {MessageOption::TF_SYNC};
56 int32_t error = InnerTransact(IBluetoothHost::Code::BT_DEREGISTER_OBSERVER, option, data, reply);
57 if (error != NO_ERROR) {
58 HILOGE("BluetoothHostProxy::DeregisterObserver done fail, error: %{public}d", error);
59 return;
60 }
61 }
62
EnableBt()63 bool BluetoothHostProxy::EnableBt()
64 {
65 MessageParcel data;
66 HILOGI("BluetoothHostProxy::EnableBt starts");
67 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
68 HILOGE("BluetoothHostProxy::EnableBt WriteInterfaceToken error");
69 return false;
70 }
71
72 MessageParcel reply;
73 MessageOption option = {MessageOption::TF_SYNC};
74 int32_t error = InnerTransact(IBluetoothHost::Code::BT_ENABLE, option, data, reply);
75 if (error != NO_ERROR) {
76 HILOGE("BluetoothHostProxy::EnableBt done fail, error: %{public}d", error);
77 return false;
78 }
79 return reply.ReadBool();
80 }
81
DisableBt()82 int32_t BluetoothHostProxy::DisableBt()
83 {
84 HILOGI("BluetoothHostProxy::DisableBt starts");
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
87 HILOGE("BluetoothHostProxy::DisableBt WriteInterfaceToken error");
88 return BT_ERR_IPC_TRANS_FAILED;
89 }
90
91 MessageParcel reply;
92 MessageOption option = {MessageOption::TF_SYNC};
93 int32_t error = InnerTransact(IBluetoothHost::Code::BT_DISABLE, option, data, reply);
94 if (error != BT_SUCCESS) {
95 HILOGE("BluetoothHostProxy::DisableBt done fail, error: %{public}d", error);
96 return BT_ERR_IPC_TRANS_FAILED;
97 }
98 return reply.ReadInt32();
99 }
100
GetProfile(const std::string & name)101 sptr<IRemoteObject> BluetoothHostProxy::GetProfile(const std::string &name)
102 {
103 MessageParcel data;
104 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
105 HILOGE("BluetoothHostProxy::GetProfile WriteInterfaceToken error");
106 return nullptr;
107 }
108 if (!data.WriteString(name)) {
109 HILOGE("BluetoothHostProxy::GetProfile name error");
110 return nullptr;
111 }
112 MessageParcel reply;
113 MessageOption option = {MessageOption::TF_SYNC};
114 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GETPROFILE, option, data, reply);
115 if (error != NO_ERROR) {
116 HILOGE("BluetoothHostProxy::GetProfile done fail, error: %{public}d", error);
117 return nullptr;
118 }
119 return reply.ReadStrongParcelable<IRemoteObject>();
120 }
121
GetBleRemote(const std::string & name)122 sptr<IRemoteObject> BluetoothHostProxy::GetBleRemote(const std::string &name)
123 {
124 MessageParcel data;
125 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
126 HILOGE("BluetoothHostProxy::GetProfile WriteInterfaceToken error");
127 return nullptr;
128 }
129 if (!data.WriteString(name)) {
130 HILOGE("BluetoothHostProxy::GetProfile name error");
131 return nullptr;
132 }
133 MessageParcel reply;
134 MessageOption option = {MessageOption::TF_SYNC};
135 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_BLE, option, data, reply);
136 if (error != NO_ERROR) {
137 HILOGE("BluetoothHostProxy::GetProfile done fail, error: %{public}d", error);
138 return nullptr;
139 }
140 return reply.ReadStrongParcelable<IRemoteObject>();
141 }
142
BluetoothFactoryReset()143 bool BluetoothHostProxy::BluetoothFactoryReset()
144 {
145 MessageParcel data;
146 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
147 HILOGE("BluetoothHostProxy::BluetoothFactoryReset WriteInterfaceToken error");
148 return false;
149 }
150 MessageParcel reply;
151 MessageOption option = {MessageOption::TF_SYNC};
152 int32_t error = InnerTransact(IBluetoothHost::Code::BT_FACTORY_RESET, option, data, reply);
153 if (error != NO_ERROR) {
154 HILOGE("BluetoothHostProxy::BluetoothFactoryReset done fail, error: %{public}d", error);
155 return false;
156 }
157 return reply.ReadBool();
158 }
159
GetBtState(int & state)160 int32_t BluetoothHostProxy::GetBtState(int &state)
161 {
162 HILOGI("BluetoothHostProxy::GetBtState starts");
163 MessageParcel data;
164 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
165 HILOGE("BluetoothHostProxy::GetBtState WriteInterfaceToken error");
166 return -1;
167 }
168 MessageParcel reply;
169 MessageOption option = {MessageOption::TF_SYNC};
170 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GETSTATE, option, data, reply);
171 if (error != NO_ERROR) {
172 HILOGE("BluetoothHostProxy::GetBtState done fail, error: %{public}d", error);
173 return BT_ERR_IPC_TRANS_FAILED;
174 }
175
176 int32_t exception = reply.ReadInt32();
177 if (exception == NO_ERROR) {
178 state = reply.ReadInt32();
179 }
180 return exception;
181 }
182
GetLocalAddress()183 std::string BluetoothHostProxy::GetLocalAddress()
184 {
185 MessageParcel data;
186 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
187 HILOGE("BluetoothHostProxy::GetLocalAddress WriteInterfaceToken error");
188 return std::string();
189 }
190 MessageParcel reply;
191 MessageOption option = {MessageOption::TF_SYNC};
192 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_LOCAL_ADDRESS, option, data, reply);
193 if (error != NO_ERROR) {
194 HILOGE("BluetoothHostProxy::GetLocalAddress done fail, error: %{public}d", error);
195 return std::string();
196 }
197 return reply.ReadString();
198 }
199
DisableBle()200 int32_t BluetoothHostProxy::DisableBle()
201 {
202 MessageParcel data;
203 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
204 HILOGE("BluetoothHostProxy::DisableBle WriteInterfaceToken error");
205 return BT_ERR_IPC_TRANS_FAILED;
206 }
207 MessageParcel reply;
208 MessageOption option = {MessageOption::TF_SYNC};
209 int32_t error = InnerTransact(IBluetoothHost::Code::BT_DISABLE_BLE, option, data, reply);
210 if (error != BT_SUCCESS) {
211 HILOGE("BluetoothHostProxy::DisableBle done fail, error: %{public}d", error);
212 return BT_ERR_IPC_TRANS_FAILED;
213 }
214 return reply.ReadInt32();
215 }
216
EnableBle()217 int32_t BluetoothHostProxy::EnableBle()
218 {
219 MessageParcel data;
220 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
221 HILOGE("BluetoothHostProxy::EnableBle WriteInterfaceToken error");
222 return BT_ERR_IPC_TRANS_FAILED;
223 }
224 MessageParcel reply;
225 MessageOption option = {MessageOption::TF_SYNC};
226
227 int32_t error = InnerTransact(IBluetoothHost::Code::BT_ENABLE_BLE, option, data, reply);
228 if (error != BT_SUCCESS) {
229 HILOGE("BluetoothHostProxy::EnableBle done fail, error: %{public}d", error);
230 return BT_ERR_IPC_TRANS_FAILED;
231 }
232 return reply.ReadInt32();
233 }
234
IsBrEnabled()235 bool BluetoothHostProxy::IsBrEnabled()
236 {
237 MessageParcel data;
238 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
239 HILOGE("BluetoothHostProxy::IsBrEnabled WriteInterfaceToken error");
240 return false;
241 }
242 MessageParcel reply;
243 MessageOption option = {MessageOption::TF_SYNC};
244 int32_t error = InnerTransact(IBluetoothHost::Code::BT_IS_BR_ENABLED, option, data, reply);
245 if (error != NO_ERROR) {
246 HILOGE("BluetoothHostProxy::IsBrEnabled done fail, error: %{public}d", error);
247 return false;
248 }
249 return reply.ReadBool();
250 }
251
IsBleEnabled()252 bool BluetoothHostProxy::IsBleEnabled()
253 {
254 MessageParcel data;
255 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
256 HILOGE("BluetoothHostProxy::IsBleEnabled WriteInterfaceToken error");
257 return false;
258 }
259 MessageParcel reply;
260 MessageOption option = {MessageOption::TF_SYNC};
261 int32_t error = InnerTransact(IBluetoothHost::Code::BT_IS_BLE_ENABLED, option, data, reply);
262 if (error != NO_ERROR) {
263 HILOGE("BluetoothHostProxy::IsBleEnabled done fail, error: %{public}d", error);
264 return false;
265 }
266 return reply.ReadBool();
267 }
268
GetProfileList()269 std::vector<uint32_t> BluetoothHostProxy::GetProfileList()
270 {
271 std::vector<uint32_t> vec;
272 MessageParcel data;
273 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
274 HILOGE("BluetoothHostProxy::GetProfileList WriteInterfaceToken error");
275 return vec;
276 }
277 MessageParcel reply;
278 MessageOption option = {MessageOption::TF_SYNC};
279 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_PROFILE_LIST, option, data, reply);
280 if (error != NO_ERROR) {
281 HILOGE("BluetoothHostProxy::GetProfileList done fail, error: %{public}d", error);
282 return vec;
283 }
284 if (!reply.ReadUInt32Vector(&vec)) {
285 HILOGE("BluetoothHostProxy::GetProfileList Read reply fail");
286 return vec;
287 }
288 return vec;
289 }
290
GetMaxNumConnectedAudioDevices()291 int32_t BluetoothHostProxy::GetMaxNumConnectedAudioDevices()
292 {
293 MessageParcel data;
294 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
295 HILOGE("BluetoothHostProxy::GetMaxNumConnectedAudioDevices WriteInterfaceToken error");
296 return -1;
297 }
298 MessageParcel reply;
299 MessageOption option = {MessageOption::TF_SYNC};
300 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_MAXNUM_CONNECTED_AUDIODEVICES, option, data, reply);
301 if (error != NO_ERROR) {
302 HILOGE("BluetoothHostProxy::GetMaxNumConnectedAudioDevices done fail, error: %{public}d", error);
303 return -1;
304 }
305 int32_t maxNum;
306 if (!reply.ReadInt32(maxNum)) {
307 HILOGE("BluetoothHostProxy::GetMaxNumConnectedAudioDevices Read reply fail");
308 return -1;
309 }
310 return maxNum;
311 }
312
GetBtConnectionState(int & state)313 int32_t BluetoothHostProxy::GetBtConnectionState(int &state)
314 {
315 HILOGI("BluetoothHostProxy::GetBtConnectionState starts");
316 MessageParcel data;
317 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
318 HILOGE("BluetoothHostProxy::GetBtConnectionState WriteInterfaceToken error");
319 return BT_ERR_IPC_TRANS_FAILED;
320 }
321 MessageParcel reply;
322 MessageOption option = {MessageOption::TF_SYNC};
323 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_BT_STATE, option, data, reply);
324 if (error != BT_SUCCESS) {
325 HILOGE("BluetoothHostProxy::GetBtConnectionState done fail, error: %{public}d", error);
326 return BT_ERR_IPC_TRANS_FAILED;
327 }
328 int32_t exception = reply.ReadInt32();
329 if (exception == BT_SUCCESS) {
330 state = reply.ReadInt32();
331 }
332 return exception;
333 }
334
GetBtProfileConnState(uint32_t profileId,int & state)335 int32_t BluetoothHostProxy::GetBtProfileConnState(uint32_t profileId, int &state)
336 {
337 MessageParcel data;
338 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
339 HILOGE("BluetoothHostProxy::GetBtProfileConnState WriteInterfaceToken error");
340 return BT_ERR_IPC_TRANS_FAILED;
341 }
342 if (!data.WriteUint32(profileId)) {
343 HILOGE("BluetoothHostProxy::GetBtProfileConnState WriteInterfaceToken error");
344 return BT_ERR_IPC_TRANS_FAILED;
345 }
346 MessageParcel reply;
347 MessageOption option = {MessageOption::TF_SYNC};
348 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_BT_PROFILE_CONNSTATE, option, data, reply);
349 if (error != BT_SUCCESS) {
350 HILOGE("BluetoothHostProxy::GetBtProfileConnState done fail, error: %{public}d", error);
351 return BT_ERR_IPC_TRANS_FAILED;
352 }
353
354 int32_t exception = reply.ReadInt32();
355 if (exception == BT_SUCCESS) {
356 state = reply.ReadInt32();
357 }
358 return exception;
359 }
360
GetLocalDeviceClass()361 int32_t BluetoothHostProxy::GetLocalDeviceClass()
362 {
363 MessageParcel data;
364 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
365 HILOGE("BluetoothHostProxy::GetLocalDeviceClass WriteInterfaceToken error");
366 return -1;
367 }
368 MessageParcel reply;
369 MessageOption option = {MessageOption::TF_SYNC};
370 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_LOCAL_DEVICE_CLASS, option, data, reply);
371 if (error != NO_ERROR) {
372 HILOGE("BluetoothHostProxy::GetLocalDeviceClass done fail, error: %{public}d", error);
373 return -1;
374 }
375
376 int32_t result;
377 if (!reply.ReadInt32(result)) {
378 HILOGE("BluetoothHostProxy::GetLocalDeviceClass Read reply fail");
379 return -1;
380 }
381 return result;
382 }
383
SetLocalDeviceClass(const int32_t & deviceClass)384 bool BluetoothHostProxy::SetLocalDeviceClass(const int32_t &deviceClass)
385 {
386 MessageParcel data;
387 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
388 HILOGE("BluetoothHostProxy::SetLocalDeviceClass WriteInterfaceToken error");
389 return false;
390 }
391 if (!data.WriteUint32(deviceClass)) {
392 HILOGE("BluetoothHostProxy::SetLocalDeviceClass WriteInterfaceToken error");
393 return false;
394 }
395 MessageParcel reply;
396 MessageOption option = {MessageOption::TF_SYNC};
397 int32_t error = InnerTransact(IBluetoothHost::Code::BT_SET_LOCAL_DEVICE_CLASS, option, data, reply);
398 if (error != NO_ERROR) {
399 HILOGE("BluetoothHostProxy::SetLocalDeviceClass done fail, error: %{public}d", error);
400 return false;
401 }
402 return reply.ReadBool();
403 }
404
GetLocalName(std::string & name)405 int32_t BluetoothHostProxy::GetLocalName(std::string &name)
406 {
407 MessageParcel data;
408 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
409 HILOGE("BluetoothHostProxy::GetLocalName WriteInterfaceToken error");
410 return BT_ERR_IPC_TRANS_FAILED;
411 }
412 MessageParcel reply;
413 MessageOption option = {MessageOption::TF_SYNC};
414 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_LOCAL_NAME, option, data, reply);
415 if (error != BT_SUCCESS) {
416 HILOGE("BluetoothHostProxy::GetLocalName done fail, error: %{public}d", error);
417 return BT_ERR_IPC_TRANS_FAILED;
418 }
419 int32_t exception = reply.ReadInt32();
420 if (exception == BT_SUCCESS) {
421 name = reply.ReadString();
422 }
423 return exception;
424 }
425
SetLocalName(const std::string & name)426 int32_t BluetoothHostProxy::SetLocalName(const std::string &name)
427 {
428 MessageParcel data;
429 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
430 HILOGE("BluetoothHostProxy::SetLocalName WriteInterfaceToken error");
431 return BT_ERR_IPC_TRANS_FAILED;
432 }
433 if (!data.WriteString(name)) {
434 HILOGE("BluetoothHostProxy::SetLocalName WriteInterfaceToken error");
435 return BT_ERR_IPC_TRANS_FAILED;
436 }
437 MessageParcel reply;
438 MessageOption option = {MessageOption::TF_SYNC};
439 int32_t error = InnerTransact(IBluetoothHost::Code::BT_SET_LOCAL_NAME, option, data, reply);
440 if (error != BT_SUCCESS) {
441 HILOGE("BluetoothHostProxy::SetLocalName done fail, error: %{public}d", error);
442 return BT_ERR_IPC_TRANS_FAILED;
443 }
444 return reply.ReadInt32();
445 }
446
GetBtScanMode(int32_t & scanMode)447 int32_t BluetoothHostProxy::GetBtScanMode(int32_t &scanMode)
448 {
449 MessageParcel data;
450 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
451 HILOGE("BluetoothHostProxy::GetBtScanMode WriteInterfaceToken error");
452 return BT_ERR_IPC_TRANS_FAILED;
453 }
454 MessageParcel reply;
455 MessageOption option = {MessageOption::TF_SYNC};
456 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_BT_SCAN_MODE, option, data, reply);
457 if (error != BT_SUCCESS) {
458 HILOGE("BluetoothHostProxy::GetBtScanMode done fail, error: %{public}d", error);
459 return BT_ERR_IPC_TRANS_FAILED;
460 }
461 int32_t exception = reply.ReadInt32();
462 if (exception == BT_SUCCESS) {
463 scanMode = reply.ReadInt32();
464 }
465 return exception;
466 }
467
SetBtScanMode(int32_t mode,int32_t duration)468 int32_t BluetoothHostProxy::SetBtScanMode(int32_t mode, int32_t duration)
469 {
470 MessageParcel data;
471 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
472 HILOGE("BluetoothHostProxy::SetBtScanMode WriteInterfaceToken error");
473 return BT_ERR_IPC_TRANS_FAILED;
474 }
475 if (!data.WriteInt32(mode)) {
476 HILOGE("BluetoothHostProxy::SetBtScanMode WriteInterfaceToken error");
477 return BT_ERR_IPC_TRANS_FAILED;
478 }
479 if (!data.WriteInt32(duration)) {
480 HILOGE("BluetoothHostProxy::SetBtScanMode WriteInterfaceToken error");
481 return BT_ERR_IPC_TRANS_FAILED;
482 }
483 MessageParcel reply;
484 MessageOption option = {MessageOption::TF_SYNC};
485 int32_t error = InnerTransact(IBluetoothHost::Code::BT_SET_BT_SCAN_MODE, option, data, reply);
486 if (error != BT_SUCCESS) {
487 HILOGE("BluetoothHostProxy::SetBtScanMode done fail, error: %{public}d", error);
488 return BT_ERR_IPC_TRANS_FAILED;
489 }
490 return reply.ReadInt32();
491 }
492
GetBondableMode(const int32_t transport)493 int32_t BluetoothHostProxy::GetBondableMode(const int32_t transport)
494 {
495 int32_t Mode;
496 MessageParcel data;
497 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
498 HILOGE("BluetoothHostProxy::GetBondableMode WriteInterfaceToken error");
499 return -1;
500 }
501 if (!data.WriteInt32(transport)) {
502 HILOGE("BluetoothHostProxy::GetBondableMode WriteInterfaceToken error");
503 return -1;
504 }
505 MessageParcel reply;
506 MessageOption option = {MessageOption::TF_SYNC};
507 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_BONDABLE_MODE, option, data, reply);
508 if (error != NO_ERROR) {
509 HILOGE("BluetoothHostProxy::GetBondableMode done fail, error: %{public}d", error);
510 return -1;
511 }
512 if (!reply.ReadInt32(Mode)) {
513 HILOGE("BluetoothHostProxy::GetBondableMode Read reply fail");
514 return -1;
515 }
516 return Mode;
517 }
518
SetBondableMode(int32_t transport,int32_t mode)519 bool BluetoothHostProxy::SetBondableMode(int32_t transport, int32_t mode)
520 {
521 MessageParcel data;
522 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
523 HILOGE("BluetoothHostProxy::SetBondableMode WriteInterfaceToken error");
524 return false;
525 }
526 if (!data.WriteInt32(transport)) {
527 HILOGE("BluetoothHostProxy::SetBondableMode WriteInterfaceToken error");
528 return false;
529 }
530 if (!data.WriteInt32(mode)) {
531 HILOGE("BluetoothHostProxy::SetBondableMode WriteInterfaceToken error");
532 return false;
533 }
534 MessageParcel reply;
535 MessageOption option = {MessageOption::TF_SYNC};
536 int32_t error = InnerTransact(IBluetoothHost::Code::BT_SET_BONDABLE_MODE, option, data, reply);
537 if (error != NO_ERROR) {
538 HILOGE("BluetoothHostProxy::SetBondableMode done fail, error: %{public}d", error);
539 return false;
540 }
541 return reply.ReadBool();
542 }
543
StartBtDiscovery()544 int32_t BluetoothHostProxy::StartBtDiscovery()
545 {
546 MessageParcel data;
547 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
548 HILOGE("BluetoothHostProxy::StartBtDiscovery WriteInterfaceToken error");
549 return BT_ERR_IPC_TRANS_FAILED;
550 }
551 MessageParcel reply;
552 MessageOption option = {MessageOption::TF_SYNC};
553 int32_t error = InnerTransact(IBluetoothHost::Code::BT_START_BT_DISCOVERY, option, data, reply);
554 if (error != BT_SUCCESS) {
555 HILOGE("BluetoothHostProxy::StartBtDiscovery done fail, error: %{public}d", error);
556 return BT_ERR_IPC_TRANS_FAILED;
557 }
558 return reply.ReadInt32();
559 }
560
CancelBtDiscovery()561 int32_t BluetoothHostProxy::CancelBtDiscovery()
562 {
563 MessageParcel data;
564 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
565 HILOGE("BluetoothHostProxy::CancelBtDiscovery WriteInterfaceToken error");
566 return BT_ERR_IPC_TRANS_FAILED;
567 }
568 MessageParcel reply;
569 MessageOption option = {MessageOption::TF_SYNC};
570
571 int32_t error = InnerTransact(IBluetoothHost::Code::BT_CANCEL_BT_DISCOVERY, option, data, reply);
572 if (error != BT_SUCCESS) {
573 HILOGE("BluetoothHostProxy::CancelBtDiscovery done fail, error: %{public}d", error);
574 return BT_ERR_IPC_TRANS_FAILED;
575 }
576 return reply.ReadInt32();
577 }
578
IsBtDiscovering(const int32_t transport)579 bool BluetoothHostProxy::IsBtDiscovering(const int32_t transport)
580 {
581 MessageParcel data;
582 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
583 HILOGE("BluetoothHostProxy::IsBtDiscovering WriteInterfaceToken error");
584 return false;
585 }
586 if (!data.WriteInt32(transport)) {
587 HILOGE("BluetoothHostProxy::IsBtDiscovering WriteInterfaceToken error");
588 return false;
589 }
590 MessageParcel reply;
591 MessageOption option = {MessageOption::TF_SYNC};
592 int32_t error = InnerTransact(IBluetoothHost::Code::BT_IS_BT_DISCOVERING, option, data, reply);
593 if (error != NO_ERROR) {
594 HILOGE("BluetoothHostProxy::Start done fail, error: %{public}d", error);
595 return false;
596 }
597 return reply.ReadBool();
598 }
599
GetBtDiscoveryEndMillis()600 long BluetoothHostProxy::GetBtDiscoveryEndMillis()
601 {
602 long millis;
603 MessageParcel data;
604 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
605 HILOGE("BluetoothHostProxy::GetBtDiscoveryEndMillis WriteInterfaceToken error");
606 return -1;
607 }
608 MessageParcel reply;
609 MessageOption option = {MessageOption::TF_SYNC};
610 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_BT_DISCOVERY_END_MILLIS, option, data, reply);
611 if (error != NO_ERROR) {
612 HILOGE("BluetoothHostProxy::GetBtDiscoveryEndMillis done fail, error: %{public}d", error);
613 return -1;
614 }
615 millis = static_cast<long>(reply.ReadInt64());
616 return millis;
617 }
618
GetPairedDevices(const int32_t transport,std::vector<BluetoothRawAddress> & pairedAddr)619 int32_t BluetoothHostProxy::GetPairedDevices(const int32_t transport, std::vector<BluetoothRawAddress> &pairedAddr)
620 {
621 std::vector<BluetoothRawAddress> vec;
622 MessageParcel data;
623 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
624 HILOGE("BluetoothHostProxy::GetPairedDevices WriteInterfaceToken error");
625 return BT_ERR_IPC_TRANS_FAILED;
626 }
627 if (!data.WriteInt32(transport)) {
628 HILOGE("BluetoothHostProxy::GetPairedDevices WriteInterfaceToken error");
629 return BT_ERR_IPC_TRANS_FAILED;
630 }
631 MessageParcel reply;
632 MessageOption option = {MessageOption::TF_SYNC};
633 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_PAIRED_DEVICES, option, data, reply);
634 if (error != BT_SUCCESS) {
635 HILOGE("BluetoothHostProxy::GetPairedDevices done fail, error: %{public}d", error);
636 return BT_ERR_IPC_TRANS_FAILED;
637 }
638 int32_t size = reply.ReadInt32();
639 for (int32_t i = 0; i < size; i++) {
640 std::shared_ptr<BluetoothRawAddress> rawAddress(reply.ReadParcelable<BluetoothRawAddress>());
641 if (!rawAddress) {
642 return BT_ERR_IPC_TRANS_FAILED;
643 }
644 pairedAddr.push_back(*rawAddress);
645 }
646 return reply.ReadInt32();
647 }
648
RemovePair(const int32_t transport,const sptr<BluetoothRawAddress> & device)649 int32_t BluetoothHostProxy::RemovePair(const int32_t transport, const sptr<BluetoothRawAddress> &device)
650 {
651 MessageParcel data;
652 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
653 HILOGE("BluetoothHostProxy::RemovePair WriteInterfaceToken error");
654 return BT_ERR_IPC_TRANS_FAILED;
655 }
656 if (!data.WriteInt32(transport)) {
657 HILOGE("BluetoothHostProxy::RemovePair WriteInterfaceToken error");
658 return BT_ERR_IPC_TRANS_FAILED;
659 }
660 if (!data.WriteStrongParcelable(device)) {
661 HILOGE("BluetoothHostProxy::RemovePair WriteInterfaceToken error");
662 return BT_ERR_IPC_TRANS_FAILED;
663 }
664 MessageParcel reply;
665 MessageOption option = {MessageOption::TF_SYNC};
666 int32_t error = InnerTransact(IBluetoothHost::Code::BT_REMOVE_PAIR, option, data, reply);
667 if (error != BT_SUCCESS) {
668 HILOGE("BluetoothHostProxy::RemovePair done fail, error: %{public}d", error);
669 return BT_ERR_IPC_TRANS_FAILED;
670 }
671 return reply.ReadInt32();
672 }
673
RemoveAllPairs()674 bool BluetoothHostProxy::RemoveAllPairs()
675 {
676 MessageParcel data;
677 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
678 HILOGE("BluetoothHostProxy::RemoveAllPairs WriteInterfaceToken error");
679 return false;
680 }
681 MessageParcel reply;
682 MessageOption option = {MessageOption::TF_SYNC};
683
684 int32_t error = InnerTransact(IBluetoothHost::Code::BT_REMOVE_ALL_PAIRS, option, data, reply);
685 if (error != NO_ERROR) {
686 HILOGE("BluetoothHostProxy::RemoveAllPairs done fail, error: %{public}d", error);
687 return false;
688 }
689 return reply.ReadBool();
690 }
691
RegisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> & observer)692 void BluetoothHostProxy::RegisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> &observer)
693 {
694 MessageParcel data;
695 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
696 HILOGE("BluetoothHostProxy::RegisterRemoteDeviceObserver WriteInterfaceToken error");
697 return;
698 }
699 if (!data.WriteRemoteObject(observer->AsObject())) {
700 HILOGE("BluetoothHostProxy::RegisterRemoteDeviceObserver WriteInterfaceToken error");
701 return;
702 }
703 MessageParcel reply;
704 MessageOption option = {MessageOption::TF_SYNC};
705 int32_t error = InnerTransact(IBluetoothHost::Code::BT_REGISTER_REMOTE_DEVICE_OBSERVER, option, data, reply);
706 if (error != NO_ERROR) {
707 HILOGE("BluetoothHostProxy::GetBtConnectionState done fail, error: %{public}d", error);
708 return;
709 }
710 return;
711 }
712
DeregisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> & observer)713 void BluetoothHostProxy::DeregisterRemoteDeviceObserver(const sptr<IBluetoothRemoteDeviceObserver> &observer)
714 {
715 MessageParcel data;
716 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
717 HILOGE("BluetoothHostProxy::DeregisterRemoteDeviceObserver WriteInterfaceToken error");
718 return;
719 }
720 if (!data.WriteRemoteObject(observer->AsObject())) {
721 HILOGE("BluetoothHostProxy::DeregisterRemoteDeviceObserver WriteInterfaceToken error");
722 return;
723 }
724 MessageParcel reply;
725 MessageOption option = {MessageOption::TF_SYNC};
726 int32_t error = InnerTransact(IBluetoothHost::Code::BT_DEREGISTER_REMOTE_DEVICE_OBSERVER, option, data, reply);
727 if (error != NO_ERROR) {
728 HILOGE("BluetoothHostProxy::DeregisterRemoteDeviceObserver done fail, error: %{public}d", error);
729 return;
730 }
731 return;
732 }
733
GetBleMaxAdvertisingDataLength()734 int32_t BluetoothHostProxy::GetBleMaxAdvertisingDataLength()
735 {
736 MessageParcel data;
737 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
738 HILOGE("BluetoothHostProxy::GetBleMaxAdvertisingDataLength WriteInterfaceToken error");
739 return false;
740 }
741 MessageParcel reply;
742 MessageOption option = {MessageOption::TF_SYNC};
743 int32_t error = InnerTransact(IBluetoothHost::Code::BT_GET_BLE_MAX_ADVERTISING_DATALENGTH, option, data, reply);
744 if (error != NO_ERROR) {
745 HILOGE("BluetoothHostProxy::GetBleMaxAdvertisingDataLength done fail, error: %{public}d", error);
746 return false;
747 }
748 return reply.ReadInt32();
749 }
750
GetDeviceType(int32_t transport,const std::string & address)751 int32_t BluetoothHostProxy::GetDeviceType(int32_t transport, const std::string &address)
752 {
753 MessageParcel data;
754 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
755 HILOGE("BluetoothHostProxy::GetDeviceType WriteInterfaceToken error");
756 return 0;
757 }
758 if (!data.WriteInt32(transport)) {
759 HILOGE("BluetoothHostProxy::GetDeviceType transport error");
760 return 0;
761 }
762 if (!data.WriteString(address)) {
763 HILOGE("BluetoothHostProxy::GetDeviceType address error");
764 return 0;
765 }
766 MessageParcel reply;
767 MessageOption option = {MessageOption::TF_SYNC};
768 int32_t error = InnerTransact(IBluetoothHost::Code::GET_DEVICE_TYPE, option, data, reply);
769 if (error != NO_ERROR) {
770 HILOGE("BluetoothHostProxy::GetDeviceType done fail, error: %{public}d", error);
771 return 0;
772 }
773 return reply.ReadInt32();
774 }
775
GetPhonebookPermission(const std::string & address)776 int32_t BluetoothHostProxy::GetPhonebookPermission(const std::string &address)
777 {
778 MessageParcel data;
779 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
780 HILOGE("BluetoothHostProxy::GetPhonebookPermission WriteInterfaceToken error");
781 return 0;
782 }
783 if (!data.WriteString(address)) {
784 HILOGE("BluetoothHostProxy::GetPhonebookPermission address error");
785 return 0;
786 }
787 MessageParcel reply;
788 MessageOption option = {MessageOption::TF_SYNC};
789 int32_t error = InnerTransact(IBluetoothHost::Code::GET_PHONEBOOK_PERMISSION, option, data, reply);
790 if (error != NO_ERROR) {
791 HILOGE("BluetoothHostProxy::GetPhonebookPermission done fail, error: %{public}d", error);
792 return 0;
793 }
794 return reply.ReadInt32();
795 }
796
SetPhonebookPermission(const std::string & address,int32_t permission)797 bool BluetoothHostProxy::SetPhonebookPermission(const std::string &address, int32_t permission)
798 {
799 MessageParcel data;
800 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
801 HILOGE("BluetoothHostProxy::SetPhonebookPermission WriteInterfaceToken error");
802 return false;
803 }
804 if (!data.WriteString(address)) {
805 HILOGE("BluetoothHostProxy::SetPhonebookPermission address error");
806 return false;
807 }
808 if (!data.WriteInt32(permission)) {
809 HILOGE("BluetoothHostProxy::SetPhonebookPermission permission error");
810 return false;
811 }
812 MessageParcel reply;
813 MessageOption option = {MessageOption::TF_SYNC};
814 int32_t error = InnerTransact(IBluetoothHost::Code::SET_PHONEBOOK_PERMISSION, option, data, reply);
815 if (error != NO_ERROR) {
816 HILOGE("BluetoothHostProxy::SetPhonebookPermission done fail, error: %{public}d", error);
817 return false;
818 }
819 return reply.ReadBool();
820 }
821
GetMessagePermission(const std::string & address)822 int32_t BluetoothHostProxy::GetMessagePermission(const std::string &address)
823 {
824 MessageParcel data;
825 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
826 HILOGE("BluetoothHostProxy::GetMessagePermission WriteInterfaceToken error");
827 return 0;
828 }
829 if (!data.WriteString(address)) {
830 HILOGE("BluetoothHostProxy::GetMessagePermission address error");
831 return 0;
832 }
833 MessageParcel reply;
834 MessageOption option = {MessageOption::TF_SYNC};
835 int32_t error = InnerTransact(IBluetoothHost::Code::GET_MESSAGE_PERMISSION, option, data, reply);
836 if (error != NO_ERROR) {
837 HILOGE("BluetoothHostProxy::GetMessagePermission done fail, error: %{public}d", error);
838 return 0;
839 }
840 return reply.ReadInt32();
841 }
842
SetMessagePermission(const std::string & address,int32_t permission)843 bool BluetoothHostProxy::SetMessagePermission(const std::string &address, int32_t permission)
844 {
845 MessageParcel data;
846 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
847 HILOGE("BluetoothHostProxy::SetMessagePermission WriteInterfaceToken error");
848 return false;
849 }
850 if (!data.WriteString(address)) {
851 HILOGE("BluetoothHostProxy::SetMessagePermission address error");
852 return false;
853 }
854 if (!data.WriteInt32(permission)) {
855 HILOGE("BluetoothHostProxy::SetMessagePermission permission error");
856 return false;
857 }
858 MessageParcel reply;
859 MessageOption option = {MessageOption::TF_SYNC};
860 int32_t error = InnerTransact(IBluetoothHost::Code::SET_MESSAGE_PERMISSION, option, data, reply);
861 if (error != NO_ERROR) {
862 HILOGE("BluetoothHostProxy::SetMessagePermission done fail, error: %{public}d", error);
863 return false;
864 }
865 return reply.ReadBool();
866 }
867
GetPowerMode(const std::string & address)868 int32_t BluetoothHostProxy::GetPowerMode(const std::string &address)
869 {
870 MessageParcel data;
871 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
872 HILOGE("BluetoothHostProxy::GetPowerMode WriteInterfaceToken error");
873 return 0;
874 }
875 if (!data.WriteString(address)) {
876 HILOGE("BluetoothHostProxy::GetPowerMode address error");
877 return 0;
878 }
879 MessageParcel reply;
880 MessageOption option = {MessageOption::TF_SYNC};
881 int32_t error = InnerTransact(IBluetoothHost::Code::GET_POWER_MODE, option, data, reply);
882 if (error != NO_ERROR) {
883 HILOGE("BluetoothHostProxy::GetPowerMode done fail, error: %{public}d", error);
884 return 0;
885 }
886 return reply.ReadInt32();
887 }
888
GetDeviceName(int32_t transport,const std::string & address,std::string & name)889 int32_t BluetoothHostProxy::GetDeviceName(int32_t transport, const std::string &address, std::string &name)
890 {
891 MessageParcel data;
892 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
893 HILOGE("BluetoothHostProxy::GetDeviceName WriteInterfaceToken error");
894 return BT_ERR_IPC_TRANS_FAILED;
895 }
896 if (!data.WriteInt32(transport)) {
897 HILOGE("BluetoothHostProxy::GetDeviceName transport error");
898 return BT_ERR_IPC_TRANS_FAILED;
899 }
900 if (!data.WriteString(address)) {
901 HILOGE("BluetoothHostProxy::GetDeviceName address error");
902 return BT_ERR_IPC_TRANS_FAILED;
903 }
904 MessageParcel reply;
905 MessageOption option = {MessageOption::TF_SYNC};
906 int32_t error = InnerTransact(IBluetoothHost::Code::GET_DEVICE_NAME, option, data, reply);
907 if (error != BT_SUCCESS) {
908 HILOGE("BluetoothHostProxy::GetDeviceName done fail, error: %{public}d", error);
909 return BT_ERR_IPC_TRANS_FAILED;
910 }
911
912 int32_t exception = reply.ReadInt32();
913 if (exception == BT_SUCCESS) {
914 name = reply.ReadString();
915 }
916 return exception;
917 }
918
GetDeviceAlias(const std::string & address)919 std::string BluetoothHostProxy::GetDeviceAlias(const std::string &address)
920 {
921 MessageParcel data;
922 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
923 HILOGE("BluetoothHostProxy::GetDeviceAlias WriteInterfaceToken error");
924 return std::string();
925 }
926 if (!data.WriteString(address)) {
927 HILOGE("BluetoothHostProxy::GetDeviceAlias address error");
928 return std::string();
929 }
930 MessageParcel reply;
931 MessageOption option = {MessageOption::TF_SYNC};
932 int32_t error = InnerTransact(IBluetoothHost::Code::GET_DEVICE_ALIAS, option, data, reply);
933 if (error != NO_ERROR) {
934 HILOGE("BluetoothHostProxy::GetDeviceAlias done fail, error: %{public}d", error);
935 return std::string();
936 }
937 return reply.ReadString();
938 }
939
SetDeviceAlias(const std::string & address,const std::string & aliasName)940 bool BluetoothHostProxy::SetDeviceAlias(const std::string &address, const std::string &aliasName)
941 {
942 MessageParcel data;
943 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
944 HILOGE("BluetoothHostProxy::SetDeviceAlias WriteInterfaceToken error");
945 return false;
946 }
947 if (!data.WriteString(address)) {
948 HILOGE("BluetoothHostProxy::SetDeviceAlias address error");
949 return false;
950 }
951 if (!data.WriteString(aliasName)) {
952 HILOGE("BluetoothHostProxy::SetDeviceAlias aliasName error");
953 return false;
954 }
955 MessageParcel reply;
956 MessageOption option = {MessageOption::TF_SYNC};
957 int32_t error = InnerTransact(IBluetoothHost::Code::SET_DEVICE_ALIAS, option, data, reply);
958 if (error != NO_ERROR) {
959 HILOGE("BluetoothHostProxy::SetDeviceAlias done fail, error: %{public}d", error);
960 return false;
961 }
962 return reply.ReadBool();
963 }
964
GetDeviceBatteryLevel(const std::string & address)965 int32_t BluetoothHostProxy::GetDeviceBatteryLevel(const std::string &address)
966 {
967 MessageParcel data;
968 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
969 HILOGE("BluetoothHostProxy::GetDeviceBatteryLevel WriteInterfaceToken error");
970 return 0;
971 }
972 if (!data.WriteString(address)) {
973 HILOGE("BluetoothHostProxy::GetDeviceBatteryLevel address error");
974 return 0;
975 }
976 MessageParcel reply;
977 MessageOption option = {MessageOption::TF_SYNC};
978 int32_t error = InnerTransact(IBluetoothHost::Code::GET_DEVICE_BATTERY_LEVEL, option, data, reply);
979 if (error != NO_ERROR) {
980 HILOGE("BluetoothHostProxy::GetDeviceBatteryLevel done fail, error: %{public}d", error);
981 return 0;
982 }
983 return reply.ReadInt32();
984 }
985
GetPairState(int32_t transport,const std::string & address)986 int32_t BluetoothHostProxy::GetPairState(int32_t transport, const std::string &address)
987 {
988 MessageParcel data;
989 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
990 HILOGE("BluetoothHostProxy::GetPairState WriteInterfaceToken error");
991 return 0;
992 }
993 if (!data.WriteInt32(transport)) {
994 HILOGE("BluetoothHostProxy::GetPairState transport error");
995 return 0;
996 }
997 if (!data.WriteString(address)) {
998 HILOGE("BluetoothHostProxy::GetPairState address error");
999 return 0;
1000 }
1001 MessageParcel reply;
1002 MessageOption option = {MessageOption::TF_SYNC};
1003 int32_t error = InnerTransact(IBluetoothHost::Code::GET_PAIR_STATE, option, data, reply);
1004 if (error != NO_ERROR) {
1005 HILOGE("BluetoothHostProxy::GetPairState done fail, error: %{public}d", error);
1006 return 0;
1007 }
1008 return reply.ReadInt32();
1009 }
1010
StartPair(int32_t transport,const std::string & address)1011 int32_t BluetoothHostProxy::StartPair(int32_t transport, const std::string &address)
1012 {
1013 MessageParcel data;
1014 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1015 HILOGE("BluetoothHostProxy::StartPair WriteInterfaceToken error");
1016 return BT_ERR_IPC_TRANS_FAILED;
1017 }
1018 if (!data.WriteInt32(transport)) {
1019 HILOGE("BluetoothHostProxy::StartPair transport error");
1020 return BT_ERR_IPC_TRANS_FAILED;
1021 }
1022 if (!data.WriteString(address)) {
1023 HILOGE("BluetoothHostProxy::StartPair address error");
1024 return BT_ERR_IPC_TRANS_FAILED;
1025 }
1026 MessageParcel reply;
1027 MessageOption option = {MessageOption::TF_SYNC};
1028 int32_t error = InnerTransact(IBluetoothHost::Code::START_PAIR, option, data, reply);
1029 if (error != BT_SUCCESS) {
1030 HILOGE("BluetoothHostProxy::StartPair done fail, error: %{public}d", error);
1031 return BT_ERR_IPC_TRANS_FAILED;
1032 }
1033 return reply.ReadInt32();
1034 }
1035
CancelPairing(int32_t transport,const std::string & address)1036 bool BluetoothHostProxy::CancelPairing(int32_t transport, const std::string &address)
1037 {
1038 MessageParcel data;
1039 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1040 HILOGE("BluetoothHostProxy::CancelPairing WriteInterfaceToken error");
1041 return false;
1042 }
1043 if (!data.WriteInt32(transport)) {
1044 HILOGE("BluetoothHostProxy::CancelPairing transport error");
1045 return false;
1046 }
1047 if (!data.WriteString(address)) {
1048 HILOGE("BluetoothHostProxy::CancelPairing address error");
1049 return false;
1050 }
1051 MessageParcel reply;
1052 MessageOption option = {MessageOption::TF_SYNC};
1053 int32_t error = InnerTransact(IBluetoothHost::Code::CANCEL_PAIRING, option, data, reply);
1054 if (error != NO_ERROR) {
1055 HILOGE("BluetoothHostProxy::CancelPairing done fail, error: %{public}d", error);
1056 return false;
1057 }
1058 return reply.ReadBool();
1059 }
1060
IsBondedFromLocal(int32_t transport,const std::string & address)1061 bool BluetoothHostProxy::IsBondedFromLocal(int32_t transport, const std::string &address)
1062 {
1063 MessageParcel data;
1064 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1065 HILOGE("BluetoothHostProxy::IsBondedFromLocal WriteInterfaceToken error");
1066 return false;
1067 }
1068 if (!data.WriteInt32(transport)) {
1069 HILOGE("BluetoothHostProxy::IsBondedFromLocal transport error");
1070 return false;
1071 }
1072 if (!data.WriteString(address)) {
1073 HILOGE("BluetoothHostProxy::IsBondedFromLocal address error");
1074 return false;
1075 }
1076 MessageParcel reply;
1077 MessageOption option = {MessageOption::TF_SYNC};
1078 int32_t error = InnerTransact(IBluetoothHost::Code::IS_BONDED_FROM_LOCAL, option, data, reply);
1079 if (error != NO_ERROR) {
1080 HILOGE("BluetoothHostProxy::IsBondedFromLocal done fail, error: %{public}d", error);
1081 return false;
1082 }
1083 return reply.ReadBool();
1084 }
1085
IsAclConnected(int32_t transport,const std::string & address)1086 bool BluetoothHostProxy::IsAclConnected(int32_t transport, const std::string &address)
1087 {
1088 MessageParcel data;
1089 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1090 HILOGE("BluetoothHostProxy::IsAclConnected WriteInterfaceToken error");
1091 return false;
1092 }
1093 if (!data.WriteInt32(transport)) {
1094 HILOGE("BluetoothHostProxy::IsAclConnected transport error");
1095 return false;
1096 }
1097 if (!data.WriteString(address)) {
1098 HILOGE("BluetoothHostProxy::IsAclConnected address error");
1099 return false;
1100 }
1101 MessageParcel reply;
1102 MessageOption option = {MessageOption::TF_SYNC};
1103 int32_t error = InnerTransact(IBluetoothHost::Code::IS_ACL_CONNECTED, option, data, reply);
1104 if (error != NO_ERROR) {
1105 HILOGE("BluetoothHostProxy::IsAclConnected done fail, error: %{public}d", error);
1106 return false;
1107 }
1108 return reply.ReadBool();
1109 }
1110
IsAclEncrypted(int32_t transport,const std::string & address)1111 bool BluetoothHostProxy::IsAclEncrypted(int32_t transport, const std::string &address)
1112 {
1113 MessageParcel data;
1114 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1115 HILOGE("BluetoothHostProxy::IsAclEncrypted WriteInterfaceToken error");
1116 return false;
1117 }
1118 if (!data.WriteInt32(transport)) {
1119 HILOGE("BluetoothHostProxy::IsAclEncrypted transport error");
1120 return false;
1121 }
1122 if (!data.WriteString(address)) {
1123 HILOGE("BluetoothHostProxy::IsAclEncrypted address error");
1124 return false;
1125 }
1126 MessageParcel reply;
1127 MessageOption option = {MessageOption::TF_SYNC};
1128 int32_t error = InnerTransact(IBluetoothHost::Code::IS_ACL_ENCRYPTED, option, data, reply);
1129 if (error != NO_ERROR) {
1130 HILOGE("BluetoothHostProxy::IsAclEncrypted done fail, error: %{public}d", error);
1131 return false;
1132 }
1133 return reply.ReadBool();
1134 }
1135
GetDeviceClass(const std::string & address,int & cod)1136 int32_t BluetoothHostProxy::GetDeviceClass(const std::string &address, int &cod)
1137 {
1138 MessageParcel data;
1139 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1140 HILOGE("BluetoothHostProxy::GetDeviceClass WriteInterfaceToken error");
1141 return BT_ERR_INTERNAL_ERROR;
1142 }
1143 if (!data.WriteString(address)) {
1144 HILOGE("BluetoothHostProxy::GetDeviceClass address error");
1145 return BT_ERR_INTERNAL_ERROR;
1146 }
1147 MessageParcel reply;
1148 MessageOption option = {MessageOption::TF_SYNC};
1149 int32_t error = InnerTransact(IBluetoothHost::Code::GET_DEVICE_CLASS, option, data, reply);
1150 if (error != BT_SUCCESS) {
1151 HILOGE("BluetoothHostProxy::GetDeviceClass done fail, error: %{public}d", error);
1152 return BT_ERR_IPC_TRANS_FAILED;
1153 }
1154 int32_t exception = reply.ReadInt32();
1155 if (exception == BT_SUCCESS) {
1156 cod = reply.ReadInt32();
1157 }
1158 return exception;
1159 }
1160
SetDevicePin(const std::string & address,const std::string & pin)1161 bool BluetoothHostProxy::SetDevicePin(const std::string &address, const std::string &pin)
1162 {
1163 MessageParcel data;
1164 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1165 HILOGE("BluetoothHostProxy::SetDevicePin WriteInterfaceToken error");
1166 return false;
1167 }
1168 if (!data.WriteString(address)) {
1169 HILOGE("BluetoothHostProxy::SetDevicePin address error");
1170 return false;
1171 }
1172 if (!data.WriteString(pin)) {
1173 HILOGE("BluetoothHostProxy::SetDevicePin pin error");
1174 return false;
1175 }
1176 MessageParcel reply;
1177 MessageOption option = {MessageOption::TF_SYNC};
1178 int32_t error = InnerTransact(IBluetoothHost::Code::SET_DEVICE_PIN, option, data, reply);
1179 if (error != NO_ERROR) {
1180 HILOGE("BluetoothHostProxy::SetDevicePin done fail, error: %{public}d", error);
1181 return false;
1182 }
1183 return reply.ReadBool();
1184 }
1185
SetDevicePairingConfirmation(int32_t transport,const std::string & address,bool accept)1186 int32_t BluetoothHostProxy::SetDevicePairingConfirmation(int32_t transport, const std::string &address, bool accept)
1187 {
1188 MessageParcel data;
1189 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1190 HILOGE("BluetoothHostProxy::SetDevicePairingConfirmation WriteInterfaceToken error");
1191 return BT_ERR_IPC_TRANS_FAILED;
1192 }
1193 if (!data.WriteInt32(transport)) {
1194 HILOGE("BluetoothHostProxy::SetDevicePairingConfirmation transport error");
1195 return BT_ERR_IPC_TRANS_FAILED;
1196 }
1197 if (!data.WriteString(address)) {
1198 HILOGE("BluetoothHostProxy::SetDevicePairingConfirmation address error");
1199 return BT_ERR_IPC_TRANS_FAILED;
1200 }
1201 if (!data.WriteBool(accept)) {
1202 HILOGE("BluetoothHostProxy::SetDevicePairingConfirmation accept error");
1203 return BT_ERR_IPC_TRANS_FAILED;
1204 }
1205 MessageParcel reply;
1206 MessageOption option = {MessageOption::TF_SYNC};
1207 int32_t error = InnerTransact(IBluetoothHost::Code::SET_DEVICE_PAIRING_CONFIRMATION, option, data, reply);
1208 if (error != BT_SUCCESS) {
1209 HILOGE("BluetoothHostProxy::SetDevicePairingConfirmation done fail, error: %{public}d", error);
1210 return BT_ERR_IPC_TRANS_FAILED;
1211 }
1212 return reply.ReadInt32();
1213 }
1214
SetDevicePasskey(int32_t transport,const std::string & address,int32_t passkey,bool accept)1215 bool BluetoothHostProxy::SetDevicePasskey(int32_t transport, const std::string &address, int32_t passkey, bool accept)
1216 {
1217 MessageParcel data;
1218 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1219 HILOGE("BluetoothHostProxy::SetDevicePasskey WriteInterfaceToken error");
1220 return false;
1221 }
1222 if (!data.WriteInt32(transport)) {
1223 HILOGE("BluetoothHostProxy::SetDevicePasskey transport error");
1224 return false;
1225 }
1226 if (!data.WriteString(address)) {
1227 HILOGE("BluetoothHostProxy::SetDevicePasskey address error");
1228 return false;
1229 }
1230 if (!data.WriteInt32(passkey)) {
1231 HILOGE("BluetoothHostProxy::SetDevicePasskey passkey error");
1232 return false;
1233 }
1234 if (!data.WriteBool(accept)) {
1235 HILOGE("BluetoothHostProxy::SetDevicePasskey accept error");
1236 return false;
1237 }
1238 MessageParcel reply;
1239 MessageOption option = {MessageOption::TF_SYNC};
1240 int32_t error = InnerTransact(IBluetoothHost::Code::SET_DEVICE_PASSKEY, option, data, reply);
1241 if (error != NO_ERROR) {
1242 HILOGE("BluetoothHostProxy::SetDevicePasskey done fail, error: %{public}d", error);
1243 return false;
1244 }
1245 return reply.ReadBool();
1246 }
1247
PairRequestReply(int32_t transport,const std::string & address,bool accept)1248 bool BluetoothHostProxy::PairRequestReply(int32_t transport, const std::string &address, bool accept)
1249 {
1250 MessageParcel data;
1251 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1252 HILOGE("BluetoothHostProxy::PairRequestReply WriteInterfaceToken error");
1253 return false;
1254 }
1255 if (!data.WriteInt32(transport)) {
1256 HILOGE("BluetoothHostProxy::PairRequestReply transport error");
1257 return false;
1258 }
1259 if (!data.WriteString(address)) {
1260 HILOGE("BluetoothHostProxy::PairRequestReply address error");
1261 return false;
1262 }
1263 if (!data.WriteBool(accept)) {
1264 HILOGE("BluetoothHostProxy::PairRequestReply accept error");
1265 return false;
1266 }
1267 MessageParcel reply;
1268 MessageOption option = {MessageOption::TF_SYNC};
1269 int32_t error = InnerTransact(IBluetoothHost::Code::PAIR_REQUEST_PEPLY, option, data, reply);
1270 if (error != NO_ERROR) {
1271 HILOGE("BluetoothHostProxy::PairRequestReply done fail, error: %{public}d", error);
1272 return false;
1273 }
1274 return reply.ReadBool();
1275 }
1276
ReadRemoteRssiValue(const std::string & address)1277 bool BluetoothHostProxy::ReadRemoteRssiValue(const std::string &address)
1278 {
1279 MessageParcel data;
1280 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1281 HILOGE("BluetoothHostProxy::ReadRemoteRssiValue WriteInterfaceToken error");
1282 return false;
1283 }
1284 if (!data.WriteString(address)) {
1285 HILOGE("BluetoothHostProxy::ReadRemoteRssiValue address error");
1286 return false;
1287 }
1288 MessageParcel reply;
1289 MessageOption option = {MessageOption::TF_SYNC};
1290 int32_t error = InnerTransact(IBluetoothHost::Code::READ_REMOTE_RSSI_VALUE, option, data, reply);
1291 if (error != NO_ERROR) {
1292 HILOGE("BluetoothHostProxy::ReadRemoteRssiValue done fail, error: %{public}d", error);
1293 return false;
1294 }
1295 return reply.ReadBool();
1296 }
1297
GetLocalSupportedUuids(std::vector<std::string> & uuids)1298 void BluetoothHostProxy::GetLocalSupportedUuids(std::vector<std::string> &uuids)
1299 {
1300 MessageParcel data;
1301 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1302 HILOGE("BluetoothHostProxy::GetLocalSupportedUuids WriteInterfaceToken error");
1303 return;
1304 }
1305 MessageParcel reply;
1306 MessageOption option = {MessageOption::TF_SYNC};
1307 int32_t error = InnerTransact(IBluetoothHost::Code::GET_LOCAL_SUPPORTED_UUIDS, option, data, reply);
1308 if (error != NO_ERROR) {
1309 HILOGE("BluetoothHostProxy::GetLocalSupportedUuids done fail, error: %{public}d", error);
1310 return;
1311 }
1312 int32_t size = reply.ReadInt32();
1313 std::string uuid;
1314 for (int32_t i = 0; i < size; i++) {
1315 uuid = reply.ReadString();
1316 uuids.push_back(uuid);
1317 }
1318 }
1319
GetDeviceUuids(int32_t transport,const std::string & address)1320 std::vector<bluetooth::Uuid> BluetoothHostProxy::GetDeviceUuids(int32_t transport, const std::string &address)
1321 {
1322 std::vector<bluetooth::Uuid> uuids;
1323 MessageParcel data;
1324 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1325 HILOGE("BluetoothHostProxy::GetDeviceUuids WriteInterfaceToken error");
1326 return uuids;
1327 }
1328 if (!data.WriteInt32(transport)) {
1329 HILOGE("BluetoothHostProxy::GetDeviceUuids Write transport error");
1330 return uuids;
1331 }
1332 if (!data.WriteString(address)) {
1333 HILOGE("BluetoothHostProxy::GetDeviceUuids Write address error");
1334 return uuids;
1335 }
1336 MessageParcel reply;
1337 MessageOption option = {MessageOption::TF_SYNC};
1338 int32_t error = InnerTransact(IBluetoothHost::Code::GET_DEVICE_UUIDS, option, data, reply);
1339 if (error != NO_ERROR) {
1340 HILOGE("BluetoothHostProxy::GetDeviceUuids done fail, error: %{public}d", error);
1341 return uuids;
1342 }
1343
1344 int32_t size = reply.ReadInt32();
1345 bluetooth::Uuid uuid;
1346 for (int32_t i = 0; i < size; i++) {
1347 uuid = ParcelBtUuid::ReadFromParcel(reply);
1348 uuids.push_back(uuid);
1349 }
1350 return uuids;
1351 }
1352
RegisterBleAdapterObserver(const sptr<IBluetoothHostObserver> & observer)1353 void BluetoothHostProxy::RegisterBleAdapterObserver(const sptr<IBluetoothHostObserver> &observer)
1354 {
1355 HILOGI("BluetoothHostProxy::RegisterBleAdapterObserver start");
1356 MessageParcel data;
1357 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1358 HILOGE("BluetoothHostProxy::RegisterBleAdapterObserver WriteInterfaceToken error");
1359 return;
1360 }
1361 if (!data.WriteRemoteObject(observer->AsObject())) {
1362 HILOGE("BluetoothHostProxy::RegisterBleAdapterObserver error");
1363 return;
1364 }
1365 MessageParcel reply;
1366 MessageOption option = {MessageOption::TF_SYNC};
1367 int32_t error = InnerTransact(IBluetoothHost::Code::BT_REGISTER_BLE_ADAPTER_OBSERVER, option, data, reply);
1368 if (error != NO_ERROR) {
1369 HILOGE("BluetoothHostProxy::RegisterBleAdapterObserver done fail, error: %{public}d", error);
1370 return;
1371 }
1372 HILOGI("BluetoothHostProxy::RegisterBleAdapterObserver success");
1373 }
1374
DeregisterBleAdapterObserver(const sptr<IBluetoothHostObserver> & observer)1375 void BluetoothHostProxy::DeregisterBleAdapterObserver(const sptr<IBluetoothHostObserver> &observer)
1376 {
1377 HILOGI("BluetoothHostProxy::DeregisterBleAdapterObserver start");
1378 MessageParcel data;
1379 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1380 HILOGE("BluetoothHostProxy::DeregisterBleAdapterObserver WriteInterfaceToken error");
1381 return;
1382 }
1383 if (!data.WriteRemoteObject(observer->AsObject())) {
1384 HILOGE("BluetoothHostProxy::DeregisterBleAdapterObserver error");
1385 return;
1386 }
1387 MessageParcel reply;
1388 MessageOption option = {MessageOption::TF_SYNC};
1389 int32_t error = InnerTransact(IBluetoothHost::Code::BT_DEREGISTER_BLE_ADAPTER_OBSERVER, option, data, reply);
1390 if (error != NO_ERROR) {
1391 HILOGE("BluetoothHostProxy::DeregisterBleAdapterObserver done fail, error: %{public}d", error);
1392 return;
1393 }
1394 }
1395
RegisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> & observer)1396 void BluetoothHostProxy::RegisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> &observer)
1397 {
1398 HILOGI("BluetoothHostProxy::RegisterBlePeripheralCallback start");
1399 MessageParcel data;
1400 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1401 HILOGE("BluetoothHostProxy::RegisterBlePeripheralCallback WriteInterfaceToken error");
1402 return;
1403 }
1404 if (!data.WriteRemoteObject(observer->AsObject())) {
1405 HILOGE("BluetoothHostProxy::RegisterBlePeripheralCallback WriteInterfaceToken error");
1406 return;
1407 }
1408 MessageParcel reply;
1409 MessageOption option = {MessageOption::TF_SYNC};
1410 int32_t error = InnerTransact(IBluetoothHost::Code::BT_REGISTER_BLE_PERIPHERAL_OBSERVER, option, data, reply);
1411 if (error != NO_ERROR) {
1412 HILOGE("BluetoothHostProxy::RegisterBlePeripheralCallback done fail, error: %{public}d", error);
1413 return;
1414 }
1415 return;
1416 }
1417
DeregisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> & observer)1418 void BluetoothHostProxy::DeregisterBlePeripheralCallback(const sptr<IBluetoothBlePeripheralObserver> &observer)
1419 {
1420 HILOGI("BluetoothHostProxy::DeregisterBlePeripheralCallback start");
1421 MessageParcel data;
1422 if (!data.WriteInterfaceToken(BluetoothHostProxy::GetDescriptor())) {
1423 HILOGE("BluetoothHostProxy::DeregisterBlePeripheralCallback WriteInterfaceToken error");
1424 return;
1425 }
1426 if (!data.WriteRemoteObject(observer->AsObject())) {
1427 HILOGE("BluetoothHostProxy::DeregisterBlePeripheralCallback WriteInterfaceToken error");
1428 return;
1429 }
1430 MessageParcel reply;
1431 MessageOption option = {MessageOption::TF_SYNC};
1432 int32_t error = InnerTransact(IBluetoothHost::Code::BT_DEREGISTER_BLE_PERIPHERAL_OBSERVER, option, data, reply);
1433 if (error != NO_ERROR) {
1434 HILOGE("BluetoothHostProxy::DeregisterBlePeripheralCallback done fail, error: %{public}d", error);
1435 return;
1436 }
1437 return;
1438 }
1439
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)1440 ErrCode BluetoothHostProxy::InnerTransact(
1441 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
1442 {
1443 auto remote = Remote();
1444 if (remote == nullptr) {
1445 HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
1446 return OBJECT_NULL;
1447 }
1448 int32_t err = remote->SendRequest(code, data, reply, flags);
1449 switch (err) {
1450 case NO_ERROR: {
1451 return NO_ERROR;
1452 }
1453 case DEAD_OBJECT: {
1454 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
1455 return DEAD_OBJECT;
1456 }
1457 default: {
1458 HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
1459 return TRANSACTION_ERR;
1460 }
1461 }
1462 return NO_ERROR;
1463 }
1464 } // namespace Bluetooth
1465 } // namespace OHOS
1466