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