Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include "TUSBScannerIO.h"
Toshihiro Shimizu 890ddd
#include "tsystem.h"
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
#include <windows.h></windows.h>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
class TUSBScannerIOPD
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
public:
Toshihiro Shimizu 890ddd
	TUSBScannerIOPD()
Toshihiro Shimizu 890ddd
		: m_handle(INVALID_HANDLE_VALUE), m_trace(false)
Toshihiro Shimizu 890ddd
	{
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	HANDLE m_handle;
Toshihiro Shimizu 890ddd
	bool m_trace;
Toshihiro Shimizu 890ddd
};
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
namespace
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
void buf2printable(const unsigned char *buffer, const int size, ostrstream &os)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int i = 0;
Toshihiro Shimizu 890ddd
	if ((size == 2) && (buffer[0] == 0x1b)) {
Toshihiro Shimizu 890ddd
		os << "ESC ";
Toshihiro Shimizu 890ddd
		char c = buffer[1];
Toshihiro Shimizu 890ddd
		if (isprint(c))
Toshihiro Shimizu 890ddd
			os << c << " ";
Toshihiro Shimizu 890ddd
		return;
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	os << std::hex;
Toshihiro Shimizu 890ddd
	for (; i < tmin(size, 0x40); ++i) {
Toshihiro Shimizu 890ddd
		char c = buffer[i];
Toshihiro Shimizu 890ddd
		os << "0x" << (unsigned int)c << " ";
Toshihiro Shimizu 890ddd
	}
Toshihiro Shimizu 890ddd
	if (i < size)
Toshihiro Shimizu 890ddd
		os << "...";
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUSBScannerIO::TUSBScannerIO()
Toshihiro Shimizu 890ddd
	: m_data(new TUSBScannerIOPD())
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
bool TUSBScannerIO::open()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_data->m_handle = CreateFile("\\\\.\\usbscan0", GENERIC_WRITE | GENERIC_READ,
Toshihiro Shimizu 890ddd
								  FILE_SHARE_WRITE | FILE_SHARE_READ,
Toshihiro Shimizu 890ddd
								  NULL,
Toshihiro Shimizu 890ddd
								  OPEN_EXISTING,
Toshihiro Shimizu 890ddd
								  0,
Toshihiro Shimizu 890ddd
								  NULL);
Toshihiro Shimizu 890ddd
	if (m_data->m_handle == INVALID_HANDLE_VALUE)
Toshihiro Shimizu 890ddd
		return false;
Toshihiro Shimizu 890ddd
	return true;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TUSBScannerIO::close()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	if (m_data->m_handle && (m_data->m_handle != INVALID_HANDLE_VALUE))
Toshihiro Shimizu 890ddd
		CloseHandle(m_data->m_handle);
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TUSBScannerIO::receive(unsigned char *buffer, int size)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int bytesLeft = size;
Toshihiro Shimizu 890ddd
	unsigned char *ptr = buffer;
Toshihiro Shimizu 890ddd
	DWORD count;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	static int m_maxReadSize = 0x10000;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
		int bytesToRead = bytesLeft;
Toshihiro Shimizu 890ddd
		notMoreThan<int>(m_maxReadSize, bytesToRead);</int>
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		OVERLAPPED overlapped;
Toshihiro Shimizu 890ddd
		memset(&overlapped, 0, sizeof(OVERLAPPED));
Toshihiro Shimizu 890ddd
		overlapped.hEvent = CreateEvent(
Toshihiro Shimizu 890ddd
			NULL, //pointertosecurityattributes,
Toshihiro Shimizu 890ddd
			//WIN95ignoresthisparameter
Toshihiro Shimizu 890ddd
			FALSE, //automaticreset
Toshihiro Shimizu 890ddd
			FALSE, //initializetonotsignaled
Toshihiro Shimizu 890ddd
			NULL); //pointertotheevent-objectname
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ReadFile(m_data->m_handle, ptr, bytesToRead, &count, &overlapped);
Toshihiro Shimizu 890ddd
		DWORD waitRC = WaitForSingleObject(overlapped.hEvent, INFINITE);
Toshihiro Shimizu 890ddd
		if (m_data->m_trace) {
Toshihiro Shimizu 890ddd
			ostrstream os;
Toshihiro Shimizu 890ddd
			os.freeze(false);
Toshihiro Shimizu 890ddd
			os << "receive: size=" << size << " got = " << count << " buf=";
Toshihiro Shimizu 890ddd
			buf2printable(ptr, count, os);
Toshihiro Shimizu 890ddd
			os << '\n' << '\0';
Toshihiro Shimizu 890ddd
			TSystem::outputDebug(os.str());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (count != bytesToRead)
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		ptr += count;
Toshihiro Shimizu 890ddd
		bytesLeft = bytesLeft - count;
Toshihiro Shimizu 890ddd
	} while (bytesLeft);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return size;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
int TUSBScannerIO::send(unsigned char *buffer, int size)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	int bytesLeft = size;
Toshihiro Shimizu 890ddd
	DWORD count;
Toshihiro Shimizu 890ddd
	static int m_maxWriteSize = 64;
Toshihiro Shimizu 890ddd
	//bytesLeft = 64;
Toshihiro Shimizu 890ddd
	do {
Toshihiro Shimizu 890ddd
		int bytesToWrite = bytesLeft;
Toshihiro Shimizu 890ddd
		notMoreThan<int>(m_maxWriteSize, bytesToWrite);</int>
Toshihiro Shimizu 890ddd
		WriteFile(m_data->m_handle, buffer, bytesToWrite, &count, 0);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (m_data->m_trace) {
Toshihiro Shimizu 890ddd
			ostrstream os;
Toshihiro Shimizu 890ddd
			os.freeze(false);
Toshihiro Shimizu 890ddd
			os << "send: size=" << size << " wrote = " << count << " buf=";
Toshihiro Shimizu 890ddd
			buf2printable(buffer, size, os);
Toshihiro Shimizu 890ddd
			os << '\n' << '\0';
Toshihiro Shimizu 890ddd
			TSystem::outputDebug(os.str());
Toshihiro Shimizu 890ddd
		}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		if (count != bytesToWrite)
Toshihiro Shimizu 890ddd
			return 0;
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
		// ptr += count;
Toshihiro Shimizu 890ddd
		bytesLeft = bytesLeft - count;
Toshihiro Shimizu 890ddd
	} while (bytesLeft);
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
	return size;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
void TUSBScannerIO::trace(bool on)
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
	m_data->m_trace = on;
Toshihiro Shimizu 890ddd
}
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
//-----------------------------------------------------------------------------
Toshihiro Shimizu 890ddd
Toshihiro Shimizu 890ddd
TUSBScannerIO::~TUSBScannerIO()
Toshihiro Shimizu 890ddd
{
Toshihiro Shimizu 890ddd
}