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