• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,int cause)21 void BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged(const RawAddress &device, int state, int cause)
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     if (!data.WriteInt32(cause)) {
37         HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged cause error");
38         return;
39     }
40 
41     MessageParcel reply;
42     MessageOption option {
43         MessageOption::TF_ASYNC
44     };
45 
46     ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_CONNECTION_STATE_CHANGED, option, data, reply);
47     if (result != NO_ERROR) {
48         HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged done fail, error: %{public}d", result);
49         return;
50     }
51 }
52 
OnCaptureConnectionStateChanged(const RawAddress & device,int state,const BluetoothA2dpCodecInfo & info)53 void BluetoothA2dpSrcObserverProxy::OnCaptureConnectionStateChanged(const RawAddress &device, int state,
54     const BluetoothA2dpCodecInfo &info)
55 {
56     MessageParcel data;
57     if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
58         HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged WriteInterfaceToken error");
59         return;
60     }
61     if (!data.WriteString(device.GetAddress())) {
62         HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged write device error");
63         return;
64     }
65     if (!data.WriteInt32(state)) {
66         HILOGE("BluetoothA2dpSrcObserverProxy::OnConnectionStateChanged state error");
67         return;
68     }
69     if (!data.WriteParcelable(&info)) {
70         HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged transport error");
71         return;
72     }
73 
74     MessageParcel reply;
75     MessageOption option {
76         MessageOption::TF_ASYNC
77     };
78 
79     ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_CAPTURE_CONNECTION_STATE_CHANGED, option, data, reply);
80     if (result != NO_ERROR) {
81         HILOGE("BluetoothA2dpSrcObserverProxy::OnCaptureConnectionStateChanged done fail, error: %{public}d", result);
82         return;
83     }
84 }
85 
OnPlayingStatusChanged(const RawAddress & device,int playingState,int error)86 void BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged(const RawAddress &device, int playingState, int error)
87 {
88     MessageParcel data;
89     if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
90         HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged WriteInterfaceToken error");
91         return;
92     }
93     if (!data.WriteString(device.GetAddress())) {
94         HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged write device error");
95         return;
96     }
97     if (!data.WriteInt32(playingState)) {
98         HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged playingState error");
99         return;
100     }
101 
102     if (!data.WriteInt32(error)) {
103         HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged error error");
104         return;
105     }
106 
107     MessageParcel reply;
108     MessageOption option {
109         MessageOption::TF_ASYNC
110     };
111 
112     ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_PLAYING_STATUS_CHANGED, option, data, reply);
113     if (result != NO_ERROR) {
114         HILOGE("BluetoothA2dpSrcObserverProxy::OnPlayingStatusChanged done fail, error: %{public}d", result);
115         return;
116     }
117 }
118 
OnConfigurationChanged(const RawAddress & device,const BluetoothA2dpCodecInfo & info,int error)119 void BluetoothA2dpSrcObserverProxy::OnConfigurationChanged(
120     const RawAddress &device, const BluetoothA2dpCodecInfo &info, int error)
121 {
122     MessageParcel data;
123     if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
124         HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged WriteInterfaceToken error");
125         return;
126     }
127     if (!data.WriteString(device.GetAddress())) {
128         HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged write device error");
129         return;
130     }
131     if (!data.WriteParcelable(&info)) {
132         HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged transport error");
133         return;
134     }
135     if (!data.WriteInt32(error)) {
136         HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged error error");
137         return;
138     }
139 
140     MessageParcel reply;
141     MessageOption option {
142         MessageOption::TF_ASYNC
143     };
144 
145     ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_CONFIGURATION_CHANGED, option, data, reply);
146     if (result != NO_ERROR) {
147         HILOGE("BluetoothA2dpSrcObserverProxy::OnConfigurationChanged done fail, error: %{public}d", result);
148         return;
149     }
150 }
151 
OnMediaStackChanged(const RawAddress & device,int action)152 void BluetoothA2dpSrcObserverProxy::OnMediaStackChanged(
153     const RawAddress &device, int action)
154 {
155     MessageParcel data;
156     if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
157         HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged WriteInterfaceToken error");
158         return;
159     }
160     if (!data.WriteString(device.GetAddress())) {
161         HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged write device error");
162         return;
163     }
164     if (!data.WriteInt32(action)) {
165         HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged error error");
166         return;
167     }
168 
169     MessageParcel reply;
170     MessageOption option {
171         MessageOption::TF_ASYNC
172     };
173 
174     ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_MEDIASTACK_CHANGED, option, data, reply);
175     if (result != NO_ERROR) {
176         HILOGE("BluetoothA2dpSrcObserverProxy::OnMediaStackChanged done fail, error: %{public}d", result);
177         return;
178     }
179 }
180 
InnerTransact(uint32_t code,MessageOption & flags,MessageParcel & data,MessageParcel & reply)181 ErrCode BluetoothA2dpSrcObserverProxy::InnerTransact(
182     uint32_t code, MessageOption &flags, MessageParcel &data, MessageParcel &reply)
183 {
184     auto remote = Remote();
185     if (remote == nullptr) {
186         HILOGE("[InnerTransact] fail: get Remote fail code %{public}d", code);
187         return ERR_DEAD_OBJECT;
188     }
189     int err = remote->SendRequest(code, data, reply, flags);
190     switch (err) {
191         case NO_ERROR: {
192             return ERR_OK;
193         }
194         case DEAD_OBJECT: {
195             HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
196             return ERR_DEAD_OBJECT;
197         }
198         default: {
199             HILOGE("[InnerTransact] fail: ipcErr=%{public}d code %{public}d", err, code);
200             return TRANSACTION_ERR;
201         }
202     }
203 }
204 
OnVirtualDeviceChanged(int32_t action,std::string address)205 void BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged(int32_t action, std::string address)
206 {
207     MessageParcel data;
208     if (!data.WriteInterfaceToken(BluetoothA2dpSrcObserverProxy::GetDescriptor())) {
209         HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged WriteInterfaceToken error");
210         return;
211     }
212     if (!data.WriteInt32(action)) {
213         HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged WriteInt32 error");
214         return;
215     }
216     if (!data.WriteString(address)) {
217         HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged WriteString error");
218         return;
219     }
220     MessageParcel reply;
221     MessageOption option {
222         MessageOption::TF_ASYNC
223     };
224     ErrCode result = InnerTransact(BT_A2DP_SRC_OBSERVER_VIRTUALDEVICE_CHANGED, option, data, reply);
225     if (result != NO_ERROR) {
226         HILOGE("BluetoothA2dpSrcObserverProxy::OnVirtualDeviceChanged done fail, error: %{public}d", result);
227         return;
228     }
229 }
230 }  // namespace Bluetooth
231 }  // namespace OHOS