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