📄 nscdevice.cpp
字号:
// Then read the register in question
DWORD cbBytes(1);
hres = RawReadData(&bValue, &cbBytes, NULL);
// Write To Log
WriteToLog( STI_TRACE_INFORMATION,
L"%s=0x%X", // Message, Error, Code
L"Reg2",
bValue) ;
}
// If the read failed bValue should be unchanged
UnLockDevice();
}
return hres;
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::ResetModeCheckAndClear checks to see if the scanner is in
the reset mode and if it is, attempts to bring it out of the reset mode
PARAMETERS:
-
RETURN VALUE:
HRESULT - return STI_OK if the operation succeeded otherwise it returns
an error code
NOTES:
Please note If the scanner is in reset mode this function will attempt will
bring it out of the reset mode by clearing the reset bit *** BUT ***
the return value does not indicate if the scanner was successfully brought
out of the reset mode , the return value simply indicates if the scanner
was in reset mode or not when this method was called.
*******************************************************************************/
HRESULT UsdNSCDevice::ResetModeCheckAndClear()
{
HRESULT hres = STI_OK;
if (!m_bPolling)
{
return hres;
}
hres = LockDevice();
const int MAX_RETRY = 20;
for (int retry = 0; retry < MAX_RETRY; retry++)
{
// Read the command register and check that the reset bit is not set
// If it is set clear it and return false to indicate that
// the bit has only now been cleared
if ( SUCCEEDED(hres) && (INVALID_HANDLE_VALUE != m_DeviceDataHandle))
{
BYTE pCommandBytes[COMMAND_BYTE_COUNT];
BYTE bValue;
// Write the command bytes for a register read
// without increment
pCommandBytes[0] = MODE_NOINC_READ;
pCommandBytes[1] = LM_COMMAND_REGISTER;
pCommandBytes[2] = 0;
pCommandBytes[3] = 1;
hres = RawWriteData(pCommandBytes, COMMAND_BYTE_COUNT, NULL);
if (SUCCEEDED(hres))
{
// Then read the register in question
DWORD cbBytes(1);
hres = RawReadData(&bValue, &cbBytes, NULL);
// Write To Log
WriteToLog( STI_TRACE_INFORMATION,
L"%s=0x%X,0x%X", // Message, Error, Code
L"Reg7,Retry",
bValue,retry) ;
}
if (SUCCEEDED(hres))
{
if ( (bValue & NS_RESET_BIT) == NS_RESET_BIT)
{
hres = STIERR_GENERIC;
BYTE pWriteData[COMMAND_BYTE_COUNT+1];
// Write the command bytes for a register read
// without increment
pWriteData[0] = MODE_NOINC_WRITE;
pWriteData[1] = LM_COMMAND_REGISTER;
pWriteData[2] = 0;
pWriteData[3] = 1;
pWriteData[4] = 0; // <--- The data for the register
// We will attempt to reset it but we really don't do
// anything if this fails
hres = RawWriteData(pWriteData, COMMAND_BYTE_COUNT+1, NULL);
}
else break; //done
}
}
}
UnLockDevice();
if (SUCCEEDED(hres))
{
hres = SetRemoteWakeup(true); //enable
}
return (hres);
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::GetEventGUID
PARAMETERS:
BYTE bStatusRegister
RETURN VALUE:
GUID
NOTES:
*******************************************************************************/
GUID UsdNSCDevice::GetEventGUID(BYTE bStatusRegister)
{
// For each MISC IO bit in the status register we will
// check if there is a corresponding event registered
// If an event is registered for a particular IO bit and
// the bit is turned on then fire that event
// if the status register bit corresponding to a event is
// turned on and that event is currently registered with the
// STI then return the event code for that event otherwise
// return a NULL guid
// NEW: only accept one bit set at once to avoid problem with
// remote wakeup
int savei,ct = 0;
for (int i=0 ; i < NS_MAX_EVENTS; i++)
{
if ((bStatusRegister & NS_MISC_IO_BIT[i]) == NS_MISC_IO_BIT[i])
{
ct++;
savei = i;
}
}
if ((ct == 1) &&
((m_bEventMask & NS_EVENT_BIT[savei]) == NS_EVENT_BIT[savei]))
return m_bEventGuidArray[savei];
return GUID_NULL;
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::LoadDeviceData
PARAMETERS:
HKEY hParameters
RETURN VALUE:
void
NOTES:
*******************************************************************************/
void UsdNSCDevice::LoadDeviceData(HKEY hParameters)
{
HKEY hDeviceData;
LONG nRes = RegOpenKey( hParameters,
"DeviceData",
&hDeviceData );
if (nRes != ERROR_SUCCESS)
{
// Write To Log
WriteToLog( STI_TRACE_ERROR,
L"%s : %s : 0x%X", // Message, Error, Code
L"UsdNSCDevice::Loading Device Data Failed - No Events will be fired",
L"Registry Read Error",
::GetLastError()) ;
return ;
}
LoadEventMask(hDeviceData);
LoadDebounceTime(hDeviceData);
LoadPollingFlag(hDeviceData);
LoadRemoteWakeupFlag(hDeviceData);
LoadKickStartTime(hDeviceData);
RegCloseKey(hDeviceData);
return;
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::LoadEventMask
PARAMETERS:
HKEY hDeviceData
RETURN VALUE:
void
NOTES:
*******************************************************************************/
void UsdNSCDevice::LoadEventMask(HKEY hDeviceData)
{
// Initially we assume that all events are turned off
m_bEventMask = 0;
DWORD dwType;
char szKey[16];
char szEventMask[8];
DWORD cbData = sizeof(szEventMask)-1;
for (int i=1; i <= NS_MAX_EVENTS; i++)
{
sprintf(szKey, "%s%d", "EventMask", i);
LONG nRes = RegQueryValueEx( hDeviceData,
szKey,
NULL,
&dwType,
(LPBYTE)szEventMask,
&cbData );
if ((nRes == ERROR_SUCCESS) && !strcmp(szEventMask, "1"))
{
m_bEventMask |= NS_EVENT_BIT[i-1];
}
}
// Also initialize the event guids for all the events
m_bEventGuidArray[0] = guidEvent1;
m_bEventGuidArray[1] = guidEvent2;
m_bEventGuidArray[2] = guidEvent3;
m_bEventsInitialized = true;
return;
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::LoadDebounceTime
PARAMETERS:
HKEY hDeviceData
RETURN VALUE:
void
NOTES:
*******************************************************************************/
void UsdNSCDevice::LoadDebounceTime(HKEY hDeviceData)
{
DWORD dwType;
char* szKey = "DebounceTime";
char szDebounceTime[8];
DWORD cbData = sizeof(szDebounceTime)-1;
LONG nRes = RegQueryValueEx( hDeviceData,
szKey,
NULL,
&dwType,
(LPBYTE)szDebounceTime,
&cbData );
// If we successsfully load the debounce value and it turns out
// to be within our limits then we will change the debounce
// time otherwise we just let it be the default value.
if ((nRes == ERROR_SUCCESS))
{
DWORD dwDebounceTime = atoi(szDebounceTime);
if ((dwDebounceTime >= NS_MIN_DEBOUNCE) &&
(dwDebounceTime <= NS_MAX_DEBOUNCE))
{
m_dwDebounceTime = dwDebounceTime;
}
}
return;
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::SetRemoteWakeup - enables or disables the remote wakeup feature
on the scanner based on the input parameter. Uses the Win32 power managment fns
to accomplish this.
PARAMETERS:
bool bEnable
RETURN VALUE:
HRESULT
NOTES:
It is assumed that when this function is called, the device handle is
valid. Since this is a protected function and cannot be invoked outside
the class this is reasoanable as only class memebers will have to make
sure that the devide handle is valid before invoking this function
The result code will be returned based on the return from the Win32 power
management fucntions that are invoked and the last error code will also
be set appropriately
*******************************************************************************/
HRESULT UsdNSCDevice::SetRemoteWakeup(bool bEnable)
{
if (!m_bRemoteWakeup) return STI_OK;
UCHAR data = 0;
IO_BLOCK stIoBlock;
stIoBlock.uOffset = 1;
stIoBlock.uLength = 1;
stIoBlock.pbyData = &data;
stIoBlock.uIndex = bEnable?NS_ENABLE_REMOTEWAKEUP:NS_DISABLE_REMOTEWAKEUP;
DWORD cbBytes = sizeof(UCHAR);
HRESULT hres = LockDevice();
if (SUCCEEDED(hres))
{
hres = RawWriteCommand(&stIoBlock, cbBytes, NULL);
UnLockDevice();
}
return (hres);
}
void UsdNSCDevice::LogError(char *string,DWORD ct,BYTE b0,BYTE b1,BYTE b2,
BYTE b3,BYTE b4,HRESULT hres)
{
//open file, save error
char dir[MAX_PATH]; // file name
dir[0] = '\0';
GetWindowsDirectory(dir,sizeof(dir));
if (dir[sizeof(dir)-1] != '\\') strcat(dir,"\\");
strcat(dir,"nscstiu_error.txt");
FILE *fp;
fp = fopen(dir,"at"); //open text file
if (fp)
{
fprintf(fp,"%s,len=%x,data=%x,%x,%x,%x,%x,err=%x\n",string,(int)ct,
(int)b0,(int)b1,(int)b2,(int)b3,(int)b4,(int)hres); //write to file
fclose(fp); //close so keep if crash
}
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::LoadPollingFlag
PARAMETERS:
HKEY hDeviceData
RETURN VALUE:
void
NOTES:
*******************************************************************************/
void UsdNSCDevice::LoadPollingFlag(HKEY hDeviceData)
{
DWORD dwType;
char* szKey = "Polling";
char szPolling[8];
DWORD cbData = sizeof(szPolling)-1;
LONG nRes = RegQueryValueEx( hDeviceData,
szKey,
NULL,
&dwType,
(LPBYTE)szPolling,
&cbData );
// If we successsfully load the debounce value and it turns out
// to be within our limits then we will change the debounce
// time otherwise we just let it be the default value.
if ((nRes == ERROR_SUCCESS))
{
DWORD dwPolling = atoi(szPolling);
if ((dwPolling == 0) ||
(dwPolling == 1))
{
m_bPolling = (dwPolling == 1);
}
}
return;
}
/*******************************************************************************
DESCRIPTION:
UsdNSCDevice::LoadRemoteWakeupFlag
PARAMETERS:
HKEY hDeviceData
RETURN VALUE:
void
NOTES:
*******************************************************************************/
void UsdNSCDevice::LoadRemoteWakeupFlag(HKEY hDeviceData)
{
DWORD dwType;
char* szKey = "RemoteWakeup";
char szRemoteWakeup[8];
DWORD cbData = sizeof(szRemoteWakeup)-1;
LONG nRes = RegQueryValueEx( hDeviceData,
szKey,
NULL,
&dwType,
(LPBYTE)szRemoteWakeup,
&cbData );
// If we successsfully load the debounce value and it turns out
// to be within our limits then we will change the debounce
// time otherwise we just let it be the default value.
if ((nRes == ERROR_SUCCESS))
{
DWORD dwRemoteWakeup = atoi(szRemoteWakeup);
if ((dwRemoteWakeup == 0) ||
(dwRemoteWakeup == 1))
{
m_bRemoteWakeup = (dwRemoteWakeup == 1);
}
}
return;
}
//For WHQL test, do RawWriteData,RawReadData
HRESULT UsdNSCDevice::WHQLRawWriteReadTest(void)
{
BYTE pCommandBytes[COMMAND_BYTE_COUNT];
BYTE bValue;
// Write the command bytes for a register read
// without increment
pCommandBytes[0] = MODE_NOINC_READ;
pCommandBytes[1] = LM_COMMAND_REGISTER;
pCommandBytes[2] = 0;
pCommandBytes[3] = 1;
m_bWHQLTest = false;
HRESULT hres = RawWriteData(pCommandBytes, COMMAND_BYTE_COUNT, NULL);
if (SUCCEEDED(hres))
{
// Then read the register in question
DWORD cbBytes(1);
hres = RawReadData(&bValue, &cbBytes, NULL);
}
m_bWHQLTest = true;
return hres;
}
//Do kick start
HRESULT UsdNSCDevice::DoKickStart()
{
NSC_TRACE("In UsdNSCDevice::DoKickStart");
HRE
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -