rikbeuncode

Dependencies:   mbed-rtos mbed Xbus

Fork of MTi-1_example by Xsens

Files at this revision

API Documentation at this revision

Comitter:
Alex Young
Date:
Wed May 20 13:58:34 2015 +0200
Parent:
24:2cc49dc854e3
Child:
26:665d3624f9ab
Commit message:
Use rtos memory pool for message reception

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
xbus/xbusparser.c Show annotated file Show diff for this revision Revisions of this file
xbus/xbusparser.h Show annotated file Show diff for this revision Revisions of this file
--- a/main.cpp	Wed May 20 12:47:50 2015 +0200
+++ b/main.cpp	Wed May 20 13:58:34 2015 +0200
@@ -1,15 +1,26 @@
 #include "mbed.h"
+#include "rtos.h"
 #include "xbusparser.h"
 #include "xbusmessage.h"
 
+#define MEMORY_POOL_SIZE (4)
+#define MAX_XBUS_DATA_SIZE (128)
+
 static Serial pc(PA_2, PA_3);
 static Serial mt(PB_9, PB_8);
 static XbusParser* xbusParser;
-static uint8_t rxBuffer[256];
+
+MemoryPool<XbusMessage, MEMORY_POOL_SIZE> g_messagePool;
+MemoryPool<uint8_t[MAX_XBUS_DATA_SIZE], MEMORY_POOL_SIZE> g_messageDataPool;
 
-static void* allocateBuffer(size_t bufSize)
+static void* allocateMessageData(size_t bufSize)
 {
-	return bufSize < sizeof(rxBuffer) ? rxBuffer : NULL;
+	return bufSize < MAX_XBUS_DATA_SIZE ? g_messageDataPool.alloc() : NULL;
+}
+
+static void deallocateMessageData(void const* buffer)
+{
+	g_messageDataPool.free((uint8_t(*)[MAX_XBUS_DATA_SIZE])buffer);
 }
 
 static void mtLowLevelHandler(void)
@@ -107,6 +118,10 @@
 	{
 		pc.printf("Received Xbus message. MID=%X, length=%d\n", message->mid, message->length);
 	}
+	if (message->data)
+	{
+		deallocateMessageData(message->data);
+	}
 }
 
 static void configureSerialPorts(void)
@@ -123,7 +138,8 @@
 int main(void)
 {
 	XbusParserCallback xbusCallback = {};
-	xbusCallback.allocateBuffer = allocateBuffer;
+	xbusCallback.allocateBuffer = allocateMessageData;
+	xbusCallback.deallocateBuffer = deallocateMessageData;
 	xbusCallback.handleMessage = mtMessageHandler;
 
 	xbusParser = XbusParser_create(&xbusCallback);
--- 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;
 	}
--- a/xbus/xbusparser.h	Wed May 20 12:47:50 2015 +0200
+++ b/xbus/xbusparser.h	Wed May 20 13:58:34 2015 +0200
@@ -40,6 +40,12 @@
 	void* (*allocateBuffer)(size_t bufSize);
 
 	/*!
+	 * \brief Deallocate a buffer that was previously allocated by a call to
+	 * allocateBuffer.
+	 */
+	void (*deallocateBuffer)(void const* buffer);
+
+	/*!
 	 * \brief Handle a received message.
 	 */
 	void (*handleMessage)(struct XbusMessage const* message);