XgPerci

Data Structures

struct  PERCI_RECORD
struct  PERCI_WRITER
struct  PERCI_READER

Defines

#define PERCI_RECSIZE   256
#define PERCI_DATASIZE   (PERCI_RECSIZE - sizeof(perci_reclen_t))

Typedefs

typedef uint8_t perci_recnum_t
typedef uint_fast8_t perci_fast_recnum_t
typedef uint8_t perci_reclen_t
typedef uint_fast8_t perci_fast_reclen_t

Functions

int PerCiInit (char *path, int recs)
 Initialize a ring buffer file.
PERCI_WRITERPerCiOpen (char *path)
 Open a ring buffer file.
void PerCiClose (PERCI_WRITER *writer)
 Close a ring buffer file.
void PerCiFlush (PERCI_WRITER *writer)
 Flush the current write buffer.
int PerCiWrite (PERCI_WRITER *writer, CONST char *data, int len)
 Write to a ring buffer file.
int PerCiWriteFormat (PERCI_WRITER *writer, CONST char *fmt,...)
 Write formatted line to a ring buffer file.
int PerCiWriteVarList (PERCI_WRITER *writer, CONST char *fmt, va_list ap)
 Write formatted line to a ring buffer file.
PERCI_READERPerCiAttachReader (PERCI_WRITER *writer)
 Start reading from a ring buffer file.
void PerCiDetachReader (PERCI_READER *reader)
 Stop reading from a ring buffer file.
int PerCiRead (PERCI_READER *reader, char *data, int len)
 Read data from a ring buffer file.
int PerCiReadLine (PERCI_READER *reader, char *line, int len)
 Read a text line from a ring buffer file.
void PerCiDump (FILE *stream, char *path)
 Dump ring buffer file to given stream.

Variables

uint8_t PERCI_RECORD::pcd_data [PERCI_RECSIZE]
long PERCI_WRITER::pcw_size
HANDLE PERCI_WRITER::pcw_mutex
perci_fast_recnum_t PERCI_WRITER::pcw_recnum
PERCI_RECORD PERCI_WRITER::pcw_rec
perci_fast_recnum_t PERCI_READER::pcr_recnum
perci_fast_reclen_t PERCI_READER::pcr_reclen
perci_fast_reclen_t PERCI_READER::pcr_recpos

Define Documentation

#define PERCI_RECSIZE   256

Definition at line 75 of file perci.h.

Referenced by PerCiFlush(), PerCiInit(), PerCiOpen(), and PerCiWrite().

#define PERCI_DATASIZE   (PERCI_RECSIZE - sizeof(perci_reclen_t))

Definition at line 89 of file perci.h.

Referenced by PerCiDump(), PerCiOpen(), PerCiWrite(), and PerCiWriteVarList().


Typedef Documentation

Definition at line 63 of file perci.h.

Definition at line 64 of file perci.h.

Definition at line 79 of file perci.h.

Definition at line 80 of file perci.h.


Function Documentation

int PerCiInit ( char *  path,
int  recs 
)

Initialize a ring buffer file.

If the file doesn't exist, a new file will be created. If the file exists, any buffered data will be erased.

The total size of the file is

 recs * PERCI_RECSIZE + sizeof(perci_reclen_t)

bytes.

The available data space is

bytes.

Parameters:
path Path name of the file.
recs Number of buffer records, at least 2.
Returns:
0 on success and -1 on failure.

Definition at line 151 of file perci.c.

References _close(), _O_BINARY, _O_CREAT, _O_RDWR, _O_TRUNC, _open(), _write(), calloc, free, NUTASSERT, and PERCI_RECSIZE.

PERCI_WRITER * PerCiOpen ( char *  path  ) 

Open a ring buffer file.

The file must have been created by a previous call to PerCiInit().

The function failes, if the file doesn't exist or if it is corrupted. Applications may use the following sequence to open a circular buffer.

 PERCI_WRITER *perci;
 char *path = "UFLASH0:data.log";
 
 while ((perci = PerCiOpen(path)) == NULL) {
     if (PerCiInit(path, 128)) {
         printf("Error %d creating %s\n", errno, path);
         break;
     }
 }
