1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
/*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*
* AsyncUsbTransceiverBase.cpp
* An Asynchronous DMX USB transceiver.
* Copyright (C) 2014 Simon Newton
*/
#include "plugins/usbdmx/AsyncUsbTransceiverBase.h"
#include "libs/usb/LibUsbAdaptor.h"
#include "ola/Logging.h"
namespace ola {
namespace plugin {
namespace usbdmx {
using ola::usb::LibUsbAdaptor;
namespace {
/*
* Called by libusb when the transfer completes.
*/
#ifdef _WIN32
__attribute__((__stdcall__))
#endif // _WIN32
void AsyncCallback(struct libusb_transfer *transfer) {
AsyncUsbTransceiverBase *widget = reinterpret_cast<AsyncUsbTransceiverBase*>(
transfer->user_data);
widget->TransferComplete(transfer);
}
} // namespace
AsyncUsbTransceiverBase::AsyncUsbTransceiverBase(LibUsbAdaptor *adaptor,
libusb_device *usb_device)
: m_adaptor(adaptor),
m_usb_device(usb_device),
m_usb_handle(NULL),
m_suppress_continuation(false),
m_transfer_state(IDLE) {
m_transfer = m_adaptor->AllocTransfer(0);
m_adaptor->RefDevice(usb_device);
}
AsyncUsbTransceiverBase::~AsyncUsbTransceiverBase() {
CancelTransfer();
m_adaptor->UnrefDevice(m_usb_device);
m_adaptor->FreeTransfer(m_transfer);
}
bool AsyncUsbTransceiverBase::Init() {
m_usb_handle = SetupHandle();
return m_usb_handle != NULL;
}
void AsyncUsbTransceiverBase::CancelTransfer() {
if (!m_transfer) {
return;
}
bool canceled = false;
while (1) {
ola::thread::MutexLocker locker(&m_mutex);
if (m_transfer_state == IDLE || m_transfer_state == DISCONNECTED) {
break;
}
if (!canceled) {
m_suppress_continuation = true;
if (m_adaptor->CancelTransfer(m_transfer) == 0) {
canceled = true;
} else {
break;
}
}
}
m_suppress_continuation = false;
}
void AsyncUsbTransceiverBase::FillControlTransfer(unsigned char *buffer,
unsigned int timeout) {
m_adaptor->FillControlTransfer(m_transfer, m_usb_handle, buffer,
&AsyncCallback, this, timeout);
}
void AsyncUsbTransceiverBase::FillBulkTransfer(unsigned char endpoint,
unsigned char *buffer,
int length,
unsigned int timeout) {
m_adaptor->FillBulkTransfer(m_transfer, m_usb_handle, endpoint, buffer,
length, &AsyncCallback, this, timeout);
}
void AsyncUsbTransceiverBase::FillInterruptTransfer(unsigned char endpoint,
unsigned char *buffer,
int length,
unsigned int timeout) {
m_adaptor->FillInterruptTransfer(m_transfer, m_usb_handle, endpoint, buffer,
length, &AsyncCallback, this, timeout);
}
int AsyncUsbTransceiverBase::SubmitTransfer() {
int ret = m_adaptor->SubmitTransfer(m_transfer);
if (ret) {
OLA_WARN << "libusb_submit_transfer returned "
<< m_adaptor->ErrorCodeToString(ret);
if (ret == LIBUSB_ERROR_NO_DEVICE) {
m_transfer_state = DISCONNECTED;
}
return false;
}
m_transfer_state = IN_PROGRESS;
return ret;
}
} // namespace usbdmx
} // namespace plugin
} // namespace ola
|