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 #ifndef LOG_TAG
16 #define LOG_TAG "bt_ipc_a2dp_src_observer_stub"
17 #endif
18
19 #include "bluetooth_a2dp_src_observer_stub.h"
20 #include "bluetooth_errorcode.h"
21 #include "bluetooth_log.h"
22 #include "raw_address.h"
23
24 namespace OHOS {
25 namespace Bluetooth {
26 using namespace OHOS::bluetooth;
BluetoothA2dpSrcObserverStub()27 BluetoothA2dpSrcObserverStub::BluetoothA2dpSrcObserverStub()
28 {
29 HILOGD("start.");
30 memberFuncMap_[static_cast<uint32_t>(
31 BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_CONNECTION_STATE_CHANGED)] =
32 BluetoothA2dpSrcObserverStub::OnConnectionStateChangedInner;
33 memberFuncMap_[static_cast<uint32_t>(
34 BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_PLAYING_STATUS_CHANGED)] =
35 BluetoothA2dpSrcObserverStub::OnPlayingStatusChangedInner;
36 memberFuncMap_[static_cast<uint32_t>(
37 BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_CONFIGURATION_CHANGED)] =
38 BluetoothA2dpSrcObserverStub::OnConfigurationChangedInner;
39 memberFuncMap_[static_cast<uint32_t>(
40 BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_MEDIASTACK_CHANGED)] =
41 BluetoothA2dpSrcObserverStub::OnMediaStackChangedInner;
42 memberFuncMap_[static_cast<uint32_t>(
43 BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_VIRTUALDEVICE_CHANGED)] =
44 BluetoothA2dpSrcObserverStub::OnVirtualDeviceChangedInner;
45 memberFuncMap_[static_cast<uint32_t>(
46 BluetoothA2dpSourceObserverInterfaceCode::BT_A2DP_SRC_OBSERVER_CAPTURE_CONNECTION_STATE_CHANGED)] =
47 BluetoothA2dpSrcObserverStub::OnCaptureConnectionStateChangedInner;
48 }
49
~BluetoothA2dpSrcObserverStub()50 BluetoothA2dpSrcObserverStub::~BluetoothA2dpSrcObserverStub()
51 {
52 HILOGD("start.");
53 memberFuncMap_.clear();
54 }
55
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)56 int BluetoothA2dpSrcObserverStub::OnRemoteRequest(
57 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
58 {
59 HILOGD("cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
60 if (BluetoothA2dpSrcObserverStub::GetDescriptor() != data.ReadInterfaceToken()) {
61 HILOGI("local descriptor is not equal to remote");
62 return ERR_INVALID_STATE;
63 }
64 auto itFunc = memberFuncMap_.find(code);
65 if (itFunc != memberFuncMap_.end()) {
66 auto memberFunc = itFunc->second;
67 if (memberFunc != nullptr) {
68 return memberFunc(this, data, reply);
69 }
70 }
71 HILOGW("default case, need check.");
72 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73 }
74
OnConnectionStateChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)75 int32_t BluetoothA2dpSrcObserverStub::OnConnectionStateChangedInner(
76 BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
77 {
78 std::string addr = data.ReadString();
79 int state = data.ReadInt32();
80 int cause = data.ReadInt32();
81
82 stub->OnConnectionStateChanged(RawAddress(addr), state, cause);
83
84 return NO_ERROR;
85 }
86
OnPlayingStatusChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)87 int32_t BluetoothA2dpSrcObserverStub::OnPlayingStatusChangedInner(
88 BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
89 {
90 std::string addr = data.ReadString();
91 int playingState = data.ReadInt32();
92 int error = data.ReadInt32();
93
94 stub->OnPlayingStatusChanged(RawAddress(addr), playingState, error);
95
96 return NO_ERROR;
97 }
98
OnConfigurationChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)99 int32_t BluetoothA2dpSrcObserverStub::OnConfigurationChangedInner(
100 BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
101 {
102 std::string addr = data.ReadString();
103 std::shared_ptr<BluetoothA2dpCodecInfo> info(data.ReadParcelable<BluetoothA2dpCodecInfo>());
104 if (!info) {
105 return BT_ERR_IPC_TRANS_FAILED;
106 }
107 int error = data.ReadInt32();
108
109 stub->OnConfigurationChanged(RawAddress(addr), *info, error);
110 return NO_ERROR;
111 }
112
OnMediaStackChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)113 int32_t BluetoothA2dpSrcObserverStub::OnMediaStackChangedInner(
114 BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
115 {
116 std::string addr = data.ReadString();
117 int action = data.ReadInt32();
118 stub->OnMediaStackChanged(RawAddress(addr), action);
119 return BT_NO_ERROR;
120 }
121
OnVirtualDeviceChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)122 ErrCode BluetoothA2dpSrcObserverStub::OnVirtualDeviceChangedInner(
123 BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
124 {
125 int action = data.ReadInt32();
126 std::string addr = data.ReadString();
127
128 stub->OnVirtualDeviceChanged(action, addr);
129
130 return BT_NO_ERROR;
131 }
132
OnCaptureConnectionStateChangedInner(BluetoothA2dpSrcObserverStub * stub,MessageParcel & data,MessageParcel & reply)133 int32_t BluetoothA2dpSrcObserverStub::OnCaptureConnectionStateChangedInner(
134 BluetoothA2dpSrcObserverStub *stub, MessageParcel &data, MessageParcel &reply)
135 {
136 std::string addr = data.ReadString();
137 int state = data.ReadInt32();
138 std::shared_ptr<BluetoothA2dpCodecInfo> info(data.ReadParcelable<BluetoothA2dpCodecInfo>());
139 if (!info) {
140 return BT_ERR_IPC_TRANS_FAILED;
141 }
142 stub->OnCaptureConnectionStateChanged(RawAddress(addr), state, *info);
143 return NO_ERROR;
144 }
145 } // namespace Bluetooth
146 } // namespace OHOS