avrcp_target.h 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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 <mutex>
  18. #include <string>
  19. #include <vector>
  20. #include "base/macros.h"
  21. #include "service/common/bluetooth/avrcp_int_value.h"
  22. #include "service/common/bluetooth/avrcp_register_notification_response.h"
  23. #include "service/common/bluetooth/avrcp_string_value.h"
  24. #include "service/bluetooth_instance.h"
  25. #include "service/hal/bluetooth_avrcp_interface.h"
  26. namespace bluetooth {
  27. // Note: presently this only supports
  28. // (BTRC_FEAT_METADATA | BTRC_FEAT_ABSOLUTE_VOLUME)
  29. class AvrcpTarget : public BluetoothInstance,
  30. private hal::BluetoothAvrcpInterface::TargetObserver {
  31. public:
  32. // We only allow one instance of this object at a time.
  33. static const int kSingletonInstanceId;
  34. class Delegate {
  35. public:
  36. virtual void OnGetRemoteFeatures(const std::string& addr,
  37. int32_t features) = 0;
  38. virtual void OnGetPlayStatus(const std::string& addr) = 0;
  39. virtual void OnListPlayerAppAttr(const std::string& addr) = 0;
  40. virtual void OnListPlayerAppValues(const std::string& addr,
  41. int32_t attr_id) = 0;
  42. virtual void OnGetPlayerAppValue(const std::string& addr,
  43. const std::vector<int32_t>& attrs) = 0;
  44. virtual void OnGetPlayerAppAttrsText(const std::string& addr,
  45. const std::vector<int32_t>& attrs) = 0;
  46. virtual void OnGetPlayerAppValuesText(
  47. const std::string& addr, int32_t attr_id,
  48. const std::vector<int32_t>& values) = 0;
  49. virtual void OnSetPlayerAppValue(
  50. const std::string& addr, const std::vector<AvrcpIntValue>& values) = 0;
  51. virtual void OnGetElementAttrs(const std::string& addr,
  52. const std::vector<int32_t>& attrs) = 0;
  53. virtual void OnRegisterNotification(const std::string& addr,
  54. int32_t event_id, uint32_t param) = 0;
  55. virtual void OnVolumeChange(const std::string& addr, int32_t volume,
  56. int32_t ctype) = 0;
  57. virtual void OnPassThroughCommand(const std::string& addr, int32_t id,
  58. int32_t key_state) = 0;
  59. protected:
  60. virtual ~Delegate() = default;
  61. };
  62. // The destructor automatically unregisters this instance from the stack.
  63. ~AvrcpTarget() override;
  64. // Assigns a delegate to this instance. |delegate| must out-live this
  65. // AvrcpTarget instance.
  66. void SetDelegate(Delegate* delegate);
  67. // BluetoothClientInstace overrides:
  68. const Uuid& GetAppIdentifier() const override;
  69. int GetInstanceId() const override;
  70. bool Enable();
  71. void Disable();
  72. bool GetPlayStatusResponse(const std::string& addr, int32_t play_status,
  73. uint32_t song_len, uint32_t song_pos);
  74. bool ListPlayerAppAttrResponse(const std::string& addr,
  75. const std::vector<int32_t>& attrs);
  76. bool GetPlayerAppValueResponse(const std::string& addr,
  77. const std::vector<AvrcpIntValue>& values);
  78. bool GetPlayerAppAttrTextResponse(const std::string& addr,
  79. const std::vector<AvrcpStringValue>& attrs);
  80. bool GetPlayerAppValueTextResponse(
  81. const std::string& addr, const std::vector<AvrcpStringValue>& attrs);
  82. bool GetElementAttrResponse(const std::string& addr,
  83. const std::vector<AvrcpStringValue>& attrs);
  84. bool SetPlayerAppValueResponse(const std::string& addr, int32_t rsp_status);
  85. bool RegisterNotificationResponse(
  86. int32_t event_id, int32_t type,
  87. const AvrcpRegisterNotificationResponse& param);
  88. bool SetVolume(int volume);
  89. private:
  90. friend class AvrcpTargetFactory;
  91. // Constructor shouldn't be called directly as instances are meant to be
  92. // obtained from the factory.
  93. AvrcpTarget(const Uuid& uuid);
  94. // hal::BluetoothAvrcpInterface::TargetObserver implementation:
  95. void RemoteFeaturesCallback(const RawAddress& bd_addr,
  96. btrc_remote_features_t features) override;
  97. void GetPlayStatusCallback(const RawAddress& bd_addr) override;
  98. void ListPlayerAppAttrCallback(const RawAddress& bd_addr) override;
  99. void ListPlayerAppValuesCallback(btrc_player_attr_t attr_id,
  100. const RawAddress& bd_addr) override;
  101. void GetPlayerAppValueCallback(uint8_t num_attr, btrc_player_attr_t* p_attrs,
  102. const RawAddress& bd_addr) override;
  103. void GetPlayerAppAttrsTextCallback(uint8_t num_attr,
  104. btrc_player_attr_t* p_attrs,
  105. const RawAddress& bd_addr) override;
  106. void GetPlayerAppValuesTextCallback(uint8_t attr_id, uint8_t num_val,
  107. uint8_t* p_vals,
  108. const RawAddress& bd_addr) override;
  109. void SetPlayerAppValueCallback(btrc_player_settings_t* p_vals,
  110. const RawAddress& bd_addr) override;
  111. void GetElementAttrCallback(uint8_t num_attr, btrc_media_attr_t* p_attrs,
  112. const RawAddress& bd_addr) override;
  113. void RegisterNotificationCallback(btrc_event_id_t event_id, uint32_t param,
  114. const RawAddress& bd_addr) override;
  115. void VolumeChangeCallback(uint8_t volume, uint8_t ctype,
  116. const RawAddress& bd_addr) override;
  117. void PassthroughCmdCallback(int id, int key_state,
  118. const RawAddress& bd_addr) override;
  119. // See getters for documentation.
  120. const Uuid app_identifier_;
  121. // Mutex that synchronizes access to the entries below.
  122. std::mutex mutex_;
  123. // Raw handle to the Delegate, which must outlive this AvrcpTarget instance.
  124. std::mutex delegate_mutex_;
  125. Delegate* delegate_ = nullptr;
  126. DISALLOW_COPY_AND_ASSIGN(AvrcpTarget);
  127. };
  128. // AvrcpTargetFactory is used to register and obtain a per-application
  129. // AvrcpTarget
  130. // instance. Users should call RegisterClient to obtain their own unique
  131. // AvrcpTarget instance that has been registered with the Bluetooth stack.
  132. class AvrcpTargetFactory
  133. : public BluetoothInstanceFactory,
  134. private hal::BluetoothAvrcpInterface::TargetObserver {
  135. public:
  136. // Don't construct/destruct directly except in tests. Instead, obtain a handle
  137. // from an Adapter instance.
  138. AvrcpTargetFactory();
  139. ~AvrcpTargetFactory() override;
  140. // BluetoothInstanceFactory override:
  141. bool RegisterInstance(const Uuid& uuid,
  142. const RegisterCallback& callback) override;
  143. private:
  144. DISALLOW_COPY_AND_ASSIGN(AvrcpTargetFactory);
  145. };
  146. } // namespace bluetooth