• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     MessageParcel reply;
387     MessageOption option(MessageOption::TF_SYNC);
388 
389     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_RENDER_POSITION, data, reply, option,
390                                    BT_ERR_IPC_TRANS_FAILED);
391     int ret = reply.ReadInt32();
392     delayValue = reply.ReadUint32();
393     sendDataSize = reply.ReadUint64();
394     timeStamp = reply.ReadUint32();
395     return ret;
396 }
397 
OffloadStartPlaying(const RawAddress & device,const std::vector<int32_t> & sessionsId)398 int BluetoothA2dpSrcProxy::OffloadStartPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)
399 {
400     return OffloadPlayingControl(device, sessionsId, BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_START_PLAYING);
401 }
402 
OffloadStopPlaying(const RawAddress & device,const std::vector<int32_t> & sessionsId)403 int BluetoothA2dpSrcProxy::OffloadStopPlaying(const RawAddress &device, const std::vector<int32_t> &sessionsId)
404 {
405     return OffloadPlayingControl(device, sessionsId, BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_STOP_PLAYING);
406 }
407 
A2dpOffloadSessionPathRequest(const RawAddress & device,const std::vector<BluetoothA2dpStreamInfo> & info)408 int BluetoothA2dpSrcProxy::A2dpOffloadSessionPathRequest(const RawAddress &device,
409     const std::vector<BluetoothA2dpStreamInfo> &info)
410 {
411     MessageParcel data;
412     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
413         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
414     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
415     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(info.size()), BT_ERR_IPC_TRANS_FAILED, "write info size error");
416     for (auto streamInfo : info) {
417         CHECK_AND_RETURN_LOG_RET(
418             data.WriteInt32(streamInfo.sessionId), BT_ERR_IPC_TRANS_FAILED, "write sessionId error");
419         CHECK_AND_RETURN_LOG_RET(
420             data.WriteInt32(streamInfo.streamType), BT_ERR_IPC_TRANS_FAILED, "write streamType error");
421         CHECK_AND_RETURN_LOG_RET(
422             data.WriteInt32(streamInfo.sampleRate), BT_ERR_IPC_TRANS_FAILED, "write sampleRate error");
423         CHECK_AND_RETURN_LOG_RET(
424             data.WriteInt32(streamInfo.isSpatialAudio), BT_ERR_IPC_TRANS_FAILED, "write isSpatialAudio error");
425     }
426 
427     MessageParcel reply;
428     MessageOption option(MessageOption::TF_SYNC);
429 
430     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_SESSION_REQUEST,
431         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
432 
433     return reply.ReadInt32();
434 }
435 
GetOffloadCodecStatus(const RawAddress & device)436 BluetoothA2dpOffloadCodecStatus BluetoothA2dpSrcProxy::GetOffloadCodecStatus(const RawAddress &device)
437 {
438     MessageParcel data;
439     BluetoothA2dpOffloadCodecStatus offloadStatus;
440     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), offloadStatus,
441         "WriteInterfaceToken error");
442     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), offloadStatus, "write device error");
443 
444     MessageParcel reply;
445     MessageOption option(MessageOption::TF_SYNC);
446 
447     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_OFFLOAD_GET_CODEC_STATUS,
448         data, reply, option, offloadStatus);
449 
450     std::shared_ptr<BluetoothA2dpOffloadCodecStatus> statusPtr(reply.ReadParcelable<BluetoothA2dpOffloadCodecStatus>());
451     CHECK_AND_RETURN_LOG_RET(statusPtr, offloadStatus, "statusPtr is nullptr.");
452     return *statusPtr;
453 }
454 
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)455 bool BluetoothA2dpSrcProxy::WriteParcelableInt32Vector(const std::vector<int32_t> &parcelableVector, Parcel &reply)
456 {
457     CHECK_AND_RETURN_LOG_RET(reply.WriteInt32(parcelableVector.size()), false, "write ParcelableVector error");
458     for (auto parcelable : parcelableVector) {
459         CHECK_AND_RETURN_LOG_RET(reply.WriteInt32(parcelable), false, "write parcelable error");
460     }
461     return true;
462 }
463 
OffloadPlayingControl(const RawAddress & device,const std::vector<int32_t> & sessionsId,int32_t control)464 int BluetoothA2dpSrcProxy::OffloadPlayingControl(const RawAddress &device, const std::vector<int32_t> &sessionsId,
465     int32_t control)
466 {
467     MessageParcel data;
468     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), BT_ERR_IPC_TRANS_FAILED,
469         "WriteInterfaceToken error control:%{public}d", control);
470     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED,
471         "write device error control:%{public}d", control);
472     CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(sessionsId, data), BT_ERR_IPC_TRANS_FAILED,
473         "write sessionId error control:%{public}d, sessions size:%{public}d", control, (int32_t)sessionsId.size());
474 
475     MessageParcel reply;
476     MessageOption option(MessageOption::TF_SYNC);
477 
478     SEND_IPC_REQUEST_RETURN_RESULT(control, data, reply, option, BT_ERR_IPC_TRANS_FAILED);
479 
480     return reply.ReadInt32();
481 }
482 
EnableAutoPlay(const RawAddress & device)483 int BluetoothA2dpSrcProxy::EnableAutoPlay(const RawAddress &device)
484 {
485     MessageParcel data;
486     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
487         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
488     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
489 
490     MessageParcel reply;
491     MessageOption option(MessageOption::TF_SYNC);
492     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_ENABLE_AUTO_PLAY,
493         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
494     return reply.ReadInt32();
495 }
496 
DisableAutoPlay(const RawAddress & device,const int duration)497 int BluetoothA2dpSrcProxy::DisableAutoPlay(const RawAddress &device, const int duration)
498 {
499     MessageParcel data;
500     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
501         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
502     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
503     CHECK_AND_RETURN_LOG_RET(data.WriteInt32(duration), BT_ERR_IPC_TRANS_FAILED, "write duration error");
504 
505     MessageParcel reply;
506     MessageOption option(MessageOption::TF_SYNC);
507     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_DISABLE_AUTO_PLAY,
508         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
509     return reply.ReadInt32();
510 }
511 
GetAutoPlayDisabledDuration(const RawAddress & device,int & duration)512 int BluetoothA2dpSrcProxy::GetAutoPlayDisabledDuration(const RawAddress &device, int &duration)
513 {
514     MessageParcel data;
515     CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()),
516         BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
517     CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
518 
519     MessageParcel reply;
520     MessageOption option(MessageOption::TF_SYNC);
521     SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_AUTO_PLAY_DISABLED_DURATION,
522         data, reply, option, BT_ERR_IPC_TRANS_FAILED);
523     int32_t res = reply.ReadInt32();
524     if (res == BT_NO_ERROR) {
525         duration = reply.ReadInt32();
526     }
527     return res;
528 }
529 
GetVirtualDeviceList(std::vector<std::string> & devices)530 void BluetoothA2dpSrcProxy::GetVirtualDeviceList(std::vector<std::string> &devices)
531 {
532     MessageParcel data;
533     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), "WriteInterfaceToken error");
534 
535     MessageParcel reply;
536     MessageOption option(MessageOption::TF_SYNC);
537     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_GET_VIRTUALDEVICE_LIST, data, reply, option);
538 
539     int32_t rawAddsSize = reply.ReadInt32();
540 
541     CHECK_AND_RETURN_LOG(rawAddsSize <= MAX_A2DP_VIRTUAL_DEVICE, "virtual device size error.");
542 
543     for (int i = 0; i < rawAddsSize; i++) {
544         devices.push_back(reply.ReadString());
545     }
546 
547     return;
548 }
549 
UpdateVirtualDevice(int32_t action,const std::string & address)550 void BluetoothA2dpSrcProxy::UpdateVirtualDevice(int32_t action, const std::string &address)
551 {
552     MessageParcel data;
553     CHECK_AND_RETURN_LOG(data.WriteInterfaceToken(BluetoothA2dpSrcProxy::GetDescriptor()), "WriteInterfaceToken error");
554     CHECK_AND_RETURN_LOG(data.WriteInt32(action), "write action error");
555     CHECK_AND_RETURN_LOG(data.WriteString(address), "write address error");
556 
557     MessageParcel reply;
558     MessageOption option(MessageOption::TF_SYNC);
559     SEND_IPC_REQUEST_RETURN(BluetoothA2dpSrcInterfaceCode::BT_A2DP_SRC_SET_VIRTUAL_DEVICE, data, reply, option);
560     return;
561 }
562 }  // namespace Bluetooth
563 }  // namespace OHOS