CANopenNode
FIFO circular buffer

Files

file  CO_fifo.h
 FIFO circular buffer.
 

Data Structures

struct  CO_fifo_t
 Fifo object. More...
 

Enumerations

enum  CO_fifo_st {
  CO_fifo_st_closed = 0x01U, CO_fifo_st_partial = 0x02U, CO_fifo_st_errTok = 0x10U, CO_fifo_st_errVal = 0x20U,
  CO_fifo_st_errBuf = 0x40U, CO_fifo_st_errInt = 0x80U, CO_fifo_st_errMask = 0xF0U
}
 Bitfields for status argument from CO_fifo_cpyTok2U8 function and similar. More...
 

Functions

void CO_fifo_init (CO_fifo_t *fifo, char *buf, size_t bufSize)
 Initialize fifo object. More...
 
static void CO_fifo_reset (CO_fifo_t *fifo)
 Reset fifo object, make it empty. More...
 
static bool_t CO_fifo_purge (CO_fifo_t *fifo)
 Purge all data in fifo object, keep other properties. More...
 
static size_t CO_fifo_getSpace (CO_fifo_t *fifo)
 Get free buffer space in CO_fifo_t object. More...
 
static size_t CO_fifo_getOccupied (CO_fifo_t *fifo)
 Get size of data inside CO_fifo_t buffer object. More...
 
static bool_t CO_fifo_putc (CO_fifo_t *fifo, const char c)
 Put one character into CO_fifo_t buffer object. More...
 
static void CO_fifo_putc_ov (CO_fifo_t *fifo, const char c)
 Put one character into CO_fifo_t buffer object. More...
 
static bool_t CO_fifo_getc (CO_fifo_t *fifo, char *buf)
 Get one character from CO_fifo_t buffer object. More...
 
size_t CO_fifo_write (CO_fifo_t *fifo, const char *buf, size_t count, uint16_t *crc)
 Write data into CO_fifo_t object. More...
 
size_t CO_fifo_read (CO_fifo_t *fifo, char *buf, size_t count, bool_t *eof)
 Read data from CO_fifo_t object. More...
 
size_t CO_fifo_altBegin (CO_fifo_t *fifo, size_t offset)
 Initializes alternate read with CO_fifo_altRead. More...
 
void CO_fifo_altFinish (CO_fifo_t *fifo, uint16_t *crc)
 Ends alternate read with CO_fifo_altRead and calculate crc checksum. More...
 
static size_t CO_fifo_altGetOccupied (CO_fifo_t *fifo)
 Get alternate size of remaining data, see CO_fifo_altRead. More...
 
size_t CO_fifo_altRead (CO_fifo_t *fifo, char *buf, size_t count)
 Alternate read data from CO_fifo_t object. More...
 
bool_t CO_fifo_CommSearch (CO_fifo_t *fifo, bool_t clear)
 Search command inside FIFO. More...
 
bool_t CO_fifo_trimSpaces (CO_fifo_t *fifo, bool_t *insideComment)
 Trim spaces inside FIFO. More...
 
size_t CO_fifo_readToken (CO_fifo_t *fifo, char *buf, size_t count, char *closed, bool_t *err)
 Get token from FIFO buffer. More...
 
size_t CO_fifo_readU82a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint8_t variable from fifo and output as ascii string. More...
 
size_t CO_fifo_readU162a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint16_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readU322a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint32_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readU642a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint64_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readX82a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint8_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readX162a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint16_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readX322a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint32_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readX642a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read uint64_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readI82a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read int8_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readI162a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read int16_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readI322a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read int32_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readI642a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read int64_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readR322a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read float32_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readR642a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read float64_t variable from fifo as ascii string, see CO_fifo_readU82a.
 
size_t CO_fifo_readHex2a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read data from fifo and output as space separated two digit ascii string, see also CO_fifo_readU82a.
 
size_t CO_fifo_readVs2a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read data from fifo and output as visible string. More...
 
size_t CO_fifo_readB642a (CO_fifo_t *fifo, char *buf, size_t count, bool_t end)
 Read data from fifo and output as mime-base64 encoded string. More...
 
size_t CO_fifo_cpyTok2U8 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Read ascii string from src fifo and copy as uint8_t variable to dest fifo. More...
 
