/* ---------------------------------------------------------------------------
 * CommListeners.c - v0.1                                (c) 2008 Micro-key bv
 * ---------------------------------------------------------------------------
 * Micro-key bv
 * Industrieweg 28, 9804 TG Noordhorn
 * Postbus 92, 9800 AB Zuidhorn
 * The Netherlands
 * Tel: +31 594 503020
 * Fax: +31 594 505825
 * Email: support@microkey.nl  
 * Web: www.microkey.nl
 * ---------------------------------------------------------------------------
 * Description: 
 * ---------------------------------------------------------------------------
 * Version(s):  0.1, Feb 15, 2008, FSc
 *                   Creation.
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * System include files
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Application include files
 * ---------------------------------------------------------------------------
 */
#include "types.h"
#include "CommListeners.h"
#include "IspProtocol.h"
#include "serial.h"
#include "bus.h"
#include "crc.h"

/* ---------------------------------------------------------------------------
 * Local constant and macro definitions
 * ---------------------------------------------------------------------------
 */
#define ACKNOWLEDGE_MSGSIZE     6


/* ---------------------------------------------------------------------------
 * Global variable definitions
 * ---------------------------------------------------------------------------
 */

/* ---------------------------------------------------------------------------
 * Local variable definitions
 * ---------------------------------------------------------------------------
 */
int com1IspHandler;
int com2IspHandler;
int bus1IspHandler;
int bus2IspHandler;

/* ---------------------------------------------------------------------------
 * Local function definitions
 * ---------------------------------------------------------------------------
 */
void sendAckOnCom1(t_isp_responses response);
void sendAckOnCom2(t_isp_responses response);
void sendAckOnBus1(t_isp_responses response);
void sendAckOnBus2(t_isp_responses response);
UINT8 *createAckMessage(t_isp_responses response );

 /** \brief Initialises the COM1 listener for handling IspProtocol on COM1 */
void initCom1Listener()
{
	com1IspHandler = ispInitProtocol( sendAckOnCom1 );
}

/** \brief Initialises the COM2 listener for handling IspProtocol on COM2 */
void initCom2Listener()
{
	com2IspHandler = ispInitProtocol( sendAckOnCom2 );

}

/** \brief Initialises the BUS1 listener for handling IspProtocol on BUS1 */
void initBus1Listener()
{
	bus1IspHandler = ispInitProtocol( sendAckOnBus1 );
}

/** \brief Initialises the BUS2 listener for handling IspProtocol on BUS2 */
void initBus2Listener()
{
	bus2IspHandler = ispInitProtocol( sendAckOnBus2 );
}

	
/** \brief Does IspProtocol handling for fresh received bytes on COM1 */
void listen2Com1()
{
	UINT8 byte;
	if (serGet( COM1, &byte) == TRUE)
	{
		ispHandleRxByte( com1IspHandler, byte );
	}
}

/** \brief Does IspProtocol handling for fresh received bytes on COM2 */
void listen2Com2()
{
	UINT8 byte;
	static int nrReceivedBytes = 0;;
	if (serGet( COM2, &byte) == TRUE)
	{
		nrReceivedBytes++;
		ispHandleRxByte( com2IspHandler, byte );
	}
}

/** \brief Does IspProtocol handling for fresh received bytes on BUS1 */
void listen2Bus1()
{
	UINT8 byte; 
	if (busGet( BUS1, &byte) == TRUE)
	{
		ispHandleRxByte( bus1IspHandler, byte );
	}

}

/** \brief Does IspProtocol handling for fresh received bytes on BUS2 */
void listen2Bus2()
{
	UINT8 byte;
	if (busGet( BUS2, &byte) == TRUE)
	{
		ispHandleRxByte( bus2IspHandler, byte );
	}
}

void sendAckOnCom1(t_isp_responses response)
{
	UINT8 *message = createAckMessage( response );
	
	// Send over COM1
	serWrite(COM1, ACKNOWLEDGE_MSGSIZE, message);
}

void sendAckOnCom2(t_isp_responses response)
{
	UINT8 *message = createAckMessage( response );
	
	// Send over COM2
	serWrite(COM2, ACKNOWLEDGE_MSGSIZE, message);
}

void sendAckOnBus1(t_isp_responses response)
{
	UINT8 *message = createAckMessage( response );
	
	// Send over BUS1
	busWrite(BUS1, ACKNOWLEDGE_MSGSIZE, message);
}

void sendAckOnBus2(t_isp_responses response)
{
	UINT8 *message = createAckMessage( response );
	
	// Send over BUS2
	busWrite(BUS2, ACKNOWLEDGE_MSGSIZE, message);
}

// 
UINT8 *createAckMessage(t_isp_responses response )
{
	static UINT8 AcknowledgeMsg[ACKNOWLEDGE_MSGSIZE];
	UINT16 msgCrc;
	
	// Assemble message
	AcknowledgeMsg[0] = ISP_START_BYTE;
	AcknowledgeMsg[1] = ISP_MSGID_ACKNOWLEDGE;
	AcknowledgeMsg[2] = 1;
	AcknowledgeMsg[3] = (UINT8)response;
	
	// Calculate CRC
	msgCrc = crcCalc( &(AcknowledgeMsg[1]), 3, 0 );
	
	AcknowledgeMsg[4] = (UINT8)(msgCrc >> 8);
	AcknowledgeMsg[5] = (UINT8)(msgCrc & 0x00FF);
	
	return AcknowledgeMsg;
}
