dsi_ant_device.hpp 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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_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. //----none----
  23. //////////////////////////////////////////////////////////////////////////////////
  24. // Public Class Prototypes
  25. //////////////////////////////////////////////////////////////////////////////////
  26. /////////////////////////////////////////////////////////////////
  27. // This class handles the connection to the USB stick, polls for
  28. // ANT messages and dispatches them to appropriate handlers (e.g.
  29. // ANTFSClientChannel or ANTFSHostChannel) for processing
  30. //
  31. // IMPORTANT: this class is under development and subject to change.
  32. // It is not recommended to use this class for custom applications.
  33. /////////////////////////////////////////////////////////////////
  34. class DSIANTDevice
  35. {
  36. private:
  37. //////////////////////////////////////////////////////////////////////////////////
  38. // Private Definitions
  39. //////////////////////////////////////////////////////////////////////////////////
  40. //----none----
  41. //////////////////////////////////////////////////////////////////////////////////
  42. // Private Variables
  43. //////////////////////////////////////////////////////////////////////////////////
  44. volatile BOOL bOpened;
  45. DSI_THREAD_ID hReceiveThread; // Handle for the receive thread.
  46. DSI_MUTEX stMutexChannelListAccess; // Mutex used with the channel list
  47. DSI_CONDITION_VAR stCondReceiveThreadExit; // Event to signal the receive thread has ended.
  48. volatile BOOL bKillThread;
  49. volatile BOOL bReceiveThreadRunning;
  50. volatile BOOL bCancel;
  51. ULONG ulUSBSerialNumber;
  52. DSIANTMessageProcessor* apclChannelList[MAX_ANT_CHANNELS];
  53. DSISerialGeneric *pclSerialObject;
  54. DSIFramerANT *pclANT;
  55. protected:
  56. DSI_MUTEX stMutexCriticalSection; // Mutex used with the wait condition
  57. //////////////////////////////////////////////////////////////////////////////////
  58. // Private Function Prototypes
  59. //////////////////////////////////////////////////////////////////////////////////
  60. void DisconnectFromDevice();
  61. void ReceiveThread(void);
  62. static DSI_THREAD_RETURN ReceiveThreadStart(void *pvParameter_);
  63. BOOL ConnectToDevice(void);
  64. protected:
  65. virtual void HandleSerialError(); //<-Note needs 'virtual' because it is called internally but needs to be overridden by subclasses
  66. public:
  67. DSIANTDevice();
  68. virtual ~DSIANTDevice();
  69. /////////////////////////////////////////////////////////////////
  70. // Opens the first available ANT device.
  71. // Returns TRUE if successful. Otherwise, it returns FALSE.
  72. //
  73. // This version of Init will only work for the ANT USB stick with
  74. // USB device descriptor "ANT USB Stick"
  75. /////////////////////////////////////////////////////////////////
  76. virtual BOOL Open();
  77. /////////////////////////////////////////////////////////////////
  78. // Opens the device with the given device number and baudrate.
  79. // Returns TRUE if successful. Otherwise, it returns FALSE.
  80. // Parameters:
  81. // ucUSBDeviceNum_: USB port number
  82. // usBaudRate_: Serial baud rate
  83. /////////////////////////////////////////////////////////////////
  84. virtual BOOL Open(UCHAR ucUSBDeviceNum_, USHORT usBaudRate_);
  85. /////////////////////////////////////////////////////////////////
  86. // Stops any pending actions, closes device down and cleans
  87. // up any resources being used by the library.
  88. /////////////////////////////////////////////////////////////////
  89. virtual void Close(void);
  90. /////////////////////////////////////////////////////////////////
  91. // Adds a message processor, to process messages for the specified
  92. // channel number.
  93. // Parameters:
  94. // ucChannelNumber_: ANT channel number
  95. // *pclChannel_: A pointer to a DSIANTMessageProcessor.
  96. // Returns TRUE if successful. Otherwise, it returns FALSE.
  97. // Operation:
  98. // After adding attaching a message processor to a channel number,
  99. // messages for the corresponding channel, are dispatched to it for
  100. // processing. Only one message processor can be added per ANT channel.
  101. /////////////////////////////////////////////////////////////////
  102. BOOL AddMessageProcessor(UCHAR ucChannelNumber_, DSIANTMessageProcessor* pclChannel_);
  103. /////////////////////////////////////////////////////////////////
  104. // Unregisters a message processor, to stop processing ANT messages.
  105. // Parameters:
  106. // *pclChannel_: A pointer to a DSIANTMessageProcessor.
  107. // Returns TRUE if successful. Otherwise, it returns FALSE.
  108. // Operation:
  109. // Remove instances of classes derived of DSIANTMessageProcessor
  110. // from the list to stop processing messages for that
  111. // channel number
  112. /////////////////////////////////////////////////////////////////
  113. BOOL RemoveMessageProcessor(DSIANTMessageProcessor* pclChannel_);
  114. /////////////////////////////////////////////////////////////////
  115. // Remove all managed channels from the list
  116. /////////////////////////////////////////////////////////////////
  117. void ClearManagedChannelList(void);
  118. /////////////////////////////////////////////////////////////////
  119. // Returns the serial number of the connected USB device
  120. /////////////////////////////////////////////////////////////////
  121. ULONG GetSerialNumber(); //TODO return false when not connected
  122. #if defined(DEBUG_FILE)
  123. /////////////////////////////////////////////////////////////////
  124. // Enables debug files
  125. // Parameters:
  126. // bDebugOn_: Enable/disable debug logs.
  127. // *pcDirectory_: A string to use as the path for storing
  128. // debug logs. Set to NULL to use the working
  129. // directory as the default path.
  130. /////////////////////////////////////////////////////////////////
  131. void SetDebug(BOOL bDebugOn_, const char *pcDirectory_ = (const char*) NULL);
  132. #endif
  133. };
  134. #endif // DSI_ANT_DEVICE_HPP