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