• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //
2 //  Copyright (C) 2017 Google, Inc.
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 <atomic>
20 #include <mutex>
21 
22 #include "base/macros.h"
23 
24 #include "bluetooth/uuid.h"
25 #include "service/bluetooth_instance.h"
26 #include "service/common/bluetooth/avrcp_media_attr.h"
27 #include "service/common/bluetooth/service.h"
28 #include "service/hal/bluetooth_avrcp_interface.h"
29 
30 namespace bluetooth {
31 
32 class AvrcpControl : public BluetoothInstance,
33                      private hal::BluetoothAvrcpInterface::ControlObserver {
34  public:
35   class Delegate {
36    public:
37     virtual void OnConnectionState(bool rc_connect, bool bt_connect,
38                                    const std::string& device_address) = 0;
39 
40     virtual void OnTrackChanged(const std::string& device_address,
41                                 const AvrcpMediaAttr& attr) = 0;
42 
43     virtual void OnSetAbsVolumeRequest(const std::string& device_address,
44                                        int32_t abs_vol, int32_t label) = 0;
45 
46     virtual void OnRegisterForAbsVolumeCallbackRequest(
47         const std::string& device_address, int32_t label) = 0;
48 
49    protected:
50     virtual ~Delegate() = default;
51   };
52 
53   // The destructor automatically unregisters this instance from the stack.
54   ~AvrcpControl() override;
55 
56   // Assigns a delegate to this instance. |delegate| must out-live this
57   // AvrcpControl instance.
58   void SetDelegate(Delegate* delegate);
59 
60   // BluetoothClientInstace overrides:
61   const Uuid& GetAppIdentifier() const override;
62   int GetInstanceId() const override;
63 
64   bool Enable();
65   void Disable();
66 
67   // Send a remote control button command. Commands which can be sent
68   // are defined here:
69   // http://1394ta.org/wp-content/uploads/2015/07/2007001.pdf
70   bool SendPassThroughCommand(const std::string& device_address,
71                               uint8_t key_code, bool key_pressed);
72 
73   // Send a response to a request to change absolute volume.
74   bool SetAbsVolumeResponse(const std::string& device_address, int32_t abs_vol,
75                             int32_t label);
76 
77   // Send a response to a register for absolute volume change callback.
78   bool RegisterForAbsVolumeCallbackResponse(const std::string& device_address,
79                                             int32_t response_type,
80                                             int32_t abs_vol, int32_t label);
81 
82  private:
83   friend class AvrcpControlFactory;
84 
85   // Constructor shouldn't be called directly as instances are meant to be
86   // obtained from the factory.
87   AvrcpControl(const Uuid& uuid, int control_id);
88 
89   // hal::BluetoothAvrcpInterface::ControlObserver implementation:
90   void ConnectionStateCallback(bool rc_connect, bool bt_connect,
91                                const RawAddress& bd_addr) override;
92   void CtrlSetabsvolCmdCallback(const RawAddress& bd_addr, uint8_t abs_vol,
93                                 uint8_t label) override;
94   void CtrlRegisternotificationAbsVolCallback(const RawAddress& bd_addr,
95                                               uint8_t label) override;
96   void CtrlTrackChangedCallback(const RawAddress& bd_addr, uint8_t num_attr,
97                                 btrc_element_attr_val_t* p_attrs) override;
98 
99   // See getters for documentation.
100   const Uuid app_identifier_;
101   const int control_id_;
102 
103   // Mutex that synchronizes access to the entries below.
104   std::mutex mutex_;
105 
106   // Raw handle to the Delegate, which must outlive this AvrcpControl instance.
107   std::mutex delegate_mutex_;
108   Delegate* delegate_ = nullptr;
109 
110   DISALLOW_COPY_AND_ASSIGN(AvrcpControl);
111 };
112 
113 // AvrcpControlFactory is used to register and obtain a per-application
114 // AvrcpControl
115 // instance. Users should call RegisterClient to obtain their own unique
116 // AvrcpControl instance that has been registered with the Bluetooth stack.
117 class AvrcpControlFactory
118     : public BluetoothInstanceFactory,
119       private hal::BluetoothAvrcpInterface::ControlObserver {
120  public:
121   // Don't construct/destruct directly except in tests. Instead, obtain a handle
122   // from an Adapter instance.
123   AvrcpControlFactory();
124   ~AvrcpControlFactory() override;
125 
126   // BluetoothInstanceFactory override:
127   bool RegisterInstance(const Uuid& uuid,
128                         const RegisterCallback& callback) override;
129 
130  private:
131   std::atomic<int> next_control_id_{0};
132   DISALLOW_COPY_AND_ASSIGN(AvrcpControlFactory);
133 };
134 
135 }  // namespace bluetooth
136