dsi_ant_device_polling.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. #if !defined(DSI_ANT_DEVICE_POLLING_HPP)
  9. #define DSI_ANT_DEVICE_POLLING_HPP
  10. #include "dsi_ant_device.hpp"
  11. #define MAX_ANT_CHANNELS 8
  12. //////////////////////////////////////////////////////////////////////////////////
  13. // Public Definitions
  14. //////////////////////////////////////////////////////////////////////////////////
  15. typedef enum
  16. {
  17. ANT_USB_RESPONSE_NONE = 0,
  18. ANT_USB_RESPONSE_OPEN_PASS,
  19. ANT_USB_RESPONSE_OPEN_FAIL,
  20. ANT_USB_RESPONSE_SERIAL_FAIL
  21. } ANT_USB_RESPONSE;
  22. // ANT-FS Host States
  23. typedef enum
  24. {
  25. ANT_USB_STATE_OFF = 0,
  26. ANT_USB_STATE_IDLE,
  27. ANT_USB_STATE_IDLE_POLLING_USB,
  28. ANT_USB_STATE_OPEN
  29. } ANT_USB_STATE;
  30. //////////////////////////////////////////////////////////////////////////////////
  31. // Public Class Prototypes
  32. //////////////////////////////////////////////////////////////////////////////////
  33. /////////////////////////////////////////////////////////////////
  34. // This class extends dsi_ant_device to add polling for usb devices in the case
  35. // the desired device is not plugged in or is removed/dead/etc.
  36. //
  37. // IMPORTANT: this class is under development and subject to change.
  38. // It is not recommended to use this class for custom applications.
  39. /////////////////////////////////////////////////////////////////
  40. class DSIANTDevicePolling: public DSIANTDevice
  41. {
  42. private:
  43. //////////////////////////////////////////////////////////////////////////////////
  44. // Private Definitions
  45. //////////////////////////////////////////////////////////////////////////////////
  46. enum ENUM_REQUEST
  47. {
  48. REQUEST_NONE = 0,
  49. REQUEST_OPEN,
  50. REQUEST_HANDLE_SERIAL_ERROR
  51. };
  52. enum RETURN_STATUS
  53. {
  54. RETURN_FAIL = 0,
  55. RETURN_PASS,
  56. RETURN_STOP,
  57. RETURN_REJECT,
  58. RETURN_NA,
  59. RETURN_SERIAL_ERROR
  60. };
  61. //////////////////////////////////////////////////////////////////////////////////
  62. // Private Variables
  63. //////////////////////////////////////////////////////////////////////////////////
  64. DSIResponseQueue<ANT_USB_RESPONSE> clResponseQueue;
  65. DSI_THREAD_ID hRequestThread; // Handle for the request thread.
  66. DSI_MUTEX stMutexResponseQueue; // Mutex used with the response queue
  67. DSI_CONDITION_VAR stCondRequestThreadExit; // Event to signal the request thread has ended.
  68. DSI_CONDITION_VAR stCondRequest; // Event to signal there is a new request
  69. DSI_CONDITION_VAR stCondWaitForResponse; // Event to signal there is a new response to the application
  70. volatile BOOL bRequestThreadRunning;
  71. volatile BOOL bKillReqThread;
  72. UCHAR ucUSBDeviceNum;
  73. USHORT usBaudRate;
  74. ULONG ulUSBSerialNumber;
  75. volatile BOOL bAutoInit;
  76. volatile ANT_USB_STATE eState;
  77. volatile ENUM_REQUEST eRequest;
  78. //////////////////////////////////////////////////////////////////////////////////
  79. // Private Function Prototypes
  80. //////////////////////////////////////////////////////////////////////////////////
  81. void RequestThread(void);
  82. static DSI_THREAD_RETURN RequestThreadStart(void *pvParameter_);
  83. BOOL ReInitDevice(void);
  84. BOOL AttemptOpen(void);
  85. void AddResponse(ANT_USB_RESPONSE eResponse_);
  86. void closePolling();
  87. protected:
  88. /// OVERRIDE
  89. virtual void HandleSerialError(); //<-Note needs 'virtual' because it is called internally but needs to be overridden by subclasses
  90. public:
  91. DSIANTDevicePolling();
  92. ~DSIANTDevicePolling();
  93. /////////////////////////////////////////////////////////////////
  94. /// OVERRIDE
  95. // Begins to initialize the DSIANTDevice object.
  96. // Returns TRUE if successful. Otherwise, it returns FALSE.
  97. //
  98. // Operation:
  99. // If it fails to initialize the USBstick,
  100. // it will continue to retry the intitialization once a second until
  101. // it is sucessful. Once everything has be sucessfully initialized
  102. // a response of ANT_USB_RESPONSE_OPEN_PASS will be sent.
  103. //
  104. // This version of Init will only work for the ANT USB stick with
  105. // USB device descriptor "ANT USB Stick"
  106. /////////////////////////////////////////////////////////////////
  107. virtual BOOL Open();
  108. /////////////////////////////////////////////////////////////////
  109. /// OVERRIDE
  110. // Begins to initialize the DSIANTDevice object.
  111. // Returns TRUE if successful. Otherwise, it returns FALSE.
  112. // Parameters:
  113. // ucUSBDeviceNum_: USB port number
  114. // usBaudRate_: Serial baud rate
  115. //
  116. // Operation:
  117. // If it fails to initialize the USBstick,
  118. // it will continue to retry the intitialization once a second until
  119. // it is sucessful. Once everything has be sucessfully initialized
  120. // a response of ANT_USB_RESPONSE_OPEN_PASS will be sent.
  121. /////////////////////////////////////////////////////////////////
  122. virtual BOOL Open(UCHAR ucUSBDeviceNum_, USHORT usBaudRate_);
  123. /////////////////////////////////////////////////////////////////
  124. /// OVERRIDE
  125. // Stops any pending actions, closes device down and cleans
  126. // up any resources being used by the library.
  127. /////////////////////////////////////////////////////////////////
  128. virtual void Close(void);
  129. /////////////////////////////////////////////////////////////////
  130. // Returns the current library status.
  131. /////////////////////////////////////////////////////////////////
  132. ANT_USB_STATE GetStatus(void);
  133. /////////////////////////////////////////////////////////////////
  134. // Wait for a response from the library
  135. // Parameters:
  136. // ulMilliseconds_: Set this value to the minimum time to
  137. // wait before returning. If the value is
  138. // 0, the function will return immediately.
  139. // If the value is DSI_THREAD_INFINITE, the
  140. // function will not time out.
  141. // If one or more responses are pending before the timeout
  142. // expires the function will return the first response that
  143. // occurred. If no response is pending at the time the timeout
  144. // expires, ANT_USB_RESPONSE_NONE is returned.
  145. /////////////////////////////////////////////////////////////////
  146. ANT_USB_RESPONSE WaitForResponse(ULONG ulMilliseconds_);
  147. };
  148. #endif // DSI_ANT_DEVICE_POLLING_HPP