roo_transport
API Documentation for roo_transport
Loading...
Searching...
No Matches
link_stream.cpp
Go to the documentation of this file.
1#ifdef ARDUINO
2
4
5#include <cstddef>
6
7namespace roo_transport {
8
9LinkStream::LinkStream(Channel& channel, uint32_t my_stream_id)
10 : link_(channel, my_stream_id) {}
11
12LinkStream::LinkStream(Link socket) : link_(std::move(socket)) {}
13
14int LinkStream::available() { return in().available(); }
15
16int LinkStream::read() {
17 if (available() == 0) {
18 yield();
19 return -1;
20 }
21 return in().read();
22}
23
24int LinkStream::peek() { return in().peek(); }
25
26size_t LinkStream::readBytes(char* buffer, size_t length) {
27 return timedRead((roo::byte*)buffer, length, roo_time::Millis(getTimeout()));
28}
29
30size_t LinkStream::readBytes(uint8_t* buffer, size_t length) {
31 return timedRead((roo::byte*)buffer, length, roo_time::Millis(getTimeout()));
32}
33
34size_t LinkStream::write(uint8_t val) {
35 out().write((const roo::byte*)&val, 1);
36 return 1;
37}
38
39size_t LinkStream::write(const uint8_t* buffer, size_t size) {
40 return out().writeFully((const roo::byte*)buffer, size);
41}
42
43int LinkStream::availableForWrite() { return out().availableForWrite(); }
44
45void LinkStream::flush() { out().flush(); }
46
47LinkStatus LinkStream::status() const { return link_.status(); }
48
49void LinkStream::awaitConnected() { link_.awaitConnected(); }
50
51bool LinkStream::awaitConnected(roo_time::Duration timeout) {
52 return link_.awaitConnected(timeout);
53}
54
55size_t LinkStream::timedRead(roo::byte* buf, size_t count,
56 roo_time::Duration timeout) {
57 roo_time::Uptime start = roo_time::Uptime::Now();
58 size_t total = 0;
59 if (in().status() != roo_io::kOk) return -1;
60 while (count > 0) {
61 for (int i = 0; i < 100; ++i) {
62 size_t result = in().tryRead(buf, count);
63 if (result == 0) {
64 if (in().status() != roo_io::kOk) return -1;
65 // link_.channel_->loop();
66 } else {
67 total += result;
68 count -= result;
69 }
70 if (count == 0) return total;
71 yield();
72 }
73 if (roo_time::Uptime::Now() - start > timeout) break;
74 delay(1);
75 }
76 return total;
77}
78
79} // namespace roo_transport
80
81#endif