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   void Cleanup();
59 
60   void RegisterBipServer(int psm);
61   void UnregisterBipServer();
62 
63   void ConnectDevice(const RawAddress& bdaddr);
64   void DisconnectDevice(const RawAddress& bdaddr);
65 
66   void SetBipClientStatus(const RawAddress& bdaddr, bool connected);
67 
68   // Functions inherited from MediaCallbacks in order to receive updates
69   void SendMediaUpdate(bool track_changed, bool play_state,
70                        bool queue) override;
71   void SendFolderUpdate(bool available_players, bool addressed_player,
72                         bool queue) override;
73   void SendActiveDeviceChanged(const RawAddress& address) override;
74 
75   class ServiceInterfaceImpl : public ServiceInterface {
76    public:
77     void Init(MediaInterface* media_interface,
78               VolumeInterface* volume_interface) override;
79     void RegisterBipServer(int psm) override;
80     void UnregisterBipServer() override;
81     bool ConnectDevice(const RawAddress& bdaddr) override;
82     bool DisconnectDevice(const RawAddress& bdaddr) override;
83     void SetBipClientStatus(const RawAddress& bdaddr, bool connected) override;
84     bool Cleanup() override;
85 
86    private:
87     std::mutex service_interface_lock_;
88   };
89 
90   static void DebugDump(int fd);
91 
92  protected:
93   void DeviceCallback(std::shared_ptr<Device> device);
94   uint16_t GetSupportedFeatures(uint16_t profile_version);
95 
96  private:
97   static AvrcpService* instance_;
98   static ServiceInterfaceImpl* service_interface_;
99 
100   uint32_t sdp_record_handle = -1;
101   uint16_t profile_version = -1;
102 
103   MediaInterface* media_interface_ = nullptr;
104   VolumeInterface* volume_interface_ = nullptr;
105 
106   ConnectionHandler* connection_handler_;
107 };
108 
109 }  // namespace avrcp
110 }  // namespace bluetooth
111 
is_new_avrcp_enabled()112 inline bool is_new_avrcp_enabled() {
113   return osi_property_get_bool("persist.bluetooth.enablenewavrcp", true);
114 }
115