/*
********************************************************************************
** SAE J1699-3 Test Source Code
**
**  Copyright (C) 2020 EnGenius. http://j1699-3.sourceforge.net/
**
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**
**  This program is free software; you can redistribute it and/or modify
**  it under the terms of the GNU General Public License as published by
**  the Free Software Foundation; either version 2 of the License, or
**  (at your option) any later version.
**
**  This program is distributed in the hope that it will be useful,
**  but WITHOUT ANY WARRANTY; without even the implied warranty of
**  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
**  GNU General Public License for more details.
**
**  You should have received a copy of the GNU General Public License
**  along with this program; if not, write to the Free Software
**  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
**
** ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
**
** This source code, when compiled and used with an SAE J2534-compatible pass
** thru device, is intended to run the tests described in the SAE J1699-3
** document in an automated manner.
**
** This computer program is based upon SAE Technical Report J1699,
** which is provided "AS IS"
**
** See j1699.c for details of how to build and run this test.
**
********************************************************************************
*/
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include "j2534.h"
#include "j1699.h"

extern STATUS ReadVIN (void);
extern STATUS PrintCALIDs (void);
extern STATUS PrintCVNs (void);
extern STATUS VerifyM01P1C (SID1 *pSid1, unsigned long SidIndex, unsigned long EcuIndex);

STATUS VehicleReport()
{
	SID_REQ       SidReq;
	unsigned long EcuIndex;
	SID1          *pSid1;




	/* Create the log file */
	ghLogFile = NULL;
	sprintf ( gLogFileName, "Vehicle Report.log" );

	/* Open the log file */
	ghLogFile = fopen (gLogFileName, "w+");
	if (ghLogFile == NULL)
	{
		Log( FAILURE, SCREENOUTPUTON, LOGOUTPUTOFF, NO_PROMPT,
		     "Cannot open log file %s\n", gLogFileName);
		StopTest(FAIL, TestPhase);
		exit (FAIL);
	}

	/* Application version, build date, OS, etc */
	LogVersionInformation (SCREENOUTPUTON, LOGOUTPUTON);

	/* Copy temp log file to actual log file */
	if (AppendLogFile () != PASS)
	{
		Log( FAILURE, SCREENOUTPUTON, LOGOUTPUTOFF, NO_PROMPT,
		     "Unable to copy temp log file to %s\n", gLogFileName);
		StopTest(FAIL, TestPhase);
		exit (FAIL);
	}
	
	/* Echo user responses to the log file */
	Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "%s%d\n", g_rgpcDisplayStrings[DSPSTR_PRMPT_MODEL_YEAR], gModelYear);

	Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "%s%d\n", g_rgpcDisplayStrings[DSPSTR_PRMPT_OBD_ECU], gUserNumEcus);

	Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "%s%d\n", g_rgpcDisplayStrings[DSPSTR_PRMPT_RPGM_ECU], gUserNumEcusReprgm);

	Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "%s%s\n", g_rgpcDisplayStrings[DSPSTR_PRMPT_ENG_TYPE], gEngineTypeStrings[gUserInput.eEngineType] );

	Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "%s%s\n", g_rgpcDisplayStrings[DSPSTR_PRMPT_PWRTRN_TYPE], gPwrTrnTypeStrings[gUserInput.ePwrTrnType] );

	Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "%s%s\n", g_rgpcDisplayStrings[DSPSTR_PRMPT_VEH_TYPE], gVehicleTypeStrings[gUserInput.eVehicleType] );

	Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "%s%s\n\n", g_rgpcDisplayStrings[DSPSTR_STMT_COMPLIANCE_TYPE],
	     gComplianceTestTypeStrings[gUserInput.eComplianceType]);


	// Determine Protocol
	TestSubsection = 1;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Determine Protocol");
	if ( DetermineProtocol() != PASS )
	{
		Log( FAILURE, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
		     "Protocol determination unsuccessful.\n" );
		return(FAIL);
	}
	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n");


	// get VIN
	TestSubsection = 2;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Read VIN");
	ReadVIN ();
	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n");

	// get CALID
	TestSubsection = 3;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Read CALIDs");
	PrintCALIDs();
	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n");

	// get CVNs
	TestSubsection = 4;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Read CVNs ");
	PrintCVNs();
	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n");

	// get Pending DTCs
	TestSubsection = 5;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Read Pending DTCs");
	VerifyDTCPendingData();
	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n");

	// get Stored DTCs
	TestSubsection = 6;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Read Stored DTCs");
	VerifyDTCStoredData();
	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n");

	// get Permanent DTCs
	TestSubsection = 7;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Read Permanent DTCs");
	VerifyPermanentCodeSupport();
	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n");

	// get SID 1 PID 1C
	TestSubsection = 8;
	Log( SUBSECTION_BEGIN, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
	     "Vehicle Report: Read SID $1  PID $1C");
	SidReq.SID    = 1;
	SidReq.NumIds = 1;
	SidReq.Ids[0] = 0x1c;
	if ( SidRequest( &SidReq, SID_REQ_NO_PERIODIC_DISABLE|SID_REQ_IGNORE_NO_RESPONSE ) == PASS )
	{
		for (EcuIndex = 0; EcuIndex < gUserNumEcus; EcuIndex++)
		{
			if (gOBDResponse[EcuIndex].Sid1PidSize > 0)
			{
				pSid1 = (SID1 *)&gOBDResponse[EcuIndex].Sid1Pid[0];

				if (pSid1->PID == 0x1c)
				{
					Log( INFORMATION, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT,
					     "SID $1 PID $1C = %d\n",pSid1->Data[0] );

					VerifyM01P1C( pSid1, 0, EcuIndex );
					break;
				}
			}
		}
	}


	Log( BLANK, SCREENOUTPUTON, LOGOUTPUTON, NO_PROMPT, "\n\n**** END OF VECHICLE REPORT ****\n");


	// End of report, disconnect
	DisconnectProtocol();
	gOBDDetermined = FALSE;

	// save Vehicle Report.log
	fclose (ghLogFile);

	// reset temp file to beginning
	fseek (ghTempLogFile, 0, SEEK_SET);

	return (PASS);
}