usb_device_handle_si.hpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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_SI_HPP
  9. #define USB_DEVICE_HANDLE_SI_HPP
  10. #include "types.h"
  11. #include "dsi_thread.h"
  12. #include "macros.h"
  13. #include "usb_device_handle.hpp"
  14. #include "usb_device_si.hpp"
  15. #include "usb_device_list.hpp"
  16. #include "dsi_silabs_library.hpp"
  17. #include "dsi_cm_library.hpp"
  18. #include "dsi_ts_queue.hpp"
  19. #include <windows.h>
  20. #include <memory>
  21. //////////////////////////////////////////////////////////////////////////////////
  22. // Public Definitions
  23. //////////////////////////////////////////////////////////////////////////////////
  24. typedef USBDeviceList<const USBDeviceSI*> USBDeviceListSI;
  25. //////////////////////////////////////////////////////////////////////////////////
  26. // Public Class Prototypes
  27. //////////////////////////////////////////////////////////////////////////////////
  28. class USBDeviceHandleSI : public USBDeviceHandle
  29. {
  30. private:
  31. // Dynamic Libraries
  32. SiLabsLibrary clSiLibrary;
  33. CMLibrary clCmLibrary;
  34. TSQueue<char> clRxQueue;
  35. // USB Variables
  36. HANDLE hUSBEvent;
  37. HANDLE hUSBDeviceHandle; // Handle to the USB device.
  38. UCHAR ucCTSCode; //!!Can we get rid of these and just use local variables?
  39. UCHAR ucRTSCode;
  40. UCHAR ucDTRCode;
  41. UCHAR ucDSRCode;
  42. UCHAR ucDCDCode;
  43. // Thread Variables
  44. DSI_THREAD_ID hReceiveThread; // Handle for the receive thread.
  45. DSI_MUTEX stMutexCriticalSection; // Mutex used with the wait condition
  46. DSI_CONDITION_VAR stEventReceiveThreadExit; // Event to signal the receive thread has ended.
  47. BOOL bStopReceiveThread; // Flag to stop the receive thread.
  48. // Device Variables
  49. const USBDeviceSI clDevice;
  50. const ULONG ulBaudRate;
  51. BOOL bDeviceGone;
  52. // Private Member Functions
  53. BOOL POpen();
  54. void PClose(BOOL bReset_ = FALSE);
  55. void ReceiveThread();
  56. static DSI_THREAD_RETURN ProcessThread(void *pvParameter_);
  57. BOOL DeviceIsGone();
  58. static USBDeviceList<const USBDeviceSI> clDeviceList; //This holds only instances of USBDeviceSI (unless someone manually makes their own)
  59. //!!Const-correctness!
  60. public:
  61. static const USBDeviceListSI GetAllDevices(); //!!List copy! //!!Should we make the list static instead and return a reference to it?
  62. static const USBDeviceListSI GetAvailableDevices(); //!!List copy!
  63. static BOOL Open(const USBDeviceSI& clDevice_, USBDeviceHandleSI*& pclDeviceHandle_, ULONG ulBaudRate_); //should these be member functions?
  64. static BOOL Close(USBDeviceHandleSI*& pclDeviceHandle_, BOOL bReset_ = FALSE);
  65. static BOOL TryOpen(const USBDeviceSI& clDevice_);
  66. static UCHAR GetNumberOfDevices();
  67. // Methods inherited from the base class:
  68. USBError::Enum Write(void* pvData_, ULONG ulSize_, ULONG& ulBytesWritten_);
  69. USBError::Enum Read(void* pvData_, ULONG ulSize_, ULONG& ulBytesRead_, ULONG ulWaitTime_);
  70. const USBDevice& GetDevice() { return clDevice; }
  71. protected:
  72. USBDeviceHandleSI(const USBDeviceSI& clDevice_, ULONG ulBaudRate_);
  73. virtual ~USBDeviceHandleSI();
  74. const USBDeviceHandleSI& operator=(const USBDeviceHandleSI& clDevicehandle_) { return clDevicehandle_; } //!!NOP
  75. };
  76. #endif // !defined(USB_DEVICE_HANDLE_SI_HPP)