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