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