Open Lighting Architecture  0.9.5
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
AsyncUsbSender.h
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
15  *
16  * AsyncUsbSender.h
17  * An Asynchronous DMX USB sender.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBDMX_ASYNCUSBSENDER_H_
22 #define PLUGINS_USBDMX_ASYNCUSBSENDER_H_
23 
24 #include <libusb.h>
25 
26 #include "ola/DmxBuffer.h"
27 #include "ola/base/Macro.h"
28 #include "ola/thread/Mutex.h"
29 
30 namespace ola {
31 namespace plugin {
32 namespace usbdmx {
33 
41  public:
47  AsyncUsbSender(class LibUsbAdaptor* const adaptor,
48  libusb_device *usb_device);
49 
53  virtual ~AsyncUsbSender();
54 
59  bool Init();
60 
66  bool SendDMX(const DmxBuffer &buffer);
67 
73  void TransferComplete(struct libusb_transfer *transfer);
74 
75  protected:
79  class LibUsbAdaptor* const m_adaptor;
80 
84  libusb_device* const m_usb_device;
85 
91  virtual libusb_device_handle* SetupHandle() = 0;
92 
102  virtual bool PerformTransfer(const DmxBuffer &buffer) = 0;
103 
110  virtual void PostTransferHook() {}
111 
115  void CancelTransfer();
116 
122  void FillControlTransfer(unsigned char *buffer, unsigned int timeout);
123 
127  void FillBulkTransfer(unsigned char endpoint, unsigned char *buffer,
128  int length, unsigned int timeout);
129 
133  void FillInterruptTransfer(unsigned char endpoint, unsigned char *buffer,
134  int length, unsigned int timeout);
135 
140  int SubmitTransfer();
141 
146  bool TransferPending() const { return m_pending_tx; }
147 
148  private:
149  enum TransferState {
150  IDLE,
151  IN_PROGRESS,
152  DISCONNECTED,
153  };
154 
155  libusb_device_handle *m_usb_handle;
156  bool m_suppress_continuation;
157  struct libusb_transfer *m_transfer;
158 
159  TransferState m_transfer_state; // GUARDED_BY(m_mutex);
160  DmxBuffer m_tx_buffer; // GUARDED_BY(m_mutex);
161  bool m_pending_tx; // GUARDED_BY(m_mutex);
162  ola::thread::Mutex m_mutex;
163 
165 };
166 } // namespace usbdmx
167 } // namespace plugin
168 } // namespace ola
169 #endif // PLUGINS_USBDMX_ASYNCUSBSENDER_H_