arduino-emulator
Loading...
Searching...
No Matches
RemoteSerial.h
1/*
2 RemoteSerial.h
3 Copyright (c) 2025 Phil Schatzmann. All right reserved.
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Lesser General Public
7 License as published by the Free Software Foundation; either
8 version 2.1 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Lesser General Public License for more details.
14
15 You should have received a copy of the GNU Lesser General Public
16 License along with this library; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18*/
19#pragma once
20
21#include "HardwareService.h"
22#include "RingBufferExt.h"
23#include "api/Stream.h"
24
25namespace arduino {
26
52class RemoteSerialClass : public Stream {
53 public:
55 this->no = no;
56 this->service.setStream(&stream);
57 }
58
59 virtual void begin(unsigned long baudrate) {
60 service.send(SerialBegin);
61 service.send(no);
62 service.send((uint64_t)baudrate);
63 service.flush();
64 }
65
66 virtual void begin(unsigned long baudrate, uint16_t config) {
67 service.send(SerialBegin);
68 service.send(no);
69 service.send((uint64_t)baudrate);
70 service.flush();
71 }
72
73 virtual void end() {
74 service.send(SerialEnd);
75 service.send(no);
76 service.flush();
77 }
78
79 virtual int available() {
80 if (read_buffer.available() > 0) {
81 return read_buffer.available();
82 }
83 // otherwise we get it from the remote system
84 service.send(SerialAvailable);
85 service.send(no);
86 service.flush();
87 return service.receive16();
88 }
89
90 virtual int read() {
91 if (read_buffer.available() == 0) {
92 uint8_t buffer[max_buffer_len];
93 int len = readBytes(buffer, max_buffer_len);
94 read_buffer.write(buffer, len);
95 }
96 if (read_buffer.available() == 0) {
97 return -1;
98 }
99 return read_buffer.read();
100 }
101
102 virtual size_t readBytes(uint8_t* buffer, size_t length) {
103 if (read_buffer.available() > 0) {
104 return read_buffer.read(buffer, length);
105 }
106 service.send(SerialRead);
107 service.send(no);
108 service.send((uint64_t)length);
109 service.flush();
110 int len = service.receive(buffer, length);
111 return len;
112 }
113
114 virtual int peek() {
115 if (read_buffer.available() > 0) {
116 return read_buffer.peek();
117 }
118 service.send(SerialPeek);
119 service.flush();
120 return service.receive16();
121 }
122
123 virtual size_t write(uint8_t c) {
124 if (write_buffer.availableToWrite() == 0) {
125 flush();
126 }
127 return write_buffer.write(c);
128 }
129
130 virtual size_t write(uint8_t* str, size_t len) {
131 flush();
132 service.send(SerialWrite);
133 service.send(no);
134 service.send((uint64_t)len);
135 service.send(str, len);
136 service.flush();
137 return service.receive16();
138 }
139
140 void flush() {
141#if defined(_MSC_VER)
142 int available;
143 while ((available = write_buffer.available()) > 0) {
144 uint8_t buffer[max_buffer_len];
145 write_buffer.read(buffer, min(available, max_buffer_len));
146 write(buffer, min(available, max_buffer_len));
147 }
148#else
149 int available = write_buffer.available();
150 if (available > 0) {
151 uint8_t buffer[available];
152 write_buffer.read(buffer, available);
153 write(buffer, available);
154 }
155#endif
156 service.send(SerialFlush);
157 service.send(no);
158 service.flush();
159 }
160
161 operator bool() { return service; }
162
163 protected:
164 HardwareService service;
165 uint8_t no;
166#if defined(_MSC_VER)
167 static constexpr int max_buffer_len = 512; // MSVC does not support VLA
168#else
169 int max_buffer_len = 512;
170#endif
171 RingBufferExt write_buffer;
172 RingBufferExt read_buffer;
173};
174
175} // namespace arduino
Definition DMAPool.h:103
Provides a virtualized hardware communication service for SPI, I2C, I2S, and GPIO over a stream.
Definition HardwareService.h:115
Remote Serial implementation that operates over a communication stream.
Definition RemoteSerial.h:52
Implementation of a Simple Circular Buffer. Instead of comparing the position of the read and write p...
Definition RingBufferExt.h:35
Definition Stream.h:51
We provide the WiFi class to simulate the Arduino WIFI. In in Linux we can expect that networking is ...
Definition CanMsg.cpp:31