Add LPC1768

Dependencies:   mbed-rtos mbed Xbus

Fork of MTi-1_example by Xsens

Revision:
31:ce1ea9ae861e
Parent:
29:d9310e7b58b5
Child:
32:fafe0f42d82b
--- a/main.cpp	Wed May 20 17:04:45 2015 +0200
+++ b/main.cpp	Thu May 21 11:43:11 2015 +0200
@@ -46,12 +46,33 @@
 	return ev.status == osEventMessage ? (XbusMessage*)ev.value.p : NULL;
 }
 
-static void freeMessage(XbusMessage const* m)
+/*!
+ * \brief RAII object to manage message memory deallocation.
+ *
+ * Will automatically free the memory used by a XbusMessage when going out
+ * of scope.
+ */
+class XbusMessageMemoryManager
 {
-	if (m->data)
-		deallocateMessageData(m->data);
-	g_messagePool.free((XbusMessage*)m);
-}
+	public:
+		XbusMessageMemoryManager(XbusMessage const* message)
+			: m_message(message)
+		{
+		}
+
+		~XbusMessageMemoryManager()
+		{
+			if (m_message)
+			{
+				if (m_message->data)
+					deallocateMessageData(m_message->data);
+				g_messagePool.free(const_cast<XbusMessage*>(m_message));
+			}
+		}
+
+	private:
+		XbusMessage const* m_message;
+};
 
 static void dumpResponse(XbusMessage const* response)
 {
@@ -91,11 +112,11 @@
 {
 	XbusMessage m = {cmdId};
 	XbusMessage const* response = doTransaction(&m);
+	XbusMessageMemoryManager janitor(response);
 
 	if (response)
 	{
 		dumpResponse(response);
-		freeMessage(response);
 	}
 	else
 	{
@@ -176,6 +197,7 @@
 {
 	XbusMessage reqDid = {XMID_ReqDid};
 	XbusMessage const* didRsp = doTransaction(&reqDid);
+	XbusMessageMemoryManager janitor(didRsp);
 	uint32_t deviceId = 0;
 	if (didRsp)
 	{
@@ -183,7 +205,6 @@
 		{
 			deviceId = *(uint32_t*)didRsp->data;
 		}
-		freeMessage(didRsp);
 	}
 	return deviceId;
 }
@@ -220,6 +241,7 @@
 
 		XbusMessage outputConfMsg = {XMID_SetOutputConfig, 5, &conf};
 		XbusMessage const* outputConfRsp = doTransaction(&outputConfMsg);
+		XbusMessageMemoryManager janitor(outputConfRsp);
 		if (outputConfRsp)
 		{
 			if (outputConfRsp->mid == XMID_OutputConfig)
@@ -236,7 +258,6 @@
 			{
 				dumpResponse(outputConfRsp);
 			}
-			freeMessage(outputConfRsp);
 		}
 		else
 		{