/*******************************************************************************
********************************************************************************
**
**  Copyright(c) 2022, Alliance for Automotive Innovation
**  Used only under license from the Alliance for Automotive Innovation. All Rights Reserved.
**
**  Project:  J1699-3
**  FileName: LogMsg.c
**  Author:   EnGenius
**  Date:     2/25/2022
**  Email:    <support@autosinnovate.org>
**
**  Purpose:  SAE J1699-3 Vehicle OBD II Compliance Test Cases Source Code.
**            This source code is intended to run the tests described in
**            the SAE J1699-3 document in an automated manner, when compiled
**            and used with an SAE J2534-compatible pass-thru device.
**
**  Description:
**
**  Modifications:  12/07/2022  Initial Version
**
********************************************************************************
*******************************************************************************/

#include <stdio.h>    // C Library input and output declarations
#include <stdlib.h>   // C Library general function declarations
#include <time.h>     // C Library time and date declarations
#include <windows.h>  // Windows API declarations
#include "j2534.h"    // j1699 project j2534 declarations
#include "j1699.h"    // j1699 project general declarations


/*******************************************************************************
**
**  Function:  LogMsg
**
**  Purpose:   Log Network messages to logfile
**
*******************************************************************************/
void LogMsg ( PASSTHRU_MSG  *pMsg,
              unsigned long  MsgFlags )
{
	char          LogBuffer[MAX_LOG_STRING_SIZE];
	unsigned long ByteIdx;
	unsigned long BufferIdx;


	BufferIdx = 0;

	if ( MsgFlags & LOG_REQ_MSG )
	{
		// For local messages, set ExtraDataIndex to message size.
		pMsg->ExtraDataIndex = pMsg->DataSize;
		BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
		                         "REQ MSG:  " );
	}
	else if ( pMsg->RxStatus & TX_MSG_TYPE )
	{
		BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
		                         "TX MSG: %10luusec ",
		                         pMsg->TimestampMsecs );
	}
	else
	{
		BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
		                         "RX MSG: %10luusec ",
		                         pMsg->TimestampMsecs );
	}


	switch ( pMsg->eProtocolID )
	{
		case J1850VPW:
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "J1850VPW " );
		}
		break;
		case J1850PWM:
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "J1850PWM " );
		}
		break;
		case ISO9141:
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "ISO9141 " );
		}
		break;
		case ISO14230:
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "ISO14230 " );
		}
		break;
		case ISO15765:
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "ISO15765 " );
		}
		break;
		case CAN:
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "CAN " );
		}
		break;
		default:
		{
			// Unsupported protocol
		}
		break;
	}

	if ( pMsg->eProtocolID == ISO15765 )
	{
		if ( pMsg->RxStatus & ISO15765_FIRST_FRAME )
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "FirstFrame Indication " );
		}
		else if ( pMsg->RxStatus & TX_DONE )
		{
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "Tx Done Indication " );
		}
	}
	if ( (pMsg->eProtocolID == ISO9141 ||
	      pMsg->eProtocolID == ISO14230) &&
	     (pMsg->RxStatus & START_OF_MESSAGE) )
	{
		BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
		                         "Start of Message " );
	}

	if ( pMsg->RxStatus & RX_BREAK )
	{
		BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
		                         "BREAK Indication " );
	}
	for ( ByteIdx = 0;
	      ByteIdx < pMsg->ExtraDataIndex &&
	      ByteIdx < pMsg->DataSize &&
	      ByteIdx < MAX_MESSAGE_LOG_SIZE;
	      ByteIdx++ )
	{
		BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
		                         "%02X ",
		                         pMsg->Data[ByteIdx] );
	}

	if ( pMsg->eProtocolID == ISO9141 ||
	     pMsg->eProtocolID == ISO14230 )
	{
		if ( pMsg->ExtraDataIndex < pMsg->DataSize )
		{
			// log ISO9141/ISO14230 checksum
			BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
			                         "(%02X) ",
			                         pMsg->Data[pMsg->ExtraDataIndex] );
		}
	}

	BufferIdx += sprintf_s ( &LogBuffer[BufferIdx], MAX_LOG_STRING_SIZE - BufferIdx,
	                         "\n" );

	Log ( NETWORK, SCREENOUTPUTOFF, LOGOUTPUTON, NO_PROMPT,
	      "%s",
	      LogBuffer );
}
