• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   class ServiceInterfaceImpl : public ServiceInterface {
80    public:
81     void Init(MediaInterface* media_interface,
82               VolumeInterface* volume_interface,
83               PlayerSettingsInterface* player_settings_interface) override;
84     void RegisterBipServer(int psm) override;
85     void UnregisterBipServer() override;
86     bool ConnectDevice(const RawAddress& bdaddr) override;
87     bool DisconnectDevice(const RawAddress& bdaddr) override;
88     void SetBipClientStatus(const RawAddress& bdaddr, bool connected) override;
89     bool Cleanup() override;
90 
91    private:
92     std::mutex service_interface_lock_;
93   };
94 
95   static void DebugDump(int fd);
96 
97  protected:
98   void DeviceCallback(std::shared_ptr<Device> device);
99   uint16_t GetSupportedFeatures(uint16_t profile_version);
100 
101  private:
102   static AvrcpService* instance_;
103   static ServiceInterfaceImpl* service_interface_;
104 
105   uint32_t sdp_record_handle = -1;
106   uint32_t ct_sdp_record_handle = -1;
107   uint16_t profile_version = -1;
108 
109   MediaInterface* media_interface_ = nullptr;
110   VolumeInterface* volume_interface_ = nullptr;
111   PlayerSettingsInterface* player_settings_interface_ = nullptr;
112 
113   ConnectionHandler* connection_handler_;
114 };
115 
116 }  // namespace avrcp
117 }  // namespace bluetooth
118 
is_new_avrcp_enabled()119 inline bool is_new_avrcp_enabled() {
120   return osi_property_get_bool("bluetooth.profile.avrcp.target.enabled", false);
121 }
122