dsi_libusb_library.hpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except
  4. in compliance with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2016
  6. All rights reserved.
  7. */
  8. #ifndef DSI_LIBUSB_LIBRARY_HPP
  9. #define DSI_LIBUSB_LIBRARY_HPP
  10. #include "types.h"
  11. #include "usb.h"
  12. #include <memory>
  13. #include <windows.h>
  14. struct LibusbError
  15. {
  16. enum Enum
  17. {
  18. NONE,
  19. NO_LIBRARY,
  20. NO_FUNCTION
  21. };
  22. private: LibusbError();
  23. };
  24. /*
  25. * This class started out as a singleton, but you cannot control when a static variable destroys
  26. * itself at the end of an application. Therefore, the class was changed to a normal wrapper around
  27. * the library. This means that if you retrieve an instance of the class and then destroy it, you will
  28. * unload the whole library. Since we would rather the library stay loaded for the rest of the application if
  29. * we use it, there is an extra call to load the library so that the library is guaranteed to not unload until
  30. * the end of the application. As well, since each instance also calls to load the library, the library is
  31. * not freed until everyone is done using it.
  32. */
  33. //NOTE: Make sure this class isn't being depended upon by another global variable!
  34. //NOTE: Not thread-safe.
  35. class LibusbLibrary
  36. {
  37. public:
  38. //!!maybe return another smart pointer like shared_ptr?
  39. static BOOL Load(std::auto_ptr<const LibusbLibrary>& clAutoLibrary_); //!! Alternative to creating directly and having to worry about try statements
  40. /* Otherwise you'd have to do this...
  41. //Get a reference to library
  42. auto_ptr<LibusbLibrary> pclAutoSiLibrary(NULL);
  43. try { pclAutoSiLibrary.reset(new LibusbLibrary); }
  44. catch(...) { return clList; }
  45. const LibusbLibrary& clSiLibrary = *pclAutoSiLibrary;
  46. */
  47. //could do...
  48. //static const LibusbLibrary& Load(BOOL& bSuccess_);
  49. LibusbLibrary(); //throw(LibusbError::Enum)
  50. virtual ~LibusbLibrary() throw();
  51. //Prototypes for functions found in the LibUSB dll.
  52. typedef int (*ClaimInterface_t)(void*, int);
  53. typedef usb_bus* (*GetBusses_t)(void);
  54. typedef int (*ClearHalt_t)(usb_dev_handle*, unsigned int);
  55. typedef int (*GetStringSimple_t)(usb_dev_handle*,int,char*,size_t);
  56. typedef int (*Close_t)(usb_dev_handle*);
  57. typedef int (*Reset_t)(usb_dev_handle*);
  58. typedef void (*Init_t)(void);
  59. typedef int (*FindBusses_t)(void);
  60. typedef int (*FindDevices_t)(void);
  61. typedef usb_dev_handle* (*Open_t)(struct usb_device*);
  62. typedef int (*ReleaseInterface_t)(usb_dev_handle*, int);
  63. typedef int (*InterruptWrite_t)(usb_dev_handle*, int, char*, int, int);
  64. typedef int (*BulkRead_t)(usb_dev_handle*, int, char*, int, int);
  65. typedef struct usb_device* (*Device_t)(usb_dev_handle*);
  66. typedef int (*SetConfiguration_t)(usb_dev_handle*, int);
  67. typedef void (*SetDebug_t)(int);
  68. //Libusb-win32 only asynch functions
  69. typedef int (*BulkSetupAsync_t)(usb_dev_handle *dev, void **context, unsigned char ep);
  70. typedef int (*SubmitAsync_t)(void *context, char *bytes, int size);
  71. typedef int (*ReapAsyncNocancel_t)(void *context, int timeout);
  72. typedef int (*CancelAsync_t)(void *context);
  73. typedef int (*FreeAsync_t)(void ** context);
  74. ClaimInterface_t ClaimInterface;
  75. GetBusses_t GetBusses;
  76. ClearHalt_t ClearHalt;
  77. GetStringSimple_t GetStringSimple;
  78. Close_t Close;
  79. Reset_t Reset;
  80. Init_t Init;
  81. FindBusses_t FindBusses;
  82. FindDevices_t FindDevices;
  83. Open_t Open;
  84. ReleaseInterface_t ReleaseInterface;
  85. InterruptWrite_t InterruptWrite;
  86. BulkRead_t BulkRead;
  87. Device_t Device;
  88. SetConfiguration_t SetConfiguration;
  89. SetDebug_t SetDebug;
  90. //Libusb-win32 only asynch functions
  91. BulkSetupAsync_t BulkSetupAsync;
  92. SubmitAsync_t SubmitAsync;
  93. ReapAsyncNocancel_t ReapAsyncNocancel;
  94. CancelAsync_t CancelAsync;
  95. FreeAsync_t FreeAsync;
  96. private:
  97. LibusbError::Enum LoadFunctions();
  98. void FreeFunctions();
  99. static std::auto_ptr<LibusbLibrary> clAutoInstance; //keeps the library loaded for the duration of the application
  100. //NOTE: There is no control when this gets destroyed at end of program
  101. // but it doesn't matter because it's main purpose is to keep the library loaded
  102. // during the duration of the whole application.
  103. HMODULE hLibHandle;
  104. static BOOL bStaticSet;
  105. //!!Could dynamically make all instances and push them onto a static list to delete when we get to it
  106. };
  107. #endif //DSI_LIBUSB_LIBRARY_HPP