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