// PythonDemo.cpp : Defines the entry point for the application.
//


#include "stdafx.h"
#include "windowsx.h"
#include "resource.h"
#include "stdio.h"
#include "PythonDemo.h"
#include "j2534.h"

//declare variables
HWND dialoghandle, hStatus;
unsigned long ulChannelID, ulMsgID;
HINSTANCE hDLL;

void WriteLog(char *);
LRESULT CALLBACK MainDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam);
int LoadFunctions();


int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
	if(LoadFunctions()==1)
	{	
		DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, (DLGPROC)MainDlg); 	
	}
	FreeLibrary(hDLL); 

	return 0;
}

void WriteLog(char *msg)
{
	ListBox_AddString(hStatus, msg);
	ListBox_SetCurSel(hStatus, ListBox_GetCount((HWND)hStatus)-1);
	UpdateWindow(hStatus);
}

LRESULT CALLBACK MainDlg(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	unsigned char data[4128];
	unsigned long RetVal, NumMsgs, RcvNumMsgs;
	PASSTHRU_MSG pMsg;
	PASSTHRU_MSG MaskMsg;
	PASSTHRU_MSG PatternMsg;
	char RcvData[16];
	char buf[1024];
	int i, j;

	switch (message)
	{
		case WM_INITDIALOG:
			dialoghandle=hDlg;
			hStatus=GetDlgItem(hDlg, IDC_STATUS);
			if(hStatus == INVALID_HANDLE_VALUE)
				return FALSE;
			return TRUE;
		case WM_COMMAND:
			switch (LOWORD(wParam))
			{
				case IDC_CONNECT:
				/* Connect to protocol */
				RetVal = PassThruConnect(J1850VPW, 0, &ulChannelID);
				if (RetVal != STATUS_NOERROR)
				{
					WriteLog("PassThruConnect Failed!");
					return (0);
				}
				else
					WriteLog("Connect Successful!");

				/* Setup pass filter to read only OBD requests / responses */
				MaskMsg.ProtocolID = J1850VPW;
				MaskMsg.TxFlags = 0;
				MaskMsg.DataSize = 2;
				MaskMsg.Data[0] = 0x00;
				MaskMsg.Data[1] = 0xFE;
				PatternMsg.ProtocolID = J1850VPW;
				PatternMsg.TxFlags = 0;
				PatternMsg.DataSize = 2;
				PatternMsg.Data[0] = 0x00;
				PatternMsg.Data[1] = 0x6A;
				RetVal = PassThruStartMsgFilter(ulChannelID, PASS_FILTER, &MaskMsg, &PatternMsg, NULL, &ulMsgID);
				if (RetVal != STATUS_NOERROR)
					return (0);
													
				//Send request for MID 01 and PID 00
				Sleep(100);
				NumMsgs = 1;
				sprintf((char*)data, "\x68\x6A\xF1\x01\x00");
				memcpy(pMsg.Data, data, 5);
				pMsg.DataSize=5;
				pMsg.ProtocolID=J1850VPW;

				RetVal= PassThruWriteMsgs(ulChannelID, &pMsg, &NumMsgs, 1000);
				if (RetVal != STATUS_NOERROR)
				{
					return (0);
				}
				else
					WriteLog("Sent request for MID 01 PID 00!");
				
				//Received response with MID 01 and PID 00
				Sleep(100);
				while(1)
				{
					RcvNumMsgs = 1;
					RetVal= PassThruReadMsgs(ulChannelID, &pMsg, &RcvNumMsgs, 1000);
					if (RetVal != STATUS_NOERROR)
					{
						break;
					}
					else
						WriteLog("Received response for MID 01 PID 00!");
					
					memcpy(RcvData, pMsg.Data, 9);
					
					sprintf(buf, "Module $%02X PIDs supported ($00-$20): ", RcvData[2]);
								
					for (i=0; i<4; i++)
					{
						for (j=0; j<8; j++)
						{
							if((RcvData[5+i]&(1<<(7-j)))==(1<<(7-j)))
							{
								sprintf(buf+strlen(buf), "$%02X, ", j+(i*8)+1);
							}
						}
					}

					buf[strlen(buf)-2]='\0';
					WriteLog(buf);
				}

				//Sending request for MID 01 and PID 01
				Sleep(100);
				NumMsgs = 1;
				sprintf((char*)data, "\x68\x6A\xF1\x01\x01");
				memcpy(pMsg.Data, data, 5);
				pMsg.DataSize=5;
				pMsg.ProtocolID=J1850VPW;

				RetVal= PassThruWriteMsgs(ulChannelID, &pMsg, &NumMsgs, 1000);
				if (RetVal != STATUS_NOERROR)
				{
					return (0);
				}
				else
					WriteLog("Sent request for MID 01 PID 01!");
				
				Sleep(100);
				while(1)
				{
					RcvNumMsgs = 1;
					RetVal= PassThruReadMsgs(ulChannelID, &pMsg, &RcvNumMsgs, 1000);
					if (RetVal != STATUS_NOERROR)
					{
						break;
					}
					else
						WriteLog("Received response for MID 01 PID 01!");

					memcpy(RcvData, pMsg.Data, 9);
					sprintf(buf, "Module $%02X MILStatus: ", RcvData[2]);
					if ((RcvData[5]&0x80) == 0x80)
						strcat(buf, "ON");
					else
						strcat(buf, "OFF");
					WriteLog(buf);
					RcvData[5] = (RcvData[5]&0x7F);
					sprintf(buf, "Module $%02X DTC Count: %d", RcvData[2], RcvData[5]); 
					WriteLog(buf);
				}

				RetVal = PassThruStopMsgFilter(ulChannelID, ulMsgID);
				if (RetVal != STATUS_NOERROR)
					return(0);
				
				RetVal = PassThruDisconnect(ulChannelID);
				if (RetVal != STATUS_NOERROR)
				{
					WriteLog("PassThruDisconnect Failed!");
					return(0);
				}
				else
					WriteLog("Disconnect Successful!");

				WriteLog("");
				break;
				case IDCANCEL:
					EndDialog(hDlg, LOWORD(wParam));
				break;
			}
			return TRUE;
			break;
		default:
			return FALSE;
	}
}

