1 //
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 <base/macros.h>
20 #include <hardware/bluetooth.h>
21 #include <hardware/bt_rc.h>
22 
23 namespace bluetooth {
24 namespace hal {
25 
26 class BluetoothAvrcpInterface {
27  public:
28   // The HAL interface doesn't allow registering "user data" that carries
29   // context beyond the callback parameters, forcing implementations to deal
30   // with global variables. The *Observer interface is to redirect these events
31   // to interested parties in an object-oriented manner.
32   class TargetObserver {
33    public:
34     virtual ~TargetObserver() = default;
35 
36     virtual void RemoteFeaturesCallback(const RawAddress& bd_addr,
37                                         btrc_remote_features_t features);
38     virtual void GetPlayStatusCallback(const RawAddress& bd_addr);
39     virtual void ListPlayerAppAttrCallback(const RawAddress& bd_addr);
40     virtual void ListPlayerAppValuesCallback(btrc_player_attr_t attr_id,
41                                              const RawAddress& bd_addr);
42     virtual void GetPlayerAppValueCallback(uint8_t num_attr,
43                                            btrc_player_attr_t* p_attrs,
44                                            const RawAddress& bd_addr);
45     virtual void GetPlayerAppAttrsTextCallback(uint8_t num_attr,
46                                                btrc_player_attr_t* p_attrs,
47                                                const RawAddress& bd_addr);
48     virtual void GetPlayerAppValuesTextCallback(uint8_t attr_id,
49                                                 uint8_t num_val,
50                                                 uint8_t* p_vals,
51                                                 const RawAddress& bd_addr);
52     virtual void SetPlayerAppValueCallback(btrc_player_settings_t* p_vals,
53                                            const RawAddress& bd_addr);
54     virtual void GetElementAttrCallback(uint8_t num_attr,
55                                         btrc_media_attr_t* p_attrs,
56                                         const RawAddress& bd_addr);
57     virtual void RegisterNotificationCallback(btrc_event_id_t event_id,
58                                               uint32_t param,
59                                               const RawAddress& bd_addr);
60     virtual void VolumeChangeCallback(uint8_t volume, uint8_t ctype,
61                                       const RawAddress& bd_addr);
62     virtual void PassthroughCmdCallback(int id, int key_state,
63                                         const RawAddress& bd_addr);
64     virtual void SetAddressedPlayerCallback(uint16_t player_id,
65                                             const RawAddress& bd_addr);
66     virtual void SetBrowsedPlayerCallback(uint16_t player_id,
67                                           const RawAddress& bd_addr);
68     virtual void GetFolderItemsCallback(uint8_t scope, uint32_t start_item,
69                                         uint32_t end_item, uint8_t num_attr,
70                                         uint32_t* p_attr_ids,
71                                         const RawAddress& bd_addr);
72     virtual void ChangePathCallback(uint8_t direction, uint8_t* folder_uid,
73                                     const RawAddress& bd_addr);
74     virtual void GetItemAttrCallback(uint8_t scope, uint8_t* uid,
75                                      uint16_t uid_counter, uint8_t num_attr,
76                                      btrc_media_attr_t* p_attrs,
77                                      const RawAddress& bd_addr);
78     virtual void PlayItemCallback(uint8_t scope, uint16_t uid_counter,
79                                   uint8_t* uid, const RawAddress& bd_addr);
80     virtual void GetTotalNumOfItemsCallback(uint8_t scope,
81                                             const RawAddress& bd_addr);
82     virtual void SearchCallback(uint16_t str_len, uint8_t* p_str,
83                                 const RawAddress& bd_addr);
84     virtual void AddToNowPlayingCallback(uint8_t scope, uint8_t* uid,
85                                          uint16_t uid_counter,
86                                          const RawAddress& bd_addr);
87   };
88 
89   class ControlObserver {
90    public:
91     virtual ~ControlObserver() = default;
92 
93     virtual void PassthroughRspCallback(const RawAddress& bd_addr, int id,
94                                         int key_state);
95     virtual void GroupnavigationRspCallback(int id, int key_state);
96     virtual void ConnectionStateCallback(bool rc_connect, bool bt_connect,
97                                          const RawAddress& bd_addr);
98     virtual void CtrlGetrcfeaturesCallback(const RawAddress& bd_addr,
99                                            int features);
100     virtual void CtrlSetplayerapplicationsettingRspCallback(
101         const RawAddress& bd_addr, uint8_t accepted);
102     virtual void CtrlPlayerapplicationsettingCallback(
103         const RawAddress& bd_addr, uint8_t num_attr,
104         btrc_player_app_attr_t* app_attrs, uint8_t num_ext_attr,
105         btrc_player_app_ext_attr_t* ext_attrs);
106     virtual void CtrlPlayerapplicationsettingChangedCallback(
107         const RawAddress& bd_addr, const btrc_player_settings_t& p_vals);
108     virtual void CtrlSetabsvolCmdCallback(const RawAddress& bd_addr,
109                                           uint8_t abs_vol, uint8_t label);
110     virtual void CtrlRegisternotificationAbsVolCallback(
111         const RawAddress& bd_addr, uint8_t label);
112     virtual void CtrlTrackChangedCallback(const RawAddress& bd_addr,
113                                           uint8_t num_attr,
114                                           btrc_element_attr_val_t* p_attrs);
115     virtual void CtrlPlayPositionChangedCallback(const RawAddress& bd_addr,
116                                                  uint32_t song_len,
117                                                  uint32_t song_pos);
118     virtual void CtrlPlayStatusChangedCallback(const RawAddress& bd_addr,
119                                                btrc_play_status_t play_status);
120     virtual void CtrlGetFolderItemsCallback(
121         const RawAddress& bd_addr, btrc_status_t status,
122         const btrc_folder_items_t* folder_items, uint8_t count);
123     virtual void CtrlChangePathCallback(const RawAddress& bd_addr,
124                                         uint32_t count);
125     virtual void CtrlSetBrowsedPlayerCallback(const RawAddress& bd_addr,
126                                               uint8_t num_items, uint8_t depth);
127     virtual void CtrlSetAddressedPlayerCallback(const RawAddress& bd_addr,
128                                                 uint8_t status);
129   };
130 
131   // Initialize and clean up the BluetoothInterface singleton. Returns false if
132   // the underlying HAL interface failed to initialize, and true on success.
133   static bool Initialize();
134 
135   // Shuts down and cleans up the interface. CleanUp must be called on the same
136   // thread that called Initialize.
137   static void CleanUp();
138 
139   // Returns true if the interface was initialized and a global singleton has
140   // been created.
141   static bool IsInitialized();
142 
143   // Initialize for testing. Use this to inject a test version of
144   // BluetoothAvrcpInterface. To be used from unit tests only.
145   static void InitializeForTesting(BluetoothAvrcpInterface* test_instance);
146 
147   // Returns the BluetoothAvrcpInterface singleton. If the interface has
148   // not been initialized, returns nullptr. This method is thread-safe, in that
149   // it will block if the internal lock is being held by another thread. Don't
150   // call this re-entrantly from an observer event as this may cause a deadlock.
151   static BluetoothAvrcpInterface* Get();
152 
153   virtual bool AvrcpControlEnable() = 0;
154   virtual void AvrcpControlDisable() = 0;
155   virtual bool AvrcpTargetEnable() = 0;
156   virtual void AvrcpTargetDisable() = 0;
157 
158   // Thread-safety is guaranteed by ObserverList.
159   virtual void AddTargetObserver(TargetObserver* observer) = 0;
160   virtual void RemoveTargetObserver(TargetObserver* observer) = 0;
161 
162   // Thread-safety is guaranteed by ObserverList.
163   virtual void AddControlObserver(ControlObserver* observer) = 0;
164   virtual void RemoveControlObserver(ControlObserver* observer) = 0;
165 
166   // The HAL module pointers provided by the shared Bluetooth library
167   virtual const btrc_interface_t* GetTargetHALInterface() const = 0;
168   virtual const btrc_ctrl_interface_t* GetControlHALInterface() const = 0;
169 
170  protected:
171   BluetoothAvrcpInterface() = default;
172   virtual ~BluetoothAvrcpInterface() = default;
173 
174  private:
175   DISALLOW_COPY_AND_ASSIGN(BluetoothAvrcpInterface);
176 };
177 
178 }  // namespace hal
179 }  // namespace bluetooth
180