195489c19Sopenharmony_ci/*
295489c19Sopenharmony_ci * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
395489c19Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
495489c19Sopenharmony_ci * you may not use this file except in compliance with the License.
595489c19Sopenharmony_ci * You may obtain a copy of the License at
695489c19Sopenharmony_ci *
795489c19Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
895489c19Sopenharmony_ci *
995489c19Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
1095489c19Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
1195489c19Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1295489c19Sopenharmony_ci * See the License for the specific language governing permissions and
1395489c19Sopenharmony_ci * limitations under the License.
1495489c19Sopenharmony_ci */
1595489c19Sopenharmony_ci
1695489c19Sopenharmony_ci#include <list>
1795489c19Sopenharmony_ci#include <memory>
1895489c19Sopenharmony_ci#include <mutex>
1995489c19Sopenharmony_ci
2095489c19Sopenharmony_ci#include "bluetooth_def.h"
2195489c19Sopenharmony_ci#include "bluetooth_avrcp_tg_proxy.h"
2295489c19Sopenharmony_ci#include "bluetooth_avrcp_tg_observer_stub.h"
2395489c19Sopenharmony_ci#include "bluetooth_host.h"
2495489c19Sopenharmony_ci#include "bluetooth_host_proxy.h"
2595489c19Sopenharmony_ci#include "bluetooth_profile_manager.h"
2695489c19Sopenharmony_ci#include "bluetooth_log.h"
2795489c19Sopenharmony_ci#include "bluetooth_utils.h"
2895489c19Sopenharmony_ci#include "bluetooth_observer_list.h"
2995489c19Sopenharmony_ci#include "iservice_registry.h"
3095489c19Sopenharmony_ci#include "raw_address.h"
3195489c19Sopenharmony_ci#include "system_ability_definition.h"
3295489c19Sopenharmony_ci#include "bluetooth_avrcp_tg.h"
3395489c19Sopenharmony_ci
3495489c19Sopenharmony_cinamespace OHOS {
3595489c19Sopenharmony_cinamespace Bluetooth {
3695489c19Sopenharmony_cistd::mutex g_avrcpTgMutex;
3795489c19Sopenharmony_cistruct AvrcpTarget::impl {
3895489c19Sopenharmony_cipublic:
3995489c19Sopenharmony_ci    class ObserverImpl : public BluetoothAvrcpTgObserverStub {
4095489c19Sopenharmony_ci    public:
4195489c19Sopenharmony_ci        explicit ObserverImpl(AvrcpTarget::impl *impl) : impl_(impl)
4295489c19Sopenharmony_ci        {}
4395489c19Sopenharmony_ci        ~ObserverImpl() override = default;
4495489c19Sopenharmony_ci
4595489c19Sopenharmony_ci        void OnConnectionStateChanged(const BluetoothRawAddress &addr, int32_t state, int32_t cause) override
4695489c19Sopenharmony_ci        {
4795489c19Sopenharmony_ci            HILOGD("enter, address: %{public}s, state: %{public}d, cause: %{public}d",
4895489c19Sopenharmony_ci                GET_ENCRYPT_RAW_ADDR(addr), state, cause);
4995489c19Sopenharmony_ci            BluetoothRemoteDevice device(addr.GetAddress(), BTTransport::ADAPTER_BREDR);
5095489c19Sopenharmony_ci            impl_->OnConnectionStateChanged(device, static_cast<int>(state), cause);
5195489c19Sopenharmony_ci
5295489c19Sopenharmony_ci            return;
5395489c19Sopenharmony_ci        }
5495489c19Sopenharmony_ci
5595489c19Sopenharmony_ci    private:
5695489c19Sopenharmony_ci        AvrcpTarget::impl *impl_;
5795489c19Sopenharmony_ci    };
5895489c19Sopenharmony_ci
5995489c19Sopenharmony_ci    impl()
6095489c19Sopenharmony_ci    {
6195489c19Sopenharmony_ci        observer_ = new (std::nothrow) ObserverImpl(this);
6295489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(observer_ != nullptr, "observer_ is nullptr");
6395489c19Sopenharmony_ci        profileRegisterId = BluetoothProfileManager::GetInstance().RegisterFunc(PROFILE_AVRCP_TG,
6495489c19Sopenharmony_ci            [this](sptr<IRemoteObject> remote) {
6595489c19Sopenharmony_ci            sptr<IBluetoothAvrcpTg> proxy = iface_cast<IBluetoothAvrcpTg>(remote);
6695489c19Sopenharmony_ci            CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
6795489c19Sopenharmony_ci            proxy->RegisterObserver(observer_);
6895489c19Sopenharmony_ci        });
6995489c19Sopenharmony_ci    }
7095489c19Sopenharmony_ci
7195489c19Sopenharmony_ci    ~impl()
7295489c19Sopenharmony_ci    {
7395489c19Sopenharmony_ci        HILOGI("enter");
7495489c19Sopenharmony_ci        BluetoothProfileManager::GetInstance().DeregisterFunc(profileRegisterId);
7595489c19Sopenharmony_ci        sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
7695489c19Sopenharmony_ci        CHECK_AND_RETURN_LOG(proxy != nullptr, "failed: no proxy");
7795489c19Sopenharmony_ci        proxy->UnregisterObserver(observer_);
7895489c19Sopenharmony_ci    }
7995489c19Sopenharmony_ci
8095489c19Sopenharmony_ci    void OnConnectionStateChanged(const BluetoothRemoteDevice &device, int state, int cause)
8195489c19Sopenharmony_ci    {
8295489c19Sopenharmony_ci        HILOGI("enter, device: %{public}s, state: %{public}d, cause: %{public}d",
8395489c19Sopenharmony_ci            GET_ENCRYPT_ADDR(device), state, cause);
8495489c19Sopenharmony_ci        std::lock_guard<std::mutex> lock(observerMutex_);
8595489c19Sopenharmony_ci        observers_.ForEach([device, state, cause](std::shared_ptr<IObserver> observer) {
8695489c19Sopenharmony_ci            observer->OnConnectionStateChanged(device, state, cause);
8795489c19Sopenharmony_ci        });
8895489c19Sopenharmony_ci    }
8995489c19Sopenharmony_ci
9095489c19Sopenharmony_ci    std::mutex observerMutex_;
9195489c19Sopenharmony_ci    BluetoothObserverList<AvrcpTarget::IObserver> observers_;
9295489c19Sopenharmony_ci    sptr<ObserverImpl> observer_;
9395489c19Sopenharmony_ci    int32_t profileRegisterId = 0;
9495489c19Sopenharmony_ci};
9595489c19Sopenharmony_ci
9695489c19Sopenharmony_ciAvrcpTarget *AvrcpTarget::GetProfile(void)
9795489c19Sopenharmony_ci{
9895489c19Sopenharmony_ci    HILOGI("enter");
9995489c19Sopenharmony_ci#ifdef DTFUZZ_TEST
10095489c19Sopenharmony_ci    static BluetoothNoDestructor<AvrcpTarget> instance;
10195489c19Sopenharmony_ci    return instance.get();
10295489c19Sopenharmony_ci#else
10395489c19Sopenharmony_ci    static AvrcpTarget instance;
10495489c19Sopenharmony_ci    return &instance;
10595489c19Sopenharmony_ci#endif
10695489c19Sopenharmony_ci}
10795489c19Sopenharmony_ci
10895489c19Sopenharmony_ciint32_t AvrcpTarget::SetDeviceAbsoluteVolume(const BluetoothRemoteDevice &device, int32_t volumeLevel)
10995489c19Sopenharmony_ci{
11095489c19Sopenharmony_ci    HILOGI("enter");
11195489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
11295489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
11395489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
11495489c19Sopenharmony_ci    }
11595489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
11695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
11795489c19Sopenharmony_ci    return proxy->SetDeviceAbsoluteVolume(BluetoothRawAddress(device.GetDeviceAddr()), volumeLevel);
11895489c19Sopenharmony_ci}
11995489c19Sopenharmony_ci
12095489c19Sopenharmony_ciint32_t AvrcpTarget::SetDeviceAbsVolumeAbility(const BluetoothRemoteDevice &device, int32_t ability)
12195489c19Sopenharmony_ci{
12295489c19Sopenharmony_ci    HILOGI("enter");
12395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
12495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
12595489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
12695489c19Sopenharmony_ci    }
12795489c19Sopenharmony_ci
12895489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
12995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
13095489c19Sopenharmony_ci    return proxy->SetDeviceAbsVolumeAbility(BluetoothRawAddress(device.GetDeviceAddr()), ability);
13195489c19Sopenharmony_ci}
13295489c19Sopenharmony_ci
13395489c19Sopenharmony_ciint32_t AvrcpTarget::GetDeviceAbsVolumeAbility(const BluetoothRemoteDevice &device, int32_t &ability)
13495489c19Sopenharmony_ci{
13595489c19Sopenharmony_ci    HILOGI("enter");
13695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
13795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
13895489c19Sopenharmony_ci        return BT_ERR_INVALID_STATE;
13995489c19Sopenharmony_ci    }
14095489c19Sopenharmony_ci
14195489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
14295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, BT_ERR_UNAVAILABLE_PROXY, "proxy is nullptr");
14395489c19Sopenharmony_ci    return proxy->GetDeviceAbsVolumeAbility(BluetoothRawAddress(device.GetDeviceAddr()), ability);
14495489c19Sopenharmony_ci}
14595489c19Sopenharmony_ci
14695489c19Sopenharmony_ci/******************************************************************
14795489c19Sopenharmony_ci * REGISTER / UNREGISTER OBSERVER                                 *
14895489c19Sopenharmony_ci ******************************************************************/
14995489c19Sopenharmony_ci
15095489c19Sopenharmony_civoid AvrcpTarget::RegisterObserver(std::shared_ptr<AvrcpTarget::IObserver> observer)
15195489c19Sopenharmony_ci{
15295489c19Sopenharmony_ci    HILOGD("enter");
15395489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
15495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
15595489c19Sopenharmony_ci    pimpl->observers_.Register(observer);
15695489c19Sopenharmony_ci}
15795489c19Sopenharmony_ci
15895489c19Sopenharmony_civoid AvrcpTarget::UnregisterObserver(std::shared_ptr<AvrcpTarget::IObserver> observer)
15995489c19Sopenharmony_ci{
16095489c19Sopenharmony_ci    HILOGD("enter");
16195489c19Sopenharmony_ci    std::lock_guard<std::mutex> lock(pimpl->observerMutex_);
16295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(pimpl != nullptr, "pimpl is null.");
16395489c19Sopenharmony_ci    pimpl->observers_.Deregister(observer);
16495489c19Sopenharmony_ci}
16595489c19Sopenharmony_ci
16695489c19Sopenharmony_ci/******************************************************************
16795489c19Sopenharmony_ci * CONNECTION                                                     *
16895489c19Sopenharmony_ci ******************************************************************/
16995489c19Sopenharmony_ci
17095489c19Sopenharmony_civoid AvrcpTarget::SetActiveDevice(const BluetoothRemoteDevice &device)
17195489c19Sopenharmony_ci{
17295489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
17395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
17495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
17595489c19Sopenharmony_ci        return;
17695489c19Sopenharmony_ci    }
17795489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
17895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
17995489c19Sopenharmony_ci
18095489c19Sopenharmony_ci    BluetoothRawAddress rawAddr(device.GetDeviceAddr());
18195489c19Sopenharmony_ci    proxy->SetActiveDevice(rawAddr);
18295489c19Sopenharmony_ci}
18395489c19Sopenharmony_ci
18495489c19Sopenharmony_cistd::vector<BluetoothRemoteDevice> AvrcpTarget::GetConnectedDevices(void)
18595489c19Sopenharmony_ci{
18695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
18795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
18895489c19Sopenharmony_ci        return std::vector<BluetoothRemoteDevice>();
18995489c19Sopenharmony_ci    }
19095489c19Sopenharmony_ci
19195489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
19295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<BluetoothRemoteDevice>(), "proxy is nullptr");
19395489c19Sopenharmony_ci
19495489c19Sopenharmony_ci    std::vector<BluetoothRemoteDevice> devices;
19595489c19Sopenharmony_ci    std::vector<BluetoothRawAddress> rawAddrs = proxy->GetConnectedDevices();
19695489c19Sopenharmony_ci    for (auto rawAddr : rawAddrs) {
19795489c19Sopenharmony_ci        BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
19895489c19Sopenharmony_ci        devices.push_back(device);
19995489c19Sopenharmony_ci    }
20095489c19Sopenharmony_ci    return devices;
20195489c19Sopenharmony_ci}
20295489c19Sopenharmony_ci
20395489c19Sopenharmony_cistd::vector<BluetoothRemoteDevice> AvrcpTarget::GetDevicesByStates(std::vector<int> states)
20495489c19Sopenharmony_ci{
20595489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
20695489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
20795489c19Sopenharmony_ci        return std::vector<BluetoothRemoteDevice>();
20895489c19Sopenharmony_ci    }
20995489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
21095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, std::vector<BluetoothRemoteDevice>(), "proxy is nullptr");
21195489c19Sopenharmony_ci
21295489c19Sopenharmony_ci    std::vector<int32_t> convertStates;
21395489c19Sopenharmony_ci    for (auto state : states) {
21495489c19Sopenharmony_ci        convertStates.push_back(static_cast<int32_t>(state));
21595489c19Sopenharmony_ci    }
21695489c19Sopenharmony_ci
21795489c19Sopenharmony_ci    std::vector<BluetoothRemoteDevice> devices;
21895489c19Sopenharmony_ci    std::vector<BluetoothRawAddress> rawAddrs = proxy->GetDevicesByStates(convertStates);
21995489c19Sopenharmony_ci    for (auto rawAddr : rawAddrs) {
22095489c19Sopenharmony_ci        BluetoothRemoteDevice device(rawAddr.GetAddress(), BTTransport::ADAPTER_BREDR);
22195489c19Sopenharmony_ci        devices.push_back(device);
22295489c19Sopenharmony_ci    }
22395489c19Sopenharmony_ci
22495489c19Sopenharmony_ci    return devices;
22595489c19Sopenharmony_ci}
22695489c19Sopenharmony_ci
22795489c19Sopenharmony_ciint AvrcpTarget::GetDeviceState(const BluetoothRemoteDevice &device)
22895489c19Sopenharmony_ci{
22995489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
23095489c19Sopenharmony_ci
23195489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
23295489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
23395489c19Sopenharmony_ci        return static_cast<int32_t>(BTConnectState::DISCONNECTED);
23495489c19Sopenharmony_ci    }
23595489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
23695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr,
23795489c19Sopenharmony_ci        static_cast<int32_t>(BTConnectState::DISCONNECTED), "proxy is nullptr");
23895489c19Sopenharmony_ci
23995489c19Sopenharmony_ci    BluetoothRawAddress rawAddr(device.GetDeviceAddr());
24095489c19Sopenharmony_ci    int32_t result = proxy->GetDeviceState(rawAddr);
24195489c19Sopenharmony_ci    return static_cast<int>(result);
24295489c19Sopenharmony_ci}
24395489c19Sopenharmony_ci
24495489c19Sopenharmony_cibool AvrcpTarget::Connect(const BluetoothRemoteDevice &device)
24595489c19Sopenharmony_ci{
24695489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
24795489c19Sopenharmony_ci
24895489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
24995489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
25095489c19Sopenharmony_ci        return RET_BAD_STATUS;
25195489c19Sopenharmony_ci    }
25295489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
25395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "proxy is nullptr");
25495489c19Sopenharmony_ci
25595489c19Sopenharmony_ci    BluetoothRawAddress rawAddr(device.GetDeviceAddr());
25695489c19Sopenharmony_ci    int result = proxy->Connect(rawAddr);
25795489c19Sopenharmony_ci    return result == RET_NO_ERROR;
25895489c19Sopenharmony_ci}
25995489c19Sopenharmony_ci
26095489c19Sopenharmony_cibool AvrcpTarget::Disconnect(const BluetoothRemoteDevice &device)
26195489c19Sopenharmony_ci{
26295489c19Sopenharmony_ci    HILOGI("enter, device: %{public}s", GET_ENCRYPT_ADDR(device));
26395489c19Sopenharmony_ci
26495489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
26595489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
26695489c19Sopenharmony_ci        return RET_BAD_STATUS;
26795489c19Sopenharmony_ci    }
26895489c19Sopenharmony_ci
26995489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
27095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG_RET(proxy != nullptr, RET_BAD_STATUS, "proxy is nullptr");
27195489c19Sopenharmony_ci
27295489c19Sopenharmony_ci    BluetoothRawAddress rawAddr(device.GetDeviceAddr());
27395489c19Sopenharmony_ci    int result = proxy->Disconnect(rawAddr);
27495489c19Sopenharmony_ci    return result == RET_NO_ERROR;
27595489c19Sopenharmony_ci}
27695489c19Sopenharmony_ci
27795489c19Sopenharmony_ci/******************************************************************
27895489c19Sopenharmony_ci * NOTIFICATION                                                   *
27995489c19Sopenharmony_ci ******************************************************************/
28095489c19Sopenharmony_ci
28195489c19Sopenharmony_civoid AvrcpTarget::NotifyPlaybackStatusChanged(uint8_t playStatus, uint32_t playbackPos)
28295489c19Sopenharmony_ci{
28395489c19Sopenharmony_ci    HILOGI("enter, playStatus: %{public}d, playbackPos: %{public}d", playStatus, playbackPos);
28495489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
28595489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
28695489c19Sopenharmony_ci        return;
28795489c19Sopenharmony_ci    }
28895489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
28995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
29095489c19Sopenharmony_ci
29195489c19Sopenharmony_ci    proxy->NotifyPlaybackStatusChanged(static_cast<int32_t>(playStatus), static_cast<int32_t>(playbackPos));
29295489c19Sopenharmony_ci}
29395489c19Sopenharmony_ci
29495489c19Sopenharmony_civoid AvrcpTarget::NotifyTrackChanged(uint64_t uid, uint32_t playbackPos)
29595489c19Sopenharmony_ci{
29695489c19Sopenharmony_ci    HILOGI("enter, playbackPos: %{public}d", playbackPos);
29795489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
29895489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
29995489c19Sopenharmony_ci        return;
30095489c19Sopenharmony_ci    }
30195489c19Sopenharmony_ci
30295489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
30395489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
30495489c19Sopenharmony_ci
30595489c19Sopenharmony_ci    proxy->NotifyTrackChanged(static_cast<int64_t>(uid), static_cast<int32_t>(playbackPos));
30695489c19Sopenharmony_ci}
30795489c19Sopenharmony_ci
30895489c19Sopenharmony_civoid AvrcpTarget::NotifyTrackReachedEnd(uint32_t playbackPos)
30995489c19Sopenharmony_ci{
31095489c19Sopenharmony_ci    HILOGI("enter, playbackPos: %{public}d", playbackPos);
31195489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
31295489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
31395489c19Sopenharmony_ci        return;
31495489c19Sopenharmony_ci    }
31595489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
31695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
31795489c19Sopenharmony_ci
31895489c19Sopenharmony_ci    proxy->NotifyTrackReachedEnd(static_cast<int32_t>(playbackPos));
31995489c19Sopenharmony_ci}
32095489c19Sopenharmony_ci
32195489c19Sopenharmony_civoid AvrcpTarget::NotifyTrackReachedStart(uint32_t playbackPos)
32295489c19Sopenharmony_ci{
32395489c19Sopenharmony_ci    HILOGI("enter, playbackPos: %{public}d", playbackPos);
32495489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
32595489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
32695489c19Sopenharmony_ci        return;
32795489c19Sopenharmony_ci    }
32895489c19Sopenharmony_ci
32995489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
33095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
33195489c19Sopenharmony_ci
33295489c19Sopenharmony_ci    proxy->NotifyTrackReachedStart(static_cast<int32_t>(playbackPos));
33395489c19Sopenharmony_ci}
33495489c19Sopenharmony_ci
33595489c19Sopenharmony_civoid AvrcpTarget::NotifyPlaybackPosChanged(uint32_t playbackPos)
33695489c19Sopenharmony_ci{
33795489c19Sopenharmony_ci    HILOGI("enter, playbackPos: %{public}d", playbackPos);
33895489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
33995489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
34095489c19Sopenharmony_ci        return;
34195489c19Sopenharmony_ci    }
34295489c19Sopenharmony_ci
34395489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
34495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
34595489c19Sopenharmony_ci
34695489c19Sopenharmony_ci    proxy->NotifyPlaybackPosChanged(static_cast<int32_t>(playbackPos));
34795489c19Sopenharmony_ci}
34895489c19Sopenharmony_ci
34995489c19Sopenharmony_civoid AvrcpTarget::NotifyPlayerAppSettingChanged(const std::vector<uint8_t> &attributes,
35095489c19Sopenharmony_ci    const std::vector<uint8_t> &values)
35195489c19Sopenharmony_ci{
35295489c19Sopenharmony_ci    HILOGI("enter");
35395489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
35495489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
35595489c19Sopenharmony_ci        return;
35695489c19Sopenharmony_ci    }
35795489c19Sopenharmony_ci
35895489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
35995489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
36095489c19Sopenharmony_ci
36195489c19Sopenharmony_ci    std::vector<int32_t> attrs;
36295489c19Sopenharmony_ci    for (auto attribute : attributes) {
36395489c19Sopenharmony_ci        attrs.push_back(attribute);
36495489c19Sopenharmony_ci    }
36595489c19Sopenharmony_ci    std::vector<int32_t> vals;
36695489c19Sopenharmony_ci    for (auto value : values) {
36795489c19Sopenharmony_ci        vals.push_back(value);
36895489c19Sopenharmony_ci    }
36995489c19Sopenharmony_ci
37095489c19Sopenharmony_ci    proxy->NotifyPlayerAppSettingChanged(attrs, vals);
37195489c19Sopenharmony_ci}
37295489c19Sopenharmony_ci
37395489c19Sopenharmony_civoid AvrcpTarget::NotifyNowPlayingContentChanged(void)
37495489c19Sopenharmony_ci{
37595489c19Sopenharmony_ci    HILOGI("enter");
37695489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
37795489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
37895489c19Sopenharmony_ci        return;
37995489c19Sopenharmony_ci    }
38095489c19Sopenharmony_ci
38195489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
38295489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
38395489c19Sopenharmony_ci
38495489c19Sopenharmony_ci    proxy->NotifyNowPlayingContentChanged();
38595489c19Sopenharmony_ci}
38695489c19Sopenharmony_ci
38795489c19Sopenharmony_civoid AvrcpTarget::NotifyAvailablePlayersChanged(void)
38895489c19Sopenharmony_ci{
38995489c19Sopenharmony_ci    HILOGI("enter");
39095489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
39195489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
39295489c19Sopenharmony_ci        return;
39395489c19Sopenharmony_ci    }
39495489c19Sopenharmony_ci
39595489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
39695489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
39795489c19Sopenharmony_ci
39895489c19Sopenharmony_ci    proxy->NotifyAvailablePlayersChanged();
39995489c19Sopenharmony_ci}
40095489c19Sopenharmony_ci
40195489c19Sopenharmony_civoid AvrcpTarget::NotifyAddressedPlayerChanged(uint16_t playerId, uint16_t uidCounter)
40295489c19Sopenharmony_ci{
40395489c19Sopenharmony_ci    HILOGI("enter, playerId: %{public}d, uidCounter: %{public}d", playerId, uidCounter);
40495489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
40595489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
40695489c19Sopenharmony_ci        return;
40795489c19Sopenharmony_ci    }
40895489c19Sopenharmony_ci
40995489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
41095489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
41195489c19Sopenharmony_ci
41295489c19Sopenharmony_ci    proxy->NotifyAddressedPlayerChanged(static_cast<int32_t>(playerId), static_cast<int32_t>(uidCounter));
41395489c19Sopenharmony_ci}
41495489c19Sopenharmony_ci
41595489c19Sopenharmony_civoid AvrcpTarget::NotifyUidChanged(uint16_t uidCounter)
41695489c19Sopenharmony_ci{
41795489c19Sopenharmony_ci    HILOGI("enter, uidCounter: %{public}d", uidCounter);
41895489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
41995489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
42095489c19Sopenharmony_ci        return;
42195489c19Sopenharmony_ci    }
42295489c19Sopenharmony_ci
42395489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
42495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
42595489c19Sopenharmony_ci
42695489c19Sopenharmony_ci    proxy->NotifyUidChanged(static_cast<int32_t>(uidCounter));
42795489c19Sopenharmony_ci}
42895489c19Sopenharmony_ci
42995489c19Sopenharmony_civoid AvrcpTarget::NotifyVolumeChanged(uint8_t volume)
43095489c19Sopenharmony_ci{
43195489c19Sopenharmony_ci    HILOGI("enter, volume: %{public}d", volume);
43295489c19Sopenharmony_ci    if (!IS_BT_ENABLED()) {
43395489c19Sopenharmony_ci        HILOGE("bluetooth is off.");
43495489c19Sopenharmony_ci        return;
43595489c19Sopenharmony_ci    }
43695489c19Sopenharmony_ci
43795489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
43895489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
43995489c19Sopenharmony_ci
44095489c19Sopenharmony_ci    proxy->NotifyVolumeChanged(static_cast<int32_t>(volume));
44195489c19Sopenharmony_ci}
44295489c19Sopenharmony_ci
44395489c19Sopenharmony_ciAvrcpTarget::AvrcpTarget(void)
44495489c19Sopenharmony_ci{
44595489c19Sopenharmony_ci    HILOGI("enter");
44695489c19Sopenharmony_ci
44795489c19Sopenharmony_ci    pimpl = std::make_unique<impl>();
44895489c19Sopenharmony_ci}
44995489c19Sopenharmony_ci
45095489c19Sopenharmony_ciAvrcpTarget::~AvrcpTarget(void)
45195489c19Sopenharmony_ci{
45295489c19Sopenharmony_ci    HILOGI("enter");
45395489c19Sopenharmony_ci    sptr<IBluetoothAvrcpTg> proxy = GetRemoteProxy<IBluetoothAvrcpTg>(PROFILE_AVRCP_TG);
45495489c19Sopenharmony_ci    CHECK_AND_RETURN_LOG(proxy != nullptr, "proxy is nullptr");
45595489c19Sopenharmony_ci    pimpl = nullptr;
45695489c19Sopenharmony_ci}
45795489c19Sopenharmony_ci} // namespace Bluetooth
45895489c19Sopenharmony_ci} // namespace OHOS
459