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

Message Container Format formatter / parser. More...

#include "util/databuffer.h"

Go to the source code of this file.

Data Structures

struct  MCFMessage
 
struct  MCFState
 

Macros

#define MCF_CLASS_ERROR   0x00
 
#define MCF_CLASS_CMD   0x01
 
#define MCF_CLASS_REPLY   0x02
 
#define MCF_CLASS_EVENT   0x03
 
#define MCF_TYPE_GROUP_SHIFT   6
 
#define MCF_TYPE_GROUP_MASK   0x0FC0
 
#define MCF_TYPE_NUM_SHIFT   0
 
#define MCF_TYPE_NUM_MASK   0x003F
 
#define MCF_MK_TYPE(GROUP, NUM)
 

Functions

uint32_t _mcfTimeStamp ()
 Dependency function. More...
 
void mcfInitState (MCFState *state, uint8_t *bufPtr, uint32_t bufLen)
 Initializes the decoding or encoding of a Message Container Formatted message. More...
 
bool mcfDecodeNext (MCFState *state)
 Decodes the next message. More...
 
bool mcfEncodeNext (MCFState *state)
 Creates a new encoding entry. More...
 
uint32_t mcfEncodeFree ()
 Returns the number of bytes available for content in the current message. More...
 
bool mcfEncodeBuffer (MCFState *state, DataBuffer *db)
 Encodes the content of a databuffer into the container. More...
 
uint32_t mcfEncodeFreeNext ()
 Returns the number of bytes available for content in the NEXT message. More...
 
uint32_t mcfEncodeFinish (MCFState *state)
 Call this after encoding to complete the MCF packet. More...
 

Detailed Description

Message Container Format formatter / parser.

Reading a MCF message can be done as follows:

void decodeMCF(uint8_t * ptr, int bufLen)
{
MFCState decoderState;
MFCMessage * msg = &decoderState.msg;
mfcInit(&decoderState, ptr, bufLen);
while (mfcDecodeNext(&decoderState))
{
printf("Current Message %u of %u\n", decoderState.msgCur, decoderState.msgCount);
printf("- Type: %u.%u\n", MFC_TYP2GROUP(msg->typ), MFC_TYP2SUB(msg->typ));
printf("- Time : %lu, Length: %u\n", msg->timestamp, msg->cntLength);
printf("- Class: %u, Identifer: %u\u", msg->clazz, msg->identifier);
// obtain msg->cntPtr for the actual content.
}
}

Write one can also be done. The next example writes a MCF message with two commands, the first contains 'hello' and the second contains 'world'.

#define HELLO_GROUP 0x1
#define HELLO_HELLO MFC_2TYPE(HELLO_GROUP, 0x1)
#define HELLO_WORLD MFC_2TYPE(HELLO_GROUP, 0x2)
static int msgId;
void encodeMsgStr(MFCState * state, uint16_t typ, char * cnt, int cntLen)
{
MFCMessage * msg = encoderState->msg;
mfcEncodeNext(state);
msg->class = MFC_CLASS_CMD;
msg->typ = typ;
msg->identifier = msgId++;
msg->timestamp = timeGetDeayMillis();
memcopy(msg->cntPtr, cnt, cntLen);
msg->cntLen = cntLen;
}
void encodeMCF(uint8_t * ptr, int bufLen)
{
MFCState encoderState;
mfcInit(&encoderState, ptr, bufLen);
encodeMsgStr(&encoderState, HELLO_HELLO, "hello", 5);
encodeMsgStr(&encoderState, HELLO_WORLD, "world", 5);
mfcEncodeFinish(&encoderState);
}

Definition in file mcf.h.

Macro Definition Documentation

#define MCF_MK_TYPE (   GROUP,
  NUM 
)
Value:
( ( ( GROUP ) << MCF_TYPE_GROUP_SHIFT & MCF_TYPE_GROUP_MASK ) | \
( ( NUM ) << MCF_TYPE_NUM_SHIFT & MCF_TYPE_NUM_MASK ) )

Definition at line 90 of file mcf.h.

Function Documentation

uint32_t _mcfTimeStamp ( )

Dependency function.

Should return milliseconds since midnight, UTF

Returns

Dependency function.

Definition at line 90 of file network.c.

bool mcfDecodeNext ( MCFState state)

Decodes the next message.

After initializing the MCF structure, you should first call this function before reading the message content.

Parameters
stateThe decoder state.
Return values
trueThere is a next message to be decoded false There are no more messages to be decoded, or interpretation failure.

Definition at line 82 of file mcf.c.

bool mcfEncodeBuffer ( MCFState state,
DataBuffer db 
)

Encodes the content of a databuffer into the container.

Parameters
stateThe encoder state
dbThe databuffer
Return values
trueDataBuffer copied as content false DataBuffer not copied, probably not enough space. See err module.

Definition at line 157 of file mcf.c.

uint32_t mcfEncodeFinish ( MCFState state)

Call this after encoding to complete the MCF packet.

Parameters
stateThe decoder state.
Returns
The complete length of the MCF message.

Definition at line 180 of file mcf.c.

uint32_t mcfEncodeFree ( )

Returns the number of bytes available for content in the current message.

Returns
The number of bytes available for content in the next message.
uint32_t mcfEncodeFreeNext ( )

Returns the number of bytes available for content in the NEXT message.

Use this to decide whether or not you wish to do a mcfEncodeNext() or mcfEncodeFinish() and start a new message.

Returns
The number of bytes available for content in the next message.
bool mcfEncodeNext ( MCFState state)

Creates a new encoding entry.

After initializing the MCF structure, you should first call this function before writing the message content.

Parameters
stateThe decoder state.
Return values
trueThe next entry was created, you can fill it in. false The next entry could not be created or the previous could not be finalized. Either the time span is too large or it is out of space.

Definition at line 170 of file mcf.c.

void mcfInitState ( MCFState state,
uint8_t *  bufPtr,
uint32_t  bufLen 
)

Initializes the decoding or encoding of a Message Container Formatted message.

Parameters
stateA pointer to a MCFState object. All values will be overwritten.
bufPtrA pointer to a buffer to read from or write into.
bufLenThe length of the buffer.

Definition at line 63 of file mcf.c.