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