bluetooth_hid_detector.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2022 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_COMPONENTS_HID_DETECTION_BLUETOOTH_HID_DETECTOR_H_
  5. #define ASH_COMPONENTS_HID_DETECTION_BLUETOOTH_HID_DETECTOR_H_
  6. #include <string>
  7. #include "third_party/abseil-cpp/absl/types/optional.h"
  8. namespace ash::hid_detection {
  9. struct BluetoothHidPairingState {
  10. BluetoothHidPairingState(const std::string& code, uint8_t num_keys_entered);
  11. BluetoothHidPairingState(BluetoothHidPairingState&& other);
  12. BluetoothHidPairingState& operator=(BluetoothHidPairingState&& other);
  13. ~BluetoothHidPairingState();
  14. // The code required to be entered for the HID to pair.
  15. std::string code;
  16. // The number of keys of the code which have been entered.
  17. uint8_t num_keys_entered;
  18. };
  19. // Manages searching for unpaired Bluetooth human interactive devices and
  20. // automatically attempting to pairing with them if their device type is not
  21. // currently paired with.
  22. class BluetoothHidDetector {
  23. public:
  24. struct InputDevicesStatus {
  25. bool pointer_is_missing;
  26. bool keyboard_is_missing;
  27. };
  28. enum class BluetoothHidType {
  29. // A mouse, trackball, touchpad, etc.
  30. kPointer,
  31. kKeyboard,
  32. kKeyboardPointerCombo
  33. };
  34. // Struct representing a Bluetooth human-interactive device.
  35. struct BluetoothHidMetadata {
  36. BluetoothHidMetadata(std::string name, BluetoothHidType type);
  37. BluetoothHidMetadata(BluetoothHidMetadata&& other);
  38. BluetoothHidMetadata& operator=(BluetoothHidMetadata&& other);
  39. ~BluetoothHidMetadata();
  40. std::string name;
  41. BluetoothHidType type;
  42. };
  43. // Struct representing the current status of BluetoothHidDetector.
  44. struct BluetoothHidDetectionStatus {
  45. BluetoothHidDetectionStatus(
  46. absl::optional<BluetoothHidMetadata> current_pairing_device,
  47. absl::optional<BluetoothHidPairingState> pairing_state);
  48. BluetoothHidDetectionStatus(BluetoothHidDetectionStatus&& other);
  49. BluetoothHidDetectionStatus& operator=(BluetoothHidDetectionStatus&& other);
  50. ~BluetoothHidDetectionStatus();
  51. // The metadata of the device currently being paired with.
  52. absl::optional<BluetoothHidMetadata> current_pairing_device;
  53. // Set if the current pairing requires a code that should be displayed to
  54. // the user to enter. This will always be null if |current_pairing_device|
  55. // is null.
  56. absl::optional<BluetoothHidPairingState> pairing_state;
  57. };
  58. class Delegate {
  59. public:
  60. virtual ~Delegate() = default;
  61. // Invoked whenever any Bluetooth detection status property changes.
  62. virtual void OnBluetoothHidStatusChanged() = 0;
  63. };
  64. virtual ~BluetoothHidDetector();
  65. // Begins scanning for Bluetooth devices. Invokes
  66. // OnBluetoothHidStatusChanged() for |delegate| whenever the HID detection
  67. // status updates. Calling this method when HID detection has already started
  68. // is an error.
  69. void StartBluetoothHidDetection(Delegate* delegate,
  70. InputDevicesStatus input_devices_status);
  71. // Stops scanning for Bluetooth devices. |is_using_bluetooth| indicates
  72. // whether or not there is at least one HID connected via Bluetooth. Calling
  73. // this method when HID detection has not been started is an error.
  74. void StopBluetoothHidDetection(bool is_using_bluetooth);
  75. // Informs BluetoothHidDetector which HID types have been connected.
  76. virtual void SetInputDevicesStatus(
  77. InputDevicesStatus input_devices_status) = 0;
  78. // Fetches the current Bluetooth HID detection status.
  79. virtual const BluetoothHidDetectionStatus
  80. GetBluetoothHidDetectionStatus() = 0;
  81. protected:
  82. BluetoothHidDetector();
  83. // Implementation-specific version of StartBluetoothHidDetection().
  84. virtual void PerformStartBluetoothHidDetection(
  85. InputDevicesStatus input_devices_status) = 0;
  86. // Implementation-specific version of StopBluetoothHidDetection().
  87. virtual void PerformStopBluetoothHidDetection(bool is_using_bluetooth) = 0;
  88. // Notifies |delegate_| of status changes; should be called by derived
  89. // types to notify observers of status changes.
  90. void NotifyBluetoothHidDetectionStatusChanged();
  91. private:
  92. Delegate* delegate_ = nullptr;
  93. };
  94. } // namespace ash::hid_detection
  95. #endif // ASH_COMPONENTS_HID_DETECTION_BLUETOOTH_HID_DETECTOR_H_