KM3NeT CLB  2.0
KM3NeT CLB v2 Embedded Software
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
databuffer.h File Reference

DataBuffer reads and writes data into a buffer in the same format as defined in the data Java DataOutput / DataInput interfaces. More...

#include <stdint.h>
#include <stdbool.h>
#include <stddef.h>
#include "util/float.h"

Go to the source code of this file.

Data Structures

struct  DataBuffer
 Defines a DataBuffer structure. More...
 

Macros

#define DB_ERR_NONE   0x00
 Indicates no error.
 
#define DB_ERR_INVALID_BUFFER   0x01
 Indicates an invalid buffer.
 
#define DB_ERR_END_OF_BUFFER   0x02
 Indicates the end of the buffer has been reached.
 
#define DB_ERR_NOT_SUPPORTED   0x03
 De type is not supported. More...
 
#define DB_STR_OVERHEAD   2
 Overhead for a String.
 
#define DB_BUF_INIT(PTR, LEN)   { .start = PTR, .cur = PTR, .end = PTR + LEN, .error = DB_ERR_NONE }
 Simple buffer initialization. More...
 

Functions

static void dbReset (DataBuffer *buf)
 Resets the buffer to its initial state. More...
 
static uint32_t dbLength (DataBuffer *buf)
 Returns the lenght of the current buffer. More...
 
static uint32_t dbFree (DataBuffer *buf)
 Returns the number of bytes still free in the buffer. More...
 
bool dbSkip (DataBuffer *buf, size_t skipSize)
 Skips a number of bytes in the buffer. More...
 
static uint32_t dbSize (DataBuffer *buf)
 Returns the total size of the buffer. More...
 
bool dbWrite (DataBuffer *buf, uint8_t *buffer, int len)
 Writes the given buffer with the specified length. More...
 
bool dbRead (DataBuffer *buf, uint8_t *buffer, int len)
 Reads into given buffer with the specified length. More...
 
bool dbWriteI8 (DataBuffer *buf, int8_t byte)
 Writes a byte. More...
 
bool dbReadI8 (DataBuffer *buf, int8_t *byte)
 Reads an byte. More...
 
bool dbWriteU8 (DataBuffer *buf, uint8_t byte)
 Writes a unsigned byte. More...
 
bool dbReadU8 (DataBuffer *buf, uint8_t *byte)
 Reads an unsigned byte. More...
 
bool dbWriteF32 (DataBuffer *buf, f32_t flt)
 Writes a 32-bit floating point. More...
 
bool dbReadF32 (DataBuffer *buf, f32_t *flt)
 Reads an 32-bit floating point. More...
 
bool dbWriteF64 (DataBuffer *buf, f64_t flt)
 Writes a 64-bit floating point. More...
 
bool dbReadF64 (DataBuffer *buf, f64_t *flt)
 Reads an 64-bit floating point. More...
 
static bool dbWriteBool (DataBuffer *buf, bool boolean)
 Writes a boolean. More...
 
static bool dbReadBool (DataBuffer *buf, bool *boolean)
 Reads a boolean. More...
 
bool dbWriteI16 (DataBuffer *buf, int16_t i)
 Writes a short (16 bits signed) integer. More...
 
bool dbWriteU16 (DataBuffer *buf, uint16_t u)
 Writes a unsigned short (16 bits unsigned) integer. More...
 
bool dbReadI16 (DataBuffer *buf, int16_t *i)
 Reads a short (16 bits signed) integer. More...
 
bool dbReadU16 (DataBuffer *buf, uint16_t *u)
 Reads a unsigned short (16 bits unsigned) integer. More...
 
bool dbWriteI32 (DataBuffer *buf, int32_t i)
 Writes an 32 bits signed integer. More...
 
bool dbWriteU32 (DataBuffer *buf, uint32_t u)
 Writes an 32 bits unsigned integer. More...
 
bool dbReadI32 (DataBuffer *buf, int32_t *i)
 Reads an 32 bits signed integer. More...
 
bool dbReadU32 (DataBuffer *buf, uint32_t *u)
 Reads an 32 bits unsigned integer. More...
 
bool dbWriteI64 (DataBuffer *buf, int64_t i)
 Writes an 64 bits signed integer. More...
 
bool dbReadI64 (DataBuffer *buf, int64_t *i)
 Reads an 64 bits signed integer. More...
 
bool dbWriteU64 (DataBuffer *buf, uint64_t i)
 Writes an 64 bits unsigned integer. More...
 
bool dbReadU64 (DataBuffer *buf, uint64_t *i)
 Reads an 64 bits unsigned integer. More...
 
