Skip to content

Commit

Permalink
Device/Port: Read() returns std::size_t
Browse files Browse the repository at this point in the history
There are no error conditions, only an empty buffer.
  • Loading branch information
MaxKellermann committed Dec 11, 2021
1 parent 01d0cba commit 7775432
Show file tree
Hide file tree
Showing 11 changed files with 17 additions and 22 deletions.
5 changes: 1 addition & 4 deletions src/Device/Port/BufferedPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,14 @@ BufferedPort::StartRxThread()
return true;
}

int
std::size_t
BufferedPort::Read(void *dest, std::size_t length)
{
assert(!running);

std::lock_guard<Mutex> 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);
Expand Down
2 changes: 1 addition & 1 deletion src/Device/Port/BufferedPort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
6 changes: 3 additions & 3 deletions src/Device/Port/DumpPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/Device/Port/DumpPort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
2 changes: 1 addition & 1 deletion src/Device/Port/K6BtPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/Device/Port/K6BtPort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};

Expand Down
4 changes: 2 additions & 2 deletions src/Device/Port/NullPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ NullPort::SetBaudrate(unsigned)
{
}

int
std::size_t
NullPort::Read(void *Buffer, std::size_t Size)
{
return -1;
return 0;
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/Device/Port/NullPort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/Device/Port/Port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions test/src/FaultInjectionPort.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 1 addition & 4 deletions test/src/ReadPort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit 7775432

Please sign in to comment.