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 
IsDeviceWearing(const BluetoothRawAddress &device)87 int32_t BluetoothAudioManagerProxy::IsDeviceWearing(const BluetoothRawAddress &device)
88 {
89     MessageParcel data;
90     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
91         BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsDeviceWearing WriteInterfaceToken error");
92     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
93         "BluetoothAudioManagerProxy::IsDeviceWearing Write device error");
94     MessageParcel reply;
95     MessageOption option = {MessageOption::TF_SYNC};
96     int error = Remote()->SendRequest(
97         BluetoothAudioManagerInterfaceCode::IS_DEVICE_WEARING, data, reply, option);
98 
99     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
100         "BluetoothAudioManagerProxy::IsDeviceWearing fail, error: %{public}d", error);
101     return reply.ReadInt32();
102 }
103 
IsWearDetectionSupported(const BluetoothRawAddress &device, bool &isSupported)104 int32_t BluetoothAudioManagerProxy::IsWearDetectionSupported(const BluetoothRawAddress &device, bool &isSupported)
105 {
106     MessageParcel data;
107     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
108         BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsWearDetectionSupported WriteInterfaceToken error");
109     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
110         "BluetoothAudioManagerProxy::IsWearDetectionSupported Write device error");
111     MessageParcel reply;
112     MessageOption option = {MessageOption::TF_SYNC};
113     int error = Remote()->SendRequest(
114         BluetoothAudioManagerInterfaceCode::BT_IS_WEAR_DETECTION_SUPPORTED, data, reply, option);
115     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
116         "BluetoothAudioManagerProxy::IsWearDetectionSupported fail, error: %{public}d", error);
117     isSupported = reply.ReadBool();
118     return reply.ReadInt32();
119 }
120 
SendDeviceSelection(const BluetoothRawAddress &device, int useA2dp, int useHfp, int userSelection)121 int32_t BluetoothAudioManagerProxy::SendDeviceSelection(const BluetoothRawAddress &device,
122     int useA2dp, int useHfp, int userSelection)
123 {
124     MessageParcel data;
125     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothAudioManagerProxy::GetDescriptor()),
126         BT_ERR_INTERNAL_ERROR, "BluetoothAudioManagerProxy::IsWearDetectionSupported WriteInterfaceToken error");
127     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&device), BT_ERR_INTERNAL_ERROR,
128         "BluetoothAudioManagerProxy::SendDeviceSelection Write device error");
129     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(useA2dp), BT_ERR_INTERNAL_ERROR,
130         "BluetoothAudioManagerProxy::SendDeviceSelection Write useA2dp error");
131     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(useHfp), BT_ERR_INTERNAL_ERROR,
132         "BluetoothAudioManagerProxy::SendDeviceSelection Write useHfp error");
133     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(userSelection), BT_ERR_INTERNAL_ERROR,
134         "BluetoothAudioManagerProxy::SendDeviceSelection Write userSelection error");
135 
136     MessageParcel reply;
137     MessageOption option = {MessageOption::TF_SYNC};
138     int error = Remote()->SendRequest(
139         BluetoothAudioManagerInterfaceCode::BT_SEND_DEVICE_SELECTION, data, reply, option);
140     CHECK_AND_RETURN_LOG_RET((error == BT_NO_ERROR), BT_ERR_INTERNAL_ERROR,
141         "BluetoothAudioManagerProxy::SendDeviceSelection fail, error: %{public}d", error);
142 
143     return reply.ReadInt32();
144 }
145 
146 }  // namespace Bluetooth
147 }  // namespace OHOS
148