blob: 41708e92f518845ad817d06ebe84e159490b3f85 [file] [log] [blame]
Brian Silverman04fac622014-01-26 18:32:15 -08001#ifndef BBB_BYTE_READER_H_
2#define BBB_BYTE_READER_H_
3
4#include <sys/types.h>
5
6#include "aos/common/time.h"
7
8namespace bbb {
9
10class ByteReaderInterface {
11 public:
12 virtual ~ByteReaderInterface() {}
13
14 // Implemented by subclasses to provide a data source
15 // for these algorithms.
16 // Returns the number of bytes read, -1 if there is an error in errno, or -2
17 // if reading takes longer than timeout.
18 virtual ssize_t ReadBytes(
19 uint8_t *dest, size_t max_bytes,
20 const ::aos::time::Time &timeout = ::aos::time::Time(0, 0)) = 0;
21};
22
23class ByteWriterInterface {
24 public:
25 virtual ~ByteWriterInterface() {}
26
27 // Implemented by subclasses to actually write the data somewhere.
28 // Returns true if it succeeds or false if it fails and there is an error in
29 // errno.
30 virtual bool WriteBytes(uint8_t *bytes, size_t number_bytes) = 0;
31};
32
33class ByteReaderWriterInterface : public ByteReaderInterface,
34 public ByteWriterInterface {};
35
36class ByteReaderAndWriter : public ByteReaderWriterInterface {
37 public:
38 ByteReaderAndWriter(ByteReaderInterface *reader, ByteWriterInterface *writer)
39 : reader_(reader), writer_(writer) {}
40
41 ByteReaderInterface *reader() { return reader_; }
42 ByteWriterInterface *writer() { return writer_; }
43
44 private:
45 ByteReaderInterface *const reader_;
46 ByteWriterInterface *const writer_;
47};
48
49} // namespace bbb
50
51#endif // BBB_BYTE_READER_H_