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_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "parcel_bt_uuid.h"
20 #include "raw_address.h"
21
22 namespace OHOS {
23 namespace Bluetooth {
24 using namespace OHOS::bluetooth;
25 const int32_t A2DP_MAX_SRC_CONNECTION_NUMS = 0x07;
BluetoothA2dpSrcStub()26 BluetoothA2dpSrcStub::BluetoothA2dpSrcStub()
27 {
28 HILOGD("%{public}s start.", __func__);
29 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_CONNECT)] =
30 &BluetoothA2dpSrcStub::ConnectInner;
31 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_DISCONNECT)] =
32 &BluetoothA2dpSrcStub::DisconnectInner;
33 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_REGISTER_OBSERVER)] =
34 &BluetoothA2dpSrcStub::RegisterObserverInner;
35 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_DEREGISTER_OBSERVER)] =
36 &BluetoothA2dpSrcStub::DeregisterObserverInner;
37 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_DEVICE_BY_STATES)] =
38 &BluetoothA2dpSrcStub::GetDevicesByStatesInner;
39 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_DEVICE_STATE)] =
40 &BluetoothA2dpSrcStub::GetDeviceStateInner;
41 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_PLAYING_STATE)] =
42 &BluetoothA2dpSrcStub::GetPlayingStateInner;
43 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_SET_CONNECT_STRATEGY)] =
44 &BluetoothA2dpSrcStub::SetConnectStrategyInner;
45 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_CONNECT_STRATEGY)] =
46 &BluetoothA2dpSrcStub::GetConnectStrategyInner;
47 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_SET_ACTIVE_SINK_DEVICE)] =
48 &BluetoothA2dpSrcStub::SetActiveSinkDeviceInner;
49 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_ACTIVE_SINK_DEVICE)] =
50 &BluetoothA2dpSrcStub::GetActiveSinkDeviceInner;
51 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_CODEC_STATUS)] =
52 &BluetoothA2dpSrcStub::GetCodecStatusInner;
53 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_SET_CODEC_PREFERENCE)] =
54 &BluetoothA2dpSrcStub::SetCodecPreferenceInner;
55 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_SWITCH_OPTIONAL_CODECS)] =
56 &BluetoothA2dpSrcStub::SwitchOptionalCodecsInner;
57 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_OPTIONAL_CODECS_SUPPORT_STATE)] =
58 &BluetoothA2dpSrcStub::GetOptionalCodecsSupportStateInner;
59 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_START_PLAYING)] =
60 &BluetoothA2dpSrcStub::StartPlayingInner;
61 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_SUSPEND_PLAYING)] =
62 &BluetoothA2dpSrcStub::SuspendPlayingInner;
63 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_STOP_PLAYING)] =
64 &BluetoothA2dpSrcStub::StopPlayingInner;
65 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_WRITE_FRAME)] =
66 &BluetoothA2dpSrcStub::WriteFrameInner;
67 memberFuncMap_[static_cast<uint32_t>(IBluetoothA2dpSrc::Code::BT_A2DP_SRC_GET_RENDER_POSITION)] =
68 &BluetoothA2dpSrcStub::GetRenderPositionInner;
69 }
70
~BluetoothA2dpSrcStub()71 BluetoothA2dpSrcStub::~BluetoothA2dpSrcStub()
72 {
73 HILOGD("%{public}s start.", __func__);
74 memberFuncMap_.clear();
75 }
76
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)77 int BluetoothA2dpSrcStub::OnRemoteRequest(
78 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
79 {
80 HILOGI("BluetoothA2dpSrcStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
81 if (BluetoothA2dpSrcStub::GetDescriptor() != data.ReadInterfaceToken()) {
82 HILOGI("local descriptor is not equal to remote");
83 return ERR_INVALID_STATE;
84 }
85 auto itFunc = memberFuncMap_.find(code);
86 if (itFunc != memberFuncMap_.end()) {
87 auto memberFunc = itFunc->second;
88 if (memberFunc != nullptr) {
89 return (this->*memberFunc)(data, reply);
90 }
91 }
92 HILOGW("BluetoothA2dpSrcStub::OnRemoteRequest, default case, need check.");
93 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
94 }
95
ConnectInner(MessageParcel & data,MessageParcel & reply)96 int32_t BluetoothA2dpSrcStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
97 {
98 std::string addr = data.ReadString();
99
100 int32_t result = Connect(RawAddress(addr));
101
102 bool ret = reply.WriteInt32(result);
103 if (!ret) {
104 HILOGE("BluetoothA2dpSrcStub: ConnectInner reply writing failed in: %{public}s.", __func__);
105 return TRANSACTION_ERR;
106 }
107
108 return NO_ERROR;
109 }
110
DisconnectInner(MessageParcel & data,MessageParcel & reply)111 int32_t BluetoothA2dpSrcStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
112 {
113 std::string addr = data.ReadString();
114
115 int32_t result = Disconnect(RawAddress(addr));
116
117 bool ret = reply.WriteInt32(result);
118 if (!ret) {
119 HILOGE("BluetoothA2dpSrcStub: DisconnectInner reply writing failed in: %{public}s.", __func__);
120 return TRANSACTION_ERR;
121 }
122
123 return NO_ERROR;
124 }
125
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)126 ErrCode BluetoothA2dpSrcStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
127 {
128 sptr<IRemoteObject> remote = data.ReadRemoteObject();
129 const sptr<IBluetoothA2dpSourceObserver> observer = OHOS::iface_cast<IBluetoothA2dpSourceObserver>(remote);
130 RegisterObserver(observer);
131
132 return NO_ERROR;
133 }
134
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)135 ErrCode BluetoothA2dpSrcStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
136 {
137 sptr<IRemoteObject> remote = data.ReadRemoteObject();
138 const sptr<IBluetoothA2dpSourceObserver> observer = OHOS::iface_cast<IBluetoothA2dpSourceObserver>(remote);
139 DeregisterObserver(observer);
140
141 return NO_ERROR;
142 }
143
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)144 ErrCode BluetoothA2dpSrcStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
145 {
146 std::vector<int32_t> states = {};
147 int32_t stateSize = data.ReadInt32();
148 if (stateSize > A2DP_MAX_SRC_CONNECTION_NUMS) {
149 return ERR_INVALID_STATE;
150 }
151
152 for (int i = 0; i < stateSize; i++) {
153 int32_t state = data.ReadInt32();
154 states.push_back(state);
155 }
156
157 std::vector<RawAddress> rawAdds = GetDevicesByStates(states);
158 reply.WriteInt32(rawAdds.size());
159
160 for (auto rawAdd : rawAdds) {
161 if (!reply.WriteString(rawAdd.GetAddress())) {
162 HILOGE("GetConnectedDevicesInner: write WriteString failed");
163 return ERR_INVALID_STATE;
164 }
165 }
166 return NO_ERROR;
167 }
168
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)169 ErrCode BluetoothA2dpSrcStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
170 {
171 std::string addr = data.ReadString();
172
173 int result = GetDeviceState(RawAddress(addr));
174
175 bool ret = reply.WriteInt32(result);
176 if (!ret) {
177 HILOGE("BluetoothA2dpSrcStub: GetDeviceStateInner reply writing failed in: %{public}s.", __func__);
178 return TRANSACTION_ERR;
179 }
180
181 return NO_ERROR;
182 }
183
GetPlayingStateInner(MessageParcel & data,MessageParcel & reply)184 int32_t BluetoothA2dpSrcStub::GetPlayingStateInner(MessageParcel &data, MessageParcel &reply)
185 {
186 std::string addr = data.ReadString();
187 int32_t state = 0;
188 int32_t result = GetPlayingState(RawAddress(addr), state);
189
190 (void)reply.WriteInt32(result);
191 (void)reply.WriteInt32(state);
192 return NO_ERROR;
193 }
194
SetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)195 ErrCode BluetoothA2dpSrcStub::SetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
196 {
197 std::string addr = data.ReadString();
198 int strategy = data.ReadInt32();
199
200 int result = SetConnectStrategy(RawAddress(addr), strategy);
201
202 bool ret = reply.WriteInt32(result);
203 if (!ret) {
204 HILOGE("BluetoothA2dpSrcStub: SetConnectStrategyInner reply writing failed in: %{public}s.", __func__);
205 return TRANSACTION_ERR;
206 }
207
208 return NO_ERROR;
209 }
210
GetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)211 ErrCode BluetoothA2dpSrcStub::GetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
212 {
213 std::string addr = data.ReadString();
214 int result = GetConnectStrategy(RawAddress(addr));
215
216 bool ret = reply.WriteInt32(result);
217 if (!ret) {
218 HILOGE("BluetoothA2dpSrcStub: GetConnectStrategyInner reply writing failed in: %{public}s.", __func__);
219 return TRANSACTION_ERR;
220 }
221
222 return NO_ERROR;
223 }
224
SetActiveSinkDeviceInner(MessageParcel & data,MessageParcel & reply)225 ErrCode BluetoothA2dpSrcStub::SetActiveSinkDeviceInner(MessageParcel &data, MessageParcel &reply)
226 {
227 std::string addr = data.ReadString();
228 int result = SetActiveSinkDevice(RawAddress(addr));
229
230 bool ret = reply.WriteInt32(result);
231 if (!ret) {
232 HILOGE("BluetoothA2dpSrcStub: SetActiveSinkDeviceInner reply writing failed in: %{public}s.", __func__);
233 return TRANSACTION_ERR;
234 }
235
236 return NO_ERROR;
237 }
238
GetActiveSinkDeviceInner(MessageParcel & data,MessageParcel & reply)239 ErrCode BluetoothA2dpSrcStub::GetActiveSinkDeviceInner(MessageParcel &data, MessageParcel &reply)
240 {
241 RawAddress adds = GetActiveSinkDevice();
242
243 if (!reply.WriteString(adds.GetAddress())) {
244 HILOGE("GetActiveSinkDeviceInner: write WriteString failed");
245 return ERR_INVALID_STATE;
246 }
247
248 return NO_ERROR;
249 }
250
GetCodecStatusInner(MessageParcel & data,MessageParcel & reply)251 ErrCode BluetoothA2dpSrcStub::GetCodecStatusInner(MessageParcel &data, MessageParcel &reply)
252 {
253 std::string addr = data.ReadString();
254 BluetoothA2dpCodecStatus result = GetCodecStatus(RawAddress(addr));
255
256 bool ret = reply.WriteParcelable(&result);
257 if (!ret) {
258 HILOGE("BluetoothA2dpSrcStub: GetCodecStatusInner reply writing failed in: %{public}s.", __func__);
259 return TRANSACTION_ERR;
260 }
261
262 return NO_ERROR;
263 }
264
SetCodecPreferenceInner(MessageParcel & data,MessageParcel & reply)265 ErrCode BluetoothA2dpSrcStub::SetCodecPreferenceInner(MessageParcel &data, MessageParcel &reply)
266 {
267 std::string addr = data.ReadString();
268 std::shared_ptr<BluetoothA2dpCodecInfo> info(data.ReadParcelable<BluetoothA2dpCodecInfo>());
269 if (!info) {
270 return TRANSACTION_ERR;
271 }
272 int result = SetCodecPreference(RawAddress(addr), *info);
273
274 bool ret = reply.WriteInt32(result);
275 if (!ret) {
276 HILOGE("BluetoothA2dpSrcStub: SetCodecPreferenceInner reply writing failed in: %{public}s.", __func__);
277 return TRANSACTION_ERR;
278 }
279
280 return NO_ERROR;
281 }
282
SwitchOptionalCodecsInner(MessageParcel & data,MessageParcel & reply)283 ErrCode BluetoothA2dpSrcStub::SwitchOptionalCodecsInner(MessageParcel &data, MessageParcel &reply)
284 {
285 std::string addr = data.ReadString();
286 bool isEnable = data.ReadBool();
287 SwitchOptionalCodecs(RawAddress(addr), isEnable);
288 return NO_ERROR;
289 }
290
GetOptionalCodecsSupportStateInner(MessageParcel & data,MessageParcel & reply)291 ErrCode BluetoothA2dpSrcStub::GetOptionalCodecsSupportStateInner(MessageParcel &data, MessageParcel &reply)
292 {
293 std::string addr = data.ReadString();
294 int result = GetOptionalCodecsSupportState(RawAddress(addr));
295
296 bool ret = reply.WriteInt32(result);
297 if (!ret) {
298 HILOGE("BluetoothA2dpSrcStub: GetOptionalCodecsSupportStateInner reply writing failed in: %{public}s.", __func__);
299 return TRANSACTION_ERR;
300 }
301
302 return NO_ERROR;
303 }
304
StartPlayingInner(MessageParcel & data,MessageParcel & reply)305 ErrCode BluetoothA2dpSrcStub::StartPlayingInner(MessageParcel &data, MessageParcel &reply)
306 {
307 std::string addr = data.ReadString();
308 int result = StartPlaying(RawAddress(addr));
309
310 bool ret = reply.WriteInt32(result);
311 if (!ret) {
312 HILOGE("BluetoothA2dpSrcStub: StartPlayingInner reply writing failed in: %{public}s.", __func__);
313 return TRANSACTION_ERR;
314 }
315
316 return NO_ERROR;
317 }
318
SuspendPlayingInner(MessageParcel & data,MessageParcel & reply)319 ErrCode BluetoothA2dpSrcStub::SuspendPlayingInner(MessageParcel &data, MessageParcel &reply)
320 {
321 std::string addr = data.ReadString();
322 int result = SuspendPlaying(RawAddress(addr));
323
324 bool ret = reply.WriteInt32(result);
325 if (!ret) {
326 HILOGE("BluetoothA2dpSrcStub: SuspendPlayingInner reply writing failed in: %{public}s.", __func__);
327 return TRANSACTION_ERR;
328 }
329
330 return NO_ERROR;
331 }
332
StopPlayingInner(MessageParcel & data,MessageParcel & reply)333 ErrCode BluetoothA2dpSrcStub::StopPlayingInner(MessageParcel &data, MessageParcel &reply)
334 {
335 std::string addr = data.ReadString();
336 int result = StopPlaying(RawAddress(addr));
337
338 bool ret = reply.WriteInt32(result);
339 if (!ret) {
340 HILOGE("BluetoothA2dpSrcStub: StopPlayingInner reply writing failed in: %{public}s.", __func__);
341 return TRANSACTION_ERR;
342 }
343
344 return NO_ERROR;
345 }
346
WriteFrameInner(MessageParcel & data,MessageParcel & reply)347 ErrCode BluetoothA2dpSrcStub::WriteFrameInner(MessageParcel &data, MessageParcel &reply)
348 {
349 std::vector<uint8_t> dataVector;
350 if (!data.ReadUInt8Vector(&dataVector)) {
351 HILOGE("BluetoothA2dpSrcStub: WriteFrameInner data reading failed in: %{public}s.", __func__);
352 return TRANSACTION_ERR;
353 }
354
355 int ret = WriteFrame(dataVector.data(), dataVector.size());
356 if (!reply.WriteInt32(ret)) {
357 HILOGE("BluetoothA2dpSrcStub: WriteFrameInner reply writing failed in: %{public}s.", __func__);
358 return TRANSACTION_ERR;
359 }
360
361 return NO_ERROR;
362 }
363
GetRenderPositionInner(MessageParcel & data,MessageParcel & reply)364 ErrCode BluetoothA2dpSrcStub::GetRenderPositionInner(MessageParcel &data, MessageParcel &reply)
365 {
366 uint16_t delayValue;
367 uint16_t sendDataSize;
368 uint32_t timeStamp;
369 GetRenderPosition(delayValue, sendDataSize, timeStamp);
370 if (!reply.WriteUint16(delayValue)) {
371 HILOGE("BluetoothA2dpSrcStub: GetRenderPositionInner reply writing failed in: %{public}s.", __func__);
372 return TRANSACTION_ERR;
373 }
374 if (!reply.WriteUint16(sendDataSize)) {
375 HILOGE("BluetoothA2dpSrcStub: GetRenderPositionInner reply writing failed in: %{public}s.", __func__);
376 return TRANSACTION_ERR;
377 }
378 if (!reply.WriteUint32(timeStamp)) {
379 HILOGE("BluetoothA2dpSrcStub: GetRenderPositionInner reply writing failed in: %{public}s.", __func__);
380 return TRANSACTION_ERR;
381 }
382 return NO_ERROR;
383 }
384
385 } // namespace Bluetooth
386 } // namespace OHOS