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