1 /*
2  * Copyright (C) 2021 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_avrcp_ct_proxy.h"
17 #include "bluetooth_log.h"
18 
19 namespace OHOS {
20 namespace Bluetooth {
RegisterObserver(const sptr<IBluetoothAvrcpCtObserver> &observer)21 void BluetoothAvrcpCtProxy::RegisterObserver(const sptr<IBluetoothAvrcpCtObserver> &observer)
22 {
23     MessageParcel data;
24     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
25         HILOGE("[RegisterObserver] fail: write interface token failed.");
26         return;
27     }
28 
29     if (!data.WriteRemoteObject(observer->AsObject())) {
30         HILOGE("[RegisterObserver] fail: write result failed");
31         return;
32     }
33 
34     MessageParcel reply;
35     MessageOption option = {MessageOption::TF_ASYNC};
36     int error = InnerTransact(
37         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_REGISTER_OBSERVER, option, data, reply);
38     if (error != NO_ERROR) {
39         HILOGE("BluetoothAvrcpCtProxy::RegisterObserver done fail, error: %{public}d", error);
40         return;
41     }
42 }
UnregisterObserver(const sptr<IBluetoothAvrcpCtObserver> &observer)43 void BluetoothAvrcpCtProxy::UnregisterObserver(const sptr<IBluetoothAvrcpCtObserver> &observer)
44 {
45     MessageParcel data;
46     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
47         HILOGE("[UnregisterObserver] fail: write interface token failed.");
48         return;
49     }
50 
51     if (!data.WriteRemoteObject(observer->AsObject())) {
52         HILOGE("[UnregisterObserver] fail: write result failed");
53         return;
54     }
55 
56     MessageParcel reply;
57     MessageOption option = {MessageOption::TF_ASYNC};
58     int error = InnerTransact(
59         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_UNREGISTER_OBSERVER, option, data, reply);
60     if (error != NO_ERROR) {
61         HILOGE("BluetoothAvrcpCtProxy::UnregisterObserver done fail, error: %{public}d", error);
62         return;
63     }
64 }
65 
GetConnectedDevices()66 std::vector<RawAddress> BluetoothAvrcpCtProxy::GetConnectedDevices()
67 {
68     MessageParcel data;
69     std::vector<RawAddress> rawAdds = {};
70     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
71         HILOGE("[GetConnectedDevices] fail: write interface token failed.");
72         return rawAdds;
73     }
74 
75     MessageParcel reply;
76     MessageOption option = {MessageOption::TF_SYNC};
77     int error = InnerTransact(
78         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_CONNECTED_DEVICES, option, data, reply);
79     if (error != NO_ERROR) {
80         HILOGE("BluetoothAvrcpCtProxy::GetConnectedDevices done fail, error: %{public}d", error);
81         return rawAdds;
82     }
83     int32_t rawAddsSize = reply.ReadInt32();
84     for (int i = 0; i < rawAddsSize; i++) {
85         rawAdds.push_back(RawAddress(reply.ReadString()));
86     }
87     return rawAdds;
88 }
89 
GetDevicesByStates(const std::vector<int32_t> &states)90 std::vector<RawAddress> BluetoothAvrcpCtProxy::GetDevicesByStates(const std::vector<int32_t> &states)
91 {
92     MessageParcel data;
93     std::vector<RawAddress> rawAdds = {};
94     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
95         HILOGE("[GetDevicesByStates] fail: write interface token failed.");
96         return rawAdds;
97     }
98 
99     if (!WriteParcelableInt32Vector(states, data)) {
100         HILOGE("[GetDevicesByStates] fail: write result failed");
101         return rawAdds;
102     }
103 
104     MessageParcel reply;
105     MessageOption option = {MessageOption::TF_SYNC};
106     int error = InnerTransact(
107         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_DEVICES_BY_STATES, option, data, reply);
108     if (error != NO_ERROR) {
109         HILOGE("BluetoothAvrcpCtProxy::GetDevicesByStates done fail, error: %{public}d", error);
110         return rawAdds;
111     }
112     int32_t rawAddsSize = reply.ReadInt32();
113     for (int i = 0; i < rawAddsSize; i++) {
114         rawAdds.push_back(RawAddress(reply.ReadString()));
115     }
116     return rawAdds;
117 }
118 
GetDeviceState(const RawAddress &device)119 int32_t BluetoothAvrcpCtProxy::GetDeviceState(const RawAddress &device)
120 {
121     MessageParcel data;
122     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
123         HILOGE("[GetDeviceState] fail: write interface token failed.");
124         return -1;
125     }
126 
127     if (!data.WriteString(device.GetAddress())) {
128         HILOGE("[GetDeviceState] fail: write result failed");
129         return -1;
130     }
131 
132     MessageParcel reply;
133     MessageOption option = {MessageOption::TF_SYNC};
134     int error = InnerTransact(
135         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_DEVICE_STATE, option, data, reply);
136     if (error != NO_ERROR) {
137         HILOGE("BluetoothAvrcpCtProxy::GetDeviceState done fail, error: %{public}d", error);
138         return -1;
139     }
140     return reply.ReadInt32();
141 }
142 
Connect(const RawAddress &device)143 int32_t BluetoothAvrcpCtProxy::Connect(const RawAddress &device)
144 {
145     MessageParcel data;
146     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
147         HILOGE("[Connect] fail: write interface token failed.");
148         return -1;
149     }
150 
151     if (!data.WriteString(device.GetAddress())) {
152         HILOGE("[Connect] fail: write result failed");
153         return -1;
154     }
155 
156     MessageParcel reply;
157     MessageOption option = {MessageOption::TF_SYNC};
158     int error = InnerTransact(
159         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_CONNECT, option, data, reply);
160     if (error != NO_ERROR) {
161         HILOGE("BluetoothAvrcpCtProxy::Connect done fail, error: %{public}d", error);
162         return -1;
163     }
164     return reply.ReadInt32();
165 }
166 
Disconnect(const RawAddress &device)167 int32_t BluetoothAvrcpCtProxy::Disconnect(const RawAddress &device)
168 {
169     MessageParcel data;
170     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
171         HILOGE("[Disconnect] fail: write interface token failed.");
172         return -1;
173     }
174 
175     if (!data.WriteString(device.GetAddress())) {
176         HILOGE("[Disconnect] fail: write result failed");
177         return -1;
178     }
179 
180     MessageParcel reply;
181     MessageOption option = {MessageOption::TF_SYNC};
182     int error = InnerTransact(
183         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_DISCONNECT, option, data, reply);
184     if (error != NO_ERROR) {
185         HILOGE("BluetoothAvrcpCtProxy::Disconnect done fail, error: %{public}d", error);
186         return -1;
187     }
188     return reply.ReadInt32();
189 }
190 
PressButton(const RawAddress &device, int32_t button)191 int32_t BluetoothAvrcpCtProxy::PressButton(const RawAddress &device, int32_t button)
192 {
193     MessageParcel data;
194     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
195         HILOGE("[PressButton] fail: write interface token failed.");
196         return -1;
197     }
198 
199     if (!data.WriteString(device.GetAddress())) {
200         HILOGE("[PressButton] fail: write result failed");
201         return -1;
202     }
203 
204     if (!data.WriteInt32(button)) {
205         HILOGE("[PressButton] fail: write result failed");
206         return -1;
207     }
208 
209     MessageParcel reply;
210     MessageOption option = {MessageOption::TF_SYNC};
211     int error = InnerTransact(
212         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_PRESS_BUTTON, option, data, reply);
213     if (error != NO_ERROR) {
214         HILOGE("BluetoothAvrcpCtProxy::PressButton done fail, error: %{public}d", error);
215         return -1;
216     }
217     return reply.ReadInt32();
218 }
219 
ReleaseButton(const RawAddress &device, int32_t button)220 int32_t BluetoothAvrcpCtProxy::ReleaseButton(const RawAddress &device, int32_t button)
221 {
222     MessageParcel data;
223     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
224         HILOGE("[ReleaseButton] fail: write interface token failed.");
225         return -1;
226     }
227 
228     if (!data.WriteString(device.GetAddress())) {
229         HILOGE("[ReleaseButton] fail: write result failed");
230         return -1;
231     }
232 
233     if (!data.WriteInt32(button)) {
234         HILOGE("[ReleaseButton] fail: write result failed");
235         return -1;
236     }
237 
238     MessageParcel reply;
239     MessageOption option = {MessageOption::TF_SYNC};
240     int error = InnerTransact(
241         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_RELEASE_BUTTON, option, data, reply);
242     if (error != NO_ERROR) {
243         HILOGE("BluetoothAvrcpCtProxy::ReleaseButton done fail, error: %{public}d", error);
244         return -1;
245     }
246     return reply.ReadInt32();
247 }
248 
GetUnitInfo(const RawAddress &device)249 int32_t BluetoothAvrcpCtProxy::GetUnitInfo(const RawAddress &device)
250 {
251     MessageParcel data;
252     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
253         HILOGE("[GetUnitInfo] fail: write interface token failed.");
254         return -1;
255     }
256 
257     if (!data.WriteString(device.GetAddress())) {
258         HILOGE("[GetUnitInfo] fail: write result failed");
259         return -1;
260     }
261 
262     MessageParcel reply;
263     MessageOption option = {MessageOption::TF_SYNC};
264     int error = InnerTransact(
265         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_UNIT_INFO, option, data, reply);
266     if (error != NO_ERROR) {
267         HILOGE("BluetoothAvrcpCtProxy::GetUnitInfo done fail, error: %{public}d", error);
268         return -1;
269     }
270     return reply.ReadInt32();
271 }
272 
GetSubUnitInfo(const RawAddress &device)273 int32_t BluetoothAvrcpCtProxy::GetSubUnitInfo(const RawAddress &device)
274 {
275     MessageParcel data;
276     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
277         HILOGE("[GetSubUnitInfo] fail: write interface token failed.");
278         return -1;
279     }
280 
281     if (!data.WriteString(device.GetAddress())) {
282         HILOGE("[GetSubUnitInfo] fail: write result failed");
283         return -1;
284     }
285 
286     MessageParcel reply;
287     MessageOption option = {MessageOption::TF_SYNC};
288     int error = InnerTransact(
289         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_SUB_UNIT_INFO, option, data, reply);
290     if (error != NO_ERROR) {
291         HILOGE("BluetoothAvrcpCtProxy::GetSubUnitInfo done fail, error: %{public}d", error);
292         return -1;
293     }
294     return reply.ReadInt32();
295 }
296 
GetSupportedCompanies(const RawAddress &device)297 int32_t BluetoothAvrcpCtProxy::GetSupportedCompanies(const RawAddress &device)
298 {
299     MessageParcel data;
300     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
301         HILOGE("[GetSupportedCompanies] fail: write interface token failed.");
302         return -1;
303     }
304 
305     if (!data.WriteString(device.GetAddress())) {
306         HILOGE("[GetSupportedCompanies] fail: write result failed");
307         return -1;
308     }
309 
310     MessageParcel reply;
311     MessageOption option = {MessageOption::TF_SYNC};
312     int error = InnerTransact(
313         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_SUPPORTED_COMPANIES, option, data, reply);
314     if (error != NO_ERROR) {
315         HILOGE("BluetoothAvrcpCtProxy::GetSupportedCompanies done fail, error: %{public}d", error);
316         return -1;
317     }
318     return reply.ReadInt32();
319 }
320 
GetSupportedEvents(const RawAddress &device)321 int32_t BluetoothAvrcpCtProxy::GetSupportedEvents(const RawAddress &device)
322 {
323     MessageParcel data;
324     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
325         HILOGE("[GetSupportedEvents] fail: write interface token failed.");
326         return -1;
327     }
328 
329     if (!data.WriteString(device.GetAddress())) {
330         HILOGE("[GetSupportedEvents] fail: write result failed");
331         return -1;
332     }
333 
334     MessageParcel reply;
335     MessageOption option = {MessageOption::TF_SYNC};
336     int error = InnerTransact(
337         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_SUPPORTED_EVENTS, option, data, reply);
338     if (error != NO_ERROR) {
339         HILOGE("BluetoothAvrcpCtProxy::GetSupportedEvents done fail, error: %{public}d", error);
340         return -1;
341     }
342     return reply.ReadInt32();
343 }
344 
GetPlayerAppSettingAttributes(const RawAddress &device)345 int32_t BluetoothAvrcpCtProxy::GetPlayerAppSettingAttributes(const RawAddress &device)
346 {
347     MessageParcel data;
348     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
349         HILOGE("[GetPlayerAppSettingAttributes] fail: write interface token failed.");
350         return -1;
351     }
352 
353     if (!data.WriteString(device.GetAddress())) {
354         HILOGE("[GetPlayerAppSettingAttributes] fail: write result failed");
355         return -1;
356     }
357 
358     MessageParcel reply;
359     MessageOption option = {MessageOption::TF_SYNC};
360     int error = InnerTransact(
361         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_PLAYER_APP_SETTING_ATTRIBUTES, option, data, reply);
362     if (error != NO_ERROR) {
363         HILOGE("BluetoothAvrcpCtProxy::GetPlayerAppSettingAttributes done fail, error: %{public}d", error);
364         return -1;
365     }
366     return reply.ReadInt32();
367 }
368 
GetPlayerAppSettingValues( const RawAddress &device, int32_t attribute)369 int32_t BluetoothAvrcpCtProxy::GetPlayerAppSettingValues(
370     const RawAddress &device, int32_t attribute)
371 {
372     MessageParcel data;
373     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
374         HILOGE("[GetPlayerAppSettingValues] fail: write interface token failed.");
375         return -1;
376     }
377 
378     if (!data.WriteString(device.GetAddress())) {
379         HILOGE("[GetPlayerAppSettingValues] fail: write result failed");
380         return -1;
381     }
382 
383     if (!data.WriteInt32(attribute)) {
384         HILOGE("[GetPlayerAppSettingValues] fail: write result failed");
385         return -1;
386     }
387 
388     MessageParcel reply;
389     MessageOption option = {MessageOption::TF_SYNC};
390     int error = InnerTransact(
391         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_PLAYER_APP_SETTING_VALUES, option, data, reply);
392     if (error != NO_ERROR) {
393         HILOGE("BluetoothAvrcpCtProxy::GetPlayerAppSettingValues done fail, error: %{public}d", error);
394         return -1;
395     }
396     return reply.ReadInt32();
397 }
398 
GetPlayerAppSettingCurrentValue(const RawAddress &device, const std::vector<int32_t> &attributes)399 int32_t BluetoothAvrcpCtProxy::GetPlayerAppSettingCurrentValue(const RawAddress &device,
400     const std::vector<int32_t> &attributes)
401 {
402     MessageParcel data;
403     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
404         HILOGE("[GetPlayerAppSettingCurrentValue] fail: write interface token failed.");
405         return -1;
406     }
407 
408     if (!data.WriteString(device.GetAddress())) {
409         HILOGE("[GetPlayerAppSettingCurrentValue] fail: write result failed");
410         return -1;
411     }
412 
413     if (!WriteParcelableInt32Vector(attributes, data)) {
414         HILOGE("[GetPlayerAppSettingCurrentValue] fail: write result failed");
415         return -1;
416     }
417 
418     MessageParcel reply;
419     MessageOption option = {MessageOption::TF_SYNC};
420     int error = InnerTransact(
421         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_PLAYER_APP_SETTING_CURRENT_VALUE, option, data, reply);
422     if (error != NO_ERROR) {
423         HILOGE("BluetoothAvrcpCtProxy::GetPlayerAppSettingCurrentValue done fail, error: %{public}d", error);
424         return -1;
425     }
426     return reply.ReadInt32();
427 }
428 
SetPlayerAppSettingCurrentValue(const RawAddress &device, const std::vector<int32_t> &attributes, const std::vector<int32_t> &values)429 int32_t BluetoothAvrcpCtProxy::SetPlayerAppSettingCurrentValue(const RawAddress &device,
430     const std::vector<int32_t> &attributes, const std::vector<int32_t> &values)
431 {
432     MessageParcel data;
433     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
434         HILOGE("[SetPlayerAppSettingCurrentValue] fail: write interface token failed.");
435         return -1;
436     }
437 
438     if (!data.WriteString(device.GetAddress())) {
439         HILOGE("[SetPlayerAppSettingCurrentValue] fail: write result failed");
440         return -1;
441     }
442 
443     if (!WriteParcelableInt32Vector(attributes, data)) {
444         HILOGE("[SetPlayerAppSettingCurrentValue] fail: write result failed");
445         return -1;
446     }
447 
448     if (!WriteParcelableInt32Vector(values, data)) {
449         HILOGE("[SetPlayerAppSettingCurrentValue] fail: write result failed");
450         return -1;
451     }
452 
453     MessageParcel reply;
454     MessageOption option = {MessageOption::TF_SYNC};
455     int error = InnerTransact(
456         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_SET_PLAYER_APP_SETTING_CURRENT_VALUE, option, data, reply);
457     if (error != NO_ERROR) {
458         HILOGE("BluetoothAvrcpCtProxy::SetPlayerAppSettingCurrentValue done fail, error: %{public}d", error);
459         return -1;
460     }
461     return reply.ReadInt32();
462 }
463 
GetPlayerAppSettingAttributeText(const RawAddress &device, const std::vector<int32_t> &attributes)464 int32_t BluetoothAvrcpCtProxy::GetPlayerAppSettingAttributeText(const RawAddress &device,
465     const std::vector<int32_t> &attributes)
466 {
467     MessageParcel data;
468     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
469         HILOGE("[GetPlayerAppSettingAttributeText] fail: write interface token failed.");
470         return -1;
471     }
472 
473     if (!data.WriteString(device.GetAddress())) {
474         HILOGE("[GetPlayerAppSettingAttributeText] fail: write result failed");
475         return -1;
476     }
477 
478     if (!WriteParcelableInt32Vector(attributes, data)) {
479         HILOGE("[GetPlayerAppSettingAttributeText] fail: write result failed");
480         return -1;
481     }
482 
483     MessageParcel reply;
484     MessageOption option = {MessageOption::TF_SYNC};
485     int error = InnerTransact(
486         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_PLAYER_APP_SETTING_ATTRIBUTE_TEXT, option, data, reply);
487     if (error != NO_ERROR) {
488         HILOGE("BluetoothAvrcpCtProxy::GetPlayerAppSettingAttributeText done fail, error: %{public}d", error);
489         return -1;
490     }
491     return reply.ReadInt32();
492 }
493 
GetPlayerAppSettingValueText(const RawAddress &device, int32_t attributes, const std::vector<int32_t> &values)494 int32_t BluetoothAvrcpCtProxy::GetPlayerAppSettingValueText(const RawAddress &device, int32_t attributes,
495     const std::vector<int32_t> &values)
496 {
497     MessageParcel data;
498     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
499         HILOGE("[GetPlayerAppSettingValueText] fail: write interface token failed.");
500         return -1;
501     }
502 
503     if (!data.WriteString(device.GetAddress())) {
504         HILOGE("[GetPlayerAppSettingValueText] fail: write result failed");
505         return -1;
506     }
507 
508     if (!data.WriteInt32(attributes)) {
509         HILOGE("[GetPlayerAppSettingValueText] fail: write result failed");
510         return -1;
511     }
512 
513     if (!WriteParcelableInt32Vector(values, data)) {
514         HILOGE("[GetPlayerAppSettingValueText] fail: write result failed");
515         return -1;
516     }
517 
518     MessageParcel reply;
519     MessageOption option = {MessageOption::TF_SYNC};
520     int error = InnerTransact(
521         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_PLAYER_APP_SETTING_VALUES_TEXT, option, data, reply);
522     if (error != NO_ERROR) {
523         HILOGE("BluetoothAvrcpCtProxy::GetPlayerAppSettingValueText done fail, error: %{public}d", error);
524         return -1;
525     }
526     return reply.ReadInt32();
527 }
528 
GetElementAttributes(const RawAddress &device, const std::vector<int32_t> &attributes)529 int32_t BluetoothAvrcpCtProxy::GetElementAttributes(const RawAddress &device,
530     const std::vector<int32_t> &attributes)
531 {
532     MessageParcel data;
533     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
534         HILOGE("[GetElementAttributes] fail: write interface token failed.");
535         return -1;
536     }
537 
538     if (!data.WriteString(device.GetAddress())) {
539         HILOGE("[GetElementAttributes] fail: write result failed");
540         return -1;
541     }
542 
543     if (!WriteParcelableInt32Vector(attributes, data)) {
544         HILOGE("[GetElementAttributes] fail: write result failed");
545         return -1;
546     }
547 
548     MessageParcel reply;
549     MessageOption option = {MessageOption::TF_SYNC};
550     int error = InnerTransact(
551         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_ELEMENT_ATTRIBUTES, option, data, reply);
552     if (error != NO_ERROR) {
553         HILOGE("BluetoothAvrcpCtProxy::GetElementAttributes done fail, error: %{public}d", error);
554         return -1;
555     }
556     return reply.ReadInt32();
557 }
558 
GetPlayStatus(const RawAddress &device)559 int32_t BluetoothAvrcpCtProxy::GetPlayStatus(const RawAddress &device)
560 {
561     MessageParcel data;
562     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
563         HILOGE("[GetPlayStatus] fail: write interface token failed.");
564         return -1;
565     }
566 
567     if (!data.WriteString(device.GetAddress())) {
568         HILOGE("[GetPlayStatus] fail: write result failed");
569         return -1;
570     }
571 
572     MessageParcel reply;
573     MessageOption option = {MessageOption::TF_SYNC};
574     int error = InnerTransact(
575         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_PLAYER_STATUS, option, data, reply);
576     if (error != NO_ERROR) {
577         HILOGE("BluetoothAvrcpCtProxy::GetPlayStatus done fail, error: %{public}d", error);
578         return -1;
579     }
580     return reply.ReadInt32();
581 }
582 
PlayItem(const RawAddress &device, int32_t scope, int64_t uid, int32_t uidCounter)583 int32_t BluetoothAvrcpCtProxy::PlayItem(const RawAddress &device, int32_t scope, int64_t uid,
584     int32_t uidCounter)
585 {
586     MessageParcel data;
587     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
588         HILOGE("[PlayItem] fail: write interface token failed.");
589         return -1;
590     }
591 
592     if (!data.WriteString(device.GetAddress())) {
593         HILOGE("[PlayItem] fail: write result failed");
594         return -1;
595     }
596 
597     if (!data.WriteInt32(scope)) {
598         HILOGE("[PlayItem] fail: write result failed");
599         return -1;
600     }
601 
602     if (!data.WriteInt64(uid)) {
603         HILOGE("[PlayItem] fail: write result failed");
604         return -1;
605     }
606 
607     if (!data.WriteInt32(uidCounter)) {
608         HILOGE("[PlayItem] fail: write result failed");
609         return -1;
610     }
611 
612     MessageParcel reply;
613     MessageOption option = {MessageOption::TF_SYNC};
614     int error = InnerTransact(
615         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_PLAY_ITEM, option, data, reply);
616     if (error != NO_ERROR) {
617         HILOGE("BluetoothAvrcpCtProxy::PlayItem done fail, error: %{public}d", error);
618         return -1;
619     }
620     return reply.ReadInt32();
621 }
622 
GetFolderItems(const RawAddress &device, int32_t startItem, int32_t endItem, const std::vector<int32_t> &attributes)623 int32_t BluetoothAvrcpCtProxy::GetFolderItems(const RawAddress &device, int32_t startItem, int32_t endItem,
624     const std::vector<int32_t> &attributes)
625 {
626     MessageParcel data;
627     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
628         HILOGE("[GetFolderItems] fail: write interface token failed.");
629         return -1;
630     }
631 
632     if (!data.WriteString(device.GetAddress())) {
633         HILOGE("[GetFolderItems] fail: write result failed");
634         return -1;
635     }
636 
637     if (!data.WriteInt32(startItem)) {
638         HILOGE("[GetFolderItems] fail: write result failed");
639         return -1;
640     }
641 
642     if (!data.WriteInt32(endItem)) {
643         HILOGE("[GetFolderItems] fail: write result failed");
644         return -1;
645     }
646 
647     if (!WriteParcelableInt32Vector(attributes, data)) {
648         HILOGE("[GetFolderItems] fail: write result failed");
649         return -1;
650     }
651 
652     MessageParcel reply;
653     MessageOption option = {MessageOption::TF_SYNC};
654     int error = InnerTransact(
655         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_FOLDER_ITEMS, option, data, reply);
656     if (error != NO_ERROR) {
657         HILOGE("BluetoothAvrcpCtProxy::GetFolderItems done fail, error: %{public}d", error);
658         return -1;
659     }
660     return reply.ReadInt32();
661 }
662 
GetTotalNumberOfItems(const RawAddress &device, int32_t scope)663 int32_t BluetoothAvrcpCtProxy::GetTotalNumberOfItems(const RawAddress &device, int32_t scope)
664 {
665     MessageParcel data;
666     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
667         HILOGE("[GetTotalNumberOfItems] fail: write interface token failed.");
668         return -1;
669     }
670 
671     if (!data.WriteString(device.GetAddress())) {
672         HILOGE("[GetTotalNumberOfItems] fail: write result failed");
673         return -1;
674     }
675 
676     if (!data.WriteInt32(scope)) {
677         HILOGE("[GetTotalNumberOfItems] fail: write result failed");
678         return -1;
679     }
680 
681     MessageParcel reply;
682     MessageOption option = {MessageOption::TF_SYNC};
683     int error = InnerTransact(
684         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_TOTAL_NUMBER_OF_ITEMS, option, data, reply);
685     if (error != NO_ERROR) {
686         HILOGE("BluetoothAvrcpCtProxy::GetTotalNumberOfItems done fail, error: %{public}d", error);
687         return -1;
688     }
689     return reply.ReadInt32();
690 }
691 
SetAbsoluteVolume(const RawAddress &device, int32_t volume)692 int32_t BluetoothAvrcpCtProxy::SetAbsoluteVolume(const RawAddress &device, int32_t volume)
693 {
694     MessageParcel data;
695     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
696         HILOGE("[SetAbsoluteVolume] fail: write interface token failed.");
697         return -1;
698     }
699 
700     if (!data.WriteString(device.GetAddress())) {
701         HILOGE("[SetAbsoluteVolume] fail: write result failed");
702         return -1;
703     }
704 
705     if (!data.WriteInt32(volume)) {
706         HILOGE("[SetAbsoluteVolume] fail: write result failed");
707         return -1;
708     }
709 
710     MessageParcel reply;
711     MessageOption option = {MessageOption::TF_SYNC};
712     int error = InnerTransact(
713         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_SET_ABSOLUTE_VOLUME, option, data, reply);
714     if (error != NO_ERROR) {
715         HILOGE("BluetoothAvrcpCtProxy::SetAbsoluteVolume done fail, error: %{public}d", error);
716         return -1;
717     }
718     return reply.ReadInt32();
719 }
720 
EnableNotification(const RawAddress &device, const std::vector<int32_t> &events, int32_t interval)721 int32_t BluetoothAvrcpCtProxy::EnableNotification(const RawAddress &device,
722     const std::vector<int32_t> &events, int32_t interval)
723 {
724     MessageParcel data;
725     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
726         HILOGE("[EnableNotification] fail: write interface token failed.");
727         return -1;
728     }
729 
730     if (!data.WriteString(device.GetAddress())) {
731         HILOGE("[EnableNotification] fail: write result failed");
732         return -1;
733     }
734 
735     if (!WriteParcelableInt32Vector(events, data)) {
736         HILOGE("[EnableNotification] fail: write result failed");
737         return -1;
738     }
739 
740     if (!data.WriteInt32(interval)) {
741         HILOGE("[EnableNotification] fail: write result failed");
742         return -1;
743     }
744 
745     MessageParcel reply;
746     MessageOption option = {MessageOption::TF_SYNC};
747     int error = InnerTransact(
748         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_ENABLE_NOTIFICATION, option, data, reply);
749     if (error != NO_ERROR) {
750         HILOGE("BluetoothAvrcpCtProxy::EnableNotification done fail, error: %{public}d", error);
751         return -1;
752     }
753     return reply.ReadInt32();
754 }
755 
DisableNotification(const RawAddress &device, const std::vector<int32_t> &events)756 int32_t BluetoothAvrcpCtProxy::DisableNotification(const RawAddress &device,
757     const std::vector<int32_t> &events)
758 {
759     MessageParcel data;
760     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
761         HILOGE("[DisableNotification] fail: write interface token failed.");
762         return -1;
763     }
764 
765     if (!data.WriteString(device.GetAddress())) {
766         HILOGE("[DisableNotification] fail: write result failed");
767         return -1;
768     }
769 
770     if (!WriteParcelableInt32Vector(events, data)) {
771         HILOGE("[DisableNotification] fail: write result failed");
772         return -1;
773     }
774 
775     MessageParcel reply;
776     MessageOption option = {MessageOption::TF_SYNC};
777     int error = InnerTransact(
778         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_DISABLE_NOTIFICATION, option, data, reply);
779     if (error != NO_ERROR) {
780         HILOGE("BluetoothAvrcpCtProxy::DisableNotification done fail, error: %{public}d", error);
781         return -1;
782     }
783     return reply.ReadInt32();
784 }
785 
GetItemAttributes(const RawAddress &device, int64_t uid, int32_t uidCounter, const std::vector<int32_t> &attributes)786 int32_t BluetoothAvrcpCtProxy::GetItemAttributes(const RawAddress &device, int64_t uid, int32_t uidCounter,
787     const std::vector<int32_t> &attributes)
788 {
789     MessageParcel data;
790     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
791         HILOGE("[DisableNotification] fail: write interface token failed.");
792         return -1;
793     }
794 
795     if (!data.WriteString(device.GetAddress())) {
796         HILOGE("[DisableNotification] fail: write result failed");
797         return -1;
798     }
799 
800     if (!data.WriteInt64(uid)) {
801         HILOGE("[DisableNotification] fail: write result failed");
802         return -1;
803     }
804 
805     if (!data.WriteInt32(uidCounter)) {
806         HILOGE("[DisableNotification] fail: write result failed");
807         return -1;
808     }
809 
810     if (!WriteParcelableInt32Vector(attributes, data)) {
811         HILOGE("[DisableNotification] fail: write result failed");
812         return -1;
813     }
814 
815     MessageParcel reply;
816     MessageOption option = {MessageOption::TF_SYNC};
817     int error = InnerTransact(
818         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_GET_ITEM_ATTRIBUTES, option, data, reply);
819     if (error != NO_ERROR) {
820         HILOGE("BluetoothAvrcpCtProxy::DisableNotification done fail, error: %{public}d", error);
821         return -1;
822     }
823     return reply.ReadInt32();
824 }
825 
SetBrowsedPlayer(const RawAddress &device, int32_t playerId)826 int32_t BluetoothAvrcpCtProxy::SetBrowsedPlayer(const RawAddress &device, int32_t playerId)
827 {
828     MessageParcel data;
829     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
830         HILOGE("[SetBrowsedPlayer] fail: write interface token failed.");
831         return -1;
832     }
833 
834     if (!data.WriteString(device.GetAddress())) {
835         HILOGE("[SetBrowsedPlayer] fail: write result failed");
836         return -1;
837     }
838 
839     if (!data.WriteInt32(playerId)) {
840         HILOGE("[SetBrowsedPlayer] fail: write result failed");
841         return -1;
842     }
843 
844     MessageParcel reply;
845     MessageOption option = {MessageOption::TF_SYNC};
846     int error = InnerTransact(
847         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_SET_BROWSERED_PLAYER, option, data, reply);
848     if (error != NO_ERROR) {
849         HILOGE("BluetoothAvrcpCtProxy::SetBrowsedPlayer done fail, error: %{public}d", error);
850         return -1;
851     }
852     return reply.ReadInt32();
853 }
854 
GetMeidaPlayerList( const RawAddress &device, int32_t startItem, int32_t endItem)855 int32_t BluetoothAvrcpCtProxy::GetMeidaPlayerList(
856     const RawAddress &device, int32_t startItem, int32_t endItem)
857 {
858     MessageParcel data;
859     if (!data.WriteInterfaceToken(BluetoothAvrcpCtProxy::GetDescriptor())) {
860         HILOGE("[GetMeidaPlayerList] fail: write interface token failed.");
861         return -1;
862     }
863 
864     if (!data.WriteString(device.GetAddress())) {
865         HILOGE("[GetMeidaPlayerList] fail: write result failed");
866         return -1;
867     }
868 
869     if (!data.WriteInt32(startItem)) {
870         HILOGE("[GetMeidaPlayerList] fail: write result failed");
871         return -1;
872     }
873 
874     if (!data.WriteInt32(endItem)) {
875         HILOGE("[GetMeidaPlayerList] fail: write result failed");
876         return -1;
877     }
878 
879     MessageParcel reply;
880     MessageOption option = {MessageOption::TF_SYNC};
881     int error = InnerTransact(
882         BluetoothAvrcpCtInterfaceCode::AVRCP_CT_MEDIA_PLAYER_LIST, option, data, reply);
883     if (error != NO_ERROR) {
884         HILOGE("BluetoothAvrcpCtProxy::GetMeidaPlayerList done fail, error: %{public}d", error);
885         return -1;
886     }
887     return reply.ReadInt32();
888 }
889 
WriteParcelableInt32Vector( const std::vector<int32_t> &parcelableVector, Parcel &reply)890 bool BluetoothAvrcpCtProxy::WriteParcelableInt32Vector(
891     const std::vector<int32_t> &parcelableVector, Parcel &reply)
892 {
893     if (!reply.WriteInt32(parcelableVector.size())) {
894         HILOGE("write ParcelableVector failed");
895         return false;
896     }
897 
898     for (auto parcelable : parcelableVector) {
899         if (!reply.WriteInt32(parcelable)) {
900             HILOGE("write ParcelableVector failed");
901             return false;
902         }
903     }
904     return true;
905 }
906 
InnerTransact( BluetoothAvrcpCtInterfaceCode interfaceCode, MessageOption &flags, MessageParcel &data, MessageParcel &reply)907 ErrCode BluetoothAvrcpCtProxy::InnerTransact(
908     BluetoothAvrcpCtInterfaceCode interfaceCode, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
909 {
910     uint32_t code = static_cast<uint32_t>(interfaceCode);
911     auto remote = Remote();
912     if (remote == nullptr) {
913         HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
914         return OBJECT_NULL;
915     }
916     int err = remote->SendRequest(code, data, reply, flags);
917     switch (err) {
918         case NO_ERROR: {
919             return NO_ERROR;
920         }
921         case DEAD_OBJECT: {
922             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
923             return DEAD_OBJECT;
924         }
925         default: {
926             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
927             return TRANSACTION_ERR;
928         }
929     }
930 }
931 }  // namespace Bluetooth
932 }  // namespace OHOS
933