Parameters:
path Path name of the file.
Returns:
A pointer to a PERCI_WRITER structure on success. The return of a NULL pointer indicates a failure.

Definition at line 202 of file perci.c.

References _close(), _filelength(), _O_BINARY, _O_RDWR, _open(), _read(), _seek(), calloc, free, NUTASSERT, NutEventPost(), PERCI_RECORD::pcd_data, PERCI_RECORD::pcd_len, PERCI_WRITER::pcw_fd, PERCI_WRITER::pcw_mutex, PERCI_WRITER::pcw_rec, PERCI_WRITER::pcw_recnum, PERCI_WRITER::pcw_size, PERCI_DATASIZE, PERCI_RECSIZE, SEEK_CUR, and SEEK_SET.

void PerCiClose ( PERCI_WRITER writer  ) 

Close a ring buffer file.

Parameters:
writer Pointer to a PERCI_WRITER structure obtained by a previous call to PerCiOpen().

Definition at line 285 of file perci.c.

References _close(), free, NUTASSERT, PERCI_RECORD::pcd_len, PERCI_WRITER::pcw_fd, PERCI_WRITER::pcw_rec, and PerCiFlush().

void PerCiFlush ( PERCI_WRITER writer  ) 

Flush the current write buffer.

Parameters:
writer Pointer to a PERCI_WRITER structure obtained by a previous call to PerCiOpen().

Definition at line 269 of file perci.c.

References _seek(), _write(), NUTASSERT, PERCI_RECORD::pcd_len, PERCI_WRITER::pcw_fd, PERCI_WRITER::pcw_rec, PERCI_WRITER::pcw_recnum, PERCI_RECSIZE, and SEEK_SET.

Referenced by PerCiClose().

int PerCiWrite ( PERCI_WRITER writer,
CONST char *  data,
int  len 
)

Write to a ring buffer file.

Parameters:
writer Pointer to a PERCI_WRITER structure obtained by a previous call to PerCiOpen().
Returns:
The number of bytes successfully written or -1 on failure.

Definition at line 305 of file perci.c.

References _seek(), _write(), memcpy(), NUT_WAIT_INFINITE, NUTASSERT, NutEventPost(), NutEventWait(), PERCI_RECORD::pcd_data, PERCI_RECORD::pcd_len, PERCI_WRITER::pcw_fd, PERCI_WRITER::pcw_mutex, PERCI_WRITER::pcw_rec, PERCI_WRITER::pcw_recnum, PERCI_WRITER::pcw_size, PERCI_DATASIZE, PERCI_RECSIZE, and SEEK_SET.

Referenced by PerCiWriteVarList().

int PerCiWriteFormat ( PERCI_WRITER writer,
CONST char *  fmt,
  ... 
)

Write formatted line to a ring buffer file.

Parameters:
writer Pointer to a PERCI_WRITER structure obtained by a previous call to PerCiOpen().
fmt Format string containing conversion specifications like printf.
Returns:
The number of bytes successfully written or -1 on failure.

Definition at line 404 of file perci.c.

References NUTASSERT, and PerCiWriteVarList().

int PerCiWriteVarList ( PERCI_WRITER writer,
CONST char *  fmt,
va_list  ap 
)

Write formatted line to a ring buffer file.

Alternate form of PerCiWriteFormat(), in which the arguments have already been captured using the variable-length argument facilities.

Parameters:
writer Pointer to a PERCI_WRITER structure obtained by a previous call to PerCiOpen().
fmt Format string containing conversion specifications like printf.
ap Pointer to the list of arguments.
Returns:
The number of bytes successfully written or -1 on failure.

Definition at line 372 of file perci.c.

References free, malloc, NUTASSERT, PERCI_DATASIZE, PerCiWrite(), strlen(), and vsprintf().

Referenced by PerCiWriteFormat().

PERCI_READER * PerCiAttachReader ( PERCI_WRITER writer  ) 

