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