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 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/*
* 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.h
* An Asynchronous DMX USB transceiver.
* Copyright (C) 2014 Simon Newton
*/
#ifndef PLUGINS_USBDMX_ASYNCUSBTRANSCEIVERBASE_H_
#define PLUGINS_USBDMX_ASYNCUSBTRANSCEIVERBASE_H_
#include <libusb.h>
#include "libs/usb/LibUsbAdaptor.h"
#include "ola/DmxBuffer.h"
#include "ola/base/Macro.h"
#include "ola/thread/Mutex.h"
namespace ola {
namespace plugin {
namespace usbdmx {
/**
* @brief A base class that implements common functionality to send or receive
* DMX asynchronously to a libusb_device.
*/
class AsyncUsbTransceiverBase {
public:
/**
* @brief Create a new AsyncUsbTransceiverBase.
* @param adaptor the LibUsbAdaptor to use.
* @param usb_device the libusb_device to use for the widget.
*/
AsyncUsbTransceiverBase(ola::usb::LibUsbAdaptor* const adaptor,
libusb_device *usb_device);
/**
* @brief Destructor
*/
virtual ~AsyncUsbTransceiverBase();
/**
* @brief Initialize the transceiver.
* @returns true if SetupHandle() returned a valid handle, false otherwise.
*/
bool Init();
/**
* @brief Called from the libusb callback when the asynchronous transfer
* completes.
* @param transfer the completed transfer.
*/
virtual void TransferComplete(struct libusb_transfer *transfer) = 0;
/**
* @brief Get the libusb_device_handle of an already opened widget
* @returns the handle of the widget or NULL if it was not opened
*/
libusb_device_handle *GetHandle() { return m_usb_handle; }
protected:
/**
* @brief The LibUsbAdaptor passed in the constructor.
*/
ola::usb::LibUsbAdaptor* const m_adaptor;
/**
* @brief The libusb_device passed in the constructor.
*/
libusb_device* const m_usb_device;
/**
* @brief Open the device handle.
* @returns A valid libusb_device_handle or NULL if the device could not be
* opened.
*/
virtual libusb_device_handle* SetupHandle() = 0;
/**
* @brief Called when the transfer completes.
*
* Some devices require multiple transfers per DMX frame. This provides a
* hook for continuation.
*/
virtual void PostTransferHook() {}
/**
* @brief Cancel any pending transfers.
*/
void CancelTransfer();
/**
* @brief Fill a control transfer.
* @param buffer passed to libusb_fill_control_transfer.
* @param timeout passed to libusb_fill_control_transfer.
*/
void FillControlTransfer(unsigned char *buffer, unsigned int timeout);
/**
* @brief Fill a bulk transfer.
*/
void FillBulkTransfer(unsigned char endpoint, unsigned char *buffer,
int length, unsigned int timeout);
/**
* @brief Fill an interrupt transfer.
*/
void FillInterruptTransfer(unsigned char endpoint, unsigned char *buffer,
int length, unsigned int timeout);
/**
* @brief Submit the transfer for tx.
* @returns the result of libusb_submit_transfer().
*/
int SubmitTransfer();
enum TransferState {
IDLE,
IN_PROGRESS,
DISCONNECTED,
};
libusb_device_handle *m_usb_handle;
bool m_suppress_continuation;
struct libusb_transfer *m_transfer;
TransferState m_transfer_state; // GUARDED_BY(m_mutex);
ola::thread::Mutex m_mutex;
private:
DISALLOW_COPY_AND_ASSIGN(AsyncUsbTransceiverBase);
};
} // namespace usbdmx
} // namespace plugin
} // namespace ola
#endif // PLUGINS_USBDMX_ASYNCUSBTRANSCEIVERBASE_H_
|