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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_a2dp_sink_proxy"
17 #endif
18
19 #include "bluetooth_a2dp_sink_proxy.h"
20 #include "bluetooth_log.h"
21 #include "parcel_bt_uuid.h"
22 #include "bluetooth_errorcode.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
Connect(const RawAddress & device)26 int BluetoothA2dpSinkProxy::Connect(const RawAddress &device)
27 {
28 MessageParcel data;
29 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
30 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
31 CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
32
33 MessageParcel reply;
34 MessageOption option(MessageOption::TF_SYNC);
35
36 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_CONNECT,
37 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
38
39 return reply.ReadInt32();
40 }
41
Disconnect(const RawAddress & device)42 int BluetoothA2dpSinkProxy::Disconnect(const RawAddress &device)
43 {
44 MessageParcel data;
45 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
46 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
47 CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
48
49 MessageParcel reply;
50 MessageOption option(MessageOption::TF_SYNC);
51
52 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DISCONNECT,
53 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
54
55 return reply.ReadInt32();
56 }
57
RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)58 void BluetoothA2dpSinkProxy::RegisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
59 {
60 MessageParcel data;
61 CHECK_AND_RETURN_LOG(
62 data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()), "WriteInterfaceToken error");
63 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "write object error");
64
65 MessageParcel reply;
66 MessageOption option(MessageOption::TF_ASYNC);
67
68 SEND_IPC_REQUEST_RETURN(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_REGISTER_OBSERVER, data, reply, option);
69 }
70
DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> & observer)71 void BluetoothA2dpSinkProxy::DeregisterObserver(const sptr<IBluetoothA2dpSinkObserver> &observer)
72 {
73 MessageParcel data;
74 CHECK_AND_RETURN_LOG(
75 data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()), "WriteInterfaceToken error");
76 CHECK_AND_RETURN_LOG(data.WriteRemoteObject(observer->AsObject()), "write object error");
77
78 MessageParcel reply;
79 MessageOption option(MessageOption::TF_ASYNC);
80
81 SEND_IPC_REQUEST_RETURN(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DEREGISTER_OBSERVER, data, reply, option);
82 }
83
GetDevicesByStates(const std::vector<int32_t> & states)84 std::vector<RawAddress> BluetoothA2dpSinkProxy::GetDevicesByStates(const std::vector<int32_t> &states)
85 {
86 MessageParcel data;
87 std::vector<RawAddress> rawAdds = {};
88 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
89 rawAdds, "WriteInterfaceToken error");
90 CHECK_AND_RETURN_LOG_RET(WriteParcelableInt32Vector(states, data), rawAdds, "write rawAdds error");
91
92 MessageParcel reply;
93 MessageOption option(MessageOption::TF_SYNC);
94
95 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_BY_STATES,
96 data, reply, option, rawAdds);
97
98 int32_t rawAddsSize = reply.ReadInt32();
99 const int32_t maxSize = 100;
100 if (rawAddsSize > maxSize) {
101 return rawAdds;
102 }
103 for (int i = 0; i < rawAddsSize; i++) {
104 rawAdds.push_back(RawAddress(reply.ReadString()));
105 }
106 return rawAdds;
107 }
108
GetDeviceState(const RawAddress & device)109 int BluetoothA2dpSinkProxy::GetDeviceState(const RawAddress &device)
110 {
111 MessageParcel data;
112 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
113 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
114 CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
115
116 MessageParcel reply;
117 MessageOption option(MessageOption::TF_SYNC);
118
119 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_STATE,
120 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
121
122 return reply.ReadInt32();
123 }
124
GetPlayingState(const RawAddress & device,int & state)125 int BluetoothA2dpSinkProxy::GetPlayingState(const RawAddress &device, int &state)
126 {
127 MessageParcel data;
128 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
129 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
130 CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
131
132 MessageParcel reply;
133 MessageOption option(MessageOption::TF_SYNC);
134
135 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_PLAYING_STATE,
136 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
137
138 int32_t exception = reply.ReadInt32();
139 if (exception == NO_ERROR) {
140 state = reply.ReadInt32();
141 }
142 return exception;
143 }
144
SetConnectStrategy(const RawAddress & device,int32_t strategy)145 int BluetoothA2dpSinkProxy::SetConnectStrategy(const RawAddress &device, int32_t strategy)
146 {
147 MessageParcel data;
148 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
149 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
150 CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
151 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(strategy), BT_ERR_IPC_TRANS_FAILED, "write strategy error");
152
153 MessageParcel reply;
154 MessageOption option(MessageOption::TF_SYNC);
155
156 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SET_CONNECT_STRATEGY,
157 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
158
159 return reply.ReadInt32();
160 }
161
GetConnectStrategy(const RawAddress & device)162 int BluetoothA2dpSinkProxy::GetConnectStrategy(const RawAddress &device)
163 {
164 MessageParcel data;
165 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
166 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
167 CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
168
169 MessageParcel reply;
170 MessageOption option(MessageOption::TF_SYNC);
171
172 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_CONNECT_STRATEGY,
173 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
174
175 return reply.ReadInt32();
176 }
177
SendDelay(const RawAddress & device,int32_t delayValue)178 int BluetoothA2dpSinkProxy::SendDelay(const RawAddress &device, int32_t delayValue)
179 {
180 MessageParcel data;
181 CHECK_AND_RETURN_LOG_RET(data.WriteInterfaceToken(BluetoothA2dpSinkProxy::GetDescriptor()),
182 BT_ERR_IPC_TRANS_FAILED, "WriteInterfaceToken error");
183 CHECK_AND_RETURN_LOG_RET(data.WriteString(device.GetAddress()), BT_ERR_IPC_TRANS_FAILED, "write device error");
184 CHECK_AND_RETURN_LOG_RET(data.WriteInt32(delayValue), BT_ERR_IPC_TRANS_FAILED, "write delayValue error");
185
186 MessageParcel reply;
187 MessageOption option(MessageOption::TF_SYNC);
188
189 SEND_IPC_REQUEST_RETURN_RESULT(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SEND_DELAY,
190 data, reply, option, BT_ERR_IPC_TRANS_FAILED);
191
192 return reply.ReadInt32();
193 }
194
WriteParcelableInt32Vector(const std::vector<int32_t> & parcelableVector,Parcel & reply)195 bool BluetoothA2dpSinkProxy::WriteParcelableInt32Vector(
196 const std::vector<int32_t> &parcelableVector, Parcel &reply)
197 {
198 if (!reply.WriteInt32(parcelableVector.size())) {
199 HILOGE("write ParcelableVector failed");
200 return false;
201 }
202
203 for (auto parcelable : parcelableVector) {
204 if (!reply.WriteInt32(parcelable)) {
205 HILOGE("write ParcelableVector failed");
206 return false;
207 }
208 }
209 return true;
210 }
211
212 } // namespace Bluetooth
213 } // namespace OHOS