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