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