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