1 /*
2  * Copyright 2018 The Android Open Source Project
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 <map>
20 #include <memory>
21 #include <mutex>
22 
23 #include "hardware/avrcp/avrcp.h"
24 #include "osi/include/properties.h"
25 #include "profile/avrcp/connection_handler.h"
26 #include "raw_address.h"
27 
28 namespace bluetooth {
29 namespace avrcp {
30 
31 /**
32  * AvrcpService is the management interface for AVRCP Target. It handles any
33  * required thread switching, interface registration, and provides an API
34  * for connecting and disconnecting devices.
35  * TODO (apanicke): Instead of providing a service interface implementation,
36  * have the AvrcpService itself be its interface so we don't have to access
37  * it indirectly.
38  */
39 class AvrcpService : public MediaCallbacks {
40  public:
41   /**
42    * Gets a handle to the AvrcpService
43    *
44    * Currently used by A2DP to tell AVRCP to initiate a connection to the
45    * remote device.
46    */
47   static AvrcpService* Get();
48 
49   /**
50    * Returns an interface to control this service. The Avrcp::ServiceInterface
51    * handles all thread switching between the caller thread and the thread the
52    * service runs on, that way whoever uses the interface doesn't need to be
53    * aware which thread the service runs on.
54    */
55   static ServiceInterface* GetServiceInterface();
56 
57   void Init(MediaInterface* media_interface, VolumeInterface* volume_interface,
58             PlayerSettingsInterface* player_settings_interface);
59   void Cleanup();
60 
61   void RegisterBipServer(int psm);
62   void UnregisterBipServer();
63 
64   void ConnectDevice(const RawAddress& bdaddr);
65   void DisconnectDevice(const RawAddress& bdaddr);
66 
67   void SetBipClientStatus(const RawAddress& bdaddr, bool connected);
68 
69   // Functions inherited from MediaCallbacks in order to receive updates
70   void SendMediaUpdate(bool track_changed, bool play_state,
71                        bool queue) override;
72   void SendFolderUpdate(bool available_players, bool addressed_player,
73                         bool queue) override;
74   void SendActiveDeviceChanged(const RawAddress& address) override;
75 
76   void SendPlayerSettingsChanged(std::vector<PlayerAttribute> attributes,
77                                  std::vector<uint8_t> values) override;
78 
79   bool IsDeviceConnected(const RawAddress& bdaddr);
80 
81   /** when a2dp connected, btif will start register vol changed, so we need a
82    * interface for it. */
83   void RegisterVolChanged(const RawAddress& bdaddr);
84 
85   class ServiceInterfaceImpl : public ServiceInterface {
86    public:
87     void Init(MediaInterface* media_interface,
88               VolumeInterface* volume_interface,
89               PlayerSettingsInterface* player_settings_interface) override;
90     void RegisterBipServer(int psm) override;
91     void UnregisterBipServer() override;
92     bool ConnectDevice(const RawAddress& bdaddr) override;
93     bool DisconnectDevice(const RawAddress& bdaddr) override;
94     void SetBipClientStatus(const RawAddress& bdaddr, bool connected) override;
95     bool Cleanup() override;
96 
97    private:
98     std::mutex service_interface_lock_;
99   };
100 
101   static void DebugDump(int fd);
102 
103  protected:
104   void DeviceCallback(std::shared_ptr<Device> device);
105   uint16_t GetSupportedFeatures(uint16_t profile_version);
106 
107  private:
108   static AvrcpService* instance_;
109   static ServiceInterfaceImpl* service_interface_;
110 
111   uint32_t sdp_record_handle = -1;
112   uint32_t ct_sdp_record_handle = -1;
113   uint16_t profile_version = -1;
114 
115   MediaInterface* media_interface_ = nullptr;
116   VolumeInterface* volume_interface_ = nullptr;
117   PlayerSettingsInterface* player_settings_interface_ = nullptr;
118 
119   ConnectionHandler* connection_handler_;
120 };
121 
122 }  // namespace avrcp
123 }  // namespace bluetooth
124 
is_new_avrcp_enabled()125 inline bool is_new_avrcp_enabled() {
126   return osi_property_get_bool("bluetooth.profile.avrcp.target.enabled", false);
127 }
128