2
0

ANTClass.cpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803
  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. 2012
  6. All rights reserved.
  7. */
  8. #include "stdafx.h"
  9. #include "ANTClass.h"
  10. #include "antplus.h"
  11. static UCHAR aucResponseBuf[10];
  12. static UCHAR aucRxBuf[17];
  13. static volatile ANT_RESPONSE_FUNC pResponseFunc = NULL;
  14. static volatile ANT_EVENT_RESPONSE_FUNC pEventResponseFunc = NULL;
  15. /**************************************************************************
  16. * ANTProtocolEventProcess
  17. *
  18. * Callback function for ANT protocol events
  19. *
  20. * ucChannel_: channel number
  21. * ucMessageCode_: message code received from ANT
  22. *
  23. * returns: N/A
  24. *
  25. **************************************************************************/
  26. void ANTProtocolEventProcess(UCHAR ucChannel_, UCHAR ucMessageCode_)
  27. {
  28. if(ANTClass::pclMyCommandTimeout->bWaitingForResponse)
  29. {
  30. if(ucMessageCode_ == MESG_RESPONSE_EVENT_ID)
  31. {
  32. if(aucResponseBuf[1] == ANTClass::pclMyCommandTimeout->ucMessageID)
  33. {
  34. ANTClass::pclMyCommandTimeout->ClearWait();
  35. }
  36. }
  37. else if(ucMessageCode_ == ANTClass::pclMyCommandTimeout->ucResponseID)
  38. {
  39. ANTClass::pclMyCommandTimeout->ClearWait();
  40. }
  41. }
  42. // Pass onto application level
  43. System::Diagnostics::Debug::Assert(pEventResponseFunc != NULL);
  44. pEventResponseFunc(ucChannel_, ucMessageCode_, (char*)&aucResponseBuf[0]);
  45. }
  46. /**************************************************************************
  47. * ANTChannelEventProcess
  48. *
  49. * Callback function for ANT channel events
  50. *
  51. * ucChannel_: ANT channel
  52. * ucMessageCode_: message code returned from ANT
  53. *
  54. * returns: N/A
  55. *
  56. **************************************************************************/
  57. void ANTChannelEventProcess(UCHAR ucChannel_, UCHAR ucMessageCode_)
  58. {
  59. if(ANTClass::paclMyAckTimeout[ucChannel_]->bWaitingForResponse)
  60. {
  61. switch(ucMessageCode_)
  62. {
  63. case EVENT_TRANSFER_TX_COMPLETED:
  64. case EVENT_TRANSFER_TX_FAILED:
  65. case EVENT_CHANNEL_CLOSED:
  66. case CHANNEL_NOT_OPENED:
  67. case TRANSFER_IN_PROGRESS:
  68. {
  69. ANTClass::paclMyAckTimeout[ucChannel_]->ClearWait();
  70. break;
  71. }
  72. default:
  73. break;
  74. }
  75. }
  76. pResponseFunc(ucChannel_, ucMessageCode_, &aucRxBuf[0]);
  77. }
  78. /**************************************************************************
  79. * LibInit
  80. *
  81. * Initializes ANT DLL and assigns response callback function
  82. *
  83. * pEventResponseFunc_: Pointer to ANT Response handler function
  84. *
  85. * returns: TRUE if ANT DLL loaded and initialized correctly. FALSE otherwise.
  86. *
  87. **************************************************************************/
  88. BOOL ANTClass::LibInit(ANT_EVENT_RESPONSE_FUNC pEventResponseFunc_)
  89. {
  90. if (pEventResponseFunc_ == NULL)
  91. return FALSE;
  92. pEventResponseFunc = pEventResponseFunc_;
  93. // Try loading the ANT DLL
  94. if (!ANT_Load())
  95. return FALSE;
  96. // Assign callback function
  97. ANT_AssignResponseFunction( (RESPONSE_FUNC) ANTProtocolEventProcess, aucResponseBuf);
  98. // Initialize the timers
  99. pclMyCommandTimeout = gcnew TimeoutStruct(gcnew ChannelDelegate(&ANTClass::TimerEvent));
  100. pclMyCommandTimeout->Interval = 500;
  101. pclMyCommandTimeout->Elapsed += gcnew System::Timers::ElapsedEventHandler(pclMyCommandTimeout, &TimeoutStruct::ProcessTimerEvent);
  102. return(TRUE);
  103. }
  104. /**************************************************************************
  105. * AssignChannelEventFunctions
  106. *
  107. * Assigns a channel event function to required number of channels.
  108. *
  109. * pResponseFunc_: Pointer to channl event handler functiion.
  110. *
  111. * returns: TRUE if input parameters make sense.
  112. *
  113. **************************************************************************/
  114. BOOL ANTClass::AssignChannelEventFunctions(int iChannels_, ANT_RESPONSE_FUNC pResponseFunc_)
  115. {
  116. int i;
  117. if (pResponseFunc_ == NULL)
  118. return FALSE;
  119. paclMyAckTimeout = gcnew array<TimeoutStruct^, 1>(iChannels_);
  120. pResponseFunc = pResponseFunc_;
  121. //After the capabilities return we know how many channels are supported and can assign the channel response function to each
  122. for(i=0; i < iChannels_; ++i)
  123. {
  124. paclMyAckTimeout[i] = gcnew TimeoutStruct(gcnew ChannelDelegate(&ANTClass::AckTimerEvent));
  125. paclMyAckTimeout[i]->Elapsed += gcnew System::Timers::ElapsedEventHandler(paclMyAckTimeout[i], &TimeoutStruct::ProcessTimerEvent);
  126. ANT_AssignChannelEventFunction((UCHAR)i, (CHANNEL_EVENT_FUNC) ANTChannelEventProcess, aucRxBuf);
  127. }
  128. return(TRUE);
  129. }
  130. /**************************************************************************
  131. * Disconnect
  132. *
  133. * Disconnects from the ANT USB stick by restting the ANT chip and unloading
  134. * the ANT DLL.
  135. *
  136. * returns: N/A.
  137. *
  138. **************************************************************************/
  139. void ANTClass::Disconnect()
  140. {
  141. ANT_ResetSystem();
  142. Sleep(600);
  143. ANT_Close();
  144. }
  145. /**************************************************************************
  146. * GetLibVersion
  147. *
  148. * Gets ANT Library (ANT DLL) version
  149. *
  150. * returns: pointer to a string of characters representing the DLL version,
  151. * if available
  152. *
  153. **************************************************************************/
  154. const UCHAR* ANTClass::GetLibVersion()
  155. {
  156. if(ANT_LibVersionSupport())
  157. {
  158. return (ANT_LibVersion()); // Get version, if supported
  159. }
  160. else
  161. return nullptr; // Version string not supported or DLL not yet loaded
  162. }
  163. /**************************************************************************
  164. * Init
  165. *
  166. * Tries to connect to the USB stick.
  167. *
  168. * Params:
  169. *
  170. * ucUSBPort_: USB Port
  171. * usBaud_: Baud rate to connect at
  172. * returns: N/A.
  173. *
  174. **************************************************************************/
  175. BOOL ANTClass::Init(UCHAR ucUSBPort_, ULONG ulBaud_, UCHAR ucPortType_, UCHAR ucSerialFrameType_)
  176. {
  177. return(ANT_Init(ucUSBPort_, ulBaud_, ucPortType_, ucSerialFrameType_));
  178. }
  179. /**************************************************************************
  180. * GetDeviceUSBInfo
  181. *
  182. * Returns USB info about the USB stick.
  183. *
  184. * Params:
  185. *
  186. * ucUSBPort_: USB Port
  187. * pucStringDesc_: String descriptor
  188. * pucSerial_: Serial number of device.
  189. * returns: N/A.
  190. *
  191. **************************************************************************/
  192. BOOL ANTClass::GetDeviceUSBInfo(UCHAR ucUSBPort_, UCHAR* pucStringDesc_, UCHAR* pucSerial_)
  193. {
  194. return(ANT_GetDeviceUSBInfo(ucUSBPort_,pucStringDesc_, pucSerial_));
  195. }
  196. /**************************************************************************
  197. * GetDeviceUSBPID
  198. *
  199. * Returns USB PID of connected USB device. .
  200. *
  201. * Params:
  202. *
  203. * pusPID_: PID word
  204. *
  205. * returns: TRUE if device is connected. FALSE otehrwise.
  206. *
  207. **************************************************************************/
  208. BOOL ANTClass::GetDeviceUSBPID(USHORT* pusPID_)
  209. {
  210. return(ANT_GetDeviceUSBPID(pusPID_));
  211. }
  212. /**************************************************************************
  213. * Close
  214. *
  215. * Unloads the ANT DLL.
  216. *
  217. * Params:
  218. *
  219. * returns: N/A.
  220. *
  221. **************************************************************************/
  222. void ANTClass::Close()
  223. {
  224. ANT_Close();
  225. }
  226. /**************************************************************************
  227. * UnAssignChannel
  228. *
  229. * Unassigns ANT channel.
  230. * Sets state variables for response or timeout.
  231. *
  232. * Params:
  233. *
  234. * ucChannel_: ANT channel number
  235. *
  236. * returns: Returns right aways (does not blowck waiting for response)
  237. *
  238. **************************************************************************/
  239. void ANTClass::UnAssignChannel(UCHAR ucChannel_)
  240. {
  241. if(!ANT_UnAssignChannel(ucChannel_, RESPONSE_TIME))
  242. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  243. }
  244. /**************************************************************************
  245. * AssignChannel
  246. *
  247. * Assigns an ANT channel.
  248. * Sets state variables for response or timeout.
  249. *
  250. * Params:
  251. *
  252. * ucChannel_: ANT channel number
  253. * ucType_: Channel type
  254. * ucNetworkNum_: Network number
  255. *
  256. * returns: Returns right aways (does not blowck waiting for response)
  257. *
  258. **************************************************************************/
  259. void ANTClass::AssignChannel(UCHAR ucChannel_, UCHAR ucType_, UCHAR ucNetworkNum_)
  260. {
  261. if(!ANT_AssignChannel(ucChannel_, ucType_, ucNetworkNum_, RESPONSE_TIME))
  262. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  263. }
  264. /**************************************************************************
  265. * SetChannelId
  266. *
  267. * Sets ANT channel ID.
  268. * Sets state variables for response or timeout.
  269. *
  270. * Params:
  271. *
  272. * ucChannel_: ANT channel number
  273. * usDeviceNumber_: Device number
  274. * ucDeviceType_: Device type
  275. * ucTransmissionType_: Transmission type
  276. *
  277. * returns: Returns right aways (does not blowck waiting for response)
  278. *
  279. **************************************************************************/
  280. void ANTClass::SetChannelId(UCHAR ucChannel_, USHORT usDeviceNumber_, UCHAR ucDeviceType_, UCHAR ucTransmissionType_)
  281. {
  282. if(!ANT_SetChannelId(ucChannel_, usDeviceNumber_, ucDeviceType_, ucTransmissionType_, RESPONSE_TIME))
  283. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  284. }
  285. /**************************************************************************
  286. * SetChannelPeriod
  287. *
  288. * Sets ANT channel message period.
  289. * Sets state variables for response or timeout.
  290. *
  291. * Params:
  292. *
  293. * ucChannel_: ANT channel number
  294. * usPeriod_: Message period
  295. *
  296. * returns: Returns right aways (does not blowck waiting for response)
  297. *
  298. **************************************************************************/
  299. void ANTClass::SetChannelPeriod(UCHAR ucChannel_, USHORT usPeriod_)
  300. {
  301. if(!ANT_SetChannelPeriod(ucChannel_, usPeriod_, RESPONSE_TIME))
  302. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  303. }
  304. /**************************************************************************
  305. * SetChannelSearchTimeout
  306. *
  307. * Sets ANT channel search timeout.
  308. * Sets state variables for response or timeout.
  309. *
  310. * Params:
  311. *
  312. * ucChannel_: ANT channel number
  313. * ucTimeout_: Search timeout
  314. *
  315. * returns: Returns right aways (does not blowck waiting for response)
  316. *
  317. **************************************************************************/
  318. void ANTClass::SetChannelSearchTimeout(UCHAR ucChannel_, UCHAR ucTimeout_)
  319. {
  320. if(!ANT_SetChannelSearchTimeout(ucChannel_, ucTimeout_, RESPONSE_TIME))
  321. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  322. }
  323. /**************************************************************************
  324. * SetChannelRFFreq
  325. *
  326. * Sets ANT channel RF frequency.
  327. * Sets state variables for response or timeout.
  328. *
  329. * Params:
  330. *
  331. * ucChannel_: ANT channel number
  332. * ucRFFreq_: RF frequency
  333. *
  334. * returns: Returns right aways (does not blowck waiting for response)
  335. *
  336. **************************************************************************/
  337. void ANTClass::SetChannelRFFreq(UCHAR ucChannel_, UCHAR ucRFFreq_)
  338. {
  339. if(!ANT_SetChannelRFFreq(ucChannel_, ucRFFreq_, RESPONSE_TIME))
  340. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  341. }
  342. /**************************************************************************
  343. * SetNetworkKey
  344. *
  345. * Sets network key.
  346. * Sets state variables for response or timeout.
  347. *
  348. * Params:
  349. *
  350. * ucChannel_: ANT channel number
  351. * pucKey_: Pointer to buffer containing network key
  352. *
  353. * returns: Returns right aways (does not blowck waiting for response)
  354. *
  355. **************************************************************************/
  356. void ANTClass::SetNetworkKey(UCHAR ucNetNum_, UCHAR* pucKey_)
  357. {
  358. if(!ANT_SetNetworkKey(ucNetNum_, pucKey_, RESPONSE_TIME))
  359. System::Windows::Forms::MessageBox::Show("Failed to set network key. Ensure device is alive and you are connected at the correct baud rate.");
  360. }
  361. /**************************************************************************
  362. * SetTransmitPower
  363. *
  364. * Sets transmit power.
  365. * Sets state variables for response or timeout.
  366. *
  367. * Params:
  368. *
  369. * ucPower_: Transmit power
  370. *
  371. * returns: Returns right aways (does not blowck waiting for response)
  372. *
  373. **************************************************************************/
  374. void ANTClass::SetTransmitPower(UCHAR ucPower_)
  375. {
  376. if(!ANT_SetTransmitPower(ucPower_, RESPONSE_TIME))
  377. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  378. }
  379. /**************************************************************************
  380. * SetChannelTxPower
  381. *
  382. * Sets transmit power.
  383. * Sets state variables for response or timeout.
  384. *
  385. * Params:
  386. *
  387. * ucChannel_: ANT channel
  388. * ucPower_: Transmit power
  389. *
  390. * returns: Returns right aways (does not block waiting for response)
  391. *
  392. **************************************************************************/
  393. void ANTClass::SetChannelTxPower(UCHAR ucChannel_, UCHAR ucPower_)
  394. {
  395. if(!ANT_SetChannelTxPower(ucChannel_, ucPower_, RESPONSE_TIME))
  396. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  397. }
  398. /**************************************************************************
  399. * ResetSystem
  400. *
  401. * Resets ANT and delays appropriate amount of time before returning.
  402. *
  403. * Params:
  404. *
  405. * ucPower_: Transmit power
  406. *
  407. * returns: Returns after ~600ms
  408. *
  409. **************************************************************************/
  410. void ANTClass::ResetSystem()
  411. {
  412. ANT_ResetSystem();
  413. Sleep(600);
  414. }
  415. /**************************************************************************
  416. * OpenChannel
  417. *
  418. * Opens ANT channel.
  419. * Sets state variables for response or timeout.
  420. *
  421. * Params:
  422. *
  423. * ucChannel_: ANT channel
  424. *
  425. * returns: Returns right aways (does not blowck waiting for response)
  426. *
  427. **************************************************************************/
  428. void ANTClass::OpenChannel(UCHAR ucChannel_)
  429. {
  430. if(!pclMyCommandTimeout->bWaitingForResponse)
  431. {
  432. ANT_OpenChannel(ucChannel_);
  433. pclMyCommandTimeout->WaitForResponse(ucChannel_, MESG_RESPONSE_EVENT_ID, MESG_OPEN_CHANNEL_ID);
  434. }
  435. else
  436. {
  437. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  438. }
  439. }
  440. /**************************************************************************
  441. * CloseChannel
  442. *
  443. * Closes ANT channel.
  444. * Sets state variables for response or timeout.
  445. *
  446. * Params:
  447. *
  448. * ucChannel_: ANT channel
  449. *
  450. * returns: Returns right aways (does not blowck waiting for response)
  451. *
  452. **************************************************************************/
  453. void ANTClass::CloseChannel(UCHAR ucChannel_)
  454. {
  455. if(!pclMyCommandTimeout->bWaitingForResponse)
  456. {
  457. ANT_CloseChannel(ucChannel_);
  458. pclMyCommandTimeout->WaitForResponse(ucChannel_, MESG_RESPONSE_EVENT_ID, MESG_CLOSE_CHANNEL_ID);
  459. }
  460. else
  461. {
  462. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  463. }
  464. }
  465. /**************************************************************************
  466. * RequestMessage
  467. *
  468. * Requests a message from ANT.
  469. * Sets state variables for response or timeout.
  470. *
  471. * Params:
  472. *
  473. * ucChannel_: ANT channel
  474. * ucMsgID_: Requested message ID
  475. *
  476. * returns: Returns right aways (does not blowck waiting for response)
  477. *
  478. **************************************************************************/
  479. void ANTClass::RequestMessage(UCHAR ucChannel_, UCHAR ucMsgID_)
  480. {
  481. if(!pclMyCommandTimeout->bWaitingForResponse)
  482. {
  483. ANT_RequestMessage(ucChannel_, ucMsgID_);
  484. pclMyCommandTimeout->WaitForResponse(ucChannel_, ucMsgID_, MESG_REQUEST_ID);
  485. }
  486. else
  487. {
  488. System::Diagnostics::Debug::Assert(FALSE, "Command sent before response or timeout recieved");
  489. }
  490. }
  491. /**************************************************************************
  492. * WriteMessage
  493. *
  494. * Sends a custom message to ANT.
  495. *
  496. * Params:
  497. *
  498. * ucMessageID: messageID
  499. * aucData: messsage payload
  500. * usMessageSize: length of payload
  501. *
  502. * returns: Returns right aways (does not block waiting for response)
  503. *
  504. **************************************************************************/
  505. void ANTClass::WriteMessage(UCHAR ucMessageID, UCHAR* aucData, USHORT usMessageSize)
  506. {
  507. ANT_WriteMessage(ucMessageID, aucData, usMessageSize);
  508. }
  509. /**************************************************************************
  510. * SendBroadcastData
  511. *
  512. * Sends a broadcast message to ANT
  513. *
  514. * Params:
  515. *
  516. * ucChannel_: ANT channel
  517. * pucData_: Pointer to broadcast data buffer
  518. *
  519. * returns: N/A
  520. *
  521. **************************************************************************/
  522. void ANTClass::SendBroadcastData(UCHAR ucChannel_, UCHAR* pucData_)
  523. {
  524. ANT_SendBroadcastData(ucChannel_, pucData_);
  525. }
  526. /**************************************************************************
  527. * SendAcknowledgedData
  528. *
  529. * Sends an acknowledged message to ANT
  530. *
  531. * Params:
  532. *
  533. * ucChannel_: ANT channel
  534. * pucData_: Pointer to acknowledge data buffer
  535. *
  536. * returns: N/A
  537. *
  538. **************************************************************************/
  539. void ANTClass::SendAcknowledgedData(UCHAR ucChannel_, UCHAR* pucData_)
  540. {
  541. SendAcknowledgedData(ucChannel_, pucData_, 0);
  542. }
  543. void ANTClass::SendAcknowledgedData(UCHAR ucChannel_, UCHAR* pucData_, USHORT usTimeout_)
  544. {
  545. System::Diagnostics::Debug::Assert(paclMyAckTimeout[ucChannel_] != nullptr, "ANT Class not initialized properly! ");
  546. if(usTimeout_)
  547. {
  548. paclMyAckTimeout[ucChannel_]->Interval = usTimeout_;
  549. paclMyAckTimeout[ucChannel_]->WaitForResponse(ucChannel_, 0,0);
  550. }
  551. ANT_SendAcknowledgedData(ucChannel_, pucData_);
  552. }
  553. /**************************************************************************
  554. * SendBurstTransfer
  555. *
  556. * Sends a block of data as burst transfers
  557. *
  558. * Params:
  559. *
  560. * ucChannel_: ANT channel
  561. * pucData_: Pointer to burst data buffer
  562. * usSize_: Number of burst packets that need to be sent.
  563. *
  564. * returns: After last burst packet has been sent to the ANT chip
  565. *
  566. **************************************************************************/
  567. void ANTClass::SendBurstTransfer(UCHAR ucChannel_, UCHAR* pucData_, USHORT usSize_)
  568. {
  569. ANT_SendBurstTransfer(ucChannel_, pucData_, usSize_);
  570. }
  571. /**************************************************************************
  572. * AckTimerEvent
  573. *
  574. * Timeout event callback routine called by Acknowledged message timer. This
  575. * should only ever fire if no valid response was recieved to an acknowledged
  576. * message. The EVENT_ACK_TIMEOUT is propagted through the Channel Event
  577. * function chain.
  578. *
  579. * Params:
  580. *
  581. * ucANTChannel_: ANT channel
  582. *
  583. * returns: N/A
  584. *
  585. **************************************************************************/
  586. void ANTClass::AckTimerEvent(UCHAR ucChannel_)
  587. {
  588. aucRxBuf[0] = ucChannel_;
  589. aucRxBuf[1] = 1;
  590. aucRxBuf[2] = EVENT_ACK_TIMEOUT;
  591. ANTChannelEventProcess(ucChannel_, EVENT_ACK_TIMEOUT);
  592. }
  593. /**************************************************************************
  594. * TimerEvent
  595. *
  596. * Timeout event callback routine called by command message timer. This
  597. * should only ever fire if no valid response was recieved to a command that
  598. * requires a response. The EVENT_ACK_TIMEOUT is propagted through the Protocol
  599. * Event function chain.
  600. *
  601. * Params:
  602. *
  603. * ucANTChannel_: ANT channel
  604. *
  605. * returns: N/A
  606. *
  607. **************************************************************************/
  608. void ANTClass::TimerEvent(UCHAR ucChannel_)
  609. {
  610. // Assert ucChannel_ == pclMyCommandTimeout->ucChannel
  611. // If this is (for example) a REQUEST message,
  612. // then need to flip the message ID to make the
  613. // response work properly.
  614. if(pclMyCommandTimeout->ucResponseID != MESG_RESPONSE_EVENT_ID)
  615. {
  616. pclMyCommandTimeout->ucResponseID = MESG_RESPONSE_EVENT_ID;
  617. }
  618. aucResponseBuf[0] = pclMyCommandTimeout->ucChannel;
  619. aucResponseBuf[1] = pclMyCommandTimeout->ucMessageID;
  620. aucResponseBuf[2] = EVENT_COMMAND_TIMEOUT;
  621. ANTProtocolEventProcess(0, pclMyCommandTimeout->ucResponseID);
  622. pclMyCommandTimeout->ClearWait();
  623. }
  624. /**************************************************************************
  625. * TimeoutStruct
  626. *
  627. * Constructor for timer command timer.
  628. *
  629. * Params:
  630. *
  631. * ChannelCallbackFunc_: Reference to delegate which is called when timer
  632. * timeout elapses.
  633. *
  634. * returns: N/A
  635. *
  636. **************************************************************************/
  637. TimeoutStruct::TimeoutStruct(ChannelDelegate^ ChannelCallbackFunc_)
  638. {
  639. ChannelCallback = ChannelCallbackFunc_;
  640. }
  641. /**************************************************************************
  642. * ProcessTimerEvent
  643. *
  644. * Callback function passed as delegate to timer. Called when timeout time
  645. * elaspes. This function handles the timeout and calls it delegate function
  646. * to propagate the timeout event to the ANT Class.
  647. *
  648. * Params: N/A (required for compatibility of timer "elapsed" delegate)
  649. *
  650. * returns: N/A
  651. *
  652. **************************************************************************/
  653. void TimeoutStruct::ProcessTimerEvent(System::Object^ sender, System::Timers::ElapsedEventArgs^ e)
  654. {
  655. Stop();
  656. if(bWaitingForResponse)
  657. {
  658. ChannelCallback(ucChannel);
  659. }
  660. }
  661. /**************************************************************************
  662. * WaitForResponse
  663. *
  664. * Sets state variables and starts timer. Called when a command is sent
  665. * that requires some response.
  666. *
  667. * Params:
  668. *
  669. * ucChannel_: ANT Channel
  670. * ucResponseID_: Message ID of the response we are waiting for
  671. * ucMessageID_: The message ID of the message just sent (the one istigating the response)
  672. *
  673. *
  674. * returns: Right away
  675. *
  676. **************************************************************************/
  677. void TimeoutStruct::WaitForResponse(UCHAR ucChannel_, UCHAR ucResponseID_, UCHAR ucMessageID_)
  678. {
  679. ucMessageID = ucMessageID_;
  680. ucResponseID = ucResponseID_;
  681. ucChannel = ucChannel_;
  682. bWaitingForResponse = TRUE;
  683. Start();
  684. }
  685. /**************************************************************************
  686. * ClearWait
  687. *
  688. * Clears the waiting state machines variables. This should be called if waiting
  689. * for a response to some command and it is recieved, or if the request times out.
  690. *
  691. *
  692. * Params: N/A
  693. *
  694. * returns: N/A
  695. *
  696. **************************************************************************/
  697. void TimeoutStruct::ClearWait()
  698. {
  699. Stop();
  700. ucMessageID = 0;
  701. ucResponseID = 0;
  702. ucChannel = 0;
  703. bWaitingForResponse = FALSE;
  704. }