Start reading from a ring buffer file.

It is assumed, that the ring buffer is kept open while the application is running. From time to time the buffered data will be retrieved. Therefore, a reader is attached to a previously open file and reading starts at the oldest record.

When all data has been read, the application must call PerCiDetachReader() to relase all allocated resources for reading.

Multiple readers may be concurrently attached to the same file.

Parameters:
writer Pointer to a PERCI_WRITER structure obtained by a previous call to PerCiOpen().
Returns:
A pointer to a PERCI_READER structure on success. The return of a NULL pointer indicates a failure.

Definition at line 490 of file perci.c.

References malloc, NUTASSERT, PERCI_READER::pcr_cil, PERCI_READER::pcr_reclen, PERCI_READER::pcr_recnum, PERCI_READER::pcr_recpos, and PERCI_WRITER::pcw_recnum.

void PerCiDetachReader ( PERCI_READER reader  ) 

Stop reading from a ring buffer file.

Parameters:
reader Pointer to a PERCI_READER structure obtained by a previous call to PerCiAttachReader().

Definition at line 514 of file perci.c.

References free, and NUTASSERT.

int PerCiRead ( PERCI_READER reader,
char *  data,
int  len 
)

Read data from a ring buffer file.

Parameters:
reader Pointer to a PERCI_READER structure obtained by a previous call to PerCiAttachReader().
data Pointer to the buffer that receives the data.
len Number of bytes to read.
Returns:
The number of bytes read. If this is lower than the requested length, then we reached the end.

Definition at line 532 of file perci.c.

References _read(), memcpy(), NUTASSERT, PERCI_RECORD::pcd_data, PERCI_READER::pcr_cil, PERCI_READER::pcr_reclen, PERCI_READER::pcr_recnum, PERCI_READER::pcr_recpos, PERCI_WRITER::pcw_fd, PERCI_WRITER::pcw_rec, and PERCI_WRITER::pcw_recnum.

Referenced by PerCiReadLine().

int PerCiReadLine ( PERCI_READER reader,
char *  line,
int  len 
)

Read a text line from a ring buffer file.

Parameters:
reader Pointer to a PERCI_READER structure obtained by a previous call to PerCiAttachReader().
data Pointer to the buffer that receives the data.
len Number of bytes to read.
Returns:
The number of bytes read. If this is lower than the requested length, then we reached the end.

Definition at line 602 of file perci.c.

References NUTASSERT, and PerCiRead().

void PerCiDump ( FILE stream,
char *  path 
)

Dump ring buffer file to given stream.

Parameters:
path Path name of the file.
stream Stream to dump the contents to.

Definition at line 71 of file perci.c.

References _close(), _filelength(), _O_BINARY, _O_RDWR, _open(), _read(), errno, fprintf(), fputc(), fputs(), free, malloc, and PERCI_DATASIZE.


Variable Documentation

uint8_t PERCI_RECORD::pcd_data[PERCI_RECSIZE] [inherited]

Definition at line 93 of file perci.h.

Referenced by PerCiOpen(), PerCiRead(), and PerCiWrite().

long PERCI_WRITER::pcw_size [inherited]

Definition at line 98 of file perci.h.

Referenced by PerCiOpen(), and PerCiWrite().

Definition at line 99 of file perci.h.

Referenced by PerCiOpen(), and PerCiWrite().

Definition at line 100 of file perci.h.

Referenced by PerCiAttachReader(), PerCiFlush(), PerCiOpen(), PerCiRead(), and PerCiWrite().

Definition at line 101 of file perci.h.

Referenced by PerCiClose(), PerCiFlush(), PerCiOpen(), PerCiRead(), and PerCiWrite().

Definition at line 106 of file perci.h.

Referenced by PerCiAttachReader(), and PerCiRead().

Definition at line 107 of file perci.h.

Referenced by PerCiAttachReader(), and PerCiRead().

Definition at line 108 of file perci.h.

Referenced by PerCiAttachReader(), and PerCiRead().


© 2000-2010 by contributors - visit http://www.ethernut.de/