Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed-rtos mbed Xbus
Fork of MTi-1_example by
Diff: xbus/xbusparser.c
- Revision:
- 25:01356fb59467
- Parent:
- 23:8171449f0dc3
- Child:
- 46:f652d199d27e
diff -r 2cc49dc854e3 -r 01356fb59467 xbus/xbusparser.c
--- a/xbus/xbusparser.c Wed May 20 12:47:50 2015 +0200
+++ b/xbus/xbusparser.c Wed May 20 13:58:34 2015 +0200
@@ -18,14 +18,6 @@
#include "xbusutility.h"
#include <stdlib.h>
#include <string.h>
-#include <assert.h>
-
-/*!
- * \brief Max message length for parsed message types.
- * Unparsed types, e.g. MtData2 packets, will have buffers dynamically
- * requested, so are not constrained by this value.
- */
-#define XBUS_MAX_MESSAGE_LENGTH (64)
enum XbusParserState
{
@@ -43,7 +35,6 @@
{
struct XbusParserCallback callbacks;
struct XbusMessage currentMessage;
- uint8_t rxBuffer[XBUS_MAX_MESSAGE_LENGTH];
uint16_t payloadReceived;
uint8_t checksum;
enum XbusParserState state;
@@ -74,29 +65,17 @@
struct XbusParser* parser = (struct XbusParser*)parserMem;
parser->state = XBPS_Preamble;
parser->callbacks.allocateBuffer = callback->allocateBuffer;
+ parser->callbacks.deallocateBuffer = callback->deallocateBuffer;
parser->callbacks.handleMessage = callback->handleMessage;
return parser;
}
-static bool canParseMessagePayload(enum XsMessageId mid)
-{
- switch (mid)
- {
- case XMID_DeviceId:
- case XMID_OutputConfig:
- return true;
-
- default:
- return false;
- }
-}
-
-static void parseDeviceId(struct XbusParser* parser)
+static void parseDeviceId(struct XbusParser* parser, uint8_t const* rawData)
{
uint32_t* deviceId = parser->callbacks.allocateBuffer(sizeof(uint32_t));
if (deviceId)
{
- XbusUtility_readU32(deviceId, parser->rxBuffer);
+ XbusUtility_readU32(deviceId, rawData);
parser->currentMessage.data = deviceId;
parser->currentMessage.length = 1;
}
@@ -106,11 +85,10 @@
}
}
-static void parseOutputConfig(struct XbusParser* parser)
+static void parseOutputConfig(struct XbusParser* parser, uint8_t const* rawData)
{
uint8_t fields = parser->currentMessage.length / 4;
struct OutputConfiguration* conf = parser->callbacks.allocateBuffer(fields * sizeof(struct OutputConfiguration));
- uint8_t const* dptr = parser->rxBuffer;
if (conf)
{
parser->currentMessage.data = conf;
@@ -118,8 +96,8 @@
for (int i = 0; i < fields; ++i)
{
- dptr = XbusUtility_readU16((uint16_t*)&conf->dtype, dptr);
- dptr = XbusUtility_readU16(&conf->freq, dptr);
+ rawData = XbusUtility_readU16((uint16_t*)&conf->dtype, rawData);
+ rawData = XbusUtility_readU16(&conf->freq, rawData);
++conf;
}
}
@@ -131,39 +109,30 @@
static void parseMessagePayload(struct XbusParser* parser)
{
+ uint8_t const* const rawData = parser->currentMessage.data;
switch (parser->currentMessage.mid)
{
+ default:
+ // Leave parsing and memory management to user code
+ return;
+
case XMID_DeviceId:
- parseDeviceId(parser);
+ parseDeviceId(parser, rawData);
break;
case XMID_OutputConfig:
- parseOutputConfig(parser);
- break;
-
- default:
- assert(!canParseMessagePayload(parser->currentMessage.mid));
+ parseOutputConfig(parser, rawData);
break;
}
+
+ if (rawData)
+ parser->callbacks.deallocateBuffer(rawData);
}
void prepareForPayload(struct XbusParser* parser)
{
- parser->currentMessage.data = NULL;
parser->payloadReceived = 0;
-
- if (canParseMessagePayload(parser->currentMessage.mid))
- {
- assert(parser->currentMessage.length < XBUS_MAX_MESSAGE_LENGTH);
- if (parser->currentMessage.length < XBUS_MAX_MESSAGE_LENGTH)
- {
- parser->currentMessage.data = parser->rxBuffer;
- }
- }
- else
- {
- parser->currentMessage.data = parser->callbacks.allocateBuffer(parser->currentMessage.length);
- }
+ parser->currentMessage.data = parser->callbacks.allocateBuffer(parser->currentMessage.length);
}
void XbusParser_parseByte(struct XbusParser* parser, const uint8_t byte)
@@ -243,6 +212,10 @@
parseMessagePayload(parser);
parser->callbacks.handleMessage(&parser->currentMessage);
}
+ else if (parser->currentMessage.data)
+ {
+ parser->callbacks.deallocateBuffer(parser->currentMessage.data);
+ }
parser->state = XBPS_Preamble;
break;
}

Xsens MTi 1-series