2 // Copyright (C) 2017 Google, Inc.
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:
8 // http://www.apache.org/licenses/LICENSE-2.0
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.
23 #include "base/macros.h"
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"
29 #include "service/bluetooth_instance.h"
30 #include "service/hal/bluetooth_avrcp_interface.h"
34 // Note: presently this only supports
35 // (BTRC_FEAT_METADATA | BTRC_FEAT_ABSOLUTE_VOLUME)
36 class AvrcpTarget : public BluetoothInstance,
37 private hal::BluetoothAvrcpInterface::TargetObserver {
39 // We only allow one instance of this object at a time.
40 static const int kSingletonInstanceId;
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,
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,
65 virtual void OnPassThroughCommand(const std::string& addr, int32_t id,
66 int32_t key_state) = 0;
69 virtual ~Delegate() = default;
72 // The destructor automatically unregisters this instance from the stack.
73 ~AvrcpTarget() override;
75 // Assigns a delegate to this instance. |delegate| must out-live this
76 // AvrcpTarget instance.
77 void SetDelegate(Delegate* delegate);
79 // BluetoothClientInstace overrides:
80 const Uuid& GetAppIdentifier() const override;
81 int GetInstanceId() const override;
86 bool GetPlayStatusResponse(const std::string& addr, int32_t play_status,
87 uint32_t song_len, uint32_t song_pos);
89 bool ListPlayerAppAttrResponse(const std::string& addr,
90 const std::vector<int32_t>& attrs);
92 bool GetPlayerAppValueResponse(const std::string& addr,
93 const std::vector<AvrcpIntValue>& values);
95 bool GetPlayerAppAttrTextResponse(const std::string& addr,
96 const std::vector<AvrcpStringValue>& attrs);
98 bool GetPlayerAppValueTextResponse(
99 const std::string& addr, const std::vector<AvrcpStringValue>& attrs);
101 bool GetElementAttrResponse(const std::string& addr,
102 const std::vector<AvrcpStringValue>& attrs);
104 bool SetPlayerAppValueResponse(const std::string& addr, int32_t rsp_status);
106 bool RegisterNotificationResponse(
107 int32_t event_id, int32_t type,
108 const AvrcpRegisterNotificationResponse& param);
110 bool SetVolume(int volume);
113 friend class AvrcpTargetFactory;
115 // Constructor shouldn't be called directly as instances are meant to be
116 // obtained from the factory.
117 AvrcpTarget(const Uuid& uuid);
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,
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;
145 // See getters for documentation.
146 const Uuid app_identifier_;
148 // Mutex that synchronizes access to the entries below.
151 // Raw handle to the Delegate, which must outlive this AvrcpTarget instance.
152 std::mutex delegate_mutex_;
153 Delegate* delegate_ = nullptr;
155 DISALLOW_COPY_AND_ASSIGN(AvrcpTarget);
158 // AvrcpTargetFactory is used to register and obtain a per-application
160 // instance. Users should call RegisterClient to obtain their own unique
161 // AvrcpTarget instance that has been registered with the Bluetooth stack.
162 class AvrcpTargetFactory
163 : public BluetoothInstanceFactory,
164 private hal::BluetoothAvrcpInterface::TargetObserver {
166 // Don't construct/destruct directly except in tests. Instead, obtain a handle
167 // from an Adapter instance.
168 AvrcpTargetFactory();
169 ~AvrcpTargetFactory() override;
171 // BluetoothInstanceFactory override:
172 bool RegisterInstance(const Uuid& uuid,
173 const RegisterCallback& callback) override;
176 DISALLOW_COPY_AND_ASSIGN(AvrcpTargetFactory);
179 } // namespace bluetooth