usb_device_libusb.cpp 4.3 KB

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