24 #define _MCF_TYPE_MASK 0x0FFF
25 #define _MCF_TYPE_SHIFT 0
26 #define _MCF_CLASS_MASK 0xF000
27 #define _MCF_CLASS_SHIFT 12
28 #define _MCF_LEN_MASK 0xFFC0
29 #define _MCF_LEN_SHIFT 6
30 #define _MCF_ID_MASK 0x003F
31 #define _MCF_ID_SHIFT 0
34 #define _MCF_MSGHDR_LEN 6
35 #define _MCF_HDR_LEN 6
37 static uint32_t mcfReadU32(
MCFState * state)
44 static uint16_t mcfReadU16(
MCFState * state)
52 static void mcfWriteU16(
MCFState * state, uint16_t v)
57 static void mcfWriteU32(
MCFState * state, uint32_t v)
65 state->buf.
cur = bufPtr;
66 state->buf.
start = bufPtr;
67 state->buf.
end = bufPtr + bufLen;
76 static void initDecode(
MCFState * state)
78 state->tsOffset = mcfReadU32(state);
79 state->msgCount = ( mcfReadU16(state) & 0x00FF ) + 1;
85 if (state->buf.
cur == state->buf.
start) initDecode(state);
87 state->buf.
cur += state->msg.cntLength;
92 if (state->msgCur >= state->msgCount)
return false;
96 state->msg.timestamp = state->tsOffset + mcfReadU16(state);
97 t = mcfReadU16(state);
99 state->msg.clazz = ( t & _MCF_CLASS_MASK ) >> _MCF_CLASS_SHIFT;
100 state->msg.typ = ( t & _MCF_TYPE_MASK ) >> _MCF_TYPE_SHIFT;
101 t = mcfReadU16(state);
102 state->msg.cntLength = ( t & _MCF_LEN_MASK ) >> _MCF_LEN_SHIFT;
103 state->msg.identifier = ( t & _MCF_ID_MASK ) >> _MCF_ID_SHIFT;
104 state->msg.cntPtr = state->buf.
cur;
108 static void mcfInitEncode(
MCFState * state)
113 state->buf.
cur += _MCF_HDR_LEN;
119 if (state->buf.
cur == state->buf.
start) {
122 return (uint32_t) (state->buf.
end - state->buf.
cur) - _MCF_HDR_LEN ;
129 if (state->buf.
cur == state->buf.
start) {
130 return (uint32_t)( state->buf.
end - state->buf.
start ) - ( _MCF_MSGHDR_LEN + _MCF_HDR_LEN );
132 return mcfEncodeFree(state) + state->msg.cntLength + _MCF_HDR_LEN;
135 static void mcfEncodeEntry(
MCFState * state)
139 mcfWriteU16(state, state->msg.timestamp - state->tsOffset);
140 t = ( state->msg.clazz << _MCF_CLASS_SHIFT ) & _MCF_CLASS_MASK;
141 t |= ( state->msg.typ << _MCF_TYPE_SHIFT) & _MCF_TYPE_MASK;
142 mcfWriteU16(state, t);
143 t = (state->msg.cntLength << _MCF_LEN_SHIFT) & _MCF_LEN_MASK;
144 t |= ( state->msg.identifier << _MCF_ID_SHIFT) & _MCF_ID_MASK;
145 mcfWriteU16(state, t);
146 state->buf.
cur += state->msg.cntLength;
147 state->msgCount += 1;
158 memcpy(state->msg.cntPtr, db->
start, len);
159 state->msg.cntLength = len;
166 if (state->buf.
cur == state->buf.
start) mcfInitEncode(state);
167 else mcfEncodeEntry(state);
169 state->msg.cntPtr = state->buf.
cur + _MCF_MSGHDR_LEN;
176 if (state->buf.
cur == state->buf.
start)
return 0;
177 mcfEncodeEntry(state);
178 uint32_t len = state->buf.
cur - state->buf.
start;
180 mcfWriteU32(state, state->tsOffset);
181 mcfWriteU16(state, state->msgCount - 1);
uint32_t mcfEncodeFinish(MCFState *state)
Call this after encoding to complete the MCF packet.
uint32_t _mcfTimeStamp()
Dependency function.
Message Container Format formatter / parser.
uint32_t mcfEncodeFree()
Returns the number of bytes available for content in the current message.
bool dbReadU16(DataBuffer *buf, uint16_t *u)
Reads a unsigned short (16 bits unsigned) integer.
uint8_t * end
End Pointer.
bool mcfEncodeBuffer(MCFState *state, DataBuffer *db)
Encodes the content of a databuffer into the container.
uint8_t * start
Start Pointer.
bool mcfEncodeNext(MCFState *state)
Creates a new encoding entry.
Defines a DataBuffer structure.
static uint32_t dbLength(DataBuffer *buf)
Returns the lenght of the current buffer.
Manages the global system error.
uint32_t mcfEncodeFreeNext()
Returns the number of bytes available for content in the NEXT message.
#define E_OUTOFMEMORY
Generic error: There is no more memory.
This module is responsible for distributing error codes.
bool dbReadU32(DataBuffer *buf, uint32_t *u)
Reads an 32 bits unsigned integer.
void mcfInitState(MCFState *state, uint8_t *bufPtr, uint32_t bufLen)
Initializes the decoding or encoding of a Message Container Formatted message.
bool errSet(uint32_t code, const char *error, const char *name)
Sets an error.
#define ERROR(CODE,...)
Expands an error code to an error code with a description (if ERROR_W_DESCR is declared).
bool mcfDecodeNext(MCFState *state)
Decodes the next message.
uint8_t * cur
Current Pointer.
bool dbWriteU32(DataBuffer *buf, uint32_t u)
Writes an 32 bits unsigned integer.
bool dbWriteU16(DataBuffer *buf, uint16_t u)
Writes a unsigned short (16 bits unsigned) integer.