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