roo_transport
API Documentation for roo_transport
Loading...
Searching...
No Matches
link_stream.h
Go to the documentation of this file.
1#pragma once
2
3#if (defined ARDUINO)
4
5#include "Arduino.h"
7
8namespace roo_transport {
9
10// Link wrapped in the arduino Stream interface. Represents a reliable
11// bidirectional stream, e.g. over Serial.
12class LinkStream : public Stream {
13 public:
14 // Creates a dummy stream link in state kIdle.
15 // Use SerialLinkTransport::connect() to create a proper connected link.
16 LinkStream() = default;
17
18 // Wrap a link into StreamLink.
19 LinkStream(Link link);
20
21 int available() override;
22 int read() override;
23 int peek() override;
24
25#ifdef ESP_PLATFORM
26 // These two methods are virtual on ESP32.
27 size_t readBytes(char* buffer, size_t length) override;
28 size_t readBytes(uint8_t* buffer, size_t length) override;
29#else
30 size_t readBytes(char* buffer, size_t length);
31 size_t readBytes(uint8_t* buffer, size_t length);
32#endif
33
34 size_t write(uint8_t) override;
35 size_t write(const uint8_t* buffer, size_t size) override;
36 int availableForWrite() override;
37 void flush() override;
38
39 // Obtains the input stream that can be used to read from the reliable
40 // stream.
41 LinkInputStream& in() { return link_.in(); }
42
43 // Obtains the output stream that can be used to write to the reliable
44 // stream.
45 LinkOutputStream& out() { return link_.out(); }
46
47 // Returns the current status of the link.
48 LinkStatus status() const;
49
50 // If the link is in state kConnecting, blocks until it becomes either
51 // kConnected or kBroken. Otherwise, returns immediately.
52 void awaitConnected();
53
54 // If the link is kIdle, kConnected, or kBroken, does nothing and returns
55 // true immediately. Otherwise (when the link is in state kConnecting), blocks
56 // until it becomes either kConnected or kBroken, or until the specified
57 // timeout elapses, and returns true if the link has changed state, and false
58 // if the timeout has elapsed.
59 bool awaitConnected(roo_time::Duration timeout);
60
61 // Retrieves the underlying link.
62 Link& link() { return link_; }
63
64 // Retrieves the underlying link.
65 const Link& link() const { return link_; }
66
67 protected:
68 void set(Link&& link) { link_ = std::move(link); }
69
70 private:
71 friend class StreamLinkTransport;
72
73 LinkStream(Channel& channel, uint32_t my_stream_id);
74
75 size_t timedRead(roo::byte* buf, size_t count, roo_time::Duration timeout);
76
77 Link link_;
78};
79
80} // namespace roo_transport
81
82#endif // defined(ARDUINO)