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_tg_proxy"
17 #endif
18 
19 #include "bluetooth_avrcp_tg_proxy.h"
20 #include "bluetooth_log.h"
21 #include "bluetooth_errorcode.h"
22 
23 namespace OHOS {
24 namespace Bluetooth {
RegisterObserver(const sptr<IBluetoothAvrcpTgObserver> & observer)25 void BluetoothAvrcpTgProxy::RegisterObserver(const sptr<IBluetoothAvrcpTgObserver> &observer)
26 {
27     HILOGI("BluetoothAvrcpTgProxy::RegisterObserver start");
28     MessageParcel data;
29     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
30         HILOGE("BluetoothAvrcpTgProxy::RegisterObserver WriteInterfaceToken error");
31         return;
32     }
33     if (!data.WriteRemoteObject(observer->AsObject())) {
34         HILOGE("BluetoothAvrcpTgProxy::RegisterObserver observer error");
35         return;
36     }
37 
38     MessageParcel reply;
39     MessageOption option = { MessageOption::TF_ASYNC };
40     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_REGISTER_OBSERVER, option, data, reply);
41     if (error != NO_ERROR) {
42         HILOGE("BluetoothAvrcpTgProxy::RegisterObserver done fail, error: %{public}d", error);
43         return;
44     }
45     return;
46 }
47 
UnregisterObserver(const sptr<IBluetoothAvrcpTgObserver> & observer)48 void BluetoothAvrcpTgProxy::UnregisterObserver(const sptr<IBluetoothAvrcpTgObserver> &observer)
49 {
50     HILOGI("BluetoothAvrcpTgProxy::UnregisterObserver start");
51     MessageParcel data;
52     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
53         HILOGE("BluetoothAvrcpTgProxy::UnregisterObserver WriteInterfaceToken error");
54         return;
55     }
56     if (!data.WriteRemoteObject(observer->AsObject())) {
57         HILOGE("BluetoothAvrcpTgProxy::UnregisterObserver observer error");
58         return;
59     }
60 
61     MessageParcel reply;
62     MessageOption option = { MessageOption::TF_ASYNC };
63     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_UNREGISTER_OBSERVER, option, data, reply);
64     if (error != NO_ERROR) {
65         HILOGE("BluetoothAvrcpTgProxy::RegisterObserver done fail, error: %{public}d", error);
66         return;
67     }
68     return;
69 }
70 
SetActiveDevice(const BluetoothRawAddress & addr)71 void BluetoothAvrcpTgProxy::SetActiveDevice(const BluetoothRawAddress &addr)
72 {
73     MessageParcel data;
74     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
75         HILOGE("[SetActiveDevice] fail: write interface token failed.");
76         return;
77     }
78 
79     if (!data.WriteParcelable(&addr)) {
80         HILOGE("[SetActiveDevice] fail: write result failed");
81         return;
82     }
83 
84     MessageParcel reply;
85     MessageOption option = {MessageOption::TF_SYNC};
86     int error = InnerTransact(
87         BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_SET_ACTIVE_DEVICE, option, data, reply);
88     if (error != NO_ERROR) {
89         HILOGE("BluetoothAvrcpCtProxy::SetActiveDevice done fail, error: %{public}d", error);
90         return;
91     }
92 }
93 
Connect(const BluetoothRawAddress & addr)94 int32_t BluetoothAvrcpTgProxy::Connect(const BluetoothRawAddress &addr)
95 {
96     HILOGI("BluetoothAvrcpTgProxy::Connect start");
97     MessageParcel data;
98     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
99         HILOGE("BluetoothAvrcpTgProxy::Connect WriteInterfaceToken error");
100         return -1;
101     }
102     if (!data.WriteParcelable(&addr)) {
103         HILOGE("BluetoothAvrcpTgProxy::Connect transport error");
104         return -1;
105     }
106 
107     MessageParcel reply;
108     MessageOption option = { MessageOption::TF_SYNC };
109     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_CONNECT, option, data, reply);
110     if (error != NO_ERROR) {
111         HILOGE("BluetoothAvrcpTgProxy::Connect done fail, error: %{public}d", error);
112         return -1;
113     }
114     return reply.ReadInt32();
115 }
116 
Disconnect(const BluetoothRawAddress & addr)117 int32_t BluetoothAvrcpTgProxy::Disconnect(const BluetoothRawAddress &addr)
118 {
119     HILOGI("BluetoothAvrcpTgProxy::Disconnect start");
120     MessageParcel data;
121     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
122         HILOGE("BluetoothAvrcpTgProxy::Disconnect WriteInterfaceToken error");
123         return -1;
124     }
125     if (!data.WriteParcelable(&addr)) {
126         HILOGE("BluetoothAvrcpTgProxy::Disconnect transport error");
127         return -1;
128     }
129 
130     MessageParcel reply;
131     MessageOption option = { MessageOption::TF_SYNC };
132     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_DISCONNECT, option, data, reply);
133     if (error != NO_ERROR) {
134         HILOGE("BluetoothAvrcpTgProxy::Disconnect done fail, error: %{public}d", error);
135         return -1;
136     }
137     return reply.ReadInt32();
138 }
139 
GetConnectedDevices()140 std::vector<BluetoothRawAddress> BluetoothAvrcpTgProxy::GetConnectedDevices()
141 {
142     std::vector<BluetoothRawAddress> vec;
143     MessageParcel data;
144     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
145         HILOGE("BluetoothAvrcpTgProxy::GetConnectedDevices WriteInterfaceToken error");
146         return vec;
147     }
148 
149     MessageParcel reply;
150     MessageOption option = {MessageOption::TF_SYNC};
151     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_GET_CONNECTED_DEVICES, option, data, reply);
152     if (error != NO_ERROR) {
153         HILOGE("BluetoothAvrcpTgProxy::GetConnectedDevices done fail, error: %{public}d", error);
154         return vec;
155     }
156     int32_t size = reply.ReadInt32();
157     const int32_t maxSize = 100;
158     if (size > maxSize) {
159         return vec;
160     }
161     for (int32_t i = 0; i < size; i++) {
162         std::shared_ptr<BluetoothRawAddress> rawAddress(reply.ReadParcelable<BluetoothRawAddress>());
163         if (!rawAddress) {
164             return vec;
165         }
166         vec.push_back(*rawAddress);
167     }
168     return vec;
169 }
170 
GetDevicesByStates(const std::vector<int32_t> & states)171 std::vector<BluetoothRawAddress> BluetoothAvrcpTgProxy::GetDevicesByStates(const std::vector<int32_t> &states)
172 {
173     std::vector<BluetoothRawAddress> vec;
174     MessageParcel data;
175     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
176         HILOGE("BluetoothAvrcpTgProxy::GetConnectedDevices WriteInterfaceToken error");
177         return vec;
178     }
179     int num = static_cast<int>(states.size());
180     if (!data.WriteInt32(num)) {
181         HILOGE("BluetoothAvrcpTgProxy::GetDevicesByStates num error");
182         return vec;
183     }
184     for (int i = 0; i < num; i++) {
185         if (!data.WriteInt32(states[i])) {
186             HILOGE("BluetoothAvrcpTgProxy::GetDevicesByStates states error");
187             return vec;
188         }
189     }
190 
191     MessageParcel reply;
192     MessageOption option = {MessageOption::TF_SYNC};
193     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_GET_DEVICES_BY_STATES, option, data, reply);
194     if (error != NO_ERROR) {
195         HILOGE("BluetoothAvrcpTgProxy::GetConnectedDevices done fail, error: %{public}d", error);
196         return vec;
197     }
198     int32_t size = reply.ReadInt32();
199     const int32_t maxSize = 100;
200     if (size > maxSize) {
201         return vec;
202     }
203     for (int32_t i = 0; i < size; i++) {
204         std::shared_ptr<BluetoothRawAddress> rawAddress(reply.ReadParcelable<BluetoothRawAddress>());
205         if (!rawAddress) {
206             return vec;
207         }
208         vec.push_back(*rawAddress);
209     }
210     return vec;
211 }
212 
GetDeviceState(const BluetoothRawAddress & addr)213 int32_t BluetoothAvrcpTgProxy::GetDeviceState(const BluetoothRawAddress &addr)
214 {
215     HILOGI("BluetoothAvrcpTgProxy::GetDeviceState start");
216     MessageParcel data;
217     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
218         HILOGE("BluetoothAvrcpTgProxy::GetDeviceState WriteInterfaceToken error");
219         return -1;
220     }
221     if (!data.WriteParcelable(&addr)) {
222         HILOGE("BluetoothAvrcpTgProxy::GetDeviceState transport error");
223         return -1;
224     }
225 
226     MessageParcel reply;
227     MessageOption option = { MessageOption::TF_SYNC };
228     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_GET_DEVICE_STATE, option, data, reply);
229     if (error != NO_ERROR) {
230         HILOGE("BluetoothAvrcpTgProxy::GetDeviceState done fail, error: %{public}d", error);
231         return -1;
232     }
233     return reply.ReadInt32();
234 }
235 
NotifyPlaybackStatusChanged(int32_t playStatus,int32_t playbackPos)236 void BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged(int32_t playStatus, int32_t playbackPos)
237 {
238     HILOGI("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged start");
239     MessageParcel data;
240     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
241         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged WriteInterfaceToken error");
242         return;
243     }
244     if (!data.WriteInt32(playStatus)) {
245         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged playStatus error");
246         return;
247     }
248     if (!data.WriteInt32(playbackPos)) {
249         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged playbackPos error");
250         return;
251     }
252 
253     MessageParcel reply;
254     MessageOption option = { MessageOption::TF_ASYNC };
255     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_PLAYBACK_STATUS_CHANGED,
256                               option, data, reply);
257     if (error != NO_ERROR) {
258         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged done fail, error: %{public}d", error);
259         return;
260     }
261     return;
262 }
263 
NotifyTrackChanged(int64_t uid,int32_t playbackPos)264 void BluetoothAvrcpTgProxy::NotifyTrackChanged(int64_t uid, int32_t playbackPos)
265 {
266     HILOGI("BluetoothAvrcpTgProxy::NotifyTrackChanged start");
267     MessageParcel data;
268     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
269         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackChanged WriteInterfaceToken error");
270         return;
271     }
272     if (!data.WriteInt64(uid)) {
273         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackChanged uid error");
274         return;
275     }
276     if (!data.WriteInt32(playbackPos)) {
277         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackChanged playbackPos error");
278         return;
279     }
280 
281     MessageParcel reply;
282     MessageOption option = { MessageOption::TF_ASYNC };
283     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_TRACK_CHANGED, option, data, reply);
284     if (error != NO_ERROR) {
285         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged done fail, error: %{public}d", error);
286         return;
287     }
288     return;
289 }
290 
NotifyTrackReachedEnd(int32_t playbackPos)291 void BluetoothAvrcpTgProxy::NotifyTrackReachedEnd(int32_t playbackPos)
292 {
293     HILOGI("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd start");
294     MessageParcel data;
295     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
296         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd WriteInterfaceToken error");
297         return;
298     }
299     if (!data.WriteInt32(playbackPos)) {
300         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd playbackPos error");
301         return;
302     }
303 
304     MessageParcel reply;
305     MessageOption option = { MessageOption::TF_ASYNC };
306     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_TRACK_REACHED_END,
307                               option, data, reply);
308     if (error != NO_ERROR) {
309         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd done fail, error: %{public}d", error);
310         return;
311     }
312     return;
313 }
314 
NotifyTrackReachedStart(int32_t playbackPos)315 void BluetoothAvrcpTgProxy::NotifyTrackReachedStart(int32_t playbackPos)
316 {
317     HILOGI("BluetoothAvrcpTgProxy::NotifyTrackReachedStart start");
318     MessageParcel data;
319     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
320         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedStart WriteInterfaceToken error");
321         return;
322     }
323     if (!data.WriteInt32(playbackPos)) {
324         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedStart playbackPos error");
325         return;
326     }
327 
328     MessageParcel reply;
329     MessageOption option = { MessageOption::TF_ASYNC };
330     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_TRACK_REACHED_START,
331                               option, data, reply);
332     if (error != NO_ERROR) {
333         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedStart done fail, error: %{public}d", error);
334         return;
335     }
336     return;
337 }
338 
NotifyPlaybackPosChanged(int32_t playbackPos)339 void BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged(int32_t playbackPos)
340 {
341     HILOGI("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged start");
342     MessageParcel data;
343     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
344         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged WriteInterfaceToken error");
345         return;
346     }
347     if (!data.WriteInt32(playbackPos)) {
348         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged playbackPos error");
349         return;
350     }
351 
352     MessageParcel reply;
353     MessageOption option = { MessageOption::TF_ASYNC };
354     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_PLAYBACK_POS_CHANGED,
355                               option, data, reply);
356     if (error != NO_ERROR) {
357         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged done fail, error: %{public}d", error);
358         return;
359     }
360     return;
361 }
362 
NotifyPlayerAppSettingChanged(const std::vector<int32_t> & attributes,const std::vector<int32_t> & values)363 void BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged(const std::vector<int32_t> &attributes,
364     const std::vector<int32_t> &values)
365 {
366     MessageParcel data;
367     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
368         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged WriteInterfaceToken error");
369         return;
370     }
371     int attributesNum = static_cast<int>(attributes.size());
372     if (!data.WriteInt32(attributesNum)) {
373         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged attributesNum error");
374         return;
375     }
376     for (int i = 0; i < attributesNum; i++) {
377         if (!data.WriteInt32(attributes[i])) {
378             HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged attributes error");
379             return;
380         }
381     }
382 
383     int valuesNum = static_cast<int>(values.size());
384     if (!data.WriteInt32(valuesNum)) {
385         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged valuesNum error");
386         return;
387     }
388     for (int i = 0; i < valuesNum; i++) {
389         if (!data.WriteInt32(values[i])) {
390             HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged values error");
391             return;
392         }
393     }
394 
395     MessageParcel reply;
396     MessageOption option = { MessageOption::TF_ASYNC };
397     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_PLAYER_APP_SETTING_CHANGED,
398                               option, data, reply);
399     if (error != NO_ERROR) {
400         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged done fail, error: %{public}d", error);
401         return;
402     }
403     return;
404 }
405 
NotifyNowPlayingContentChanged()406 void BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged()
407 {
408     HILOGI("BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged start");
409     MessageParcel data;
410     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
411         HILOGE("BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged WriteInterfaceToken error");
412         return;
413     }
414 
415     MessageParcel reply;
416     MessageOption option = { MessageOption::TF_ASYNC };
417     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_NOWPLAYING_CONTENT_CHANGED,
418                               option, data, reply);
419     if (error != NO_ERROR) {
420         HILOGE("BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged done fail, error: %{public}d", error);
421         return;
422     }
423     return;
424 }
425 
NotifyAvailablePlayersChanged()426 void BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged()
427 {
428     HILOGI("BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged start");
429     MessageParcel data;
430     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
431         HILOGE("BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged WriteInterfaceToken error");
432         return;
433     }
434 
435     MessageParcel reply;
436     MessageOption option = { MessageOption::TF_ASYNC };
437 
438     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_AVAILABLE_PLAYERS_CHANGED,
439                               option, data, reply);
440     if (error != NO_ERROR) {
441         HILOGE("BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged done fail, error: %{public}d", error);
442         return;
443     }
444     return;
445 }
446 
NotifyAddressedPlayerChanged(int32_t playerId,int32_t uidCounter)447 void BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged(int32_t playerId, int32_t uidCounter)
448 {
449     HILOGI("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged start");
450     MessageParcel data;
451     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
452         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged WriteInterfaceToken error");
453         return;
454     }
455     if (!data.WriteInt32(playerId)) {
456         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged playStatus error");
457         return;
458     }
459     if (!data.WriteInt32(uidCounter)) {
460         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged playbackPos error");
461         return;
462     }
463 
464     MessageParcel reply;
465     MessageOption option = { MessageOption::TF_ASYNC };
466     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_ADDRESSED_PLAYER_CHANGED,
467                               option, data, reply);
468     if (error != NO_ERROR) {
469         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged done fail, error: %{public}d", error);
470         return;
471     }
472     return;
473 }
474 
NotifyUidChanged(int32_t uidCounter)475 void BluetoothAvrcpTgProxy::NotifyUidChanged(int32_t uidCounter)
476 {
477     HILOGI("BluetoothAvrcpTgProxy::NotifyUidChanged start");
478     MessageParcel data;
479     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
480         HILOGE("BluetoothAvrcpTgProxy::NotifyUidChanged WriteInterfaceToken error");
481         return;
482     }
483     if (!data.WriteInt32(uidCounter)) {
484         HILOGE("BluetoothAvrcpTgProxy::NotifyUidChanged playStatus error");
485         return;
486     }
487 
488     MessageParcel reply;
489     MessageOption option = { MessageOption::TF_ASYNC };
490     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_UID_CHANGED, option, data, reply);
491     if (error != NO_ERROR) {
492         HILOGE("BluetoothAvrcpTgProxy::NotifyUidChanged done fail, error: %{public}d", error);
493         return;
494     }
495     return;
496 }
497 
NotifyVolumeChanged(int32_t volume)498 void BluetoothAvrcpTgProxy::NotifyVolumeChanged(int32_t volume)
499 {
500     HILOGI("BluetoothAvrcpTgProxy::NotifyVolumeChanged start");
501     MessageParcel data;
502     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
503         HILOGE("BluetoothAvrcpTgProxy::NotifyVolumeChanged WriteInterfaceToken error");
504         return;
505     }
506     if (!data.WriteInt32(volume)) {
507         HILOGE("BluetoothAvrcpTgProxy::NotifyVolumeChanged playStatus error");
508         return;
509     }
510 
511     MessageParcel reply;
512     MessageOption option = { MessageOption::TF_ASYNC };
513     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_VOLUME_CHANGED, option, data, reply);
514     if (error != NO_ERROR) {
515         HILOGE("BluetoothAvrcpTgProxy::NotifyVolumeChanged done fail, error: %{public}d", error);
516         return;
517     }
518     return;
519 }
520 
SetDeviceAbsoluteVolume(const BluetoothRawAddress & addr,int32_t volumeLevel)521 int32_t BluetoothAvrcpTgProxy::SetDeviceAbsoluteVolume(const BluetoothRawAddress &addr, int32_t volumeLevel)
522 {
523     MessageParcel data;
524     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
525         HILOGE("[SetDeviceAbsoluteVolume] WriteInterfaceToken error");
526         return BT_ERR_IPC_TRANS_FAILED;
527     }
528 
529     if (!data.WriteParcelable(&addr)) {
530         HILOGE("[SetDeviceAbsoluteVolume] WriteParcelable error");
531         return BT_ERR_IPC_TRANS_FAILED;
532     }
533 
534     if (!data.WriteInt32(volumeLevel)) {
535         HILOGE("[SetDeviceAbsoluteVolume] WriteInt32 error");
536         return BT_ERR_IPC_TRANS_FAILED;
537     }
538 
539     MessageParcel reply;
540     MessageOption option = {MessageOption::TF_SYNC};
541     int error =
542         InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_SET_DEVICE_ABSOLUTE_VOLUME, option, data, reply);
543     if (error != NO_ERROR) {
544         HILOGE("BluetoothAvrcpTgProxy::SetDeviceAbsoluteVolume done fail, error: %{public}d", error);
545         return BT_ERR_IPC_TRANS_FAILED;
546     }
547 
548     return reply.ReadInt32();
549 }
SetDeviceAbsVolumeAbility(const BluetoothRawAddress & addr,int32_t ability)550 int32_t BluetoothAvrcpTgProxy::SetDeviceAbsVolumeAbility(const BluetoothRawAddress &addr, int32_t ability)
551 {
552     MessageParcel data;
553     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
554         HILOGE("[SetDeviceAbsVolumeAbility] WriteInterfaceToken error");
555         return BT_ERR_IPC_TRANS_FAILED;
556     }
557 
558     if (!data.WriteParcelable(&addr)) {
559         HILOGE("[SetDeviceAbsVolumeAbility] WriteParcelable error");
560         return BT_ERR_IPC_TRANS_FAILED;
561     }
562 
563     if (!data.WriteInt32(ability)) {
564         HILOGE("[SetDeviceAbsVolumeAbility] WriteInt32 error");
565         return BT_ERR_IPC_TRANS_FAILED;
566     }
567 
568     MessageParcel reply;
569     MessageOption option = {MessageOption::TF_SYNC};
570     int error =
571         InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_SET_DEVICE_ABS_VOLUME_ABILITY, option, data, reply);
572     if (error != NO_ERROR) {
573         HILOGE("BluetoothAvrcpTgProxy::SetDeviceAbsVolumeAbility done fail, error: %{public}d", error);
574         return BT_ERR_IPC_TRANS_FAILED;
575     }
576 
577     return reply.ReadInt32();
578 }
GetDeviceAbsVolumeAbility(const BluetoothRawAddress & addr,int32_t & ability)579 int32_t BluetoothAvrcpTgProxy::GetDeviceAbsVolumeAbility(const BluetoothRawAddress &addr, int32_t &ability)
580 {
581     MessageParcel data;
582     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
583         HILOGE("[GetDeviceAbsVolumeAbility] WriteInterfaceToken error");
584         return BT_ERR_IPC_TRANS_FAILED;
585     }
586 
587     if (!data.WriteParcelable(&addr)) {
588         HILOGE("[GetDeviceAbsVolumeAbility] WriteParcelable error");
589         return BT_ERR_IPC_TRANS_FAILED;
590     }
591 
592     MessageParcel reply;
593     MessageOption option = {MessageOption::TF_SYNC};
594     int error =
595         InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_GET_DEVICE_ABS_VOLUME_ABILITY, option, data, reply);
596     if (error != NO_ERROR) {
597         HILOGE("BluetoothAvrcpTgProxy::GetDeviceAbsVolumeAbility done fail, error: %{public}d", error);
598         return BT_ERR_IPC_TRANS_FAILED;
599     }
600 
601     int32_t ret = reply.ReadInt32();
602     if (ret == BT_NO_ERROR) {
603         ability = reply.ReadInt32();
604     }
605 
606     return ret;
607 }
608 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)609 ErrCode BluetoothAvrcpTgProxy::InnerTransact(
610     uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
611 {
612     auto remote = Remote();
613     if (remote == nullptr) {
614         HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
615         return OBJECT_NULL;
616     }
617     int err = remote->SendRequest(code, data, reply, flags);
618     switch (err) {
619         case NO_ERROR: {
620             return NO_ERROR;
621         }
622         case DEAD_OBJECT: {
623             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
624             return DEAD_OBJECT;
625         }
626         default: {
627             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
628             return TRANSACTION_ERR;
629         }
630     }
631 }
632 }  // namespace Bluetooth
633 }  // namespace OHOS