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_sink_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_SNK_CONNECTION_NUMS = 0x07;
BluetoothA2dpSinkStub()26 BluetoothA2dpSinkStub::BluetoothA2dpSinkStub()
27 {
28 HILOGD("%{public}s start.", __func__);
29 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_CONNECT)] =
30 nullptr;
31 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DISCONNECT)] =
32 &BluetoothA2dpSinkStub::DisconnectInner;
33 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_REGISTER_OBSERVER)] =
34 &BluetoothA2dpSinkStub::RegisterObserverInner;
35 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_DEREGISTER_OBSERVER)] =
36 &BluetoothA2dpSinkStub::DeregisterObserverInner;
37 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_BY_STATES)] =
38 &BluetoothA2dpSinkStub::GetDevicesByStatesInner;
39 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_DEVICE_STATE)] =
40 &BluetoothA2dpSinkStub::GetDeviceStateInner;
41 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_PLAYING_STATE)] =
42 &BluetoothA2dpSinkStub::GetPlayingStateInner;
43 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SET_CONNECT_STRATEGY)] =
44 &BluetoothA2dpSinkStub::SetConnectStrategyInner;
45 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_GET_CONNECT_STRATEGY)] =
46 &BluetoothA2dpSinkStub::GetConnectStrategyInner;
47 memberFuncMap_[static_cast<uint32_t>(BluetoothA2dpSinkInterfaceCode::BT_A2DP_SINK_SEND_DELAY)] =
48 &BluetoothA2dpSinkStub::SendDelayInner;
49 }
50
~BluetoothA2dpSinkStub()51 BluetoothA2dpSinkStub::~BluetoothA2dpSinkStub()
52 {
53 HILOGD("%{public}s start.", __func__);
54 memberFuncMap_.clear();
55 }
56
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)57 int BluetoothA2dpSinkStub::OnRemoteRequest(
58 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
59 {
60 HILOGI("BluetoothA2dpSinkStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
61 if (BluetoothA2dpSinkStub::GetDescriptor() != data.ReadInterfaceToken()) {
62 HILOGI("local descriptor is not equal to remote");
63 return ERR_INVALID_STATE;
64 }
65 auto itFunc = memberFuncMap_.find(code);
66 if (itFunc != memberFuncMap_.end()) {
67 auto memberFunc = itFunc->second;
68 if (memberFunc != nullptr) {
69 return (this->*memberFunc)(data, reply);
70 }
71 }
72 HILOGW("BluetoothA2dpSinkStub::OnRemoteRequest, default case, need check.");
73 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
74 }
75
ConnectInner(MessageParcel & data,MessageParcel & reply)76 ErrCode BluetoothA2dpSinkStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
77 {
78 std::string addr = data.ReadString();
79
80 int result = Connect(RawAddress(addr));
81
82 bool ret = reply.WriteInt32(result);
83 if (!ret) {
84 HILOGE("BluetoothA2dpSinkStub: ConnectInner reply writing failed in: %{public}s.", __func__);
85 return TRANSACTION_ERR;
86 }
87
88 return NO_ERROR;
89 }
90
DisconnectInner(MessageParcel & data,MessageParcel & reply)91 ErrCode BluetoothA2dpSinkStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
92 {
93 std::string addr = data.ReadString();
94
95 int result = Disconnect(RawAddress(addr));
96
97 bool ret = reply.WriteInt32(result);
98 if (!ret) {
99 HILOGE("BluetoothA2dpSinkStub: DisconnectInner reply writing failed in: %{public}s.", __func__);
100 return TRANSACTION_ERR;
101 }
102
103 return NO_ERROR;
104 }
105
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)106 ErrCode BluetoothA2dpSinkStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
107 {
108 sptr<IRemoteObject> remote = data.ReadRemoteObject();
109 const sptr<IBluetoothA2dpSinkObserver> observer = OHOS::iface_cast<IBluetoothA2dpSinkObserver>(remote);
110 RegisterObserver(observer);
111
112 return NO_ERROR;
113 }
114
DeregisterObserverInner(MessageParcel & data,MessageParcel & reply)115 ErrCode BluetoothA2dpSinkStub::DeregisterObserverInner(MessageParcel &data, MessageParcel &reply)
116 {
117 sptr<IRemoteObject> remote = data.ReadRemoteObject();
118 const sptr<IBluetoothA2dpSinkObserver> observer = OHOS::iface_cast<IBluetoothA2dpSinkObserver>(remote);
119 DeregisterObserver(observer);
120
121 return NO_ERROR;
122 }
123
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)124 ErrCode BluetoothA2dpSinkStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
125 {
126 std::vector<int32_t> states = {};
127 int32_t stateSize = data.ReadInt32();
128 if (stateSize > A2DP_MAX_SNK_CONNECTION_NUMS) {
129 return ERR_INVALID_STATE;
130 }
131
132 for (int i = 0; i < stateSize; i++) {
133 int32_t state = data.ReadInt32();
134 states.push_back(state);
135 }
136
137 std::vector<RawAddress> rawAdds = GetDevicesByStates(states);
138 reply.WriteInt32(rawAdds.size());
139
140 for (auto rawAdd : rawAdds) {
141 if (!reply.WriteString(rawAdd.GetAddress())) {
142 HILOGE("GetConnectedDevicesInner: write WriteString failed");
143 return ERR_INVALID_STATE;
144 }
145 }
146 return NO_ERROR;
147 }
148
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)149 ErrCode BluetoothA2dpSinkStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
150 {
151 std::string addr = data.ReadString();
152
153 int result = GetDeviceState(RawAddress(addr));
154
155 bool ret = reply.WriteInt32(result);
156 if (!ret) {
157 HILOGE("BluetoothA2dpSinkStub: GetDeviceStateInner reply writing failed in: %{public}s.", __func__);
158 return TRANSACTION_ERR;
159 }
160
161 return NO_ERROR;
162 }
163
GetPlayingStateInner(MessageParcel & data,MessageParcel & reply)164 ErrCode BluetoothA2dpSinkStub::GetPlayingStateInner(MessageParcel &data, MessageParcel &reply)
165 {
166 std::string addr = data.ReadString();
167 int32_t state = 0;
168 int result = GetPlayingState(RawAddress(addr), state);
169
170 (void)reply.WriteInt32(result);
171 (void)reply.WriteInt32(state);
172 return NO_ERROR;
173 }
174
SetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)175 ErrCode BluetoothA2dpSinkStub::SetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
176 {
177 std::string addr = data.ReadString();
178 int strategy = data.ReadInt32();
179
180 int result = SetConnectStrategy(RawAddress(addr), strategy);
181
182 bool ret = reply.WriteInt32(result);
183 if (!ret) {
184 HILOGE("BluetoothA2dpSinkStub: SetConnectStrategyInner reply writing failed in: %{public}s.", __func__);
185 return TRANSACTION_ERR;
186 }
187
188 return NO_ERROR;
189 }
190
GetConnectStrategyInner(MessageParcel & data,MessageParcel & reply)191 ErrCode BluetoothA2dpSinkStub::GetConnectStrategyInner(MessageParcel &data, MessageParcel &reply)
192 {
193 std::string addr = data.ReadString();
194 int result = GetConnectStrategy(RawAddress(addr));
195
196 bool ret = reply.WriteInt32(result);
197 if (!ret) {
198 HILOGE("BluetoothA2dpSinkStub: GetConnectStrategyInner reply writing failed in: %{public}s.", __func__);
199 return TRANSACTION_ERR;
200 }
201
202 return NO_ERROR;
203 }
204
SendDelayInner(MessageParcel & data,MessageParcel & reply)205 ErrCode BluetoothA2dpSinkStub::SendDelayInner(MessageParcel &data, MessageParcel &reply)
206 {
207 std::string addr = data.ReadString();
208 int delayValue = data.ReadInt32();
209
210 int result = SendDelay(RawAddress(addr), delayValue);
211
212 bool ret = reply.WriteInt32(result);
213 if (!ret) {
214 HILOGE("BluetoothA2dpSinkStub: SendDelayInner reply writing failed in: %{public}s.", __func__);
215 return TRANSACTION_ERR;
216 }
217
218 return NO_ERROR;
219 }
220 } // namespace Bluetooth
221 } // namespace OHOS