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_avrcp_tg_stub.h"
17 #include "bluetooth_log.h"
18 #include "ipc_types.h"
19 #include "string_ex.h"
20
21 namespace OHOS {
22 namespace Bluetooth {
BluetoothAvrcpTgStub()23 BluetoothAvrcpTgStub::BluetoothAvrcpTgStub()
24 {
25 HILOGD("%{public}s start.", __func__);
26 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_REGISTER_OBSERVER)] =
27 &BluetoothAvrcpTgStub::RegisterObserverInner;
28 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_UNREGISTER_OBSERVER)] =
29 &BluetoothAvrcpTgStub::UnregisterObserverInner;
30 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_SET_ACTIVE_DEVICE)] =
31 &BluetoothAvrcpTgStub::SetActiveDeviceInner;
32 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_CONNECT)] =
33 &BluetoothAvrcpTgStub::ConnectInner;
34 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_DISCONNECT)] =
35 &BluetoothAvrcpTgStub::DisconnectInner;
36 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_GET_CONNECTED_DEVICES)] =
37 &BluetoothAvrcpTgStub::GetConnectedDevicesInner;
38 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_GET_DEVICES_BY_STATES)] =
39 &BluetoothAvrcpTgStub::GetDevicesByStatesInner;
40 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_GET_DEVICE_STATE)] =
41 &BluetoothAvrcpTgStub::GetDeviceStateInner;
42 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_PLAYBACK_STATUS_CHANGED)] =
43 &BluetoothAvrcpTgStub::NotifyPlaybackStatusChangedInner;
44 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_TRACK_CHANGED)] =
45 &BluetoothAvrcpTgStub::NotifyTrackChangedInner;
46 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_TRACK_REACHED_END)] =
47 &BluetoothAvrcpTgStub::NotifyTrackReachedEndInner;
48 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_TRACK_REACHED_START)] =
49 &BluetoothAvrcpTgStub::NotifyTrackReachedStartInner;
50 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_PLAYBACK_POS_CHANGED)] =
51 &BluetoothAvrcpTgStub::NotifyPlaybackPosChangedInner;
52 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_PLAYER_APP_SETTING_CHANGED)] =
53 &BluetoothAvrcpTgStub::NotifyPlayerAppSettingChangedInner;
54 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_NOWPLAYING_CONTENT_CHANGED)] =
55 &BluetoothAvrcpTgStub::NotifyNowPlayingContentChangedInner;
56 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_AVAILABLE_PLAYERS_CHANGED)] =
57 &BluetoothAvrcpTgStub::NotifyAvailablePlayersChangedInner;
58 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_ADDRESSED_PLAYER_CHANGED)] =
59 &BluetoothAvrcpTgStub::NotifyAddressedPlayerChangedInner;
60 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_UID_CHANGED)] =
61 &BluetoothAvrcpTgStub::NotifyUidChangedInner;
62 memberFuncMap_[static_cast<uint32_t>(IBluetoothAvrcpTg::Code::BT_AVRCP_TG_NOTIFY_VOLUME_CHANGED)] =
63 &BluetoothAvrcpTgStub::NotifyVolumeChangedInner;
64 }
65
~BluetoothAvrcpTgStub()66 BluetoothAvrcpTgStub::~BluetoothAvrcpTgStub()
67 {
68 HILOGD("%{public}s start.", __func__);
69 memberFuncMap_.clear();
70 }
71
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)72 int BluetoothAvrcpTgStub::OnRemoteRequest(
73 uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
74 {
75 HILOGD("BluetoothAvrcpTgStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
76 std::u16string descriptor = BluetoothAvrcpTgStub::GetDescriptor();
77 std::u16string remoteDescriptor = data.ReadInterfaceToken();
78 if (descriptor != remoteDescriptor) {
79 HILOGI("local descriptor is not equal to remote");
80 return ERR_INVALID_STATE;
81 }
82 auto itFunc = memberFuncMap_.find(code);
83 if (itFunc != memberFuncMap_.end()) {
84 auto memberFunc = itFunc->second;
85 if (memberFunc != nullptr) {
86 return (this->*memberFunc)(data, reply);
87 }
88 }
89 HILOGW("BluetoothAvrcpTgStub::OnRemoteRequest, default case, need check.");
90 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
91 }
92
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)93 ErrCode BluetoothAvrcpTgStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
94 {
95 HILOGI("BluetoothAvrcpTgStub::RegisterObserverInner starts");
96 sptr<IRemoteObject> remote = data.ReadRemoteObject();
97 const sptr<IBluetoothAvrcpTgObserver> observer = OHOS::iface_cast<IBluetoothAvrcpTgObserver>(remote);
98
99 RegisterObserver(observer);
100 return NO_ERROR;
101 }
102
UnregisterObserverInner(MessageParcel & data,MessageParcel & reply)103 ErrCode BluetoothAvrcpTgStub::UnregisterObserverInner(MessageParcel &data, MessageParcel &reply)
104 {
105 HILOGI("BluetoothAvrcpTgStub::UnregisterObserverInner starts");
106 sptr<IRemoteObject> remote = data.ReadRemoteObject();
107 const sptr<IBluetoothAvrcpTgObserver> observer = OHOS::iface_cast<IBluetoothAvrcpTgObserver>(remote);
108
109 UnregisterObserver(observer);
110 return NO_ERROR;
111 }
112
SetActiveDeviceInner(MessageParcel & data,MessageParcel & reply)113 ErrCode BluetoothAvrcpTgStub::SetActiveDeviceInner(MessageParcel &data, MessageParcel &reply)
114 {
115 HILOGI("BluetoothAvrcpTgStub::SetActiveDeviceInner starts");
116 const BluetoothRawAddress *addr = data.ReadParcelable<BluetoothRawAddress>();
117
118 SetActiveDevice(*addr);
119 return NO_ERROR;
120 }
121
122
ConnectInner(MessageParcel & data,MessageParcel & reply)123 ErrCode BluetoothAvrcpTgStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
124 {
125 HILOGI("BluetoothAvrcpTgStub::ConnectInner starts");
126 const BluetoothRawAddress *addr = data.ReadParcelable<BluetoothRawAddress>();
127
128 int result = Connect(*addr);
129 bool ret = reply.WriteInt32(result);
130 if (!ret) {
131 HILOGE("BluetoothAvrcpTgStub: reply writing failed in: %{public}s.", __func__);
132 return ERR_INVALID_VALUE;
133 }
134 return NO_ERROR;
135 }
136
DisconnectInner(MessageParcel & data,MessageParcel & reply)137 ErrCode BluetoothAvrcpTgStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
138 {
139 HILOGI("BluetoothAvrcpTgStub::DisconnectInner starts");
140 const BluetoothRawAddress *addr = data.ReadParcelable<BluetoothRawAddress>();
141
142 int result = Disconnect(*addr);
143 bool ret = reply.WriteInt32(result);
144 if (!ret) {
145 HILOGE("BluetoothAvrcpTgStub: reply writing failed in: %{public}s.", __func__);
146 return ERR_INVALID_VALUE;
147 }
148 return NO_ERROR;
149 }
150
GetConnectedDevicesInner(MessageParcel & data,MessageParcel & reply)151 ErrCode BluetoothAvrcpTgStub::GetConnectedDevicesInner(MessageParcel &data, MessageParcel &reply)
152 {
153 HILOGI("BluetoothAvrcpTgStub::GetConnectedDevicesInner starts");
154 std::vector<sptr<BluetoothRawAddress>> result = GetConnectedDevices();
155 if (!reply.WriteInt32(result.size())) {
156 HILOGE("BluetoothHostStub: reply writing failed in: %{public}s.", __func__);
157 return TRANSACTION_ERR;
158 } else {
159 for (auto device : result) {
160 reply.WriteStrongParcelable(device);
161 }
162 }
163 return NO_ERROR;
164 }
165
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)166 ErrCode BluetoothAvrcpTgStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
167 {
168 int32_t statesSize = data.ReadInt32();
169 std::vector<int32_t> states;
170 for (int i = 0; i < statesSize; ++i) {
171 int32_t state = data.ReadInt32();
172 states.push_back(state);
173 }
174
175 std::vector<sptr<BluetoothRawAddress>> result = GetDevicesByStates(states);
176 if (!reply.WriteInt32(result.size())) {
177 HILOGE("BluetoothHostStub: reply writing failed in: %{public}s.", __func__);
178 return TRANSACTION_ERR;
179 } else {
180 for (auto device : result) {
181 reply.WriteStrongParcelable(device);
182 }
183 }
184 return NO_ERROR;
185 }
186
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)187 ErrCode BluetoothAvrcpTgStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
188 {
189 HILOGI("BluetoothAvrcpTgStub::GetDeviceStateInner starts");
190 const BluetoothRawAddress *addr = data.ReadParcelable<BluetoothRawAddress>();
191
192 int result = GetDeviceState(*addr);
193 bool ret = reply.WriteInt32(result);
194 if (!ret) {
195 HILOGE("BluetoothAvrcpTgStub: reply writing failed in: %{public}s.", __func__);
196 return ERR_INVALID_VALUE;
197 }
198 return NO_ERROR;
199 }
200
NotifyPlaybackStatusChangedInner(MessageParcel & data,MessageParcel & reply)201 ErrCode BluetoothAvrcpTgStub::NotifyPlaybackStatusChangedInner(MessageParcel &data, MessageParcel &reply)
202 {
203 HILOGI("BluetoothAvrcpTgStub::NotifyPlaybackStatusChangedInner starts");
204 int32_t playStatus = data.ReadInt32();
205 int32_t playbackPos = data.ReadInt32();
206
207 NotifyPlaybackStatusChanged(playStatus, playbackPos);
208 return NO_ERROR;
209 }
210
NotifyTrackChangedInner(MessageParcel & data,MessageParcel & reply)211 ErrCode BluetoothAvrcpTgStub::NotifyTrackChangedInner(MessageParcel &data, MessageParcel &reply)
212 {
213 HILOGI("BluetoothAvrcpTgStub::NotifyTrackChangedInner starts");
214 long uid = data.ReadInt64();
215 int32_t playbackPos = data.ReadInt32();
216
217 NotifyTrackChanged(uid, playbackPos);
218 return NO_ERROR;
219 }
220
NotifyTrackReachedEndInner(MessageParcel & data,MessageParcel & reply)221 ErrCode BluetoothAvrcpTgStub::NotifyTrackReachedEndInner(MessageParcel &data, MessageParcel &reply)
222 {
223 HILOGI("BluetoothAvrcpTgStub::NotifyTrackReachedEndInner starts");
224 int32_t playbackPos = data.ReadInt32();
225
226 NotifyTrackReachedEnd(playbackPos);
227 return NO_ERROR;
228 }
229
NotifyTrackReachedStartInner(MessageParcel & data,MessageParcel & reply)230 ErrCode BluetoothAvrcpTgStub::NotifyTrackReachedStartInner(MessageParcel &data, MessageParcel &reply)
231 {
232 HILOGI("BluetoothAvrcpTgStub::NotifyTrackReachedStartInner starts");
233 int32_t playbackPos = data.ReadInt32();
234
235 NotifyTrackReachedStart(playbackPos);
236 return NO_ERROR;
237 }
238
NotifyPlaybackPosChangedInner(MessageParcel & data,MessageParcel & reply)239 ErrCode BluetoothAvrcpTgStub::NotifyPlaybackPosChangedInner(MessageParcel &data, MessageParcel &reply)
240 {
241 HILOGI("BluetoothAvrcpTgStub::NotifyPlaybackPosChangedInner starts");
242 int32_t playbackPos = data.ReadInt32();
243
244 NotifyPlaybackPosChanged(playbackPos);
245 return NO_ERROR;
246 }
247
NotifyPlayerAppSettingChangedInner(MessageParcel & data,MessageParcel & reply)248 ErrCode BluetoothAvrcpTgStub::NotifyPlayerAppSettingChangedInner(MessageParcel &data, MessageParcel &reply)
249 {
250 HILOGI("BluetoothAvrcpTgStub::NotifyPlayerAppSettingChangedInner starts");
251 int32_t attributesSize = data.ReadInt32();
252 std::vector<int32_t> attributes;
253 for (int i = 0; i < attributesSize; ++i) {
254 int32_t attribute = data.ReadInt32();
255 attributes.push_back(attribute);
256 }
257
258 int32_t valuesSize = data.ReadInt32();
259 std::vector<int32_t> values;
260 for (int i = 0; i < valuesSize; ++i) {
261 int32_t value = data.ReadInt32();
262 values.push_back(value);
263 }
264
265 NotifyPlayerAppSettingChanged(attributes, values);
266 return NO_ERROR;
267 }
268
NotifyNowPlayingContentChangedInner(MessageParcel & data,MessageParcel & reply)269 ErrCode BluetoothAvrcpTgStub::NotifyNowPlayingContentChangedInner(MessageParcel &data, MessageParcel &reply)
270 {
271 HILOGI("BluetoothAvrcpTgStub::NotifyNowPlayingContentChangedInner starts");
272
273 NotifyNowPlayingContentChanged();
274 return NO_ERROR;
275 }
276
NotifyAvailablePlayersChangedInner(MessageParcel & data,MessageParcel & reply)277 ErrCode BluetoothAvrcpTgStub::NotifyAvailablePlayersChangedInner(MessageParcel &data, MessageParcel &reply)
278 {
279 HILOGI("BluetoothAvrcpTgStub::NotifyAvailablePlayersChangedInner starts");
280
281 NotifyAvailablePlayersChanged();
282 return NO_ERROR;
283 }
284
NotifyAddressedPlayerChangedInner(MessageParcel & data,MessageParcel & reply)285 ErrCode BluetoothAvrcpTgStub::NotifyAddressedPlayerChangedInner(MessageParcel &data, MessageParcel &reply)
286 {
287 HILOGI("BluetoothAvrcpTgStub::NotifyAddressedPlayerChangedInner starts");
288 int32_t playerId = data.ReadInt32();
289 int32_t uidCounter = data.ReadInt32();
290
291 NotifyAddressedPlayerChanged(playerId, uidCounter);
292 return NO_ERROR;
293 }
294
NotifyUidChangedInner(MessageParcel & data,MessageParcel & reply)295 ErrCode BluetoothAvrcpTgStub::NotifyUidChangedInner(MessageParcel &data, MessageParcel &reply)
296 {
297 HILOGI("BluetoothAvrcpTgStub::NotifyUidChangedInner starts");
298 int32_t uidCounter = data.ReadInt32();
299
300 NotifyUidChanged(uidCounter);
301 return NO_ERROR;
302 }
303
NotifyVolumeChangedInner(MessageParcel & data,MessageParcel & reply)304 ErrCode BluetoothAvrcpTgStub::NotifyVolumeChangedInner(MessageParcel &data, MessageParcel &reply)
305 {
306 HILOGI("BluetoothAvrcpTgStub::NotifyVolumeChangedInner starts");
307 int32_t volume = data.ReadInt32();
308
309 NotifyVolumeChanged(volume);
310 return NO_ERROR;
311 }
312
313 } // namespace Bluetooth
314 } // namespace OHOS