blob: 8c2bcceb0bdd6d7da2cabbbe71512c31b2ec1fac [file] [log] [blame]
Bailey Forrest780e29c2018-08-21 17:20:29 -07001//
2// Copyright (C) 2017 Google, Inc.
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at:
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15//
16
17#pragma once
18
19#include <mutex>
20#include <string>
21#include <vector>
22
23#include "base/macros.h"
24
25#include "service/common/bluetooth/avrcp_int_value.h"
26#include "service/common/bluetooth/avrcp_register_notification_response.h"
27#include "service/common/bluetooth/avrcp_string_value.h"
28
29#include "service/bluetooth_instance.h"
30#include "service/hal/bluetooth_avrcp_interface.h"
31
32namespace bluetooth {
33
34// Note: presently this only supports
35// (BTRC_FEAT_METADATA | BTRC_FEAT_ABSOLUTE_VOLUME)
36class AvrcpTarget : public BluetoothInstance,
37 private hal::BluetoothAvrcpInterface::TargetObserver {
38 public:
39 // We only allow one instance of this object at a time.
40 static const int kSingletonInstanceId;
41
42 class Delegate {
43 public:
44 virtual void OnGetRemoteFeatures(const std::string& addr,
45 int32_t features) = 0;
46 virtual void OnGetPlayStatus(const std::string& addr) = 0;
47 virtual void OnListPlayerAppAttr(const std::string& addr) = 0;
48 virtual void OnListPlayerAppValues(const std::string& addr,
49 int32_t attr_id) = 0;
50 virtual void OnGetPlayerAppValue(const std::string& addr,
51 const std::vector<int32_t>& attrs) = 0;
52 virtual void OnGetPlayerAppAttrsText(const std::string& addr,
53 const std::vector<int32_t>& attrs) = 0;
54 virtual void OnGetPlayerAppValuesText(
55 const std::string& addr, int32_t attr_id,
56 const std::vector<int32_t>& values) = 0;
57 virtual void OnSetPlayerAppValue(
58 const std::string& addr, const std::vector<AvrcpIntValue>& values) = 0;
59 virtual void OnGetElementAttrs(const std::string& addr,
60 const std::vector<int32_t>& attrs) = 0;
61 virtual void OnRegisterNotification(const std::string& addr,
62 int32_t event_id, uint32_t param) = 0;
63 virtual void OnVolumeChange(const std::string& addr, int32_t volume,
64 int32_t ctype) = 0;
65 virtual void OnPassThroughCommand(const std::string& addr, int32_t id,
66 int32_t key_state) = 0;
67
68 protected:
69 virtual ~Delegate() = default;
70 };
71
72 // The destructor automatically unregisters this instance from the stack.
73 ~AvrcpTarget() override;
74
75 // Assigns a delegate to this instance. |delegate| must out-live this
76 // AvrcpTarget instance.
77 void SetDelegate(Delegate* delegate);
78
79 // BluetoothClientInstace overrides:
80 const Uuid& GetAppIdentifier() const override;
81 int GetInstanceId() const override;
82
83 bool Enable();
84 void Disable();
85
86 bool GetPlayStatusResponse(const std::string& addr, int32_t play_status,
87 uint32_t song_len, uint32_t song_pos);
88
89 bool ListPlayerAppAttrResponse(const std::string& addr,
90 const std::vector<int32_t>& attrs);
91
92 bool GetPlayerAppValueResponse(const std::string& addr,
93 const std::vector<AvrcpIntValue>& values);
94
95 bool GetPlayerAppAttrTextResponse(const std::string& addr,
96 const std::vector<AvrcpStringValue>& attrs);
97
98 bool GetPlayerAppValueTextResponse(
99 const std::string& addr, const std::vector<AvrcpStringValue>& attrs);
100
101 bool GetElementAttrResponse(const std::string& addr,
102 const std::vector<AvrcpStringValue>& attrs);
103
104 bool SetPlayerAppValueResponse(const std::string& addr, int32_t rsp_status);
105
106 bool RegisterNotificationResponse(
107 int32_t event_id, int32_t type,
108 const AvrcpRegisterNotificationResponse& param);
109
110 bool SetVolume(int volume);
111
112 private:
113 friend class AvrcpTargetFactory;
114
115 // Constructor shouldn't be called directly as instances are meant to be
116 // obtained from the factory.
117 AvrcpTarget(const Uuid& uuid);
118
119 // hal::BluetoothAvrcpInterface::TargetObserver implementation:
120 void RemoteFeaturesCallback(const RawAddress& bd_addr,
121 btrc_remote_features_t features) override;
122 void GetPlayStatusCallback(const RawAddress& bd_addr) override;
123 void ListPlayerAppAttrCallback(const RawAddress& bd_addr) override;
124 void ListPlayerAppValuesCallback(btrc_player_attr_t attr_id,
125 const RawAddress& bd_addr) override;
126 void GetPlayerAppValueCallback(uint8_t num_attr, btrc_player_attr_t* p_attrs,
127 const RawAddress& bd_addr) override;
128 void GetPlayerAppAttrsTextCallback(uint8_t num_attr,
129 btrc_player_attr_t* p_attrs,
130 const RawAddress& bd_addr) override;
131 void GetPlayerAppValuesTextCallback(uint8_t attr_id, uint8_t num_val,
132 uint8_t* p_vals,
133 const RawAddress& bd_addr) override;
134 void SetPlayerAppValueCallback(btrc_player_settings_t* p_vals,
135 const RawAddress& bd_addr) override;
136 void GetElementAttrCallback(uint8_t num_attr, btrc_media_attr_t* p_attrs,
137 const RawAddress& bd_addr) override;
138 void RegisterNotificationCallback(btrc_event_id_t event_id, uint32_t param,
139 const RawAddress& bd_addr) override;
140 void VolumeChangeCallback(uint8_t volume, uint8_t ctype,
141 const RawAddress& bd_addr) override;
142 void PassthroughCmdCallback(int id, int key_state,
143 const RawAddress& bd_addr) override;
144
145 // See getters for documentation.
146 const Uuid app_identifier_;
147
148 // Mutex that synchronizes access to the entries below.
149 std::mutex mutex_;
150
151 // Raw handle to the Delegate, which must outlive this AvrcpTarget instance.
152 std::mutex delegate_mutex_;
153 Delegate* delegate_ = nullptr;
154
155 DISALLOW_COPY_AND_ASSIGN(AvrcpTarget);
156};
157
158// AvrcpTargetFactory is used to register and obtain a per-application
159// AvrcpTarget
160// instance. Users should call RegisterClient to obtain their own unique
161// AvrcpTarget instance that has been registered with the Bluetooth stack.
162class AvrcpTargetFactory
163 : public BluetoothInstanceFactory,
164 private hal::BluetoothAvrcpInterface::TargetObserver {
165 public:
166 // Don't construct/destruct directly except in tests. Instead, obtain a handle
167 // from an Adapter instance.
168 AvrcpTargetFactory();
169 ~AvrcpTargetFactory() override;
170
171 // BluetoothInstanceFactory override:
172 bool RegisterInstance(const Uuid& uuid,
173 const RegisterCallback& callback) override;
174
175 private:
176 DISALLOW_COPY_AND_ASSIGN(AvrcpTargetFactory);
177};
178
179} // namespace bluetooth