From d259c03079b7671bed4802ea41a83190e3110c13 Mon Sep 17 00:00:00 2001 From: Peter Barker Date: Tue, 1 Oct 2019 00:05:32 +1000 Subject: [PATCH] AP_HAL: BetterStream add a read(uint8_t*buffer, uint16_t count method --- libraries/AP_HAL/utility/BetterStream.cpp | 12 ++++++++++++ libraries/AP_HAL/utility/BetterStream.h | 9 +++++++-- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/libraries/AP_HAL/utility/BetterStream.cpp b/libraries/AP_HAL/utility/BetterStream.cpp index d140ec7456..de9387eb5c 100644 --- a/libraries/AP_HAL/utility/BetterStream.cpp +++ b/libraries/AP_HAL/utility/BetterStream.cpp @@ -19,3 +19,15 @@ size_t AP_HAL::BetterStream::write(const char *str) { return write((const uint8_t *)str, strlen(str)); } + +ssize_t AP_HAL::BetterStream::read(uint8_t *buffer, uint16_t count) { + uint16_t offset = 0; + while (count--) { + const int16_t x = read(); + if (x == -1) { + return offset; + } + buffer[offset++] = (uint8_t)x; + } + return offset; +} diff --git a/libraries/AP_HAL/utility/BetterStream.h b/libraries/AP_HAL/utility/BetterStream.h index 9924f337f8..0881fdefaa 100644 --- a/libraries/AP_HAL/utility/BetterStream.h +++ b/libraries/AP_HAL/utility/BetterStream.h @@ -19,11 +19,12 @@ // #pragma once -#include - #include #include +#include +#include + class AP_HAL::BetterStream { public: @@ -48,6 +49,10 @@ public: // returns false if discard failed (e.g. port locked) virtual bool discard_input() = 0; // discard all bytes available for reading + // returns -1 on error (e.g. port locked), number of bytes read + // otherwise + virtual ssize_t read(uint8_t *buffer, uint16_t count); + /* NB txspace was traditionally a member of BetterStream in the * FastSerial library. As far as concerns go, it belongs with available() */ virtual uint32_t txspace() = 0;