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     memberFuncMap_[static_cast<uint32_t>(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_SET_DEVICE_ABSOLUTE_VOLUME)] =
67         &BluetoothAvrcpTgStub::SetDeviceAbsoluteVolumeInner;
68     memberFuncMap_[static_cast<uint32_t>(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_SET_DEVICE_ABS_VOLUME_ABILITY)] =
69         &BluetoothAvrcpTgStub::SetDeviceAbsVolumeAbilityInner;
70     memberFuncMap_[static_cast<uint32_t>(BluetoothAvrcpTgInterfaceCode::BT_AVRCP_TG_GET_DEVICE_ABS_VOLUME_ABILITY)] =
71         &BluetoothAvrcpTgStub::GetDeviceAbsVolumeAbilityInner;
72 }
73 
~BluetoothAvrcpTgStub()74 BluetoothAvrcpTgStub::~BluetoothAvrcpTgStub()
75 {
76     HILOGD("%{public}s start.", __func__);
77     memberFuncMap_.clear();
78 }
79 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)80 int BluetoothAvrcpTgStub::OnRemoteRequest(
81     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
82 {
83     HILOGD("BluetoothAvrcpTgStub::OnRemoteRequest, cmd = %{public}d, flags= %{public}d", code, option.GetFlags());
84     std::u16string descriptor = BluetoothAvrcpTgStub::GetDescriptor();
85     std::u16string remoteDescriptor = data.ReadInterfaceToken();
86     if (descriptor != remoteDescriptor) {
87         HILOGI("local descriptor is not equal to remote");
88         return ERR_INVALID_STATE;
89     }
90     auto itFunc = memberFuncMap_.find(code);
91     if (itFunc != memberFuncMap_.end()) {
92         auto memberFunc = itFunc->second;
93         if (memberFunc != nullptr) {
94             return (this->*memberFunc)(data, reply);
95         }
96     }
97     HILOGW("BluetoothAvrcpTgStub::OnRemoteRequest, default case, need check.");
98     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
99 }
100 
RegisterObserverInner(MessageParcel & data,MessageParcel & reply)101 ErrCode BluetoothAvrcpTgStub::RegisterObserverInner(MessageParcel &data, MessageParcel &reply)
102 {
103     HILOGI("BluetoothAvrcpTgStub::RegisterObserverInner starts");
104     sptr<IRemoteObject> remote = data.ReadRemoteObject();
105     const sptr<IBluetoothAvrcpTgObserver> observer = OHOS::iface_cast<IBluetoothAvrcpTgObserver>(remote);
106 
107     RegisterObserver(observer);
108     return NO_ERROR;
109 }
110 
UnregisterObserverInner(MessageParcel & data,MessageParcel & reply)111 ErrCode BluetoothAvrcpTgStub::UnregisterObserverInner(MessageParcel &data, MessageParcel &reply)
112 {
113     HILOGI("BluetoothAvrcpTgStub::UnregisterObserverInner starts");
114     sptr<IRemoteObject> remote = data.ReadRemoteObject();
115     const sptr<IBluetoothAvrcpTgObserver> observer = OHOS::iface_cast<IBluetoothAvrcpTgObserver>(remote);
116 
117     UnregisterObserver(observer);
118     return NO_ERROR;
119 }
120 
SetActiveDeviceInner(MessageParcel & data,MessageParcel & reply)121 ErrCode BluetoothAvrcpTgStub::SetActiveDeviceInner(MessageParcel &data, MessageParcel &reply)
122 {
123     HILOGI("BluetoothAvrcpTgStub::SetActiveDeviceInner starts");
124     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
125     if (!addr) {
126         return TRANSACTION_ERR;
127     }
128     SetActiveDevice(*addr);
129     return NO_ERROR;
130 }
131 
132 
ConnectInner(MessageParcel & data,MessageParcel & reply)133 ErrCode BluetoothAvrcpTgStub::ConnectInner(MessageParcel &data, MessageParcel &reply)
134 {
135     HILOGI("BluetoothAvrcpTgStub::ConnectInner starts");
136     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
137     if (!addr) {
138         return TRANSACTION_ERR;
139     }
140 
141     int result = Connect(*addr);
142     bool ret = reply.WriteInt32(result);
143     if (!ret) {
144         HILOGE("BluetoothAvrcpTgStub: reply writing failed in: %{public}s.", __func__);
145         return ERR_INVALID_VALUE;
146     }
147     return NO_ERROR;
148 }
149 
DisconnectInner(MessageParcel & data,MessageParcel & reply)150 ErrCode BluetoothAvrcpTgStub::DisconnectInner(MessageParcel &data, MessageParcel &reply)
151 {
152     HILOGI("BluetoothAvrcpTgStub::DisconnectInner starts");
153     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
154     if (!addr) {
155         return TRANSACTION_ERR;
156     }
157 
158     int result = Disconnect(*addr);
159 
160     bool ret = reply.WriteInt32(result);
161     if (!ret) {
162         HILOGE("BluetoothAvrcpTgStub: reply writing failed in: %{public}s.", __func__);
163         return ERR_INVALID_VALUE;
164     }
165     return NO_ERROR;
166 }
167 
GetConnectedDevicesInner(MessageParcel & data,MessageParcel & reply)168 ErrCode BluetoothAvrcpTgStub::GetConnectedDevicesInner(MessageParcel &data, MessageParcel &reply)
169 {
170     HILOGI("BluetoothAvrcpTgStub::GetConnectedDevicesInner starts");
171     std::vector<BluetoothRawAddress> result = GetConnectedDevices();
172     if (!reply.WriteInt32(result.size())) {
173         HILOGE("BluetoothHostStub: reply writing failed in: %{public}s.", __func__);
174         return TRANSACTION_ERR;
175     } else {
176         for (auto device : result) {
177             reply.WriteParcelable(&device);
178         }
179     }
180     return NO_ERROR;
181 }
182 
GetDevicesByStatesInner(MessageParcel & data,MessageParcel & reply)183 ErrCode BluetoothAvrcpTgStub::GetDevicesByStatesInner(MessageParcel &data, MessageParcel &reply)
184 {
185     int32_t statesSize = data.ReadInt32();
186     if (IsInvalidAttributesSize(statesSize)) {
187         HILOGE("BluetoothAvrcpTgStub::GetDevicesByStatesInner Invalid MessageParcel device states");
188         return ERR_INVALID_VALUE;
189     }
190     std::vector<int32_t> states;
191     for (int i = 0; i < statesSize; ++i) {
192         int32_t state = data.ReadInt32();
193         states.push_back(state);
194     }
195 
196     std::vector<BluetoothRawAddress> result = GetDevicesByStates(states);
197     if (!reply.WriteInt32(result.size())) {
198         HILOGE("BluetoothHostStub: reply writing failed in: %{public}s.", __func__);
199         return TRANSACTION_ERR;
200     } else {
201         for (auto device : result) {
202             reply.WriteParcelable(&device);
203         }
204     }
205     return NO_ERROR;
206 }
207 
GetDeviceStateInner(MessageParcel & data,MessageParcel & reply)208 ErrCode BluetoothAvrcpTgStub::GetDeviceStateInner(MessageParcel &data, MessageParcel &reply)
209 {
210     HILOGI("BluetoothAvrcpTgStub::GetDeviceStateInner starts");
211     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
212     if (!addr) {
213         return TRANSACTION_ERR;
214     }
215 
216     int result = GetDeviceState(*addr);
217     bool ret = reply.WriteInt32(result);
218     if (!ret) {
219         HILOGE("BluetoothAvrcpTgStub: reply writing failed in: %{public}s.", __func__);
220         return ERR_INVALID_VALUE;
221     }
222     return NO_ERROR;
223 }
224 
NotifyPlaybackStatusChangedInner(MessageParcel & data,MessageParcel & reply)225 ErrCode BluetoothAvrcpTgStub::NotifyPlaybackStatusChangedInner(MessageParcel &data, MessageParcel &reply)
226 {
227     HILOGI("BluetoothAvrcpTgStub::NotifyPlaybackStatusChangedInner starts");
228     int32_t playStatus = data.ReadInt32();
229     int32_t playbackPos = data.ReadInt32();
230 
231     NotifyPlaybackStatusChanged(playStatus, playbackPos);
232     return NO_ERROR;
233 }
234 
NotifyTrackChangedInner(MessageParcel & data,MessageParcel & reply)235 ErrCode BluetoothAvrcpTgStub::NotifyTrackChangedInner(MessageParcel &data, MessageParcel &reply)
236 {
237     HILOGI("BluetoothAvrcpTgStub::NotifyTrackChangedInner starts");
238     long uid = data.ReadInt64();
239     int32_t playbackPos = data.ReadInt32();
240 
241     NotifyTrackChanged(uid, playbackPos);
242     return NO_ERROR;
243 }
244 
NotifyTrackReachedEndInner(MessageParcel & data,MessageParcel & reply)245 ErrCode BluetoothAvrcpTgStub::NotifyTrackReachedEndInner(MessageParcel &data, MessageParcel &reply)
246 {
247     HILOGI("BluetoothAvrcpTgStub::NotifyTrackReachedEndInner starts");
248     int32_t playbackPos = data.ReadInt32();
249 
250     NotifyTrackReachedEnd(playbackPos);
251     return NO_ERROR;
252 }
253 
NotifyTrackReachedStartInner(MessageParcel & data,MessageParcel & reply)254 ErrCode BluetoothAvrcpTgStub::NotifyTrackReachedStartInner(MessageParcel &data, MessageParcel &reply)
255 {
256     HILOGI("BluetoothAvrcpTgStub::NotifyTrackReachedStartInner starts");
257     int32_t playbackPos = data.ReadInt32();
258 
259     NotifyTrackReachedStart(playbackPos);
260     return NO_ERROR;
261 }
262 
NotifyPlaybackPosChangedInner(MessageParcel & data,MessageParcel & reply)263 ErrCode BluetoothAvrcpTgStub::NotifyPlaybackPosChangedInner(MessageParcel &data, MessageParcel &reply)
264 {
265     HILOGI("BluetoothAvrcpTgStub::NotifyPlaybackPosChangedInner starts");
266     int32_t playbackPos = data.ReadInt32();
267 
268     NotifyPlaybackPosChanged(playbackPos);
269     return NO_ERROR;
270 }
271 
NotifyPlayerAppSettingChangedInner(MessageParcel & data,MessageParcel & reply)272 ErrCode BluetoothAvrcpTgStub::NotifyPlayerAppSettingChangedInner(MessageParcel &data, MessageParcel &reply)
273 {
274     HILOGI("BluetoothAvrcpTgStub::NotifyPlayerAppSettingChangedInner starts");
275     int32_t attributesSize = data.ReadInt32();
276     if (IsInvalidAttributesSize(attributesSize)) {
277         HILOGE("BluetoothAvrcpTgStub::NotifyPlayerAppSettingChangedInner Invalid MessageParcel attributes");
278         return ERR_INVALID_VALUE;
279     }
280     std::vector<int32_t> attributes;
281     for (int i = 0; i < attributesSize; ++i) {
282         int32_t attribute = data.ReadInt32();
283         attributes.push_back(attribute);
284     }
285 
286     int32_t valuesSize = data.ReadInt32();
287     if (IsInvalidAttributesSize(valuesSize)) {
288         HILOGE("BluetoothAvrcpTgStub::NotifyPlayerAppSettingChangedInner Invalid MessageParcel values");
289         return ERR_INVALID_VALUE;
290     }
291     std::vector<int32_t> values;
292     for (int i = 0; i < valuesSize; ++i) {
293         int32_t value = data.ReadInt32();
294         values.push_back(value);
295     }
296 
297     NotifyPlayerAppSettingChanged(attributes, values);
298     return NO_ERROR;
299 }
300 
NotifyNowPlayingContentChangedInner(MessageParcel & data,MessageParcel & reply)301 ErrCode BluetoothAvrcpTgStub::NotifyNowPlayingContentChangedInner(MessageParcel &data, MessageParcel &reply)
302 {
303     HILOGI("BluetoothAvrcpTgStub::NotifyNowPlayingContentChangedInner starts");
304 
305     NotifyNowPlayingContentChanged();
306     return NO_ERROR;
307 }
308 
NotifyAvailablePlayersChangedInner(MessageParcel & data,MessageParcel & reply)309 ErrCode BluetoothAvrcpTgStub::NotifyAvailablePlayersChangedInner(MessageParcel &data, MessageParcel &reply)
310 {
311     HILOGI("BluetoothAvrcpTgStub::NotifyAvailablePlayersChangedInner starts");
312 
313     NotifyAvailablePlayersChanged();
314     return NO_ERROR;
315 }
316 
NotifyAddressedPlayerChangedInner(MessageParcel & data,MessageParcel & reply)317 ErrCode BluetoothAvrcpTgStub::NotifyAddressedPlayerChangedInner(MessageParcel &data, MessageParcel &reply)
318 {
319     HILOGI("BluetoothAvrcpTgStub::NotifyAddressedPlayerChangedInner starts");
320     int32_t playerId = data.ReadInt32();
321     int32_t uidCounter = data.ReadInt32();
322 
323     NotifyAddressedPlayerChanged(playerId, uidCounter);
324     return NO_ERROR;
325 }
326 
NotifyUidChangedInner(MessageParcel & data,MessageParcel & reply)327 ErrCode BluetoothAvrcpTgStub::NotifyUidChangedInner(MessageParcel &data, MessageParcel &reply)
328 {
329     HILOGI("BluetoothAvrcpTgStub::NotifyUidChangedInner starts");
330     int32_t uidCounter = data.ReadInt32();
331 
332     NotifyUidChanged(uidCounter);
333     return NO_ERROR;
334 }
335 
NotifyVolumeChangedInner(MessageParcel & data,MessageParcel & reply)336 ErrCode BluetoothAvrcpTgStub::NotifyVolumeChangedInner(MessageParcel &data, MessageParcel &reply)
337 {
338     HILOGI("BluetoothAvrcpTgStub::NotifyVolumeChangedInner starts");
339     int32_t volume = data.ReadInt32();
340 
341     NotifyVolumeChanged(volume);
342     return NO_ERROR;
343 }
344 
SetDeviceAbsoluteVolumeInner(MessageParcel & data,MessageParcel & reply)345 ErrCode BluetoothAvrcpTgStub::SetDeviceAbsoluteVolumeInner(MessageParcel &data, MessageParcel &reply)
346 {
347     HILOGI("enter");
348     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
349     if (!addr) {
350         return BT_ERROR_IPC_TRANS_FAILED;
351     }
352     int32_t volumeLevel = data.ReadInt32();
353 
354     int32_t result = SetDeviceAbsoluteVolume(*addr, volumeLevel);
355     if (!reply.WriteInt32(result)) {
356         return BT_ERROR_IPC_TRANS_FAILED;
357     }
358     return NO_ERROR;
359 }
SetDeviceAbsVolumeAbilityInner(MessageParcel & data,MessageParcel & reply)360 ErrCode BluetoothAvrcpTgStub::SetDeviceAbsVolumeAbilityInner(MessageParcel &data, MessageParcel &reply)
361 {
362     HILOGI("enter");
363     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
364     if (!addr) {
365         return BT_ERROR_IPC_TRANS_FAILED;
366     }
367     int32_t ability = data.ReadInt32();
368     int32_t result = SetDeviceAbsVolumeAbility(*addr, ability);
369     if (!reply.WriteInt32(result)) {
370         return BT_ERROR_IPC_TRANS_FAILED;
371     }
372     return NO_ERROR;
373 }
GetDeviceAbsVolumeAbilityInner(MessageParcel & data,MessageParcel & reply)374 ErrCode BluetoothAvrcpTgStub::GetDeviceAbsVolumeAbilityInner(MessageParcel &data, MessageParcel &reply)
375 {
376     HILOGI("enter");
377     std::shared_ptr<BluetoothRawAddress> addr(data.ReadParcelable<BluetoothRawAddress>());
378     if (!addr) {
379         return BT_ERROR_IPC_TRANS_FAILED;
380     }
381     int32_t ability = 0;
382     int32_t result = GetDeviceAbsVolumeAbility(*addr, ability);
383     if (!reply.WriteInt32(result)) {
384         return BT_ERROR_IPC_TRANS_FAILED;
385     }
386 
387     if (!reply.WriteInt32(ability)) {
388         return BT_ERROR_IPC_TRANS_FAILED;
389     }
390     return NO_ERROR;
391 }
392 
IsInvalidAttributesSize(int32_t attributesSize)393 bool BluetoothAvrcpTgStub::IsInvalidAttributesSize(int32_t attributesSize)
394 {
395     bool ret = false;
396     const int32_t COUNT_OF_AVRC_PLAYER_ATTRIBUTE = 255;
397     if (attributesSize > COUNT_OF_AVRC_PLAYER_ATTRIBUTE) {
398         ret =  true;
399     }
400     return ret;
401 }
402 
IsInvalidDeviceStatesSize(int32_t statesSize)403 bool BluetoothAvrcpTgStub::IsInvalidDeviceStatesSize(int32_t statesSize)
404 {
405     bool ret = false;
406     const int32_t COUNT_OF_DEVICE_STATE = 4;
407     if (statesSize > COUNT_OF_DEVICE_STATE) {
408         ret =  true;
409     }
410     return ret;
411 }
412 }  // namespace Bluetooth
413 }  // namespace OHOS