1 /*
2  * Copyright (C) 2021 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_a2dp_sink_proxy.h"
17 #include "bluetooth_log.h"
18 #include "parcel_bt_uuid.h"
19 #include "bluetooth_errorcode.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
Connect(const RawAddress &device)23 int BluetoothA2dpSinkProxy::Connect(const RawAddress &device)
24 {
25     MessageParcel data;
26     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
27         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
28     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
29 
30     MessageParcel reply;
31     MessageOption option(MessageOption::TF_SYNC);
32 
33     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_CONNECT,
34         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
35 
36     return reply.ReadInt32();
37 }
38 
Disconnect(const RawAddress &device)39 int BluetoothA2dpSinkProxy::Disconnect(const RawAddress &device)
40 {
41     MessageParcel data;
42     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
43         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
44     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
45 
46     MessageParcel reply;
47     MessageOption option(MessageOption::TF_SYNC);
48 
49     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DISCONNECT,
50         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
51 
52     return reply.ReadInt32();
53 }
54 
RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)55 void BluetoothA2dpSinkProxy::RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
56 {
57     MessageParcel data;
58     CHECK_AND_RETURN_LOG(
59         data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()), "WriteInterfaceToken error");
60     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "write object error");
61 
62     MessageParcel reply;
63     MessageOption option(MessageOption::TF_ASYNC);
64 
65     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_REGISTER_OBSERVER, data, reply, option);
66 }
67 
DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)68 void BluetoothA2dpSinkProxy::DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
69 {
70     MessageParcel data;
71     CHECK_AND_RETURN_LOG(
72         data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()), "WriteInterfaceToken error");
73     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "write object error");
74 
75     MessageParcel reply;
76     MessageOption option(MessageOption::TF_ASYNC);
77 
78     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DEREGISTER_OBSERVER, data, reply, option);
79 }
80 
GetDevicesByStates(const std::vector<int32_t> &states)81 std::vector<RawAddress> BluetoothA2dpSinkProxy::GetDevicesByStates(const std::vector<int32_t> &states)
82 {
83     MessageParcel data;
84     std::vector<RawAddress> rawAdds = {};
85     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
86         rawAdds, "WriteInterfaceToken error");
87     CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(states, data), rawAdds, "write rawAdds error");
88 
89     MessageParcel reply;
90     MessageOption option(MessageOption::TF_SYNC);
91 
92     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_BY_STATES,
93         data, reply, option, rawAdds);
94 
95     int32_t rawAddsSize = reply.ReadInt32();
96     for (int i = 0; i < rawAddsSize; i++) {
97         rawAdds.push_back(RawAddress(reply.ReadString()));
98     }
99     return rawAdds;
100 }
101 
GetDeviceState(const RawAddress &device)102 int BluetoothA2dpSinkProxy::GetDeviceState(const RawAddress &device)
103 {
104     MessageParcel data;
105     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
106         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
107     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
108 
109     MessageParcel reply;
110     MessageOption option(MessageOption::TF_SYNC);
111 
112     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_STATE,
113         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
114 
115     return reply.ReadInt32();
116 }
117 
GetPlayingState(const RawAddress &device, int &state)118 int BluetoothA2dpSinkProxy::GetPlayingState(const RawAddress &device, int &state)
119 {
120     MessageParcel data;
121     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
122         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
123     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
124 
125     MessageParcel reply;
126     MessageOption option(MessageOption::TF_SYNC);
127 
128     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_PLAYING_STATE,
129         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
130 
131     int32_t exception = reply.ReadInt32();
132     if (exception == NO_ERROR) {
133         state = reply.ReadInt32();
134     }
135     return exception;
136 }
137 
SetConnectStrategy(const RawAddress &device, int32_t strategy)138 int BluetoothA2dpSinkProxy::SetConnectStrategy(const RawAddress &device, int32_t strategy)
139 {
140     MessageParcel data;
141     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
142         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
143     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
144     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "write strategy error");
145 
146     MessageParcel reply;
147     MessageOption option(MessageOption::TF_SYNC);
148 
149     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SET_CONNECT_STRATEGY,
150         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
151 
152     return reply.ReadInt32();
153 }
154 
GetConnectStrategy(const RawAddress &device)155 int BluetoothA2dpSinkProxy::GetConnectStrategy(const RawAddress &device)
156 {
157     MessageParcel data;
158     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
159         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
160     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
161 
162     MessageParcel reply;
163     MessageOption option(MessageOption::TF_SYNC);
164 
165     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_CONNECT_STRATEGY,
166         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
167 
168     return reply.ReadInt32();
169 }
170 
SendDelay(const RawAddress &device, int32_t delayValue)171 int BluetoothA2dpSinkProxy::SendDelay(const RawAddress &device, int32_t delayValue)
172 {
173     MessageParcel data;
174     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
175         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
176     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
177     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(delayValue), BT_ERR_IPC_TRANS_FAILED, "write delayValue error");
178 
179     MessageParcel reply;
180     MessageOption option(MessageOption::TF_SYNC);
181 
182     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SEND_DELAY,
183         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
184 
185     return reply.ReadInt32();
186 }
187 
WriteParcelableInt32Vector( const std::vector<int32_t> &parcelableVector, Parcel &reply)188 bool BluetoothA2dpSinkProxy::WriteParcelableInt32Vector(
189     const std::vector<int32_t> &parcelableVector, Parcel &reply)
190 {
191     if (!reply.WriteInt32(parcelableVector.size())) {
192         HILOGE("write ParcelableVector failed");
193         return false;
194     }
195 
196     for (auto parcelable : parcelableVector) {
197         if (!reply.WriteInt32(parcelable)) {
198             HILOGE("write ParcelableVector failed");
199             return false;
200         }
201     }
202     return true;
203 }
204 
205 }  // namespace Bluetooth
206 }  // namespace OHOS