usb_device_handle_libusb.hpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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 USB_DEVICE_HANDLE_LIBUSB_HPP
  9. #define USB_DEVICE_HANDLE_LIBUSB_HPP
  10. #include "types.h"
  11. #include "dsi_thread.h"
  12. #include "dsi_libusb_library.hpp"
  13. #include "usb_device_handle.hpp"
  14. #include "usb_device_libusb.hpp"
  15. #include "dsi_ts_queue.hpp"
  16. #include "usb_device_list.hpp"
  17. #include <deque>
  18. #include <list>
  19. //////////////////////////////////////////////////////////////////////////////////
  20. // Public Definitions
  21. //////////////////////////////////////////////////////////////////////////////////
  22. typedef USBDeviceList<const USBDeviceLibusb*> USBDeviceListLibusb;
  23. /*
  24. //for internal use only!
  25. struct SerialData
  26. {
  27. SerialData()
  28. {
  29. iSize = 0;
  30. iDataStart = 0;
  31. }
  32. char data[4096];
  33. int iSize;
  34. int iDataStart;
  35. };
  36. */
  37. //////////////////////////////////////////////////////////////////////////////////
  38. // Public Class Prototypes
  39. //////////////////////////////////////////////////////////////////////////////////
  40. class USBDeviceHandleLibusb : public USBDeviceHandle
  41. {
  42. private:
  43. LibusbLibrary clLibusbLibrary;
  44. //std::deque<SerialData*> clOverflowQueue; //used if the user does not specify a big enough array
  45. TSQueue<UCHAR> clRxQueue;
  46. const USBDeviceLibusb clDevice;
  47. usb_dev_handle* device_handle;
  48. // Thread Variables
  49. DSI_THREAD_ID hReceiveThread; // Handle for the receive thread.
  50. DSI_MUTEX stMutexCriticalSection; // Mutex used with the wait condition
  51. DSI_CONDITION_VAR stEventReceiveThreadExit; // Event to signal the receive thread has ended.
  52. BOOL bStopReceiveThread; // Flag to stop the receive thread.
  53. BOOL bDeviceGone;
  54. BOOL POpen();
  55. void PClose(BOOL bReset_ = FALSE);
  56. void ReceiveThread();
  57. static DSI_THREAD_RETURN ProcessThread(void* pvParameter_);
  58. static USBDeviceList<const USBDeviceLibusb> clDeviceList; //This holds only instances of USBDeviceLibusb (unless someone manually makes their own)
  59. //!!Const-correctness!
  60. public:
  61. static const USBDeviceListLibusb GetAllDevices(); //!!have static list?
  62. static const USBDeviceListLibusb GetAvailableDevices();
  63. static BOOL Open(const USBDeviceLibusb& clDevice_, USBDeviceHandleLibusb*& pclDeviceHandle_); //should these be member functions?
  64. static BOOL Close(USBDeviceHandleLibusb*& pclDeviceHandle_, BOOL bReset_ = FALSE);
  65. static BOOL TryOpen(const USBDeviceLibusb& clDevice_);
  66. //USBDeviceHandle Base Class//
  67. USBError::Enum Write(void* pvData_, ULONG ulSize_, ULONG& ulBytesWritten_);
  68. USBError::Enum Read(void* pvData_, ULONG ulSize_, ULONG& ulBytesRead_, ULONG ulWaitTime_);
  69. const USBDevice& GetDevice() { return clDevice; }
  70. ////
  71. protected:
  72. USBDeviceHandleLibusb(const USBDeviceLibusb& clDevice_);
  73. virtual ~USBDeviceHandleLibusb();
  74. const USBDeviceHandleLibusb& operator=(const USBDeviceHandleLibusb& clDevicehandle_) { return clDevicehandle_; } //!!NOP
  75. };
  76. #endif // !defined(USB_DEVICE_HANDLE_LIBUSB_HPP)