1 /*
2 * Copyright (C) 2023 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "bluetooth_audio_manager_proxy.h"
17 #include "bluetooth_errorcode.h"
18 #include "bluetooth_log.h"
19 #include "bluetooth_service_ipc_interface_code.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
23
EnableWearDetection(const std::string & deviceId)24 int BluetoothAudioManagerProxy::EnableWearDetection(const std::string &deviceId)
25 {
26 return SetWearDetection(deviceId, true);
27 }
28
DisableWearDetection(const std::string & deviceId)29 int BluetoothAudioManagerProxy::DisableWearDetection(const std::string &deviceId)
30 {
31 return SetWearDetection(deviceId, false);
32 }
33
SetWearDetection(const std::string & deviceId,bool enable)34 int BluetoothAudioManagerProxy::SetWearDetection(const std::string &deviceId, bool enable)
35 {
36 MessageParcel data;
37 if (!data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor())) {
38 HILOGE("SetWearDetection error");
39 return BT_ERR_IPC_TRANS_FAILED;
40 }
41 if (!data.WriteString(deviceId)) {
42 HILOGE("SetWearDetectionwrite device error");
43 return BT_ERR_IPC_TRANS_FAILED;
44 }
45
46 MessageParcel reply;
47 MessageOption option{MessageOption::TF_SYNC};
48 int opcode = enable ? BluetoothAudioManagerInterfaceCode::WEAR_DETECTION_ENABLE
49 : BluetoothAudioManagerInterfaceCode::WEAR_DETECTION_DISABLE;
50 int error = Remote()->SendRequest(opcode, data, reply, option);
51 if (error != BT_NO_ERROR) {
52 HILOGE("SetWearDetection done fail, error: %{public}d", error);
53 return BT_ERR_IPC_TRANS_FAILED;
54 }
55 return reply.ReadInt32();
56 }
57
GetWearDetectionState(const std::string & deviceId,int32_t & ability)58 int BluetoothAudioManagerProxy::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
59 {
60 MessageParcel data;
61 if (!data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor())) {
62 HILOGE("BluetoothWearDetectionProxy::Enable wear detection error");
63 return BT_ERR_IPC_TRANS_FAILED;
64 }
65 if (!data.WriteString(deviceId)) {
66 HILOGE("BluetoothWearDetectionProxy::EnableWearDetection write device error");
67 return BT_ERR_IPC_TRANS_FAILED;
68 }
69
70 MessageParcel reply;
71 MessageOption option{MessageOption::TF_SYNC};
72 int error = Remote()->SendRequest(
73 static_cast<uint32_t>(BluetoothAudioManagerInterfaceCode::IS_WEAR_DETECTION_ENABLED), data, reply, option);
74 if (error != BT_NO_ERROR) {
75 HILOGE("BluetoothWearDetectionProxy::EnableWearDetections done fail, error: %{public}d", error);
76 return BT_ERR_IPC_TRANS_FAILED;
77 }
78
79 int32_t ret = reply.ReadInt32();
80 if (ret == BT_NO_ERROR) {
81 ability = reply.ReadInt32();
82 }
83
84 return ret;
85 }
86
IsWearDetectionSupported(const BluetoothRawAddress & device,bool & isSupported)87 int32_t BluetoothAudioManagerProxy::IsWearDetectionSupported(const BluetoothRawAddress &device, bool &isSupported)
88 {
89 MessageParcel data;
90 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
91 BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsWearDetectionSupported WriteInterfaceToken error");
92 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
93 "BluetoothAudioManagerProxy::IsWearDetectionSupported Write device error");
94 MessageParcel reply;
95 MessageOption option = {MessageOption::TF_SYNC};
96 int error = Remote()->SendRequest(
97 BluetoothAudioManagerInterfaceCode::BT_IS_WEAR_DETECTION_SUPPORTED, data, reply, option);
98 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
99 "BluetoothAudioManagerProxy::IsWearDetectionSupported fail, error: %{public}d", error);
100 isSupported = reply.ReadBool();
101 return reply.ReadInt32();
102 }
103
SendDeviceSelection(const BluetoothRawAddress & device,int useA2dp,int useHfp,int userSelection)104 int32_t BluetoothAudioManagerProxy::SendDeviceSelection(const BluetoothRawAddress &device,
105 int useA2dp, int useHfp, int userSelection)
106 {
107 MessageParcel data;
108 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
109 BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsWearDetectionSupported WriteInterfaceToken error");
110 CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
111 "BluetoothAudioManagerProxy::SendDeviceSelection Write device error");
112 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(useA2dp), BT_ERR_INTERNAL_ERROR,
113 "BluetoothAudioManagerProxy::SendDeviceSelection Write useA2dp error");
114 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(useHfp), BT_ERR_INTERNAL_ERROR,
115 "BluetoothAudioManagerProxy::SendDeviceSelection Write useHfp error");
116 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(userSelection), BT_ERR_INTERNAL_ERROR,
117 "BluetoothAudioManagerProxy::SendDeviceSelection Write userSelection error");
118
119 MessageParcel reply;
120 MessageOption option = {MessageOption::TF_SYNC};
121 int error = Remote()->SendRequest(
122 BluetoothAudioManagerInterfaceCode::BT_SEND_DEVICE_SELECTION, data, reply, option);
123 CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
124 "BluetoothAudioManagerProxy::SendDeviceSelection fail, error: %{public}d", error);
125
126 return reply.ReadInt32();
127 }
128
129 } // namespace Bluetooth
130 } // namespace OHOS
131