grblHAL core  20240704
stream.h
Go to the documentation of this file.
1 /*
2  stream.h - high level (serial) stream handling
3 
4  Part of grblHAL
5 
6  Copyright (c) 2019-2023 Terje Io
7 
8  Grbl is free software: you can redistribute it and/or modify
9  it under the terms of the GNU General Public License as published by
10  the Free Software Foundation, either version 3 of the License, or
11  (at your option) any later version.
12 
13  Grbl is distributed in the hope that it will be useful,
14  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  GNU General Public License for more details.
17 
18  You should have received a copy of the GNU General Public License
19  along with Grbl. If not, see <http://www.gnu.org/licenses/>.
20 */
21 
28 #ifndef _STREAM_H_
29 #define _STREAM_H_
30 
31 #define ASCII_SOH 0x01
32 #define ASCII_STX 0x02
33 #define ASCII_ETX 0x03
34 #define ASCII_EOT 0x04
35 #define ASCII_ENQ 0x05
36 #define ASCII_ACK 0x06
37 #define ASCII_BS 0x08
38 #define ASCII_TAB 0x09
39 #define ASCII_LF 0x0A
40 #define ASCII_CR 0x0D
41 #define ASCII_XON 0x11
42 #define ASCII_XOFF 0x13
43 #define ASCII_NAK 0x15
44 #define ASCII_EOF 0x1A
45 #define ASCII_CAN 0x18
46 #define ASCII_EM 0x19
47 #define ASCII_ESC 0x1B
48 #define ASCII_DEL 0x7F
49 #define ASCII_EOL "\r\n"
50 
51 #ifndef RX_BUFFER_SIZE
52 #define RX_BUFFER_SIZE 1024 // must be a power of 2
53 #endif
54 
55 #ifndef TX_BUFFER_SIZE
56 #define TX_BUFFER_SIZE 512 // must be a power of 2
57 #endif
58 
59 #ifndef BLOCK_TX_BUFFER_SIZE
60 #define BLOCK_TX_BUFFER_SIZE 256
61 #endif
62 
63 // Serial baud rate
64 #ifndef BAUD_RATE
65 #define BAUD_RATE 115200
66 #endif
67 
68 // Value to be returned from input stream when no data is available
69 #ifndef SERIAL_NO_DATA
70 #define SERIAL_NO_DATA -1
71 #endif
72 
73 #define BUFNEXT(ptr, buffer) ((ptr + 1) & (sizeof(buffer.data) - 1))
74 #define BUFCOUNT(head, tail, size) ((head >= tail) ? (head - tail) : (size - tail + head))
75 
76 #include <stddef.h>
77 #include <stdint.h>
78 #include <stdbool.h>
79 
80 #include "vfs.h"
81 
82 typedef enum {
88  StreamType_SDCard, // deprecated, use StreamType_File instead
93 
97 typedef bool (*stream_is_connected_ptr)(void);
98 
102 typedef uint16_t (*get_stream_buffer_count_ptr)(void);
103 
107 typedef int16_t (*stream_read_ptr)(void);
108 
114 typedef void (*stream_write_ptr)(const char *s);
115 
120 typedef void (*stream_write_n_ptr)(const char *s, uint16_t len);
121 
122 
126 typedef bool (*stream_write_char_ptr)(const char c);
127 
133 typedef bool (*enqueue_realtime_command_ptr)(char c);
134 
135 
144 typedef bool (*enqueue_realtime_command2_ptr)(char c);
145 
146 
155 
156 
161 typedef bool (*set_baud_rate_ptr)(uint32_t baud_rate);
162 
164 typedef void (*flush_stream_buffer_ptr)(void);
165 
171 typedef void (*cancel_read_buffer_ptr)(void);
172 
191 typedef bool (*suspend_read_ptr)(bool await);
192 
202 typedef bool (*disable_rx_stream_ptr)(bool disable);
203 
204 typedef union {
205  uint8_t value;
206  struct {
207  uint8_t connected :1,
214  unused :1;
215  };
217 
218 typedef union {
219  uint8_t value;
220  struct {
221  uint8_t connected :1,
223  is_usb :1,
224  unused :5;
225  };
227 
229 typedef struct {
231  uint8_t instance;
251 } io_stream_t;
252 
253 typedef const io_stream_t *(*stream_claim_ptr)(uint32_t baud_rate);
254 
255 typedef struct {
257  uint8_t instance;
261 
262 typedef bool (*stream_enumerate_callback_ptr)(io_stream_properties_t const *properties);
263 
264 typedef struct io_stream_details {
265  uint8_t n_streams;
269 
270 // The following structures and functions are not referenced in the core code, may be used by drivers
271 
272 typedef struct {
273  volatile uint_fast16_t head;
274  volatile uint_fast16_t tail;
275  volatile bool rts_state;
276  bool overflow;
277  bool backup;
278  char data[RX_BUFFER_SIZE];
280 
281 typedef struct {
282  volatile uint_fast16_t head;
283  volatile uint_fast16_t tail;
284  char data[TX_BUFFER_SIZE];
286 
287 typedef struct {
288  uint_fast16_t length;
289  uint_fast16_t max_length;
290  char *s;
293 
294 // double buffered tx stream
295 typedef struct {
296  uint_fast16_t length;
297  uint_fast16_t max_length;
298  char *s;
301  char data2[BLOCK_TX_BUFFER_SIZE];
303 
304 #ifdef __cplusplus
305 extern "C" {
306 #endif
307 
311 int16_t stream_get_null (void);
312 
318 bool stream_rx_suspend (stream_rx_buffer_t *rxbuffer, bool suspend);
319 
320 bool stream_mpg_register (const io_stream_t *stream, bool rx_only, stream_write_char_ptr write_char);
321 
326 bool stream_mpg_enable (bool on);
327 
328 void stream_mpg_set_mode (void *data);
329 
330 bool stream_mpg_check_enable (char c);
331 
332 bool stream_buffer_all (char c);
333 
334 bool stream_tx_blocking (void);
335 
336 bool stream_enqueue_realtime_command (char c);
337 
339 
341 
342 bool stream_connect (const io_stream_t *stream);
343 
344 bool stream_connect_instance (uint8_t instance, uint32_t baud_rate);
345 
346 void stream_disconnect (const io_stream_t *stream);
347 
348 bool stream_connected (void);
349 
350 const io_stream_t *stream_get_base (void);
351 
353 
354 const io_stream_t *stream_null_init (uint32_t baud_rate);
355 
356 io_stream_t const *stream_open_instance (uint8_t instance, uint32_t baud_rate, stream_write_char_ptr rx_handler, const char *description);
357 
358 bool stream_set_description (const io_stream_t *stream, const char *description);
359 
360 #ifdef DEBUGOUT
361 void debug_write (const char *s);
362 void debug_writeln (const char *s);
363 bool debug_stream_init (void);
364 #endif
365 
366 #ifdef __cplusplus
367 }
368 #endif
369 
370 #endif
uint8_t instance
Definition: stream.c:407
uint32_t baud_rate
Definition: stream.c:408
bool(* enqueue_realtime_command_ptr)(char c)
Pointer to function for extracting real-time commands from the input stream and enqueue them for proc...
Definition: stream.h:133
void(* stream_write_n_ptr)(const char *s, uint16_t len)
Pointer to function for writing a n character long string to the output stream.
Definition: stream.h:120
bool stream_connected(void)
Definition: stream.c:174
void stream_disconnect(const io_stream_t *stream)
Definition: stream.c:429
uint16_t(* get_stream_buffer_count_ptr)(void)
Pointer to function for getting number of characters available or free in a stream buffer.
Definition: stream.h:102
bool(* stream_is_connected_ptr)(void)
Pointer to function for getting stream connected status.
Definition: stream.h:97
void(* stream_write_ptr)(const char *s)
Pointer to function for writing a null terminated string to the output stream.
Definition: stream.h:114
bool stream_enumerate_streams(stream_enumerate_callback_ptr callback)
Definition: stream.c:81
bool(* stream_enumerate_callback_ptr)(io_stream_properties_t const *properties)
Definition: stream.h:262
bool stream_mpg_check_enable(char c)
Definition: stream.c:457
void stream_mpg_set_mode(void *data)
Definition: stream.c:452
bool stream_enqueue_realtime_command(char c)
Definition: stream.c:162
bool stream_tx_blocking(void)
Definition: stream.c:102
bool(* stream_write_char_ptr)(const char c)
Pointer to function for writing a single character to the output stream.
Definition: stream.h:126
io_stream_t const * stream_open_instance(uint8_t instance, uint32_t baud_rate, stream_write_char_ptr rx_handler, const char *description)
Definition: stream.c:435
void(* cancel_read_buffer_ptr)(void)
Pointer to function for flushing the input buffer and inserting an ASCII_CAN character.
Definition: stream.h:171
void stream_register_streams(io_stream_details_t *details)
Definition: stream.c:73
io_stream_flags_t stream_get_flags(io_stream_t stream)
Definition: stream.c:365
bool stream_mpg_enable(bool on)
Function for enabling/disabling input from a secondary input stream.
Definition: stream.c:512
#define RX_BUFFER_SIZE
Definition: stream.h:52
int16_t stream_get_null(void)
Dummy function for reading data from a virtual empty input buffer.
Definition: stream.c:112
bool stream_connect_instance(uint8_t instance, uint32_t baud_rate)
Definition: stream.c:420
const io_stream_t * stream_null_init(uint32_t baud_rate)
Definition: stream.c:619
bool stream_connect(const io_stream_t *stream)
Definition: stream.c:396
bool(* enqueue_realtime_command2_ptr)(char c)
Optional, but recommended, pointer to function for enqueueing realtime command characters.
Definition: stream.h:144
bool stream_mpg_register(const io_stream_t *stream, bool rx_only, stream_write_char_ptr write_char)
Definition: stream.c:470
void(* flush_stream_buffer_ptr)(void)
Pointer to function for flushing a stream buffer.
Definition: stream.h:164
#define TX_BUFFER_SIZE
Definition: stream.h:56
bool stream_set_description(const io_stream_t *stream, const char *description)
Definition: stream.c:384
bool(* set_baud_rate_ptr)(uint32_t baud_rate)
Pointer to function for setting the stream baud rate.
Definition: stream.h:161
struct io_stream_details io_stream_details_t
const io_stream_t *(* stream_claim_ptr)(uint32_t baud_rate)
Definition: stream.h:253
bool stream_rx_suspend(stream_rx_buffer_t *rxbuffer, bool suspend)
Function for blocking reads from or restoring an input buffer.
Definition: stream.c:134
int16_t(* stream_read_ptr)(void)
Pointer to function for reading a single character from a input stream.
Definition: stream.h:107
#define BLOCK_TX_BUFFER_SIZE
Definition: stream.h:60
bool stream_buffer_all(char c)
Definition: stream.c:157
bool(* disable_rx_stream_ptr)(bool disable)
Pointer to function for disabling/enabling stream input.
Definition: stream.h:202
enqueue_realtime_command_ptr(* set_enqueue_rt_handler_ptr)(enqueue_realtime_command_ptr handler)
Pointer to function for setting the enqueue realtime commands handler.
Definition: stream.h:154
stream_type_t
Definition: stream.h:82
@ StreamType_Bluetooth
Definition: stream.h:85
@ StreamType_Telnet
Definition: stream.h:86
@ StreamType_SDCard
Definition: stream.h:88
@ StreamType_MPG
Definition: stream.h:84
@ StreamType_WebSocket
Definition: stream.h:87
@ StreamType_File
Definition: stream.h:89
@ StreamType_Null
Definition: stream.h:91
@ StreamType_Serial
Definition: stream.h:83
@ StreamType_Redirected
Definition: stream.h:90
bool(* suspend_read_ptr)(bool await)
Pointer to function for blocking reads from and restoring a input buffer.
Definition: stream.h:191
const io_stream_t * stream_get_base(void)
Definition: stream.c:360
Definition: stream.h:264
struct io_stream_details * next
Definition: stream.h:267
io_stream_properties_t * streams
Definition: stream.h:266
uint8_t n_streams
Definition: stream.h:265
Definition: stream.h:255
uint8_t instance
Instance of stream type, starts from 0.
Definition: stream.h:257
stream_type_t type
Type of stream.
Definition: stream.h:256
stream_claim_ptr claim
Definition: stream.h:259
io_stream_flags_t flags
Definition: stream.h:258
Properties and handlers for stream I/O.
Definition: stream.h:229
uint8_t instance
Instance of stream type, starts from 0.
Definition: stream.h:231
flush_stream_buffer_ptr reset_read_buffer
Handler for flushing the input buffer.
Definition: stream.h:240
stream_write_char_ptr write_char
Handler for writing a single character to current stream only.
Definition: stream.h:237
set_enqueue_rt_handler_ptr set_enqueue_rt_handler
Handler for setting the enqueue realtime command character handler.
Definition: stream.h:242
stream_write_ptr write
Handler for writing string to current output stream only.
Definition: stream.h:235
enqueue_realtime_command2_ptr enqueue_rt_command
(Optional) handler for enqueueing a realtime command character.
Definition: stream.h:238
io_stream_state_t state
Optional status flags such as connected status.
Definition: stream.h:232
get_stream_buffer_count_ptr get_rx_buffer_count
Optional handler for getting number of characters in the input buffer.
Definition: stream.h:246
stream_type_t type
Type of stream.
Definition: stream.h:230
stream_write_n_ptr write_n
Optional handler for writing n characters to current output stream only. Required for Modbus support.
Definition: stream.h:244
get_stream_buffer_count_ptr get_tx_buffer_count
Optional handler for getting number of characters in the output buffer(s). Count shall include any un...
Definition: stream.h:247
disable_rx_stream_ptr disable_rx
Optional handler for disabling/enabling a stream. Recommended?
Definition: stream.h:245
set_baud_rate_ptr set_baud_rate
Optional handler for setting the stream baud rate. Required for Modbus support, recommended for Bluet...
Definition: stream.h:249
stream_is_connected_ptr is_connected
Handler for getting stream connected status.
Definition: stream.h:233
cancel_read_buffer_ptr cancel_read_buffer
Handler for flushing the input buffer and inserting an ASCII_CAN character.
Definition: stream.h:241
stream_write_ptr write_all
Handler for writing string to all active output streams.
Definition: stream.h:236
vfs_file_t * file
File handle, non-null if streaming from a file.
Definition: stream.h:250
stream_read_ptr read
Handler for reading a single character from the input stream.
Definition: stream.h:239
flush_stream_buffer_ptr reset_write_buffer
Optional handler for flushing the output buffer. Any transmit FIFO shall be flushed as well....
Definition: stream.h:248
get_stream_buffer_count_ptr get_rx_buffer_free
Handler for getting number of free characters in the input buffer.
Definition: stream.h:234
suspend_read_ptr suspend_read
Optional handler for saving away and restoring the current input buffer.
Definition: stream.h:243
Definition: stream.h:295
bool use_tx2data
Definition: stream.h:299
uint_fast16_t length
Definition: stream.h:296
uint_fast16_t max_length
Definition: stream.h:297
char * s
Definition: stream.h:298
Definition: stream.h:287
uint_fast16_t length
Definition: stream.h:288
uint_fast16_t max_length
Definition: stream.h:289
char * s
Definition: stream.h:290
Definition: stream.h:272
bool overflow
Definition: stream.h:276
volatile uint_fast16_t head
Definition: stream.h:273
volatile bool rts_state
Definition: stream.h:275
bool backup
Definition: stream.h:277
volatile uint_fast16_t tail
Definition: stream.h:274
Definition: stream.h:281
volatile uint_fast16_t head
Definition: stream.h:282
volatile uint_fast16_t tail
Definition: stream.h:283
Definition: vfs.h:80
Definition: stream.h:204
uint8_t can_set_baud
Definition: stream.h:210
uint8_t claimed
Definition: stream.h:209
uint8_t value
Definition: stream.h:205
uint8_t rx_only
Definition: stream.h:211
uint8_t connected
deprecated
Definition: stream.h:207
uint8_t modbus_ready
Definition: stream.h:212
uint8_t rts_handshake
Definition: stream.h:213
uint8_t claimable
Definition: stream.h:208
uint8_t unused
Definition: stream.h:214
Definition: stream.h:218
uint8_t is_usb
Definition: stream.h:223
uint8_t value
Definition: stream.h:219
uint8_t webui_connected
Definition: stream.h:222
uint8_t connected
Definition: stream.h:221
uint8_t unused
Definition: stream.h:224