// Defines the entry point for the DLL application.
//
#include "stdafx.h"
//#define LOG_CALLS
char * atoh = "0123456789ABCDEF";
// " 0 1 2 3 4 5 6 7 8 9 : ; < = > ? @ A B C D E F"
BYTE htoa1[] = {0x00,0x10,0x20,0x30,0x40,0x50,0x60,0x70,0x80,0x90,0,0,0,0,0,0,0,0xA0,0xB0,0xC0,0xD0,0xE0,0xF0};
BYTE htoa2[] = {0x00,0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0,0,0,0,0,0,0,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F};
void Convert( LPVOID pBuffer, DWORD Count )
{
char* pCh = (char*)pBuffer;
char ch, crc1, crc2; DWORD nCRC; BYTE state = 10;
for (DWORD i=0;i<Count; i++)
{
ch = pCh[i];
if (ch == '$')
state = 11;
else
{
switch ( state )
{
// case 10: state = ch == '$'? 11:10; break;
case 11: state = ch == 'G'? 12:10; break;
case 12: state = ch == 'P'? 13:10; break;
case 13: state = ch == 'R'? 14:10; break;
case 14: state = ch == 'M'? 15:10; break;
case 15: state = ch == 'C'? 16:10; break;
case 16: state = ch == ','? 20:10; break;
case 20: if (ch == 'A') state = 21; break;
case 21: state = ch == '*'? 22:20; break;
case 22:
crc1 = ch; state = 23; break;
case 23:
{
crc2 = ch;
// get the crc
nCRC = htoa1[crc1 - '0'] + htoa2[crc2 - '0'];
// remove the chars we are deleting
nCRC -= 'A';
nCRC -= ',';
// ... ,,A*72\n
// ^ current pos
// ^ current pos
pCh[i-4]='*';
pCh[i-3]=atoh[(nCRC & 0xF0) >> 4];
pCh[i-2]=atoh[ nCRC & 0x0F ];
pCh[i-1]=13;
pCh[i-0]=10;
state = 10;
}
break;
}
}
}
}
#ifdef LOG_CALLS
void Log(LPCTSTR szStr, bool bLF = true)
{
HANDLE hFile = CreateFile ( _T("\\log.txt"), GENERIC_WRITE, FILE_SHARE_READ, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
SetFilePointer( hFile, 0, 0, FILE_END );
DWORD dwLen;
BOOL bOK = WriteFile( hFile, szStr, _tcslen(szStr)*sizeof(TCHAR), &dwLen, NULL );
WriteFile( hFile, _T("\n"), _tcslen(_T("\n")), &dwLen, NULL );
CloseHandle( hFile );
OutputDebugString( szStr );
/*
FILE *f = fopen("\\DemoDriverLog.txt", "a");
fprintf(f, "%u", GetTickCount());
fprintf(f, "%s", szStr);
if (bLF)
fprintf(f, "\n");
fflush(f);
fclose(f);
*/
}
#endif
GPSVIRTUALDRIVERDLL_API DWORD COM_Init(LPCTSTR pContext, LPCVOID lpvBusContext);
GPSVIRTUALDRIVERDLL_API BOOL COM_Deinit( DWORD hDeviceContext );
GPSVIRTUALDRIVERDLL_API DWORD COM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode );
GPSVIRTUALDRIVERDLL_API BOOL COM_Close( DWORD hOpenContext );
GPSVIRTUALDRIVERDLL_API BOOL COM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut );
GPSVIRTUALDRIVERDLL_API void COM_PowerUp( DWORD hDeviceContext );
GPSVIRTUALDRIVERDLL_API void COM_PowerDown( DWORD hDeviceContext );
GPSVIRTUALDRIVERDLL_API DWORD COM_Read( DWORD hOpenContext, LPVOID pBuffer, DWORD Count );
GPSVIRTUALDRIVERDLL_API DWORD COM_Write( DWORD hOpenContext, LPCVOID pBuffer, DWORD Count );
GPSVIRTUALDRIVERDLL_API DWORD COM_Seek( DWORD hOpenContext, long Amount, WORD Type );
#define DEVICE_CONTEXT 0x1450
#define OPEN_CONTEXT 0x1451
HANDLE hComm = INVALID_HANDLE_VALUE;
DWORD dwManageIndex = 1;
unsigned char tmpbuf[2048];
BOOL APIENTRY DllMain( HANDLE hModule,
DWORD ul_reason_for_call,
LPVOID lpReserved
)
{
switch ( ul_reason_for_call )
{
case DLL_PROCESS_ATTACH:
break;
case DLL_PROCESS_DETACH:
break;
case DLL_THREAD_ATTACH:
break;
case DLL_THREAD_DETACH:
break;
}
return TRUE;
}
GPSVIRTUALDRIVERDLL_API DWORD COM_Init( LPCTSTR pContext, LPCVOID lpvBusContext)
{
#ifdef LOG_CALLS
Log(_T("Init\n"));
#endif
DWORD Index = 0;
HKEY hKey;
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, pContext, 0, 0, &hKey) == ERROR_SUCCESS)
{
TCHAR szDriverKey[255];
DWORD dwSize = 255;
DWORD dwType;
RETAILMSG(1,(TEXT("COM_Init, pContext = %s\r\n"), pContext));
if (RegQueryValueEx(hKey, L"Key", 0, &dwType, (BYTE *)szDriverKey, &dwSize) == ERROR_SUCCESS)
{
RegCloseKey(hKey);
if (RegOpenKeyEx(HKEY_LOCAL_MACHINE, szDriverKey, 0, 0, &hKey) == ERROR_SUCCESS)
{
dwSize=4;
if (RegQueryValueEx(hKey, L"Index", 0, &dwType, (BYTE *)&Index, &dwSize) != ERROR_SUCCESS)
Index = 0;
RegCloseKey(hKey);
}
}
}
if (Index != 0)
dwManageIndex = Index;
return DEVICE_CONTEXT;
}
GPSVIRTUALDRIVERDLL_API BOOL COM_Deinit( DWORD hDeviceContext )
{
if (hDeviceContext != DEVICE_CONTEXT)
{
#ifdef LOG_CALLS
Log(_T("False Deinst\n"));
#endif
return FALSE;
}
#ifdef LOG_CALLS
Log(_T("DeInit\n"));
#endif
return TRUE;
}
GPSVIRTUALDRIVERDLL_API DWORD COM_Open( DWORD hDeviceContext, DWORD AccessCode, DWORD ShareMode )
{
if (hDeviceContext != DEVICE_CONTEXT)
{
#ifdef LOG_CALLS
Log(_T("False Open\n"));
#endif
return 0;
}
#ifdef LOG_CALLS
TCHAR buf[255];
_stprintf(buf, _T("Open port %s for access %u and share %u %s\n"), szPort, AccessCode, ShareMode, hComm ? _T("OK") : _T("Failed"));
Log(buf);
#endif
return OPEN_CONTEXT;
}
GPSVIRTUALDRIVERDLL_API BOOL COM_Close( DWORD hOpenContext )
{
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log(_T("False Close\n"));
#endif
return 0;
}
#ifdef LOG_CALLS
Log(_T("Close\n"));
#endif
BOOL bRet = CloseHandle(hComm);
hComm = INVALID_HANDLE_VALUE;
return bRet;
}
GPSVIRTUALDRIVERDLL_API BOOL COM_IOControl( DWORD hOpenContext, DWORD dwCode, PBYTE pBufIn, DWORD dwLenIn, PBYTE pBufOut, DWORD dwLenOut, PDWORD pdwActualOut )
{
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log(_T("False IOControl\n"));
#endif
return 0;
}
#ifdef LOG_CALLS
TCHAR buf[255];
_stprintf(buf, _T("Control code=%u, inlen=%u\n"), dwCode, dwLenIn);
Log(buf);
#endif
BOOL bRet=DeviceIoControl(
hComm,
dwCode,
pBufIn,
dwLenIn,
pBufOut,
dwLenOut,
pdwActualOut,
NULL);
#ifdef LOG_CALLS
_stprintf(buf, _T("IOControl return bRet=%d, outlen=%u\n"), bRet, *pdwActualOut);
Log(buf);
#endif
return bRet;
}
GPSVIRTUALDRIVERDLL_API void COM_PowerUp( DWORD hDeviceContext )
{
}
GPSVIRTUALDRIVERDLL_API void COM_PowerDown( DWORD hDeviceContext )
{
}
GPSVIRTUALDRIVERDLL_API DWORD COM_Read( DWORD hOpenContext, LPVOID pBuffer, DWORD Count )
{
DWORD dwBytes = 0;
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log(_T("False Read\n"));
#endif
return 0;
}
if(dwManageIndex==6)
{
hComm = CreateFile(
_T("\\log6.txt"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
//if(hComm!=INVALID_HANDLE_VALUE)Count=GetFileSize(hComm, NULL);
RETAILMSG(1,(TEXT("open log6.txt %d\r\n"),Count));
}
if(dwManageIndex==9)
{
hComm = CreateFile(
_T("\\log9.txt"),
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL,
NULL);
//if(hComm!=INVALID_HANDLE_VALUE)Count=GetFileSize(hComm, NULL);
RETAILMSG(1,(TEXT("open log9.txt %d\r\n"),Count));
}
//if(Count==0xFFFFFFFF)Count=0;
if(hComm!=INVALID_HANDLE_VALUE)
{
SetFilePointer( hComm, 0, 0, FILE_BEGIN );
ReadFile(hComm, tmpbuf,512, &dwBytes, NULL);
RETAILMSG(1,(TEXT("read txt %x%x%x %x\r\n"),tmpbuf[0],tmpbuf[1],tmpbuf[2],dwBytes));
CloseHandle(hComm);
if(dwManageIndex==6)
{
DeleteFile(_T("\\log6.txt"));
}
if(dwManageIndex==9)
{
DeleteFile(_T("\\log9.txt"));
}
}
pBuffer=&tmpbuf[0];
//Convert( pBuffer, dwBytes );
#ifdef LOG_CALLS
TCHAR buf[100];
_stprintf( buf, _T("Read max=%u, read count: %u\n"), Count, dwBytes );
Log(buf);
#endif
return dwBytes;
}
GPSVIRTUALDRIVERDLL_API DWORD COM_Write( DWORD hOpenContext, LPCVOID pBuffer, DWORD Count )
{
if (hOpenContext != OPEN_CONTEXT)
{
#ifdef LOG_CALLS
Log(_T("False Write\n"));
#endif
return 0;
}
#ifdef LOG_CALLS
TCHAR buf[255];
_stprintf(buf, _T("Write bytes=%u\n"), Count);
Log(buf);
#endif
DWORD dwBytes = 0;
//WriteFile(hComm, pBuffer, Count, &dwBytes, NULL);
return dwBytes;
}
GPSVIRTUALDRIVERDLL_API DWORD COM_Seek( DWORD hOpenContext, long Amount, WORD Type )
{
#ifdef LOG_CALLS
TCHAR buf[255];
_stprintf(buf, _T("Seek Amount=%u, Type=%u\n"), Amount, Type);
Log(buf);
#endif
return 0;
}