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 
22 #include "hardware/avrcp/avrcp.h"
23 #include "osi/include/properties.h"
24 #include "profile/avrcp/connection_handler.h"
25 #include "raw_address.h"
26 
27 namespace bluetooth {
28 namespace avrcp {
29 
30 /**
31  * AvrcpService is the management interface for AVRCP Target. It handles any
32  * required thread switching, interface registration, and provides an API
33  * for connecting and disconnecting devices.
34  * TODO (apanicke): Instead of providing a service interface implementation,
35  * have the AvrcpService itself be its interface so we don't have to access
36  * it indirectly.
37  */
38 class AvrcpService : public MediaCallbacks {
39  public:
40   /**
41    * Gets a handle to the AvrcpService
42    *
43    * Currently used by A2DP to tell AVRCP to initiate a connection to the
44    * remote device.
45    */
46   static AvrcpService* Get();
47 
48   /**
49    * Returns an interface to control this service. The Avrcp::ServiceInterface
50    * handles all thread switching between the caller thread and the thread the
51    * service runs on, that way whoever uses the interface doesn't need to be
52    * aware which thread the service runs on.
53    */
54   static ServiceInterface* GetServiceInterface();
55 
56   void Init(MediaInterface* media_interface, VolumeInterface* volume_interface);
57   void Cleanup();
58 
59   void ConnectDevice(const RawAddress& bdaddr);
60   void DisconnectDevice(const RawAddress& bdaddr);
61 
62   // Functions inherited from MediaCallbacks in order to receive updates
63   void SendMediaUpdate(bool track_changed, bool play_state,
64                        bool queue) override;
65   void SendFolderUpdate(bool available_players, bool addressed_player,
66                         bool queue) override;
67   void SendActiveDeviceChanged(const RawAddress& address) override;
68 
69   class ServiceInterfaceImpl : public ServiceInterface {
70    public:
71     void Init(MediaInterface* media_interface,
72               VolumeInterface* volume_interface) override;
73     bool ConnectDevice(const RawAddress& bdaddr) override;
74     bool DisconnectDevice(const RawAddress& bdaddr) override;
75     bool Cleanup() override;
76 
77    private:
78     std::mutex service_interface_lock_;
79   };
80 
81   static void DebugDump(int fd);
82 
83  protected:
84   void DeviceCallback(std::shared_ptr<Device> device);
85 
86  private:
87   static AvrcpService* instance_;
88   static ServiceInterfaceImpl* service_interface_;
89 
90   MediaInterface* media_interface_ = nullptr;
91   VolumeInterface* volume_interface_ = nullptr;
92 
93   ConnectionHandler* connection_handler_;
94 };
95 
96 }  // namespace avrcp
97 }  // namespace bluetooth
98 
is_new_avrcp_enabled()99 inline bool is_new_avrcp_enabled() {
100   return osi_property_get_bool("persist.bluetooth.enablenewavrcp", true);
101 }