dsi_framer_ant.cpp 94 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683
  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. #include "types.h"
  9. #include "defines.h"
  10. #include "antmessage.h"
  11. #include "antdefines.h"
  12. #include "checksum.h"
  13. #include "dsi_thread.h"
  14. #include "dsi_framer_ant.hpp"
  15. #include <string.h>
  16. #define WAIT_TO_FEED_TRANSFER
  17. #include "dsi_debug.hpp"
  18. #if defined(DEBUG_FILE)
  19. #define SERIAL_DEBUG
  20. #endif
  21. //////////////////////////////////////////////////////////////////////////////////
  22. // Private Definitions
  23. //////////////////////////////////////////////////////////////////////////////////
  24. #define TX_FIFO_SIZE ((USHORT) 256)
  25. #define ANT_DATA_CHANNEL_NUM_OFFSET 0
  26. #define ANT_DATA_EVENT_ID_OFFSET 1
  27. #define ANT_DATA_EVENT_CODE_OFFSET 2
  28. //#define ANT_DATA_REQUESTED_MESG_ID_OFFSET 1
  29. #define ANT_BASIC_CAPABILITIES_SIZE 4
  30. //////////////////////////////////////////////////////////////////////////////////
  31. // Public Class Functions
  32. //////////////////////////////////////////////////////////////////////////////////
  33. ///////////////////////////////////////////////////////////////////////
  34. DSIFramerANT::DSIFramerANT()
  35. {
  36. bInitOkay = TRUE;
  37. bClosing = FALSE;
  38. pbCancel = (volatile BOOL*)NULL;
  39. bSplitAdvancedBursts = FALSE;
  40. if (DSIThread_CondInit(&stCondMessageReady) != DSI_THREAD_ENONE)
  41. bInitOkay = FALSE;
  42. if (DSIThread_MutexInit(&stMutexCriticalSection) != DSI_THREAD_ENONE)
  43. bInitOkay = FALSE;
  44. if (DSIThread_MutexInit(&stMutexResponseRequest) != DSI_THREAD_ENONE)
  45. bInitOkay = FALSE;
  46. pclResponseListStart = (ANTMessageResponse*)NULL;
  47. Init((DSISerial*)NULL);
  48. }
  49. DSIFramerANT::DSIFramerANT(DSISerial *pclSerial_) : DSIFramer(pclSerial_)
  50. {
  51. bInitOkay = TRUE;
  52. bClosing = FALSE;
  53. pbCancel = (volatile BOOL*)NULL;
  54. bSplitAdvancedBursts = FALSE;
  55. if (DSIThread_CondInit(&stCondMessageReady) != DSI_THREAD_ENONE)
  56. bInitOkay = FALSE;
  57. if (DSIThread_MutexInit(&stMutexCriticalSection) != DSI_THREAD_ENONE)
  58. bInitOkay = FALSE;
  59. if (DSIThread_MutexInit(&stMutexResponseRequest) != DSI_THREAD_ENONE)
  60. bInitOkay = FALSE;
  61. pclResponseListStart = (ANTMessageResponse*)NULL;
  62. Init(pclSerial_);
  63. }
  64. ///////////////////////////////////////////////////////////////////////
  65. DSIFramerANT::~DSIFramerANT()
  66. {
  67. DSIThread_CondDestroy(&stCondMessageReady);
  68. DSIThread_MutexDestroy(&stMutexCriticalSection);
  69. DSIThread_MutexDestroy(&stMutexResponseRequest);
  70. }
  71. ///////////////////////////////////////////////////////////////////////
  72. void DSIFramerANT::SetSplitAdvBursts(BOOL bSplitAdvBursts_)
  73. {
  74. bSplitAdvancedBursts = bSplitAdvBursts_;
  75. }
  76. ///////////////////////////////////////////////////////////////////////
  77. void DSIFramerANT::SetCancelParameter(volatile BOOL *pbCancel_)
  78. {
  79. pbCancel = pbCancel_;
  80. }
  81. ///////////////////////////////////////////////////////////////////////
  82. volatile BOOL* DSIFramerANT::GetCancelParameter()
  83. {
  84. return pbCancel;
  85. }
  86. ///////////////////////////////////////////////////////////////////////
  87. BOOL DSIFramerANT::Init(DSISerial *pclSerial_)
  88. {
  89. ucRxIndex = 0;
  90. usMessageHead = 0;
  91. usMessageTail = 0;
  92. ucError = 0;
  93. if (pclSerial_ != NULL)
  94. pclSerial = pclSerial_;
  95. if (pclSerial == (DSISerial*)NULL)
  96. return FALSE;
  97. return bInitOkay;
  98. }
  99. ///////////////////////////////////////////////////////////////////////
  100. BOOL DSIFramerANT::WriteMessage(void *pvData_, USHORT usMessageSize_)
  101. {
  102. UCHAR aucTxFifo[TX_FIFO_SIZE];
  103. UCHAR ucTotalSize;
  104. if (usMessageSize_ > MESG_MAX_SIZE_VALUE)
  105. {
  106. #if defined(DEBUG_FILE)
  107. DSIDebug::ThreadWrite("Framer->WriteMessage(): Failed, Msg Size > MESG_MAX_SIZE_VALUE.");
  108. #endif
  109. return FALSE;
  110. }
  111. ucTotalSize = (UCHAR) usMessageSize_ + MESG_HEADER_SIZE;
  112. aucTxFifo[0] = MESG_TX_SYNC;
  113. aucTxFifo[MESG_SIZE_OFFSET] = (UCHAR) usMessageSize_;
  114. aucTxFifo[MESG_ID_OFFSET] = ((ANT_MESSAGE *) pvData_)->ucMessageID;
  115. memcpy(&aucTxFifo[MESG_DATA_OFFSET], ((ANT_MESSAGE *) pvData_)->aucData, usMessageSize_);
  116. aucTxFifo[ucTotalSize] = CheckSum_Calc8(aucTxFifo, ucTotalSize);
  117. ++ucTotalSize;
  118. // Pad with two zeros.
  119. aucTxFifo[ucTotalSize++] = 0;
  120. aucTxFifo[ucTotalSize++] = 0;
  121. if (pclSerial->WriteBytes(aucTxFifo, ucTotalSize))
  122. {
  123. #if defined(SERIAL_DEBUG)
  124. if (aucTxFifo[MESG_ID_OFFSET] == 0x46)
  125. memset(&aucTxFifo[MESG_DATA_OFFSET+1],0x00,8);
  126. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "Tx", aucTxFifo, ucTotalSize);
  127. #endif
  128. return TRUE;
  129. }
  130. #if defined(SERIAL_DEBUG)
  131. if (aucTxFifo[MESG_ID_OFFSET] == 0x46)
  132. memset(&aucTxFifo[MESG_DATA_OFFSET+1],0x00,8);
  133. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "***Tx Error***", aucTxFifo, ucTotalSize);
  134. #endif
  135. return FALSE;
  136. }
  137. ///////////////////////////////////////////////////////////////////////
  138. USHORT DSIFramerANT::WaitForMessage(ULONG ulMilliseconds_)
  139. {
  140. USHORT usMessageSize;
  141. DSIThread_MutexLock(&stMutexCriticalSection);
  142. usMessageSize = GetMessageSize();
  143. if ((usMessageSize == DSI_FRAMER_TIMEDOUT) && (ulMilliseconds_ != 0))
  144. {
  145. UCHAR ucStatus = DSIThread_CondTimedWait(&stCondMessageReady, &stMutexCriticalSection, ulMilliseconds_);
  146. if(ucStatus == DSI_THREAD_ENONE)
  147. {
  148. usMessageSize = GetMessageSize();
  149. }
  150. else if(ucStatus == DSI_THREAD_ETIMEDOUT)
  151. {
  152. usMessageSize = DSI_FRAMER_TIMEDOUT;
  153. }
  154. else //CondWait() failed
  155. {
  156. ucError = (UCHAR)(DSI_FRAMER_ERROR & 0xFF); //Set ucError so we can distinguish from a normal error if this ever occurs
  157. usMessageSize = DSI_FRAMER_ERROR;
  158. }
  159. }
  160. DSIThread_MutexUnlock(&stMutexCriticalSection);
  161. return usMessageSize;
  162. }
  163. ///////////////////////////////////////////////////////////////////////
  164. USHORT DSIFramerANT::GetMessage(void *pvData_, USHORT usSize_)
  165. {
  166. USHORT usRetVal;
  167. DSIThread_MutexLock(&stMutexCriticalSection);
  168. if (ucError)
  169. {
  170. ((ANT_MESSAGE *) pvData_)->ucMessageID = ucError;
  171. if (ucError == DSI_FRAMER_ANT_ESERIAL)
  172. ((ANT_MESSAGE *) pvData_)->aucData[0] = ucSerialError;
  173. ucError = 0;
  174. usRetVal = DSI_FRAMER_ERROR;
  175. }
  176. else
  177. {
  178. if ((usMessageHead - usMessageTail) != 0)
  179. {
  180. // Determine the number of bytes to copy.
  181. usRetVal = astMessageBuffer[usMessageTail].ucSize; // The reported number of bytes in the queue.
  182. if (usSize_ != 0)
  183. usRetVal = MIN(usRetVal, usSize_); // If the usSize_ parameter is non-zero, limit the number of bytes copied from the queue to usSize_.
  184. if (usRetVal > MESG_MAX_SIZE_VALUE) // Check to make sure we are not copying beyond the end of the message buffers
  185. {
  186. ((ANT_MESSAGE *) pvData_)->ucMessageID = DSI_FRAMER_ANT_EINVALID_SIZE;
  187. usRetVal = DSI_FRAMER_ERROR;
  188. }
  189. else
  190. {
  191. ((ANT_MESSAGE *) pvData_)->ucMessageID = astMessageBuffer[usMessageTail].stANTMessage.ucMessageID;
  192. memcpy(((ANT_MESSAGE *) pvData_)->aucData, astMessageBuffer[usMessageTail].stANTMessage.aucData, usRetVal);
  193. }
  194. usMessageTail++; // Rollover of usMessageTail happens automagically because our buffer size is MAX_USHORT + 1.
  195. }
  196. else
  197. {
  198. usRetVal = DSI_FRAMER_TIMEDOUT;
  199. }
  200. }
  201. DSIThread_MutexUnlock(&stMutexCriticalSection);
  202. return usRetVal;
  203. }
  204. ///////////////////////////////////////////////////////////////////////
  205. #define MESG_CHANNEL_OFFSET 0
  206. #define MESG_EVENT_ID_OFFSET 1
  207. UCHAR DSIFramerANT::GetChannelNumber(ANT_MESSAGE* pstMessage)
  208. {
  209. // Get the channel number
  210. // Returns MAX_UCHAR if this message does not have a channel field
  211. UCHAR ucANTchannel = pstMessage->aucData[MESG_CHANNEL_OFFSET] & CHANNEL_NUMBER_MASK;
  212. // Some messages do not include the channel number in the response, so
  213. // they might get processed incorrectly
  214. if(pstMessage->ucMessageID == MESG_RESPONSE_EVENT_ID)
  215. {
  216. if(pstMessage->aucData[MESG_EVENT_ID_OFFSET] == MESG_NETWORK_KEY_ID || // we would need to look at the network number to figure it out
  217. pstMessage->aucData[MESG_EVENT_ID_OFFSET] == MESG_RADIO_TX_POWER_ID || // this affects all channels
  218. pstMessage->aucData[MESG_EVENT_ID_OFFSET] == MESG_RX_EXT_MESGS_ENABLE_ID) // this affects all channels
  219. {
  220. return MAX_UCHAR;
  221. }
  222. }
  223. else if(pstMessage->ucMessageID == MESG_STARTUP_MESG_ID ||
  224. pstMessage->ucMessageID == MESG_CAPABILITIES_ID ||
  225. pstMessage->ucMessageID == MESG_VERSION_ID ||
  226. pstMessage->ucMessageID == MESG_GET_SERIAL_NUM_ID)
  227. {
  228. return MAX_UCHAR;
  229. }
  230. return ucANTchannel;
  231. }
  232. ///////////////////////////////////////////////////////////////////////
  233. void DSIFramerANT::ProcessByte(UCHAR ucByte_)
  234. {
  235. DSIThread_MutexLock(&stMutexCriticalSection);
  236. if (ucRxIndex == 0) // If we are looking for the start of a message.
  237. {
  238. if (ucByte_ == MESG_TX_SYNC) // If it is a valid first byte.
  239. {
  240. aucRxFifo[ucRxIndex++] = ucByte_; // Save it.
  241. ucCheckSum = ucByte_; // Initialize the checksum.
  242. ucRxSize = 2; // We have to init high so we can read enough bytes to determine real length
  243. }
  244. }
  245. else if (ucRxIndex == 1) // Determine RX message size.
  246. {
  247. aucRxFifo[ucRxIndex++] = ucByte_; // Save it.
  248. ucRxSize = ucByte_ + (MESG_FRAME_SIZE - MESG_SYNC_SIZE); // We just got the length.
  249. ucCheckSum ^= ucByte_; // Calculate checksum.
  250. if ((USHORT)ucRxSize > RX_FIFO_SIZE) // If our buffer can't handle this message, turf it.
  251. {
  252. #if defined(SERIAL_DEBUG)
  253. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "ERROR: size > RX_FIFO_SIZE", aucRxFifo, ucRxIndex);
  254. #endif
  255. if (ucByte_ == MESG_TX_SYNC)
  256. {
  257. aucRxFifo[0] = ucByte_; // Save the byte.
  258. ucCheckSum = ucByte_; // Initialize the checksum.
  259. ucRxSize = 2; // We have to init high so we can read enough bytes to determine real length
  260. ucRxIndex = 1; // Set the Rx Index for the next iteration.
  261. }
  262. else
  263. {
  264. ucRxIndex = 0; // Invalid size, so restart.
  265. }
  266. }
  267. }
  268. else
  269. {
  270. aucRxFifo[ucRxIndex] = ucByte_; // Save the byte.
  271. ucCheckSum ^= ucByte_; // Calculate checksum.
  272. if (ucRxIndex >= ucRxSize) // If we have received the whole message.
  273. {
  274. if (ucCheckSum == 0) // The CRC passed.
  275. {
  276. ProcessMessage(); // Process the ANT message.
  277. }
  278. else
  279. {
  280. // Set a serial error for the bad crc.
  281. ucSerialError = DSI_FRAMER_ANT_CRC_ERROR;
  282. ucError = DSI_FRAMER_ANT_ESERIAL;
  283. DSIThread_CondSignal(&stCondMessageReady);
  284. #if defined(SERIAL_DEBUG)
  285. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "Bad CRC",aucRxFifo,ucRxIndex);
  286. #endif
  287. }
  288. ucRxIndex = 0; // Reset the index.
  289. }
  290. else
  291. {
  292. ucRxIndex++;
  293. }
  294. }
  295. DSIThread_MutexUnlock(&stMutexCriticalSection);
  296. }
  297. ///////////////////////////////////////////////////////////////////////
  298. void DSIFramerANT::Error(UCHAR ucError_)
  299. {
  300. DSIThread_MutexLock(&stMutexCriticalSection);
  301. ucSerialError = ucError_;
  302. ucError = DSI_FRAMER_ANT_ESERIAL;
  303. DSIThread_CondSignal(&stCondMessageReady);
  304. DSIThread_MutexUnlock(&stMutexCriticalSection);
  305. }
  306. ///////////////////////////////////////////////////////////////////////
  307. BOOL DSIFramerANT::SetNetworkKey(UCHAR ucNetworkNumber_, UCHAR *pucKey_, ULONG ulResponseTime_)
  308. {
  309. ANT_MESSAGE stMessage;
  310. stMessage.ucMessageID = MESG_NETWORK_KEY_ID;
  311. stMessage.aucData[0] = ucNetworkNumber_;
  312. memcpy(&stMessage.aucData[1], pucKey_, 8);
  313. return SendCommand(&stMessage, MESG_NETWORK_KEY_SIZE, ulResponseTime_);
  314. }
  315. ///////////////////////////////////////////////////////////////////////
  316. BOOL DSIFramerANT::UnAssignChannel(UCHAR ucANTChannel_, ULONG ulResponseTime_)
  317. {
  318. ANT_MESSAGE stMessage;
  319. stMessage.ucMessageID = MESG_UNASSIGN_CHANNEL_ID;
  320. stMessage.aucData[0] = ucANTChannel_;
  321. return SendCommand(&stMessage, MESG_UNASSIGN_CHANNEL_SIZE, ulResponseTime_);
  322. }
  323. ///////////////////////////////////////////////////////////////////////
  324. BOOL DSIFramerANT::AssignChannel(UCHAR ucANTChannel_, UCHAR ucChannelType_, UCHAR ucNetworkNumber_, ULONG ulResponseTime_)
  325. {
  326. ANT_MESSAGE stMessage;
  327. stMessage.ucMessageID = MESG_ASSIGN_CHANNEL_ID;
  328. stMessage.aucData[0] = ucANTChannel_;
  329. stMessage.aucData[1] = ucChannelType_;
  330. stMessage.aucData[2] = ucNetworkNumber_;
  331. return SendCommand(&stMessage, MESG_ASSIGN_CHANNEL_SIZE, ulResponseTime_);
  332. }
  333. ///////////////////////////////////////////////////////////////////////
  334. BOOL DSIFramerANT::AssignChannelExt(UCHAR ucANTChannel_, UCHAR* pucChannelType_, UCHAR ucSize_, UCHAR ucNetworkNumber_, ULONG ulResponseTime_)
  335. {
  336. if( (pucChannelType_ == NULL) || (ucSize_ < 1) )
  337. return FALSE;
  338. ANT_MESSAGE stMessage;
  339. stMessage.ucMessageID = MESG_ASSIGN_CHANNEL_ID;
  340. stMessage.aucData[0] = ucANTChannel_;
  341. stMessage.aucData[1] = pucChannelType_[0];
  342. stMessage.aucData[2] = ucNetworkNumber_;
  343. for(UCHAR i=1; i<ucSize_; i++)
  344. stMessage.aucData[2+i] = pucChannelType_[i];
  345. return SendCommand(&stMessage, ucSize_ + 2, ulResponseTime_);
  346. }
  347. ///////////////////////////////////////////////////////////////////////
  348. BOOL DSIFramerANT::SetChannelID(UCHAR ucANTChannel_, USHORT usDeviceNumber_, UCHAR ucDeviceType_, UCHAR ucTransmitType_, ULONG ulResponseTime_)
  349. {
  350. ANT_MESSAGE stMessage;
  351. stMessage.ucMessageID = MESG_CHANNEL_ID_ID;
  352. stMessage.aucData[0] = ucANTChannel_;
  353. stMessage.aucData[1] = (UCHAR)(usDeviceNumber_ & 0xFF);
  354. stMessage.aucData[2] = (UCHAR)((usDeviceNumber_ >>8) & 0xFF);
  355. stMessage.aucData[3] = ucDeviceType_;
  356. stMessage.aucData[4] = ucTransmitType_;
  357. return SendCommand(&stMessage, MESG_CHANNEL_ID_SIZE, ulResponseTime_);
  358. }
  359. ///////////////////////////////////////////////////////////////////////
  360. BOOL DSIFramerANT::SetChannelPeriod(UCHAR ucANTChannel_, USHORT usMessagePeriod_, ULONG ulResponseTime_)
  361. {
  362. ANT_MESSAGE stMessage;
  363. stMessage.ucMessageID = MESG_CHANNEL_MESG_PERIOD_ID;
  364. stMessage.aucData[0] = ucANTChannel_;
  365. stMessage.aucData[1] = (UCHAR)(usMessagePeriod_ & 0xFF);
  366. stMessage.aucData[2] = (UCHAR)((usMessagePeriod_ >>8) & 0xFF);
  367. return SendCommand(&stMessage, MESG_CHANNEL_MESG_PERIOD_SIZE, ulResponseTime_);
  368. }
  369. ///////////////////////////////////////////////////////////////////////
  370. BOOL DSIFramerANT::SetFastSearch(UCHAR ucANTChannel_, ULONG ulResponseTime_)
  371. {
  372. ANT_MESSAGE stMessage;
  373. stMessage.ucMessageID = 0x49;
  374. stMessage.aucData[0] = ucANTChannel_;
  375. stMessage.aucData[1] = 86;
  376. stMessage.aucData[2] = 0;
  377. return SendCommand(&stMessage, 3, ulResponseTime_);
  378. }
  379. ///////////////////////////////////////////////////////////////////////
  380. BOOL DSIFramerANT::SetLowPriorityChannelSearchTimeout(UCHAR ucANTChannel_, UCHAR ucSearchTimeout_, ULONG ulResponseTime_)
  381. {
  382. ANT_MESSAGE stMessage;
  383. stMessage.ucMessageID = MESG_SET_LP_SEARCH_TIMEOUT_ID;
  384. stMessage.aucData[0] = ucANTChannel_;
  385. stMessage.aucData[1] = ucSearchTimeout_;
  386. return SendCommand(&stMessage, MESG_SET_LP_SEARCH_TIMEOUT_SIZE, ulResponseTime_);
  387. }
  388. ///////////////////////////////////////////////////////////////////////
  389. BOOL DSIFramerANT::SetChannelSearchTimeout(UCHAR ucANTChannel_, UCHAR ucSearchTimeout_, ULONG ulResponseTime_)
  390. {
  391. ANT_MESSAGE stMessage;
  392. stMessage.ucMessageID = MESG_CHANNEL_SEARCH_TIMEOUT_ID;
  393. stMessage.aucData[0] = ucANTChannel_;
  394. stMessage.aucData[1] = ucSearchTimeout_;
  395. return SendCommand(&stMessage, MESG_CHANNEL_SEARCH_TIMEOUT_SIZE, ulResponseTime_);
  396. }
  397. ///////////////////////////////////////////////////////////////////////
  398. BOOL DSIFramerANT::SetChannelRFFrequency(UCHAR ucANTChannel_, UCHAR ucRFFrequency_, ULONG ulResponseTime_)
  399. {
  400. ANT_MESSAGE stMessage;
  401. stMessage.ucMessageID = MESG_CHANNEL_RADIO_FREQ_ID;
  402. stMessage.aucData[0] = ucANTChannel_;
  403. stMessage.aucData[1] = ucRFFrequency_;
  404. return SendCommand(&stMessage, MESG_CHANNEL_RADIO_FREQ_SIZE, ulResponseTime_);
  405. }
  406. ///////////////////////////////////////////////////////////////////////
  407. BOOL DSIFramerANT::CrystalEnable(ULONG ulResponseTime_)
  408. {
  409. ANT_MESSAGE stMessage;
  410. stMessage.ucMessageID = MESG_XTAL_ENABLE_ID;
  411. stMessage.aucData[0] = 0;
  412. return SendCommand(&stMessage, MESG_XTAL_ENABLE_SIZE, ulResponseTime_);
  413. }
  414. ///////////////////////////////////////////////////////////////////////
  415. BOOL DSIFramerANT::SetLibConfig(UCHAR ucLibConfigFlags_, ULONG ulResponseTime_)
  416. {
  417. ANT_MESSAGE stMessage;
  418. stMessage.ucMessageID = MESG_ANTLIB_CONFIG_ID;
  419. stMessage.aucData[0] = 0;
  420. stMessage.aucData[1] = ucLibConfigFlags_;
  421. return SendCommand(&stMessage, MESG_ANTLIB_CONFIG_SIZE, ulResponseTime_);
  422. }
  423. ///////////////////////////////////////////////////////////////////////
  424. BOOL DSIFramerANT::SetProximitySearch(UCHAR ucANTChannel_, UCHAR ucSearchThreshold_, ULONG ulResponseTime_)
  425. {
  426. ANT_MESSAGE stMessage;
  427. stMessage.ucMessageID = MESG_PROX_SEARCH_CONFIG_ID;
  428. stMessage.aucData[0] = ucANTChannel_;
  429. stMessage.aucData[1] = ucSearchThreshold_;
  430. return SendCommand(&stMessage, MESG_PROX_SEARCH_CONFIG_SIZE, ulResponseTime_);
  431. }
  432. ///////////////////////////////////////////////////////////////////////
  433. BOOL DSIFramerANT::ConfigEventBuffer(UCHAR ucConfig_, USHORT usSize_, USHORT usTime_, ULONG ulResponseTime_)
  434. {
  435. ANT_MESSAGE stMessage;
  436. stMessage.ucMessageID = MESG_EVENT_BUFFERING_CONFIG_ID;
  437. stMessage.aucData[0] = 0;
  438. stMessage.aucData[1] = ucConfig_;
  439. stMessage.aucData[2] = (UCHAR)(usSize_ & 0xFF);
  440. stMessage.aucData[3] = (UCHAR)((usSize_ >>8) & 0xFF);
  441. stMessage.aucData[4] = (UCHAR)(usTime_ & 0xFF);
  442. stMessage.aucData[5] = (UCHAR)((usTime_ >>8) & 0xFF);
  443. return SendCommand(&stMessage, MESG_EVENT_BUFFERING_CONFIG_SIZE, ulResponseTime_);
  444. }
  445. ///////////////////////////////////////////////////////////////////////
  446. BOOL DSIFramerANT::ConfigEventFilter(USHORT usEventFilter_, ULONG ulResponseTime_)
  447. {
  448. ANT_MESSAGE stMessage;
  449. stMessage.ucMessageID = MESG_EVENT_FILTER_CONFIG_ID;
  450. stMessage.aucData[0] = 0;
  451. stMessage.aucData[1] = (UCHAR)(usEventFilter_ & 0xFF);
  452. stMessage.aucData[2] = (UCHAR)((usEventFilter_ >>8) & 0xFF);
  453. return SendCommand(&stMessage, MESG_EVENT_FILTER_CONFIG_SIZE, ulResponseTime_);
  454. }
  455. ///////////////////////////////////////////////////////////////////////
  456. BOOL DSIFramerANT::ConfigHighDutySearch(UCHAR ucEnable_, UCHAR ucSuppressionCycles_, ULONG ulResponseTime_)
  457. {
  458. ANT_MESSAGE stMessage;
  459. stMessage.ucMessageID = MESG_HIGH_DUTY_SEARCH_MODE_ID;
  460. stMessage.aucData[0] = 0;
  461. stMessage.aucData[1] = ucEnable_;
  462. stMessage.aucData[2] = ucSuppressionCycles_;
  463. return SendCommand(&stMessage, MESG_HIGH_DUTY_SEARCH_MODE_SIZE, ulResponseTime_);
  464. }
  465. ///////////////////////////////////////////////////////////////////////
  466. BOOL DSIFramerANT::ConfigSelectiveDataUpdate(UCHAR ucANTChannel_, UCHAR ucSduConfig_, ULONG ulResponseTime_)
  467. {
  468. ANT_MESSAGE stMessage;
  469. stMessage.ucMessageID = MESG_SDU_CONFIG_ID;
  470. stMessage.aucData[0] = ucANTChannel_;
  471. stMessage.aucData[1] = ucSduConfig_;
  472. return SendCommand(&stMessage, MESG_SDU_CONFIG_SIZE, ulResponseTime_);
  473. }
  474. ///////////////////////////////////////////////////////////////////////
  475. BOOL DSIFramerANT::SetSelectiveDataUpdateMask(UCHAR ucMaskNumber_, UCHAR* pucSduMask_, ULONG ulResponseTime_)
  476. {
  477. ANT_MESSAGE stMessage;
  478. stMessage.ucMessageID = MESG_SDU_SET_MASK_ID;
  479. stMessage.aucData[0] = ucMaskNumber_;
  480. memcpy(&stMessage.aucData[1], pucSduMask_, 8);
  481. return SendCommand(&stMessage, MESG_SDU_SET_MASK_SIZE, ulResponseTime_);
  482. }
  483. ///////////////////////////////////////////////////////////////////////
  484. BOOL DSIFramerANT::ConfigUserNVM(USHORT usAddress_, UCHAR* pucData_, UCHAR ucSize_, ULONG ulResponseTime_)
  485. {
  486. if ((ucSize_ + MESG_USER_CONFIG_PAGE_SIZE) <= MESG_MAX_SIZE_VALUE)
  487. {
  488. ANT_MESSAGE stMessage;
  489. stMessage.ucMessageID = MESG_USER_CONFIG_PAGE_ID;
  490. stMessage.aucData[0] = 0;
  491. // Addr is passed in LE
  492. stMessage.aucData[1] = (UCHAR)(usAddress_ & 0xFF);
  493. stMessage.aucData[2] = (UCHAR)((usAddress_ >>8) & 0xFF);
  494. memcpy(&stMessage.aucData[3], pucData_, ucSize_);
  495. return SendCommand(&stMessage, MESG_USER_CONFIG_PAGE_SIZE+ucSize_, ulResponseTime_);
  496. }
  497. else
  498. return false;
  499. }
  500. ///////////////////////////////////////////////////////////////////////
  501. BOOL DSIFramerANT::SetChannelSearchPriority(UCHAR ucANTChannel_, UCHAR ucPriorityLevel_, ULONG ulResponseTime_)
  502. {
  503. ANT_MESSAGE stMessage;
  504. stMessage.ucMessageID = MESG_SET_SEARCH_CH_PRIORITY_ID;
  505. stMessage.aucData[0] = ucANTChannel_;
  506. stMessage.aucData[1] = ucPriorityLevel_;
  507. return SendCommand(&stMessage, MESG_SET_SEARCH_CH_PRIORITY_SIZE, ulResponseTime_);
  508. }
  509. ///////////////////////////////////////////////////////////////////////
  510. BOOL DSIFramerANT::SetSearchSharingCycles(UCHAR ucANTChannel_, UCHAR ucSearchSharingCycles_, ULONG ulResponseTime_)
  511. {
  512. ANT_MESSAGE stMessage;
  513. stMessage.ucMessageID = MESG_ACTIVE_SEARCH_SHARING_ID;
  514. stMessage.aucData[0] = ucANTChannel_;
  515. stMessage.aucData[1] = ucSearchSharingCycles_;
  516. return SendCommand(&stMessage, MESG_ACTIVE_SEARCH_SHARING_SIZE, ulResponseTime_);
  517. }
  518. ///////////////////////////////////////////////////////////////////////
  519. BOOL DSIFramerANT::SleepMessage(ULONG ulResponseTime_)
  520. {
  521. ANT_MESSAGE stMessage;
  522. stMessage.ucMessageID = MESG_SLEEP_ID;
  523. stMessage.aucData[0] = 0;
  524. return SendCommand(&stMessage, MESG_SLEEP_SIZE, ulResponseTime_);
  525. }
  526. ///////////////////////////////////////////////////////////////////////
  527. BOOL DSIFramerANT::ConfigFrequencyAgility(UCHAR ucANTChannel_, UCHAR ucFreq1_, UCHAR ucFreq2_, UCHAR ucFreq3_, ULONG ulResponseTime_)
  528. {
  529. ANT_MESSAGE stMessage;
  530. stMessage.ucMessageID = MESG_AUTO_FREQ_CONFIG_ID;
  531. stMessage.aucData[0] = ucANTChannel_;
  532. stMessage.aucData[1] = ucFreq1_;
  533. stMessage.aucData[2] = ucFreq2_;
  534. stMessage.aucData[3] = ucFreq3_;
  535. return SendCommand(&stMessage, MESG_AUTO_FREQ_CONFIG_SIZE, ulResponseTime_);
  536. }
  537. ///////////////////////////////////////////////////////////////////////
  538. BOOL DSIFramerANT::SetAllChannelsTransmitPower(UCHAR ucTransmitPower_, ULONG ulResponseTime_)
  539. {
  540. ANT_MESSAGE stMessage;
  541. stMessage.ucMessageID = MESG_RADIO_TX_POWER_ID;
  542. stMessage.aucData[0] = 0;
  543. stMessage.aucData[1] = ucTransmitPower_;
  544. return SendCommand(&stMessage, MESG_RADIO_TX_POWER_SIZE, ulResponseTime_);
  545. }
  546. ///////////////////////////////////////////////////////////////////////
  547. BOOL DSIFramerANT::SetChannelTransmitPower(UCHAR ucANTChannel_,UCHAR ucTransmitPower_, ULONG ulResponseTime_)
  548. {
  549. ANT_MESSAGE stMessage;
  550. stMessage.ucMessageID = MESG_CHANNEL_RADIO_TX_POWER_ID;
  551. stMessage.aucData[0] = ucANTChannel_;
  552. stMessage.aucData[1] = ucTransmitPower_;
  553. return SendCommand(&stMessage, MESG_CHANNEL_RADIO_TX_POWER_SIZE, ulResponseTime_);
  554. }
  555. ///////////////////////////////////////////////////////////////////////
  556. BOOL DSIFramerANT::InitCWTestMode(ULONG ulResponseTime_)
  557. {
  558. ANT_MESSAGE stMessage;
  559. stMessage.ucMessageID = MESG_RADIO_CW_INIT_ID;
  560. stMessage.aucData[0] = 0;
  561. return SendCommand(&stMessage, MESG_RADIO_CW_INIT_SIZE, ulResponseTime_);
  562. }
  563. ///////////////////////////////////////////////////////////////////////
  564. BOOL DSIFramerANT::SetCWTestMode(UCHAR ucTransmitPower_, UCHAR ucRFFreq_, ULONG ulResponseTime_)
  565. {
  566. ANT_MESSAGE stMessage;
  567. stMessage.ucMessageID = MESG_RADIO_CW_MODE_ID;
  568. stMessage.aucData[0] = 0;
  569. stMessage.aucData[1] = ucTransmitPower_;
  570. stMessage.aucData[2] = ucRFFreq_;
  571. return SendCommand(&stMessage, MESG_RADIO_CW_MODE_SIZE, ulResponseTime_);
  572. }
  573. ///////////////////////////////////////////////////////////////////////
  574. BOOL DSIFramerANT::AddChannelID(UCHAR ucANTChannel_, USHORT usDeviceNumber_,UCHAR ucDeviceType_, UCHAR ucTransmissionType_, UCHAR ucListIndex_, ULONG ulResponseTime_)
  575. {
  576. ANT_MESSAGE stMessage;
  577. stMessage.ucMessageID = MESG_ID_LIST_ADD_ID;
  578. stMessage.aucData[0] = ucANTChannel_;
  579. stMessage.aucData[1] = (UCHAR)(usDeviceNumber_ & 0xFF);
  580. stMessage.aucData[2] = (UCHAR)((usDeviceNumber_ >>8) & 0xFF);
  581. stMessage.aucData[3] = ucDeviceType_;
  582. stMessage.aucData[4] = ucTransmissionType_;
  583. stMessage.aucData[5] = ucListIndex_;
  584. return SendCommand(&stMessage, MESG_ID_LIST_ADD_SIZE, ulResponseTime_);
  585. }
  586. ///////////////////////////////////////////////////////////////////////
  587. BOOL DSIFramerANT::AddCryptoID(UCHAR ucANTChannel_, UCHAR* pucData_, UCHAR ucListIndex_, ULONG ulResponseTime_)
  588. {
  589. ANT_MESSAGE stMessage;
  590. stMessage.ucMessageID = MESG_CRYPTO_ID_LIST_ADD_ID;
  591. stMessage.aucData[0] = ucANTChannel_;
  592. memcpy(&stMessage.aucData[1], pucData_, 4);
  593. stMessage.aucData[5] = ucListIndex_;
  594. return SendCommand(&stMessage, MESG_CRYPTO_ID_LIST_ADD_SIZE, ulResponseTime_);
  595. }
  596. ///////////////////////////////////////////////////////////////////////
  597. BOOL DSIFramerANT::ConfigList(UCHAR ucANTChannel_, UCHAR ucListSize_, UCHAR ucExclude_, ULONG ulResponseTime_)
  598. {
  599. ANT_MESSAGE stMessage;
  600. stMessage.ucMessageID = MESG_ID_LIST_CONFIG_ID;
  601. stMessage.aucData[0] = ucANTChannel_;
  602. stMessage.aucData[1] = ucListSize_;
  603. stMessage.aucData[2] = ucExclude_;
  604. return SendCommand(&stMessage, MESG_CRYPTO_ID_LIST_CONFIG_SIZE, ulResponseTime_);
  605. }
  606. ///////////////////////////////////////////////////////////////////////
  607. BOOL DSIFramerANT::ConfigCryptoList(UCHAR ucANTChannel_, UCHAR ucListSize_, UCHAR ucBlacklist_, ULONG ulResponseTime_)
  608. {
  609. ANT_MESSAGE stMessage;
  610. stMessage.ucMessageID = MESG_CRYPTO_ID_LIST_CONFIG_ID;
  611. stMessage.aucData[0] = ucANTChannel_;
  612. stMessage.aucData[1] = ucListSize_;
  613. stMessage.aucData[2] = ucBlacklist_;
  614. return SendCommand(&stMessage, MESG_ID_LIST_CONFIG_SIZE, ulResponseTime_);
  615. }
  616. ///////////////////////////////////////////////////////////////////////
  617. BOOL DSIFramerANT::OpenRxScanMode( ULONG ulResponseTime_)
  618. {
  619. ANT_MESSAGE stMessage;
  620. stMessage.ucMessageID = MESG_OPEN_RX_SCAN_ID;
  621. stMessage.aucData[0] = 0;
  622. return SendCommand(&stMessage, MESG_OPEN_RX_SCAN_SIZE, ulResponseTime_);
  623. }
  624. ///////////////////////////////////////////////////////////////////////
  625. // Priority: Any
  626. //
  627. // Called by the application to construct and send an extended broadcast data message.
  628. // This message will be broadcast on the next synchronous channel period.
  629. ///////////////////////////////////////////////////////////////////////
  630. BOOL DSIFramerANT::SendExtBroadcastData(UCHAR ucANTChannel_, UCHAR *pucData_)
  631. {
  632. ANT_MESSAGE stMessage;
  633. stMessage.ucMessageID = MESG_EXT_BROADCAST_DATA_ID;
  634. stMessage.aucData[0] = ucANTChannel_;
  635. memcpy(&stMessage.aucData[1],pucData_, MESG_EXT_DATA_SIZE-1);
  636. return WriteMessage(&stMessage, MESG_EXT_DATA_SIZE);
  637. }
  638. ///////////////////////////////////////////////////////////////////////
  639. // Priority: Any
  640. //
  641. // Called by the application to construct and send an extended acknowledged data
  642. // mesg. This message will be transmitted on the next synchronous channel
  643. // period.
  644. ///////////////////////////////////////////////////////////////////////
  645. BOOL DSIFramerANT::SendExtAcknowledgedData(UCHAR ucANTChannel_, UCHAR *pucData_, ULONG ulResponseTime_)
  646. {
  647. return(SetupAckDataTransfer(
  648. MESG_EXT_ACKNOWLEDGED_DATA_ID,
  649. ucANTChannel_,
  650. pucData_,
  651. MESG_EXT_DATA_SIZE,
  652. ulResponseTime_));
  653. }
  654. ///////////////////////////////////////////////////////////////////////
  655. // Priority: Any
  656. //
  657. // Used to send extended burst data with individual packets. Proper sequence number
  658. // of packet is maintained by the application.
  659. ///////////////////////////////////////////////////////////////////////
  660. BOOL DSIFramerANT::SendExtBurstTransferPacket(UCHAR ucANTChannelSeq_, UCHAR *pucData_)
  661. {
  662. ANT_MESSAGE stMessage;
  663. stMessage.ucMessageID = MESG_EXT_BURST_DATA_ID;
  664. stMessage.aucData[0] = ucANTChannelSeq_;
  665. memcpy(&stMessage.aucData[1],pucData_, MESG_EXT_DATA_SIZE-1);
  666. return WriteMessage(&stMessage, MESG_EXT_DATA_SIZE);
  667. }
  668. ///////////////////////////////////////////////////////////////////////
  669. // Priority: Any
  670. //
  671. // Used to send extended burst data using a block of data. Proper sequence number
  672. // of packet is maintained by the function. Useful for testing purposes.
  673. ///////////////////////////////////////////////////////////////////////
  674. ANTFRAMER_RETURN DSIFramerANT::SendExtBurstTransfer(UCHAR ucANTChannel_, UCHAR *pucData_, ULONG ulSize_, ULONG ulResponseTime_)
  675. {
  676. return(SetupBurstDataTransfer(
  677. MESG_EXT_BURST_DATA_ID,
  678. ucANTChannel_,
  679. pucData_,
  680. ulSize_,
  681. MESG_EXT_DATA_SIZE,
  682. ulResponseTime_));
  683. }
  684. ///////////////////////////////////////////////////////////////////////
  685. // Priority: Any
  686. //
  687. // Used to force the module to use extended rx messages all the time
  688. ///////////////////////////////////////////////////////////////////////
  689. BOOL DSIFramerANT::RxExtMesgsEnable(UCHAR ucEnable_, ULONG ulResponseTime_)
  690. {
  691. ANT_MESSAGE stMessage;
  692. stMessage.ucMessageID = MESG_RX_EXT_MESGS_ENABLE_ID;
  693. stMessage.aucData[0] = 0;
  694. stMessage.aucData[1] = ucEnable_;
  695. return SendCommand(&stMessage, MESG_RX_EXT_MESGS_ENABLE_SIZE, ulResponseTime_);
  696. }
  697. ///////////////////////////////////////////////////////////////////////
  698. // Priority: Any
  699. //
  700. // Used to set a channel device ID to the module serial number
  701. ///////////////////////////////////////////////////////////////////////
  702. BOOL DSIFramerANT::SetSerialNumChannelId(UCHAR ucANTChannel_, UCHAR ucDeviceType_, UCHAR ucTransmissionType_, ULONG ulResponseTime_)
  703. {
  704. ANT_MESSAGE stMessage;
  705. stMessage.ucMessageID = MESG_SERIAL_NUM_SET_CHANNEL_ID_ID;
  706. stMessage.aucData[0] = ucANTChannel_;
  707. stMessage.aucData[1] = ucDeviceType_;
  708. stMessage.aucData[2] = ucTransmissionType_;
  709. return SendCommand(&stMessage, MESG_SERIAL_NUM_SET_CHANNEL_ID_SIZE, ulResponseTime_);
  710. }
  711. ///////////////////////////////////////////////////////////////////////
  712. // Priority: Any
  713. //
  714. // Enables the module LED to flash on RF activity
  715. ///////////////////////////////////////////////////////////////////////
  716. BOOL DSIFramerANT::EnableLED(UCHAR ucEnable_, ULONG ulResponseTime_)
  717. {
  718. ANT_MESSAGE stMessage;
  719. stMessage.ucMessageID = MESG_ENABLE_LED_FLASH_ID;
  720. stMessage.aucData[0] = 0;
  721. stMessage.aucData[1] = ucEnable_;
  722. return SendCommand(&stMessage, MESG_ENABLE_LED_FLASH_SIZE, ulResponseTime_);
  723. }
  724. ///////////////////////////////////////////////////////////////////////
  725. BOOL DSIFramerANT::ConfigAdvancedBurst_ext (BOOL bEnable_, UCHAR ucMaxPacketLength_,
  726. ULONG ulRequiredFields_, ULONG ulOptionalFields_,
  727. USHORT usStallCount_, UCHAR ucRetryCount_, ULONG ulResponseTime_)
  728. {
  729. ANT_MESSAGE stMessage;
  730. stMessage.ucMessageID = MESG_CONFIG_ADV_BURST_ID;
  731. stMessage.aucData[0] = 0;
  732. stMessage.aucData[1] = (bEnable_) ? 1 : 0;
  733. stMessage.aucData[2] = ucMaxPacketLength_;
  734. stMessage.aucData[3] = (UCHAR)(ulRequiredFields_ & 0xFF);
  735. stMessage.aucData[4] = (UCHAR)((ulRequiredFields_ >> 8) & 0xFF);
  736. stMessage.aucData[5] = (UCHAR)((ulRequiredFields_ >> 16) & 0xFF);
  737. stMessage.aucData[6] = (UCHAR)(ulOptionalFields_ & 0xFF);
  738. stMessage.aucData[7] = (UCHAR)((ulOptionalFields_ >> 8) & 0xFF);
  739. stMessage.aucData[8] = (UCHAR)((ulOptionalFields_ >> 16) & 0xFF);
  740. stMessage.aucData[9] = (UCHAR)(usStallCount_ & 0xFF);
  741. stMessage.aucData[10] = (UCHAR)((usStallCount_ >> 8) & 0xFF);
  742. stMessage.aucData[11] = ucRetryCount_;
  743. return SendCommand(&stMessage, MESG_CONFIG_ADV_BURST_SIZE_EXT, ulResponseTime_);
  744. }
  745. ///////////////////////////////////////////////////////////////////////
  746. BOOL DSIFramerANT::ConfigAdvancedBurst (BOOL bEnable_, UCHAR ucMaxPacketLength_,
  747. ULONG ulRequiredFields_, ULONG ulOptionalFields_,
  748. ULONG ulResponseTime_)
  749. {
  750. ANT_MESSAGE stMessage;
  751. stMessage.ucMessageID = MESG_CONFIG_ADV_BURST_ID;
  752. stMessage.aucData[0] = 0;
  753. stMessage.aucData[1] = (bEnable_) ? 1 : 0;
  754. stMessage.aucData[2] = ucMaxPacketLength_;
  755. stMessage.aucData[3] = (UCHAR)(ulRequiredFields_ & 0xFF);
  756. stMessage.aucData[4] = (UCHAR)((ulRequiredFields_ >> 8) & 0xFF);
  757. stMessage.aucData[5] = (UCHAR)((ulRequiredFields_ >> 16) & 0xFF);
  758. stMessage.aucData[6] = (UCHAR)(ulOptionalFields_ & 0xFF);
  759. stMessage.aucData[7] = (UCHAR)((ulOptionalFields_ >> 8) & 0xFF);
  760. stMessage.aucData[8] = (UCHAR)((ulOptionalFields_ >> 16) & 0xFF);
  761. return SendCommand(&stMessage, MESG_CONFIG_ADV_BURST_SIZE, ulResponseTime_);
  762. }
  763. ///////////////////////////////////////////////////////////////////////
  764. BOOL DSIFramerANT::EncryptedChannelEnable(UCHAR ucANTChannel_, UCHAR ucMode_,
  765. UCHAR ucVolatileKeyIndex_, UCHAR ucDecimationRate_,
  766. ULONG ulResponseTime_)
  767. {
  768. ANT_MESSAGE stMessage;
  769. stMessage.ucMessageID = MESG_ENCRYPT_ENABLE_ID;
  770. stMessage.aucData[0] = ucANTChannel_;
  771. stMessage.aucData[1] = ucMode_;
  772. stMessage.aucData[2] = ucVolatileKeyIndex_;
  773. stMessage.aucData[3] = ucDecimationRate_;
  774. return SendCommand(&stMessage, MESG_ENCRYPT_ENABLE_SIZE, ulResponseTime_);
  775. }
  776. ///////////////////////////////////////////////////////////////////////
  777. BOOL DSIFramerANT::SetCryptoKey(UCHAR ucVolatileKeyIndex_, UCHAR *pucKey_, ULONG ulResponseTime_)
  778. {
  779. ANT_MESSAGE stMessage;
  780. stMessage.ucMessageID = MESG_SET_CRYPTO_KEY_ID;
  781. stMessage.aucData[0] = ucVolatileKeyIndex_;
  782. memcpy(&stMessage.aucData[1], pucKey_, 16);
  783. return SendCommand(&stMessage, MESG_SET_CRYPTO_KEY_SIZE, ulResponseTime_);
  784. }
  785. ///////////////////////////////////////////////////////////////////////
  786. BOOL DSIFramerANT::SetCryptoID(UCHAR *pucData_, ULONG ulResponseTime_)
  787. {
  788. ANT_MESSAGE stMessage;
  789. stMessage.ucMessageID = MESG_SET_CRYPTO_INFO_ID;
  790. stMessage.aucData[0] = 0;
  791. memcpy(&stMessage.aucData[1], pucData_, 4);
  792. return SendCommand(&stMessage, MESG_SET_CRYPTO_ID_SIZE, ulResponseTime_);
  793. }
  794. ///////////////////////////////////////////////////////////////////////
  795. BOOL DSIFramerANT::SetCryptoUserInfo(UCHAR *pucData_, ULONG ulResponseTime_)
  796. {
  797. ANT_MESSAGE stMessage;
  798. stMessage.ucMessageID = MESG_SET_CRYPTO_INFO_ID;
  799. stMessage.aucData[0] = 1;
  800. memcpy(&stMessage.aucData[1], pucData_, 19);
  801. return SendCommand(&stMessage, MESG_SET_CRYPTO_USER_INFO_SIZE, ulResponseTime_);
  802. }
  803. ///////////////////////////////////////////////////////////////////////
  804. BOOL DSIFramerANT::SetCryptoRNGSeed(UCHAR *pucData_, ULONG ulResponseTime_)
  805. {
  806. ANT_MESSAGE stMessage;
  807. stMessage.ucMessageID = MESG_SET_CRYPTO_INFO_ID;
  808. stMessage.aucData[0] = 2;
  809. memcpy(&stMessage.aucData[1], pucData_, 16);
  810. return SendCommand(&stMessage, MESG_SET_CRYPTO_RNG_SEED_SIZE, ulResponseTime_);
  811. }
  812. ///////////////////////////////////////////////////////////////////////
  813. BOOL DSIFramerANT::SetCryptoInfo(UCHAR ucSetParameter_, UCHAR *pucData_, ULONG ulResponseTime_)
  814. {
  815. switch(ucSetParameter_)
  816. {
  817. case 0:
  818. default:
  819. return SetCryptoID(pucData_, ulResponseTime_);
  820. case 1:
  821. return SetCryptoUserInfo(pucData_, ulResponseTime_);
  822. case 2:
  823. return SetCryptoRNGSeed(pucData_, ulResponseTime_);
  824. }
  825. }
  826. ///////////////////////////////////////////////////////////////////////
  827. BOOL DSIFramerANT::LoadCryptoKeyNVMOp(UCHAR ucNVMKeyIndex_, UCHAR ucVolatileKeyIndex_, ULONG ulResponseTime_)
  828. {
  829. ANT_MESSAGE stMessage;
  830. stMessage.ucMessageID = MESG_NVM_CRYPTO_KEY_OPS_ID;
  831. stMessage.aucData[0] = 0;
  832. stMessage.aucData[1] = ucNVMKeyIndex_;
  833. stMessage.aucData[2] = ucVolatileKeyIndex_;
  834. return SendCommand(&stMessage, MESG_NVM_CRYPTO_KEY_LOAD_SIZE, ulResponseTime_);
  835. }
  836. ///////////////////////////////////////////////////////////////////////
  837. BOOL DSIFramerANT::StoreCryptoKeyNVMOp(UCHAR ucNVMKeyIndex_, UCHAR *pucKey_, ULONG ulResponseTime_)
  838. {
  839. ANT_MESSAGE stMessage;
  840. stMessage.ucMessageID = MESG_NVM_CRYPTO_KEY_OPS_ID;
  841. stMessage.aucData[0] = 1;
  842. stMessage.aucData[1] = ucNVMKeyIndex_;
  843. memcpy(&stMessage.aucData[2], pucKey_, 16);
  844. return SendCommand(&stMessage, MESG_NVM_CRYPTO_KEY_STORE_SIZE, ulResponseTime_);
  845. }
  846. ///////////////////////////////////////////////////////////////////////
  847. BOOL DSIFramerANT::CryptoKeyNVMOp(UCHAR ucOperation_, UCHAR ucNVMKeyIndex_, UCHAR *pucData_, ULONG ulResponseTime_)
  848. {
  849. switch(ucOperation_)
  850. {
  851. case 0:
  852. default:
  853. return LoadCryptoKeyNVMOp(ucNVMKeyIndex_, *pucData_, ulResponseTime_);
  854. case 1:
  855. return StoreCryptoKeyNVMOp(ucNVMKeyIndex_, pucData_, ulResponseTime_);
  856. }
  857. }
  858. ///////////////////////////////////////////////////////////////////////
  859. BOOL DSIFramerANT::RequestMessage(UCHAR ucChannel_, UCHAR ucMessageID_)
  860. {
  861. ANT_MESSAGE_ITEM stResponse;
  862. if (SendRequest(ucMessageID_, ucChannel_, &stResponse, 0) == FALSE)
  863. return FALSE;
  864. return TRUE;
  865. }
  866. ///////////////////////////////////////////////////////////////////////
  867. BOOL DSIFramerANT::ScriptWrite( UCHAR ucSize_, UCHAR *pucCmdData_, ULONG ulResponseTime_)
  868. {
  869. ANT_MESSAGE stMessage;
  870. stMessage.ucMessageID = MESG_SCRIPT_DATA_ID;
  871. memcpy(stMessage.aucData, pucCmdData_, ucSize_);
  872. return SendCommand(&stMessage, ucSize_, ulResponseTime_);
  873. }
  874. ///////////////////////////////////////////////////////////////////////
  875. BOOL DSIFramerANT::ScriptClear(ULONG ulResponseTime_ )
  876. {
  877. ANT_MESSAGE stMessage;
  878. stMessage.ucMessageID = MESG_SCRIPT_CMD_ID;
  879. stMessage.aucData[0] = 0;
  880. stMessage.aucData[1] = SCRIPT_CMD_FORMAT;
  881. stMessage.aucData[2] = 0;
  882. return SendCommand(&stMessage, MESG_SCRIPT_CMD_SIZE, ulResponseTime_);
  883. }
  884. ///////////////////////////////////////////////////////////////////////
  885. BOOL DSIFramerANT::ScriptSetDefaultSector( UCHAR ucSectNumber_, ULONG ulResponseTime_ )
  886. {
  887. ANT_MESSAGE stMessage;
  888. stMessage.ucMessageID = MESG_SCRIPT_CMD_ID;
  889. stMessage.aucData[0] = 0;
  890. stMessage.aucData[1] = SCRIPT_CMD_SET_DEFAULT_SECTOR;
  891. stMessage.aucData[2] = ucSectNumber_;
  892. return SendCommand(&stMessage, MESG_SCRIPT_CMD_SIZE, ulResponseTime_);
  893. }
  894. ///////////////////////////////////////////////////////////////////////
  895. BOOL DSIFramerANT::ScriptEndSector( ULONG ulResponseTime_ )
  896. {
  897. ANT_MESSAGE stMessage;
  898. stMessage.ucMessageID = MESG_SCRIPT_CMD_ID;
  899. stMessage.aucData[0] = 0;
  900. stMessage.aucData[1] = SCRIPT_CMD_END_SECTOR;
  901. stMessage.aucData[2] = 0;
  902. return SendCommand(&stMessage, MESG_SCRIPT_CMD_SIZE, ulResponseTime_);
  903. }
  904. ///////////////////////////////////////////////////////////////////////
  905. BOOL DSIFramerANT::ScriptDump( ULONG ulResponseTime_ )
  906. {
  907. ANT_MESSAGE stMessage;
  908. stMessage.ucMessageID = MESG_SCRIPT_CMD_ID;
  909. stMessage.aucData[0] = 0;
  910. stMessage.aucData[1] = SCRIPT_CMD_DUMP;
  911. stMessage.aucData[2] = 0;
  912. return SendCommand(&stMessage, MESG_SCRIPT_CMD_SIZE, ulResponseTime_);
  913. }
  914. ///////////////////////////////////////////////////////////////////////
  915. BOOL DSIFramerANT::ScriptLock( ULONG ulResponseTime_ )
  916. {
  917. ANT_MESSAGE stMessage;
  918. stMessage.ucMessageID = MESG_SCRIPT_CMD_ID;
  919. stMessage.aucData[0] = 0;
  920. stMessage.aucData[1] = SCRIPT_CMD_LOCK;
  921. stMessage.aucData[2] = 0;
  922. return SendCommand(&stMessage, MESG_SCRIPT_CMD_SIZE, ulResponseTime_);
  923. }
  924. ///////////////////////////////////////////////////////////////////////
  925. BOOL DSIFramerANT::FITSetFEState(UCHAR ucFEState_, ULONG ulResponseTime_)
  926. {
  927. ANT_MESSAGE stMessage;
  928. stMessage.ucMessageID = MESG_FIT1_SET_EQUIP_STATE_ID;
  929. stMessage.aucData[0] = 0;
  930. stMessage.aucData[1] = ucFEState_;
  931. return SendCommand(&stMessage, MESG_FIT1_SET_EQUIP_STATE_SIZE, ulResponseTime_);
  932. }
  933. ///////////////////////////////////////////////////////////////////////
  934. BOOL DSIFramerANT::FITAdjustPairingSettings(UCHAR ucSearchLv_, UCHAR ucPairLv_, UCHAR ucTrackLv_, ULONG ulResponseTime_)
  935. {
  936. ANT_MESSAGE stMessage;
  937. stMessage.ucMessageID = MESG_FIT1_SET_AGC_ID;
  938. stMessage.aucData[0] = 0;
  939. stMessage.aucData[1] = ucSearchLv_;
  940. stMessage.aucData[2] = ucPairLv_;
  941. stMessage.aucData[3] = ucTrackLv_;
  942. return SendCommand(&stMessage, MESG_FIT1_SET_AGC_SIZE, ulResponseTime_);
  943. }
  944. ///////////////////////////////////////////////////////////////////////
  945. BOOL DSIFramerANT::GetCapabilities(UCHAR *pucCapabilities_, ULONG ulResponseTime_)
  946. {
  947. //Get Capabilities using new Extended method
  948. UCHAR ucBasicCapabilities = ANT_BASIC_CAPABILITIES_SIZE;
  949. return GetCapabilitiesExt(pucCapabilities_, &ucBasicCapabilities, ulResponseTime_);
  950. }
  951. ///////////////////////////////////////////////////////////////////////
  952. BOOL DSIFramerANT::GetCapabilitiesExt(UCHAR *pucCapabilities_, UCHAR *pucMaxBytes_, ULONG ulResponseTime_)
  953. {
  954. ANT_MESSAGE_ITEM stResponse;
  955. UCHAR ucBytes;
  956. UCHAR ucCount;
  957. //Sanity check because we can't populate the array if we don't know how big it is.
  958. if ( pucCapabilities_ != NULL && pucMaxBytes_ == NULL )
  959. return FALSE;
  960. //Send request, if it returns successfully copy the array
  961. if (SendRequest(MESG_CAPABILITIES_ID, 0, &stResponse, ulResponseTime_) == FALSE)
  962. return FALSE;
  963. if (pucCapabilities_ == NULL || ulResponseTime_ == 0)
  964. return TRUE;
  965. //If Max Bytes > Response then use response size, else limit the size based on how many bytes the buffer can handle
  966. ucBytes = (*pucMaxBytes_ > stResponse.ucSize) ? stResponse.ucSize : *pucMaxBytes_;
  967. for (ucCount = 0; ucCount < ucBytes; ucCount++)
  968. {
  969. *pucCapabilities_++ = stResponse.stANTMessage.aucData[ucCount];
  970. }
  971. //Update the pointer to indicate how many bytes we copied.
  972. *pucMaxBytes_ = ucBytes;
  973. return TRUE;
  974. }
  975. ///////////////////////////////////////////////////////////////////////
  976. BOOL DSIFramerANT::GetChannelID(UCHAR ucANTChannel_, USHORT *pusDeviceNumber_, UCHAR *pucDeviceType_, UCHAR *pucTransmitType_, ULONG ulResponseTime_)
  977. {
  978. ANT_MESSAGE_ITEM stResponse;
  979. if (SendRequest(MESG_CHANNEL_ID_ID, ucANTChannel_, &stResponse, ulResponseTime_) == FALSE)
  980. return FALSE;
  981. if(pusDeviceNumber_ == NULL || pucDeviceType_ == NULL || pucTransmitType_ == NULL || ulResponseTime_ == 0)
  982. return TRUE;
  983. *pusDeviceNumber_ = ((USHORT)stResponse.stANTMessage.aucData[1]|(USHORT)stResponse.stANTMessage.aucData[2]<<8);
  984. *pucDeviceType_ = stResponse.stANTMessage.aucData[3];
  985. *pucTransmitType_ = stResponse.stANTMessage.aucData[4];
  986. return TRUE;
  987. }
  988. ///////////////////////////////////////////////////////////////////////
  989. BOOL DSIFramerANT::GetChannelStatus(UCHAR ucANTChannel_, UCHAR *pucStatus_, ULONG ulResponseTime_)
  990. {
  991. ANT_MESSAGE_ITEM stResponse;
  992. if (SendRequest(MESG_CHANNEL_STATUS_ID, ucANTChannel_, &stResponse, ulResponseTime_) == FALSE)
  993. return FALSE;
  994. if (pucStatus_ == NULL || ulResponseTime_ == 0)
  995. return TRUE;
  996. *pucStatus_ = stResponse.stANTMessage.aucData[1];
  997. return TRUE;
  998. }
  999. ///////////////////////////////////////////////////////////////////////
  1000. BOOL DSIFramerANT::ResetSystem(ULONG ulResponseTime_)
  1001. {
  1002. ANT_MESSAGE stMessage;
  1003. stMessage.ucMessageID = MESG_SYSTEM_RESET_ID;
  1004. stMessage.aucData[0] = 0;
  1005. //// Modified SendCommand() function. ////
  1006. ANTMessageResponse *pclCommandResponse = (ANTMessageResponse*)NULL;
  1007. // If we are going to be waiting for a response setup the Response object
  1008. if (ulResponseTime_ != 0)
  1009. {
  1010. pclCommandResponse = new ANTMessageResponse();
  1011. pclCommandResponse->Attach(MESG_STARTUP_MESG_ID, (UCHAR*)NULL, 0, this);
  1012. }
  1013. // Write the command message.
  1014. if (!WriteMessage(&stMessage, MESG_SYSTEM_RESET_SIZE))
  1015. {
  1016. #if defined(DEBUG_FILE)
  1017. DSIDebug::ThreadWrite("Framer->ResetSystem(): WriteMessage Failed.");
  1018. #endif
  1019. if(pclCommandResponse != NULL)
  1020. {
  1021. delete pclCommandResponse;
  1022. pclCommandResponse = (ANTMessageResponse*)NULL;
  1023. }
  1024. return FALSE;
  1025. }
  1026. // Return immediately if we aren't waiting for the response.
  1027. if (ulResponseTime_ == 0)
  1028. return TRUE;
  1029. // Wait for the response.
  1030. pclCommandResponse->WaitForResponse(ulResponseTime_);
  1031. pclCommandResponse->Remove(); //detach from list
  1032. // We haven't received a response in the allotted time.
  1033. //if (pclCommandResponse->stMessageItem.ucSize == 0)
  1034. if (pclCommandResponse->bResponseReady == FALSE)
  1035. {
  1036. #if defined(DEBUG_FILE)
  1037. DSIDebug::ThreadWrite("Framer->ResetSystem(): Timeout.");
  1038. #endif
  1039. delete pclCommandResponse;
  1040. return FALSE;
  1041. }
  1042. delete pclCommandResponse;
  1043. return TRUE;
  1044. }
  1045. ///////////////////////////////////////////////////////////////////////
  1046. BOOL DSIFramerANT::OpenChannel(UCHAR ucANTChannel_, ULONG ulResponseTime_)
  1047. {
  1048. ANT_MESSAGE stMessage;
  1049. stMessage.ucMessageID = MESG_OPEN_CHANNEL_ID;
  1050. stMessage.aucData[0] = ucANTChannel_;
  1051. return SendCommand(&stMessage, MESG_OPEN_CHANNEL_SIZE, ulResponseTime_);
  1052. }
  1053. ///////////////////////////////////////////////////////////////////////
  1054. BOOL DSIFramerANT::SetRSSISearchThreshold(UCHAR ucANTChannel_, UCHAR ucSearchThreshold_, ULONG ulResponseTime_)
  1055. {
  1056. ANT_MESSAGE stMessage;
  1057. stMessage.ucMessageID = MESG_RSSI_SEARCH_THRESHOLD_ID;
  1058. stMessage.aucData[0] = ucANTChannel_;
  1059. stMessage.aucData[1] = ucSearchThreshold_;
  1060. return SendCommand(&stMessage, MESG_RSSI_SEARCH_THRESHOLD_SIZE, ulResponseTime_);
  1061. }
  1062. ///////////////////////////////////////////////////////////////////////
  1063. BOOL DSIFramerANT::CloseChannel(UCHAR ucANTChannel_, ULONG ulResponseTime_)
  1064. {
  1065. BOOL bReturn;
  1066. ANT_MESSAGE stMessage;
  1067. ANTMessageResponse *pclEventResponse = (ANTMessageResponse*)NULL;
  1068. stMessage.ucMessageID = MESG_CLOSE_CHANNEL_ID;
  1069. stMessage.aucData[0] = ucANTChannel_;
  1070. // If we are going to be waiting for a response setup the Response object to wait for the channel closed event
  1071. if (ulResponseTime_ != 0)
  1072. {
  1073. UCHAR aucDesiredData[3];
  1074. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_;
  1075. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1076. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_CHANNEL_CLOSED;
  1077. pclEventResponse = new ANTMessageResponse();
  1078. pclEventResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this);
  1079. }
  1080. bReturn = SendCommand(&stMessage, MESG_CLOSE_CHANNEL_SIZE, ulResponseTime_);
  1081. if (ulResponseTime_ == 0)
  1082. return bReturn;
  1083. if (bReturn == TRUE)
  1084. {
  1085. // Wait for the close event.
  1086. bReturn = pclEventResponse->WaitForResponse(ulResponseTime_);
  1087. }
  1088. pclEventResponse->Remove(); //detach from list
  1089. delete pclEventResponse;
  1090. return bReturn;
  1091. }
  1092. ///////////////////////////////////////////////////////////////////////
  1093. BOOL DSIFramerANT::SendBroadcastData(UCHAR ucANTChannel_, UCHAR *pucData_)
  1094. {
  1095. ANT_MESSAGE stMessage;
  1096. stMessage.ucMessageID = MESG_BROADCAST_DATA_ID;
  1097. stMessage.aucData[0] = ucANTChannel_;
  1098. memcpy(&stMessage.aucData[1],pucData_, ANT_STANDARD_DATA_PAYLOAD_SIZE);
  1099. return WriteMessage(&stMessage, ANT_STANDARD_DATA_PAYLOAD_SIZE + MESG_CHANNEL_NUM_SIZE);
  1100. }
  1101. ///////////////////////////////////////////////////////////////////////
  1102. BOOL DSIFramerANT::SendBurstDataPacket(UCHAR ucANTChannelSeq_, UCHAR *pucData_)
  1103. {
  1104. ANT_MESSAGE stMessage;
  1105. stMessage.ucMessageID = MESG_BURST_DATA_ID;
  1106. stMessage.aucData[0] = ucANTChannelSeq_;
  1107. memcpy(&stMessage.aucData[1],pucData_, MESG_DATA_SIZE-1);
  1108. return WriteMessage(&stMessage, MESG_DATA_SIZE);
  1109. }
  1110. ///////////////////////////////////////////////////////////////////////
  1111. BOOL DSIFramerANT::SendAdvancedBurstDataPacket(UCHAR ucANTChannelSeq_, UCHAR *pucData_, UCHAR ucStdPcktsPerSerialMsg_)
  1112. {
  1113. ANT_MESSAGE* stMessage = (ANT_MESSAGE*) NULL;
  1114. USHORT packetDataSize = (MESG_DATA_SIZE-1) * ucStdPcktsPerSerialMsg_;
  1115. if(!CreateAntMsg_wOptExtBuf(&stMessage, packetDataSize+1)) //reqMinSize = packetSize + seqNum
  1116. {
  1117. #if defined(DEBUG_FILE)
  1118. DSIDebug::ThreadWrite("Framer->SendAdvancedBurstDataPacket(): Failed, ucStdPcktsPerSerialMsg too big for this part");
  1119. #endif
  1120. return FALSE;
  1121. }
  1122. stMessage->ucMessageID = MESG_ADV_BURST_DATA_ID;
  1123. stMessage->aucData[0] = ucANTChannelSeq_;
  1124. memcpy(&(stMessage->aucData[1]),pucData_, packetDataSize);
  1125. BOOL result = WriteMessage(stMessage, 1 + packetDataSize); //seqNum + (payloadLength*pcktsPerSerial)
  1126. delete[] stMessage;
  1127. return result;
  1128. }
  1129. ///////////////////////////////////////////////////////////////////////
  1130. ANTFRAMER_RETURN DSIFramerANT::SendAcknowledgedData(UCHAR ucANTChannel_, UCHAR *pucData_, ULONG ulResponseTime_)
  1131. {
  1132. return(SetupAckDataTransfer(
  1133. MESG_ACKNOWLEDGED_DATA_ID,
  1134. ucANTChannel_,
  1135. pucData_,
  1136. MESG_DATA_SIZE,
  1137. ulResponseTime_));
  1138. }
  1139. ANTFRAMER_RETURN DSIFramerANT::SetupAckDataTransfer(UCHAR ucMessageID_, UCHAR ucANTChannel_, UCHAR *pucData_, UCHAR ucMaxDataSize_, ULONG ulResponseTime_)
  1140. {
  1141. ANTFRAMER_RETURN eReturn = ANTFRAMER_PASS;
  1142. ANT_MESSAGE stMessage;
  1143. ULONG ulStartTime = DSIThread_GetSystemTime();
  1144. ANTMessageResponse *pclPassResponse = (ANTMessageResponse*)NULL;
  1145. ANTMessageResponse *pclFailResponse = (ANTMessageResponse*)NULL;
  1146. ANTMessageResponse *pclErrorResponse = (ANTMessageResponse*)NULL;
  1147. volatile BOOL *pbCancel_;
  1148. BOOL bDummyCancel = FALSE;
  1149. if (pbCancel == NULL)
  1150. pbCancel_ = &bDummyCancel;
  1151. else
  1152. pbCancel_ = pbCancel;
  1153. stMessage.ucMessageID = ucMessageID_;
  1154. stMessage.aucData[0] = ucANTChannel_;
  1155. memcpy(&stMessage.aucData[1],pucData_, ucMaxDataSize_-1);
  1156. // If we are going to be waiting for a response setup the Response objects
  1157. if (ulResponseTime_ != 0)
  1158. {
  1159. UCHAR aucDesiredData[3];
  1160. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx complete
  1161. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1162. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_COMPLETED;
  1163. pclPassResponse = new ANTMessageResponse();
  1164. pclPassResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this);
  1165. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx fail
  1166. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1167. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_FAILED;
  1168. pclFailResponse = new ANTMessageResponse();
  1169. pclFailResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this, pclPassResponse->pstCondResponseReady);
  1170. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch any errors like transfer in progress.
  1171. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = ucMessageID_;
  1172. pclErrorResponse = new ANTMessageResponse();
  1173. pclErrorResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, 2, this, pclPassResponse->pstCondResponseReady);
  1174. }
  1175. if (WriteMessage(&stMessage, ucMaxDataSize_) == FALSE)
  1176. eReturn = ANTFRAMER_FAIL;
  1177. // Return immediately if we aren't waiting for the response.
  1178. if (ulResponseTime_ == 0)
  1179. return ANTFRAMER_PASS;
  1180. DSIThread_MutexLock(&stMutexResponseRequest);
  1181. if (eReturn == ANTFRAMER_PASS) //Only try to wait if we haven't failed yet
  1182. {
  1183. while((pclPassResponse->bResponseReady == FALSE) &&
  1184. (pclFailResponse->bResponseReady == FALSE) &&
  1185. (pclErrorResponse->bResponseReady == FALSE) &&
  1186. (*pbCancel_ == FALSE) &&
  1187. ((DSIThread_GetSystemTime() - ulStartTime) < ulResponseTime_))
  1188. {
  1189. DSIThread_CondTimedWait(pclPassResponse->pstCondResponseReady, &stMutexResponseRequest, 1000);
  1190. }
  1191. if (pclPassResponse->bResponseReady == FALSE) //The only time we are sucessful is if we get a tx transfer complete
  1192. {
  1193. //figure out the reason why we failed/stopped
  1194. if ((pclErrorResponse->bResponseReady == TRUE) || (pclFailResponse->bResponseReady == TRUE))
  1195. eReturn = ANTFRAMER_FAIL;
  1196. else if (*pbCancel_ == TRUE)
  1197. eReturn = ANTFRAMER_CANCELLED;
  1198. else
  1199. eReturn = ANTFRAMER_TIMEOUT;
  1200. }
  1201. }
  1202. pclPassResponse->Remove();
  1203. pclFailResponse->Remove();
  1204. pclErrorResponse->Remove();
  1205. DSIThread_MutexUnlock(&stMutexResponseRequest);
  1206. delete pclPassResponse;
  1207. delete pclFailResponse;
  1208. delete pclErrorResponse;
  1209. return eReturn;
  1210. }
  1211. ANTFRAMER_RETURN DSIFramerANT::SetupBurstDataTransfer(UCHAR ucMessageID_, UCHAR ucANTChannel_, UCHAR * pucData_, ULONG ulSize_,UCHAR ucMaxDataSize_, ULONG ulResponseTime_)
  1212. {
  1213. ANTFRAMER_RETURN eReturn = ANTFRAMER_PASS;
  1214. ULONG ulStartTime = DSIThread_GetSystemTime();
  1215. ANTMessageResponse *pclPassResponse = (ANTMessageResponse*)NULL;
  1216. ANTMessageResponse *pclFailResponse = (ANTMessageResponse*)NULL;
  1217. ANTMessageResponse *pclErrorResponse = (ANTMessageResponse*)NULL;
  1218. ANT_MESSAGE* stMessage = (ANT_MESSAGE*)NULL;
  1219. if(!CreateAntMsg_wOptExtBuf(&stMessage, ucMaxDataSize_))
  1220. {
  1221. #if defined(DEBUG_FILE)
  1222. DSIDebug::ThreadWrite("Framer->SetupBurstDataTransfer(): Failed, ucMaxDataSize_ too big for this part");
  1223. #endif
  1224. return ANTFRAMER_INVALIDPARAM;
  1225. }
  1226. volatile BOOL *pbCancel_;
  1227. BOOL bDummyCancel = FALSE;
  1228. if (pbCancel == NULL)
  1229. pbCancel_ = &bDummyCancel;
  1230. else
  1231. pbCancel_ = pbCancel;
  1232. UCHAR aucDesiredData[3];
  1233. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx complete
  1234. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1235. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_COMPLETED;
  1236. pclPassResponse = new ANTMessageResponse();
  1237. pclPassResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this);
  1238. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx fail
  1239. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1240. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_FAILED;
  1241. pclFailResponse = new ANTMessageResponse();
  1242. pclFailResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this, pclPassResponse->pstCondResponseReady);
  1243. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch any errors like transfer in progress.
  1244. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = ucMessageID_;
  1245. pclErrorResponse = new ANTMessageResponse();
  1246. pclErrorResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, 2, this, pclPassResponse->pstCondResponseReady);
  1247. //getting error Rx will also effectively lose the transfer, but only on an AP1
  1248. stMessage->ucMessageID = ucMessageID_;
  1249. stMessage->aucData[0] = ucANTChannel_ & CHANNEL_NUMBER_MASK; // Clear the sequence bits
  1250. while ((eReturn == ANTFRAMER_PASS) && ulSize_)
  1251. {
  1252. if (ulSize_ > (UCHAR)(ucMaxDataSize_-1))
  1253. {
  1254. memcpy (&stMessage->aucData[1],pucData_,ucMaxDataSize_-1);
  1255. ulSize_ -= ucMaxDataSize_-1;
  1256. pucData_ += ucMaxDataSize_-1;
  1257. }
  1258. else
  1259. {
  1260. while((UCHAR)(ucMaxDataSize_-9) >= ulSize_)
  1261. ucMaxDataSize_ -= 8; //Shorten the last packet by 8-bytes if possible.
  1262. stMessage->aucData[0] |= SEQUENCE_LAST_MESSAGE;
  1263. memset (&stMessage->aucData[1], 0x00, ucMaxDataSize_-1);
  1264. memcpy (&stMessage->aucData[1],pucData_,ulSize_);
  1265. ulSize_ = 0;
  1266. }
  1267. if (WriteMessage(stMessage,ucMaxDataSize_) == FALSE)
  1268. eReturn = ANTFRAMER_FAIL;
  1269. //Adjust sequence number
  1270. if ((stMessage->aucData[0] & SEQUENCE_NUMBER_MASK) == SEQUENCE_NUMBER_ROLLOVER)
  1271. stMessage->aucData[0] = SEQUENCE_NUMBER_INC | ucANTChannel_;
  1272. else
  1273. stMessage->aucData[0] += SEQUENCE_NUMBER_INC;
  1274. if (*pbCancel_ == TRUE)
  1275. eReturn = ANTFRAMER_CANCELLED;
  1276. //Abort transfer on errors, don't keep trying to send
  1277. if ((pclFailResponse->bResponseReady == TRUE) || (pclErrorResponse->bResponseReady == TRUE))
  1278. eReturn = ANTFRAMER_FAIL;
  1279. if (ulResponseTime_ != 0) //Check for errors
  1280. {
  1281. if ((DSIThread_GetSystemTime() - ulStartTime) > ulResponseTime_)
  1282. eReturn = ANTFRAMER_TIMEOUT;
  1283. }
  1284. }
  1285. DSIThread_MutexLock(&stMutexResponseRequest);
  1286. if (ulResponseTime_ != 0) //Check for errors
  1287. {
  1288. if (eReturn == ANTFRAMER_PASS) //Only try to wait if we haven't failed yet
  1289. {
  1290. while((pclPassResponse->bResponseReady == FALSE) &&
  1291. (pclFailResponse->bResponseReady == FALSE) &&
  1292. (pclErrorResponse->bResponseReady == FALSE) &&
  1293. (*pbCancel_ == FALSE) &&
  1294. ((DSIThread_GetSystemTime() - ulStartTime) < ulResponseTime_))
  1295. {
  1296. DSIThread_CondTimedWait(pclPassResponse->pstCondResponseReady, &stMutexResponseRequest, 1000);
  1297. }
  1298. if (pclPassResponse->bResponseReady == FALSE) //The only time we are sucessful is if we get a tx transfer complete
  1299. {
  1300. //figure out the reason why we failed/stopped
  1301. if ((pclErrorResponse->bResponseReady == TRUE) || (pclFailResponse->bResponseReady == TRUE))
  1302. eReturn = ANTFRAMER_FAIL;
  1303. else if (*pbCancel_ == TRUE)
  1304. eReturn = ANTFRAMER_CANCELLED;
  1305. else
  1306. eReturn = ANTFRAMER_TIMEOUT;
  1307. }
  1308. }
  1309. }
  1310. pclPassResponse->Remove();
  1311. pclFailResponse->Remove();
  1312. pclErrorResponse->Remove();
  1313. DSIThread_MutexUnlock(&stMutexResponseRequest);
  1314. delete pclPassResponse;
  1315. delete pclFailResponse;
  1316. delete pclErrorResponse;
  1317. delete[] stMessage;
  1318. //Always return true with no timeout, so nobody relies on this return value
  1319. if(ulResponseTime_ == 0)
  1320. eReturn = ANTFRAMER_PASS;
  1321. return eReturn;
  1322. }
  1323. ///////////////////////////////////////////////////////////////////////
  1324. ANTFRAMER_RETURN DSIFramerANT::SendTransfer(UCHAR ucANTChannel_, UCHAR * pucData_, ULONG ulSize_, ULONG ulResponseTime_)
  1325. {
  1326. return(SetupBurstDataTransfer(
  1327. MESG_BURST_DATA_ID,
  1328. ucANTChannel_,
  1329. pucData_,
  1330. ulSize_,
  1331. MESG_DATA_SIZE,
  1332. ulResponseTime_));
  1333. }
  1334. ANTFRAMER_RETURN DSIFramerANT::SendAdvancedTransfer(UCHAR ucANTChannel_, UCHAR * pucData_, ULONG ulSize_, UCHAR ucStdPcktsPerSerialMsg_, ULONG ulResponseTime_)
  1335. {
  1336. return(SetupBurstDataTransfer(
  1337. MESG_ADV_BURST_DATA_ID,
  1338. ucANTChannel_,
  1339. pucData_,
  1340. ulSize_,
  1341. 1 + ((MESG_DATA_SIZE-1) * ucStdPcktsPerSerialMsg_), //seqNum + (payloadLength*pcktsPerSerial)
  1342. ulResponseTime_));
  1343. }
  1344. ///////////////////////////////////////////////////////////////////////
  1345. ANTFRAMER_RETURN DSIFramerANT::SendANTFSTransfer(UCHAR ucANTChannel_, UCHAR* pucHeader_, UCHAR* pucFooter_, UCHAR * pucData_, ULONG ulSize_, ULONG ulResponseTime_, volatile ULONG *pulProgress_)
  1346. {
  1347. ANTFRAMER_RETURN eReturn = ANTFRAMER_PASS;
  1348. ANT_MESSAGE stMessage;
  1349. ULONG ulStartTime = DSIThread_GetSystemTime();
  1350. UCHAR *pucDataSource;
  1351. ANTMessageResponse *pclPassResponse = (ANTMessageResponse*)NULL;
  1352. ANTMessageResponse *pclFailResponse = (ANTMessageResponse*)NULL;
  1353. ANTMessageResponse *pclErrorResponse = (ANTMessageResponse*)NULL;
  1354. #if defined(WAIT_TO_FEED_TRANSFER)
  1355. ANTMessageResponse *pclBroadcastResponse = (ANTMessageResponse*)NULL;
  1356. ANTMessageResponse *pclAcknowledgeResponse = (ANTMessageResponse*)NULL;
  1357. BOOL bFirstPacket = TRUE;
  1358. UCHAR ucSyncMesgCount = 0;
  1359. UCHAR ucChannelStatus;
  1360. #endif
  1361. ULONG ulDummyProgress = 0;
  1362. volatile BOOL *pbCancel_;
  1363. BOOL bDummyCancel = FALSE;
  1364. if (pbCancel == NULL)
  1365. pbCancel_ = &bDummyCancel;
  1366. else
  1367. pbCancel_ = pbCancel;
  1368. if (pulProgress_ == NULL)
  1369. pulProgress_ = &ulDummyProgress;
  1370. if (pucHeader_) //if the header is not NULL, set the data pointer and add 8 to the size
  1371. {
  1372. pucDataSource = pucHeader_;
  1373. ulSize_ += 8;
  1374. }
  1375. else
  1376. {
  1377. pucDataSource = pucData_;
  1378. }
  1379. #if defined(WAIT_TO_FEED_TRANSFER)
  1380. if(GetChannelStatus(ucANTChannel_,&ucChannelStatus, 2000) == FALSE)
  1381. return ANTFRAMER_FAIL;
  1382. if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) != STATUS_TRACKING_CHANNEL)
  1383. return ANTFRAMER_FAIL;
  1384. #endif
  1385. // If we are going to be waiting for a response setup the Response objects
  1386. if (ulResponseTime_ != 0)
  1387. {
  1388. UCHAR aucDesiredData[3];
  1389. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx complete
  1390. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1391. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_COMPLETED;
  1392. pclPassResponse = new ANTMessageResponse();
  1393. pclPassResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this);
  1394. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx fail
  1395. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1396. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_FAILED;
  1397. pclFailResponse = new ANTMessageResponse();
  1398. pclFailResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this, pclPassResponse->pstCondResponseReady);
  1399. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch any errors like transfer in progress.
  1400. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_BURST_DATA_ID;
  1401. pclErrorResponse = new ANTMessageResponse();
  1402. pclErrorResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, 2, this, pclPassResponse->pstCondResponseReady);
  1403. //getting error Rx will also effectively lose the transfer, but only on an AP1
  1404. #if defined(WAIT_TO_FEED_TRANSFER)
  1405. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch only broadcast/acknowledged messages for this channel
  1406. pclBroadcastResponse = new ANTMessageResponse();
  1407. pclBroadcastResponse->Attach(MESG_BROADCAST_DATA_ID, aucDesiredData, 1, this, pclPassResponse->pstCondResponseReady);
  1408. pclAcknowledgeResponse = new ANTMessageResponse();
  1409. pclAcknowledgeResponse->Attach(MESG_ACKNOWLEDGED_DATA_ID, aucDesiredData, 1, this, pclPassResponse->pstCondResponseReady);
  1410. #endif
  1411. }
  1412. stMessage.ucMessageID = MESG_BURST_DATA_ID;
  1413. stMessage.aucData[0] = ucANTChannel_ & CHANNEL_NUMBER_MASK; // Clear the sequence bits
  1414. while (eReturn == ANTFRAMER_PASS && ulSize_)
  1415. {
  1416. if (*pbCancel_ == TRUE)
  1417. eReturn = ANTFRAMER_CANCELLED;
  1418. if (ulSize_ > 8)
  1419. {
  1420. memcpy (&stMessage.aucData[1],pucDataSource,8);
  1421. *pulProgress_ += 8;
  1422. ulSize_ -= 8;
  1423. }
  1424. else
  1425. {
  1426. if (pucFooter_ == (UCHAR*)NULL)
  1427. stMessage.aucData[0] |= SEQUENCE_LAST_MESSAGE;
  1428. memset (&stMessage.aucData[1], 0x00, 8);
  1429. memcpy (&stMessage.aucData[1],pucDataSource,ulSize_);
  1430. *pulProgress_ += ulSize_;
  1431. ulSize_ = 0;
  1432. }
  1433. if (ulSize_)
  1434. {
  1435. if (pucDataSource == pucHeader_)
  1436. pucDataSource = pucData_;
  1437. else
  1438. pucDataSource += 8;
  1439. }
  1440. if (WriteMessage(&stMessage,9) == FALSE)
  1441. eReturn = ANTFRAMER_FAIL;
  1442. //Adjust sequence number
  1443. if ((stMessage.aucData[0] & SEQUENCE_NUMBER_MASK) == SEQUENCE_NUMBER_ROLLOVER)
  1444. stMessage.aucData[0] = SEQUENCE_NUMBER_INC | ucANTChannel_;
  1445. else
  1446. stMessage.aucData[0] += SEQUENCE_NUMBER_INC;
  1447. #if defined(WAIT_TO_FEED_TRANSFER)
  1448. if (bFirstPacket)
  1449. {
  1450. bFirstPacket = FALSE;
  1451. DSIThread_MutexLock(&stMutexResponseRequest);
  1452. if (eReturn == ANTFRAMER_PASS) //Only try to wait if we haven't failed yet
  1453. {
  1454. if ((pclBroadcastResponse->bResponseReady == FALSE) &&
  1455. (pclAcknowledgeResponse->bResponseReady == FALSE) &&
  1456. (pclPassResponse->bResponseReady == FALSE) &&
  1457. (pclFailResponse->bResponseReady == FALSE) &&
  1458. (pclErrorResponse->bResponseReady == FALSE) &&
  1459. (*pbCancel_ == FALSE) &&
  1460. ((DSIThread_GetSystemTime() - ulStartTime) < ulResponseTime_))
  1461. {
  1462. UCHAR ucStatus = DSIThread_CondTimedWait(pclBroadcastResponse->pstCondResponseReady, &stMutexResponseRequest, 3000); //Try to wait for the next syncronous event to send out the next packet.
  1463. if (ucStatus != DSI_THREAD_ENONE) //If we timeout
  1464. {
  1465. #if defined(DEBUG_FILE)
  1466. if(ucStatus == DSI_THREAD_EOTHER)
  1467. DSIDebug::ThreadWrite("Framer->SendFSTransfer(): CondTimedWait() Failed!");
  1468. DSIDebug::ThreadWrite("Framer->SendFSTransfer(): Wait for sync mesg failed.");
  1469. #endif
  1470. //if we didn't get a syncronous event,
  1471. stMessage.aucData[0] += SEQUENCE_NUMBER_INC; // mess up the sequence number on purpose, this will result in the transfer being cleared off of the device and will result in us exiting this function due to the sync error.
  1472. }
  1473. }
  1474. pclBroadcastResponse->bResponseReady = FALSE;
  1475. pclAcknowledgeResponse->bResponseReady = FALSE;
  1476. //reset the variables and continue on
  1477. }
  1478. DSIThread_MutexUnlock(&stMutexResponseRequest);
  1479. }
  1480. #endif
  1481. if (ulResponseTime_ != 0) //Check for errors
  1482. {
  1483. if ((pclFailResponse->bResponseReady == TRUE) || (pclErrorResponse->bResponseReady == TRUE))
  1484. eReturn = ANTFRAMER_FAIL;
  1485. if ((DSIThread_GetSystemTime() - ulStartTime) > ulResponseTime_)
  1486. eReturn = ANTFRAMER_TIMEOUT;
  1487. }
  1488. }
  1489. if ((eReturn == ANTFRAMER_PASS) && (pucFooter_))
  1490. {
  1491. stMessage.aucData[0] |= SEQUENCE_LAST_MESSAGE;
  1492. memcpy (&stMessage.aucData[1],pucFooter_,8);
  1493. *pulProgress_ += 8;
  1494. if (WriteMessage(&stMessage,9) == FALSE)
  1495. eReturn = ANTFRAMER_FAIL;
  1496. }
  1497. if (ulResponseTime_ != 0) //Check for errors
  1498. {
  1499. DSIThread_MutexLock(&stMutexResponseRequest);
  1500. if (eReturn == ANTFRAMER_PASS) //Only try to wait if we haven't failed yet
  1501. {
  1502. while((pclPassResponse->bResponseReady == FALSE) &&
  1503. (pclFailResponse->bResponseReady == FALSE) &&
  1504. (pclErrorResponse->bResponseReady == FALSE) &&
  1505. (*pbCancel_ == FALSE) &&
  1506. ((DSIThread_GetSystemTime() - ulStartTime) < ulResponseTime_))
  1507. {
  1508. DSIThread_CondTimedWait(pclPassResponse->pstCondResponseReady, &stMutexResponseRequest, 1000);
  1509. #if defined(WAIT_TO_FEED_TRANSFER)
  1510. if ((pclBroadcastResponse->bResponseReady == TRUE) ||
  1511. (pclAcknowledgeResponse->bResponseReady == TRUE))
  1512. {
  1513. pclBroadcastResponse->bResponseReady = FALSE;
  1514. pclAcknowledgeResponse->bResponseReady = FALSE;
  1515. if (ucSyncMesgCount++ > 2) //If we get more than 2 sync events (3 or 4) when we're waiting for our transfer to complete
  1516. {
  1517. pclErrorResponse->bResponseReady = TRUE; //Set the error response flag
  1518. }
  1519. }
  1520. #endif
  1521. }
  1522. if (pclPassResponse->bResponseReady == FALSE) //The only time we are sucessful is if we get a tx transfer complete
  1523. {
  1524. //figure out the reason why we failed/stopped
  1525. if ((pclErrorResponse->bResponseReady == TRUE) || (pclFailResponse->bResponseReady == TRUE))
  1526. eReturn = ANTFRAMER_FAIL;
  1527. else if (*pbCancel_ == TRUE)
  1528. eReturn = ANTFRAMER_CANCELLED;
  1529. else
  1530. eReturn = ANTFRAMER_TIMEOUT;
  1531. }
  1532. }
  1533. pclPassResponse->Remove();
  1534. pclFailResponse->Remove();
  1535. pclErrorResponse->Remove();
  1536. #if defined(WAIT_TO_FEED_TRANSFER)
  1537. pclBroadcastResponse->Remove();
  1538. pclAcknowledgeResponse->Remove();
  1539. #endif
  1540. DSIThread_MutexUnlock(&stMutexResponseRequest);
  1541. delete pclPassResponse;
  1542. delete pclFailResponse;
  1543. delete pclErrorResponse;
  1544. #if defined(WAIT_TO_FEED_TRANSFER)
  1545. delete pclBroadcastResponse;
  1546. delete pclAcknowledgeResponse;
  1547. #endif
  1548. }
  1549. return eReturn;
  1550. }
  1551. ///////////////////////////////////////////////////////////////////////
  1552. ANTFRAMER_RETURN DSIFramerANT::SendANTFSClientTransfer(UCHAR ucANTChannel_, ANTFS_DATA* pstHeader_, ANTFS_DATA* pstFooter_, ANTFS_DATA* pstData_, ULONG ulResponseTime_, volatile ULONG *pulProgress_)
  1553. {
  1554. ANTFRAMER_RETURN eReturn = ANTFRAMER_PASS;
  1555. ANT_MESSAGE stMessage;
  1556. ULONG ulStartTime = DSIThread_GetSystemTime();
  1557. UCHAR *pucDataSource;
  1558. ULONG ulBlockSize;
  1559. ULONG ulTotalSize;
  1560. ANTMessageResponse *pclPassResponse = (ANTMessageResponse*)NULL;
  1561. ANTMessageResponse *pclFailResponse = (ANTMessageResponse*)NULL;
  1562. ANTMessageResponse *pclErrorResponse = (ANTMessageResponse*)NULL;
  1563. ULONG ulDummyProgress = 0;
  1564. volatile BOOL *pbCancel_;
  1565. BOOL bDummyCancel = FALSE;
  1566. if (pbCancel == NULL)
  1567. pbCancel_ = &bDummyCancel;
  1568. else
  1569. pbCancel_ = pbCancel;
  1570. if (pulProgress_ == NULL)
  1571. pulProgress_ = &ulDummyProgress;
  1572. if (pstHeader_->pucData) //if there is a header, set the data pointer
  1573. {
  1574. pucDataSource = pstHeader_->pucData;
  1575. ulBlockSize = pstHeader_->ulSize;
  1576. }
  1577. else
  1578. {
  1579. pucDataSource = pstData_->pucData;
  1580. ulBlockSize = pstData_->ulSize;
  1581. }
  1582. ulTotalSize = pstHeader_->ulSize + pstData_->ulSize + pstFooter_->ulSize;
  1583. // If we are going to be waiting for a response setup the Response objects
  1584. if (ulResponseTime_ != 0)
  1585. {
  1586. UCHAR aucDesiredData[3];
  1587. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx complete
  1588. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1589. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_COMPLETED;
  1590. pclPassResponse = new ANTMessageResponse();
  1591. pclPassResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this);
  1592. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch tx fail
  1593. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_EVENT_ID;
  1594. aucDesiredData[ANT_DATA_EVENT_CODE_OFFSET] = EVENT_TRANSFER_TX_FAILED;
  1595. pclFailResponse = new ANTMessageResponse();
  1596. pclFailResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, sizeof(aucDesiredData), this, pclPassResponse->pstCondResponseReady);
  1597. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = ucANTChannel_; //Setup response to catch any errors like transfer in progress.
  1598. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = MESG_BURST_DATA_ID;
  1599. pclErrorResponse = new ANTMessageResponse();
  1600. pclErrorResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, 2, this, pclPassResponse->pstCondResponseReady);
  1601. }
  1602. stMessage.ucMessageID = MESG_BURST_DATA_ID;
  1603. stMessage.aucData[0] = ucANTChannel_ & CHANNEL_NUMBER_MASK; // Clear the sequence bits
  1604. while (eReturn == ANTFRAMER_PASS && ulTotalSize)
  1605. {
  1606. UCHAR ucCurrentBlockSize;
  1607. if (*pbCancel_ == TRUE)
  1608. eReturn = ANTFRAMER_CANCELLED;
  1609. if (ulBlockSize > 8)
  1610. {
  1611. ucCurrentBlockSize = 8;
  1612. memcpy (&stMessage.aucData[1],pucDataSource, ucCurrentBlockSize);
  1613. *pulProgress_ += ucCurrentBlockSize;
  1614. ulTotalSize -= ucCurrentBlockSize;
  1615. ulBlockSize -= ucCurrentBlockSize;
  1616. pucDataSource += ucCurrentBlockSize;
  1617. }
  1618. else
  1619. {
  1620. ucCurrentBlockSize = (UCHAR)ulBlockSize;
  1621. memset (&stMessage.aucData[1], 0x00, 8);
  1622. memcpy (&stMessage.aucData[1],pucDataSource, ucCurrentBlockSize);
  1623. *pulProgress_ += ucCurrentBlockSize;
  1624. ulTotalSize -= ucCurrentBlockSize;
  1625. ulBlockSize = 0;
  1626. }
  1627. if (ulBlockSize == 0)
  1628. {
  1629. if(pucDataSource == (pstHeader_->pucData + pstHeader_->ulSize - ucCurrentBlockSize))
  1630. {
  1631. if(pstData_->pucData)
  1632. {
  1633. pucDataSource = pstData_->pucData;
  1634. ulBlockSize = pstData_->ulSize;
  1635. }
  1636. else if(pstFooter_->pucData)
  1637. {
  1638. pucDataSource = pstFooter_->pucData;
  1639. ulBlockSize = pstFooter_->ulSize;
  1640. }
  1641. else
  1642. {
  1643. stMessage.aucData[0] |= SEQUENCE_LAST_MESSAGE;
  1644. }
  1645. }
  1646. else if(pucDataSource == (pstData_->pucData + pstData_->ulSize - ucCurrentBlockSize))
  1647. {
  1648. if(pstFooter_->pucData)
  1649. {
  1650. pucDataSource = pstFooter_->pucData;
  1651. ulBlockSize = pstFooter_->ulSize;
  1652. }
  1653. else
  1654. {
  1655. stMessage.aucData[0] |= SEQUENCE_LAST_MESSAGE;
  1656. }
  1657. }
  1658. else // footer
  1659. {
  1660. stMessage.aucData[0] |= SEQUENCE_LAST_MESSAGE;
  1661. }
  1662. }
  1663. if (WriteMessage(&stMessage,9) == FALSE)
  1664. eReturn = ANTFRAMER_FAIL;
  1665. //Adjust sequence number
  1666. if ((stMessage.aucData[0] & SEQUENCE_NUMBER_MASK) == SEQUENCE_NUMBER_ROLLOVER)
  1667. stMessage.aucData[0] = SEQUENCE_NUMBER_INC | ucANTChannel_;
  1668. else
  1669. stMessage.aucData[0] += SEQUENCE_NUMBER_INC;
  1670. if (ulResponseTime_ != 0) //Check for errors
  1671. {
  1672. if ((pclFailResponse->bResponseReady == TRUE) || (pclErrorResponse->bResponseReady == TRUE))
  1673. eReturn = ANTFRAMER_FAIL;
  1674. if ((DSIThread_GetSystemTime() - ulStartTime) > ulResponseTime_)
  1675. eReturn = ANTFRAMER_TIMEOUT;
  1676. }
  1677. } // while loop
  1678. if (ulResponseTime_ != 0) //Check for errors
  1679. {
  1680. DSIThread_MutexLock(&stMutexResponseRequest);
  1681. if (eReturn == ANTFRAMER_PASS) //Only try to wait if we haven't failed yet
  1682. {
  1683. while((pclPassResponse->bResponseReady == FALSE) &&
  1684. (pclFailResponse->bResponseReady == FALSE) &&
  1685. (pclErrorResponse->bResponseReady == FALSE) &&
  1686. (*pbCancel_ == FALSE) &&
  1687. ((DSIThread_GetSystemTime() - ulStartTime) < ulResponseTime_))
  1688. {
  1689. DSIThread_CondTimedWait(pclPassResponse->pstCondResponseReady, &stMutexResponseRequest, 1000);
  1690. }
  1691. if (pclPassResponse->bResponseReady == FALSE) //The only time we are sucessful is if we get a tx transfer complete
  1692. {
  1693. //figure out the reason why we failed/stopped
  1694. if ((pclErrorResponse->bResponseReady == TRUE) || (pclFailResponse->bResponseReady == TRUE))
  1695. eReturn = ANTFRAMER_FAIL;
  1696. else if (*pbCancel_ == TRUE)
  1697. eReturn = ANTFRAMER_CANCELLED;
  1698. else
  1699. eReturn = ANTFRAMER_TIMEOUT;
  1700. }
  1701. }
  1702. pclPassResponse->Remove();
  1703. pclFailResponse->Remove();
  1704. pclErrorResponse->Remove();
  1705. DSIThread_MutexUnlock(&stMutexResponseRequest);
  1706. delete pclPassResponse;
  1707. delete pclFailResponse;
  1708. delete pclErrorResponse;
  1709. }
  1710. return eReturn;
  1711. }
  1712. //////////////////////////////////////////////////////////////////////////////////
  1713. // Private Class Functions
  1714. //////////////////////////////////////////////////////////////////////////////////
  1715. ///////////////////////////////////////////////////////////////////////
  1716. // stMutexCriticalSection must be locked before calling this function.
  1717. ///////////////////////////////////////////////////////////////////////
  1718. USHORT DSIFramerANT::GetMessageSize(void)
  1719. {
  1720. USHORT usRetVal;
  1721. if (ucError)
  1722. usRetVal = DSI_FRAMER_ERROR;
  1723. else if ((usMessageHead - usMessageTail) != 0)
  1724. usRetVal = astMessageBuffer[usMessageTail].ucSize;
  1725. else
  1726. usRetVal = DSI_FRAMER_TIMEDOUT;
  1727. return usRetVal;
  1728. }
  1729. ///////////////////////////////////////////////////////////////////////
  1730. void DSIFramerANT::ProcessMessage(void)
  1731. {
  1732. UCHAR ucMessageID = aucRxFifo[MESG_ID_OFFSET];
  1733. UCHAR ucSize = aucRxFifo[MESG_SIZE_OFFSET]; // Set size as reported by message.
  1734. CheckResponseList();
  1735. if(ucMessageID == MESG_BURST_DATA_ID || ucMessageID == MESG_EXT_BURST_DATA_ID)
  1736. {
  1737. ucPrevSequenceNum = aucRxFifo[MESG_DATA_OFFSET] & SEQUENCE_NUMBER_MASK;
  1738. //If the burst flag only holds the SEQUENCE_LAST_MESSAGE and no sequence number
  1739. //we recode it as an acknowledged message. This is because there is a timing bug in some
  1740. //versions of the chips that had the first implementation of advanced burst support,
  1741. //such as the USBm, that cause a received ACK to be sent in this wrong format.
  1742. //Fixing it here means nobody else has to work around this bug, and this is essentially
  1743. //the same logic that is used in the ANT stack anyway.
  1744. if(ucPrevSequenceNum == SEQUENCE_LAST_MESSAGE)
  1745. {
  1746. #if defined(SERIAL_DEBUG)
  1747. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "Rx - Recevied ack msg as burst packet, recoding to ack.", (UCHAR*)NULL, 0);
  1748. #endif
  1749. aucRxFifo[MESG_ID_OFFSET] = MESG_ACKNOWLEDGED_DATA_ID;
  1750. aucRxFifo[MESG_DATA_OFFSET] &= ~SEQUENCE_NUMBER_MASK; //Also remove the burst flags
  1751. ProcessMessage(); //Recurse to handle the recoded message
  1752. return; //Now we are done
  1753. }
  1754. }
  1755. if(ucMessageID == MESG_ADV_BURST_DATA_ID && bSplitAdvancedBursts) // split into normal burst messages.
  1756. {
  1757. #if defined(SERIAL_DEBUG)
  1758. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "Decomposing", aucRxFifo, ucSize+4);
  1759. #endif
  1760. for(UCHAR i = 0; i * 8 < ucSize - 1; i++) //For each 8-byte packet
  1761. {
  1762. if(ucPrevSequenceNum == SEQUENCE_NUMBER_ROLLOVER) //Increment sequence num
  1763. ucPrevSequenceNum = 0;
  1764. ucPrevSequenceNum += SEQUENCE_NUMBER_INC;
  1765. if((aucRxFifo[MESG_DATA_OFFSET] & SEQUENCE_LAST_MESSAGE) != 0 && (i+1)*8 == ucSize - 1) //If the last packet.
  1766. ucPrevSequenceNum |= SEQUENCE_LAST_MESSAGE;
  1767. // Add message to the queue.
  1768. if ((USHORT)(usMessageHead - usMessageTail) < (USHORT)(sizeof(astMessageBuffer) / sizeof(ANT_MESSAGE_ITEM) - 1))
  1769. {
  1770. astMessageBuffer[usMessageHead].ucSize = 9;
  1771. astMessageBuffer[usMessageHead].stANTMessage.ucMessageID = MESG_BURST_DATA_ID;
  1772. astMessageBuffer[usMessageHead].stANTMessage.aucData[0] = ucPrevSequenceNum | (aucRxFifo[MESG_DATA_OFFSET] & CHANNEL_NUMBER_MASK);
  1773. memcpy(astMessageBuffer[usMessageHead].stANTMessage.aucData + 1, &aucRxFifo[MESG_DATA_OFFSET + 1 + i*8], 8);
  1774. usMessageHead++; // Rollover of usMessageHead happens automagically because our buffer size is MAX_USHORT + 1.
  1775. }
  1776. else
  1777. {
  1778. ucError = DSI_FRAMER_ANT_EQUEUE_OVERFLOW;
  1779. }
  1780. DSIThread_CondSignal(&stCondMessageReady);
  1781. #if defined(SERIAL_DEBUG)
  1782. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "Simulated Rx", astMessageBuffer[usMessageHead-1].stANTMessage.aucData, astMessageBuffer[usMessageHead-1].ucSize);
  1783. #endif
  1784. }
  1785. }
  1786. else
  1787. {
  1788. // Add message to the queue.
  1789. if ((USHORT)(usMessageHead - usMessageTail) < (USHORT)(sizeof(astMessageBuffer) / sizeof(ANT_MESSAGE_ITEM) - 1))
  1790. {
  1791. astMessageBuffer[usMessageHead].ucSize = ucSize;
  1792. astMessageBuffer[usMessageHead].stANTMessage.ucMessageID = ucMessageID;
  1793. memcpy(astMessageBuffer[usMessageHead].stANTMessage.aucData, &aucRxFifo[MESG_DATA_OFFSET], ucSize);
  1794. usMessageHead++; // Rollover of usMessageHead happens automagically because our buffer size is MAX_USHORT + 1.
  1795. }
  1796. else
  1797. {
  1798. ucError = DSI_FRAMER_ANT_EQUEUE_OVERFLOW;
  1799. }
  1800. DSIThread_CondSignal(&stCondMessageReady);
  1801. #if defined(SERIAL_DEBUG)
  1802. DSIDebug::SerialWrite(pclSerial->GetDeviceNumber(), "Rx", aucRxFifo, ucSize + 4);
  1803. #endif
  1804. }
  1805. }
  1806. ///////////////////////////////////////////////////////////////////////
  1807. void DSIFramerANT::CheckResponseList(void)
  1808. {
  1809. ANTMessageResponse *pclResponseList;
  1810. BOOL bMatch;
  1811. DSIThread_MutexLock(&stMutexResponseRequest);
  1812. pclResponseList = pclResponseListStart;
  1813. while (pclResponseList != NULL)
  1814. {
  1815. bMatch = TRUE;
  1816. if (pclResponseList->bResponseReady == FALSE && pclResponseList->stMessageItem.stANTMessage.ucMessageID == aucRxFifo[MESG_ID_OFFSET])
  1817. {
  1818. for(int i=0; i < pclResponseList->ucBytesToMatch; i++)
  1819. {
  1820. if (pclResponseList->stMessageItem.stANTMessage.aucData[i] != aucRxFifo[MESG_DATA_OFFSET + i])
  1821. {
  1822. bMatch = FALSE; // Data byte did not match
  1823. }
  1824. }
  1825. }
  1826. else
  1827. {
  1828. bMatch = FALSE; // Mesg ID did not match
  1829. }
  1830. if (bMatch)
  1831. {
  1832. int i = pclResponseList->ucBytesToMatch;
  1833. pclResponseList->stMessageItem.ucSize = aucRxFifo[MESG_SIZE_OFFSET];
  1834. memcpy(&(pclResponseList->stMessageItem.stANTMessage.aucData[i]), &(aucRxFifo[MESG_DATA_OFFSET + i]), MESG_MAX_SIZE_VALUE - i); // Copy the rest of the message
  1835. pclResponseList->bResponseReady = TRUE;
  1836. DSIThread_CondSignal(pclResponseList->pstCondResponseReady);
  1837. }
  1838. pclResponseList = pclResponseList->pclNext;
  1839. }
  1840. DSIThread_MutexUnlock(&stMutexResponseRequest);
  1841. }
  1842. ///////////////////////////////////////////////////////////////////////
  1843. BOOL DSIFramerANT::SendCommand(ANT_MESSAGE *pstANTMessage_, USHORT usMessageSize_, ULONG ulResponseTime_)
  1844. {
  1845. ANTMessageResponse *pclCommandResponse = (ANTMessageResponse*)NULL;
  1846. // If we are going to be waiting for a response setup the Response object
  1847. if (ulResponseTime_ != 0)
  1848. {
  1849. UCHAR aucDesiredData[2];
  1850. UCHAR bytesToMatch = 2;
  1851. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = pstANTMessage_->aucData[ANT_DATA_CHANNEL_NUM_OFFSET];
  1852. aucDesiredData[ANT_DATA_EVENT_ID_OFFSET] = pstANTMessage_->ucMessageID;
  1853. //Script dump success can be determined by looking for the script cmd 0x04 dump complete code
  1854. if(pstANTMessage_->ucMessageID == MESG_SCRIPT_CMD_ID && pstANTMessage_->aucData[ANT_DATA_EVENT_ID_OFFSET] == SCRIPT_CMD_DUMP)
  1855. {
  1856. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] = SCRIPT_CMD_END_DUMP;
  1857. bytesToMatch = 1; //The second byte is the number of commands returned, which we can't guess so only match the first byte
  1858. }
  1859. else if(pstANTMessage_->ucMessageID == MESG_SCRIPT_DATA_ID)
  1860. {
  1861. //The first byte of script write is the id of the message being written, not the channel, and it is not overwritten but it is returned with the burst mask, so we need to ensure that is what we are looking for
  1862. aucDesiredData[ANT_DATA_CHANNEL_NUM_OFFSET] &= 0x1F;
  1863. }
  1864. pclCommandResponse = new ANTMessageResponse();
  1865. pclCommandResponse->Attach(MESG_RESPONSE_EVENT_ID, aucDesiredData, bytesToMatch, this);
  1866. }
  1867. // Write the command message.
  1868. if (!WriteMessage(pstANTMessage_, usMessageSize_))
  1869. {
  1870. #if defined(DEBUG_FILE)
  1871. DSIDebug::ThreadWrite("Framer->SendCommand(): WriteMessage Failed.");
  1872. #endif
  1873. if(pclCommandResponse != NULL)
  1874. {
  1875. delete pclCommandResponse;
  1876. pclCommandResponse = (ANTMessageResponse*)NULL;
  1877. }
  1878. return FALSE;
  1879. }
  1880. // Return immediately if we aren't waiting for the response.
  1881. if (ulResponseTime_ == 0)
  1882. return TRUE;
  1883. // Wait for the response.
  1884. pclCommandResponse->WaitForResponse(ulResponseTime_);
  1885. pclCommandResponse->Remove(); //detach from list
  1886. // We haven't received a response in the allotted time.
  1887. //if (pclCommandResponse->stMessageItem.ucSize == 0)
  1888. if (pclCommandResponse->bResponseReady == FALSE)
  1889. {
  1890. delete pclCommandResponse;
  1891. #if defined(DEBUG_FILE)
  1892. DSIDebug::ThreadWrite("Framer->SendCommand(): Timeout.");
  1893. #endif
  1894. return FALSE;
  1895. }
  1896. // Check the response.
  1897. if (pclCommandResponse->stMessageItem.stANTMessage.aucData[ANT_DATA_EVENT_CODE_OFFSET] != RESPONSE_NO_ERROR)
  1898. {
  1899. delete pclCommandResponse;
  1900. #if defined(DEBUG_FILE)
  1901. DSIDebug::ThreadWrite("Framer->SendCommand(): Response != RESPONSE_NO_ERROR.");
  1902. #endif
  1903. return FALSE;
  1904. }
  1905. delete pclCommandResponse;
  1906. return TRUE;
  1907. }
  1908. ///////////////////////////////////////////////////////////////////////
  1909. BOOL DSIFramerANT::SendRequest(UCHAR ucRequestedMesgID_, UCHAR ucANTChannel_ , ANT_MESSAGE_ITEM *pstANTResponse_, ULONG ulResponseTime_)
  1910. {
  1911. ANT_MESSAGE stMessage;
  1912. ANTMessageResponse *pclRequestResponse = (ANTMessageResponse*)NULL;
  1913. // Build the request message
  1914. stMessage.ucMessageID = MESG_REQUEST_ID;
  1915. stMessage.aucData[0] = ucANTChannel_;
  1916. stMessage.aucData[1] = ucRequestedMesgID_;
  1917. // If we are going to be waiting for a response setup the Response object
  1918. if ((ulResponseTime_ != 0) && (pstANTResponse_ != NULL))
  1919. {
  1920. pclRequestResponse = new ANTMessageResponse();
  1921. pclRequestResponse->Attach(ucRequestedMesgID_, (UCHAR*)NULL, 0, this);
  1922. }
  1923. // Write the command message.
  1924. if (!WriteMessage(&stMessage, MESG_REQUEST_SIZE))
  1925. {
  1926. if(pclRequestResponse != NULL)
  1927. {
  1928. delete pclRequestResponse;
  1929. pclRequestResponse = (ANTMessageResponse*)NULL;
  1930. }
  1931. return FALSE;
  1932. }
  1933. // Return immediately if we aren't waiting for the response.
  1934. if ((ulResponseTime_ == 0) || (pstANTResponse_ == NULL))
  1935. return TRUE;
  1936. // Wait for the response.
  1937. pclRequestResponse->WaitForResponse(ulResponseTime_);
  1938. pclRequestResponse->Remove();
  1939. // We haven't received a response in the allotted time.
  1940. if (pclRequestResponse->bResponseReady == FALSE)
  1941. {
  1942. delete pclRequestResponse;
  1943. return FALSE;
  1944. }
  1945. // Copy out the response
  1946. pstANTResponse_->ucSize = pclRequestResponse->stMessageItem.ucSize;
  1947. pstANTResponse_->stANTMessage.ucMessageID = pclRequestResponse->stMessageItem.stANTMessage.ucMessageID;
  1948. memcpy (pstANTResponse_->stANTMessage.aucData, pclRequestResponse->stMessageItem.stANTMessage.aucData, pclRequestResponse->stMessageItem.ucSize);
  1949. delete pclRequestResponse;
  1950. return TRUE;
  1951. }
  1952. ///////////////////////////////////////////////////////////////////////
  1953. BOOL DSIFramerANT::SendUserNvmRequest(UCHAR ucRequestedMesgID_, UCHAR ucANTChannel_ , ANT_MESSAGE_ITEM *pstANTResponse_, USHORT usAddress_, UCHAR ucSize_, ULONG ulResponseTime_)
  1954. {
  1955. ANT_MESSAGE stMessage;
  1956. ANTMessageResponse *pclRequestResponse = (ANTMessageResponse*)NULL;
  1957. // Build the request message
  1958. stMessage.ucMessageID = MESG_REQUEST_ID;
  1959. stMessage.aucData[0] = ucANTChannel_;
  1960. stMessage.aucData[1] = ucRequestedMesgID_;
  1961. stMessage.aucData[2] = (UCHAR)(usAddress_ & 0xFF);
  1962. stMessage.aucData[3] = (UCHAR)((usAddress_ >>8) & 0xFF);
  1963. stMessage.aucData[4] = ucSize_;
  1964. // If we are going to be waiting for a response setup the Response object
  1965. if ((ulResponseTime_ != 0) && (pstANTResponse_ != NULL))
  1966. {
  1967. pclRequestResponse = new ANTMessageResponse();
  1968. pclRequestResponse->Attach(ucRequestedMesgID_, (UCHAR*)NULL, 0, this);
  1969. }
  1970. // Write the command message.
  1971. if (!WriteMessage(&stMessage, MESG_REQUEST_USER_NVM_SIZE))
  1972. {
  1973. if(pclRequestResponse != NULL)
  1974. {
  1975. delete pclRequestResponse;
  1976. pclRequestResponse = (ANTMessageResponse*)NULL;
  1977. }
  1978. return FALSE;
  1979. }
  1980. // Return immediately if we aren't waiting for the response.
  1981. if ((ulResponseTime_ == 0) || (pstANTResponse_ == NULL))
  1982. return TRUE;
  1983. // Wait for the response.
  1984. pclRequestResponse->WaitForResponse(ulResponseTime_);
  1985. pclRequestResponse->Remove();
  1986. // We haven't received a response in the allotted time.
  1987. if (pclRequestResponse->bResponseReady == FALSE)
  1988. {
  1989. delete pclRequestResponse;
  1990. return FALSE;
  1991. }
  1992. // Copy out the response
  1993. pstANTResponse_->ucSize = pclRequestResponse->stMessageItem.ucSize;
  1994. pstANTResponse_->stANTMessage.ucMessageID = pclRequestResponse->stMessageItem.stANTMessage.ucMessageID;
  1995. memcpy (pstANTResponse_->stANTMessage.aucData, pclRequestResponse->stMessageItem.stANTMessage.aucData, pclRequestResponse->stMessageItem.ucSize);
  1996. delete pclRequestResponse;
  1997. return TRUE;
  1998. }
  1999. ///////////////////////////////////////////////////////////////////////
  2000. BOOL DSIFramerANT::SetUSBDescriptorString(UCHAR ucStringNum_, UCHAR *pucDescString_, UCHAR ucStringSize_, ULONG ulResponseTime_)
  2001. {
  2002. ANT_MESSAGE stMessage;
  2003. stMessage.ucMessageID = MESG_SET_USB_INFO_ID;
  2004. stMessage.aucData[0] = ucStringNum_;
  2005. UCHAR ucMesgSize = ucStringSize_ + 2; // Message size depends on string length
  2006. if(ucMesgSize > MESG_MAX_SIZE_VALUE)
  2007. ucMesgSize = MESG_MAX_SIZE_VALUE; // Check size does not exceed buffer space
  2008. // Copy the descriptor string
  2009. memcpy(&stMessage.aucData[1], pucDescString_, ucMesgSize - 2);
  2010. // Check that string is NULL terminated
  2011. if(ucStringNum_ != USB_DESCRIPTOR_VID_PID) // except for string VID/PID
  2012. {
  2013. if(stMessage.aucData[ucMesgSize-2] != 0)
  2014. {
  2015. if(ucMesgSize < MESG_MAX_SIZE_VALUE)
  2016. {
  2017. ucMesgSize++; // append NULL character at the end and adjust message size, or truncate string if too big
  2018. }
  2019. stMessage.aucData[ucMesgSize-2] = 0;
  2020. }
  2021. }
  2022. stMessage.aucData[ucMesgSize - 1] = ucMesgSize - 2;
  2023. return SendCommand(&stMessage, ucMesgSize, ulResponseTime_);
  2024. }
  2025. ///////////////////////////////////////////////////////////////////////
  2026. BOOL DSIFramerANT::GetDeviceUSBInfo(UCHAR ucDeviceNum_, UCHAR* pucProductString_, UCHAR* pucSerialString_, USHORT usBufferSize_)
  2027. {
  2028. return(pclSerial->GetDeviceUSBInfo(ucDeviceNum_, pucProductString_, pucSerialString_, usBufferSize_));
  2029. }
  2030. ///////////////////////////////////////////////////////////////////////
  2031. BOOL DSIFramerANT::GetDeviceUSBPID(USHORT& usPid_)
  2032. {
  2033. return(pclSerial->GetDevicePID(usPid_));
  2034. }
  2035. ///////////////////////////////////////////////////////////////////////
  2036. BOOL DSIFramerANT::GetDeviceUSBVID(USHORT& usVid_)
  2037. {
  2038. return(pclSerial->GetDeviceVID(usVid_));
  2039. }
  2040. ///Allocates a new standard ANT_MESSAGE struct which must be deleted after use.
  2041. BOOL DSIFramerANT::CreateAntMsg_wOptExtBuf(ANT_MESSAGE **ppstExtBufAntMsg_, ULONG ulReqMinDataSize_)
  2042. {
  2043. if(ulReqMinDataSize_ > MESG_MAX_SIZE_VALUE)
  2044. {
  2045. *ppstExtBufAntMsg_ = (ANT_MESSAGE*) NULL;
  2046. return FALSE;
  2047. }
  2048. else
  2049. {
  2050. *ppstExtBufAntMsg_ = (ANT_MESSAGE*)new UCHAR[sizeof(ANT_MESSAGE)];
  2051. return TRUE;
  2052. }
  2053. }
  2054. ///////////////////////////////////////////////////////////////////////
  2055. ///////////////////////////////////////////////////////////////////////
  2056. ///////////////////////////////////////////////////////////////////////
  2057. ANTMessageResponse::ANTMessageResponse()
  2058. {
  2059. pstCondResponseReady = &stCondResponseReady;
  2060. bResponseReady = FALSE;
  2061. pclNext = (ANTMessageResponse*)NULL;
  2062. pclFramer = (DSIFramerANT*)NULL;
  2063. if (DSIThread_CondInit(pstCondResponseReady) != DSI_THREAD_ENONE) //Init the wait object
  2064. return; //need to think of a different way to handle the failure
  2065. }
  2066. ///////////////////////////////////////////////////////////////////////
  2067. ANTMessageResponse::~ANTMessageResponse()
  2068. {
  2069. Remove();
  2070. DSIThread_CondDestroy(&stCondResponseReady);
  2071. }
  2072. ///////////////////////////////////////////////////////////////////////
  2073. BOOL ANTMessageResponse::Attach(UCHAR ucMessageID_, UCHAR *pucData_, UCHAR ucBytesToMatch_, DSIFramerANT * pclFramer_, DSI_CONDITION_VAR *pstCondResponseReady_)
  2074. {
  2075. ANTMessageResponse *pclResponse;
  2076. bResponseReady = FALSE; //Init ResponseReady
  2077. stMessageItem.stANTMessage.ucMessageID = ucMessageID_; //Set mesg ID to look for
  2078. ucBytesToMatch = ucBytesToMatch_; //Set number of data bytes to match
  2079. memcpy( &(stMessageItem.stANTMessage.aucData[0]), pucData_, ucBytesToMatch_); //Set data bytes to match
  2080. if (pstCondResponseReady_ != NULL)
  2081. pstCondResponseReady = pstCondResponseReady_;
  2082. else
  2083. pstCondResponseReady = &stCondResponseReady;
  2084. pclFramer = pclFramer_;
  2085. if (pclFramer == NULL)
  2086. return FALSE;
  2087. DSIThread_MutexLock(&(pclFramer->stMutexResponseRequest)); // Lock the mutex and begin list manipulation
  2088. pclResponse = pclFramer->pclResponseListStart;
  2089. if (pclFramer->pclResponseListStart == NULL) // Check if the list is empty
  2090. {
  2091. pclFramer->pclResponseListStart = this; // Add this reponse object to the list
  2092. }
  2093. else
  2094. {
  2095. pclResponse = pclFramer->pclResponseListStart; // If the list is not empty, walk through the list
  2096. while (pclResponse->pclNext != NULL)
  2097. {
  2098. pclResponse = pclResponse->pclNext;
  2099. }
  2100. pclResponse->pclNext = this; // Add ourself to the end of the list
  2101. }
  2102. DSIThread_MutexUnlock(&(pclFramer->stMutexResponseRequest)); // Unlock mutex when we're done
  2103. return TRUE;
  2104. }
  2105. ///////////////////////////////////////////////////////////////////////
  2106. void ANTMessageResponse::Remove()
  2107. {
  2108. ANTMessageResponse **ppclResponse;
  2109. if (pclFramer == NULL)
  2110. return;
  2111. DSIThread_MutexLock(&(pclFramer->stMutexResponseRequest)); // Lock the mutex and begin list manipulation
  2112. ppclResponse = &(pclFramer->pclResponseListStart); // Set the ppointer to point to pclResponseListStart
  2113. while (*ppclResponse != NULL) // While the pclNext is not NULL
  2114. {
  2115. if (*ppclResponse == this) // Check if pclNext is pointing to us
  2116. {
  2117. *ppclResponse = pclNext; // Remove this object from the List by changing the pointer to point to the element behind us
  2118. if (pclNext == NULL) // If we are at the end of the list, then break because we are done.
  2119. break;
  2120. }
  2121. else
  2122. {
  2123. ppclResponse = &((*ppclResponse)->pclNext); // Advance the ppointer to point to the pclNext element of the next object in the list
  2124. }
  2125. }
  2126. DSIThread_MutexUnlock(&(pclFramer->stMutexResponseRequest)); // Unlock mutex when we're done
  2127. }
  2128. ///////////////////////////////////////////////////////////////////////
  2129. BOOL ANTMessageResponse::WaitForResponse(ULONG ulMilliseconds_)
  2130. {
  2131. DSIThread_MutexLock(&(pclFramer->stMutexResponseRequest));
  2132. if ((bResponseReady == FALSE) && (ulMilliseconds_ != 0))
  2133. {
  2134. DSIThread_CondTimedWait(pstCondResponseReady, &(pclFramer->stMutexResponseRequest), ulMilliseconds_);
  2135. }
  2136. DSIThread_MutexUnlock(&(pclFramer->stMutexResponseRequest));
  2137. return bResponseReady;
  2138. }