diff --git a/src/Device/Port/BufferedPort.cpp b/src/Device/Port/BufferedPort.cpp index 1bf9741df84..0eb5a707a36 100644 --- a/src/Device/Port/BufferedPort.cpp +++ b/src/Device/Port/BufferedPort.cpp @@ -60,7 +60,7 @@ BufferedPort::StartRxThread() return true; } -int +std::size_t BufferedPort::Read(void *dest, std::size_t length) { assert(!running); @@ -68,9 +68,6 @@ BufferedPort::Read(void *dest, std::size_t length) std::lock_guard lock(mutex); auto r = buffer.Read(); - if (r.size == 0) - return -1; - std::size_t nbytes = std::min(length, r.size); std::copy_n(r.data, nbytes, (std::byte *)dest); buffer.Consume(nbytes); diff --git a/src/Device/Port/BufferedPort.hpp b/src/Device/Port/BufferedPort.hpp index d36248eacf1..7a95ccb5f36 100644 --- a/src/Device/Port/BufferedPort.hpp +++ b/src/Device/Port/BufferedPort.hpp @@ -60,7 +60,7 @@ class BufferedPort : public Port, protected DataHandler { public: /* virtual methods from class Port */ void Flush() override; - int Read(void *Buffer, std::size_t Size) override; + std::size_t Read(void *Buffer, std::size_t Size) override; void WaitRead(std::chrono::steady_clock::duration timeout) override; bool StopRxThread() override; bool StartRxThread() override; diff --git a/src/Device/Port/DumpPort.cpp b/src/Device/Port/DumpPort.cpp index ebad2d35395..f8871e0c755 100644 --- a/src/Device/Port/DumpPort.cpp +++ b/src/Device/Port/DumpPort.cpp @@ -143,17 +143,17 @@ DumpPort::StartRxThread() return port->StartRxThread(); } -int +std::size_t DumpPort::Read(void *buffer, std::size_t size) { const bool enabled = CheckEnabled(); if (enabled) LogFormat("Read(%u)", (unsigned)size); - int nbytes = port->Read(buffer, size); + auto nbytes = port->Read(buffer, size); if (enabled) { - LogFormat("Read(%u)=%d", (unsigned)size, nbytes); + LogFormat("Read(%u)=%u", (unsigned)size, (unsigned)nbytes); if (nbytes > 0) HexDump("R ", buffer, nbytes); } diff --git a/src/Device/Port/DumpPort.hpp b/src/Device/Port/DumpPort.hpp index 9aa4cbf7358..5eb6026ab85 100644 --- a/src/Device/Port/DumpPort.hpp +++ b/src/Device/Port/DumpPort.hpp @@ -90,7 +90,7 @@ class DumpPort final : public Port { void SetBaudrate(unsigned baud_rate) override; bool StopRxThread() override; bool StartRxThread() override; - int Read(void *buffer, std::size_t size) override; + std::size_t Read(void *buffer, std::size_t size) override; void WaitRead(std::chrono::steady_clock::duration timeout) override; }; diff --git a/src/Device/Port/K6BtPort.cpp b/src/Device/Port/K6BtPort.cpp index aed5826f71f..fe1bc12a972 100644 --- a/src/Device/Port/K6BtPort.cpp +++ b/src/Device/Port/K6BtPort.cpp @@ -180,7 +180,7 @@ K6BtPort::StartRxThread(void) return port->StartRxThread(); } -int +std::size_t K6BtPort::Read(void *Buffer, std::size_t Size) { return port->Read(Buffer, Size); diff --git a/src/Device/Port/K6BtPort.hpp b/src/Device/Port/K6BtPort.hpp index 6d0572a0524..5735ab6c840 100644 --- a/src/Device/Port/K6BtPort.hpp +++ b/src/Device/Port/K6BtPort.hpp @@ -63,7 +63,7 @@ class K6BtPort : public Port { unsigned GetBaudrate() const noexcept override; bool StopRxThread() override; bool StartRxThread() override; - int Read(void *Buffer, std::size_t Size) override; + std::size_t Read(void *Buffer, std::size_t Size) override; void WaitRead(std::chrono::steady_clock::duration timeout) override; }; diff --git a/src/Device/Port/NullPort.cpp b/src/Device/Port/NullPort.cpp index 69117e0f8a7..fb9a04dd821 100644 --- a/src/Device/Port/NullPort.cpp +++ b/src/Device/Port/NullPort.cpp @@ -83,10 +83,10 @@ NullPort::SetBaudrate(unsigned) { } -int +std::size_t NullPort::Read(void *Buffer, std::size_t Size) { - return -1; + return 0; } void diff --git a/src/Device/Port/NullPort.hpp b/src/Device/Port/NullPort.hpp index ac269b49326..932f6f4496f 100644 --- a/src/Device/Port/NullPort.hpp +++ b/src/Device/Port/NullPort.hpp @@ -44,7 +44,7 @@ class NullPort : public Port, private DataHandler { void SetBaudrate(unsigned baud_rate) override; bool StopRxThread() override; bool StartRxThread() override; - int Read(void *Buffer, std::size_t Size) override; + std::size_t Read(void *Buffer, std::size_t Size) override; [[noreturn]] void WaitRead(std::chrono::steady_clock::duration timeout) override; diff --git a/src/Device/Port/Port.hpp b/src/Device/Port/Port.hpp index 673beab74c2..763caf5a669 100644 --- a/src/Device/Port/Port.hpp +++ b/src/Device/Port/Port.hpp @@ -167,10 +167,11 @@ class Port { * Read data from the serial port * @param Buffer Pointer to the buffer * @param Size Size of the buffer - * @return Number of bytes read from the serial port or -1 on failure + * @return Number of bytes read from the port (0 if no data is + * available currently) */ gcc_nonnull_all - virtual int Read(void *Buffer, std::size_t Size) = 0; + virtual std::size_t Read(void *Buffer, std::size_t Size) = 0; /** * Wait until data becomes available or the timeout expires. diff --git a/test/src/FaultInjectionPort.hpp b/test/src/FaultInjectionPort.hpp index 7579cb9d264..583d2570a6c 100644 --- a/test/src/FaultInjectionPort.hpp +++ b/test/src/FaultInjectionPort.hpp @@ -79,9 +79,9 @@ class FaultInjectionPort : public Port { return true; } - int Read(void *Buffer, size_t Size) override { + std::size_t Read(void *Buffer, size_t Size) override { if (inject_port_fault == 0) - return -1; + return 0; if (--inject_port_fault == 0) StateChanged(); diff --git a/test/src/ReadPort.cpp b/test/src/ReadPort.cpp index fd1770f4337..ac71ff873a6 100644 --- a/test/src/ReadPort.cpp +++ b/test/src/ReadPort.cpp @@ -64,10 +64,7 @@ try { continue; } - int nbytes = port->Read(buffer, sizeof(buffer)); - if (nbytes < 0) - break; - + std::size_t nbytes = port->Read(buffer, sizeof(buffer)); fwrite((const void *)buffer, 1, nbytes, stdout); }