bool dbWriteString (DataBuffer *buf, const char *s, int max)
 Writes a String as 'sort of' UTF-8 encoding, as defined in the Java DataOuput and DataInput writeUTF / readUTF methods. More...
 
bool dbReadString (DataBuffer *buf, char *s, int size)
 Reads a string from the stream. More...
 

Detailed Description

DataBuffer reads and writes data into a buffer in the same format as defined in the data Java DataOutput / DataInput interfaces.

Supported are: the 'Write', Byte, Short, Int, Long and Boolean.

Definition in file databuffer.h.

Macro Definition Documentation

#define DB_BUF_INIT (   PTR,
  LEN 
)    { .start = PTR, .cur = PTR, .end = PTR + LEN, .error = DB_ERR_NONE }

Simple buffer initialization.

Usage: char buf[80]; DataBuffer db = DB_BUF_INIT( buf, sizeof(buf) );

Parameters
PTRThe pointer to the start of the buffer.
LENThe length of the buffer in bytes.

Definition at line 63 of file databuffer.h.

#define DB_ERR_NOT_SUPPORTED   0x03

De type is not supported.

Definition at line 37 of file databuffer.h.

Function Documentation

static uint32_t dbFree ( DataBuffer buf)
inlinestatic

Returns the number of bytes still free in the buffer.

Parameters
bufThe buffer to query.
Returns
The number of bytes free in the buffer.

Definition at line 95 of file databuffer.h.

static uint32_t dbLength ( DataBuffer buf)
inlinestatic

Returns the lenght of the current buffer.

Parameters
bufThe buffer to query.
Returns
The number of bytes occupied in the buffer.

Definition at line 83 of file databuffer.h.

bool dbRead ( DataBuffer buf,
uint8_t *  buffer,
int  len 
)

Reads into given buffer with the specified length.

Parameters
bufferThe buffer to copy in to.
lenThe number of bytes to copy
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 48 of file databuffer.c.

static bool dbReadBool ( DataBuffer buf,
bool *  boolean 
)
inlinestatic

Reads a boolean.

Parameters
bufThe buffer to read from.
booleanThe boolean.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 263 of file databuffer.h.

bool dbReadF32 ( DataBuffer buf,
f32_t flt 
)

Reads an 32-bit floating point.

Parameters
bufThe buffer to read from.
fltThe the byte
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 340 of file databuffer.c.

bool dbReadF64 ( DataBuffer buf,
f64_t flt 
)

Reads an 64-bit floating point.

Parameters
bufThe buffer to read from.
fltThe the byte
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 345 of file databuffer.c.

bool dbReadI16 ( DataBuffer buf,
int16_t *  i 
)

Reads a short (16 bits signed) integer.

Parameters
bufThe buffer to write into.
iPointer to a 16 bit signed integer. Will write the value to the location of the pointer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 194 of file databuffer.c.

bool dbReadI32 ( DataBuffer buf,
int32_t *  i 
)

Reads an 32 bits signed integer.

Parameters
bufThe buffer to write into.
iPointer to the 32 bit signed integer. Will write the value to the location of the pointer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 262 of file databuffer.c.

bool dbReadI64 ( DataBuffer buf,
int64_t *  i 
)

Reads an 64 bits signed integer.

Parameters
bufThe buffer to write into.
iPointer to the 64 bit signed integer. Will write the value to the location of the pointer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 284 of file databuffer.c.

bool dbReadI8 ( DataBuffer buf,
int8_t *  byte 
)

Reads an byte.

Parameters
bufThe buffer to read from.
byteThe the byte
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 136 of file databuffer.c.

bool dbReadString ( DataBuffer buf,
char *  s,
int  size 
)

Reads a string from the stream.

Values which are out of the range of this sytems char type are replaced with a question mark.

See Also
http://docs.oracle.com/javase/6/docs/api/java/io/DataOutput.html#writeUTF(java.lang.String)
Parameters
bufThe buffer to read from.
sThe target string pointer with pre-allocated memory
sizeThe size of hte target memory to prevent overflow.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 103 of file databuffer.c.

bool dbReadU16 ( DataBuffer buf,
uint16_t *  u 
)

Reads a unsigned short (16 bits unsigned) integer.

Parameters
bufThe buffer to write into.
uPointer to a 16 bit unsigned integer. Will write the value to the location of the pointer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 186 of file databuffer.c.

bool dbReadU32 ( DataBuffer buf,
uint32_t *  u 
)

Reads an 32 bits unsigned integer.

Parameters
bufThe buffer to write into.
uPointer to the 32 bit unsigned integer. Will write the value to the location of the pointer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 219 of file databuffer.c.

bool dbReadU64 ( DataBuffer buf,
uint64_t *  i 
)

