MME2 changes - Propped commits from openmme/paging branch. Added scripts
for code gen

Change-Id: Ie55032217232214ac8544ca76ea34335205329e4
diff --git a/src/cmn/Makefile b/src/cmn/Makefile
new file mode 100644
index 0000000..69bb798
--- /dev/null
+++ b/src/cmn/Makefile
@@ -0,0 +1,73 @@
+#
+# Copyright (c) 2019, Infosys Ltd.
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+include ../../Makefile.common
+
+CC := g++
+CFLAGS += -std=c++11 
+
+ifeq ($(DEBUG),true)
+        CFLAGS += -g
+endif
+ifeq ($(DEBUG),false)
+        CFLAGS += -O3
+endif
+
+IPC_LIBNAME = $(LIBDIR)/libipcfwk.so
+DATAGROUPMGR_LIBNAME = $(LIBDIR)/libdatagroupmgr.so
+CMNUTILS_LIBNAME = $(LIBDIR)/libcmnUtils.so
+
+IPC_SRCS := ./ipcChannel.cpp  \
+            ./tipcSocket.cpp
+DATAGROUPMGR_SRCS := ./dataGroupManager.cpp
+CMNUTILS_SRCS := ./debug.cpp \
+		./msgBuffer.cpp
+
+SRCDIR := .
+SRCEXT := cpp
+IPC_OBJS := $(patsubst $(SRCDIR)/%,$(OBJDIR)/cmn/%,$(IPC_SRCS:.$(SRCEXT)=.o))
+DATAGROUPMGR_OBJS := $(patsubst $(SRCDIR)/%,$(OBJDIR)/cmn/%,$(DATAGROUPMGR_SRCS:.$(SRCEXT)=.o))
+CMNUTILS_OBJS := $(patsubst $(SRCDIR)/%,$(OBJDIR)/cmn/%,$(CMNUTILS_SRCS:.$(SRCEXT)=.o))
+
+
+OBJS := $(IPC_OBJS) \
+	$(DATAGROUPMGR_OBJS)\
+	$(CMNUTILS_OBJS)
+
+all : buildCommonLIBS
+
+buildCommonLIBS : $(OBJS)
+	@echo "Linking"
+	@mkdir -p $(LIBDIR)
+	$(CC) $(CFLAGS) $(IPC_OBJS) -shared -o $(IPC_LIBNAME)
+	$(CC) $(CFLAGS) $(DATAGROUPMGR_OBJS) -shared -o $(DATAGROUPMGR_LIBNAME)
+	$(CC) $(CFLAGS) $(CMNUTILS_OBJS) -shared -o $(CMNUTILS_LIBNAME)
+
+$(OBJS) : $(OBJDIR)/cmn/%.o : $(SRCDIR)/%.cpp
+	@mkdir -p $(OBJDIR)/cmn
+	$(CC) $(CFLAGS) $(INC_DIRS) -fPIC -c -o $@ $<
+
+install:all
+	-@mkdir -p $(TARGET_DIR)/lib
+	-@cp $(IPC_LIBNAME) $(TARGET_DIR)/lib
+	-@cp $(DATAGROUPMGR_LIBNAME) $(TARGET_DIR)/lib
+	-@cp $(CMNUTILS_LIBNAME) $(TARGET_DIR)/lib
+
+clean:
+	@echo " Cleaning...";
+	-@rm -rf $(OBJDIR)/cmn
+	-@rm -f $(IPC_LIBNAME) $(DATAGROUPMGR_LIBNAME) $(CMNUTILS_LIBNAME)
+
diff --git a/src/cmn/dataGroupManager.cpp b/src/cmn/dataGroupManager.cpp
new file mode 100644
index 0000000..9799c8a
--- /dev/null
+++ b/src/cmn/dataGroupManager.cpp
@@ -0,0 +1,74 @@
+/*
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "dataGroupManager.h"
+#include "memPoolManager.h"
+
+namespace cmn {
+namespace DGM
+{
+	DataGroupManager::DataGroupManager():cbstore_m(NULL)
+	{
+
+	}
+	
+	DataGroupManager::~DataGroupManager()
+	{
+		delete[] cbstore_m;
+	}
+	
+	void DataGroupManager::initializeCBStore( int DataCount )
+	{
+		cbstore_m = new ControlBlock[DataCount];
+		for( int idx = 0; idx < DataCount; idx ++ )
+		{
+			freeQ_m.push_back( &cbstore_m[idx] );
+		}
+	}
+	
+	ControlBlock* DataGroupManager::allocateCB()
+	{
+		std::lock_guard<std::mutex> lock(mutex_m);
+
+		ControlBlock* cbp = freeQ_m.front();
+		freeQ_m.pop_front();
+
+		if (cbp != NULL)
+		{
+		    cbp->setControlBlockState(ALLOCATED);
+		}
+
+		return cbp;
+	}
+
+	ControlBlock* DataGroupManager::findControlBlock(uint32_t cbIndex)
+	{
+		ControlBlock* cbp = &cbstore_m[cbIndex - 1];
+		if (cbp != NULL && cbp->getControlBlockState() != FREE)
+		    return cbp;
+		else
+		    return NULL;
+	}
+	
+	void DataGroupManager::deAllocateCB( uint32_t cbIndex )
+	{
+        cbstore_m[cbIndex - 1].reset();
+
+		std::lock_guard<std::mutex> lock(mutex_m);
+		freeQ_m.push_back( &cbstore_m[cbIndex - 1]);
+	}
+};
+};
diff --git a/src/cmn/debug.cpp b/src/cmn/debug.cpp
new file mode 100644
index 0000000..b574468
--- /dev/null
+++ b/src/cmn/debug.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <debug.h>
+
+using namespace cmn::utils;
+
+Debug::Debug()
+{
+  indent = 0;
+  newLine = true;
+}
+
+Debug::~Debug()
+{
+  //TODO
+}
+
+void Debug::printDebugStream()
+{
+  fprintf(stderr, "%s" , stream.str().c_str());
+}
+
+void Debug::clearStream()
+{
+  stream.str("");
+}
+
+void Debug::printDebugStreamToFile()
+{
+  // TODO 
+}
+
+void Debug::add(char* data)
+{
+  if (newLine)
+     startNewLine();
+  stream << data;
+}
+
+void Debug::add(uint8_t data)
+{
+  if (newLine)
+     startNewLine();
+  stream << (uint16_t)data;
+}
+
+void Debug::add(uint16_t data)
+{
+  if (newLine)
+     startNewLine();
+  stream << data;
+}
+
+void Debug::add(uint32_t data)
+{
+  if (newLine)
+     startNewLine();
+  stream << data;
+}
+
+void Debug::add(uint64_t data)
+{
+  if (newLine)
+     startNewLine();
+  stream << data;
+}
+
+
+void Debug::endOfLine()
+{
+  stream <<"\n";
+  newLine = true;
+}
+
+void Debug::startNewLine()
+{
+  uint8_t i;
+  for (i = 0; i<= indent; i++)
+  {
+    stream <<"  ";
+  }
+  newLine = false;
+}
+
+void Debug::incrIndent()
+{
+  indent++;
+}
+
+void Debug::decrIndent()
+{
+  if (indent > 0)
+    indent --;
+}
+
+void Debug::setHexOutput()
+{
+  stream.setf(ios::hex, ios::basefield);
+}
+
+void Debug::unsetHexOutput()
+{
+  stream.unsetf(ios::hex);
+}
+
diff --git a/src/cmn/ipcChannel.cpp b/src/cmn/ipcChannel.cpp
new file mode 100644
index 0000000..7e5071d
--- /dev/null
+++ b/src/cmn/ipcChannel.cpp
@@ -0,0 +1,56 @@
+/*
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "../../include/cmn/ipcChannel.h"
+
+namespace cmn {
+namespace ipc {
+
+IpcChannel::IpcChannel(IpcChannelType ipcType):
+		ipcType_m(ipcType),
+		ipcHdl_m()
+{
+
+}
+
+IpcChannel::~IpcChannel()
+{
+
+}
+
+IpcChannelType IpcChannel::getIpcChannelType() const
+{
+	return ipcType_m;
+}
+
+void IpcChannel::setIpcChannelType(const IpcChannelType ipcType)
+{
+	ipcType_m = ipcType;
+}
+
+IpcChannelHdl IpcChannel::getIpcChannelHdl() const
+{
+	return ipcHdl_m;
+}
+
+void IpcChannel::setIpcChannelHdl(const IpcChannelHdl ipcHdl, const IpcChannelType ipcType)
+{
+	if (ipcType == IpcChannelType::tipc_c)
+		ipcHdl_m.u32 = ipcHdl.u32;
+}
+
+} /* namespace ipc */
+} /* namespace cmn */
diff --git a/src/cmn/msgBuffer.cpp b/src/cmn/msgBuffer.cpp
new file mode 100644
index 0000000..2255b37
--- /dev/null
+++ b/src/cmn/msgBuffer.cpp
@@ -0,0 +1,378 @@
+/*
+ * msgBuffer.cpp
+ *
+ *  Created on: Dec 2012
+ *      Author: hariharanb
+ */
+
+#include <arpa/inet.h>
+#include <stdio.h>
+#include <string.h>
+#include <basicTypes.h>
+#include <msgBuffer.h>
+
+using namespace std;
+
+using namespace cmn::utils;
+
+MsgBuffer::MsgBuffer()
+{
+	initialize(DEFAULT_BUFF_SIZE);
+}
+
+MsgBuffer::MsgBuffer(uint16_t size)
+{
+	initialize(size);
+}
+
+MsgBuffer::~MsgBuffer()
+{
+	delete data_mp;
+}
+
+void MsgBuffer::initialize(uint16_t size)
+{
+	bufSize = size;
+	data_mp = new uint8_t[size];
+	memset(data_mp, 0, size);
+	bitIndex = 0;
+	byteIndex = 0;
+	length = 0;
+	bitLength = 0;
+}
+
+bool MsgBuffer::writeBits(uint8_t data, uint8_t size, bool append)
+{
+
+    uint8_t mask = (0xFF >> (8 - size));
+    data = data & mask;
+
+	if ((bitIndex + size) <= 8)
+	{
+		if (append)
+		{
+			goToEnd();
+		}
+
+		data_mp[byteIndex] = data_mp[byteIndex] | (data << (8-(bitIndex+size)));
+		incrBitIndex(size);
+
+		return true;
+
+	}
+	else
+	{
+		// Attempt to write beyond byte boundary
+		return false;
+	}
+}
+
+bool MsgBuffer::writeBytes(uint8_t* data, uint16_t size, bool append)
+{
+	bool rc = false;
+	if (bitIndex == 0)
+	{
+		if (append)
+		{
+			goToEnd();
+		}
+
+		if ((byteIndex + size) <= bufSize)
+		{
+			memcpy(&data_mp[byteIndex], data, size);
+			rc = incrByteIndex(size);
+			if (byteIndex > length)
+			{
+				length = byteIndex;
+			}
+		}
+	}
+	return rc;
+}
+
+bool MsgBuffer::writeUint8(uint8_t data,  bool append)
+{
+	return writeBytes(&data, sizeof(uint8_t), append);
+}
+
+bool MsgBuffer::writeUint16(uint16_t data,  bool append)
+{
+	uint16_t localData = htons(data);
+	return writeBytes((uint8_t*)&localData, sizeof(uint16_t), append);
+}
+
+bool MsgBuffer::writeUint32(uint32_t data,  bool append)
+{
+	uint32_t localData = htonl(data);
+	return writeBytes((uint8_t*)&localData, sizeof(uint32_t), append);
+}
+
+bool MsgBuffer::writeUint64(uint64_t data, bool append)
+{
+	uint64_t localData = htonl(data);
+	return writeBytes((uint8_t*)&localData, sizeof(uint64_t), append);
+}
+
+bool MsgBuffer::readBit()
+{
+	uint8_t byteValue = data_mp[byteIndex];
+	uint8_t bitMask = 0x80;
+	bitMask = bitMask >> bitIndex;
+
+	// Adjust the indices
+	incrBitIndex(1);
+
+	if (byteValue & bitMask)
+	{
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+void MsgBuffer::readUint8(uint8_t &data)
+{
+	data = data_mp[byteIndex];
+	nextByte();
+}
+
+bool MsgBuffer::readUint16(uint16_t &data)
+{
+	if ((byteIndex + sizeof(uint16_t)) <= length)
+	{
+		memcpy(&data, &data_mp[byteIndex], sizeof(uint16_t));
+		data = ntohs(data);
+		incrByteIndex(sizeof(uint16_t));
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+bool MsgBuffer::readUint32(uint32_t &data)
+{
+	if ((byteIndex + sizeof(uint32_t)) <= length)
+	{
+		memcpy(&data, &data_mp[byteIndex], sizeof(uint32_t));
+		data = ntohl(data);
+		incrByteIndex(sizeof(uint32_t));
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+bool MsgBuffer::readUint64(uint64_t &data)
+{
+	if ((byteIndex + sizeof(uint64_t)) <= length)
+	{
+		memcpy(&data, &data_mp[byteIndex], sizeof(uint64_t));
+		data = ntohl(data);
+		incrByteIndex(sizeof(uint64_t));
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+uint8_t MsgBuffer::readBits(uint16_t size)
+{
+	uint8_t data = 0;
+
+	if ((bitIndex + size) > 8)
+	{
+		cout << "Attempt to read beyond byte boundary";
+		return 0;
+	}
+
+	uint16_t i;
+	for (i = 0; i <size; i++)
+	{
+		data = data << 1;
+		if (readBit())
+		{
+			data = data | 0x0001;
+		}
+	}
+	return data;
+}
+
+
+bool MsgBuffer::readBytes(uint8_t* data, uint16_t size)
+{
+	if ((byteIndex + size) <= length)
+	{
+		memcpy(data, &data_mp[byteIndex], size);
+		byteIndex += size;
+		bitIndex   = 0;
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+
+
+}
+
+void MsgBuffer::rewind()
+{
+	bitIndex = 0;
+	byteIndex = 0;
+}
+
+void MsgBuffer::reset()
+{
+	memset(data_mp, 0, length);
+	rewind();
+    length = 0;
+    bitLength = 0;
+}
+
+void MsgBuffer::skipBits(uint8_t size)
+{
+	incrBitIndex(size);
+}
+
+void MsgBuffer::skipBytes(uint16_t size)
+{
+	incrByteIndex(size);
+}
+
+void MsgBuffer::display(Debug &stream)
+{
+
+}
+
+bool MsgBuffer::incrBitIndex(uint8_t size)
+{
+    bool rc = false;
+    if ((bitIndex + size) <= 8)
+    {
+    	uint16_t savedBitIndex = bitIndex;
+    	bitIndex += size;
+        rc = true;
+    	if (bitIndex == 8)
+    	{
+    	  rc = incrByteIndex(1);
+          bitIndex = 0;
+    	}
+
+    	if (!rc)
+    	{
+    		bitIndex = savedBitIndex;
+    	}
+    }
+    // adjust the length now
+    if ((byteIndex == length)&&(bitIndex > bitLength))
+    {
+      bitLength = bitIndex;
+    }
+    else if (byteIndex > length)
+    {
+      length = byteIndex;
+      bitLength = bitIndex;
+    }
+    return rc;
+}
+
+bool MsgBuffer::incrByteIndex(uint16_t size)
+{
+	if ((byteIndex + size) <= bufSize)
+	{
+		byteIndex += size;
+		return true;
+	}
+	else
+	{
+		return false;
+	}
+}
+
+void MsgBuffer::nextByte()
+{
+	byteIndex++;
+	if (byteIndex > length)
+	{
+		byteIndex = 0;
+	}
+	bitIndex = 0;
+}
+
+void MsgBuffer::goToEnd()
+{
+	byteIndex = length;
+	bitIndex = bitLength;
+}
+
+uint16_t MsgBuffer::getLength()
+{
+       if (bitIndex == 0)
+           return length;
+       else
+           return (length + 1);
+}
+
+uint16_t MsgBuffer::getBufferSize()
+{
+  return bufSize;
+}
+
+uint16_t MsgBuffer::getCurrentIndex()
+{
+       return byteIndex;
+}
+
+void MsgBuffer::goToIndex(uint16_t idx)
+{
+       byteIndex = idx;
+}
+
+uint16_t MsgBuffer::lengthLeft()
+{
+  return (length - byteIndex);
+}
+
+uint16_t MsgBuffer::sizeLeft()
+{
+  return (bufSize - byteIndex);
+}
+
+void* MsgBuffer::getDataPointer() 
+{
+  return data_mp;
+}
+
+void MsgBuffer::setLength(uint16_t bufLen)
+{
+  length = bufLen;
+}
+
+uint8_t MsgBuffer::charToHex (uint8_t x)
+{
+  if ((x >= '0') && (x <= '9'))
+  {
+    return (x - '0');
+  }
+  else if ((x >= 'a') && (x <= 'f'))
+  {
+    return (x - 'a' + 10);
+  }
+  else if ((x >= 'A') && (x <= 'F'))
+  {
+    return (x - 'A' + 10);
+  }
+  else
+  {
+    return 0;
+  }
+}
+
diff --git a/src/cmn/tipcSocket.cpp b/src/cmn/tipcSocket.cpp
new file mode 100644
index 0000000..044dcd2
--- /dev/null
+++ b/src/cmn/tipcSocket.cpp
@@ -0,0 +1,119 @@
+/*
+ * Copyright (c) 2019, Infosys Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "../../include/cmn/tipcSocket.h"
+#include "../../include/cmn/tipcTypes.h"
+#include "../../include/common/log.h"
+#include <sys/types.h>
+#include <sys/socket.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <unistd.h>
+#include <linux/tipc.h>
+
+#define TIPC_SERVICE_ADDR       2
+
+namespace cmn {
+namespace ipc {
+
+TipcSocket::TipcSocket():IpcChannel(IpcChannelType::tipc_c)
+{
+	initialize();
+}
+
+TipcSocket::~TipcSocket()
+{
+	if (ipcHdl_m.u32 > 0)
+		close(ipcHdl_m.u32);
+}
+
+void TipcSocket::initialize()
+{
+	ipcHdl_m.u32 = socket(AF_TIPC, SOCK_RDM, 0);
+    if (ipcHdl_m.u32 <= 0)
+        log_msg(LOG_ERROR, "Failed to create TIPC socket. error: %s", strerror(errno));
+
+    return;
+}
+
+bool TipcSocket::bindTipcSocket(IpcAddress myAddress)
+{
+	if (ipcHdl_m.u32 <= 0)
+	{
+		log_msg(LOG_ERROR, "Invalid socket fd");
+		return false;
+	}
+
+    struct sockaddr_tipc server;
+
+    server.family = AF_TIPC;
+    server.addrtype = TIPC_SERVICE_ADDR;
+    server.scope = TIPC_CLUSTER_SCOPE;
+    server.addr.name.name.type = tipcServiceAddressType_c;
+    server.addr.name.name.instance = myAddress.u32;
+
+    bool rc = true;
+
+    if (0 != bind(ipcHdl_m.u32, (sockaddr*)&server, sizeof(server)))
+    {
+    	log_msg(LOG_ERROR, "Failed to bind TIPC socket. error: %s", strerror(errno));
+        rc = false;
+    }
+    return rc;
+}
+
+void TipcSocket::sendMsgTo(void * buffer, uint32_t len, IpcAddress destAddress)
+{
+    struct sockaddr_tipc dest;
+    dest.family = AF_TIPC;
+    dest.addrtype = TIPC_SERVICE_ADDR;
+    dest.scope = TIPC_CLUSTER_SCOPE;
+    dest.addr.name.domain = 0;
+    dest.addr.name.name.type = tipcServiceAddressType_c;
+    dest.addr.name.name.instance = destAddress.u32;
+
+
+   if (0 > sendto(ipcHdl_m.u32, buffer, len, 0, (sockaddr*)&dest, sizeof(dest)))
+   {
+	   log_msg(LOG_ERROR, "Failed to send message via TIPC socket. error: %s", strerror(errno));
+   }
+   else
+   {
+	   log_msg(LOG_INFO, "Message sent successfully");
+   }
+}
+
+
+uint32_t TipcSocket::recvMsgFrom(void * buffer, uint32_t len, IpcAddress& srcAddr)
+{
+    uint32_t bytesRead = 0;
+
+    struct sockaddr_tipc client;
+    socklen_t alen = sizeof(client);
+
+    if ((bytesRead = recvfrom(ipcHdl_m.u32, buffer, len, 0,
+			    (struct sockaddr *)&client, &alen)) > 0) 
+    {
+    	srcAddr.u32 = client.addr.name.name.instance;
+    }
+
+    return bytesRead;
+}
+
+} /* namespace ipc */
+} /* namespace cmn */