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