Skip to content

Commit

Permalink
pw_uart: Move non-blocking methods from Uart to UartBase
Browse files Browse the repository at this point in the history
* ConservativeReadAvailable()
* ClearPendingReceiveBytes()

Bug: 369679732
Change-Id: Idecd3f2089631e0062c3a186e519c10abbad3392
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/238533
Presubmit-Verified: CQ Bot Account <[email protected]>
Lint: Lint 🤖 <[email protected]>
Commit-Queue: Jonathon Reinhart <[email protected]>
Reviewed-by: Taylor Cramer <[email protected]>
Docs-Not-Needed: Jonathon Reinhart <[email protected]>
  • Loading branch information
JonathonReinhart authored and CQ Bot Account committed Sep 27, 2024
1 parent 14204b5 commit d31705b
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 29 deletions.
28 changes: 0 additions & 28 deletions pw_uart/public/pw_uart/uart.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,15 +230,6 @@ class Uart : public UartBase {
return DoTryWriteFor(tx_buffer, timeout);
}

/// Returns the number of bytes currently available for reading.
///
/// This function checks the receive buffer to determine how many bytes of
/// data are ready to be read.
///
/// @returns The number of bytes available for reading. When no data is
/// available or in case of an error this function returns 0.
size_t ConservativeReadAvailable() { return DoConservativeReadAvailable(); }

/// Blocks until all queued data in the UART has been transmitted and the
/// FIFO is empty.
///
Expand All @@ -257,23 +248,6 @@ class Uart : public UartBase {
/// @endrst
Status FlushOutput() { return DoFlushOutput(); }

/// Empties the UART's receive buffer and discards any unread data.
///
/// This function removes all data from the receive buffer, resetting the
/// buffer to an empty state. This is useful for situations where you want to
/// disregard any previously received data and resynchronize.
///
/// @returns @rst
///
/// .. pw-status-codes::
///
/// OK: The operation was successful.
///
/// May return other implementation-specific status codes.
///
/// @endrst
Status ClearPendingReceiveBytes() { return DoClearPendingReceiveBytes(); }

private:
/// Reads data from the UART into a provided buffer with an optional timeout
/// provided.
Expand Down Expand Up @@ -377,9 +351,7 @@ class Uart : public UartBase {
virtual StatusWithSize DoTryWriteFor(
ConstByteSpan tx_buffer,
std::optional<chrono::SystemClock::duration> timeout) = 0;
virtual size_t DoConservativeReadAvailable() = 0;
virtual Status DoFlushOutput() = 0;
virtual Status DoClearPendingReceiveBytes() = 0;
};

} // namespace pw::uart
28 changes: 28 additions & 0 deletions pw_uart/public/pw_uart/uart_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,40 @@ class UartBase {
/// @endrst
Status SetFlowControl(bool enable) { return DoSetFlowControl(enable); }

/// Returns the number of bytes currently available for reading.
///
/// This function checks the receive buffer to determine how many bytes of
/// data are ready to be read.
///
/// @returns The number of bytes available for reading. When no data is
/// available or in case of an error this function returns 0.
size_t ConservativeReadAvailable() { return DoConservativeReadAvailable(); }

/// Empties the UART's receive buffer and discards any unread data.
///
/// This function removes all data from the receive buffer, resetting the
/// buffer to an empty state. This is useful for situations where you want to
/// disregard any previously received data and resynchronize.
///
/// @returns @rst
///
/// .. pw-status-codes::
///
/// OK: The operation was successful.
///
/// May return other implementation-specific status codes.
///
/// @endrst
Status ClearPendingReceiveBytes() { return DoClearPendingReceiveBytes(); }

private:
virtual Status DoEnable(bool enable) = 0;
virtual Status DoSetBaudRate(uint32_t baud_rate) = 0;
virtual Status DoSetFlowControl(bool /*enable*/) {
return pw::Status::Unimplemented();
}
virtual size_t DoConservativeReadAvailable() = 0;
virtual Status DoClearPendingReceiveBytes() = 0;
};

} // namespace pw::uart
4 changes: 3 additions & 1 deletion pw_uart/uart_non_blocking_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ class UartNonBlockingStub : public UartNonBlocking {
return OkStatus();
}
bool DoCancelWrite() override { return true; }
size_t DoConservativeReadAvailable() override { return 0; }
Status DoClearPendingReceiveBytes() override { return OkStatus(); }
};

class UartNonBlockingTest : public ::testing::Test {
Expand All @@ -57,4 +59,4 @@ class UartNonBlockingTest : public ::testing::Test {
TEST_F(UartNonBlockingTest, CompilationSucceeds) { EXPECT_TRUE(true); }

} // namespace
} // namespace pw::uart
} // namespace pw::uart

0 comments on commit d31705b

Please sign in to comment.