📄 tlp3ce.c
字号:
return dwData;
}
BOOL
SCR_Close(
DWORD Handle
)
{
BOOL fRet;
PSMARTCARD_EXTENSION pSmartcardExtension = (PSMARTCARD_EXTENSION) Handle;
SmartcardDebug(DEBUG_TRACE,(TEXT("%s: Close(%x) - entered\n"),szDriverName,Handle));
if (!ValidateAndEnterDevice(pSmartcardExtension))
{
SetLastError(ERROR_BAD_DEVICE);
return FALSE;
}
if (pSmartcardExtension->ReaderExtension->d_uReaderState != STATE_OPENED)
{
fRet = FALSE;
SetLastError(ERROR_BAD_DEVICE);
// ASSERT(FALSE);
}
else
{
DWORD retry = 0;
pSmartcardExtension->ReaderExtension->d_uReaderState = STATE_CLOSED;
while (pSmartcardExtension->ReaderExtension->d_RefCount > 1 && ++retry < 3)
{
// cancel any outstanding blocking calls
SmartcardDebug(DEBUG_TRACE,(TEXT("%s: Close - waiting for %d threads to exit\n"),
szDriverName, pSmartcardExtension->ReaderExtension->d_RefCount -1
));
SmartcardDeviceControl(pSmartcardExtension, IOCTL_SMARTCARD_CANCEL_BLOCKING,
NULL,0,NULL,0,NULL);
Sleep(10);
}
// TLP3CloseSerialPort(pSmartcardExtension );
if (pSmartcardExtension->ReaderExtension->hBackgroundThread) {
DWORD dwWait;
dwWait = WaitForSingleObject(pSmartcardExtension->ReaderExtension->hBackgroundThread,1000);
// ASSERT(dwWait != WAIT_TIMEOUT);
CloseHandle(pSmartcardExtension->ReaderExtension->hBackgroundThread);
pSmartcardExtension->ReaderExtension->hBackgroundThread=NULL;
}
fRet = TRUE;
}
LeaveDevice(pSmartcardExtension);
return fRet;
}
//
// I/O Control function - responds to info, read and write control codes.
//
BOOL
SCR_IOControl(
DWORD Handle,
DWORD dwIoControlCode,
PBYTE pInBuf,
DWORD nInBufSize,
PBYTE pOutBuf,
DWORD nOutBufSize,
PDWORD pBytesReturned
)
{
NTSTATUS status;
PSMARTCARD_EXTENSION pSmartcardExtension = (PSMARTCARD_EXTENSION) Handle;
{ PBYTE buf = pInBuf ;
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *buf));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+1)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+2)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+3)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+4)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+5)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+6)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+7)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+8)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+9)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+10)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+11)));
DEBUGMSG(1, (TEXT("buf=0x%x\r\n"), *(buf+12)));
}
if (!ValidateAndEnterDevice(pSmartcardExtension))
{
SetLastError(ERROR_BAD_DEVICE);
// ASSERT(FALSE);
return FALSE;
}
if (pSmartcardExtension->ReaderExtension->d_uReaderState != STATE_OPENED)
{
SmartcardDebug(DEBUG_ERROR,(TEXT("%s: DeviceIOCTL - invalid state %d\n"), szDriverName,
pSmartcardExtension->ReaderExtension->d_uReaderState
));
status = ERROR_GEN_FAILURE;
}
else {
// ASSERT(pSmartcardExtension->ReaderExtension->ReaderPowerState ==
// PowerReaderWorking);
status = SmartcardDeviceControl(
pSmartcardExtension, dwIoControlCode,
pInBuf, nInBufSize,
pOutBuf, nOutBufSize,
pBytesReturned);
};
//to application;
// pOutBuf = (PBYTE)pSmartcardExtension->CardCapabilities.ATR.Buffer;
// nOutBufSize=(DWORD)pSmartcardExtension->CardCapabilities.ATR.Length;
LeaveDevice(pSmartcardExtension);
return (status == STATUS_SUCCESS ? TRUE: FALSE);
}
// The function Not supported.
DWORD SCR_Read(DWORD Handle, LPVOID pBuffer, DWORD dwNumBytes){return 0;}
DWORD SCR_Write(DWORD Handle, LPCVOID pBuffer, DWORD dwNumBytes){return 0;}
DWORD SCR_Seek(DWORD Handle, long lDistance, DWORD dwMoveMethod){return 0;}
DWORD SCR_ISTthread(PSMARTCARD_EXTENSION smartcardExtension)
{
/* This thread is run for the lifetime of the OS */
while(1)
{
/* Block the thread and wait for an event to be signalled */
WaitForSingleObject(hSCIevent,INFINITE);
// InitializeDevice( );
g_SCI0RegBase->RegIntClear = 0x1FFF;
InterruptDone(SYSINTR_SCI);
} /* while(1) */
return 0;
}
// Power Up and Down Support
void SCR_PowerUp(void)
{
SmartcardDebug(DEBUG_TRACE,(TEXT("%s: PowerUp() - entered\n"),szDriverName));
}
void SCR_PowerDown(void)
{
SmartcardDebug(DEBUG_TRACE,(TEXT("%s: PowerDown() - entered\n"),szDriverName));
}
BOOL
TLP3UpdateReaderState(PSMARTCARD_EXTENSION SmartcardExtension)
{
BOOL bReturn;
SmartcardLockDevice(SmartcardExtension);
bReturn=TRUE;
if(((g_SCI0RegBase->RegSyncAct)&SCI0_CARD_PRESENT)==0)
{
SmartcardDebug(
DEBUG_THREAD,
(TEXT("%s!TLP3UpdateReaderState: Reader removed\r\n"),
szDriverName)
);
// During the reader absent, User may switch or remove the card
// so we treat them as removing card.
// Card is removed
// SmartcardExtension->CardCapabilities.ATR.Length = 0;
SmartcardExtension->ReaderCapabilities.CurrentState =
SCARD_ABSENT;
SmartcardExtension->ReaderExtension->CardStatus=PL131_STATE_NOCARD;
bReturn = FALSE;
} else {
// Card is inserted
SmartcardExtension->ReaderCapabilities.CurrentState = SCARD_PRESENT;
// SCARD_SWALLOWED;
SmartcardExtension->ReaderExtension->CardStatus=PL131_STATE_INACTIVECARD;
};
// SmartcardExtension->ReaderExtension->ModemStatusChanged=TRUE;
SmartcardUnlockDevice(SmartcardExtension);
return bReturn;
}
// Event Track Thread Entry
static DWORD CALLBACK
TLP3SerialEvent(PVOID pData )
/*++
--*/
{
PSMARTCARD_EXTENSION SmartcardExtension=(PSMARTCARD_EXTENSION)pData;
// BOOL bReturn;
// DWORD dwEventChanged;
SmartcardDebug(DEBUG_THREAD | DEBUG_INIT,
(TEXT("%s: TLP3SerialEvent(%x) - entered\n"),szDriverName,pData));
while (SmartcardExtension->ReaderExtension->d_uReaderState == STATE_OPENED) // infinit loop
{
if(SmartcardExtension->ReaderCapabilities.CurrentState==SCARD_ABSENT)
{
while (SmartcardExtension->ReaderCapabilities.CurrentState==SCARD_ABSENT)
{
if((g_SCI0RegBase->RegIntStatus)&SCI0_CARD_INIS)
{
g_SCI0RegBase->RegIntClear = 0x0001;
if( TLP3UpdateReaderState(SmartcardExtension))
break;
}
}
SmartcardCompleteCardTracking(SmartcardExtension);
}
else
{
while (SmartcardExtension->ReaderCapabilities.CurrentState!=SCARD_ABSENT)
{ Sleep(500);
if(((g_SCI0RegBase->RegSyncAct)&SCI0_CARD_PRESENT)==0)
{
TLP3UpdateReaderState(SmartcardExtension);
break;
}
}
SmartcardCompleteCardTracking(SmartcardExtension);
}
}
SmartcardDebug(
DEBUG_THREAD,
(TEXT("%s!TLP3SerialEvent: Thread exiting\n"),
szDriverName)
);
return 0;
}
BOOL
GetSerialPort(LPTSTR pSerialPort,LPTSTR ActiveKey)
{
HKEY hActiveKey;
DWORD ValType,ValLen;
DWORD status;
TCHAR buildInPath[REG_PATH_LEN];
SmartcardDebug(DEBUG_INIT|DEBUG_TRACE,
(TEXT("%s:TLP_Init Active Key %s \r\n"),szDriverName,ActiveKey));
//
// Get Device Name & Serial Port Device Name
if ((status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,ActiveKey,0,0,&hActiveKey))!=ERROR_SUCCESS) {
SmartcardDebug(DEBUG_INIT|DEBUG_ERROR,
(TEXT("%s: RegOpenKeyEx failed for key %s. Error Code \r\n"),szDriverName,ActiveKey,status));
return FALSE;
};
ValLen=sizeof(buildInPath);
status = RegQueryValueEx(hActiveKey,DEVLOAD_DEVKEY_VALNAME,NULL,
&ValType,(LPBYTE)&buildInPath[0],&ValLen);
RegCloseKey(hActiveKey);
if (status!=ERROR_SUCCESS) {
SmartcardDebug(DEBUG_INIT|DEBUG_ERROR,
(TEXT("%s: RegQueryValueEx failed for name %s. Error Code \r\n"),szDriverName,DEVLOAD_DEVKEY_VALNAME,status));
return FALSE;
};
if ((status=RegOpenKeyEx(HKEY_LOCAL_MACHINE,buildInPath,0,0,&hActiveKey))!=ERROR_SUCCESS) {
SmartcardDebug(DEBUG_INIT|DEBUG_ERROR,
(TEXT("%s: RegOpenKeyEx failed for key %s. Error Code \r\n"),szDriverName,ActiveKey,status));
return FALSE;
};
ValLen=DEVNAME_LEN*sizeof(TCHAR);
return TRUE;
};
DWORD
GetDeviceName(
LPTSTR ActivePath,
LPTSTR szDeviceName
)
{
DWORD ValType;
DWORD ValLen;
HKEY hCardKey;
DWORD status;
//
// Open active device registry key
//
status = RegOpenKeyEx(
HKEY_LOCAL_MACHINE,
ActivePath,
0,
0,
&hCardKey);
if (status) {
SmartcardDebug(DEBUG_INIT|DEBUG_ERROR,
(TEXT("GetDeviceName RegOpenKeyEx(HLM\\%s) returned %d!!!\r\n"),
ActivePath, status));
return status;
}
// get our device name (eg. PSC1:)
ValLen = DEVNAME_LEN*sizeof(TCHAR);
status = RegQueryValueEx(
hCardKey,
DEVLOAD_DEVNAME_VALNAME,
NULL,
&ValType,
(PUCHAR)szDeviceName,
&ValLen);
if (status != ERROR_SUCCESS) {
SmartcardDebug(DEBUG_INIT|DEBUG_ERROR,
(TEXT("GetDeviceName - RegQueryValueEx(%s) returned %d\r\n"),
DEVLOAD_DEVNAME_VALNAME, status));
}
RegCloseKey(hCardKey);
return status;
}
void
MakeFriendlyName(PSMARTCARD_EXTENSION SmartcardExtension, LPTSTR szFriendlyName)
{
TCHAR szUnitNoPart[] = L" [ ]";
szFriendlyName[0] = 0; // NULL in case IfdType is empty
mbstowcs(szFriendlyName,SmartcardExtension->VendorAttr.IfdType.Buffer,SmartcardExtension->VendorAttr.IfdType.Length);
szUnitNoPart[2] = (WCHAR)(SmartcardExtension->VendorAttr.UnitNo % 10 + '0');
lstrcatW(szFriendlyName,szUnitNoPart);
}
//
// TLP3LoadDevice:
// 1. Allocate and init the SMARTCARDEXTENSION struct
// 2. Init the device
//
// ActivePath is the registry path to our device's active key under
// HKEY_LOCAL_MACHINE\Drivers\Active
//
// Return pointer to new device structure or NULL.
//
PSMARTCARD_EXTENSION
TLP3LoadDevice(
LPTSTR ActiveKey
)
{
DWORD status;
PREADER_EXTENSION readerExtension;
PSMARTCARD_EXTENSION smartcardExtension;
static ULONG dataRatesSupported[] = { 9600, 19200, 38400, 57600, 115200 };
// allocate the device context (including smartcard and reader extension)
smartcardExtension = LocalAlloc(
LPTR,
sizeof( SMARTCARD_EXTENSION )
+ sizeof(READER_EXTENSION)
);
// ASSERT(smartcardExtension);
if( smartcardExtension == NULL )
return NULL;
//point to the reader extension
readerExtension = (PREADER_EXTENSION)(smartcardExtension +1);
smartcardExtension->ReaderExtension = readerExtension;
SmartcardDebug( DEBUG_TRACE | DEBUG_INIT,
( TEXT("%s!TLP3CreateDevice: Device created\n"),szDriverName));
if (!GetSerialPort(readerExtension->d_SerialPort,ActiveKey))
{
SmartcardDebug(DEBUG_INIT|DEBUG_ERROR,(TEXT("%s: SmartcardInitialize: no COM Port configured\n"),
szDriverName));
LocalFree(smartcardExtension);
return NULL;
}
// Write the version of the lib we use to the smartcard extension
smartcardExtension->Version = SMCLIB_VERSION;
smartcardExtension->SmartcardRequest.BufferSize =
smartcardExtension->SmartcardReply.BufferSize = MIN_BUFFER_SIZE;
//
// Now let the lib allocate the buffer for data transmission
// We can either tell the lib how big the buffer should be
// by assigning a value to BufferSize or let the lib
// allocate the default size
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -