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 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
27 HILOGE("WriteInterfaceToken error");
28 return ERROR;
29 }
30 if (!data.WriteString(device.GetAddress())) {
31 HILOGE("write device error");
32 return ERROR;
33 }
34
35 MessageParcel reply;
36 MessageOption option {
37 MessageOption::TF_SYNC
38 };
39
40 int error = Remote()->SendRequest(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_CONNECT, data, reply, option);
41 if (error != NO_ERROR) {
42 HILOGE("error: %{public}d", error);
43 return ERROR;
44 }
45
46 return reply.ReadInt32();
47 }
48
Disconnect(const RawAddress & device)49 int BluetoothA2dpSinkProxy::Disconnect(const RawAddress &device)
50 {
51 MessageParcel data;
52 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
53 HILOGE("WriteInterfaceToken error");
54 return ERROR;
55 }
56 if (!data.WriteString(device.GetAddress())) {
57 HILOGE("write device error");
58 return ERROR;
59 }
60
61 MessageParcel reply;
62 MessageOption option {
63 MessageOption::TF_SYNC
64 };
65
66 int error = Remote()->SendRequest(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DISCONNECT, data, reply, option);
67 if (error != NO_ERROR) {
68 HILOGE("error: %{public}d", error);
69 return ERROR;
70 }
71
72 return reply.ReadInt32();
73 }
74
RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)75 void BluetoothA2dpSinkProxy::RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
76 {
77 MessageParcel data;
78 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
79 HILOGE("WriteInterfaceToken error");
80 return;
81 }
82 if (!data.WriteRemoteObject(observer->AsObject())) {
83 HILOGE("error");
84 return;
85 }
86
87 MessageParcel reply;
88 MessageOption option {
89 MessageOption::TF_ASYNC
90 };
91
92 int error = Remote()->SendRequest(
93 BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_REGISTER_OBSERVER, data, reply, option);
94 if (error != NO_ERROR) {
95 HILOGE("error: %{public}d", error);
96 return;
97 }
98 }
99
DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)100 void BluetoothA2dpSinkProxy::DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
101 {
102 MessageParcel data;
103 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
104 HILOGE("WriteInterfaceToken error");
105 return;
106 }
107 if (!data.WriteRemoteObject(observer->AsObject())) {
108 HILOGE("error");
109 return;
110 }
111
112 MessageParcel reply;
113 MessageOption option {
114 MessageOption::TF_ASYNC
115 };
116
117 int error = Remote()->SendRequest(
118 BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DEREGISTER_OBSERVER, data, reply, option);
119 if (error != NO_ERROR) {
120 HILOGE("error: %{public}d", error);
121 return;
122 }
123 }
124
GetDevicesByStates(const std::vector<int32_t> & states)125 std::vector<RawAddress> BluetoothA2dpSinkProxy::GetDevicesByStates(const std::vector<int32_t> &states)
126 {
127 MessageParcel data;
128 std::vector<RawAddress> rawAdds = {};
129 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
130 HILOGE("[GetDevicesByStates] fail: write interface token failed.");
131 return rawAdds;
132 }
133
134 if (!WriteParcelableInt32Vector(states, data)) {
135 HILOGE("[GetDevicesByStates] fail: write result failed");
136 return rawAdds;
137 }
138
139 MessageParcel reply;
140 MessageOption option = {MessageOption::TF_SYNC};
141 int error = Remote()->SendRequest(
142 BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_BY_STATES, data, reply, option);
143 if (error != NO_ERROR) {
144 HILOGE("error: %{public}d", error);
145 return rawAdds;
146 }
147 int32_t rawAddsSize = reply.ReadInt32();
148 for (int i = 0; i < rawAddsSize; i++) {
149 rawAdds.push_back(RawAddress(reply.ReadString()));
150 }
151 return rawAdds;
152 }
153
GetDeviceState(const RawAddress & device)154 int BluetoothA2dpSinkProxy::GetDeviceState(const RawAddress &device)
155 {
156 MessageParcel data;
157 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
158 HILOGE("WriteInterfaceToken error");
159 return ERROR;
160 }
161 if (!data.WriteString(device.GetAddress())) {
162 HILOGE("write device error");
163 return ERROR;
164 }
165
166 MessageParcel reply;
167 MessageOption option {
168 MessageOption::TF_SYNC
169 };
170
171 int error = Remote()->SendRequest(
172 BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_STATE, data, reply, option);
173 if (error != NO_ERROR) {
174 HILOGE("error: %{public}d", error);
175 return ERROR;
176 }
177
178 return reply.ReadInt32();
179 }
180
GetPlayingState(const RawAddress & device,int & state)181 int BluetoothA2dpSinkProxy::GetPlayingState(const RawAddress &device, int &state)
182 {
183 MessageParcel data;
184 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
185 HILOGE("WriteInterfaceToken error");
186 return BT_ERR_IPC_TRANS_FAILED;
187 }
188 if (!data.WriteString(device.GetAddress())) {
189 HILOGE("write device error");
190 return BT_ERR_IPC_TRANS_FAILED;
191 }
192
193 MessageParcel reply;
194 MessageOption option {
195 MessageOption::TF_SYNC
196 };
197
198 int error = Remote()->SendRequest(
199 BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_PLAYING_STATE, data, reply, option);
200 if (error != NO_ERROR) {
201 HILOGE("error: %{public}d", error);
202 return BT_ERR_IPC_TRANS_FAILED;
203 }
204
205 int32_t exception = reply.ReadInt32();
206 if (exception == NO_ERROR) {
207 state = reply.ReadInt32();
208 }
209 return exception;
210 }
211
SetConnectStrategy(const RawAddress & device,int32_t strategy)212 int BluetoothA2dpSinkProxy::SetConnectStrategy(const RawAddress &device, int32_t strategy)
213 {
214 MessageParcel data;
215 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
216 HILOGE("WriteInterfaceToken error");
217 return ERROR;
218 }
219 if (!data.WriteString(device.GetAddress())) {
220 HILOGE("write device error");
221 return ERROR;
222 }
223 if (!data.WriteInt32(strategy)) {
224 HILOGE("write strategy error");
225 return ERROR;
226 }
227
228 MessageParcel reply;
229 MessageOption option {
230 MessageOption::TF_SYNC
231 };
232
233 int error = Remote()->SendRequest(
234 BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SET_CONNECT_STRATEGY, data, reply, option);
235 if (error != NO_ERROR) {
236 HILOGE("error: %{public}d", error);
237 return ERROR;
238 }
239
240 return reply.ReadInt32();
241 }
242
GetConnectStrategy(const RawAddress & device)243 int BluetoothA2dpSinkProxy::GetConnectStrategy(const RawAddress &device)
244 {
245 MessageParcel data;
246 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
247 HILOGE("WriteInterfaceToken error");
248 return ERROR;
249 }
250 if (!data.WriteString(device.GetAddress())) {
251 HILOGE("write device error");
252 return ERROR;
253 }
254
255 MessageParcel reply;
256 MessageOption option {
257 MessageOption::TF_SYNC
258 };
259
260 int error = Remote()->SendRequest(
261 BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_CONNECT_STRATEGY, data, reply, option);
262 if (error != NO_ERROR) {
263 HILOGE("error: %{public}d", error);
264 return ERROR;
265 }
266
267 return reply.ReadInt32();
268 }
269
SendDelay(const RawAddress & device,int32_t delayValue)270 int BluetoothA2dpSinkProxy::SendDelay(const RawAddress &device, int32_t delayValue)
271 {
272 MessageParcel data;
273 if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
274 HILOGE("WriteInterfaceToken error");
275 return ERROR;
276 }
277 if (!data.WriteString(device.GetAddress())) {
278 HILOGE("write device error");
279 return ERROR;
280 }
281 if (!data.WriteInt32(delayValue)) {
282 HILOGE("write delayValue error");
283 return ERROR;
284 }
285
286 MessageParcel reply;
287 MessageOption option {
288 MessageOption::TF_SYNC
289 };
290
291 int error = Remote()->SendRequest(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SEND_DELAY, data, reply, option);
292 if (error != NO_ERROR) {
293 HILOGE("error: %{public}d", error);
294 return ERROR;
295 }
296
297 return reply.ReadInt32();
298 }
299
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)300 bool BluetoothA2dpSinkProxy::WriteParcelableInt32Vector(
301 const std::vector<int32_t> &parcelableVector, Parcel &reply)
302 {
303 if (!reply.WriteInt32(parcelableVector.size())) {
304 HILOGE("write ParcelableVector failed");
305 return false;
306 }
307
308 for (auto parcelable : parcelableVector) {
309 if (!reply.WriteInt32(parcelable)) {
310 HILOGE("write ParcelableVector failed");
311 return false;
312 }
313 }
314 return true;
315 }
316
317 } // namespace Bluetooth
318 } // namespace OHOS