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