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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_audio_manager_proxy"
17 #endif
18 
19 #include "bluetooth_audio_manager_proxy.h"
20 #include "bluetooth_errorcode.h"
21 #include "bluetooth_log.h"
22 #include "bluetooth_service_ipc_interface_code.h"
23 
24 namespace OHOS {
25 namespace Bluetooth {
26 
EnableWearDetection(const std::string & deviceId)27 int BluetoothAudioManagerProxy::EnableWearDetection(const std::string &deviceId)
28 {
29     return SetWearDetection(deviceId, true);
30 }
31 
DisableWearDetection(const std::string & deviceId)32 int BluetoothAudioManagerProxy::DisableWearDetection(const std::string &deviceId)
33 {
34     return SetWearDetection(deviceId, false);
35 }
36 
SetWearDetection(const std::string & deviceId,bool enable)37 int BluetoothAudioManagerProxy::SetWearDetection(const std::string &deviceId, bool enable)
38 {
39     MessageParcel data;
40     if (!data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor())) {
41         HILOGE("SetWearDetection error");
42         return BT_ERR_IPC_TRANS_FAILED;
43     }
44     if (!data.WriteString(deviceId)) {
45         HILOGE("SetWearDetectionwrite device error");
46         return BT_ERR_IPC_TRANS_FAILED;
47     }
48 
49     MessageParcel reply;
50     MessageOption option{MessageOption::TF_SYNC};
51     int opcode = enable ? BluetoothAudioManagerInterfaceCode::WEAR_DETECTION_ENABLE
52                         : BluetoothAudioManagerInterfaceCode::WEAR_DETECTION_DISABLE;
53     int error = Remote()->SendRequest(opcode, data, reply, option);
54     if (error != BT_NO_ERROR) {
55         HILOGE("SetWearDetection done fail, error: %{public}d", error);
56         return BT_ERR_IPC_TRANS_FAILED;
57     }
58     return reply.ReadInt32();
59 }
60 
GetWearDetectionState(const std::string & deviceId,int32_t & ability)61 int BluetoothAudioManagerProxy::GetWearDetectionState(const std::string &deviceId, int32_t &ability)
62 {
63     MessageParcel data;
64     if (!data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor())) {
65         HILOGE("BluetoothWearDetectionProxy::Enable wear detection error");
66         return BT_ERR_IPC_TRANS_FAILED;
67     }
68     if (!data.WriteString(deviceId)) {
69         HILOGE("BluetoothWearDetectionProxy::EnableWearDetection write device error");
70         return BT_ERR_IPC_TRANS_FAILED;
71     }
72 
73     MessageParcel reply;
74     MessageOption option{MessageOption::TF_SYNC};
75     int error = Remote()->SendRequest(
76         static_cast<uint32_t>(BluetoothAudioManagerInterfaceCode::IS_WEAR_DETECTION_ENABLED), data, reply, option);
77     if (error != BT_NO_ERROR) {
78         HILOGE("BluetoothWearDetectionProxy::EnableWearDetections done fail, error: %{public}d", error);
79         return BT_ERR_IPC_TRANS_FAILED;
80     }
81 
82     int32_t ret = reply.ReadInt32();
83     if (ret == BT_NO_ERROR) {
84         ability = reply.ReadInt32();
85     }
86 
87     return ret;
88 }
89 
IsDeviceWearing(const BluetoothRawAddress & device)90 int32_t BluetoothAudioManagerProxy::IsDeviceWearing(const BluetoothRawAddress &device)
91 {
92     MessageParcel data;
93     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
94         BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsDeviceWearing WriteInterfaceToken error");
95     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
96         "BluetoothAudioManagerProxy::IsDeviceWearing Write device error");
97     MessageParcel reply;
98     MessageOption option = {MessageOption::TF_SYNC};
99     int error = Remote()->SendRequest(
100         BluetoothAudioManagerInterfaceCode::IS_DEVICE_WEARING, data, reply, option);
101 
102     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
103         "BluetoothAudioManagerProxy::IsDeviceWearing fail, error: %{public}d", error);
104     return reply.ReadInt32();
105 }
106 
IsWearDetectionSupported(const BluetoothRawAddress & device,bool & isSupported)107 int32_t BluetoothAudioManagerProxy::IsWearDetectionSupported(const BluetoothRawAddress &device, bool &isSupported)
108 {
109     MessageParcel data;
110     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
111         BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsWearDetectionSupported WriteInterfaceToken error");
112     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
113         "BluetoothAudioManagerProxy::IsWearDetectionSupported Write device error");
114     MessageParcel reply;
115     MessageOption option = {MessageOption::TF_SYNC};
116     int error = Remote()->SendRequest(
117         BluetoothAudioManagerInterfaceCode::BT_IS_WEAR_DETECTION_SUPPORTED, data, reply, option);
118     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
119         "BluetoothAudioManagerProxy::IsWearDetectionSupported fail, error: %{public}d", error);
120     isSupported = reply.ReadBool();
121     return reply.ReadInt32();
122 }
123 
SendDeviceSelection(const BluetoothRawAddress & device,int useA2dp,int useHfp,int userSelection)124 int32_t BluetoothAudioManagerProxy::SendDeviceSelection(const BluetoothRawAddress &device,
125     int useA2dp, int useHfp, int userSelection)
126 {
127     MessageParcel data;
128     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
129         BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsWearDetectionSupported WriteInterfaceToken error");
130     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
131         "BluetoothAudioManagerProxy::SendDeviceSelection Write device error");
132     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(useA2dp), BT_ERR_INTERNAL_ERROR,
133         "BluetoothAudioManagerProxy::SendDeviceSelection Write useA2dp error");
134     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(useHfp), BT_ERR_INTERNAL_ERROR,
135         "BluetoothAudioManagerProxy::SendDeviceSelection Write useHfp error");
136     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(userSelection), BT_ERR_INTERNAL_ERROR,
137         "BluetoothAudioManagerProxy::SendDeviceSelection Write userSelection error");
138 
139     MessageParcel reply;
140     MessageOption option = {MessageOption::TF_SYNC};
141     int error = Remote()->SendRequest(
142         BluetoothAudioManagerInterfaceCode::BT_SEND_DEVICE_SELECTION, data, reply, option);
143     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
144         "BluetoothAudioManagerProxy::SendDeviceSelection fail, error: %{public}d", error);
145 
146     return reply.ReadInt32();
147 }
148 
149 }  // namespace Bluetooth
150 }  // namespace OHOS
151