usb_device_libusb.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. #if defined(_MSC_VER)
  11. #include "WinDevice.h"
  12. #endif
  13. #include "usb_device_libusb.hpp"
  14. #include "macros.h"
  15. #include "dsi_libusb_library.hpp"
  16. #include <memory>
  17. using namespace std;
  18. USBDeviceLibusb::USBDeviceLibusb(struct usb_device& stDevice_)
  19. :
  20. pstDevice(&stDevice_),
  21. usVid(stDevice_.descriptor.idVendor),
  22. usPid(stDevice_.descriptor.idProduct),
  23. ulSerialNumber(0)
  24. {
  25. szProductDescription[0] = '\0';
  26. szSerialString[0] = '\0';
  27. //Get a reference to library
  28. auto_ptr<const LibusbLibrary> pclAutoLibusbLibrary(NULL);
  29. if(LibusbLibrary::Load(pclAutoLibusbLibrary) == FALSE)
  30. return;
  31. const LibusbLibrary& clLibusbLibrary = *pclAutoLibusbLibrary;
  32. usb_dev_handle* pstTempDeviceHandle = clLibusbLibrary.Open(&stDevice_); //We can open the device to get the info even if someone else is using it, so this is okay.
  33. if(pstTempDeviceHandle == NULL)
  34. return;
  35. int ret = clLibusbLibrary.GetStringSimple(pstTempDeviceHandle, (clLibusbLibrary.Device(pstTempDeviceHandle))->descriptor.iProduct, (char*)szProductDescription, sizeof(szProductDescription));
  36. if(ret < 0)
  37. {
  38. szProductDescription[0] = '\0';
  39. }
  40. ret = clLibusbLibrary.GetStringSimple(pstTempDeviceHandle, (clLibusbLibrary.Device(pstTempDeviceHandle))->descriptor.iSerialNumber, (char*)szSerialString, sizeof(szSerialString));
  41. if(ret < 0)
  42. {
  43. szSerialString[0] = '\0';
  44. ulSerialNumber = 0;
  45. }
  46. else
  47. {
  48. USBDeviceLibusb::GetDeviceSerialNumber(ulSerialNumber);
  49. }
  50. clLibusbLibrary.Close(pstTempDeviceHandle);
  51. return;
  52. }
  53. USBDeviceLibusb::USBDeviceLibusb(const USBDeviceLibusb& clDevice_)
  54. :
  55. pstDevice(clDevice_.pstDevice),
  56. usVid(clDevice_.pstDevice->descriptor.idVendor),
  57. usPid(clDevice_.pstDevice->descriptor.idProduct),
  58. ulSerialNumber(clDevice_.ulSerialNumber)
  59. {
  60. STRNCPY((char*)szProductDescription, (char*)clDevice_.szProductDescription, sizeof(szProductDescription));
  61. memcpy(szSerialString, clDevice_.szSerialString, sizeof(szSerialString));
  62. return;
  63. }
  64. USBDeviceLibusb& USBDeviceLibusb::operator=(const USBDeviceLibusb& clDevice_)
  65. {
  66. if(this == &clDevice_)
  67. return *this;
  68. pstDevice = clDevice_.pstDevice;
  69. usVid = clDevice_.pstDevice->descriptor.idVendor;
  70. usPid = clDevice_.pstDevice->descriptor.idProduct;
  71. ulSerialNumber = clDevice_.ulSerialNumber;
  72. STRNCPY((char*)szProductDescription, (char*)clDevice_.szProductDescription, sizeof(szProductDescription));
  73. memcpy(szSerialString, clDevice_.szSerialString, sizeof(szSerialString));
  74. return *this;
  75. }
  76. BOOL USBDeviceLibusb::USBReset() const //!!make static?
  77. {
  78. #if defined(_MSC_VER)
  79. // !! Borland builder chokes on cfgmgr32
  80. TCHAR line1[64];
  81. TCHAR* argv_[2];
  82. //Only do the soft reset if we fail to open a device or it seems like theres no deivces to open (device in a "hosed" state)
  83. //The soft reset will have no effect on devices currently opened by other applications.
  84. argv_[0] = line1;
  85. SNPRINTF(&(line1[0]),sizeof(line1), "@USB\\VID_%04X&PID_%04X\\*", usVid, usPid);
  86. WinDevice_Disable(1,argv_);
  87. WinDevice_Enable(1,argv_);
  88. #endif
  89. return TRUE;
  90. }
  91. BOOL USBDeviceLibusb::GetProductDescription(UCHAR* pucProductDescription_, USHORT usBufferSize_) const
  92. {
  93. return(STRNCPY((char*) pucProductDescription_, (char*) szProductDescription, usBufferSize_));
  94. }
  95. BOOL USBDeviceLibusb::GetSerialString(UCHAR* pucSerialString_, USHORT usBufferSize_) const
  96. {
  97. if(sizeof(szSerialString) > usBufferSize_)
  98. {
  99. memcpy(pucSerialString_, szSerialString, usBufferSize_);
  100. return FALSE;
  101. }
  102. memcpy(pucSerialString_, szSerialString, sizeof(szSerialString));
  103. return TRUE;
  104. }
  105. //The serial number actually is not limited to a ULONG by USB specs,
  106. //so, our range here is determined by whatever we do in our products.
  107. //For now we have it defined as 1 to (ULONG_MAX-1)
  108. BOOL USBDeviceLibusb::GetDeviceSerialNumber(ULONG& ulSerialNumber_)
  109. {
  110. ULONG ulSerial = strtoul((char*)szSerialString, NULL, 10);
  111. if(ulSerial == 0 || ulSerial == ULONG_MAX)
  112. return FALSE;
  113. ulSerialNumber_ = ulSerial;
  114. return TRUE;
  115. }
  116. #endif //defined(DSI_TYPES_WINDOWS)