1 /*
2 * Copyright (C) 2021-2022 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_a2dp_src_observer_proxy.h"
17 #include "bluetooth_log.h"
18
19 namespace OHOS {
20 namespace Bluetooth {
OnConnectionStateChanged(const RawAddress & device,int state)21 void BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged(const RawAddress &device, int state)
22 {
23 MessageParcel data;
24 if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
25 HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged WriteInterfaceToken error");
26 return;
27 }
28 if (!data.WriteString(device.GetAddress())) {
29 HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged write device error");
30 return;
31 }
32 if (!data.WriteInt32(state)) {
33 HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged state error");
34 return;
35 }
36
37 MessageParcel reply;
38 MessageOption option {
39 MessageOption::TF_ASYNC
40 };
41
42 ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_CONNECTION_STATE_CHANGED, option, data, reply);
43 if (result != NO_ERROR) {
44 HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged done fail, error: %{public}d", result);
45 return;
46 }
47 }
48
OnPlayingStatusChanged(const RawAddress & device,int playingState,int error)49 void BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged(const RawAddress &device, int playingState, int error)
50 {
51 MessageParcel data;
52 if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
53 HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged WriteInterfaceToken error");
54 return;
55 }
56 if (!data.WriteString(device.GetAddress())) {
57 HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged write device error");
58 return;
59 }
60 if (!data.WriteInt32(playingState)) {
61 HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged playingState error");
62 return;
63 }
64
65 if (!data.WriteInt32(error)) {
66 HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged error error");
67 return;
68 }
69
70 MessageParcel reply;
71 MessageOption option {
72 MessageOption::TF_ASYNC
73 };
74
75 ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_PLAYING_STATUS_CHANGED, option, data, reply);
76 if (result != NO_ERROR) {
77 HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged done fail, error: %{public}d", result);
78 return;
79 }
80 }
81
OnConfigurationChanged(const RawAddress & device,const BluetoothA2dpCodecInfo & info,int error)82 void BluetoothA2dpSrcObserverProxy::OnConfigurationChanged(
83 const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error)
84 {
85 MessageParcel data;
86 if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
87 HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged WriteInterfaceToken error");
88 return;
89 }
90 if (!data.WriteString(device.GetAddress())) {
91 HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged write device error");
92 return;
93 }
94 if (!data.WriteParcelable(&info)) {
95 HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged transport error");
96 return;
97 }
98 if (!data.WriteInt32(error)) {
99 HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged error error");
100 return;
101 }
102
103 MessageParcel reply;
104 MessageOption option {
105 MessageOption::TF_ASYNC
106 };
107
108 ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_CONFIGURATION_CHANGED, option, data, reply);
109 if (result != NO_ERROR) {
110 HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged done fail, error: %{public}d", result);
111 return;
112 }
113 }
114
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)115 ErrCode BluetoothA2dpSrcObserverProxy::InnerTransact(
116 uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
117 {
118 auto remote = Remote();
119 if (remote == nullptr) {
120 HILOGE("[InnerTransact] fail: get Remote fail code %{public}d", code);
121 return ERR_DEAD_OBJECT;
122 }
123 int err = remote->SendRequest(code, data, reply, flags);
124 switch (err) {
125 case NO_ERROR: {
126 return ERR_OK;
127 }
128 case DEAD_OBJECT: {
129 HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
130 return ERR_DEAD_OBJECT;
131 }
132 default: {
133 HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
134 return TRANSACTION_ERR;
135 }
136 }
137 }
138 } // namespace Bluetooth
139 } // namespace OHOS