usb_device_handle_win.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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 "antmessage.h"
  11. #include "usb_device_handle.hpp"
  12. #include "usb_device_handle_si.hpp"
  13. #include "usb_device_handle_libusb.hpp"
  14. #include "usb_device_si.hpp"
  15. #include "usb_device_libusb.hpp"
  16. #include "usb_device_list.hpp"
  17. BOOL SiDeviceMatch(const USBDeviceSI* const & pclDevice_)
  18. {
  19. USHORT usVid = pclDevice_->GetVid();
  20. return (usVid == USBDeviceHandle::USB_ANT_VID);
  21. }
  22. BOOL LibusbDeviceMatch(const USBDeviceLibusb* const & pclDevice_)
  23. {
  24. //!!Can also find device by it's description string?
  25. USHORT usVid = pclDevice_->GetVid();
  26. return (usVid == USBDeviceHandle::USB_ANT_VID || usVid == USBDeviceHandle::USB_ANT_VID_TWO);
  27. }
  28. BOOL USBDeviceHandle::CopyANTDevice(const USBDevice*& pclUSBDeviceCopy_, const USBDevice* pclUSBDeviceOrg_)
  29. {
  30. if (pclUSBDeviceOrg_ == NULL)
  31. return FALSE;
  32. if (pclUSBDeviceCopy_ != NULL)
  33. return FALSE;
  34. switch(pclUSBDeviceOrg_->GetDeviceType())
  35. {
  36. case DeviceType::SI_LABS:
  37. {
  38. const USBDeviceSI& clDeviceSi = dynamic_cast<const USBDeviceSI&>(*pclUSBDeviceOrg_);
  39. pclUSBDeviceCopy_ = new USBDeviceSI(clDeviceSi);
  40. break;
  41. }
  42. case DeviceType::LIBUSB:
  43. {
  44. const USBDeviceLibusb& clDeviceLibusb = dynamic_cast<const USBDeviceLibusb&>(*pclUSBDeviceOrg_);
  45. pclUSBDeviceCopy_ = new USBDeviceLibusb(clDeviceLibusb);
  46. break;
  47. }
  48. default:
  49. {
  50. return FALSE;
  51. }
  52. }
  53. return TRUE;
  54. }
  55. //!!Just here temporarily until we get ANTDeviceList to do it.
  56. const ANTDeviceList USBDeviceHandle::GetAllDevices(ULONG ulDeviceTypeField_)
  57. {
  58. ANTDeviceList clDeviceList;
  59. if( (ulDeviceTypeField_ & DeviceType::SI_LABS) != 0)
  60. {
  61. const USBDeviceListSI clDeviceSIList = USBDeviceHandleSI::GetAllDevices(); //!!There is a list copy here!
  62. clDeviceList.Add(clDeviceSIList.GetSubList(SiDeviceMatch) );
  63. }
  64. if( (ulDeviceTypeField_ & DeviceType::LIBUSB) != 0)
  65. {
  66. const USBDeviceListLibusb clDeviceLibusbList = USBDeviceHandleLibusb::GetAllDevices(); //!!There is a list copy here!
  67. clDeviceList.Add(clDeviceLibusbList.GetSubList(LibusbDeviceMatch) );
  68. }
  69. return clDeviceList;
  70. }
  71. //!!Just here temporarily until we get ANTDeviceList to do it.
  72. const ANTDeviceList USBDeviceHandle::GetAvailableDevices(ULONG ulDeviceTypeField_)
  73. {
  74. ANTDeviceList clDeviceList;
  75. if( (ulDeviceTypeField_ & DeviceType::SI_LABS) != 0)
  76. {
  77. const USBDeviceListSI clDeviceSIList = USBDeviceHandleSI::GetAvailableDevices(); //!!There is a list copy here!
  78. clDeviceList.Add(clDeviceSIList.GetSubList(SiDeviceMatch) );
  79. }
  80. if( (ulDeviceTypeField_ & DeviceType::LIBUSB) != 0)
  81. {
  82. const USBDeviceListLibusb clDeviceLibusbList = USBDeviceHandleLibusb::GetAvailableDevices(); //!!There is a list copy here!
  83. clDeviceList.Add(clDeviceLibusbList.GetSubList(LibusbDeviceMatch) );
  84. }
  85. return clDeviceList;
  86. }
  87. //!!Polymorphism would be easier to implement!
  88. BOOL USBDeviceHandle::Open(const USBDevice& clDevice_, USBDeviceHandle*& pclDeviceHandle_, ULONG ulBaudRate_)
  89. {
  90. //dynamic_cast does not handle *& types
  91. BOOL bSuccess;
  92. switch(clDevice_.GetDeviceType())
  93. {
  94. case DeviceType::SI_LABS:
  95. {
  96. const USBDeviceSI& clDeviceSi = dynamic_cast<const USBDeviceSI&>(clDevice_);
  97. USBDeviceHandleSI* pclDeviceHandleSi;
  98. bSuccess = USBDeviceHandleSI::Open(clDeviceSi, pclDeviceHandleSi, ulBaudRate_);
  99. //pclDeviceHandle_ = dynamic_cast<USBDeviceHandle*>(pclDeviceHandleSi);
  100. pclDeviceHandle_ = pclDeviceHandleSi;
  101. break;
  102. }
  103. case DeviceType::LIBUSB:
  104. {
  105. const USBDeviceLibusb& clDeviceLibusb = dynamic_cast<const USBDeviceLibusb&>(clDevice_);
  106. USBDeviceHandleLibusb* pclDeviceHandleLibusb;
  107. bSuccess = USBDeviceHandleLibusb::Open(clDeviceLibusb, pclDeviceHandleLibusb);
  108. //pclDeviceHandle_ = dynamic_cast<USBDeviceHandle*>(pclDeviceHandleLibusb);
  109. pclDeviceHandle_ = pclDeviceHandleLibusb;
  110. break;
  111. }
  112. default:
  113. {
  114. pclDeviceHandle_ = NULL;
  115. bSuccess = FALSE;
  116. break;
  117. }
  118. }
  119. return bSuccess;
  120. }
  121. BOOL USBDeviceHandle::Close(USBDeviceHandle*& pclDeviceHandle_, BOOL bReset_)
  122. {
  123. if(pclDeviceHandle_ == NULL)
  124. return FALSE;
  125. //dynamic_cast does not handle *& types
  126. BOOL bSuccess;
  127. switch(pclDeviceHandle_->GetDevice().GetDeviceType())
  128. {
  129. case DeviceType::SI_LABS:
  130. {
  131. USBDeviceHandleSI* pclDeviceHandleSi = reinterpret_cast<USBDeviceHandleSI*>(pclDeviceHandle_);
  132. bSuccess = USBDeviceHandleSI::Close(pclDeviceHandleSi, bReset_);
  133. pclDeviceHandle_ = pclDeviceHandleSi;
  134. break;
  135. }
  136. case DeviceType::LIBUSB:
  137. {
  138. USBDeviceHandleLibusb* pclDeviceHandleLibusb = reinterpret_cast<USBDeviceHandleLibusb*>(pclDeviceHandle_);
  139. bSuccess = USBDeviceHandleLibusb::Close(pclDeviceHandleLibusb, bReset_);
  140. pclDeviceHandle_ = pclDeviceHandleLibusb;
  141. break;
  142. }
  143. default:
  144. {
  145. pclDeviceHandle_ = NULL;
  146. bSuccess = FALSE;
  147. break;
  148. }
  149. }
  150. return bSuccess;
  151. }
  152. #endif //defined(DSI_TYPES_WINDOWS)