Reads an 64 bits unsigned integer.

Parameters
bufThe buffer to write into.
iPointer to the 64 bit unsigned integer. Will write the value to the location of the pointer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 230 of file databuffer.c.

bool dbReadU8 ( DataBuffer buf,
uint8_t *  byte 
)

Reads an unsigned byte.

Parameters
bufThe buffer to read from.
byteThe the byte
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 153 of file databuffer.c.

static void dbReset ( DataBuffer buf)
inlinestatic

Resets the buffer to its initial state.

Parameters
bufThe buffer to reset.

Definition at line 70 of file databuffer.h.

static uint32_t dbSize ( DataBuffer buf)
inlinestatic

Returns the total size of the buffer.

Parameters
bufThe buffer to query.
Returns
The total size of the buffer.

Definition at line 119 of file databuffer.h.

bool dbSkip ( DataBuffer buf,
size_t  skipSize 
)

Skips a number of bytes in the buffer.

Parameters
bufThe buffer.
skipSizeThe number of bytes to skip
Return values
trueSuccess
falseFailure, check buf.error. Prolly not so many bytes available.

Definition at line 179 of file databuffer.c.

bool dbWrite ( DataBuffer buf,
uint8_t *  buffer,
int  len 
)

Writes the given buffer with the specified length.

Parameters
bufferThe buffer to copy
lenThe number of bytes to copy
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 40 of file databuffer.c.

static bool dbWriteBool ( DataBuffer buf,
bool  boolean 
)
inlinestatic

Writes a boolean.

Parameters
bufThe buffer to write into.
booleanThe boolean.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 249 of file databuffer.h.

bool dbWriteF32 ( DataBuffer buf,
f32_t  flt 
)

Writes a 32-bit floating point.

Parameters
bufThe buffer to write into.
fltThe floating-point number.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 318 of file databuffer.c.

bool dbWriteF64 ( DataBuffer buf,
f64_t  flt 
)

Writes a 64-bit floating point.

Parameters
bufThe buffer to write into.
fltThe floating-point number.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 332 of file databuffer.c.

bool dbWriteI16 ( DataBuffer buf,
int16_t  i 
)

Writes a short (16 bits signed) integer.

Parameters
bufThe buffer to write into.
iThe 16 bit signed integer to write.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 173 of file databuffer.c.

bool dbWriteI32 ( DataBuffer buf,
int32_t  i 
)

Writes an 32 bits signed integer.

Parameters
bufThe buffer to write into.
iThe 32 bit signed integer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 213 of file databuffer.c.

bool dbWriteI64 ( DataBuffer buf,
int64_t  i 
)

Writes an 64 bits signed integer.

Parameters
bufThe buffer to write into.
iThe 64 bit signed integer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 269 of file databuffer.c.

bool dbWriteI8 ( DataBuffer buf,
int8_t  byte 
)

Writes a byte.

Parameters
bufThe buffer to write into.
byteThe byte.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 129 of file databuffer.c.

bool dbWriteString ( DataBuffer buf,
const char *  s,
int  max 
)

Writes a String as 'sort of' UTF-8 encoding, as defined in the Java DataOuput and DataInput writeUTF / readUTF methods.

However, it does not write any characters above ASCII value 127. If these values are encountered, a '@' (at) sign will be inserted instead.

See Also
http://docs.oracle.com/javase/6/docs/api/java/io/DataOutput.html#writeUTF(java.lang.String)
Parameters
bufThe buffer to write to
sThe string to write (must be null-terminated!)
maxThe maximum bytes in buffer it should use. Else string is truncated.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 74 of file databuffer.c.

bool dbWriteU16 ( DataBuffer buf,
uint16_t  u 
)

Writes a unsigned short (16 bits unsigned) integer.

Parameters
bufThe buffer to write into.
uThe 16 bit unsigned integer to write.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 162 of file databuffer.c.

bool dbWriteU32 ( DataBuffer buf,
uint32_t  u 
)

Writes an 32 bits unsigned integer.

Parameters
bufThe buffer to write into.
uThe 32 bit unsigned integer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 200 of file databuffer.c.

bool dbWriteU64 ( DataBuffer buf,
uint64_t  i 
)

Writes an 64 bits unsigned integer.

Parameters
bufThe buffer to write into.
iThe 64 bit signed integer.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 245 of file databuffer.c.

bool dbWriteU8 ( DataBuffer buf,
uint8_t  byte 
)

Writes a unsigned byte.

Parameters
bufThe buffer to write into.
byteThe byte.
Return values
trueSuccess
falseFailure, check buf.error

Definition at line 146 of file databuffer.c.