dsi_libusb_library.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include "types.h"
  9. #if defined(DSI_TYPES_WINDOWS)
  10. #include "dsi_libusb_library.hpp"
  11. #include <memory>
  12. using namespace std;
  13. #define LIBRARY_NAME "libusb0.dll"
  14. //Static variable initializations
  15. std::auto_ptr<LibusbLibrary> LibusbLibrary::clAutoInstance(NULL);
  16. BOOL LibusbLibrary::bStaticSet = FALSE;
  17. //return an auto_ptr?
  18. BOOL LibusbLibrary::Load(auto_ptr<const LibusbLibrary>& clAutoLibrary_) //!!Should we lose the dependency on auto_ptr and just return the pointer (and let the user make their own auto_ptr)?
  19. {
  20. try
  21. {
  22. clAutoLibrary_.reset(new LibusbLibrary());
  23. }
  24. catch(...)
  25. {
  26. clAutoLibrary_.reset(NULL);
  27. return FALSE;
  28. }
  29. return TRUE;
  30. }
  31. LibusbLibrary::LibusbLibrary() //throw(LibusbError::Enum&)
  32. :
  33. ClaimInterface(NULL),
  34. GetBusses(NULL),
  35. ClearHalt(NULL),
  36. GetStringSimple(NULL),
  37. Close(NULL),
  38. Reset(NULL),
  39. Init(NULL),
  40. FindBusses(NULL),
  41. FindDevices(NULL),
  42. Open(NULL),
  43. ReleaseInterface(NULL),
  44. InterruptWrite(NULL),
  45. BulkRead(NULL),
  46. Device(NULL),
  47. SetConfiguration(NULL),
  48. SetDebug(NULL),
  49. BulkSetupAsync(NULL),
  50. SubmitAsync(NULL),
  51. ReapAsyncNocancel(NULL),
  52. CancelAsync(NULL),
  53. FreeAsync(NULL),
  54. hLibHandle(NULL)
  55. {
  56. //Check static instance
  57. if(bStaticSet == FALSE)
  58. {
  59. bStaticSet = TRUE;
  60. try
  61. {
  62. clAutoInstance.reset(new LibusbLibrary());
  63. }
  64. catch(...)
  65. {
  66. bStaticSet = FALSE;
  67. throw;
  68. }
  69. }
  70. //load library
  71. LibusbError::Enum ret = LoadFunctions();
  72. if(ret != LibusbError::NONE)
  73. throw(ret);
  74. return;
  75. }
  76. LibusbLibrary::~LibusbLibrary() throw()
  77. {
  78. FreeFunctions();
  79. }
  80. ///////////////////////////////////////////////////////////////////////
  81. // Loads USB interface functions from the DLLs.
  82. ///////////////////////////////////////////////////////////////////////
  83. LibusbError::Enum LibusbLibrary::LoadFunctions()
  84. {
  85. hLibHandle = LoadLibrary(LIBRARY_NAME);
  86. if(hLibHandle == NULL)
  87. return LibusbError::NO_LIBRARY;
  88. BOOL bStatus = TRUE;
  89. ClaimInterface = (ClaimInterface_t)GetProcAddress(hLibHandle, "usb_claim_interface");
  90. if(ClaimInterface == NULL)
  91. bStatus = FALSE;
  92. GetBusses = (GetBusses_t)GetProcAddress(hLibHandle, "usb_get_busses");
  93. if(GetBusses == NULL)
  94. bStatus = FALSE;
  95. ClearHalt = (ClearHalt_t)GetProcAddress(hLibHandle, "usb_clear_halt");
  96. if(ClearHalt == NULL)
  97. bStatus = FALSE;
  98. GetStringSimple = (GetStringSimple_t)GetProcAddress(hLibHandle, "usb_get_string_simple");
  99. if(GetStringSimple == NULL)
  100. bStatus = FALSE;
  101. Close = (Close_t)GetProcAddress(hLibHandle, "usb_close");
  102. if(Close == NULL)
  103. bStatus = FALSE;
  104. Reset = (Reset_t)GetProcAddress(hLibHandle, "usb_reset");
  105. if(Reset == NULL)
  106. bStatus = FALSE;
  107. Init = (Init_t)GetProcAddress(hLibHandle, "usb_init");
  108. if(Init == NULL)
  109. bStatus = FALSE;
  110. FindBusses = (FindBusses_t)GetProcAddress(hLibHandle, "usb_find_busses");
  111. if(FindBusses == NULL)
  112. bStatus = FALSE;
  113. FindDevices = (FindDevices_t)GetProcAddress(hLibHandle, "usb_find_devices");
  114. if(FindDevices == NULL)
  115. bStatus = FALSE;
  116. Open = (Open_t)GetProcAddress(hLibHandle, "usb_open");
  117. if(Open == NULL)
  118. bStatus = FALSE;
  119. ReleaseInterface = (ReleaseInterface_t)GetProcAddress(hLibHandle, "usb_release_interface");
  120. if(ReleaseInterface == NULL)
  121. bStatus = FALSE;
  122. InterruptWrite = (InterruptWrite_t)GetProcAddress(hLibHandle, "usb_interrupt_write");
  123. if(InterruptWrite == NULL)
  124. bStatus = FALSE;
  125. BulkRead = (BulkRead_t)GetProcAddress(hLibHandle, "usb_bulk_read");
  126. if(BulkRead == NULL)
  127. bStatus = FALSE;
  128. Device = (Device_t)GetProcAddress(hLibHandle, "usb_device");
  129. if(Device == NULL)
  130. bStatus = FALSE;
  131. SetConfiguration = (SetConfiguration_t)GetProcAddress(hLibHandle, "usb_set_configuration");
  132. if(SetConfiguration == NULL)
  133. bStatus = FALSE;
  134. SetDebug = (SetDebug_t)GetProcAddress(hLibHandle, "usb_set_debug");
  135. if(SetDebug == NULL)
  136. bStatus = FALSE;
  137. //Libusb-win32 only asynch functions
  138. BulkSetupAsync = (BulkSetupAsync_t)GetProcAddress(hLibHandle, "usb_bulk_setup_async");
  139. if(BulkSetupAsync == NULL)
  140. bStatus = FALSE;
  141. SubmitAsync = (SubmitAsync_t)GetProcAddress(hLibHandle, "usb_submit_async");
  142. if(SubmitAsync == NULL)
  143. bStatus = FALSE;
  144. ReapAsyncNocancel = (ReapAsyncNocancel_t)GetProcAddress(hLibHandle, "usb_reap_async_nocancel");
  145. if(ReapAsyncNocancel == NULL)
  146. bStatus = FALSE;
  147. CancelAsync = (CancelAsync_t)GetProcAddress(hLibHandle, "usb_cancel_async");
  148. if(CancelAsync == NULL)
  149. bStatus = FALSE;
  150. FreeAsync = (FreeAsync_t)GetProcAddress(hLibHandle, "usb_free_async");
  151. if(CancelAsync == NULL)
  152. bStatus = FALSE;
  153. if(bStatus == FALSE)
  154. {
  155. FreeFunctions();
  156. return LibusbError::NO_FUNCTION;
  157. }
  158. return LibusbError::NONE;
  159. }
  160. ///////////////////////////////////////////////////////////////////////
  161. // Unloads USB DLLs.
  162. ///////////////////////////////////////////////////////////////////////
  163. void LibusbLibrary::FreeFunctions()
  164. {
  165. if(hLibHandle != NULL)
  166. {
  167. FreeLibrary(hLibHandle);
  168. hLibHandle = NULL;
  169. }
  170. return;
  171. }
  172. #endif //defined(DSI_TYPES_WINDOWS)