int LoadFunctions()
{
	//Load J2534 Functions
	if ((hDLL = LoadLibrary("DGUNAT32.dll")) == NULL)
	{
		WriteLog("Failed to load the library!");
		return(0);
	}
	if ((PassThruConnect = (PTCONNECT)GetProcAddress(hDLL, "PassThruConnect")) == NULL)
	{
		WriteLog("PassThruConnect Failed!");
		return(0);
	}
	if ((PassThruDisconnect = (PTDISCONNECT)GetProcAddress(hDLL, "PassThruDisconnect")) == NULL)
	{
		WriteLog("PassThruDisconnect Failed!");
		return(0);
	}
	if ((PassThruReadMsgs = (PTREADMSGS)GetProcAddress(hDLL, "PassThruReadMsgs")) == NULL)
	{
		WriteLog("PassThruReadMsgs Failed!");
		return(0);
	}
	if ((PassThruWriteMsgs = (PTWRITEMSGS)GetProcAddress(hDLL, "PassThruWriteMsgs")) == NULL)
	{
		WriteLog("PassThruWriteMsgs Failed!");
		return(0);
	}
	if ((PassThruStartPeriodicMsg = (PTSTARTPERIODICMSG)GetProcAddress(hDLL,
	"PassThruStartPeriodicMsg")) == NULL)
	{
		WriteLog("PassThruStartPeriodicMsg Failed!");
		return(0);
	}
	if ((PassThruStopPeriodicMsg = (PTSTOPPERIODICMSG)GetProcAddress(hDLL,
	"PassThruStopPeriodicMsg")) == NULL)
	{
		WriteLog("PassThruStopPeriodicMsg Failed!");
		return(0);
	}
	if ((PassThruStartMsgFilter = (PTSTARTMSGFILTER)GetProcAddress(hDLL,
	"PassThruStartMsgFilter")) == NULL)
	{
		WriteLog("PassThruStartMsgFilter Failed!");
		return(0);
	}
	if ((PassThruStopMsgFilter = (PTSTOPMSGFILTER)GetProcAddress(hDLL, "PassThruStopMsgFilter"))
	== NULL)
	{
		WriteLog("PassThruStopMsgFilter Failed!");
		return(0);
	}
	if ((PassThruSetProgrammingVoltage = (PTSETPROGRAMMINGVOLTAGE)GetProcAddress(hDLL,
	"PassThruSetProgrammingVoltage")) == NULL)
	{
		WriteLog("PassThruSetProgrammingVoltage Failed!");
		return(0);
	}
	if ((PassThruReadVersion = (PTREADVERSION)GetProcAddress(hDLL, "PassThruReadVersion"))
	== NULL)
	{
		WriteLog("PassThruReadVersion Failed!");
		return(0);
	}
	if ((PassThruGetLastError = (PTGETLASTERROR)GetProcAddress(hDLL, "PassThruGetLastError"))
	== NULL)
	{
		WriteLog("PassThruGetLastError Failed!");
		return(0);
	}
	if ((PassThruIoctl = (PTIOCTL)GetProcAddress(hDLL, "PassThruIoctl")) == NULL)
	{
		WriteLog("PassThruIoctl Failed!");
		return(0);
	}
	return(1);
}



// Dearborn Group, Inc.   (248) 488-2080 (248) 488-2082 FAX   dg@dgtech.com

