Пример #1
0
qint32 QUsbDevice::write(const QByteArray* buf, quint32 maxSize)
{
    UsbPrintFuncName();
    Q_CHECK_PTR(buf);
    if (mUsbHandle==INVALID_HANDLE_VALUE
            || !mConfig.writeEp
            || !mConnected)
    {
        return -1;
    }

    ulong cbSent = 0;
    if (mDebug) qDebug("Writing %d bytes to endpoint 0x%02x", maxSize,(uint)mConfig.writeEp);
    bool bResult = WinUsb_WritePipe(mUsbHandle, mConfig.writeEp, (uchar*)buf->data(), maxSize, &cbSent, NULL);

    if (mDebug) {
        QString datastr, s;
        for (qint64 i = 0; i < maxSize; i++) {
            datastr.append(s.sprintf("%02X:", (uchar)buf->at(i)));
        }
        datastr.remove(datastr.size()-1, 1); //remove last colon
        qDebug() << "Sent" << cbSent << "/" << maxSize << "bytes:" << datastr;
    }
    if (!bResult) {
        printUsbError("WinUsb_WritePipe");
        return -1;
    }
    return cbSent;
}
Пример #2
0
void NiashLibUsbWriteBulk(const int iHandle, SANE_Byte* const pabData, const int iSize)
{
	assert(iHandle==1);
	assert(s_hWinUSB);
	assert(s_Pipes.BulkOut);

	//Send setup packet
	SANE_Byte abSetup[8] = { 0x01, 0x01, 0x00, 0x00, (iSize) & 0xFF, (iSize >> 8) & 0xFF, 0x00, 0x00 };
	WINUSB_SETUP_PACKET Packet;
	Packet.RequestType = BMREQUEST_HOST_TO_DEVICE<<7|BMREQUEST_VENDOR<<5; //Vendor type request, PC to device
	Packet.Request = 0x04; //Send multiple bytes
	Packet.Value = static_cast<USHORT>(USB_SETUP);
	Packet.Index = 0x00;	
	Packet.Length = 0x08;
	SendControlTransfer(Packet,abSetup);

	ULONG LengthTransferred;
	if(!WinUsb_WritePipe(s_hWinUSB, s_Pipes.BulkOut, pabData, iSize, &LengthTransferred, nullptr))
	{
		wprintf_s(L"WinUsb_WritePipe: %s\n", _com_error(GetLastError()).ErrorMessage());		
		return;
	}
}

void NiashLibUsbReadBulk (const int iHandle, SANE_Byte* const pabData, const int iSize)
{
	assert(iHandle==1);
	assert(s_hWinUSB);
	assert(s_Pipes.BulkIn);

	//Send setup packet
	SANE_Byte abSetup[8] = { 0x00, 0x00, 0x00, 0x00, (iSize) & 0xFF, (iSize >> 8) & 0xFF, 0x00, 0x00 };
	WINUSB_SETUP_PACKET Packet;
	Packet.RequestType = BMREQUEST_HOST_TO_DEVICE<<7|BMREQUEST_VENDOR<<5; //Vendor type request, PC to device
	Packet.Request = 0x04; //Send multiple bytes
	Packet.Value = static_cast<USHORT>(USB_SETUP);
	Packet.Index = 0x00;	
	Packet.Length = 0x08;
	SendControlTransfer(Packet,abSetup);

	ULONG LengthTransferred;
	if(!WinUsb_ReadPipe(s_hWinUSB, s_Pipes.BulkIn, pabData, iSize, &LengthTransferred, nullptr))
	{
		wprintf_s(L"WinUsb_WritePipe: %s\n", _com_error(GetLastError()).ErrorMessage());		
		return;
	}
}

