• 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_a2dp_sink_proxy.h"
17 #include "bluetooth_log.h"
18 #include "parcel_bt_uuid.h"
19 #include "bluetooth_errorcode.h"
20 
21 namespace OHOS {
22 namespace Bluetooth {
Connect(const RawAddress & device)23 int BluetoothA2dpSinkProxy::Connect(const RawAddress &device)
24 {
25     MessageParcel data;
26     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
27         HILOGE("BluetoothA2dpSinkProxy::Connect WriteInterfaceToken error");
28         return ERROR;
29     }
30     if (!data.WriteString(device.GetAddress())) {
31         HILOGE("BluetoothA2dpSinkProxy::Connect write device error");
32         return ERROR;
33     }
34 
35     MessageParcel reply;
36     MessageOption option {
37         MessageOption::TF_SYNC
38     };
39 
40     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_DEVICE_STATE, data, reply, option);
41     if (error != NO_ERROR) {
42         HILOGE("BluetoothA2dpSinkProxy::Connect done fail, error: %{public}d", error);
43         return ERROR;
44     }
45 
46     return reply.ReadInt32();
47 }
48 
Disconnect(const RawAddress & device)49 int BluetoothA2dpSinkProxy::Disconnect(const RawAddress &device)
50 {
51     MessageParcel data;
52     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
53         HILOGE("BluetoothA2dpSinkProxy::Disconnect WriteInterfaceToken error");
54         return ERROR;
55     }
56     if (!data.WriteString(device.GetAddress())) {
57         HILOGE("BluetoothA2dpSinkProxy::Disconnect write device error");
58         return ERROR;
59     }
60 
61     MessageParcel reply;
62     MessageOption option {
63         MessageOption::TF_SYNC
64     };
65 
66     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_DEVICE_STATE, data, reply, option);
67     if (error != NO_ERROR) {
68         HILOGE("BluetoothA2dpSinkProxy::Disconnect done fail, error: %{public}d", error);
69         return ERROR;
70     }
71 
72     return reply.ReadInt32();
73 }
74 
RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)75 void BluetoothA2dpSinkProxy::RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
76 {
77     MessageParcel data;
78     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
79         HILOGE("BluetoothA2dpSinkProxy::RegisterObserver WriteInterfaceToken error");
80         return;
81     }
82     if (!data.WriteRemoteObject(observer->AsObject())) {
83         HILOGE("BluetoothA2dpSinkProxy::RegisterObserver error");
84         return;
85     }
86 
87     MessageParcel reply;
88     MessageOption option {
89         MessageOption::TF_ASYNC
90     };
91 
92     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_REGISTER_OBSERVER, data, reply, option);
93     if (error != NO_ERROR) {
94         HILOGE("BluetoothA2dpSinkProxy::RegisterObserver done fail, error: %{public}d", error);
95         return;
96     }
97 }
98 
DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)99 void BluetoothA2dpSinkProxy::DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
100 {
101     MessageParcel data;
102     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
103         HILOGE("BluetoothA2dpSinkProxy::DeregisterObserver WriteInterfaceToken error");
104         return;
105     }
106     if (!data.WriteRemoteObject(observer->AsObject())) {
107         HILOGE("BluetoothA2dpSinkProxy::DeregisterObserver error");
108         return;
109     }
110 
111     MessageParcel reply;
112     MessageOption option {
113         MessageOption::TF_ASYNC
114     };
115 
116     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_DEREGISTER_OBSERVER, data, reply, option);
117     if (error != NO_ERROR) {
118         HILOGE("BluetoothA2dpSinkProxy::DeregisterObserver done fail, error: %{public}d", error);
119         return;
120     }
121 }
122 
GetDevicesByStates(const std::vector<int32_t> & states)123 std::vector<RawAddress> BluetoothA2dpSinkProxy::GetDevicesByStates(const std::vector<int32_t> &states)
124 {
125     MessageParcel data;
126     std::vector<RawAddress> rawAdds = {};
127     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
128         HILOGE("[GetDevicesByStates] fail: write interface token failed.");
129         return rawAdds;
130     }
131 
132     if (!WriteParcelableInt32Vector(states, data)) {
133         HILOGE("[GetDevicesByStates] fail: write result failed");
134         return rawAdds;
135     }
136 
137     MessageParcel reply;
138     MessageOption option = {MessageOption::TF_SYNC};
139     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_DEVICE_BY_STATES, data, reply, option);
140     if (error != NO_ERROR) {
141         HILOGE("BluetoothA2dpSinkProxy::GetDevicesByStates done fail, error: %{public}d", error);
142         return rawAdds;
143     }
144     int32_t rawAddsSize = reply.ReadInt32();
145     for (int i = 0; i < rawAddsSize; i++) {
146         rawAdds.push_back(RawAddress(reply.ReadString()));
147     }
148     return rawAdds;
149 }
150 
GetDeviceState(const RawAddress & device)151 int BluetoothA2dpSinkProxy::GetDeviceState(const RawAddress &device)
152 {
153     MessageParcel data;
154     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
155         HILOGE("BluetoothA2dpSinkProxy::GetDeviceState WriteInterfaceToken error");
156         return ERROR;
157     }
158     if (!data.WriteString(device.GetAddress())) {
159         HILOGE("BluetoothA2dpSinkProxy::GetDeviceState write device error");
160         return ERROR;
161     }
162 
163     MessageParcel reply;
164     MessageOption option {
165         MessageOption::TF_SYNC
166     };
167 
168     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_DEVICE_STATE, data, reply, option);
169     if (error != NO_ERROR) {
170         HILOGE("BluetoothA2dpSinkProxy::GetDeviceState done fail, error: %{public}d", error);
171         return ERROR;
172     }
173 
174     return reply.ReadInt32();
175 }
176 
GetPlayingState(const RawAddress & device)177 int BluetoothA2dpSinkProxy::GetPlayingState(const RawAddress &device)
178 {
179     MessageParcel data;
180     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
181         HILOGE("BluetoothA2dpSinkProxy::GetPlayingState WriteInterfaceToken error");
182         return ERROR;
183     }
184     if (!data.WriteString(device.GetAddress())) {
185         HILOGE("BluetoothA2dpSinkProxy::GetPlayingState write device error");
186         return ERROR;
187     }
188 
189     MessageParcel reply;
190     MessageOption option {
191         MessageOption::TF_SYNC
192     };
193 
194     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_PLAYING_STATE, data, reply, option);
195     if (error != NO_ERROR) {
196         HILOGE("BluetoothA2dpSinkProxy::GetPlayingState done fail, error: %{public}d", error);
197         return ERROR;
198     }
199 
200     return reply.ReadInt32();
201 }
202 
SetConnectStrategy(const RawAddress & device,int32_t strategy)203 int BluetoothA2dpSinkProxy::SetConnectStrategy(const RawAddress &device, int32_t strategy)
204 {
205     MessageParcel data;
206     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
207         HILOGE("BluetoothA2dpSinkProxy::GetPlayingState WriteInterfaceToken error");
208         return ERROR;
209     }
210     if (!data.WriteString(device.GetAddress())) {
211         HILOGE("BluetoothA2dpSinkProxy::GetPlayingState write device error");
212         return ERROR;
213     }
214     if (!data.WriteInt32(strategy)) {
215         HILOGE("BluetoothA2dpSinkProxy::GetPlayingState write strategy error");
216         return ERROR;
217     }
218 
219     MessageParcel reply;
220     MessageOption option {
221         MessageOption::TF_SYNC
222     };
223 
224     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_SET_CONNECT_STRATEGY, data, reply, option);
225     if (error != NO_ERROR) {
226         HILOGE("BluetoothA2dpSinkProxy::GetPlayingState done fail, error: %{public}d", error);
227         return ERROR;
228     }
229 
230     return reply.ReadInt32();
231 }
232 
GetConnectStrategy(const RawAddress & device)233 int BluetoothA2dpSinkProxy::GetConnectStrategy(const RawAddress &device)
234 {
235     MessageParcel data;
236     if (!data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor())) {
237         HILOGE("BluetoothA2dpSinkProxy::GetConnectStrategy WriteInterfaceToken error");
238         return ERROR;
239     }
240     if (!data.WriteString(device.GetAddress())) {
241         HILOGE("BluetoothA2dpSinkProxy::GetConnectStrategy write device error");
242         return ERROR;
243     }
244 
245     MessageParcel reply;
246     MessageOption option {
247         MessageOption::TF_SYNC
248     };
249 
250     int error = Remote()->SendRequest(IBluetoothA2dpSink::Code::BT_A2DP_SINK_GET_CONNECT_STRATEGY, data, reply, option);
251     if (error != NO_ERROR) {
252         HILOGE("BluetoothA2dpSinkProxy::GetConnectStrategy done fail, error: %{public}d", error);
253         return ERROR;
254     }
255 
256     return reply.ReadInt32();
257 }
258 
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)259 bool BluetoothA2dpSinkProxy::WriteParcelableInt32Vector(
260     const std::vector<int32_t> &parcelableVector, Parcel &reply)
261 {
262     if (!reply.WriteInt32(parcelableVector.size())) {
263         HILOGE("write ParcelableVector failed");
264         return false;
265     }
266 
267     for (auto parcelable : parcelableVector) {
268         if (!reply.WriteInt32(parcelable)) {
269             HILOGE("write ParcelableVector failed");
270             return false;
271         }
272     }
273     return true;
274 }
275 
276 }  // namespace Bluetooth
277 }  // namespace OHOS