message_stream.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // Copyright 2021 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef ASH_QUICK_PAIR_MESSAGE_STREAM_MESSAGE_STREAM_H_
  5. #define ASH_QUICK_PAIR_MESSAGE_STREAM_MESSAGE_STREAM_H_
  6. #include <string>
  7. #include "ash/services/quick_pair/quick_pair_process_manager.h"
  8. #include "base/containers/circular_deque.h"
  9. #include "base/memory/scoped_refptr.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/observer_list.h"
  12. #include "base/observer_list_types.h"
  13. #include "device/bluetooth/bluetooth_socket.h"
  14. #include "third_party/abseil-cpp/absl/types/optional.h"
  15. namespace net {
  16. class IOBuffer;
  17. } // namespace net
  18. namespace ash {
  19. namespace quick_pair {
  20. // Receives MessageStreamMessage bytes from a given BluetoothSocket and uses
  21. // the FastPairDataParser to parse a MessageStreamMessage from the data, and
  22. // then notifies observers of the corresponding message data, and stores them.
  23. class MessageStream {
  24. public:
  25. class Observer : public base::CheckedObserver {
  26. public:
  27. // Model ID message:
  28. // https://developers.google.com/nearby/fast-pair/spec#model_id_2
  29. virtual void OnModelIdMessage(const std::string& device_address,
  30. const std::string& model_id) {}
  31. // BLE Address Update message:
  32. // https://developers.google.com/nearby/fast-pair/spec#ble_address_2
  33. virtual void OnBleAddressUpdateMessage(const std::string& device_address,
  34. const std::string& ble_address) {}
  35. // Batter Update message:
  36. // https://developers.google.com/nearby/fast-pair/spec#battery_updated
  37. virtual void OnBatteryUpdateMessage(
  38. const std::string& device_address,
  39. const mojom::BatteryUpdatePtr& battery_update) {}
  40. // Remaining Battery Time message:
  41. // https://developers.google.com/nearby/fast-pair/spec#battery_updated
  42. virtual void OnRemainingBatteryTimeMessage(
  43. const std::string& device_address,
  44. uint16_t remaining_battery_time) {}
  45. // Silence Mode message:
  46. // https://developers.google.com/nearby/fast-pair/spec#SilenceMode
  47. virtual void OnEnableSilenceModeMessage(const std::string& device_address,
  48. bool enable_silence_mode) {}
  49. // Companion App Log Buffer full message:
  50. // https://developers.google.com/nearby/fast-pair/spec#companion_app_events
  51. virtual void OnCompanionAppLogBufferFullMessage(
  52. const std::string& device_address) {}
  53. // Active components message:
  54. // https://developers.google.com/nearby/fast-pair/spec#MessageStreamActiveComponents
  55. virtual void OnActiveComponentsMessage(const std::string& device_address,
  56. uint8_t active_components_byte) {}
  57. // Ring device message:
  58. // https://developers.google.com/nearby/fast-pair/spec#ringing_a_device
  59. virtual void OnRingDeviceMessage(const std::string& device_address,
  60. const mojom::RingDevicePtr& ring_device) {}
  61. // Acknowledgement message:
  62. // https://developers.google.com/nearby/fast-pair/spec#MessageStreamAcknowledgements
  63. virtual void OnAcknowledgementMessage(
  64. const std::string& device_address,
  65. const mojom::AcknowledgementMessagePtr& acknowledgement) {}
  66. // Platform type message:
  67. // https://developers.google.com/nearby/fast-pair/spec#PlatformType
  68. virtual void OnAndroidSdkVersionMessage(const std::string& device_address,
  69. uint8_t sdk_version) {}
  70. // Observers are notified when the socket is disconnected, which means the
  71. // MessageStream will no longer receive new messages.
  72. virtual void OnDisconnected(const std::string& device_address) = 0;
  73. // Observers are notified when the MessageStream is being destroyed to
  74. // alert them to clean up their MessageStream memory objects.
  75. virtual void OnMessageStreamDestroyed(
  76. const std::string& device_address) = 0;
  77. };
  78. MessageStream(const std::string& device_address,
  79. scoped_refptr<device::BluetoothSocket> socket);
  80. MessageStream(const MessageStream&) = delete;
  81. MessageStream& operator=(const MessageStream&) = delete;
  82. ~MessageStream();
  83. void AddObserver(Observer* observer);
  84. void RemoveObserver(Observer* observer);
  85. void Disconnect(base::OnceClosure on_disconnect_callback);
  86. // Return buffer of messages.
  87. const base::circular_deque<mojom::MessageStreamMessagePtr>& messages() {
  88. return messages_;
  89. }
  90. private:
  91. // Attempts to receive data from socket
  92. void Receive();
  93. // Socket disconnected callbacks
  94. void OnSocketDisconnected();
  95. void OnSocketDisconnectedWithCallback(
  96. base::OnceClosure on_disconnect_callback);
  97. // Receive data from socket callbacks
  98. void ReceiveDataSuccess(int buffer_size,
  99. scoped_refptr<net::IOBuffer> io_buffer);
  100. void ReceiveDataError(device::BluetoothSocket::ErrorReason error,
  101. const std::string& error_message);
  102. // ParseMessageStreamMessage callbacks
  103. void ParseMessageStreamSuccess(
  104. std::vector<mojom::MessageStreamMessagePtr> messages);
  105. void OnUtilityProcessStopped(
  106. QuickPairProcessManager::ShutdownReason shutdown_reason);
  107. // Checks all fields of the union for the MessageStreamMessage type, and
  108. // notifies observers with the proper method based on the value stored.
  109. void NotifyObservers(const mojom::MessageStreamMessagePtr& message);
  110. int receive_retry_counter_ = 0;
  111. std::string device_address_;
  112. // The circular deque of messages is capped at |1000| messages, and old
  113. // messages will be removed from the front when new messages are added once
  114. // it reaches capacity.
  115. base::circular_deque<mojom::MessageStreamMessagePtr> messages_;
  116. scoped_refptr<device::BluetoothSocket> socket_;
  117. base::ObserverList<Observer> observers_;
  118. base::WeakPtrFactory<MessageStream> weak_ptr_factory_{this};
  119. };
  120. } // namespace quick_pair
  121. } // namespace ash
  122. #endif // ASH_QUICK_PAIR_MESSAGE_STREAM_MESSAGE_STREAM_H_