/** SGX

The   sss    gggg x   x   
     s      g      x x    Engine
      sss   g  gg   x
         s  g   g  x x          Copyright 1995-2008.
     ssss    gggg x   x         Steven Goodwin. All Rights Reserved.
    
This version has been released under the GNU Public License v2.0.

Please see the license files for details.

File: src/sgx/core/comms/commsport.cpp

*/

#include "sgx/core/core.hpp"

/**
Typical usage:

  CCommsPort *pPort = new CCommsPort(CCommPortSettings("com3", 9600));

  pPort->open();

  while(TRUE) {
	  pPort->update();
		if (pPort->isReady()) {
			pPort->read(...

 The update method is needed to bring data into the buffer. It is, alas, 
 blocking, so should be done in a separate thread.
*/
CCommsPort::CCommsPort(const CCommPortSettings &settings) 
:	m_Settings(settings)
 
{
	m_pDevice = NULL;

	m_pBuffer = new tBYTE[COMM_DEFAULT_BUFFER_SIZE];
	m_iBufferIdx = 0;
	m_iBufferSize = COMM_DEFAULT_BUFFER_SIZE;

}

CCommsPort::~CCommsPort() {
	delete[] m_pBuffer;
}

tBOOL CCommsPort::open() {
	if (isOpen()) {
		return TRUE;
	}

	m_pDevice = CSGXCommsSystem::openCommsPort(m_Settings);

	if (!m_pDevice) {
		return FALSE;
	}

	// Woo hoo!
	m_iBufferIdx = 0;
	//
	return TRUE;
}

tBOOL CCommsPort::isOpen() {
	return m_pDevice ? TRUE : FALSE;
}

void CCommsPort::update() {
	// If the port read method on your platform is blocking this 
	// should, ideally, run in a separate thread.
	if (isOpen()) {
		int spaceLeft = m_iBufferSize - m_iBufferIdx;

		m_iBufferIdx += m_pDevice->read(&m_pBuffer[m_iBufferIdx], spaceLeft);
	}
}

void CCommsPort::setBufferSize(tMEM_SIZE s) {
	tBYTE *tmp = new tBYTE[s];
	tMEM_SIZE cp = sgxMin<tMEM_SIZE>(s, m_iBufferSize);
	sgxMemcpy(tmp, m_pBuffer, cp);
	delete[] m_pBuffer;

	m_pBuffer = tmp;
	m_iBufferSize = s;
}


tBOOL CCommsPort::isReady() {
	return m_iBufferIdx > 0;
}

int CCommsPort::getReadySize() {
	return m_iBufferIdx;
}

tBOOL CCommsPort::write(const int i) {
	tBYTE p[] = { i>>24, (i>>16)&255, (i>>8)&255, i&255};
	tBOOL rt = write(p, 4);
	return rt;
}

tBOOL CCommsPort::write(const sgxString s) {
	return write((const tBYTE *)s.c_str(), sgxStrlen(s.c_str())+1);
}

tBOOL CCommsPort::write(const tBYTE *pData, const int size) {
	if (isOpen()) {
		unsigned long written;
		written = m_pDevice->write(pData, size);
		return written == size;
	}
	return FALSE;
}


tBOOL CCommsPort::read(tBYTE &c) {
	if (m_iBufferIdx < 1) {
		return FALSE;
	}

	c = m_pBuffer[0];
	sgxMemmove(&m_pBuffer[0], &m_pBuffer[1], m_iBufferSize-1);
	--m_iBufferIdx;
	return TRUE;
}

tBOOL CCommsPort::read(int &i) {
	tBYTE c1, c2, c3, c4;
	tBOOL r = read(c1) && read(c2) && read(c3) && read(c4);
	i = (c1<<24) | (c2<<16) | (c3<<8) | c4;
	return r;
}

tBOOL CCommsPort::read(sgxString &s) {
	s = "";
	tBYTE c;
	do {
		while(read(c)) {
			if (c == '\0' || c == '\n') {
				return TRUE;
			}
			s += c;
		}
		// WE've exhausted the data, try a new read
		update();
	} while(isReady());

	return FALSE;
}


tBOOL CCommsPort::read(tBYTE *pData, int maxSize) {
	tBYTE c;
	for(int i=0;i<maxSize;++i) {
		if (read(c) == FALSE) {
			return FALSE;
		}
		pData[i] = c;
	}
	return TRUE;
}


tBOOL CCommsPort::close() {
	
	if (isOpen()) {
		m_pDevice->close();
		m_pDevice = NULL;
		return TRUE;
	}
	
	return FALSE;
}