size_t CO_fifo_cpyTok2U16 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to uint16_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2U32 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to uint32_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2U64 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to uint64_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2I8 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to int8_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2I16 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to int16_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2I32 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to int32_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2I64 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to int64_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2R32 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to float32_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2R64 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy ascii string to float64_t variable, see CO_fifo_cpyTok2U8.
 
size_t CO_fifo_cpyTok2Hex (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy bytes written as two hex digits into to data. More...
 
size_t CO_fifo_cpyTok2Vs (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Copy visible string to data. More...
 
size_t CO_fifo_cpyTok2B64 (CO_fifo_t *dest, CO_fifo_t *src, CO_fifo_st *status)
 Read ascii mime-base64 encoded string from src fifo and copy as binary data to dest fifo. More...
 

Detailed Description

FIFO is organized as circular buffer with predefined capacity. It must be initialized by CO_fifo_init(). Functions are not not thread safe.

It can be used as general purpose FIFO circular buffer for any data. Data can be written by CO_fifo_write() and read by CO_fifo_read() functions.

Buffer has additional functions for usage with CiA309-3 standard. It acts as circular buffer for storing ascii commands and fetching tokens from them.

Enumeration Type Documentation

◆ CO_fifo_st

enum CO_fifo_st

Bitfields for status argument from CO_fifo_cpyTok2U8 function and similar.

Enumerator
CO_fifo_st_closed 

Bit is set, if command delimiter is reached in src.

CO_fifo_st_partial 

Bit is set, if copy was partial and more data are available.

If unset and no error, then all data was successfully copied.

CO_fifo_st_errTok 

Bit is set, if no valid token found.

CO_fifo_st_errVal 

Bit is set, if value is not valid or out of limits.

CO_fifo_st_errBuf 

Bit is set, if destination buffer is to small.

CO_fifo_st_errInt 

Bit is set, if internal error.

CO_fifo_st_errMask 

Bitmask for error bits.

Function Documentation

◆ CO_fifo_init()

void CO_fifo_init ( CO_fifo_t fifo,
char *  buf,
size_t  bufSize 
)

Initialize fifo object.

Parameters
fifoThis object will be initialized
bufPointer to externally defined buffer
bufSizeSize of the above buffer. Usable size of the buffer will be one byte less than bufSize, it is used for operation of the circular buffer.

◆ CO_fifo_reset()

static void CO_fifo_reset ( CO_fifo_t fifo)
inlinestatic

Reset fifo object, make it empty.

Parameters
fifoThis object

◆ CO_fifo_purge()

static bool_t CO_fifo_purge ( CO_fifo_t fifo)
inlinestatic

Purge all data in fifo object, keep other properties.

Parameters
fifoThis object
Returns
true, if data were purged or false, if fifo were already empty

◆ CO_fifo_getSpace()

static size_t CO_fifo_getSpace ( CO_fifo_t fifo)
inlinestatic

Get free buffer space in CO_fifo_t object.

Parameters
fifoThis object
Returns
number of available bytes

◆ CO_fifo_getOccupied()

static size_t CO_fifo_getOccupied ( CO_fifo_t fifo)
inlinestatic

Get size of data inside CO_fifo_t buffer object.

Parameters
fifoThis object
Returns
number of occupied bytes

◆ CO_fifo_putc()

static bool_t CO_fifo_putc ( CO_fifo_t fifo,
const char  c 
)
inlinestatic

Put one character into CO_fifo_t buffer object.

Parameters
fifoThis object
cCharacter to put
Returns
true, if write was successful (enough space in fifo buffer)

◆ CO_fifo_putc_ov()

static void CO_fifo_putc_ov ( CO_fifo_t fifo,
const char  c 
)
inlinestatic

Put one character into CO_fifo_t buffer object.

Overwrite old characters, if run out of space

Parameters
fifoThis object
cCharacter to put

◆ CO_fifo_getc()

static bool_t CO_fifo_getc ( CO_fifo_t fifo,
char *  buf 
)
inlinestatic

Get one character from CO_fifo_t buffer object.

Parameters
fifoThis object
bufBuffer of length one byte, where character will be copied
Returns
true, if read was successful (non-empty fifo buffer)

◆ CO_fifo_write()

size_t CO_fifo_write ( CO_fifo_t fifo,
const char *  buf,
size_t  count,
uint16_t crc 
)

Write data into CO_fifo_t object.

This function copies data from buf into internal buffer of CO_fifo_t object. Function returns number of bytes successfully copied. If there is not enough space in destination, not all bytes will be copied.

Parameters
fifoThis object
bufBuffer which will be copied
countNumber of bytes in buf
[in,out]crcExternally defined variable for CRC checksum, ignored if NULL
Returns
number of bytes actually written.

◆ CO_fifo_read()

size_t CO_fifo_read ( CO_fifo_t fifo,
char *  buf,
size_t  count,
bool_t eof 
)

Read data from CO_fifo_t object.

This function copies data from internal buffer of CO_fifo_t object into buf. Function returns number of bytes successfully copied. Function also writes true into eof argument, if command delimiter character is reached.

Parameters
fifoThis object
bufBuffer into which data will be copied
countCopy up to count bytes into buffer
[out]eofIf different than NULL, then search for delimiter character. If found, then read up to (including) that character and set *eof to true. Otherwise set *eof to false.
Returns
number of bytes actually read.

◆ CO_fifo_altBegin()

size_t CO_fifo_altBegin ( CO_fifo_t fifo,
size_t  offset 
)

Initializes alternate read with CO_fifo_altRead.

Parameters
fifoThis object
offsetOffset in bytes from original read pointer
Returns
same as offset or lower, if there is not enough data.

◆ CO_fifo_altFinish()

void CO_fifo_altFinish ( CO_fifo_t fifo,
uint16_t crc 
)

Ends alternate read with CO_fifo_altRead and calculate crc checksum.

Parameters
fifoThis object
[in,out]crcExternally defined variable for CRC checksum, ignored if NULL

◆ CO_fifo_altGetOccupied()

static size_t CO_fifo_altGetOccupied ( CO_fifo_t fifo)
inlinestatic

Get alternate size of remaining data, see CO_fifo_altRead.

Parameters
fifoThis object
Returns
number of occupied bytes.

◆ CO_fifo_altRead()

size_t CO_fifo_altRead ( CO_fifo_t fifo,
char *  buf,
size_t  count 
)

Alternate read data from CO_fifo_t object.

This function is similar as CO_fifo_read(), but uses alternate read pointer inside circular buffer. It reads data from the buffer and data remains in it. This function uses alternate read pointer and keeps original read pointer unchanged. Before using this function, alternate read pointer must be initialized with CO_fifo_altBegin(). CO_fifo_altFinish() sets original read pointer to alternate read pointer and so empties the buffer.

Parameters
fifoThis object
bufBuffer into which data will be copied
countCopy up to count bytes into buffer
Returns
number of bytes actually read.

◆ CO_fifo_CommSearch()

bool_t CO_fifo_CommSearch ( CO_fifo_t fifo,
bool_t  clear 
)

Search command inside FIFO.

If there are some data inside the FIFO, then search for command delimiter.

If command is long, then in the buffer may not be enough space for it. In that case buffer is full and no delimiter is present. Function then returns true and command should be processed for the starting tokens. Buffer can later be refilled multiple times, until command is closed by command delimiter.

If this function returns different than 0, then buffer is usually read by multiple CO_fifo_readToken() calls. If reads was successful, then delimiter is reached and fifo->readPtr is set after the command. If any CO_fifo_readToken() returns nonzero *err, then there is an error and command should be cleared. All this procedure must be implemented inside single function call.

Parameters
fifoThis object.
clearIf true, then command will be cleared from the buffer. If there is no delimiter, buffer will be cleared entirely.
Returns
true if command with delimiter found or buffer full.

◆ CO_fifo_trimSpaces()

bool_t CO_fifo_trimSpaces ( CO_fifo_t fifo,
bool_t insideComment 
)

Trim spaces inside FIFO.

Function removes all non graphical characters and comments from fifo buffer. It stops on first graphical character or on command delimiter (later is also removed).

Parameters
fifoThis object.
[in,out]insideCommentif set to true as input, it skips all characters and searches only for delimiter. As output it is set to true, if fifo is empty, is inside comment and command delimiter is not found.
Returns
true if command delimiter was found.

◆ CO_fifo_readToken()

size_t CO_fifo_readToken ( CO_fifo_t fifo,
char *  buf,
size_t  count,
char *  closed,
bool_t err 
)

Get token from FIFO buffer.

Function search FIFO buffer for token. Token is string of only graphical characters. Graphical character is any printable character except space with acsii code within limits: 0x20 < code < 0x7F (see isgraph() function).

If token is found, then copy it to the buf, if count is large enough. On success also set readPtr to point to the next graphical character.

Each token must have at least one empty space after it (space, command delimiter, '\0', etc.). Delimiter must not be graphical character.

If comment delimiter (delimComment, see CO_fifo_init) character is found, then all string till command delimiter (delimCommand, see CO_fifo_init) will be erased from the buffer.

See also CO_fifo_CommSearch().

Parameters
fifoThis object.
bufBuffer into which data will be copied. Will be terminated by '\0'.
countCopy up to count bytes into buffer
[in,out]closedThis is input/output variable. Not used if NULL.
  • As output variable it is set to 1, if command delimiter (delimCommand, see CO_fifo_init) is found after the token and set to 0 otherwise.
  • As input variable it is used for verifying error condition:
    • *closed = 0: Set *err to true if token is empty or command delimiter is found.
    • *closed = 1: Set *err to true if token is empty or command delimiter is NOT found.
    • *closed = any other value: No checking of token size or command delimiter.
[out]errIf not NULL, it is set to true if token is larger than buf or in matching combination in 'closed' argument. If it is already true, then function returns immediately.
Returns
Number of bytes read.

◆ CO_fifo_readU82a()

size_t CO_fifo_readU82a ( CO_fifo_t fifo,
char *  buf,
size_t  count,
bool_t  end 
)

Read uint8_t variable from fifo and output as ascii string.

Parameters
fifoThis object.
bufBuffer into which ascii string will be copied.
countAvailable count of bytes inside the buf.
endTrue indicates, that fifo contains last bytes of data.
Returns
Number of ascii bytes written into buf.

◆ CO_fifo_readVs2a()

size_t CO_fifo_readVs2a ( CO_fifo_t fifo,
char *  buf,
size_t  count,
bool_t  end 
)

Read data from fifo and output as visible string.

A visible string is enclosed with double quotes. If a double quote is used within the string, the quotes are escaped by a second quotes, e.g. “Hello “”World””, CANopen is great”. UTF-8 characters and also line breaks works with this function. Function removes all NULL and CR characters from output string. See also CO_fifo_readU82a

◆ CO_fifo_readB642a()

size_t CO_fifo_readB642a ( CO_fifo_t fifo,
char *  buf,
size_t  count,
bool_t  end 
)

Read data from fifo and output as mime-base64 encoded string.

Encoding is as specified in RFC 2045, without CR-LF, but one long string. See also CO_fifo_readU82a

◆ CO_fifo_cpyTok2U8()

size_t CO_fifo_cpyTok2U8 ( CO_fifo_t dest,
CO_fifo_t src,
CO_fifo_st status 
)

Read ascii string from src fifo and copy as uint8_t variable to dest fifo.

Parameters
destdestination fifo buffer object.
srcsource fifo buffer object.
[out]statusbitfield of the CO_fifo_st type.
Returns
Number of bytes written into dest.

◆ CO_fifo_cpyTok2Hex()

size_t CO_fifo_cpyTok2Hex ( CO_fifo_t dest,
CO_fifo_t src,
CO_fifo_st status 
)

Copy bytes written as two hex digits into to data.

Bytes may be space separated. See CO_fifo_cpyTok2U8 for parameters.

◆ CO_fifo_cpyTok2Vs()

size_t CO_fifo_cpyTok2Vs ( CO_fifo_t dest,
CO_fifo_t src,
CO_fifo_st status 
)

Copy visible string to data.

A visible string must be enclosed with double quotes, if it contains space. If a double quote is used within the string, the quotes are escaped by a second quotes. Input string can not contain newline characters. See CO_fifo_cpyTok2U8

◆ CO_fifo_cpyTok2B64()

size_t CO_fifo_cpyTok2B64 ( CO_fifo_t dest,
CO_fifo_t src,
CO_fifo_st status 
)

Read ascii mime-base64 encoded string from src fifo and copy as binary data to dest fifo.

Encoding is as specified in RFC 2045, without CR-LF, but one long string in single line. See also CO_fifo_readU82a