• 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 
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         vec.push_back(*rawAddress);
195     }
196     return vec;
197 }
198 
GetDeviceState(const BluetoothRawAddress & addr)199 int32_t BluetoothAvrcpTgProxy::GetDeviceState(const BluetoothRawAddress &addr)
200 {
201     HILOGI("BluetoothAvrcpTgProxy::GetDeviceState start");
202     MessageParcel data;
203     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
204         HILOGE("BluetoothAvrcpTgProxy::GetDeviceState WriteInterfaceToken error");
205         return -1;
206     }
207     if (!data.WriteParcelable(&addr)) {
208         HILOGE("BluetoothAvrcpTgProxy::GetDeviceState transport error");
209         return -1;
210     }
211 
212     MessageParcel reply;
213     MessageOption option = { MessageOption::TF_SYNC };
214     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_GET_DEVICE_STATE, option, data, reply);
215     if (error != NO_ERROR) {
216         HILOGE("BluetoothAvrcpTgProxy::GetDeviceState done fail, error: %{public}d", error);
217         return -1;
218     }
219     return reply.ReadInt32();
220 }
221 
NotifyPlaybackStatusChanged(int32_t playStatus,int32_t playbackPos)222 void BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged(int32_t playStatus, int32_t playbackPos)
223 {
224     HILOGI("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged start");
225     MessageParcel data;
226     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
227         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged WriteInterfaceToken error");
228         return;
229     }
230     if (!data.WriteInt32(playStatus)) {
231         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged playStatus error");
232         return;
233     }
234     if (!data.WriteInt32(playbackPos)) {
235         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged playbackPos error");
236         return;
237     }
238 
239     MessageParcel reply;
240     MessageOption option = { MessageOption::TF_ASYNC };
241     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_PLAYBACK_STATUS_CHANGED,
242                               option, data, reply);
243     if (error != NO_ERROR) {
244         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged done fail, error: %{public}d", error);
245         return;
246     }
247     return;
248 }
249 
NotifyTrackChanged(int64_t uid,int32_t playbackPos)250 void BluetoothAvrcpTgProxy::NotifyTrackChanged(int64_t uid, int32_t playbackPos)
251 {
252     HILOGI("BluetoothAvrcpTgProxy::NotifyTrackChanged start");
253     MessageParcel data;
254     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
255         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackChanged WriteInterfaceToken error");
256         return;
257     }
258     if (!data.WriteInt64(uid)) {
259         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackChanged uid error");
260         return;
261     }
262     if (!data.WriteInt32(playbackPos)) {
263         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackChanged playbackPos error");
264         return;
265     }
266 
267     MessageParcel reply;
268     MessageOption option = { MessageOption::TF_ASYNC };
269     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_TRACK_CHANGED, option, data, reply);
270     if (error != NO_ERROR) {
271         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackStatusChanged done fail, error: %{public}d", error);
272         return;
273     }
274     return;
275 }
276 
NotifyTrackReachedEnd(int32_t playbackPos)277 void BluetoothAvrcpTgProxy::NotifyTrackReachedEnd(int32_t playbackPos)
278 {
279     HILOGI("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd start");
280     MessageParcel data;
281     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
282         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd WriteInterfaceToken error");
283         return;
284     }
285     if (!data.WriteInt32(playbackPos)) {
286         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd playbackPos error");
287         return;
288     }
289 
290     MessageParcel reply;
291     MessageOption option = { MessageOption::TF_ASYNC };
292     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_TRACK_REACHED_END,
293                               option, data, reply);
294     if (error != NO_ERROR) {
295         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedEnd done fail, error: %{public}d", error);
296         return;
297     }
298     return;
299 }
300 
NotifyTrackReachedStart(int32_t playbackPos)301 void BluetoothAvrcpTgProxy::NotifyTrackReachedStart(int32_t playbackPos)
302 {
303     HILOGI("BluetoothAvrcpTgProxy::NotifyTrackReachedStart start");
304     MessageParcel data;
305     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
306         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedStart WriteInterfaceToken error");
307         return;
308     }
309     if (!data.WriteInt32(playbackPos)) {
310         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedStart playbackPos error");
311         return;
312     }
313 
314     MessageParcel reply;
315     MessageOption option = { MessageOption::TF_ASYNC };
316     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_TRACK_REACHED_START,
317                               option, data, reply);
318     if (error != NO_ERROR) {
319         HILOGE("BluetoothAvrcpTgProxy::NotifyTrackReachedStart done fail, error: %{public}d", error);
320         return;
321     }
322     return;
323 }
324 
NotifyPlaybackPosChanged(int32_t playbackPos)325 void BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged(int32_t playbackPos)
326 {
327     HILOGI("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged start");
328     MessageParcel data;
329     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
330         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged WriteInterfaceToken error");
331         return;
332     }
333     if (!data.WriteInt32(playbackPos)) {
334         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged playbackPos error");
335         return;
336     }
337 
338     MessageParcel reply;
339     MessageOption option = { MessageOption::TF_ASYNC };
340     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_PLAYBACK_POS_CHANGED,
341                               option, data, reply);
342     if (error != NO_ERROR) {
343         HILOGE("BluetoothAvrcpTgProxy::NotifyPlaybackPosChanged done fail, error: %{public}d", error);
344         return;
345     }
346     return;
347 }
348 
NotifyPlayerAppSettingChanged(const std::vector<int32_t> & attributes,const std::vector<int32_t> & values)349 void BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged(const std::vector<int32_t> &attributes,
350     const std::vector<int32_t> &values)
351 {
352     MessageParcel data;
353     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
354         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged WriteInterfaceToken error");
355         return;
356     }
357     int attributesNum = static_cast<int>(attributes.size());
358     if (!data.WriteInt32(attributesNum)) {
359         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged attributesNum error");
360         return;
361     }
362     for (int i = 0; i < attributesNum; i++) {
363         if (!data.WriteInt32(attributes[i])) {
364             HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged attributes error");
365             return;
366         }
367     }
368 
369     int valuesNum = static_cast<int>(values.size());
370     if (!data.WriteInt32(valuesNum)) {
371         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged valuesNum error");
372         return;
373     }
374     for (int i = 0; i < valuesNum; i++) {
375         if (!data.WriteInt32(values[i])) {
376             HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged values error");
377             return;
378         }
379     }
380 
381     MessageParcel reply;
382     MessageOption option = { MessageOption::TF_ASYNC };
383     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_PLAYER_APP_SETTING_CHANGED,
384                               option, data, reply);
385     if (error != NO_ERROR) {
386         HILOGE("BluetoothAvrcpTgProxy::NotifyPlayerAppSettingChanged done fail, error: %{public}d", error);
387         return;
388     }
389     return;
390 }
391 
NotifyNowPlayingContentChanged()392 void BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged()
393 {
394     HILOGI("BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged start");
395     MessageParcel data;
396     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
397         HILOGE("BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged WriteInterfaceToken error");
398         return;
399     }
400 
401     MessageParcel reply;
402     MessageOption option = { MessageOption::TF_ASYNC };
403     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_NOWPLAYING_CONTENT_CHANGED,
404                               option, data, reply);
405     if (error != NO_ERROR) {
406         HILOGE("BluetoothAvrcpTgProxy::NotifyNowPlayingContentChanged done fail, error: %{public}d", error);
407         return;
408     }
409     return;
410 }
411 
NotifyAvailablePlayersChanged()412 void BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged()
413 {
414     HILOGI("BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged start");
415     MessageParcel data;
416     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
417         HILOGE("BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged WriteInterfaceToken error");
418         return;
419     }
420 
421     MessageParcel reply;
422     MessageOption option = { MessageOption::TF_ASYNC };
423 
424     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_AVAILABLE_PLAYERS_CHANGED,
425                               option, data, reply);
426     if (error != NO_ERROR) {
427         HILOGE("BluetoothAvrcpTgProxy::NotifyAvailablePlayersChanged done fail, error: %{public}d", error);
428         return;
429     }
430     return;
431 }
432 
NotifyAddressedPlayerChanged(int32_t playerId,int32_t uidCounter)433 void BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged(int32_t playerId, int32_t uidCounter)
434 {
435     HILOGI("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged start");
436     MessageParcel data;
437     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
438         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged WriteInterfaceToken error");
439         return;
440     }
441     if (!data.WriteInt32(playerId)) {
442         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged playStatus error");
443         return;
444     }
445     if (!data.WriteInt32(uidCounter)) {
446         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged playbackPos error");
447         return;
448     }
449 
450     MessageParcel reply;
451     MessageOption option = { MessageOption::TF_ASYNC };
452     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_ADDRESSED_PLAYER_CHANGED,
453                               option, data, reply);
454     if (error != NO_ERROR) {
455         HILOGE("BluetoothAvrcpTgProxy::NotifyAddressedPlayerChanged done fail, error: %{public}d", error);
456         return;
457     }
458     return;
459 }
460 
NotifyUidChanged(int32_t uidCounter)461 void BluetoothAvrcpTgProxy::NotifyUidChanged(int32_t uidCounter)
462 {
463     HILOGI("BluetoothAvrcpTgProxy::NotifyUidChanged start");
464     MessageParcel data;
465     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
466         HILOGE("BluetoothAvrcpTgProxy::NotifyUidChanged WriteInterfaceToken error");
467         return;
468     }
469     if (!data.WriteInt32(uidCounter)) {
470         HILOGE("BluetoothAvrcpTgProxy::NotifyUidChanged playStatus error");
471         return;
472     }
473 
474     MessageParcel reply;
475     MessageOption option = { MessageOption::TF_ASYNC };
476     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_UID_CHANGED, option, data, reply);
477     if (error != NO_ERROR) {
478         HILOGE("BluetoothAvrcpTgProxy::NotifyUidChanged done fail, error: %{public}d", error);
479         return;
480     }
481     return;
482 }
483 
NotifyVolumeChanged(int32_t volume)484 void BluetoothAvrcpTgProxy::NotifyVolumeChanged(int32_t volume)
485 {
486     HILOGI("BluetoothAvrcpTgProxy::NotifyVolumeChanged start");
487     MessageParcel data;
488     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
489         HILOGE("BluetoothAvrcpTgProxy::NotifyVolumeChanged WriteInterfaceToken error");
490         return;
491     }
492     if (!data.WriteInt32(volume)) {
493         HILOGE("BluetoothAvrcpTgProxy::NotifyVolumeChanged playStatus error");
494         return;
495     }
496 
497     MessageParcel reply;
498     MessageOption option = { MessageOption::TF_ASYNC };
499     int error = InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_NOTIFY_VOLUME_CHANGED, option, data, reply);
500     if (error != NO_ERROR) {
501         HILOGE("BluetoothAvrcpTgProxy::NotifyVolumeChanged done fail, error: %{public}d", error);
502         return;
503     }
504     return;
505 }
506 
SetDeviceAbsoluteVolume(const BluetoothRawAddress & addr,int32_t volumeLevel)507 int32_t BluetoothAvrcpTgProxy::SetDeviceAbsoluteVolume(const BluetoothRawAddress &addr, int32_t volumeLevel)
508 {
509     MessageParcel data;
510     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
511         HILOGE("[SetDeviceAbsoluteVolume] WriteInterfaceToken error");
512         return BT_ERR_IPC_TRANS_FAILED;
513     }
514 
515     if (!data.WriteParcelable(&addr)) {
516         HILOGE("[SetDeviceAbsoluteVolume] WriteParcelable error");
517         return BT_ERR_IPC_TRANS_FAILED;
518     }
519 
520     if (!data.WriteInt32(volumeLevel)) {
521         HILOGE("[SetDeviceAbsoluteVolume] WriteInt32 error");
522         return BT_ERR_IPC_TRANS_FAILED;
523     }
524 
525     MessageParcel reply;
526     MessageOption option = {MessageOption::TF_SYNC};
527     int error =
528         InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_SET_DEVICE_ABSOLUTE_VOLUME, option, data, reply);
529     if (error != NO_ERROR) {
530         HILOGE("BluetoothAvrcpTgProxy::SetDeviceAbsoluteVolume done fail, error: %{public}d", error);
531         return BT_ERR_IPC_TRANS_FAILED;
532     }
533 
534     return reply.ReadInt32();
535 }
SetDeviceAbsVolumeAbility(const BluetoothRawAddress & addr,int32_t ability)536 int32_t BluetoothAvrcpTgProxy::SetDeviceAbsVolumeAbility(const BluetoothRawAddress &addr, int32_t ability)
537 {
538     MessageParcel data;
539     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
540         HILOGE("[SetDeviceAbsVolumeAbility] WriteInterfaceToken error");
541         return BT_ERR_IPC_TRANS_FAILED;
542     }
543 
544     if (!data.WriteParcelable(&addr)) {
545         HILOGE("[SetDeviceAbsVolumeAbility] WriteParcelable error");
546         return BT_ERR_IPC_TRANS_FAILED;
547     }
548 
549     if (!data.WriteInt32(ability)) {
550         HILOGE("[SetDeviceAbsVolumeAbility] WriteInt32 error");
551         return BT_ERR_IPC_TRANS_FAILED;
552     }
553 
554     MessageParcel reply;
555     MessageOption option = {MessageOption::TF_SYNC};
556     int error =
557         InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_SET_DEVICE_ABS_VOLUME_ABILITY, option, data, reply);
558     if (error != NO_ERROR) {
559         HILOGE("BluetoothAvrcpTgProxy::SetDeviceAbsVolumeAbility done fail, error: %{public}d", error);
560         return BT_ERR_IPC_TRANS_FAILED;
561     }
562 
563     return reply.ReadInt32();
564 }
GetDeviceAbsVolumeAbility(const BluetoothRawAddress & addr,int32_t & ability)565 int32_t BluetoothAvrcpTgProxy::GetDeviceAbsVolumeAbility(const BluetoothRawAddress &addr, int32_t &ability)
566 {
567     MessageParcel data;
568     if (!data.WriteInterfaceToken(BluetoothAvrcpTgProxy::GetDescriptor())) {
569         HILOGE("[GetDeviceAbsVolumeAbility] WriteInterfaceToken error");
570         return BT_ERR_IPC_TRANS_FAILED;
571     }
572 
573     if (!data.WriteParcelable(&addr)) {
574         HILOGE("[GetDeviceAbsVolumeAbility] WriteParcelable error");
575         return BT_ERR_IPC_TRANS_FAILED;
576     }
577 
578     MessageParcel reply;
579     MessageOption option = {MessageOption::TF_SYNC};
580     int error =
581         InnerTransact(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_GET_DEVICE_ABS_VOLUME_ABILITY, option, data, reply);
582     if (error != NO_ERROR) {
583         HILOGE("BluetoothAvrcpTgProxy::GetDeviceAbsVolumeAbility done fail, error: %{public}d", error);
584         return BT_ERR_IPC_TRANS_FAILED;
585     }
586 
587     int32_t ret = reply.ReadInt32();
588     if (ret == BT_NO_ERROR) {
589         ability = reply.ReadInt32();
590     }
591 
592     return ret;
593 }
594 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)595 ErrCode BluetoothAvrcpTgProxy::InnerTransact(
596     uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
597 {
598     auto remote = Remote();
599     if (remote == nullptr) {
600         HILOGW("[InnerTransact] fail: get Remote fail code %{public}d", code);
601         return OBJECT_NULL;
602     }
603     int err = remote->SendRequest(code, data, reply, flags);
604     switch (err) {
605         case NO_ERROR: {
606             return NO_ERROR;
607         }
608         case DEAD_OBJECT: {
609             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
610             return DEAD_OBJECT;
611         }
612         default: {
613             HILOGW("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
614             return TRANSACTION_ERR;
615         }
616     }
617 }
618 }  // namespace Bluetooth
619 }  // namespace OHOS