roo_io
API Documentation for roo_io
Loading...
Searching...
No Matches
uart_input_stream.cpp
Go to the documentation of this file.
1#if (defined ESP_PLATFORM || defined ROO_TESTING)
2
4
5#include "driver/uart.h"
6
7namespace roo_io {
8
9size_t Esp32UartInputStream::tryRead(roo::byte* buf, size_t count) {
10 if (!isOpen() || count == 0) return 0;
11 int read = uart_read_bytes(port_, buf, count, 0);
12 return read > 0 ? read : 0;
13}
14
15size_t Esp32UartInputStream::read(roo::byte* buf, size_t count) {
16 if (!isOpen() || count == 0) return 0;
17 while (true) {
18 int read = uart_read_bytes(port_, buf, count, 0);
19 if (read > 0) return read;
20 // Block to read at least one byte.
22 if (read > 0) {
23 if (count > static_cast<size_t>(read)) {
24 // Opportunistically try to read some more bytes if they're available.
25 int more = uart_read_bytes(port_, buf + read,
26 count - static_cast<size_t>(read), 0);
27 if (more < 0) status_ = roo_io::kReadError;
28 read += more;
29 }
30 return read;
31 }
32 if (read < 0) {
33 status_ = roo_io::kReadError;
34 return 0;
35 }
36 }
37}
38
39size_t Esp32UartInputStream::readFully(roo::byte* buf, size_t count) {
40 if (!isOpen() || count == 0) return 0;
41 size_t total = 0;
42 while (total < count) {
44 if (read < 0) {
45 status_ = roo_io::kReadError;
46 break;
47 }
48 total += static_cast<size_t>(read);
49 buf += read;
50 count -= static_cast<size_t>(read);
51 }
52 return total;
53}
54
55} // namespace roo_io
56
57#endif // (defined ESP_PLATFORM || defined ROO_TESTING)
Definition byte.h:6
roo::basic_string_view< CharT, Traits > basic_string_view
Definition string_view.h:8
size_t count
Definition compare.h:45
@ kReadError
Definition status.h:13