void NiashLibUsbWaitForInterrupt()
{
	assert(s_hWinUSB);
	assert(s_Pipes.Interrupt);
	BYTE buf[1];
	ULONG LengthTransferred;
	if(!WinUsb_ReadPipe(s_hWinUSB,s_Pipes.Interrupt,buf,_countof(buf),&LengthTransferred,nullptr))
	{
		wprintf_s(L"WinUsb_ReadPipe: %s\n", _com_error(GetLastError()).ErrorMessage());		
		return;
	}
}
Пример #3
0
BOOL WriteToBulkEndpoint(WINUSB_INTERFACE_HANDLE hDeviceHandle, UCHAR* pID, ULONG* pcbWritten, UCHAR *szBuffer, ULONG cbSize)
{
    if (hDeviceHandle==INVALID_HANDLE_VALUE || !pID || !pcbWritten) {
        return FALSE;
    }

    BOOL bResult = TRUE;

//    UCHAR szBuffer[] = "Hello World";
//    ULONG cbSize = strlen((char*)szBuffer);
    ULONG cbSent = 0;

    bResult = WinUsb_WritePipe(hDeviceHandle, *pID, szBuffer, cbSize, &cbSent, 0);
    if(!bResult) {
        goto done;
    }

 //==   printf("Wrote to pipe %d: %s \nActual data transferred: %d.\n", *pID, szBuffer, cbSent);
    *pcbWritten = cbSent;


done:
    return bResult;

} // End of WriteToBulkEndpoint()
Пример #4
0
/*! \brief Write the data to the USB port.
	\details The function writes the given data to the connected USB port.
	The default timeout is respected in this operation.
	\param length The number of bytes to write.
	\param data A pointer to a memory buffer that contains the bytes to send
	\param written An optional pointer to storage for the actual number of bytes that were written
	\returns XRV_OK if the data was successfully written
	\sa writeData(const XsByteArray&, XsSize*)
*/
XsResultValue UsbInterface::writeData(const XsSize length, const void *data, XsSize* written)
{
	XsSize bytes;
	if (written == NULL)
		written = &bytes;

	if (!isOpen())
		return (d->m_lastResult = XRV_NOPORTOPEN);

	*written = 0;

#ifdef USE_WINUSB
	ULONG dataWritten;
	WinUsb_WritePipe(d->m_usbHandle[1],
		d->m_bulkOutPipe,
		(uint8_t*)data,
		(ULONG)length,
		&dataWritten,
		NULL);	//lint !e534

	*written = dataWritten;
#else
	*written = 0;
	while (*written < length)
	{
		int actual;
		int result = libusb_bulk_transfer(d->m_deviceHandle, (d->m_dataOutEndPoint|LIBUSB_ENDPOINT_OUT), (unsigned char *)data, length, &actual, 0);
		*written += actual;
		if (result != LIBUSB_SUCCESS)
		{
			JLALERT(gJournal, "bulk write failed: " << d->libusbErrorToString(result) << ". " << actual << " bytes sent");
			return (d->m_lastResult = d->libusbErrorToXrv(result));
		}
	}

#endif

#ifdef LOG_RX_TX
	if (*written > 0)
	{
		if (d->tx_log == NULL)
		{
			char fname[XS_MAX_FILENAME_LENGTH];
			sprintf(fname,"tx_USB%03u_%03u.log", usbBus(), usbAddress());
			d->tx_log = fopen(fname,"wb");
		}
		fwrite(data,1,*written,d->tx_log);
		fflush(d->tx_log);
	}
#endif

	return (d->m_lastResult = XRV_OK);
}
int
USBWrite(void *deviceHandle, unsigned char endpoint, char * data, int numberOfBytes) {
    /* Local variables */
    long transferred = 0;
    __usb_interface_t *usb;

    if(0 == deviceHandle) {
        return WRITE_FAILED;
    }

    usb = (__usb_interface_t *)deviceHandle;

    WinUsb_WritePipe(usb->winUSBHandle, endpoint, data, numberOfBytes,
        &transferred, NULL);

    return (int)transferred;
}
Пример #6
0
int USBDriver_Impl_WinUSB::BulkWrite( int iEndpoint, char *pData, int iSize, int iTimeout )
{
	int iRet = -1;
	WinUsb_WritePipe( m_hDevice, iEndpoint, pData, iSize );
	return iRet;
}