avrcp_control.h 4.8 KB

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