Open Lighting Architecture  0.9.4
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
LibUsbAdaptor.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  * LibUsbAdaptor.h
17  * The wrapper around libusb calls.
18  * Copyright (C) 2014 Simon Newton
19  */
20 
21 #ifndef PLUGINS_USBDMX_LIBUSBADAPTOR_H_
22 #define PLUGINS_USBDMX_LIBUSBADAPTOR_H_
23 
24 #include <libusb.h>
25 #include <string>
26 
27 #include "ola/base/Macro.h"
28 
29 namespace ola {
30 namespace plugin {
31 namespace usbdmx {
32 
37  public:
39  std::string manufacturer;
40  std::string product;
41  std::string serial;
42  };
43 
44  virtual ~LibUsbAdaptor() {}
45 
46  // Device handling and enumeration
47 
53  virtual libusb_device* RefDevice(libusb_device *dev) = 0;
54 
59  virtual void UnrefDevice(libusb_device *dev) = 0;
60 
67  virtual bool OpenDevice(libusb_device *usb_device,
68  libusb_device_handle **usb_handle) = 0;
69 
78  virtual bool OpenDeviceAndClaimInterface(
79  libusb_device *usb_device,
80  int interface,
81  libusb_device_handle **usb_handle) = 0;
82 
87  virtual void Close(libusb_device_handle *usb_handle) = 0;
88 
102  virtual int SetConfiguration(libusb_device_handle *dev,
103  int configuration) = 0;
104 
117  virtual int ClaimInterface(libusb_device_handle *dev,
118  int interface_number) = 0;
119 
133  virtual int DetachKernelDriver(libusb_device_handle *dev,
134  int interface_number) = 0;
135 
136  // USB descriptors
137 
148  virtual int GetActiveConfigDescriptor(
149  libusb_device *dev,
150  struct libusb_config_descriptor **config) = 0;
151 
163  virtual int GetConfigDescriptor(libusb_device *dev,
164  uint8_t config_index,
165  struct libusb_config_descriptor **config) = 0;
166 
171  virtual void FreeConfigDescriptor(
172  struct libusb_config_descriptor *config) = 0;
173 
174  // Asynchronous device I/O
175 
181  virtual struct libusb_transfer* AllocTransfer(int iso_packets) = 0;
182 
187  virtual void FreeTransfer(struct libusb_transfer *transfer) = 0;
188 
199  virtual int SubmitTransfer(struct libusb_transfer *transfer) = 0;
200 
209  virtual int CancelTransfer(struct libusb_transfer *transfer) = 0;
210 
222  virtual void FillControlSetup(unsigned char *buffer,
223  uint8_t bmRequestType,
224  uint8_t bRequest,
225  uint16_t wValue,
226  uint16_t wIndex,
227  uint16_t wLength) = 0;
228 
240  virtual void FillControlTransfer(struct libusb_transfer *transfer,
241  libusb_device_handle *dev_handle,
242  unsigned char *buffer,
243  libusb_transfer_cb_fn callback,
244  void *user_data,
245  unsigned int timeout) = 0;
246 
260  virtual void FillBulkTransfer(struct libusb_transfer *transfer,
261  libusb_device_handle *dev_handle,
262  unsigned char endpoint,
263  unsigned char *buffer,
264  int length,
265  libusb_transfer_cb_fn callback,
266  void *user_data,
267  unsigned int timeout) = 0;
268 
280  virtual void FillInterruptTransfer(struct libusb_transfer *transfer,
281  libusb_device_handle *dev_handle,
282  unsigned char endpoint,
283  unsigned char *buffer,
284  int length,
285  libusb_transfer_cb_fn callback,
286  void *user_data,
287  unsigned int timeout) = 0;
288 
289  // Synchronous device I/O
290 
308  virtual int ControlTransfer(libusb_device_handle *dev_handle,
309  uint8_t bmRequestType,
310  uint8_t bRequest,
311  uint16_t wValue,
312  uint16_t wIndex,
313  unsigned char *data,
314  uint16_t wLength,
315  unsigned int timeout) = 0;
316 
327  virtual int BulkTransfer(struct libusb_device_handle *dev_handle,
328  unsigned char endpoint,
329  unsigned char *data,
330  int length,
331  int *transferred,
332  unsigned int timeout) = 0;
333 
343  virtual int InterruptTransfer(libusb_device_handle *dev_handle,
344  unsigned char endpoint,
345  unsigned char *data,
346  int length,
347  int *actual_length,
348  unsigned int timeout) = 0;
349 
350  // Static helper methods.
351 
359  static bool GetDeviceInfo(
360  struct libusb_device *usb_device,
361  const struct libusb_device_descriptor &device_descriptor,
362  DeviceInformation *device_info);
363 
370  static bool CheckManufacturer(const std::string &expected,
371  const DeviceInformation &device_info);
372 
379  static bool CheckProduct(const std::string &expected,
380  const DeviceInformation &device_info);
381 
387  static std::string ErrorCodeToString(const int error_code);
388 };
389 
394  public:
395  // Device handling and enumeration
396  libusb_device* RefDevice(libusb_device *dev);
397 
398  void UnrefDevice(libusb_device *dev);
399 
400  int SetConfiguration(libusb_device_handle *dev, int configuration);
401 
402  int ClaimInterface(libusb_device_handle *dev, int interface_number);
403 
404  int DetachKernelDriver(libusb_device_handle *dev, int interface_number);
405 
406  // USB descriptors
408  libusb_device *dev,
409  struct libusb_config_descriptor **config);
410 
411  int GetConfigDescriptor(libusb_device *dev,
412  uint8_t config_index,
413  struct libusb_config_descriptor **config);
414 
415  void FreeConfigDescriptor(struct libusb_config_descriptor *config);
416 
417  // Asynchronous device I/O
418  struct libusb_transfer* AllocTransfer(int iso_packets);
419 
420  void FreeTransfer(struct libusb_transfer *transfer);
421 
422  int SubmitTransfer(struct libusb_transfer *transfer);
423 
424  int CancelTransfer(struct libusb_transfer *transfer);
425 
426  void FillControlSetup(unsigned char *buffer,
427  uint8_t bmRequestType,
428  uint8_t bRequest,
429  uint16_t wValue,
430  uint16_t wIndex,
431  uint16_t wLength);
432 
433  void FillControlTransfer(struct libusb_transfer *transfer,
434  libusb_device_handle *dev_handle,
435  unsigned char *buffer,
436  libusb_transfer_cb_fn callback,
437  void *user_data,
438  unsigned int timeout);
439 
440  void FillBulkTransfer(struct libusb_transfer *transfer,
441  libusb_device_handle *dev_handle,
442  unsigned char endpoint,
443  unsigned char *buffer,
444  int length,
445  libusb_transfer_cb_fn callback,
446  void *user_data,
447  unsigned int timeout);
448 
449  void FillInterruptTransfer(struct libusb_transfer *transfer,
450  libusb_device_handle *dev_handle,
451  unsigned char endpoint,
452  unsigned char *buffer,
453  int length,
454  libusb_transfer_cb_fn callback,
455  void *user_data,
456  unsigned int timeout);
457 
458  // Synchronous device I/O
459  int ControlTransfer(libusb_device_handle *dev_handle,
460  uint8_t bmRequestType,
461  uint8_t bRequest,
462  uint16_t wValue,
463  uint16_t wIndex,
464  unsigned char *data,
465  uint16_t wLength,
466  unsigned int timeout);
467 
468  int BulkTransfer(struct libusb_device_handle *dev_handle,
469  unsigned char endpoint,
470  unsigned char *data,
471  int length,
472  int *transferred,
473  unsigned int timeout);
474 
475  int InterruptTransfer(libusb_device_handle *dev_handle,
476  unsigned char endpoint,
477  unsigned char *data,
478  int length,
479  int *actual_length,
480  unsigned int timeout);
481 };
482 
490  public:
492 
493  bool OpenDevice(libusb_device *usb_device,
494  libusb_device_handle **usb_handle);
495 
496  bool OpenDeviceAndClaimInterface(libusb_device *usb_device,
497  int interface,
498  libusb_device_handle **usb_handle);
499 
500  void Close(libusb_device_handle *usb_handle);
501 
502  private:
503  DISALLOW_COPY_AND_ASSIGN(SyncronousLibUsbAdaptor);
504 };
505 
513  public:
514  explicit AsyncronousLibUsbAdaptor(class LibUsbThread *thread)
515  : m_thread(thread) {
516  }
517 
518  bool OpenDevice(libusb_device *usb_device,
519  libusb_device_handle **usb_handle);
520 
521  bool OpenDeviceAndClaimInterface(libusb_device *usb_device,
522  int interface,
523  libusb_device_handle **usb_handle);
524 
525  void Close(libusb_device_handle *usb_handle);
526 
527  int ControlTransfer(libusb_device_handle *dev_handle,
528  uint8_t bmRequestType,
529  uint8_t bRequest,
530  uint16_t wValue,
531  uint16_t wIndex,
532  unsigned char *data,
533  uint16_t wLength,
534  unsigned int timeout);
535 
536  int BulkTransfer(struct libusb_device_handle *dev_handle,
537  unsigned char endpoint,
538  unsigned char *data,
539  int length,
540  int *transferred,
541  unsigned int timeout);
542 
543  int InterruptTransfer(libusb_device_handle *dev_handle,
544  unsigned char endpoint,
545  unsigned char *data,
546  int length,
547  int *actual_length,
548  unsigned int timeout);
549 
550  private:
551  class LibUsbThread *m_thread;
552 
554 };
555 } // namespace usbdmx
556 } // namespace plugin
557 } // namespace ola
558 #endif // PLUGINS_USBDMX_LIBUSBADAPTOR_H_