dsi_ant_device.hpp 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2014
  6. All rights reserved.
  7. */
  8. #if !defined(DSI_ANT_DEVICE_HPP)
  9. #define DSI_ANT_DEVICE_HPP
  10. #include "types.h"
  11. #include "dsi_thread.h"
  12. #include "dsi_framer_ant.hpp"
  13. #include "dsi_serial_generic.hpp"
  14. #include "dsi_debug.hpp"
  15. #include "antmessage.h"
  16. #include "dsi_ant_message_processor.hpp"
  17. #include "dsi_response_queue.hpp"
  18. #define MAX_ANT_CHANNELS 8
  19. //////////////////////////////////////////////////////////////////////////////////
  20. // Public Definitions
  21. //////////////////////////////////////////////////////////////////////////////////
  22. typedef enum
  23. {
  24. ANT_USB_RESPONSE_NONE = 0,
  25. ANT_USB_RESPONSE_OPEN_PASS,
  26. ANT_USB_RESPONSE_OPEN_FAIL,
  27. ANT_USB_RESPONSE_SERIAL_FAIL
  28. } ANT_USB_RESPONSE;
  29. // ANT-FS Host States
  30. typedef enum
  31. {
  32. ANT_USB_STATE_OFF = 0,
  33. ANT_USB_STATE_IDLE,
  34. ANT_USB_STATE_IDLE_POLLING_USB,
  35. ANT_USB_STATE_OPEN
  36. } ANT_USB_STATE;
  37. //////////////////////////////////////////////////////////////////////////////////
  38. // Public Class Prototypes
  39. //////////////////////////////////////////////////////////////////////////////////
  40. /////////////////////////////////////////////////////////////////
  41. // This class handles the connection to the USB stick, polls for
  42. // ANT messages and dispatches them to appropriate handlers (e.g.
  43. // ANTFSClientChannel or ANTFSHostChannel) for processing
  44. //
  45. // IMPORTANT: this class is under development and subject to change.
  46. // It is not recommended to use this class for custom applications.
  47. /////////////////////////////////////////////////////////////////
  48. class DSIANTDevice
  49. {
  50. private:
  51. //////////////////////////////////////////////////////////////////////////////////
  52. // Private Definitions
  53. //////////////////////////////////////////////////////////////////////////////////
  54. enum ENUM_REQUEST
  55. {
  56. REQUEST_NONE = 0,
  57. REQUEST_OPEN,
  58. REQUEST_HANDLE_SERIAL_ERROR,
  59. REQUEST_SERIAL_ERROR_HANDLED
  60. };
  61. enum RETURN_STATUS
  62. {
  63. RETURN_FAIL = 0,
  64. RETURN_PASS,
  65. RETURN_STOP,
  66. RETURN_REJECT,
  67. RETURN_NA,
  68. RETURN_SERIAL_ERROR
  69. };
  70. //////////////////////////////////////////////////////////////////////////////////
  71. // Private Variables
  72. //////////////////////////////////////////////////////////////////////////////////
  73. DSIResponseQueue<ANT_USB_RESPONSE> clResponseQueue;
  74. volatile BOOL bOpened;
  75. BOOL bInitFailed;
  76. DSI_THREAD_ID hRequestThread; // Handle for the request thread.
  77. DSI_THREAD_ID hReceiveThread; // Handle for the receive thread.
  78. DSI_MUTEX stMutexResponseQueue; // Mutex used with the response queue
  79. DSI_MUTEX stMutexCriticalSection; // Mutex used with the wait condition
  80. DSI_MUTEX stMutexChannelListAccess; // Mutex used with the channel list
  81. DSI_CONDITION_VAR stCondReceiveThreadExit; // Event to signal the receive thread has ended.
  82. DSI_CONDITION_VAR stCondRequestThreadExit; // Event to signal the request thread has ended.
  83. DSI_CONDITION_VAR stCondRequest; // Event to signal there is a new request
  84. DSI_CONDITION_VAR stCondWaitForResponse; // Event to signal there is a new response to the application
  85. volatile BOOL bKillThread;
  86. volatile BOOL bReceiveThreadRunning;
  87. volatile BOOL bRequestThreadRunning;
  88. volatile BOOL bCancel;
  89. UCHAR ucUSBDeviceNum;
  90. USHORT usBaudRate;
  91. ULONG ulUSBSerialNumber;
  92. volatile BOOL bAutoInit;
  93. volatile BOOL bPollUSB;
  94. DSIANTMessageProcessor* apclChannelList[MAX_ANT_CHANNELS];
  95. DSISerialGeneric *pclSerialObject;
  96. DSIFramerANT *pclANT;
  97. volatile ANT_USB_STATE eState;
  98. volatile ENUM_REQUEST eRequest;
  99. //////////////////////////////////////////////////////////////////////////////////
  100. // Private Function Prototypes
  101. //////////////////////////////////////////////////////////////////////////////////
  102. BOOL ReInitDevice(void);
  103. BOOL AttemptOpen(void);
  104. void CloseDevice(BOOL bReset_);
  105. void HandleSerialError();
  106. void RequestThread(void);
  107. static DSI_THREAD_RETURN RequestThreadStart(void *pvParameter_);
  108. void ReceiveThread(void);
  109. static DSI_THREAD_RETURN ReceiveThreadStart(void *pvParameter_);
  110. void AddResponse(ANT_USB_RESPONSE eResponse_);
  111. public:
  112. DSIANTDevice();
  113. ~DSIANTDevice();
  114. BOOL Init(BOOL bPollUSB_ = FALSE);
  115. /////////////////////////////////////////////////////////////////
  116. // Begins to initialize the DSIANTDevice object
  117. // Returns TRUE if successful. Otherwise, it returns FALSE.
  118. // Parameters:
  119. // bPollUSB_: Specify whether to continue to retry initialization
  120. // if it fails
  121. // Operation:
  122. // If bPollUSB_ is set to TRUE and it fails to initialize the USBstick,
  123. // it will continue to retry the intitialization once a second until
  124. // it is sucessful. Once everything has be sucessfully initialized
  125. // a response of ANT_USB_RESPONSE_OPEN_PASS will be sent.
  126. // If bPollUSB_ is set to FALSE, and initialization fails, a
  127. // response of ANT_USB_RESPONSE_OPEN_FAIL will be sent.
  128. //
  129. // This version of Init will only work for the ANT USB stick with
  130. // USB device descriptor "ANT USB Stick"
  131. /////////////////////////////////////////////////////////////////
  132. BOOL Init(UCHAR ucUSBDeviceNum_, USHORT usBaudRate_, BOOL bPollUSB_ = FALSE);
  133. /////////////////////////////////////////////////////////////////
  134. // Begins to initialize the DSIANTDevice object
  135. // Returns TRUE if successful. Otherwise, it returns FALSE.
  136. // Parameters:
  137. // ucUSBDeviceNum_: USB port number
  138. // usBaudRate_: Serial baud rate
  139. // bPollUSB_: Specify whether to continue to retry initialization
  140. // if it fails
  141. // Operation:
  142. // If bPollUSB_ is set to TRUE and it fails to initialize the USBstick,
  143. // it will continue to retry the intitialization once a second until
  144. // it is sucessful. Once everything has be sucessfully initialized
  145. // a response of ANT_USB_RESPONSE_OPEN_PASS will be sent.
  146. // If bPollUSB_ is set to FALSE, and initialization fails, a
  147. // response of ANT_USB_RESPONSE_OPEN_FAIL will be sent.
  148. /////////////////////////////////////////////////////////////////
  149. void Close(void);
  150. /////////////////////////////////////////////////////////////////
  151. // Stops any pending actions, closes all devices down and cleans
  152. // up any resources being used by the library.
  153. /////////////////////////////////////////////////////////////////
  154. ULONG GetSerialNumber();
  155. /////////////////////////////////////////////////////////////////
  156. // Returns the serial number of the USB device
  157. /////////////////////////////////////////////////////////////////
  158. ANT_USB_STATE GetStatus(void);
  159. /////////////////////////////////////////////////////////////////
  160. // Returns the current library status.
  161. /////////////////////////////////////////////////////////////////
  162. ANT_USB_RESPONSE WaitForResponse(ULONG ulMilliseconds_);
  163. /////////////////////////////////////////////////////////////////
  164. // Wait for a response from the library
  165. // Parameters:
  166. // ulMilliseconds_: Set this value to the minimum time to
  167. // wait before returning. If the value is
  168. // 0, the function will return immediately.
  169. // If the value is DSI_THREAD_INFINITE, the
  170. // function will not time out.
  171. // If one or more responses are pending before the timeout
  172. // expires the function will return the first response that
  173. // occurred. If no response is pending at the time the timeout
  174. // expires, ANT_USB_RESPONSE_NONE is returned.
  175. /////////////////////////////////////////////////////////////////
  176. BOOL AddMessageProcessor(UCHAR ucChannelNumber_, DSIANTMessageProcessor* pclChannel_);
  177. /////////////////////////////////////////////////////////////////
  178. // Adds a message processor, to process messages for the specified
  179. // channel number.
  180. // Parameters:
  181. // ucChannelNumber_: ANT channel number
  182. // *pclChannel_: A pointer to a DSIANTMessageProcessor.
  183. // Returns TRUE if successful. Otherwise, it returns FALSE.
  184. // Operation:
  185. // After adding attaching a message processor to a channel number,
  186. // messages for the corresponding channel, are dispatched to it for
  187. // processing. Only one message processor can be added per ANT channel.
  188. /////////////////////////////////////////////////////////////////
  189. BOOL RemoveMessageProcessor(DSIANTMessageProcessor* pclChannel_);
  190. /////////////////////////////////////////////////////////////////
  191. // Unregisters a message processor, to stop processing ANT messages.
  192. // Parameters:
  193. // *pclChannel_: A pointer to a DSIANTMessageProcessor.
  194. // Returns TRUE if successful. Otherwise, it returns FALSE.
  195. // Operation:
  196. // Remove instances of classes derived of DSIANTMessageProcessor
  197. // from the list to stop processing messages for that
  198. // channel number
  199. /////////////////////////////////////////////////////////////////
  200. void ClearManagedChannelList(void);
  201. /////////////////////////////////////////////////////////////////
  202. // Remove all managed channels from the list
  203. /////////////////////////////////////////////////////////////////
  204. #if defined(DEBUG_FILE)
  205. void SetDebug(BOOL bDebugOn_, const char *pcDirectory_ = (const char*) NULL);
  206. /////////////////////////////////////////////////////////////////
  207. // Enables debug files
  208. // Parameters:
  209. // bDebugOn_: Enable/disable debug logs.
  210. // *pcDirectory_: A string to use as the path for storing
  211. // debug logs. Set to NULL to use the working
  212. // directory as the default path.
  213. /////////////////////////////////////////////////////////////////
  214. #endif
  215. };
  216. #endif // DSI_ANT_DEVICE_HPP