File syn_stream.c¶
FileList > src > syntropic > util > syn_stream.c
Go to the source code of this file
Cooperative byte stream implementation.
#include "syn_stream.h"#include "syn_ringbuf.h"#include <string.h>
Public Functions¶
| Type | Name |
|---|---|
| void | syn_stream_clear_delimiter (SYN_Stream * s) Disable delimiter mode, reverting to default or threshold mode. |
| size_t | syn_stream_count (const SYN_Stream * s) Return the number of bytes currently in the stream. |
| void | syn_stream_flush (SYN_Stream * s) Discard all data in the stream. |
| size_t | syn_stream_free (const SYN_Stream * s) Return the number of free bytes available for writing. |
| void | syn_stream_init (SYN_Stream * s, uint8_t * buf, size_t size) Initialize a stream with a caller-owned backing buffer. |
| bool | syn_stream_put (SYN_Stream * s, uint8_t byte) Put a single byte into the stream. |
| size_t | syn_stream_read (SYN_Stream * s, uint8_t * buf, size_t max_len) Read up to max_len bytes from the stream. |
| size_t | syn_stream_read_line (SYN_Stream * s, uint8_t * buf, size_t max_len) Read up to and including the next delimiter byte. |
| bool | syn_stream_readable (const SYN_Stream * s) Check if the stream is readable (mode-aware). |
| void | syn_stream_set_delimiter (SYN_Stream * s, uint8_t delim) Enable delimiter mode. |
| void | syn_stream_set_threshold (SYN_Stream * s, size_t n) Set the byte threshold for readability. |
| size_t | syn_stream_write (SYN_Stream * s, const uint8_t * data, size_t len) Write bytes into the stream. |
Public Static Functions¶
| Type | Name |
|---|---|
| size_t | stream_find_delimiter (const SYN_Stream * s) Scan the ringbuf for the delimiter byte. |
Public Functions Documentation¶
function syn_stream_clear_delimiter¶
Disable delimiter mode, reverting to default or threshold mode.
Parameters:
sStream instance.
function syn_stream_count¶
Return the number of bytes currently in the stream.
Parameters:
sStream instance.
Returns:
Byte count.
function syn_stream_flush¶
Discard all data in the stream.
Parameters:
sStream instance.
function syn_stream_free¶
Return the number of free bytes available for writing.
Parameters:
sStream instance.
Returns:
Free byte count.
function syn_stream_init¶
Initialize a stream with a caller-owned backing buffer.
Parameters:
sStream instance.bufBacking array (caller-owned, must outlive the stream).sizeSize of backing array. Usable capacity is size - 1.
function syn_stream_put¶
Put a single byte into the stream.
ISR-safe convenience wrapper around syn_stream_write().
Parameters:
sStream instance.byteByte to store.
Returns:
true if the byte was stored, false if the buffer is full.
function syn_stream_read¶
Read up to max_len bytes from the stream.
Non-blocking — returns 0 if the buffer is empty. Does not respect threshold or delimiter; reads whatever is available.
Parameters:
sStream instance.bufDestination buffer.max_lenMaximum bytes to read.
Returns:
Number of bytes actually read.
function syn_stream_read_line¶
Read up to and including the next delimiter byte.
Reads bytes from the stream into buf, stopping after the first delimiter byte (which is included in the output). If no delimiter is found in the buffer, returns 0 and reads nothing.
Pair with PT_STREAM_WAIT in delimiter mode to wait for a complete line:
Parameters:
sStream instance.bufDestination buffer.max_lenMaximum bytes to read (including delimiter).
Returns:
Number of bytes read (0 if no delimiter found or empty).
function syn_stream_readable¶
Check if the stream is readable (mode-aware).
Behavior depends on configured mode: * Delimiter mode: true if the delimiter byte is in the buffer. * Threshold mode: true if count >= threshold. * Default: true if any bytes are available.
Delimiter mode takes precedence over threshold mode.
Parameters:
sStream instance.
Returns:
true if the stream has data meeting the readability criteria.
function syn_stream_set_delimiter¶
Enable delimiter mode.
When enabled, syn_stream_readable() returns true only when the delimiter byte is present in the buffer. Pair with syn_stream_read_line() to consume one delimited chunk at a time.
Delimiter mode takes precedence over threshold mode.
Parameters:
sStream instance.delimDelimiter byte (e.g. '\n').
function syn_stream_set_threshold¶
Set the byte threshold for readability.
When set to N > 0, syn_stream_readable() returns true only when at least N bytes are in the buffer. Set to 0 to revert to default (readable when any bytes are available).
Parameters:
sStream instance.nByte threshold (0 = any).
function syn_stream_write¶
Write bytes into the stream.
ISR-safe (single-producer). Writes up to len bytes. Returns the number actually written (may be less if the buffer fills up).
Parameters:
sStream instance.dataSource data.lenNumber of bytes to write.
Returns:
Number of bytes actually written.
Public Static Functions Documentation¶
function stream_find_delimiter¶
Scan the ringbuf for the delimiter byte.
Parameters:
sStream to scan.
Returns:
1-based position of the first delimiter, or 0 if not found.
The documentation for this class was generated from the following file src/syntropic/util/syn_stream.c