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