1 /*
2  * Copyright (C) 2021-2022 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_src_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 {
23 #define MAX_A2DP_VIRTUAL_DEVICE 10
Connect(const RawAddress &device)24 int32_t BluetoothA2dpSrcProxy::Connect(const RawAddress &device)
25 {
26     MessageParcel data;
27     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
28         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
29     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
30 
31     MessageParcel reply;
32     MessageOption option(MessageOption::TF_SYNC);
33 
34     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_CONNECT,
35         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
36 
37     return reply.ReadInt32();
38 }
39 
Disconnect(const RawAddress &device)40 int32_t BluetoothA2dpSrcProxy::Disconnect(const RawAddress &device)
41 {
42     MessageParcel data;
43     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
44         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
45     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
46 
47     MessageParcel reply;
48     MessageOption option(MessageOption::TF_SYNC);
49 
50     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_DISCONNECT,
51         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
52 
53     return reply.ReadInt32();
54 }
55 
RegisterObserver(const sptr<IBluetoothA2dpSourceObserver> &observer)56 void BluetoothA2dpSrcProxy::RegisterObserver(const sptr<IBluetoothA2dpSourceObserver> &observer)
57 {
58     MessageParcel data;
59     CHECK_AND_RETURN_LOG(
60         data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), "WriteInterfaceToken error");
61     CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "write object error");
62 
63     MessageParcel reply;
64     MessageOption option(MessageOption::TF_ASYNC);
65 
66     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_REGISTER_OBSERVER, data, reply, option);
67 }
68 
DeregisterObserver(const sptr<IBluetoothA2dpSourceObserver> &observer)69 void BluetoothA2dpSrcProxy::DeregisterObserver(const sptr<IBluetoothA2dpSourceObserver> &observer)
70 {
71     MessageParcel data;
72     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::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(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_DEREGISTER_OBSERVER, data, reply, option);
79 }
80 
GetDevicesByStates(const std::vector<int32_t> &states, std::vector<RawAddress> &rawAddrs)81 int BluetoothA2dpSrcProxy::GetDevicesByStates(const std::vector<int32_t> &states, std::vector<RawAddress> &rawAddrs)
82 {
83     MessageParcel data;
84     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
85         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
86     CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(states, data), BT_ERR_IPC_TRANS_FAILED, "write states error");
87 
88     MessageParcel reply;
89     MessageOption option(MessageOption::TF_SYNC);
90 
91     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_DEVICE_BY_STATES,
92         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
93 
94     int errorCode = reply.ReadInt32();
95     if (errorCode == NO_ERROR) {
96         int32_t rawAddsSize = reply.ReadInt32();
97         for (int i = 0; i < rawAddsSize; i++) {
98             rawAddrs.push_back(RawAddress(reply.ReadString()));
99         }
100     }
101 
102     return errorCode;
103 }
104 
GetDeviceState(const RawAddress &device, int &state)105 int BluetoothA2dpSrcProxy::GetDeviceState(const RawAddress &device, int &state)
106 {
107     MessageParcel data;
108     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
109         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
110     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
111 
112     MessageParcel reply;
113     MessageOption option(MessageOption::TF_SYNC);
114 
115     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_DEVICE_STATE,
116         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
117 
118     int errorCode = reply.ReadInt32();
119     if (errorCode == NO_ERROR) {
120         state = reply.ReadInt32();
121     }
122 
123     return errorCode;
124 }
125 
GetPlayingState(const RawAddress &device, int &state)126 int32_t BluetoothA2dpSrcProxy::GetPlayingState(const RawAddress &device, int &state)
127 {
128     MessageParcel data;
129     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
130         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
131     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
132 
133     MessageParcel reply;
134     MessageOption option(MessageOption::TF_SYNC);
135 
136     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_PLAYING_STATE,
137         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
138 
139     int32_t exception = reply.ReadInt32();
140     if (exception == NO_ERROR) {
141         state = reply.ReadInt32();
142     }
143     return exception;
144 }
145 
SetConnectStrategy(const RawAddress &device, int32_t strategy)146 int BluetoothA2dpSrcProxy::SetConnectStrategy(const RawAddress &device, int32_t strategy)
147 {
148     MessageParcel data;
149     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
150         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
151     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
152     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "write strategy error");
153 
154     MessageParcel reply;
155     MessageOption option(MessageOption::TF_SYNC);
156 
157     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_SET_CONNECT_STRATEGY,
158         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
159 
160     return reply.ReadInt32();
161 }
162 
GetConnectStrategy(const RawAddress &device, int &strategy)163 int BluetoothA2dpSrcProxy::GetConnectStrategy(const RawAddress &device, int &strategy)
164 {
165     MessageParcel data;
166     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
167         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
168     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
169 
170     MessageParcel reply;
171     MessageOption option(MessageOption::TF_SYNC);
172 
173     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_CONNECT_STRATEGY,
174         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
175 
176     int32_t res = reply.ReadInt32();
177     if (res == NO_ERROR) {
178         strategy = reply.ReadInt32();
179     }
180     return res;
181 }
182 
SetActiveSinkDevice(const RawAddress &device)183 int BluetoothA2dpSrcProxy::SetActiveSinkDevice(const RawAddress &device)
184 {
185     MessageParcel data;
186     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
187         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
188     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
189 
190     MessageParcel reply;
191     MessageOption option(MessageOption::TF_SYNC);
192 
193     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_SET_ACTIVE_SINK_DEVICE,
194         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
195 
196     return reply.ReadInt32();
197 }
198 
GetActiveSinkDevice()199 RawAddress BluetoothA2dpSrcProxy::GetActiveSinkDevice()
200 {
201     MessageParcel data;
202     std::string address = "";
203     RawAddress rawAddress = RawAddress(address);
204     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
205         rawAddress, "WriteInterfaceToken error");
206 
207     MessageParcel reply;
208     MessageOption option(MessageOption::TF_SYNC);
209 
210     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_ACTIVE_SINK_DEVICE,
211         data, reply, option, rawAddress);
212 
213     rawAddress = RawAddress(reply.ReadString());
214     return rawAddress;
215 }
216 
GetCodecStatus(const RawAddress &device)217 BluetoothA2dpCodecStatus BluetoothA2dpSrcProxy::GetCodecStatus(const RawAddress &device)
218 {
219     MessageParcel data;
220     BluetoothA2dpCodecStatus codecStatus;
221     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
222         codecStatus, "WriteInterfaceToken error");
223     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), codecStatus, "write device error");
224 
225     MessageParcel reply;
226     MessageOption option(MessageOption::TF_SYNC);
227 
228     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_CODEC_STATUS,
229         data, reply, option, codecStatus);
230 
231     std::shared_ptr<BluetoothA2dpCodecStatus> statusPtr(reply.ReadParcelable<BluetoothA2dpCodecStatus>());
232     if (!statusPtr) {
233         return codecStatus;
234     }
235 
236     return *statusPtr;
237 }
238 
GetCodecPreference(const RawAddress &device, BluetoothA2dpCodecInfo &info)239 int BluetoothA2dpSrcProxy::GetCodecPreference(const RawAddress &device, BluetoothA2dpCodecInfo &info)
240 {
241     MessageParcel data;
242     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
243         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
244     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
245 
246     MessageParcel reply;
247     MessageOption option(MessageOption::TF_SYNC);
248 
249     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_CODEC_PREFERENCE,
250         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
251 
252     int32_t exception = reply.ReadInt32();
253     if (exception != BT_NO_ERROR) {
254         HILOGE("error: %{public}d", exception);
255         return exception;
256     }
257     std::shared_ptr<BluetoothA2dpCodecInfo> bluetoothA2dpCodecInfo(reply.ReadParcelable<BluetoothA2dpCodecInfo>());
258     if (bluetoothA2dpCodecInfo == nullptr) {
259         HILOGE("bluetoothA2dpCodecInfo is nullptr");
260         return BT_ERR_IPC_TRANS_FAILED;
261     }
262     info = *bluetoothA2dpCodecInfo;
263     return exception;
264 }
265 
SetCodecPreference(const RawAddress &device, const BluetoothA2dpCodecInfo &info)266 int BluetoothA2dpSrcProxy::SetCodecPreference(const RawAddress &device, const BluetoothA2dpCodecInfo &info)
267 {
268     MessageParcel data;
269     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
270         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
271     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
272     CHECK_AND_RETURN_LOG_RET(data.WriteParcelable(&info), BT_ERR_IPC_TRANS_FAILED, "write info error");
273 
274     MessageParcel reply;
275     MessageOption option(MessageOption::TF_SYNC);
276 
277     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_SET_CODEC_PREFERENCE,
278         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
279 
280     return reply.ReadInt32();
281 }
282 
SwitchOptionalCodecs(const RawAddress &device, bool isEnable)283 void BluetoothA2dpSrcProxy::SwitchOptionalCodecs(const RawAddress &device, bool isEnable)
284 {
285     MessageParcel data;
286     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
287         "WriteInterfaceToken error");
288     CHECK_AND_RETURN_LOG(data.WriteString(device.GetAddress()), "write device error");
289     CHECK_AND_RETURN_LOG(data.WriteBool(isEnable), "write isEnable error");
290 
291     MessageParcel reply;
292     MessageOption option(MessageOption::TF_SYNC);
293 
294     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_SWITCH_OPTIONAL_CODECS, data, reply, option);
295 }
296 
GetOptionalCodecsSupportState(const RawAddress &device)297 int BluetoothA2dpSrcProxy::GetOptionalCodecsSupportState(const RawAddress &device)
298 {
299     MessageParcel data;
300     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
301         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
302     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
303 
304     MessageParcel reply;
305     MessageOption option(MessageOption::TF_SYNC);
306 
307     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_OPTIONAL_CODECS_SUPPORT_STATE,
308         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
309 
310     return reply.ReadInt32();
311 }
312 
StartPlaying(const RawAddress &device)313 int BluetoothA2dpSrcProxy::StartPlaying(const RawAddress &device)
314 {
315     MessageParcel data;
316     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
317         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
318     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
319 
320     MessageParcel reply;
321     MessageOption option(MessageOption::TF_SYNC);
322 
323     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_START_PLAYING,
324         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
325 
326     return reply.ReadInt32();
327 }
328 
SuspendPlaying(const RawAddress &device)329 int BluetoothA2dpSrcProxy::SuspendPlaying(const RawAddress &device)
330 {
331     MessageParcel data;
332     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
333         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
334     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
335 
336     MessageParcel reply;
337     MessageOption option(MessageOption::TF_SYNC);
338 
339     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_SUSPEND_PLAYING,
340         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
341 
342     return reply.ReadInt32();
343 }
344 
StopPlaying(const RawAddress &device)345 int BluetoothA2dpSrcProxy::StopPlaying(const RawAddress &device)
346 {
347     MessageParcel data;
348     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
349         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
350     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
351 
352     MessageParcel reply;
353     MessageOption option(MessageOption::TF_SYNC);
354 
355     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_STOP_PLAYING,
356         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
357 
358     return reply.ReadInt32();
359 }
360 
WriteFrame(const uint8_t *data, uint32_t size)361 int BluetoothA2dpSrcProxy::WriteFrame(const uint8_t *data, uint32_t size)
362 {
363     MessageParcel messageData;
364     CHECK_AND_RETURN_LOG_RET(messageData.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
365         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
366     std::vector<uint8_t> dataVector;
367     dataVector.assign(data, data + size);
368     messageData.WriteUInt8Vector(dataVector);
369 
370     MessageParcel reply;
371     MessageOption option(MessageOption::TF_SYNC);
372 
373     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_WRITE_FRAME,
374         messageData, reply, option, BT_ERR_IPC_TRANS_FAILED);
375 
376     return reply.ReadInt32();
377 }
378 
GetRenderPosition(const RawAddress &device, uint32_t &delayValue, uint64_t &sendDataSize, uint32_t &timeStamp)379 int BluetoothA2dpSrcProxy::GetRenderPosition(const RawAddress &device, uint32_t &delayValue, uint64_t &sendDataSize,
380                                              uint32_t &timeStamp)
381 {
382     MessageParcel data;
383     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), BT_ERR_IPC_TRANS_FAILED,
384                              "WriteInterfaceToken error");
385     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
386 
387     MessageParcel reply;
388     MessageOption option(MessageOption::TF_SYNC);
389 
390     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_RENDER_POSITION, data, reply, option,
391                                    BT_ERR_IPC_TRANS_FAILED);
392     int ret = reply.ReadInt32();
393     delayValue = reply.ReadUint32();
394     sendDataSize = reply.ReadUint64();
395     timeStamp = reply.ReadUint32();
396     return ret;
397 }
398 
OffloadStartPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)399 int BluetoothA2dpSrcProxy::OffloadStartPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)
400 {
401     return OffloadPlayingControl(device, sessionsId, BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_START_PLAYING);
402 }
403 
OffloadStopPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)404 int BluetoothA2dpSrcProxy::OffloadStopPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)
405 {
406     return OffloadPlayingControl(device, sessionsId, BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_STOP_PLAYING);
407 }
408 
A2dpOffloadSessionPathRequest(const RawAddress &device, const std::vector<BluetoothA2dpStreamInfo> &info)409 int BluetoothA2dpSrcProxy::A2dpOffloadSessionPathRequest(const RawAddress &device,
410     const std::vector<BluetoothA2dpStreamInfo> &info)
411 {
412     MessageParcel data;
413     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
414         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
415     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
416     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(info.size()), BT_ERR_IPC_TRANS_FAILED, "write info size error");
417     for (auto streamInfo : info) {
418         CHECK_AND_RETURN_LOG_RET(
419             data.WriteInt32(streamInfo.sessionId), BT_ERR_IPC_TRANS_FAILED, "write sessionId error");
420         CHECK_AND_RETURN_LOG_RET(
421             data.WriteInt32(streamInfo.streamType), BT_ERR_IPC_TRANS_FAILED, "write streamType error");
422         CHECK_AND_RETURN_LOG_RET(
423             data.WriteInt32(streamInfo.sampleRate), BT_ERR_IPC_TRANS_FAILED, "write sampleRate error");
424         CHECK_AND_RETURN_LOG_RET(
425             data.WriteInt32(streamInfo.isSpatialAudio), BT_ERR_IPC_TRANS_FAILED, "write isSpatialAudio error");
426     }
427 
428     MessageParcel reply;
429     MessageOption option(MessageOption::TF_SYNC);
430 
431     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_SESSION_REQUEST,
432         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
433 
434     return reply.ReadInt32();
435 }
436 
GetOffloadCodecStatus(const RawAddress &device)437 BluetoothA2dpOffloadCodecStatus BluetoothA2dpSrcProxy::GetOffloadCodecStatus(const RawAddress &device)
438 {
439     MessageParcel data;
440     BluetoothA2dpOffloadCodecStatus offloadStatus;
441     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), offloadStatus,
442         "WriteInterfaceToken error");
443     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), offloadStatus, "write device error");
444 
445     MessageParcel reply;
446     MessageOption option(MessageOption::TF_SYNC);
447 
448     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_GET_CODEC_STATUS,
449         data, reply, option, offloadStatus);
450 
451     std::shared_ptr<BluetoothA2dpOffloadCodecStatus> statusPtr(reply.ReadParcelable<BluetoothA2dpOffloadCodecStatus>());
452     CHECK_AND_RETURN_LOG_RET(statusPtr, offloadStatus, "statusPtr is nullptr.");
453     return *statusPtr;
454 }
455 
WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)456 bool BluetoothA2dpSrcProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
457 {
458     CHECK_AND_RETURN_LOG_RET(reply.WriteInt32(parcelableVector.size()), false, "write ParcelableVector error");
459     for (auto parcelable : parcelableVector) {
460         CHECK_AND_RETURN_LOG_RET(reply.WriteInt32(parcelable), false, "write parcelable error");
461     }
462     return true;
463 }
464 
OffloadPlayingControl(const RawAddress &device, const std::vector<int32_t> &sessionsId, int32_t control)465 int BluetoothA2dpSrcProxy::OffloadPlayingControl(const RawAddress &device, const std::vector<int32_t> &sessionsId,
466     int32_t control)
467 {
468     MessageParcel data;
469     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), BT_ERR_IPC_TRANS_FAILED,
470         "WriteInterfaceToken error control:%{public}d", control);
471     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED,
472         "write device error control:%{public}d", control);
473     CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(sessionsId, data), BT_ERR_IPC_TRANS_FAILED,
474         "write sessionId error control:%{public}d, sessions size:%{public}d", control, (int32_t)sessionsId.size());
475 
476     MessageParcel reply;
477     MessageOption option(MessageOption::TF_SYNC);
478 
479     SEND_IPC_REQUEST_RETURN_RESULT(control, data, reply, option, BT_ERR_IPC_TRANS_FAILED);
480 
481     return reply.ReadInt32();
482 }
483 
EnableAutoPlay(const RawAddress &device)484 int BluetoothA2dpSrcProxy::EnableAutoPlay(const RawAddress &device)
485 {
486     MessageParcel data;
487     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
488         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
489     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
490 
491     MessageParcel reply;
492     MessageOption option(MessageOption::TF_SYNC);
493     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_ENABLE_AUTO_PLAY,
494         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
495     return reply.ReadInt32();
496 }
497 
DisableAutoPlay(const RawAddress &device, const int duration)498 int BluetoothA2dpSrcProxy::DisableAutoPlay(const RawAddress &device, const int duration)
499 {
500     MessageParcel data;
501     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
502         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
503     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
504     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(duration), BT_ERR_IPC_TRANS_FAILED, "write duration error");
505 
506     MessageParcel reply;
507     MessageOption option(MessageOption::TF_SYNC);
508     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_DISABLE_AUTO_PLAY,
509         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
510     return reply.ReadInt32();
511 }
512 
GetAutoPlayDisabledDuration(const RawAddress &device, int &duration)513 int BluetoothA2dpSrcProxy::GetAutoPlayDisabledDuration(const RawAddress &device, int &duration)
514 {
515     MessageParcel data;
516     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
517         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
518     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
519 
520     MessageParcel reply;
521     MessageOption option(MessageOption::TF_SYNC);
522     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_AUTO_PLAY_DISABLED_DURATION,
523         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
524     int32_t res = reply.ReadInt32();
525     if (res == BT_NO_ERROR) {
526         duration = reply.ReadInt32();
527     }
528     return res;
529 }
530 
GetVirtualDeviceList(std::vector<std::string> &devices)531 void BluetoothA2dpSrcProxy::GetVirtualDeviceList(std::vector<std::string> &devices)
532 {
533     MessageParcel data;
534     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), "WriteInterfaceToken error");
535 
536     MessageParcel reply;
537     MessageOption option(MessageOption::TF_SYNC);
538     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_VIRTUALDEVICE_LIST, data, reply, option);
539 
540     int32_t rawAddsSize = reply.ReadInt32();
541 
542     CHECK_AND_RETURN_LOG(rawAddsSize <= MAX_A2DP_VIRTUAL_DEVICE, "virtual device size error.");
543 
544     for (int i = 0; i < rawAddsSize; i++) {
545         devices.push_back(reply.ReadString());
546     }
547 
548     return;
549 }
550 
UpdateVirtualDevice(int32_t action, const std::string &address)551 void BluetoothA2dpSrcProxy::UpdateVirtualDevice(int32_t action, const std::string &address)
552 {
553     MessageParcel data;
554     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), "WriteInterfaceToken error");
555     CHECK_AND_RETURN_LOG(data.WriteInt32(action), "write action error");
556     CHECK_AND_RETURN_LOG(data.WriteString(address), "write address error");
557 
558     MessageParcel reply;
559     MessageOption option(MessageOption::TF_SYNC);
560     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_SET_VIRTUAL_DEVICE, data, reply, option);
561     return;
562 }
563 }  // namespace Bluetooth
564 }  // namespace OHOS