fido_ble_transaction.h 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Copyright 2017 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 DEVICE_FIDO_CABLE_FIDO_BLE_TRANSACTION_H_
  5. #define DEVICE_FIDO_CABLE_FIDO_BLE_TRANSACTION_H_
  6. #include <memory>
  7. #include <vector>
  8. #include "base/component_export.h"
  9. #include "base/containers/queue.h"
  10. #include "base/memory/weak_ptr.h"
  11. #include "base/timer/timer.h"
  12. #include "device/fido/cable/fido_ble_frames.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace device {
  15. class FidoBleConnection;
  16. // This class encapsulates logic related to a single U2F BLE request and
  17. // response. FidoBleTransaction is owned by FidoBleDevice, which is the only
  18. // class that should make use of this class.
  19. class COMPONENT_EXPORT(DEVICE_FIDO) FidoBleTransaction {
  20. public:
  21. using FrameCallback = base::OnceCallback<void(absl::optional<FidoBleFrame>)>;
  22. FidoBleTransaction(FidoBleConnection* connection,
  23. uint16_t control_point_length);
  24. FidoBleTransaction(const FidoBleTransaction&) = delete;
  25. FidoBleTransaction& operator=(const FidoBleTransaction&) = delete;
  26. ~FidoBleTransaction();
  27. void WriteRequestFrame(FidoBleFrame request_frame, FrameCallback callback);
  28. void OnResponseFragment(std::vector<uint8_t> data);
  29. // Cancel requests that a cancelation command be sent if possible.
  30. void Cancel();
  31. private:
  32. void WriteRequestFragment(const FidoBleFrameFragment& fragment);
  33. void OnRequestFragmentWritten(bool success);
  34. void ProcessResponseFrame();
  35. void StartTimeout();
  36. void StopTimeout();
  37. void OnError(absl::optional<FidoBleFrame> response_frame);
  38. FidoBleConnection* connection_;
  39. uint16_t control_point_length_;
  40. absl::optional<FidoBleFrame> request_frame_;
  41. FrameCallback callback_;
  42. base::queue<FidoBleFrameContinuationFragment> request_cont_fragments_;
  43. absl::optional<FidoBleFrameAssembler> response_frame_assembler_;
  44. std::vector<uint8_t> buffer_;
  45. base::OneShotTimer timer_;
  46. bool has_pending_request_fragment_write_ = false;
  47. // cancel_pending_ is true if a cancelation should be sent after the current
  48. // set of frames has finished transmitting.
  49. bool cancel_pending_ = false;
  50. // cancel_sent_ records whether a cancel message has already been sent.
  51. bool cancel_sent_ = false;
  52. base::WeakPtrFactory<FidoBleTransaction> weak_factory_{this};
  53. };
  54. } // namespace device
  55. #endif // DEVICE_FIDO_CABLE_FIDO_BLE_TRANSACTION_H_