/* * Copyright (C) 2021 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef LOG_TAG #define LOG_TAG "bt_fwk_avrcp_tg" #endif #include #include #include #include "bluetooth_avrcp_ct.h" #include "bluetooth_avrcp_ct_observer_stub.h" #include "bluetooth_def.h" #include "bluetooth_host.h" #include "bluetooth_profile_manager.h" #include "bluetooth_log.h" #include "bluetooth_utils.h" #include "bluetooth_observer_list.h" #include "i_bluetooth_avrcp_ct.h" #include "iservice_registry.h" #include "system_ability_definition.h" namespace OHOS { namespace Bluetooth { using namespace OHOS::bluetooth; std::mutex g_avrcpProxyMutex; AvrcpCtResponse::AvrcpCtResponse(uint8_t type, int resp) : type_(type), resp_(resp) { HILOGI("enter"); } AvrcpCtResponse::~AvrcpCtResponse() { HILOGI("enter"); } struct AvrcpController::impl { public: class ObserverImpl : public BluetoothAvrcpCtObserverStub { public: explicit ObserverImpl(AvrcpController::impl *impl) : impl_(impl) {} ~ObserverImpl() override = default; void OnConnectionStateChanged(const RawAddress &rawAddr, int state, int cause) override { HILOGD("enter, address: %{public}s, state: %{public}d, cause: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), state, cause); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnConnectionStateChanged(device, static_cast(state), cause); return; } void OnPressButton(const RawAddress &rawAddr, uint8_t button, int result) override { HILOGI("enter, address: %{public}s, button: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), button, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnPressButton(device, static_cast(button), static_cast(result)); return; } void OnReleaseButton(const RawAddress &rawAddr, uint8_t button, int result) override { HILOGI("enter, address: %{public}s, button: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), button, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnReleaseButton(device, static_cast(button), static_cast(result)); return; } void OnSetBrowsedPlayer(const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numberOfItems, const std::vector &folderNames, int result, int detail) override { HILOGI("enter, address: %{public}s, res: %{public}d, detail: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnSetBrowsedPlayer(device, static_cast(uidCounter), static_cast(numberOfItems), folderNames, result, detail); return; } void OnGetCapabilities(const RawAddress &rawAddr, const std::vector &companies, const std::vector &events, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetCapabilities(device, companies, events, result); return; } void OnGetPlayerAppSettingAttributes( const RawAddress &rawAddr, std::vector attributes, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetPlayerAppSettingAttributes(device, attributes, static_cast(result)); return; } void OnGetPlayerAppSettingValues(const RawAddress &rawAddr, int attribute, const std::vector &values, int result) override { HILOGI("enter, address: %{public}s, attribute: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), attribute, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetPlayerAppSettingValues( device, static_cast(attribute), values, static_cast(result)); return; } void OnGetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, const std::vector attributes, const std::vector &values, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetPlayerAppSettingCurrentValue(device, attributes, values, static_cast(result)); return; } void OnSetPlayerAppSettingCurrentValue(const RawAddress &rawAddr, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnSetPlayerAppSettingCurrentValue(device, static_cast(result)); return; } void OnGetPlayerAppSettingAttributeText(const RawAddress &rawAddr, const std::vector attribtues, const std::vector &attributeName, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetPlayerAppSettingAttributeText(device, attribtues, attributeName, static_cast(result)); return; } void OnGetPlayerAppSettingValueText(const RawAddress &rawAddr, const std::vector &values, const std::vector &valueName, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetPlayerAppSettingValueText(device, values, valueName, static_cast(result)); return; } void OnGetElementAttributes(const RawAddress &rawAddr, const std::vector &attribtues, const std::vector &valueName, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetElementAttributes(device, attribtues, valueName, static_cast(result)); return; } void OnGetPlayStatus(const RawAddress &rawAddr, uint32_t songLength, uint32_t songPosition, uint8_t playStatus, int result) override { HILOGI("enter, address: %{public}s, status: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), playStatus, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetPlayStatus(device, static_cast(songLength), static_cast(songPosition), static_cast(playStatus), static_cast(result)); return; } void OnPlayItem(const RawAddress &rawAddr, int status, int result) override { HILOGI("enter, address: %{public}s, status: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), status, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnPlayItem(device, static_cast(status), static_cast(result)); return; } void OnGetMediaPlayers(const RawAddress &rawAddr, uint16_t uidCounter, std::vector &items, int result, int detail) override { HILOGI("enter, address: %{public}s, uidCounter: %{public}d, res: %{public}d, detail: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, result, detail); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); std::vector myItems; for (size_t i = 0; i < items.size(); i++) { AvrcMpItem myItem; myItem.itemType_ = items.at(i).itemType_; myItem.playerId_ = items.at(i).playerId_; myItem.majorType_ = items.at(i).majorType_; myItem.subType_ = items.at(i).subType_; myItem.playStatus_ = items.at(i).playStatus_; myItem.features_ = items.at(i).features_; myItem.name_ = items.at(i).name_; myItems.push_back(myItem); } impl_->OnGetMediaPlayers( device, static_cast(uidCounter), myItems, static_cast(result), static_cast(detail)); return; } void OnGetFolderItems(const RawAddress &rawAddr, uint16_t uidCounter, std::vector &items, int result, int detail) override { BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); std::vector myItems; for (size_t i = 0; i < items.size(); i++) { AvrcMeItem myItem; myItem.uid_ = items.at(i).uid_; myItem.type_ = items.at(i).type_; myItem.playable_ = items.at(i).playable_; myItem.name_ = items.at(i).name_; myItem.attributes_ = items.at(i).attributes_; myItem.values_ = items.at(i).values_; myItems.push_back(myItem); } impl_->OnGetFolderItems( device, static_cast(uidCounter), myItems, static_cast(result), static_cast(detail)); return; } void OnGetItemAttributes(const RawAddress &rawAddr, const std::vector &attribtues, const std::vector &values, int result, int detail) override { HILOGI("enter, address: %{public}s, res: %{public}d, detail: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result, detail); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetItemAttributes(device, attribtues, values, static_cast(result), static_cast(detail)); return; } void OnGetTotalNumberOfItems(const RawAddress &rawAddr, uint16_t uidCounter, uint32_t numOfItems, int result, int detail) override { HILOGI("address: %{public}s, uidCounter: %{public}d, numOfItems: %{public}d, res: %{public}d, " "detail: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, numOfItems, result, detail); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnGetTotalNumberOfItems(device, static_cast(uidCounter), static_cast(numOfItems), static_cast(result), static_cast(detail)); return; } void OnSetAbsoluteVolume(const RawAddress &rawAddr, uint8_t volume, int result) override { HILOGI("enter, address: %{public}s, volume: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), volume, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnSetAbsoluteVolume(device, static_cast(volume), result); return; } void OnPlaybackStatusChanged(const RawAddress &rawAddr, uint8_t playStatus, int result) override { HILOGI("enter, address: %{public}s, status: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), playStatus, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnPlaybackStatusChanged(device, static_cast(playStatus), static_cast(result)); return; } void OnTrackChanged(const RawAddress &rawAddr, uint64_t uid, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnTrackChanged(device, static_cast(uid), static_cast(result)); return; } void OnTrackReachedEnd(const RawAddress &rawAddr, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnTrackReachedEnd(device, static_cast(result)); return; } void OnTrackReachedStart(const RawAddress &rawAddr, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnTrackReachedStart(device, static_cast(result)); return; } void OnPlaybackPosChanged(const RawAddress &rawAddr, uint32_t playbackPos, int result) override { HILOGI("enter, address: %{public}s, playbackPos: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), playbackPos, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnPlaybackPosChanged(device, static_cast(playbackPos), static_cast(result)); return; } void OnPlayerAppSettingChanged(const RawAddress &rawAddr, const std::vector &attributes, const std::vector &values, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnPlayerAppSettingChanged(device, attributes, values, static_cast(result)); return; } void OnNowPlayingContentChanged(const RawAddress &rawAddr, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnNowPlayingContentChanged(device, static_cast(result)); return; } void OnAvailablePlayersChanged(const RawAddress &rawAddr, int result) override { HILOGI("enter, address: %{public}s, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnAvailablePlayersChanged(device, static_cast(result)); return; } void OnAddressedPlayerChanged( const RawAddress &rawAddr, uint16_t playerId, uint16_t uidCounter, int result) override { HILOGI("enter, address: %{public}s, playerId: %{public}d, uidCounter: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), playerId, uidCounter, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnAddressedPlayerChanged( device, static_cast(playerId), static_cast(uidCounter), static_cast(result)); return; } void OnUidChanged(const RawAddress &rawAddr, uint16_t uidCounter, int result) override { HILOGI("enter, address: %{public}s, uidCounter: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), uidCounter, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnUidChanged(device, static_cast(uidCounter), static_cast(result)); return; } void OnVolumeChanged(const RawAddress &rawAddr, uint8_t volume, int result) override { HILOGI("enter, address: %{public}s, volume: %{public}d, res: %{public}d", GET_ENCRYPT_RAW_ADDR(rawAddr), volume, result); BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); impl_->OnVolumeChanged(device, static_cast(volume), static_cast(result)); return; } private: AvrcpController::impl *impl_; }; impl(); ~impl() { HILOGI("enter"); BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId); sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy"); proxy->UnregisterObserver(observer_); } void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause) { HILOGI("enter, device: %{public}s, state: %{public}d, cause: %{public}d", GET_ENCRYPT_ADDR(device), state, cause); std::lock_guard lock(observerMutex_); observers_.ForEach([device, state, cause](std::shared_ptr observer) { observer->OnConnectionStateChanged(device, state, cause); }); } void OnPressButton(const BluetoothRemoteDevice &device, uint8_t button, int result) { HILOGI("enter, device: %{public}s, button: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), button, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, button, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_PRESS_BUTTON, result); resp.button_ = std::make_unique(button); observer->OnActionCompleted(device, resp); }); } void OnReleaseButton(const BluetoothRemoteDevice &device, uint8_t button, int result) { HILOGI("enter, device: %{public}s, button: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), button, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, button, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_RELEASE_BUTTON, result); resp.button_ = std::make_unique(button); observer->OnActionCompleted(device, resp); }); } void OnSetBrowsedPlayer(const BluetoothRemoteDevice &device, uint16_t uidCounter, uint32_t numOfItems, const std::vector &folderNames, int result, int detail) { HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d", GET_ENCRYPT_ADDR(device), result, detail); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_SET_BROWSED_PLAYER, result); observer->OnActionCompleted(device, resp); }); } void OnGetCapabilities(const BluetoothRemoteDevice &device, const std::vector &companies, const std::vector &events, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, companies, events, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_CAPABILITIES, result); if (companies.size() == 0) { resp.capabilities_ = std::make_unique(events); } else { resp.capabilities_ = std::make_unique(companies); } observer->OnActionCompleted(device, resp); }); } void OnGetPlayerAppSettingAttributes( const BluetoothRemoteDevice &device, std::vector attributes, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, attributes, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_ATTRIBUTES, result); resp.playerAttrs_ = std::make_unique(attributes); observer->OnActionCompleted(device, resp); }); } void OnGetPlayerAppSettingValues( const BluetoothRemoteDevice &device, uint8_t attribute, std::vector values, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, attribute, values, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_VALUES, result); resp.playerVals_ = std::make_unique(attribute, values); observer->OnActionCompleted(device, resp); }); } void OnGetPlayerAppSettingCurrentValue( const BluetoothRemoteDevice &device, std::vector attributes, std::vector values, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, attributes, values, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_CURRENT_VALUE, result); resp.playerCurVal_ = std::make_unique(attributes, values); observer->OnActionCompleted(device, resp); }); } void OnSetPlayerAppSettingCurrentValue(const BluetoothRemoteDevice &device, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_SET_PLAYER_APP_SETTING_CURRENT_VALUE, result); observer->OnActionCompleted(device, resp); }); } void OnGetPlayerAppSettingAttributeText(const BluetoothRemoteDevice &device, const std::vector &attributes, const std::vector &valueName, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, attributes, valueName, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_ATTRIBUTE_TEXT, result); resp.playerText_ = std::make_unique(attributes, valueName); observer->OnActionCompleted(device, resp); }); } void OnGetPlayerAppSettingValueText(const BluetoothRemoteDevice &device, const std::vector &values, const std::vector &valueName, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result, values, valueName](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAYER_APP_SETTING_VALUE_TEXT, result); resp.playerText_ = std::make_unique(values, valueName); observer->OnActionCompleted(device, resp); }); } void OnGetElementAttributes(const BluetoothRemoteDevice &device, const std::vector &attributes, const std::vector &valueName, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result, attributes, valueName](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_ELEMENT_ATTRIBUTES, result); resp.eleSts_ = std::make_unique(attributes, valueName); observer->OnActionCompleted(device, resp); }); } void OnGetPlayStatus( const BluetoothRemoteDevice &device, uint32_t songLength, uint32_t songPosition, uint8_t playStatus, int result) { HILOGI("enter, device: %{public}s, songLength: %{public}d, songPosition: %{public}d, playStatus: %{public}d," " res: %{public}d", GET_ENCRYPT_ADDR(device), songLength, songPosition, playStatus, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_PLAY_STATUS, result); observer->OnActionCompleted(device, resp); }); } void OnPlayItem(const BluetoothRemoteDevice &device, int result, int detail) { HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d", GET_ENCRYPT_ADDR(device), result, detail); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_PLAY_ITEM, result); observer->OnActionCompleted(device, resp); }); } void OnGetMediaPlayers(const BluetoothRemoteDevice &device, uint16_t uidCounter, const std::vector &items, int result, int detail) { HILOGI("enter, device: %{public}s, uidCounter: %{public}d, res: %{public}d, detail: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter, result, detail); std::vector MediaPlayers; for (int i = 0; i < static_cast(items.size()); i++) { AvrcpCtResponse::MediaPlayers::MediaPlayer mediaPlayer; mediaPlayer.itemType_ = items.at(i).itemType_; mediaPlayer.playerId_ = items.at(i).playerId_; mediaPlayer.majorType_ = items.at(i).majorType_; mediaPlayer.subType_ = items.at(i).subType_; mediaPlayer.playStatus_ = items.at(i).playStatus_; mediaPlayer.features_ = items.at(i).features_; mediaPlayer.name_ = items.at(i).name_; MediaPlayers.push_back(mediaPlayer); } std::lock_guard lock(observerMutex_); observers_.ForEach([device, result, MediaPlayers, uidCounter](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_MEDIA_PLAYER_LIST, result); resp.mediaPlayers_ = std::make_unique(uidCounter, MediaPlayers); observer->OnActionCompleted(device, resp); }); } void OnGetFolderItems(const BluetoothRemoteDevice &device, uint16_t uidCounter, const std::vector &items, int result, int detail) { HILOGI("enter, device: %{public}s, uidCounter: %{public}d, res: %{public}d, detail: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter, result, detail); std::vector mediaItems; for (int i = 0; i < static_cast(items.size()); i++) { AvrcpCtResponse::MediaItems::MediaItem mediaItem; mediaItem.uid_ = items.at(i).uid_; mediaItem.type_ = items.at(i).type_; mediaItem.playable_ = items.at(i).playable_; mediaItem.name_ = items.at(i).name_; mediaItem.attributes_ = items.at(i).attributes_; mediaItem.values_ = items.at(i).values_; mediaItems.push_back(mediaItem); } std::lock_guard lock(observerMutex_); observers_.ForEach([device, result, mediaItems, uidCounter](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_FOLDER_ITEMS, result); resp.mediaItems_ = std::make_unique(uidCounter, mediaItems); observer->OnActionCompleted(device, resp); }); } void OnGetItemAttributes(const BluetoothRemoteDevice &device, const std::vector &attributes, const std::vector &values, int result, int detail) { HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d", GET_ENCRYPT_ADDR(device), result, detail); std::vector itemAttrs; for (int i = 0; i < static_cast(attributes.size()); i++) { AvrcpCtResponse::ItemAttributes::ItemAttribute itemAttr; itemAttr.attribute_ = attributes.at(i); itemAttr.value_ = values.at(i); itemAttrs.push_back(itemAttr); } std::lock_guard lock(observerMutex_); observers_.ForEach([device, result, itemAttrs](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_ITEM_ATTRIBUTES, result); resp.itemAttrs_ = std::make_unique(itemAttrs); observer->OnActionCompleted(device, resp); }); } void OnGetTotalNumberOfItems( const BluetoothRemoteDevice &device, uint16_t uidCounter, uint32_t numOfItems, int result, int detail) { HILOGI("enter, device: %{public}s, res: %{public}d, detail: %{public}d", GET_ENCRYPT_ADDR(device), result, detail); std::lock_guard lock(observerMutex_); observers_.ForEach([device, uidCounter, numOfItems, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_GET_TOTAL_NUMBER_OF_ITEMS, result); resp.totalItems_ = std::make_unique(uidCounter, numOfItems); observer->OnActionCompleted(device, resp); }); } void OnSetAbsoluteVolume(const BluetoothRemoteDevice &device, uint16_t volume, int result) { HILOGI("enter, device: %{public}s, volume: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), volume, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, volume, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_SET_ABSOLUTE_VOLUME, result); resp.absVolume_ = std::make_unique(volume); observer->OnActionCompleted(device, resp); }); } void OnPlaybackStatusChanged(const BluetoothRemoteDevice &device, uint8_t playStatus, int result) { HILOGI("enter, device: %{public}s, playStatus: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), playStatus, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, playStatus, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_STATUS_CHANGED, result); resp.notify_ = std::make_unique(playStatus, 0x00); observer->OnActionCompleted(device, resp); }); } void OnTrackChanged(const BluetoothRemoteDevice &device, uint64_t uid, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, uid, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_TRACK_CHANGED, result); resp.notify_ = std::make_unique(uid); observer->OnActionCompleted(device, resp); }); } void OnTrackReachedEnd(const BluetoothRemoteDevice &device, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_TRACK_REACHED_END, result); observer->OnActionCompleted(device, resp); }); } void OnTrackReachedStart(const BluetoothRemoteDevice &device, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_TRACK_REACHED_START, result); observer->OnActionCompleted(device, resp); }); } void OnPlaybackPosChanged(const BluetoothRemoteDevice &device, uint32_t playbackPos, int result) { HILOGI("enter, device: %{public}s, playbackPos: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), playbackPos, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, playbackPos, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_PLAYBACK_POS_CHANGED, result); resp.notify_ = std::make_unique(playbackPos); observer->OnActionCompleted(device, resp); }); } void OnPlayerAppSettingChanged(const BluetoothRemoteDevice &device, const std::vector &attributes, const std::vector &values, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, attributes, values, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_PLAYER_APPLICATION_SETTING_CHANGED, result); resp.notify_ = std::make_unique(attributes, values); observer->OnActionCompleted(device, resp); }); } void OnNowPlayingContentChanged(const BluetoothRemoteDevice &device, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_NOW_PLAYING_CONTENT_CHANGED, result); observer->OnActionCompleted(device, resp); }); } void OnAvailablePlayersChanged(const BluetoothRemoteDevice &device, int result) { HILOGI("enter, device: %{public}s, res: %{public}d", GET_ENCRYPT_ADDR(device), result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_AVAILABLE_PLAYERS_CHANGED, result); observer->OnActionCompleted(device, resp); }); } void OnAddressedPlayerChanged( const BluetoothRemoteDevice &device, uint16_t playerId, uint16_t uidCounter, int result) { HILOGI("enter, device: %{public}s, playerId: %{public}d, uidCounter: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), playerId, uidCounter, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, uidCounter, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_ADDRESSED_PLAYER_CHANGED, result); resp.notify_ = std::make_unique(uidCounter); observer->OnActionCompleted(device, resp); }); } void OnUidChanged(const BluetoothRemoteDevice &device, uint16_t uidCounter, int result) { HILOGI("enter, device: %{public}s, uidCounter: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, result, uidCounter](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_UIDS_CHANGED, result); resp.notify_ = std::make_unique(uidCounter); observer->OnActionCompleted(device, resp); }); } void OnVolumeChanged(const BluetoothRemoteDevice &device, uint8_t volume, int result) { HILOGI("enter, device: %{public}s, volume: %{public}d, res: %{public}d", GET_ENCRYPT_ADDR(device), volume, result); std::lock_guard lock(observerMutex_); observers_.ForEach([device, volume, result](std::shared_ptr observer) { AvrcpCtResponse resp(AVRC_ACTION_TYPE_NOTIFY_VOLUME_CHANGED, result); resp.notify_ = std::make_unique(0x00, volume); observer->OnActionCompleted(device, resp); }); } std::mutex observerMutex_; BluetoothObserverList observers_; sptr observer_; int32_t profileRegisterId = 0; }; AvrcpController::impl::impl() { observer_ = new (std::nothrow) ObserverImpl(this); CHECK_AND_RETURN_LOG(observer_ != nullptr, "observer_ is nullptr"); profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_AVRCP_CT, [this](sptr remote) { sptr proxy = iface_cast(remote); CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy"); proxy->RegisterObserver(observer_); }); } AvrcpController *AvrcpController::GetProfile(void) { HILOGI("enter"); #ifdef DTFUZZ_TEST static BluetoothNoDestructor instance; return instance.get(); #else static AvrcpController instance; return &instance; #endif } /****************************************************************** * REGISTER / UNREGISTER OBSERVER * ******************************************************************/ void AvrcpController::RegisterObserver(std::shared_ptr observer) { HILOGD("enter"); std::lock_guard lock(pimpl->observerMutex_); CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null."); pimpl->observers_.Register(observer); } void AvrcpController::UnregisterObserver(std::shared_ptr observer) { HILOGD("enter"); std::lock_guard lock(pimpl->observerMutex_); CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null."); pimpl->observers_.Deregister(observer); } /****************************************************************** * CONNECTION * ******************************************************************/ std::vector AvrcpController::GetConnectedDevices(void) { HILOGI("enter"); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return std::vector(); } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector(), "failed: no proxy"); std::lock_guard lock(pimpl->observerMutex_); std::vector devices; std::vector rawAddrs = proxy->GetConnectedDevices(); for (auto rawAddr : rawAddrs) { BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); devices.push_back(device); } return devices; } std::vector AvrcpController::GetDevicesByStates(const std::vector &states) { HILOGI("enter"); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return std::vector(); } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector(), "failed: no proxy"); std::vector devices; if (proxy != nullptr) { std::vector convertStates; for (auto state : states) { convertStates.push_back(static_cast(state)); } std::vector rawAddrs = proxy->GetDevicesByStates(convertStates); for (auto rawAddr : rawAddrs) { BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR); devices.push_back(device); } } return devices; } int AvrcpController::GetDeviceState(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return static_cast(BTConnectState::DISCONNECTED); } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, static_cast(BTConnectState::DISCONNECTED), "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetDeviceState(rawAddr); } bool AvrcpController::Connect(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return false; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); int result = proxy->Connect(rawAddr); return result == RET_NO_ERROR; } bool AvrcpController::Disconnect(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return false; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, false, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); int result = proxy->Disconnect(rawAddr); return result == RET_NO_ERROR; } /****************************************************************** * BUTTON OPERATION * ******************************************************************/ int AvrcpController::PressButton(const BluetoothRemoteDevice &device, uint8_t button) { HILOGI("enter, device: %{public}s, button: %{public}d", GET_ENCRYPT_ADDR(device), button); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_BAD_STATUS; switch (button) { case AVRC_KEY_OPERATION_VOLUME_UP: case AVRC_KEY_OPERATION_VOLUME_DOWN: case AVRC_KEY_OPERATION_MUTE: case AVRC_KEY_OPERATION_PLAY: case AVRC_KEY_OPERATION_STOP: case AVRC_KEY_OPERATION_PAUSE: case AVRC_KEY_OPERATION_REWIND: case AVRC_KEY_OPERATION_FAST_FORWARD: case AVRC_KEY_OPERATION_FORWARD: case AVRC_KEY_OPERATION_BACKWARD: { BluetoothRawAddress rawAddr(device.GetDeviceAddr()); result = proxy->PressButton(rawAddr, static_cast(button)); break; } default: result = RET_NO_SUPPORT; if (button >= AVRC_KEY_OPERATION_INVALID) { result = RET_BAD_PARAM; } break; } return result; } int AvrcpController::ReleaseButton(const BluetoothRemoteDevice &device, uint8_t button) { HILOGI("enter, device: %{public}s, button: %{public}d", GET_ENCRYPT_ADDR(device), button); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_BAD_STATUS; switch (button) { case AVRC_KEY_OPERATION_VOLUME_UP: case AVRC_KEY_OPERATION_VOLUME_DOWN: case AVRC_KEY_OPERATION_MUTE: case AVRC_KEY_OPERATION_PLAY: case AVRC_KEY_OPERATION_STOP: case AVRC_KEY_OPERATION_PAUSE: case AVRC_KEY_OPERATION_REWIND: case AVRC_KEY_OPERATION_FAST_FORWARD: case AVRC_KEY_OPERATION_FORWARD: case AVRC_KEY_OPERATION_BACKWARD: { BluetoothRawAddress rawAddr(device.GetDeviceAddr()); result = proxy->ReleaseButton(rawAddr, static_cast(button)); break; } default: result = RET_NO_SUPPORT; if (button >= AVRC_KEY_OPERATION_INVALID) { result = RET_BAD_PARAM; } break; } return result; } /****************************************************************** * TEMP UNIT INFO / SUB UNIT INFO * ******************************************************************/ int AvrcpController::GetUnitInfo(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetUnitInfo(rawAddr); } int AvrcpController::GetSubUnitInfo(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetSubUnitInfo(rawAddr); } /****************************************************************** * Capabilities * ******************************************************************/ int AvrcpController::GetSupportedCompanies(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetSupportedCompanies(rawAddr); } int AvrcpController::GetSupportedEvents(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetSupportedEvents(rawAddr); } /****************************************************************** * PLAYER APPLICATION SETTINGS * ******************************************************************/ int AvrcpController::GetPlayerAppSettingAttributes(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetPlayerAppSettingAttributes(rawAddr); } int AvrcpController::GetPlayerAppSettingValues(const BluetoothRemoteDevice &device, uint8_t attribute) { HILOGI("enter, device: %{public}s, attribute: %{public}d", GET_ENCRYPT_ADDR(device), attribute); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_BAD_STATUS; do { if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) { result = RET_BAD_PARAM; break; } if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) { result = RET_BAD_PARAM; break; } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); result = proxy->GetPlayerAppSettingValues(rawAddr, static_cast(attribute)); } while (false); return result; } int AvrcpController::GetPlayerAppSettingCurrentValue( const BluetoothRemoteDevice &device, const std::vector &attributes) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_NO_ERROR; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_NO_ERROR; do { std::vector attrs; for (auto attribute : attributes) { if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) { result = RET_BAD_PARAM; break; } if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) { result = RET_BAD_PARAM; break; } attrs.push_back(attribute); } if (result != RET_NO_ERROR) { break; } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); result = proxy->GetPlayerAppSettingCurrentValue(rawAddr, attrs); } while (false); return result; } int AvrcpController::SetPlayerAppSettingCurrentValue( const BluetoothRemoteDevice &device, const std::vector &attributes, const std::vector &values) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_NO_ERROR; std::vector myAttributes; std::vector myValues; do { if (attributes.size() != values.size()) { break; } for (auto attribute : attributes) { if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) { result = RET_BAD_PARAM; break; } if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) { result = RET_BAD_PARAM; break; } } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); for (auto attribute : attributes) { myAttributes.push_back(static_cast(attribute)); } for (auto value : values) { myValues.push_back(static_cast(value)); } if (result != RET_NO_ERROR) { break; } result = proxy->SetPlayerAppSettingCurrentValue(rawAddr, myAttributes, myValues); } while (false); return result; } int AvrcpController::GetPlayerApplicationSettingAttributeText( const BluetoothRemoteDevice &device, const std::vector &attributes) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_BAD_STATUS; do { std::vector attrs; for (auto attribute : attributes) { if (attribute == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) { break; } if (attribute >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attribute <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) { break; } attrs.push_back(static_cast(attribute)); } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); result = proxy->GetPlayerAppSettingAttributeText(rawAddr, attrs); } while (false); return result; } int AvrcpController::GetPlayerApplicationSettingValueText( const BluetoothRemoteDevice &device, uint8_t attributeId, const std::vector &values) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_BAD_STATUS; do { std::vector myValues; if (attributeId == AVRC_PLAYER_ATTRIBUTE_ILLEGAL) { result = RET_BAD_PARAM; break; } if (attributeId >= AVRC_PLAYER_ATTRIBUTE_RESERVED_MIN && attributeId <= AVRC_PLAYER_ATTRIBUTE_RESERVED_MAX) { result = RET_BAD_PARAM; break; } for (auto value : values) { myValues.push_back(static_cast(value)); } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); result = proxy->GetPlayerAppSettingValueText(rawAddr, static_cast(attributeId), myValues); } while (false); return result; } /****************************************************************** * MEDIA INFORMATION * ******************************************************************/ int AvrcpController::GetElementAttributes(const BluetoothRemoteDevice &device, const std::vector &attributes) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); std::vector attrs; for (auto attribute : attributes) { attrs.push_back(static_cast(attribute)); } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetElementAttributes(rawAddr, attrs); } /****************************************************************** * PLAY * ******************************************************************/ int AvrcpController::GetPlayStatus(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetPlayStatus(rawAddr); } int AvrcpController::PlayItem(const BluetoothRemoteDevice &device, uint64_t uid, uint16_t uidCounter) { HILOGI("enter, device: %{public}s, uidCounter: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->PlayItem(rawAddr, static_cast(AVRC_MEDIA_SCOPE_NOW_PLAYING), static_cast(uid), static_cast(uidCounter)); } /****************************************************************** * OPERATE THE VIRTUAL FILE SYSTEM * ******************************************************************/ int AvrcpController::GetFolderItems( const BluetoothRemoteDevice &device, uint32_t startItem, uint32_t endItem, const std::vector &attributes) { HILOGI("enter, device: %{public}s, startItem: %{public}d, endItem: %{public}d", GET_ENCRYPT_ADDR(device), startItem, endItem); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); int result = RET_NO_ERROR; do { if (startItem > endItem) { result = RET_BAD_PARAM; break; } for (auto attribute : attributes) { if (attribute <= AVRC_MEDIA_ATTRIBUTE_NOT_USED || attribute >= AVRC_MEDIA_ATTRIBUTE_RESERVED) { result = RET_BAD_PARAM; break; } } if (result != RET_NO_ERROR) { break; } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); std::vector attrs; for (auto attribute : attributes) { attrs.push_back(static_cast(attribute)); } result = proxy->GetFolderItems( rawAddr, static_cast(startItem), static_cast(endItem), attrs); } while (false); return result; } int AvrcpController::GetMeidaPlayerList(const BluetoothRemoteDevice &device, uint32_t startItem, uint32_t endItem) { HILOGI("enter, device: %{public}s, startItem: %{public}d, endItem: %{public}d", GET_ENCRYPT_ADDR(device), startItem, endItem); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetMeidaPlayerList(rawAddr, static_cast(startItem), static_cast(endItem)); } int AvrcpController::GetTotalNumberOfItems(const BluetoothRemoteDevice &device) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); uint8_t scope = AVRC_MEDIA_SCOPE_NOW_PLAYING; BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetTotalNumberOfItems(rawAddr, static_cast(scope)); } /****************************************************************** * ABSOLUTE VOLUME * ******************************************************************/ int AvrcpController::SetAbsoluteVolume(const BluetoothRemoteDevice &device, uint8_t volume) { HILOGI("enter, device: %{public}s, volume: %{public}d", GET_ENCRYPT_ADDR(device), volume); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->SetAbsoluteVolume(rawAddr, static_cast(volume)); } /****************************************************************** * NOTIFY * ******************************************************************/ int AvrcpController::EnableNotification( const BluetoothRemoteDevice &device, const std::vector &events, uint32_t interval) { HILOGI("enter, device: %{public}s, interval: %{public}d", GET_ENCRYPT_ADDR(device), interval); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); std::vector myEvents; for (auto event : events) { myEvents.push_back(static_cast(event)); } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->EnableNotification(rawAddr, myEvents, static_cast(interval)); } int AvrcpController::DisableNotification(const BluetoothRemoteDevice &device, const std::vector &events) { HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device)); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); std::vector myEvents; for (auto event : events) { myEvents.push_back(static_cast(event)); } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->DisableNotification(rawAddr, myEvents); } /****************************************************************** * DO NOT EXPOSE THE INTERFACE * ******************************************************************/ int AvrcpController::SetAddressedPlayer(const BluetoothRemoteDevice &device, uint16_t playerId) { HILOGI("enter, device: %{public}s, playerId: %{public}d", GET_ENCRYPT_ADDR(device), playerId); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); return RET_BAD_STATUS; } int AvrcpController::SetBrowsedPlayer(const BluetoothRemoteDevice &device, uint16_t playerId) { HILOGI("enter, device: %{public}s, playerId: %{public}d", GET_ENCRYPT_ADDR(device), playerId); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->SetBrowsedPlayer(rawAddr, (int32_t)playerId); } int AvrcpController::ChangePath( const BluetoothRemoteDevice &device, uint16_t uidCounter, uint16_t direction, uint64_t folderUid) { HILOGI("enter, device: %{public}s, uidCounter: %{public}d, direction: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter, direction); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); return RET_BAD_STATUS; } int AvrcpController::GetItemAttributes( const BluetoothRemoteDevice &device, uint64_t uid, uint16_t uidCounter, const std::vector &attributes) { HILOGI("enter, device: %{public}s, uidCounter: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); std::vector attrs; for (auto attribute : attributes) { attrs.push_back(static_cast(attribute)); } BluetoothRawAddress rawAddr(device.GetDeviceAddr()); return proxy->GetItemAttributes(rawAddr, (int64_t)uid, (int32_t)uidCounter, attrs); } int AvrcpController::RequestContinuingResponse(const BluetoothRemoteDevice &device, uint8_t pduId) { HILOGI("enter, device: %{public}s, pduId: %{public}d", GET_ENCRYPT_ADDR(device), pduId); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); return RET_BAD_STATUS; } int AvrcpController::AbortContinuingResponse(const BluetoothRemoteDevice &device, uint8_t pduId) { HILOGI("enter, device: %{public}s, pduId: %{public}d", GET_ENCRYPT_ADDR(device), pduId); int result = RET_BAD_STATUS; return result; } int AvrcpController::AddToNowPlaying(const BluetoothRemoteDevice &device, uint64_t uid, uint16_t uidCounter) { HILOGI("enter, device: %{public}s, uidCounter: %{public}d", GET_ENCRYPT_ADDR(device), uidCounter); if (!IS_BT_ENABLED()) { HILOGE("bluetooth is off."); return RET_BAD_STATUS; } sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "failed: no proxy"); return RET_BAD_STATUS; } AvrcpController::AvrcpController(void) { HILOGI("enter"); pimpl = std::make_unique(); } AvrcpController::~AvrcpController(void) { HILOGI("enter"); sptr proxy = GetRemoteProxy(PROFILE_AVRCP_CT); CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy"); pimpl = nullptr; } } // namespace Bluetooth } // namespace OHOS