34 #define MSG_MAX_SIZE 1024
36 static bool _msgTxHoldOff;
37 static uint8_t _msgBuffer[MSG_MAX_SIZE];
39 static MCFMessage *
const _msgTxMsg = &(_msgTxState.msg);
41 static uint16_t _msgId;
51 #define MSG_CMD(CMD_TYPE, CMD_STUB) \
52 bool _msg ## CMD_STUB (MsgId * id, DataBuffer * buf);
57 #define MSG_EVT(EVT_TYPE, EVT_STUB) \
58 void _evt ## EVT_STUB (MsgId * id, DataBuffer * buf);
66 #define MSG_CMD(CMD_TYPE, CMD_STUB) \
67 bool _msg ## CMD_STUB (MsgId * id, DataBuffer * buf) __attribute__((weak, alias("_msgNotImpl")));
72 #define MSG_EVT(EVT_TYPE, EVT_STUB) \
73 void _evt ## EVT_STUB (MsgId * id, DataBuffer * buf) __attribute__((weak, alias("_evtNotImpl")));
92 #define MSG_CMD(CMD_TYPE, CMD_STUB) \
94 if (! _msg ## CMD_STUB(id, buf)) goto failed; \
104 sprintf(msg,
"RX Type=%04x", id->
msgType);
124 #define MSG_EVT(EVT_TYPE, EVT_STUB) \
126 _evt ## EVT_STUB(id, buf); \
148 if (buf->
cur != buf->
end) {
161 .msgId = msg->identifier
170 case MCF_CLASS_EVENT:
181 static void msgTxInit(
SockAddr * addr)
187 static void msgTxSend()
190 _msgTx(&_msgTxAddr, _msgBuffer, len);
199 _msgTxHoldOff =
true;
206 _msgTxHoldOff =
false;
219 msgTxInit(&(id->
addr));
223 msgTxInit(&(id->
addr));
228 _msgTxMsg->clazz = clazz;
229 _msgTxMsg->typ =
id->msgType;
230 _msgTxMsg->identifier =
id->msgId;
243 _msgTxMsg->cntLength = 0;
246 if (!_msgTxHoldOff) msgTxSend();
253 if (sockAddr == NULL) {
255 logInfo(
"Event target cleared");
258 if ( _msgEvtAddr.
ip == sockAddr->
ip && _msgEvtAddr.
port == sockAddr->
port )
return;
259 _msgEvtAddr = *sockAddr;
260 logInfo(
"Event target set to %08x:%u", _msgEvtAddr.
ip, _msgEvtAddr.
port);
266 *sockAddr = _msgEvtAddr;
272 if (_msgEvtAddr.
ip == 0)
return false;
277 id.addr = _msgEvtAddr;
279 #ifdef MSG_DONTACK_EVENTS
280 return msgTxDontAck(&
id, MCF_CLASS_EVENT, buf);
282 return msgTxRespond(&
id, MCF_CLASS_EVENT, buf);
289 return msgTxRespond(
id, MCF_CLASS_REPLY, buf);
297 errMsg = E_UNKNOWN_DESCR;
300 printf(
"MSG Error (type=%d, id=%d): %s (0x%08x)\n", id->
msgType, id->
msgId, errMsg, errCode);
304 if (errMsg == NULL) {
309 return msgTxRespond(
id, MCF_CLASS_ERROR, &db);
uint32_t mcfEncodeFinish(MCFState *state)
Call this after encoding to complete the MCF packet.
void msgGetEvtTarget(SockAddr *sockAddr)
Returns the event target address.
#define E_MSG_TYPE_UNKNOWN
Message unknown.
void _msgTx(SockAddr *addr, uint8_t *data, int len)
Stub function, invoked by RMT when sending a MCF packet.
#define E_MSG_FUNCTION
Message function returned false.
bool msgTxEvent(int msgType, DataBuffer *buf)
Invoked to send an event.
bool dbWriteI32(DataBuffer *buf, int32_t i)
Writes an 32 bits signed integer.
uint16_t port
Port number.
uint8_t * end
End Pointer.
bool mcfEncodeBuffer(MCFState *state, DataBuffer *db)
Encodes the content of a databuffer into the container.
#define DB_ERR_NONE
Indicates no error.
uint8_t * start
Start Pointer.
bool mcfEncodeNext(MCFState *state)
Creates a new encoding entry.
Defines a DataBuffer structure.
#define logWarn(MSG,...)
Format a log message with warning level.
uint16_t msgType
the message type
static uint32_t dbLength(DataBuffer *buf)
Returns the lenght of the current buffer.
Handles MCF packed messages from the higher protocol layer.
void msgSetEvtTarget(SockAddr *sockAddr)
Set the target for all events, or pass NULL to unset.
Combination of IP address and port.
#define MSG_COMMANDS
List containing all commands.
uint32_t mcfEncodeFreeNext()
Returns the number of bytes available for content in the NEXT message.
bool msgTxReply(MsgId *id, DataBuffer *buf)
Invoke to send a reply.
#define E_NOTIMPLEMENTED
Generic error: not implemented.
#define E_MSG_CLASS_UNSUPPORT
Message class received not supported.
DataBuffer reads and writes data into a buffer in the same format as defined in the data Java DataOut...
bool msgTxError(MsgId *id, int errCode, const char *errMsg, const char *name)
Invoke to send an error response.
void msgRx(SockAddr *addr, uint8_t *data, int len)
Receive a packet with data as a command.
bool msgRxBufCheck(DataBuffer *buf)
Checks the received buffer, and logs an error if there is something wrong.
#define MSG_EVENTS
List containing all events.
This module is responsible for distributing error codes.
Defines all remote commands.
#define LOG_DEF(NAME,...)
Define a logger for a module.
void mcfInitState(MCFState *state, uint8_t *bufPtr, uint32_t bufLen)
Initializes the decoding or encoding of a Message Container Formatted message.
SockAddr addr
the address of the sender
If defined, events will not require an acknowledge.
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 ...
#define DB_ERR_INVALID_BUFFER
Indicates an invalid buffer.
bool mcfDecodeNext(MCFState *state)
Decodes the next message.
Implements a generic logger facility.
#define DB_BUF_INIT(PTR, LEN)
Simple buffer initialization.
uint8_t * cur
Current Pointer.
uint16_t msgId
the message identifier
#define E_UNKNOWN
Generic error: Unknown error.
#define logInfo(MSG,...)
Write a log message with formatting on info level.