antfs_client_channel.cpp 146 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907
  1. /*
  2. This software is subject to the license described in the License.txt file
  3. included with this software distribution. You may not use this file except in compliance
  4. with this license.
  5. Copyright (c) Dynastream Innovations Inc. 2013
  6. All rights reserved.
  7. */
  8. #include "types.h"
  9. #include "dsi_framer_ant.hpp"
  10. #include "dsi_serial_generic.hpp"
  11. #include "dsi_thread.h"
  12. #include "dsi_timer.hpp"
  13. #include "dsi_convert.h"
  14. #include "crc.h"
  15. #include "antfs_client_channel.hpp"
  16. #include "antfsmessage.h"
  17. #include "config.h"
  18. #include "dsi_debug.hpp"
  19. #if defined(DEBUG_FILE)
  20. #include "macros.h"
  21. #endif
  22. #include <stdio.h>
  23. #include <string.h>
  24. #include <stdlib.h>
  25. #include <time.h>
  26. //////////////////////////////////////////////////////////////////////////////////
  27. // Private Definitions
  28. //////////////////////////////////////////////////////////////////////////////////
  29. static const UCHAR caucNetworkKey[] = NETWORK_KEY;
  30. //////////////////////////////////////////////////////////////////////////////////
  31. // Public Functions
  32. //////////////////////////////////////////////////////////////////////////////////
  33. ANTFSClientChannel::ANTFSClientChannel()
  34. {
  35. bInitFailed = FALSE;
  36. bTimerRunning = FALSE;
  37. bKillThread = FALSE;
  38. bANTFSThreadRunning = FALSE;
  39. pclANT = (DSIFramerANT*) NULL;
  40. pbCancel = &bCancel;
  41. *pbCancel = FALSE;
  42. // Default channel configuration
  43. ucChannelNumber = ANTFS_CHANNEL;
  44. ucNetworkNumber = ANTFS_NETWORK;
  45. usRadioChannelID = ANTFS_CLIENT_NUMBER;
  46. ucTheDeviceType = ANTFS_DEVICE_TYPE;
  47. ucTheTransmissionType = ANTFS_TRANSMISSION_TYPE;
  48. usTheMessagePeriod = ANTFS_MESSAGE_PERIOD;
  49. usBeaconChannelPeriod = ANTFS_MESSAGE_PERIOD;
  50. memcpy(aucTheNetworkkey,caucNetworkKey,8);
  51. ucLinkTxPower = RADIO_TX_POWER_LVL_3;
  52. ucSessionTxPower = RADIO_TX_POWER_LVL_3;
  53. bCustomTxPower = FALSE;
  54. // Default beacon configuration
  55. SetDefaultBeacon();
  56. memset(aucFriendlyName, 0, sizeof(aucFriendlyName));
  57. memset(aucPassKey, 0, sizeof(aucPassKey));
  58. ucPassKeySize = 0;
  59. ucFriendlyNameSize = 0;
  60. memset(&stHostDisconnectParams, 0, sizeof(stHostDisconnectParams));
  61. eANTFSState = ANTFS_CLIENT_STATE_OFF;
  62. ResetClientState();
  63. // Debugging is initialized by DSIANTDevice (or ANTDevice in Managed Lib)
  64. // Timer
  65. pclTimer = (DSITimer*)NULL;
  66. // Threading
  67. hANTFSThread = (DSI_THREAD_ID)NULL; // Handle for the ANTFS thread
  68. if (DSIThread_MutexInit(&stMutexCriticalSection) != DSI_THREAD_ENONE)
  69. {
  70. bInitFailed = TRUE;
  71. }
  72. if (DSIThread_MutexInit(&stMutexResponseQueue) != DSI_THREAD_ENONE)
  73. {
  74. bInitFailed = TRUE;
  75. }
  76. if (DSIThread_CondInit(&stCondANTFSThreadExit) != DSI_THREAD_ENONE)
  77. {
  78. bInitFailed = TRUE;
  79. }
  80. if (DSIThread_CondInit(&stCondRequest) != DSI_THREAD_ENONE)
  81. {
  82. bInitFailed = TRUE;
  83. }
  84. if (DSIThread_CondInit(&stCondRxEvent) != DSI_THREAD_ENONE)
  85. {
  86. bInitFailed = TRUE;
  87. }
  88. if (DSIThread_CondInit(&stCondWaitForResponse) != DSI_THREAD_ENONE)
  89. {
  90. bInitFailed = TRUE;
  91. }
  92. }
  93. ///////////////////////////////////////////////////////////////////////
  94. ANTFSClientChannel::~ANTFSClientChannel()
  95. {
  96. this->Close();
  97. if (bInitFailed == FALSE)
  98. {
  99. DSIThread_MutexDestroy(&stMutexCriticalSection);
  100. DSIThread_MutexDestroy(&stMutexResponseQueue);
  101. DSIThread_CondDestroy(&stCondANTFSThreadExit);
  102. DSIThread_CondDestroy(&stCondRequest);
  103. DSIThread_CondDestroy(&stCondRxEvent);
  104. DSIThread_CondDestroy(&stCondWaitForResponse);
  105. }
  106. }
  107. ///////////////////////////////////////////////////////////////////////
  108. BOOL ANTFSClientChannel::Init(DSIFramerANT* pclANT_, UCHAR ucChannel_)
  109. {
  110. if (bInitFailed == TRUE)
  111. {
  112. #if defined(DEBUG_FILE)
  113. DSIDebug::ThreadWrite("ANTFSClientChannel::Init(): bInitFailed == TRUE");
  114. #endif
  115. return FALSE;
  116. }
  117. ucChannelNumber = ucChannel_;
  118. pclANT = pclANT_;
  119. if(pclANT)
  120. {
  121. if(pclANT->GetCancelParameter() == (BOOL*) NULL) // If cancel parameter has not been configured in framer, use internal
  122. pclANT->SetCancelParameter(pbCancel);
  123. else // Use cancel parameter configured in framer
  124. pbCancel = pclANT->GetCancelParameter();
  125. }
  126. return ReInitDevice();
  127. }
  128. ///////////////////////////////////////////////////////////////////////
  129. void ANTFSClientChannel::Close(void)
  130. {
  131. #if defined(DEBUG_FILE)
  132. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Closing ANTFS...");
  133. #endif
  134. DSIThread_MutexLock(&stMutexCriticalSection);
  135. // Stop the threads.
  136. bKillThread = TRUE;
  137. *pbCancel = TRUE;
  138. #if defined(DEBUG_FILE)
  139. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): SetEvent(stCondWaitForResponse).");
  140. #endif
  141. DSIThread_MutexLock(&stMutexResponseQueue);
  142. DSIThread_CondSignal(&stCondWaitForResponse);
  143. clResponseQueue.Clear();
  144. DSIThread_MutexUnlock(&stMutexResponseQueue);
  145. if (hANTFSThread)
  146. {
  147. if (bANTFSThreadRunning == TRUE)
  148. {
  149. #if defined(DEBUG_FILE)
  150. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): SetEvent(stCondRequest).");
  151. #endif
  152. DSIThread_CondSignal(&stCondRequest);
  153. #if defined(DEBUG_FILE)
  154. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): SetEvent(stCondRxEvent).");
  155. #endif
  156. DSIThread_CondSignal(&stCondRxEvent);
  157. #if defined(DEBUG_FILE)
  158. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Killing thread.");
  159. #endif
  160. if (DSIThread_CondTimedWait(&stCondANTFSThreadExit, &stMutexCriticalSection, 9000) != DSI_THREAD_ENONE)
  161. {
  162. #if defined(DEBUG_FILE)
  163. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Thread not dead.");
  164. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Forcing thread termination...");
  165. #endif
  166. DSIThread_DestroyThread(hANTFSThread);
  167. }
  168. else
  169. {
  170. #if defined(DEBUG_FILE)
  171. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Thread terminated successfully.");
  172. #endif
  173. }
  174. }
  175. DSIThread_ReleaseThreadID(hANTFSThread);
  176. hANTFSThread = (DSI_THREAD_ID)NULL;
  177. }
  178. DSIThread_MutexUnlock(&stMutexCriticalSection);
  179. if (bTimerRunning)
  180. {
  181. #if defined(DEBUG_FILE)
  182. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Deleting Command Timer...");
  183. #endif
  184. delete pclTimer;
  185. pclTimer = (DSITimer*)NULL;
  186. DSIThread_MutexDestroy(&stMutexPairingTimeout);
  187. #if defined(DEBUG_FILE)
  188. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Deleted Timer.");
  189. #endif
  190. bTimerRunning = FALSE;
  191. }
  192. eANTFSState = ANTFS_CLIENT_STATE_OFF;
  193. pclANT = (DSIFramerANT*) NULL;
  194. #if defined(DEBUG_FILE)
  195. DSIDebug::ThreadWrite("ANTFSClientChannel::Close(): Closed.");
  196. #endif
  197. if (pucTransferBufferDynamic)
  198. {
  199. delete[] pucTransferBufferDynamic;
  200. pucTransferBufferDynamic = (UCHAR*)NULL;
  201. }
  202. }
  203. ///////////////////////////////////////////////////////////////////////
  204. void ANTFSClientChannel::Cancel(void)
  205. {
  206. #if defined(DEBUG_FILE)
  207. DSIDebug::ThreadWrite("ANTFSClientChannel::Cancel(): Cancel current operation...");
  208. #endif
  209. DSIThread_MutexLock(&stMutexCriticalSection);
  210. *pbCancel = TRUE;
  211. DSIThread_CondSignal(&stCondRxEvent);
  212. DSIThread_CondSignal(&stCondRequest);
  213. DSIThread_MutexUnlock(&stMutexCriticalSection);
  214. return;
  215. }
  216. ///////////////////////////////////////////////////////////////////////
  217. void ANTFSClientChannel::ProcessDeviceNotification(ANT_DEVICE_NOTIFICATION eCode_, void* pvParameter_)
  218. {
  219. DSIThread_MutexLock(&stMutexCriticalSection);
  220. if(eCode_ != ANT_DEVICE_NOTIFICATION_RESET &&
  221. eCode_ != ANT_DEVICE_NOTIFICATION_SHUTDOWN)
  222. {
  223. #if defined(DEBUG_FILE)
  224. UCHAR aucString[256];
  225. SNPRINTF((char *) aucString, 256, "ANTFSClientChannel::ProcessDeviceNotification(): Unknown code %0", eCode_);
  226. DSIDebug::ThreadWrite((char *) aucString);
  227. #endif
  228. DSIThread_MutexUnlock(&stMutexCriticalSection);
  229. return;
  230. }
  231. if(eANTFSState <= ANTFS_CLIENT_STATE_IDLE)
  232. {
  233. // We do not need to do anything, since ANT-FS is already in idle state
  234. DSIThread_MutexUnlock(&stMutexCriticalSection);
  235. return;
  236. }
  237. #if defined(DEBUG_FILE)
  238. DSIDebug::ThreadWrite("ANTFSClientChannel::ProcessDeviceNotification(): Resetting state...");
  239. #endif
  240. *pbCancel = TRUE;
  241. eANTFSRequest = ANTFS_REQUEST_INIT;
  242. DSIThread_CondSignal(&stCondRxEvent);
  243. DSIThread_CondSignal(&stCondRequest);
  244. DSIThread_MutexUnlock(&stMutexCriticalSection);
  245. return;
  246. }
  247. ///////////////////////////////////////////////////////////////////////
  248. ANTFS_RETURN ANTFSClientChannel::ConfigureClientParameters(ANTFS_CLIENT_PARAMS* pstInitParams_)
  249. {
  250. DSIThread_MutexLock(&stMutexCriticalSection);
  251. if(eANTFSState >= ANTFS_CLIENT_STATE_BEACONING)
  252. {
  253. #if defined(DEBUG_FILE)
  254. DSIDebug::ThreadWrite("ANTFSClientChannel::ConfigureClientParameters(): Incorrect state.");
  255. #endif
  256. DSIThread_MutexUnlock(&stMutexCriticalSection);
  257. return ANTFS_RETURN_FAIL;
  258. }
  259. if(pstInitParams_->ucAuthType == AUTH_COMMAND_PAIR && pstInitParams_->bPairingEnabled)
  260. {
  261. #if defined(DEBUG_FILE)
  262. DSIDebug::ThreadWrite("ANTFSClientChannel::ConfigureClientParameters(): Pairing was selected, but it is not supported.");
  263. #endif
  264. DSIThread_MutexUnlock(&stMutexCriticalSection);
  265. return ANTFS_RETURN_FAIL;
  266. }
  267. stInitParams = *pstInitParams_;
  268. ucActiveBeaconStatus1 = 0;
  269. ucActiveBeaconStatus1 |= ((stInitParams.ucLinkPeriod & BEACON_PERIOD_MASK) << BEACON_PERIOD_SHIFT);
  270. ucActiveBeaconStatus1 |= stInitParams.bPairingEnabled * PAIRING_AVAILABLE_FLAG_MASK;
  271. ucActiveBeaconStatus1 |= stInitParams.bUploadEnabled * UPLOAD_ENABLED_FLAG_MASK;
  272. ucActiveBeaconStatus1 |= stInitParams.bDataAvailable * DATA_AVAILABLE_FLAG_MASK;
  273. DSIThread_MutexUnlock(&stMutexCriticalSection);
  274. return ANTFS_RETURN_PASS;
  275. }
  276. ///////////////////////////////////////////////////////////////////////
  277. ANTFS_RETURN ANTFSClientChannel::SetPairingEnabled(BOOL bEnable_)
  278. {
  279. DSIThread_MutexLock(&stMutexCriticalSection);
  280. if(eANTFSState == ANTFS_CLIENT_STATE_AUTHENTICATING || eANTFSState == ANTFS_CLIENT_STATE_PAIRING_WAIT_FOR_RESPONSE ||
  281. (eANTFSState == ANTFS_CLIENT_STATE_CONNECTED && ucLinkCommandInProgress != ANTFS_CMD_NONE))
  282. {
  283. // Should not change the pairing capabilities while processing an authentication request
  284. #if defined(DEBUG_FILE)
  285. DSIDebug::ThreadWrite("ANTFSClientChannel::SetPairingEnabled(): Busy processing an authentication request.");
  286. #endif
  287. DSIThread_MutexUnlock(&stMutexCriticalSection);
  288. return ANTFS_RETURN_BUSY;
  289. }
  290. stInitParams.bPairingEnabled = bEnable_;
  291. if(bEnable_)
  292. ucActiveBeaconStatus1 |= PAIRING_AVAILABLE_FLAG_MASK;
  293. else
  294. ucActiveBeaconStatus1 &= ~PAIRING_AVAILABLE_FLAG_MASK;
  295. DSIThread_MutexUnlock(&stMutexCriticalSection);
  296. return ANTFS_RETURN_PASS;
  297. }
  298. ///////////////////////////////////////////////////////////////////////
  299. ANTFS_RETURN ANTFSClientChannel::SetUploadEnabled(BOOL bEnable_)
  300. {
  301. DSIThread_MutexLock(&stMutexCriticalSection);
  302. if(eANTFSState == ANTFS_CLIENT_STATE_UPLOADING || eANTFSState == ANTFS_CLIENT_STATE_UPLOADING_WAIT_FOR_RESPONSE ||
  303. (eANTFSState == ANTFS_CLIENT_STATE_TRANSPORT && ucLinkCommandInProgress != ANTFS_CMD_NONE))
  304. {
  305. // Should not change the upload capabilities while processing an upload
  306. #if defined(DEBUG_FILE)
  307. DSIDebug::ThreadWrite("ANTFSClientChannel::SetUploadEnabled(): Busy processing a transport request.");
  308. #endif
  309. DSIThread_MutexUnlock(&stMutexCriticalSection);
  310. return ANTFS_RETURN_BUSY;
  311. }
  312. stInitParams.bUploadEnabled = bEnable_;
  313. if(bEnable_)
  314. ucActiveBeaconStatus1 |= UPLOAD_ENABLED_FLAG_MASK;
  315. else
  316. ucActiveBeaconStatus1 &= ~UPLOAD_ENABLED_FLAG_MASK;
  317. DSIThread_MutexUnlock(&stMutexCriticalSection);
  318. return ANTFS_RETURN_PASS;
  319. }
  320. ///////////////////////////////////////////////////////////////////////
  321. ANTFS_RETURN ANTFSClientChannel::SetDataAvailable(BOOL bDataAvailable_)
  322. {
  323. DSIThread_MutexLock(&stMutexCriticalSection);
  324. if(eANTFSState == ANTFS_CLIENT_STATE_DOWNLOADING || eANTFSState == ANTFS_CLIENT_STATE_DOWNLOADING_WAIT_FOR_DATA ||
  325. (eANTFSState == ANTFS_CLIENT_STATE_TRANSPORT && ucLinkCommandInProgress != ANTFS_CMD_NONE))
  326. {
  327. // Should not change the data available bit while processing a download
  328. #if defined(DEBUG_FILE)
  329. DSIDebug::ThreadWrite("ANTFSClientChannel::SetDataAvailable(): Busy processing a transport request.");
  330. #endif
  331. DSIThread_MutexUnlock(&stMutexCriticalSection);
  332. return ANTFS_RETURN_BUSY;
  333. }
  334. stInitParams.bDataAvailable = bDataAvailable_;
  335. if(bDataAvailable_)
  336. ucActiveBeaconStatus1 |= DATA_AVAILABLE_FLAG_MASK;
  337. else
  338. ucActiveBeaconStatus1 &= ~DATA_AVAILABLE_FLAG_MASK;
  339. DSIThread_MutexUnlock(&stMutexCriticalSection);
  340. return ANTFS_RETURN_PASS;
  341. }
  342. ///////////////////////////////////////////////////////////////////////
  343. void ANTFSClientChannel::SetBeaconTimeout(UCHAR ucTimeout_)
  344. {
  345. stInitParams.ucBeaconTimeout = ucTimeout_;
  346. }
  347. ///////////////////////////////////////////////////////////////////////
  348. void ANTFSClientChannel::SetPairingTimeout(UCHAR ucTimeout_)
  349. {
  350. stInitParams.ucPairingTimeout = ucTimeout_;
  351. }
  352. ///////////////////////////////////////////////////////////////////////
  353. ANTFS_RETURN ANTFSClientChannel::SetFriendlyName(UCHAR* pucFriendlyName_, UCHAR ucFriendlyNameSize_)
  354. {
  355. DSIThread_MutexLock(&stMutexCriticalSection);
  356. if(eANTFSState == ANTFS_CLIENT_STATE_AUTHENTICATING)
  357. {
  358. // Should not change the friendly name while sending an authentication response
  359. #if defined(DEBUG_FILE)
  360. DSIDebug::ThreadWrite("ANTFSClientChannel::SetFriendlyName(): Busy authenticating.");
  361. #endif
  362. DSIThread_MutexUnlock(&stMutexCriticalSection);
  363. return ANTFS_RETURN_BUSY;
  364. }
  365. memset(aucFriendlyName, 0, sizeof(aucFriendlyName));
  366. if(pucFriendlyName_)
  367. {
  368. ucFriendlyNameSize = ucFriendlyNameSize_;
  369. memcpy(aucFriendlyName, pucFriendlyName_, ucFriendlyNameSize);
  370. }
  371. else
  372. {
  373. ucFriendlyNameSize = 0;
  374. }
  375. DSIThread_MutexUnlock(&stMutexCriticalSection);
  376. return ANTFS_RETURN_PASS;
  377. }
  378. ///////////////////////////////////////////////////////////////////////
  379. ANTFS_RETURN ANTFSClientChannel::SetPassKey(UCHAR* pucPassKey_, UCHAR ucPassKeySize_)
  380. {
  381. DSIThread_MutexLock(&stMutexCriticalSection);
  382. if(eANTFSState == ANTFS_CLIENT_STATE_CONNECTED && ucLinkCommandInProgress != ANTFS_CMD_NONE)
  383. {
  384. // Should not change the passkey while we are processing a request and comparing the keys
  385. #if defined(DEBUG_FILE)
  386. DSIDebug::ThreadWrite("ANTFSClientChannel::SetPassKey(): Busy processing an authentication request.");
  387. #endif
  388. DSIThread_MutexUnlock(&stMutexCriticalSection);
  389. return ANTFS_RETURN_BUSY;
  390. }
  391. memset(aucPassKey, 0, sizeof(aucPassKey));
  392. if(pucPassKey_)
  393. {
  394. ucPassKeySize = ucPassKeySize_;
  395. memcpy(aucPassKey, pucPassKey_, ucPassKeySize);
  396. }
  397. else
  398. {
  399. ucPassKeySize = 0;
  400. }
  401. DSIThread_MutexUnlock(&stMutexCriticalSection);
  402. return ANTFS_RETURN_PASS;
  403. }
  404. ///////////////////////////////////////////////////////////////////////
  405. void ANTFSClientChannel::SetChannelID(UCHAR ucDeviceType_, UCHAR ucTransmissionType_)
  406. {
  407. ucTheDeviceType = ucDeviceType_;
  408. ucTheTransmissionType = ucTransmissionType_;
  409. }
  410. ///////////////////////////////////////////////////////////////////////
  411. void ANTFSClientChannel::SetChannelPeriod(USHORT usChannelPeriod_)
  412. {
  413. usBeaconChannelPeriod = usChannelPeriod_;
  414. // TODO: Should we also change the content of the beacon?
  415. }
  416. ///////////////////////////////////////////////////////////////////////
  417. void ANTFSClientChannel::SetNetworkKey(UCHAR ucNetwork_, UCHAR ucNetworkkey[])
  418. {
  419. ucNetworkNumber = ucNetwork_;
  420. memcpy(aucTheNetworkkey,ucNetworkkey,8);
  421. }
  422. ///////////////////////////////////////////////////////////////////////
  423. void ANTFSClientChannel::SetTxPower(UCHAR ucPairingLv_, UCHAR ucConnectedLv_)
  424. {
  425. ucLinkTxPower = ucPairingLv_;
  426. ucSessionTxPower = ucConnectedLv_;
  427. bCustomTxPower = TRUE;
  428. }
  429. ///////////////////////////////////////////////////////////////////////
  430. ANTFS_RETURN ANTFSClientChannel::OpenBeacon()
  431. {
  432. DSIThread_MutexLock(&stMutexCriticalSection);
  433. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  434. {
  435. #if defined(DEBUG_FILE)
  436. DSIDebug::ThreadWrite("ANTFSClientChannel::OpenBeacon(): Request Busy.");
  437. #endif
  438. DSIThread_MutexUnlock(&stMutexCriticalSection);
  439. return ANTFS_RETURN_BUSY;
  440. }
  441. if (eANTFSState != ANTFS_CLIENT_STATE_IDLE)
  442. {
  443. #if defined(DEBUG_FILE)
  444. DSIDebug::ThreadWrite("ANTFSCient::OpenBeacon(): Not in correct state.");
  445. #endif
  446. DSIThread_MutexUnlock(&stMutexCriticalSection);
  447. return ANTFS_RETURN_FAIL;
  448. }
  449. #if defined(DEBUG_FILE)
  450. DSIDebug::ThreadWrite("ANTFSClientChannel::OpenBeacon(): Beacon starting...");
  451. #endif
  452. memset(&stHostDisconnectParams, 0, sizeof(stHostDisconnectParams)); // Clear old disconnect parameters
  453. ConfigureClientParameters(&stInitParams);
  454. SetANTChannelPeriod(stInitParams.ucLinkPeriod);
  455. ucActiveBeaconFrequency = stInitParams.ucBeaconFrequency;
  456. if(stInitParams.ulSerialNumber & 0x0000FFFF)
  457. {
  458. usRadioChannelID = (USHORT) (stInitParams.ulSerialNumber & 0x0000FFFF); // make sure ANT device number is not zero
  459. }
  460. eANTFSRequest = ANTFS_REQUEST_OPEN_BEACON;
  461. DSIThread_CondSignal(&stCondRequest);
  462. DSIThread_MutexUnlock(&stMutexCriticalSection);
  463. #if defined(DEBUG_FILE)
  464. DSIDebug::ThreadWrite("ANTFSClientChannel::OpenBeacon(): Open beacon request pending...");
  465. #endif
  466. return ANTFS_RETURN_PASS;
  467. }
  468. ///////////////////////////////////////////////////////////////////////
  469. ANTFS_RETURN ANTFSClientChannel::CloseBeacon(BOOL bReturnToBroadcast_)
  470. {
  471. ANTFS_RETURN eReturn = ANTFS_RETURN_PASS;
  472. DSIThread_MutexLock(&stMutexCriticalSection);
  473. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  474. {
  475. #if defined(DEBUG_FILE)
  476. DSIDebug::ThreadWrite("ANTFSClientChannel::CloseBeacon(): Request Busy.");
  477. #endif
  478. DSIThread_MutexUnlock(&stMutexCriticalSection);
  479. return ANTFS_RETURN_BUSY;
  480. }
  481. if (eANTFSState < ANTFS_CLIENT_STATE_OPENING)
  482. {
  483. #if defined(DEBUG_FILE)
  484. DSIDebug::ThreadWrite("ANTFSClientChannel::CloseBeacon(): Beacon is already closed.");
  485. #endif
  486. DSIThread_MutexUnlock(&stMutexCriticalSection);
  487. return ANTFS_RETURN_FAIL;
  488. }
  489. #if defined(DEBUG_FILE)
  490. DSIDebug::ThreadWrite("ANTFSClientChannel::CloseBeacon(): Beacon closing...");
  491. #endif
  492. bReturnToBroadcast = bReturnToBroadcast_;
  493. eANTFSRequest = ANTFS_REQUEST_CLOSE_BEACON;
  494. #if defined(DEBUG_FILE)
  495. DSIDebug::ThreadWrite("ANTFSClientChannel::CloseBeacon(): Close beacon request pending...");
  496. #endif
  497. DSIThread_CondSignal(&stCondRequest);
  498. DSIThread_MutexUnlock(&stMutexCriticalSection);
  499. return eReturn;
  500. }
  501. ///////////////////////////////////////////////////////////////////////
  502. #define MESG_CHANNEL_OFFSET 0
  503. #define MESG_EVENT_ID_OFFSET 1
  504. #define MESG_EVENT_CODE_OFFSET 2
  505. void ANTFSClientChannel::ProcessMessage(ANT_MESSAGE* pstMessage_, USHORT usMesgSize_)
  506. {
  507. UCHAR ucANTChannel;
  508. BOOL bProcessed = FALSE;
  509. if(!GetEnabled())
  510. return; // Only process ANT messages if ANT-FS is on
  511. if (usMesgSize_ < DSI_FRAMER_TIMEDOUT) //if the return isn't DSI_FRAMER_TIMEDOUT or DSI_FRAMER_ERROR
  512. {
  513. ucANTChannel = pstMessage_->aucData[MESG_CHANNEL_OFFSET] & CHANNEL_NUMBER_MASK;
  514. if(!FilterANTMessages(pstMessage_, ucANTChannel))
  515. return;
  516. switch (pstMessage_->ucMessageID)
  517. {
  518. case MESG_RESPONSE_EVENT_ID:
  519. if (pstMessage_->aucData[MESG_EVENT_ID_OFFSET] != MESG_EVENT_ID) // this is a response
  520. {
  521. memcpy(aucResponseBuf, pstMessage_->aucData, MESG_RESPONSE_EVENT_SIZE);
  522. bProcessed = ANTProtocolEventProcess(ucANTChannel, MESG_RESPONSE_EVENT_ID);
  523. }
  524. else // this is an event
  525. {
  526. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  527. bProcessed = ANTChannelEventProcess(ucANTChannel, pstMessage_->aucData[MESG_EVENT_CODE_OFFSET]); // pass through any events not handled here
  528. }
  529. break;
  530. case MESG_BROADCAST_DATA_ID:
  531. //Call channel event function with Broadcast message code
  532. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  533. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_BROADCAST);
  534. break;
  535. case MESG_ACKNOWLEDGED_DATA_ID:
  536. //Call channel event function with Acknowledged message code
  537. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  538. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_ACKNOWLEDGED);
  539. break;
  540. case MESG_BURST_DATA_ID:
  541. //Call channel event function with Burst message code
  542. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  543. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_BURST_PACKET);
  544. break;
  545. case MESG_EXT_BROADCAST_DATA_ID:
  546. //Call channel event function with Broadcast message code
  547. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  548. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_EXT_BROADCAST);
  549. break;
  550. case MESG_EXT_ACKNOWLEDGED_DATA_ID:
  551. //Call channel event function with Acknowledged message code
  552. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  553. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_EXT_ACKNOWLEDGED);
  554. break;
  555. case MESG_EXT_BURST_DATA_ID:
  556. //Call channel event function with Burst message code
  557. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  558. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_EXT_BURST_PACKET);
  559. break;
  560. case MESG_RSSI_BROADCAST_DATA_ID:
  561. //Call channel event function with Broadcast message code
  562. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  563. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_RSSI_BROADCAST);
  564. break;
  565. case MESG_RSSI_ACKNOWLEDGED_DATA_ID:
  566. //Call channel event function with Acknowledged message code
  567. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  568. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_RSSI_ACKNOWLEDGED);
  569. break;
  570. case MESG_RSSI_BURST_DATA_ID:
  571. //Call channel event function with Burst message code
  572. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  573. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_RSSI_BURST_PACKET);
  574. break;
  575. default:
  576. memcpy(aucResponseBuf, pstMessage_->aucData, usMesgSize_);
  577. bProcessed = ANTProtocolEventProcess(ucANTChannel, pstMessage_->ucMessageID );
  578. break;
  579. }
  580. }
  581. return;
  582. }
  583. ///////////////////////////////////////////////////////////////////////
  584. BOOL ANTFSClientChannel::GetEnabled()
  585. {
  586. if(eANTFSState < ANTFS_CLIENT_STATE_OPENING)
  587. {
  588. return FALSE;
  589. }
  590. return TRUE;
  591. }
  592. ///////////////////////////////////////////////////////////////////////
  593. ANTFS_CLIENT_STATE ANTFSClientChannel::GetStatus(void)
  594. {
  595. return eANTFSState;
  596. }
  597. ///////////////////////////////////////////////////////////////////////
  598. BOOL ANTFSClientChannel::GetHostName(UCHAR *aucHostFriendlyName_, UCHAR *pucBufferSize_)
  599. {
  600. if(!stHostFriendlyName.bNameSet)
  601. {
  602. *pucBufferSize_ = 0;
  603. return FALSE;
  604. }
  605. memset(aucHostFriendlyName_, 0, *pucBufferSize_);
  606. if (stHostFriendlyName.ucSize < *pucBufferSize_)
  607. {
  608. *pucBufferSize_ = stHostFriendlyName.ucSize;
  609. }
  610. memcpy(aucHostFriendlyName_, stHostFriendlyName.acFriendlyName, *pucBufferSize_);
  611. return TRUE;
  612. }
  613. ///////////////////////////////////////////////////////////////////////
  614. BOOL ANTFSClientChannel::GetRequestParameters(ANTFS_REQUEST_PARAMS* pstRequestParams_)
  615. {
  616. DSIThread_MutexLock(&stMutexCriticalSection);
  617. if(ucLinkCommandInProgress == ANTFS_CMD_NONE)
  618. {
  619. #if defined(DEBUG_FILE)
  620. DSIDebug::ThreadWrite("ANTFSClientChannel::GetRequestParameters(): No request in progress.");
  621. #endif
  622. DSIThread_MutexUnlock(&stMutexCriticalSection);
  623. return FALSE;
  624. }
  625. DSIThread_MutexUnlock(&stMutexCriticalSection);
  626. // Make a copy of the request parameters
  627. memcpy(pstRequestParams_, &stHostRequestParams, sizeof(stHostRequestParams));
  628. return TRUE;
  629. }
  630. ///////////////////////////////////////////////////////////////////////
  631. BOOL ANTFSClientChannel::GetRequestedFileIndex(USHORT* pusIndex_)
  632. {
  633. DSIThread_MutexLock(&stMutexCriticalSection);
  634. if(ucLinkCommandInProgress == ANTFS_CMD_NONE)
  635. {
  636. #if defined(DEBUG_FILE)
  637. DSIDebug::ThreadWrite("ANTFSClientChannel::GetRequestedFileIndex(): No request in progress.");
  638. #endif
  639. DSIThread_MutexUnlock(&stMutexCriticalSection);
  640. return FALSE;
  641. }
  642. DSIThread_MutexUnlock(&stMutexCriticalSection);
  643. *pusIndex_ = stHostRequestParams.usFileIndex;
  644. return TRUE;
  645. }
  646. ///////////////////////////////////////////////////////////////////////
  647. BOOL ANTFSClientChannel::GetDownloadStatus(ULONG *pulByteProgress_, ULONG *pulTotalLength_)
  648. {
  649. DSIThread_MutexLock(&stMutexCriticalSection);
  650. if (eANTFSState < ANTFS_CLIENT_STATE_TRANSPORT)
  651. {
  652. #if defined(DEBUG_FILE)
  653. DSIDebug::ThreadWrite("ANTFSClientChannel::GetDownloadStatus(): Incorrect state.");
  654. #endif
  655. *pulTotalLength_ = 10; // Avoid division by zero when calculating progress
  656. *pulByteProgress_ = 0;
  657. DSIThread_MutexUnlock(&stMutexCriticalSection);
  658. return FALSE;
  659. }
  660. if (ulTransferFileSize == 0)
  661. {
  662. #if defined(DEBUG_FILE)
  663. DSIDebug::ThreadWrite("ANTFSClientChannel::GetDownloadStatus(): Download not in progress.");
  664. #endif
  665. *pulTotalLength_ = 10; // Avoid division by zero when calculating progress
  666. *pulByteProgress_ = 0;
  667. DSIThread_MutexUnlock(&stMutexCriticalSection);
  668. return FALSE;
  669. }
  670. *pulTotalLength_ = ulTransferBurstIndex + ulTransferBytesRemaining;
  671. *pulByteProgress_ = ulTransferBurstIndex; // initialize with the current location within the data block
  672. if (ulDownloadProgress >= 24)
  673. {
  674. *pulByteProgress_ = *pulByteProgress_ + ulDownloadProgress - 24; // do not count the 24 bytes of the header
  675. }
  676. if (ulDownloadProgress >= ulTransferBytesRemaining + 24)
  677. {
  678. *pulByteProgress_ = *pulByteProgress_ - 8; // do not count the 8 byte footer
  679. }
  680. #if defined(DEBUG_FILE)
  681. UCHAR aucString[256];
  682. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::GetDownloadStatus(): %lu/%lu", *pulByteProgress_, *pulTotalLength_);
  683. DSIDebug::ThreadWrite((char*) aucString);
  684. #endif
  685. DSIThread_MutexUnlock(&stMutexCriticalSection);
  686. return TRUE;
  687. }
  688. ///////////////////////////////////////////////////////////////////////
  689. BOOL ANTFSClientChannel::GetUploadStatus(ULONG *pulByteProgress_, ULONG *pulTotalLength_)
  690. {
  691. // TODO: Implement GetUploadStatus
  692. return FALSE;
  693. }
  694. ///////////////////////////////////////////////////////////////////////
  695. BOOL ANTFSClientChannel::GetTransferData(ULONG *pulDataSize_ , void *pvData_)
  696. {
  697. // TODO: Implement GetTransferData
  698. return FALSE;
  699. }
  700. ///////////////////////////////////////////////////////////////////////
  701. BOOL ANTFSClientChannel::GetDisconnectParameters(ANTFS_DISCONNECT_PARAMS* pstDisconnectParams_)
  702. {
  703. // Make a copy of the requested parameters
  704. memcpy(pstDisconnectParams_, &stHostDisconnectParams, sizeof(stHostDisconnectParams));
  705. return TRUE;
  706. }
  707. ///////////////////////////////////////////////////////////////////////
  708. ANTFS_RETURN ANTFSClientChannel::SendPairingResponse(BOOL bAccept_)
  709. {
  710. DSIThread_MutexLock(&stMutexPairingTimeout);
  711. if(bTimeoutEvent)
  712. {
  713. #if defined(DEBUG_FILE)
  714. DSIDebug::ThreadWrite("ANTFSClientChannel::SendPairingResponse(): Pairing request timed out.");
  715. #endif
  716. DSIThread_MutexUnlock(&stMutexPairingTimeout);
  717. return ANTFS_RETURN_FAIL;
  718. }
  719. ucPairingTimeout = MAX_UCHAR; // Disable timeout
  720. DSIThread_MutexUnlock(&stMutexPairingTimeout);
  721. DSIThread_MutexLock(&stMutexCriticalSection);
  722. if(stInitParams.bPairingEnabled != TRUE)
  723. {
  724. #if defined(DEBUG_FILE)
  725. DSIDebug::ThreadWrite("ANTFSClientChannel::SendPairingResponse(): Pairing not supported.");
  726. #endif
  727. DSIThread_MutexUnlock(&stMutexCriticalSection);
  728. return ANTFS_RETURN_FAIL;
  729. }
  730. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  731. {
  732. #if defined(DEBUG_FILE)
  733. DSIDebug::ThreadWrite("ANTFSClientChannel::SendPairingResponse(): Request Busy.");
  734. #endif
  735. DSIThread_MutexUnlock(&stMutexCriticalSection);
  736. return ANTFS_RETURN_BUSY;
  737. }
  738. if (eANTFSState != ANTFS_CLIENT_STATE_PAIRING_WAIT_FOR_RESPONSE)
  739. {
  740. #if defined(DEBUG_FILE)
  741. DSIDebug::ThreadWrite("ANTFSClientChannel::SendPairingResponse(): Not in correct state.");
  742. #endif
  743. DSIThread_MutexUnlock(&stMutexCriticalSection);
  744. return ANTFS_RETURN_FAIL;
  745. }
  746. bAcceptRequest = bAccept_;
  747. ucLinkCommandInProgress = ANTFS_AUTHENTICATE_ID;
  748. eANTFSState = ANTFS_CLIENT_STATE_AUTHENTICATING;
  749. eANTFSRequest = ANTFS_REQUEST_AUTHENTICATE;
  750. DSIThread_CondSignal(&stCondRequest);
  751. DSIThread_MutexUnlock(&stMutexCriticalSection);
  752. return ANTFS_RETURN_PASS;
  753. }
  754. ///////////////////////////////////////////////////////////////////////
  755. ANTFS_RETURN ANTFSClientChannel::SendDownloadResponse(UCHAR ucResponse_, ANTFS_DOWNLOAD_PARAMS* pstDownloadInfo_, ULONG ulDataLength_, void *pvData_)
  756. {
  757. DSIThread_MutexLock(&stMutexCriticalSection);
  758. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  759. {
  760. #if defined(DEBUG_FILE)
  761. DSIDebug::ThreadWrite("ANTFSClientChannel::SendDownloadResponse(): Request Busy.");
  762. #endif
  763. DSIThread_MutexUnlock(&stMutexCriticalSection);
  764. return ANTFS_RETURN_BUSY;
  765. }
  766. if (eANTFSState != ANTFS_CLIENT_STATE_DOWNLOADING_WAIT_FOR_DATA)
  767. {
  768. #if defined(DEBUG_FILE)
  769. DSIDebug::ThreadWrite("ANTFSClientChannel::SendDownloadResponse(): Not in correct state.");
  770. #endif
  771. DSIThread_MutexUnlock(&stMutexCriticalSection);
  772. return ANTFS_RETURN_FAIL;
  773. }
  774. if(pstDownloadInfo_ && usTransferDataFileIndex != pstDownloadInfo_->usFileIndex)
  775. {
  776. #if defined(DEBUG_FILE)
  777. UCHAR aucString[256];
  778. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::SendDownloadResponse(): This is not the file requested, expected %hu and got %hu",
  779. usTransferDataFileIndex, pstDownloadInfo_->usFileIndex);
  780. DSIDebug::ThreadWrite((char*) aucString);
  781. #endif
  782. DSIThread_MutexUnlock(&stMutexCriticalSection);
  783. return ANTFS_RETURN_FAIL;
  784. }
  785. ucRequestResponse = ucResponse_;
  786. if(ucRequestResponse != DOWNLOAD_RESPONSE_OK)
  787. {
  788. // Download is being rejected, there are no other parameters to check
  789. ulTransferBytesRemaining = 0;
  790. ulTransferBurstIndex = 0;
  791. ulTransferFileSize = 0;
  792. eANTFSRequest = ANTFS_REQUEST_DOWNLOAD_RESPONSE;
  793. DSIThread_CondSignal(&stCondRequest);
  794. DSIThread_MutexUnlock(&stMutexCriticalSection);
  795. return ANTFS_RETURN_PASS;
  796. }
  797. ulTransferFileSize = ulDataLength_; // File size of the requested download
  798. pucDownloadData = (UCHAR*)pvData_; // Data block to download
  799. usTransferCrc = 0; // Initialize to zero, as application only receives the initial request
  800. ulDownloadProgress = 0; // No data burst yet
  801. ulTransferBurstIndex = stHostRequestParams.ulOffset; // Initialize current position in burst to offset requested by host
  802. if(ulTransferBurstIndex > ulTransferFileSize)
  803. {
  804. ulTransferBurstIndex = ulTransferFileSize;
  805. }
  806. ulTransferBytesRemaining = stHostRequestParams.ulBlockSize; // Initialize number of remaining bytes to host specified maximum block size
  807. if((stHostRequestParams.ulBlockSize == 0) || (ulTransferFileSize < stHostRequestParams.ulBlockSize)) // If the host is not limiting download size or the file size does not exceed the host's download size limit
  808. {
  809. ulTransferBytesRemaining = ulTransferFileSize; // Number of bytes remaining to be downloaded in this block is the file size
  810. }
  811. if((ulTransferFileSize - ulTransferBurstIndex) < ulTransferBytesRemaining)
  812. {
  813. ulTransferBytesRemaining = ulTransferFileSize - ulTransferBurstIndex; // Calculate number of remaining bytes in this block based on the offset
  814. }
  815. ulTransferBlockSize = pstDownloadInfo_->ulMaxBlockSize;
  816. if((pstDownloadInfo_->ulMaxBlockSize != 0) && (ulTransferBytesRemaining > pstDownloadInfo_->ulMaxBlockSize)) // If the application is limiting the block size
  817. {
  818. ulTransferBytesRemaining = pstDownloadInfo_->ulMaxBlockSize; // Number of remaining bytes in this block is the application defined block size
  819. }
  820. eANTFSRequest = ANTFS_REQUEST_DOWNLOAD_RESPONSE;
  821. DSIThread_CondSignal(&stCondRequest);
  822. DSIThread_MutexUnlock(&stMutexCriticalSection);
  823. return ANTFS_RETURN_PASS;
  824. }
  825. ///////////////////////////////////////////////////////////////////////
  826. ANTFS_RETURN ANTFSClientChannel::SendUploadResponse(UCHAR ucResponse_, ANTFS_UPLOAD_PARAMS* pstUploadInfo_, ULONG ulDataLength_, void *pvData_)
  827. {
  828. DSIThread_MutexLock(&stMutexCriticalSection);
  829. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  830. {
  831. #if defined(DEBUG_FILE)
  832. DSIDebug::ThreadWrite("ANTFSClientChannel::SendUploadResponse(): Request Busy.");
  833. #endif
  834. DSIThread_MutexUnlock(&stMutexCriticalSection);
  835. return ANTFS_RETURN_BUSY;
  836. }
  837. if (eANTFSState != ANTFS_CLIENT_STATE_UPLOADING_WAIT_FOR_RESPONSE)
  838. {
  839. #if defined(DEBUG_FILE)
  840. DSIDebug::ThreadWrite("ANTFSClientChannel::SendUploadResponse(): Not in correct state.");
  841. #endif
  842. DSIThread_MutexUnlock(&stMutexCriticalSection);
  843. return ANTFS_RETURN_FAIL;
  844. }
  845. if(pstUploadInfo_ && usTransferDataFileIndex != pstUploadInfo_->usFileIndex)
  846. {
  847. #if defined(DEBUG_FILE)
  848. UCHAR aucString[256];
  849. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::SendUploadResponse(): This is not the file requested, expected %hu and got %hu",
  850. usTransferDataFileIndex, pstUploadInfo_->usFileIndex);
  851. DSIDebug::ThreadWrite((char*) aucString);
  852. #endif
  853. DSIThread_MutexUnlock(&stMutexCriticalSection);
  854. return ANTFS_RETURN_FAIL;
  855. }
  856. ucRequestResponse = ucResponse_;
  857. if(ucRequestResponse != UPLOAD_RESPONSE_OK)
  858. {
  859. // Upload is being rejected, there are no other parameters to check
  860. ulTransferBlockOffset = 0;
  861. ulTransferMaxIndex = 0;
  862. ulTransferBlockSize = 0;
  863. usTransferCrc = 0;
  864. eANTFSRequest = ANTFS_REQUEST_UPLOAD_RESPONSE;
  865. DSIThread_CondSignal(&stCondRequest);
  866. DSIThread_MutexUnlock(&stMutexCriticalSection);
  867. return ANTFS_RETURN_PASS;
  868. }
  869. if(stHostRequestParams.ulMaxSize > pstUploadInfo_->ulMaxSize)
  870. ucRequestResponse = UPLOAD_RESPONSE_INSUFFICIENT_SPACE;
  871. if((stHostRequestParams.ulOffset > stHostRequestParams.ulMaxSize) && (stHostRequestParams.ulOffset != MAX_ULONG))
  872. ucRequestResponse = UPLOAD_RESPONSE_REQUEST_INVALID;
  873. ulTransferMaxIndex = pstUploadInfo_->ulMaxSize;
  874. if(pstUploadInfo_->ulMaxBlockSize)
  875. ulTransferBlockSize = pstUploadInfo_->ulMaxBlockSize;
  876. else
  877. ulTransferBlockSize = ulTransferMaxIndex;
  878. ulTransferBurstIndex = 0;
  879. ulTransferBlockOffset = 0;
  880. usTransferCrc = 0;
  881. ulTransferBytesRemaining = ulTransferMaxIndex;
  882. if(stHostRequestParams.ulOffset != MAX_ULONG)
  883. {
  884. ulTransferBytesRemaining = stHostRequestParams.ulMaxSize - stHostRequestParams.ulOffset;
  885. ulTransferBlockOffset = stHostRequestParams.ulOffset;
  886. usTransferCrc = 0; // TODO: Validate that data was provided and calculate CRC.
  887. }
  888. eANTFSRequest = ANTFS_REQUEST_UPLOAD_RESPONSE;
  889. DSIThread_CondSignal(&stCondRequest);
  890. DSIThread_MutexUnlock(&stMutexCriticalSection);
  891. return ANTFS_RETURN_PASS;
  892. }
  893. ///////////////////////////////////////////////////////////////////////
  894. ANTFS_RETURN ANTFSClientChannel::SendEraseResponse(UCHAR ucResponse_)
  895. {
  896. DSIThread_MutexLock(&stMutexCriticalSection);
  897. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  898. {
  899. #if defined(DEBUG_FILE)
  900. DSIDebug::ThreadWrite("ANTFSClientChannel::SendEraseResponse(): Request Busy.");
  901. #endif
  902. DSIThread_MutexUnlock(&stMutexCriticalSection);
  903. return ANTFS_RETURN_BUSY;
  904. }
  905. if (eANTFSState != ANTFS_CLIENT_STATE_ERASING)
  906. {
  907. #if defined(DEBUG_FILE)
  908. DSIDebug::ThreadWrite("ANTFSClientChannel::SendEraseResponse(): Not in correct state.");
  909. #endif
  910. DSIThread_MutexUnlock(&stMutexCriticalSection);
  911. return ANTFS_RETURN_FAIL;
  912. }
  913. ucRequestResponse = ucResponse_;
  914. ucLinkCommandInProgress = ANTFS_ERASE_ID;
  915. eANTFSState = ANTFS_CLIENT_STATE_ERASING;
  916. eANTFSRequest = ANTFS_REQUEST_ERASE_RESPONSE;
  917. DSIThread_CondSignal(&stCondRequest);
  918. DSIThread_MutexUnlock(&stMutexCriticalSection);
  919. return ANTFS_RETURN_PASS;
  920. }
  921. ///////////////////////////////////////////////////////////////////////
  922. //Returns a response if there is one ready, otherwise waits the specified time for one to occur
  923. ANTFS_CLIENT_RESPONSE ANTFSClientChannel::WaitForResponse(ULONG ulMilliseconds_)
  924. {
  925. ANTFS_CLIENT_RESPONSE stResponse = ANTFS_CLIENT_RESPONSE_NONE;
  926. if (bKillThread == TRUE)
  927. return ANTFS_CLIENT_RESPONSE_NONE;
  928. //Wait for response
  929. DSIThread_MutexLock(&stMutexResponseQueue);
  930. if(clResponseQueue.isEmpty())
  931. {
  932. UCHAR ucResult = DSIThread_CondTimedWait(&stCondWaitForResponse, &stMutexResponseQueue, ulMilliseconds_);
  933. switch(ucResult)
  934. {
  935. case DSI_THREAD_ENONE:
  936. stResponse = clResponseQueue.GetResponse();
  937. break;
  938. case DSI_THREAD_ETIMEDOUT:
  939. stResponse = ANTFS_CLIENT_RESPONSE_NONE;
  940. break;
  941. case DSI_THREAD_EOTHER:
  942. #if defined(DEBUG_FILE)
  943. DSIDebug::ThreadWrite("ANTFSClientChannel::WaitForResponse(): CondTimedWait() Failed!");
  944. #endif
  945. stResponse = ANTFS_CLIENT_RESPONSE_NONE;
  946. break;
  947. default:
  948. #if defined(DEBUG_FILE)
  949. DSIDebug::ThreadWrite("ANTFSClientChannel::WaitForResponse(): Error Unknown...");
  950. #endif
  951. stResponse = ANTFS_CLIENT_RESPONSE_NONE;
  952. break;
  953. }
  954. }
  955. else
  956. {
  957. stResponse = clResponseQueue.GetResponse();
  958. }
  959. DSIThread_MutexUnlock(&stMutexResponseQueue);
  960. return stResponse;
  961. }
  962. //////////////////////////////////////////////////////////////////////////////////
  963. // Private Functions
  964. //////////////////////////////////////////////////////////////////////////////////
  965. DSI_THREAD_RETURN ANTFSClientChannel::ANTFSThreadStart(void *pvParameter_)
  966. {
  967. #if defined(DEBUG_FILE)
  968. DSIDebug::ThreadInit("ANTFSClient");
  969. #endif
  970. ((ANTFSClientChannel *)pvParameter_)->ANTFSThread();
  971. return 0;
  972. }
  973. ///////////////////////////////////////////////////////////////////////
  974. // ANTFS Task Thread
  975. ///////////////////////////////////////////////////////////////////////
  976. void ANTFSClientChannel::ANTFSThread(void)
  977. {
  978. ANTFS_CLIENT_RESPONSE eResponse;
  979. bANTFSThreadRunning = TRUE;
  980. while (bKillThread == FALSE)
  981. {
  982. eResponse = ANTFS_CLIENT_RESPONSE_NONE;
  983. #if defined(DEBUG_FILE)
  984. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Awaiting Requests...");
  985. #endif
  986. DSIThread_MutexLock(&stMutexCriticalSection);
  987. if (*pbCancel)
  988. {
  989. *pbCancel = FALSE;
  990. if (eANTFSRequest != ANTFS_REQUEST_INIT && eANTFSRequest != ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  991. {
  992. eANTFSRequest = ANTFS_REQUEST_NONE; //Clear any other request
  993. }
  994. AddResponse(ANTFS_CLIENT_RESPONSE_CANCEL_DONE);
  995. }
  996. if ((eANTFSRequest == ANTFS_REQUEST_NONE) && (bKillThread == FALSE))
  997. {
  998. UCHAR ucResult = DSIThread_CondTimedWait(&stCondRequest, &stMutexCriticalSection, (ULONG) (stInitParams.ucBeaconTimeout * 1000));
  999. if (ucResult != DSI_THREAD_ENONE)
  1000. {
  1001. #if defined(DEBUG_FILE)
  1002. if(ucResult == DSI_THREAD_EOTHER)
  1003. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): CondTimedWait() Failed!");
  1004. #endif
  1005. // If connected to a host, and we have not received any requests, go back to link state
  1006. if ((eANTFSRequest == ANTFS_REQUEST_NONE) && (stInitParams.ucBeaconTimeout != CMD_TIMEOUT_DISABLED) && (eANTFSState >= ANTFS_CLIENT_STATE_CONNECTED) && (ucLinkCommandInProgress == ANTFS_CMD_NONE))
  1007. {
  1008. eANTFSRequest = ANTFS_REQUEST_CONNECTION_LOST;
  1009. #if defined(DEBUG_FILE)
  1010. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): No requests received, dropping back to link");
  1011. #endif
  1012. }
  1013. // If we are in link state, reload the beacon
  1014. // We do this in order to be able to detect serial failures while in this state
  1015. if((eANTFSState == ANTFS_CLIENT_STATE_BEACONING) && (eANTFSRequest == ANTFS_REQUEST_NONE))
  1016. {
  1017. if(SwitchToLink() == RETURN_SERIAL_ERROR)
  1018. {
  1019. #if defined(DEBUG_FILE)
  1020. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Serial error while beaconing");
  1021. #endif
  1022. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1023. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1024. }
  1025. }
  1026. // If ping is disabled, go back to transport state if we were waiting for an application response
  1027. // We need to do this to avoid getting stuck in busy state if the application never sends a response
  1028. if((stInitParams.ucBeaconTimeout == CMD_TIMEOUT_DISABLED) && (eANTFSRequest == ANTFS_REQUEST_NONE))
  1029. {
  1030. if(eANTFSState == ANTFS_CLIENT_STATE_ERASING)
  1031. {
  1032. ucRequestResponse = ERASE_RESPONSE_REJECT;
  1033. eANTFSRequest = ANTFS_REQUEST_ERASE_RESPONSE;
  1034. #if defined(DEBUG_FILE)
  1035. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): No response received for the erase request, rejecting...");
  1036. #endif
  1037. }
  1038. else if(eANTFSState == ANTFS_CLIENT_STATE_DOWNLOADING_WAIT_FOR_DATA)
  1039. {
  1040. ucRequestResponse = DOWNLOAD_RESPONSE_NOT_READY;
  1041. ulTransferBytesRemaining = 0;
  1042. ulTransferBurstIndex = 0;
  1043. ulTransferFileSize = 0;
  1044. usTransferCrc = 0;
  1045. eANTFSRequest = ANTFS_REQUEST_DOWNLOAD_RESPONSE;
  1046. #if defined(DEBUG_FILE)
  1047. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): No response received for the download request, rejecting...");
  1048. #endif
  1049. }
  1050. else if(eANTFSState == ANTFS_CLIENT_STATE_UPLOADING_WAIT_FOR_RESPONSE)
  1051. {
  1052. ucRequestResponse = UPLOAD_RESPONSE_NOT_READY;
  1053. ulTransferBlockOffset = 0;
  1054. ulTransferMaxIndex = 0;
  1055. ulTransferBlockSize = 0;
  1056. usTransferCrc = 0;
  1057. eANTFSRequest = ANTFS_REQUEST_UPLOAD_RESPONSE;
  1058. #if defined(DEBUG_FILE)
  1059. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): No response received for the upload request, rejecting...");
  1060. #endif
  1061. }
  1062. }
  1063. }
  1064. }
  1065. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1066. if (bKillThread)
  1067. break;
  1068. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  1069. {
  1070. #if defined(DEBUG_FILE)
  1071. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Request received");
  1072. #endif
  1073. switch (eANTFSRequest)
  1074. {
  1075. case ANTFS_REQUEST_INIT:
  1076. {
  1077. #if defined(DEBUG_FILE)
  1078. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Idle.");
  1079. #endif
  1080. ResetClientState();
  1081. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  1082. eResponse = ANTFS_CLIENT_RESPONSE_INIT_PASS;
  1083. } // ANTFS_REQUEST_INIT
  1084. break;
  1085. case ANTFS_REQUEST_OPEN_BEACON:
  1086. {
  1087. RETURN_STATUS eReturn;
  1088. #if defined(DEBUG_FILE)
  1089. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Opening beacon...");
  1090. #endif
  1091. eANTFSState = ANTFS_CLIENT_STATE_OPENING;
  1092. eReturn = AttemptOpenBeacon();
  1093. if (eReturn == RETURN_SERIAL_ERROR)
  1094. {
  1095. #if defined(DEBUG_FILE)
  1096. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Open beacon serial error.");
  1097. #endif
  1098. DSIThread_MutexLock(&stMutexCriticalSection);
  1099. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1100. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1101. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1102. }
  1103. else if (eReturn == RETURN_FAIL)
  1104. {
  1105. #if defined(DEBUG_FILE)
  1106. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Opening beacon failed.");
  1107. #endif
  1108. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  1109. }
  1110. else if (eReturn == RETURN_STOP)
  1111. {
  1112. #if defined(DEBUG_FILE)
  1113. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Opening beacon stopped.");
  1114. #endif
  1115. AttemptCloseBeacon();
  1116. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  1117. }
  1118. else if (eReturn == RETURN_PASS)
  1119. {
  1120. SwitchToLink();
  1121. eANTFSState = ANTFS_CLIENT_STATE_BEACONING;
  1122. eResponse = ANTFS_CLIENT_RESPONSE_BEACON_OPEN;
  1123. #if defined(DEBUG_FILE)
  1124. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Beacon Open...");
  1125. #endif
  1126. }
  1127. } // ANTFS_REQUEST_OPEN_BEACON
  1128. break;
  1129. case ANTFS_REQUEST_CLOSE_BEACON:
  1130. {
  1131. RETURN_STATUS eReturn;
  1132. #if defined(DEBUG_FILE)
  1133. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Closing beacon...");
  1134. #endif
  1135. eReturn = AttemptCloseBeacon();
  1136. if (eReturn == RETURN_SERIAL_ERROR)
  1137. {
  1138. #if defined(DEBUG_FILE)
  1139. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Close beacon serial error.");
  1140. #endif
  1141. DSIThread_MutexLock(&stMutexCriticalSection);
  1142. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1143. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1144. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1145. }
  1146. else if (eReturn == RETURN_PASS)
  1147. {
  1148. ResetClientState();
  1149. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  1150. eResponse = ANTFS_CLIENT_RESPONSE_BEACON_CLOSED;
  1151. }
  1152. } // ANTFS_CLOSE_BEACON
  1153. break;
  1154. case ANTFS_REQUEST_CONNECT:
  1155. {
  1156. RETURN_STATUS eReturn;
  1157. #if defined(DEBUG_FILE)
  1158. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Connecting to host device...");
  1159. #endif
  1160. eReturn = SwitchToAuthenticate();
  1161. if (eReturn == RETURN_SERIAL_ERROR)
  1162. {
  1163. #if defined(DEBUG_FILE)
  1164. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Serial error while connecting to host device.");
  1165. #endif
  1166. DSIThread_MutexLock(&stMutexCriticalSection);
  1167. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1168. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1169. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1170. }
  1171. else if (eReturn == RETURN_FAIL)
  1172. {
  1173. #if defined(DEBUG_FILE)
  1174. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Connecting to host device failed.");
  1175. #endif
  1176. eANTFSState = ANTFS_CLIENT_STATE_BEACONING;
  1177. eANTFSRequest = ANTFS_REQUEST_NONE;
  1178. }
  1179. else if (eReturn == RETURN_PASS)
  1180. {
  1181. eANTFSRequest = ANTFS_REQUEST_NONE;
  1182. eANTFSState = ANTFS_CLIENT_STATE_CONNECTED;
  1183. eResponse = ANTFS_CLIENT_RESPONSE_CONNECT_PASS;
  1184. #if defined(DEBUG_FILE)
  1185. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Connected to host device...");
  1186. #endif
  1187. }
  1188. } // ANTFS_REQUEST_CONNECT
  1189. break;
  1190. case ANTFS_REQUEST_DISCONNECT:
  1191. {
  1192. RETURN_STATUS eReturn;
  1193. ANTFS_CLIENT_STATE ePrevState = eANTFSState;
  1194. #if defined(DEBUG_FILE)
  1195. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Disconnecting...");
  1196. #endif
  1197. eReturn = SwitchToLink();
  1198. if (eReturn == RETURN_SERIAL_ERROR)
  1199. {
  1200. #if defined(DEBUG_FILE)
  1201. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Serial error while disconnecting from host device.");
  1202. #endif
  1203. DSIThread_MutexLock(&stMutexCriticalSection);
  1204. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1205. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1206. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1207. }
  1208. else if (eReturn == RETURN_FAIL)
  1209. {
  1210. #if defined(DEBUG_FILE)
  1211. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Disconnecting from host device failed.");
  1212. #endif
  1213. eANTFSState = ePrevState; // Remain in previous state
  1214. eANTFSRequest = ANTFS_REQUEST_NONE;
  1215. }
  1216. else if (eReturn == RETURN_PASS)
  1217. {
  1218. if(stHostDisconnectParams.ucCommandType == DISCONNECT_COMMAND_BROADCAST)
  1219. {
  1220. ResetClientState();
  1221. eANTFSRequest = ANTFS_REQUEST_NONE;
  1222. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  1223. eResponse = ANTFS_CLIENT_RESPONSE_DISCONNECT_PASS;
  1224. #if defined(DEBUG_FILE)
  1225. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): End of ANT-FS session. Return to broadcast.");
  1226. #endif
  1227. }
  1228. else
  1229. {
  1230. eANTFSRequest = ANTFS_REQUEST_NONE;
  1231. eANTFSState = ANTFS_CLIENT_STATE_BEACONING;
  1232. eResponse = ANTFS_CLIENT_RESPONSE_DISCONNECT_PASS;
  1233. #if defined(DEBUG_FILE)
  1234. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Disconnected from host device...");
  1235. #endif
  1236. }
  1237. }
  1238. } // ANTFS_REQUEST_DISCONNECT
  1239. break;
  1240. case ANTFS_REQUEST_PING:
  1241. {
  1242. // Do nothing
  1243. eANTFSRequest = ANTFS_REQUEST_NONE;
  1244. } // ANTFS_REQUEST_PING
  1245. break;
  1246. case ANTFS_REQUEST_PAIR:
  1247. {
  1248. #if defined(DEBUG_FILE)
  1249. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Pairing request.");
  1250. #endif
  1251. DSIThread_MutexLock(&stMutexPairingTimeout);
  1252. eANTFSState = ANTFS_CLIENT_STATE_PAIRING_WAIT_FOR_RESPONSE;
  1253. eResponse = ANTFS_CLIENT_RESPONSE_PAIRING_REQUEST;
  1254. ucPairingTimeout = stInitParams.ucPairingTimeout; // Changed from PAIRING_TIMEOUT to match the timeout at host, but this might be too short
  1255. DSIThread_MutexUnlock(&stMutexPairingTimeout);
  1256. } // ANTFS_REQUEST_PAIR
  1257. break;
  1258. case ANTFS_REQUEST_AUTHENTICATE:
  1259. {
  1260. RETURN_STATUS eReturn;
  1261. DSIThread_MutexLock(&stMutexPairingTimeout);
  1262. if(bTimeoutEvent)
  1263. {
  1264. bAcceptRequest = FALSE;
  1265. bTimeoutEvent = FALSE;
  1266. AddResponse(ANTFS_CLIENT_RESPONSE_PAIRING_TIMEOUT);
  1267. #if defined(DEBUG_FILE)
  1268. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Pairing timeout.");
  1269. #endif
  1270. }
  1271. eANTFSState = ANTFS_CLIENT_STATE_AUTHENTICATING;
  1272. DSIThread_MutexUnlock(&stMutexPairingTimeout);
  1273. #if defined(DEBUG_FILE)
  1274. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Authenticating...");
  1275. #endif
  1276. eReturn = AttemptAuthenticateResponse();
  1277. if (eReturn == RETURN_PASS)
  1278. {
  1279. #if defined(DEBUG_FILE)
  1280. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Authentication request accepted.");
  1281. #endif
  1282. SwitchToTransport(); // We passed auth, so go to transport state
  1283. eResponse = ANTFS_CLIENT_RESPONSE_AUTHENTICATE_PASS;
  1284. }
  1285. else if (eReturn == RETURN_FAIL)
  1286. {
  1287. #if defined(DEBUG_FILE)
  1288. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Authentication failed.");
  1289. #endif
  1290. SwitchToAuthenticate(); // Stand by ready for retry
  1291. }
  1292. else if (eReturn == RETURN_REJECT)
  1293. {
  1294. #if defined(DEBUG_FILE)
  1295. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Authentication request rejected.");
  1296. #endif
  1297. SwitchToLink(); // We failed auth, so go to link state
  1298. eResponse = ANTFS_CLIENT_RESPONSE_AUTHENTICATE_REJECT;
  1299. }
  1300. else if (eReturn == RETURN_SERIAL_ERROR)
  1301. {
  1302. #if defined(DEBUG_FILE)
  1303. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Authentication serial error.");
  1304. #endif
  1305. DSIThread_MutexLock(&stMutexCriticalSection);
  1306. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1307. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1308. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1309. }
  1310. else if (eReturn == RETURN_STOP)
  1311. {
  1312. #if defined(DEBUG_FILE)
  1313. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Authentication stopped.");
  1314. #endif
  1315. SwitchToAuthenticate();
  1316. }
  1317. else //RETURN_NA
  1318. {
  1319. #if defined(DEBUG_FILE)
  1320. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Authentication NA.");
  1321. #endif
  1322. SwitchToAuthenticate();
  1323. eResponse = ANTFS_CLIENT_RESPONSE_AUTHENTICATE_NA;
  1324. }
  1325. } // ANTFS_REQUEST_AUTHENTICATE
  1326. break;
  1327. case ANTFS_REQUEST_CHANGE_LINK:
  1328. {
  1329. RETURN_STATUS eReturn;
  1330. #if defined(DEBUG_FILE)
  1331. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Changing radio frequency and channel period...");
  1332. #endif
  1333. eReturn = SwitchLinkParameters();
  1334. if (eReturn == RETURN_SERIAL_ERROR)
  1335. {
  1336. #if defined(DEBUG_FILE)
  1337. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Serial error while changing radio frequency/period.");
  1338. #endif
  1339. DSIThread_MutexLock(&stMutexCriticalSection);
  1340. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1341. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1342. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1343. }
  1344. else if (eReturn == RETURN_FAIL)
  1345. {
  1346. #if defined(DEBUG_FILE)
  1347. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Changing radio frequency and channel period failed.");
  1348. #endif
  1349. eANTFSRequest = ANTFS_REQUEST_NONE;
  1350. }
  1351. else if (eReturn == RETURN_PASS)
  1352. {
  1353. eANTFSRequest = ANTFS_REQUEST_NONE;
  1354. #if defined(DEBUG_FILE)
  1355. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Changed radio frequency and channel period.");
  1356. #endif
  1357. }
  1358. } // ANTFS_REQUEST_CHANGE_LINK
  1359. break;
  1360. case ANTFS_REQUEST_ERASE:
  1361. {
  1362. #if defined(DEBUG_FILE)
  1363. UCHAR aucString[256];
  1364. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::ANTFSThread(): Erase request for index: %hu.", stHostRequestParams.usFileIndex);
  1365. DSIDebug::ThreadWrite((char*) aucString);
  1366. #endif
  1367. eANTFSState = ANTFS_CLIENT_STATE_ERASING;
  1368. eResponse = ANTFS_CLIENT_RESPONSE_ERASE_REQUEST;
  1369. } // ANTFS_REQUEST_ERASE
  1370. break;
  1371. case ANTFS_REQUEST_ERASE_RESPONSE:
  1372. {
  1373. RETURN_STATUS eReturn;
  1374. #if defined(DEBUG_FILE)
  1375. UCHAR aucString[256];
  1376. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::ANTFSThread(): Erasing... Response: %u.", ucRequestResponse);
  1377. DSIDebug::ThreadWrite((char*)aucString);
  1378. #endif
  1379. eReturn = AttemptEraseResponse();
  1380. if (eReturn == RETURN_PASS)
  1381. {
  1382. #if defined(DEBUG_FILE)
  1383. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Erase complete.");
  1384. #endif
  1385. SwitchToTransport();
  1386. eResponse = ANTFS_CLIENT_RESPONSE_ERASE_PASS;
  1387. }
  1388. else if (eReturn == RETURN_REJECT)
  1389. {
  1390. #if defined(DEBUG_FILE)
  1391. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Erase request rejected.");
  1392. #endif
  1393. SwitchToTransport();
  1394. eResponse = ANTFS_CLIENT_RESPONSE_ERASE_REJECT;
  1395. }
  1396. else if (eReturn == RETURN_FAIL)
  1397. {
  1398. #if defined(DEBUG_FILE)
  1399. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Erase fail.");
  1400. #endif
  1401. SwitchToTransport();
  1402. eResponse = ANTFS_CLIENT_RESPONSE_ERASE_FAIL;
  1403. }
  1404. else if (eReturn == RETURN_SERIAL_ERROR)
  1405. {
  1406. #if defined(DEBUG_FILE)
  1407. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Erase serial error.");
  1408. #endif
  1409. DSIThread_MutexLock(&stMutexCriticalSection);
  1410. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1411. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1412. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1413. }
  1414. else
  1415. {
  1416. SwitchToTransport();
  1417. }
  1418. } // ANTFS_REQUEST_ERASE_RESPONSE
  1419. break;
  1420. case ANTFS_REQUEST_DOWNLOAD:
  1421. {
  1422. #if defined(DEBUG_FILE)
  1423. UCHAR aucString[256];
  1424. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::ANTFSThread(): Download request for index: %d.", stHostRequestParams.usFileIndex);
  1425. DSIDebug::ThreadWrite((char*) aucString);
  1426. #endif
  1427. usTransferDataFileIndex = stHostRequestParams.usFileIndex;
  1428. eANTFSState = ANTFS_CLIENT_STATE_DOWNLOADING_WAIT_FOR_DATA;
  1429. eResponse = ANTFS_CLIENT_RESPONSE_DOWNLOAD_REQUEST;
  1430. } // ANTFS_REQUEST_DOWNLOAD
  1431. break;
  1432. case ANTFS_REQUEST_DOWNLOAD_RESPONSE:
  1433. {
  1434. RETURN_STATUS eReturn;
  1435. #if defined(DEBUG_FILE)
  1436. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Downloading...");
  1437. #endif
  1438. eANTFSState = ANTFS_CLIENT_STATE_DOWNLOADING;
  1439. eReturn = AttemptDownloadResponse();
  1440. if (eReturn == RETURN_PASS)
  1441. {
  1442. #if defined(DEBUG_FILE)
  1443. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Download completed.");
  1444. #endif
  1445. SwitchToTransport();
  1446. eResponse = ANTFS_CLIENT_RESPONSE_DOWNLOAD_PASS;
  1447. }
  1448. else if (eReturn == RETURN_FAIL)
  1449. {
  1450. #if defined(DEBUG_FILE)
  1451. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Download failed.");
  1452. #endif
  1453. SwitchToTransport();
  1454. eResponse = ANTFS_CLIENT_RESPONSE_DOWNLOAD_FAIL;
  1455. }
  1456. else if (eReturn == RETURN_REJECT)
  1457. {
  1458. #if defined(DEBUG_FILE)
  1459. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Download request rejected.");
  1460. #endif
  1461. SwitchToTransport();
  1462. switch(ucRequestResponse)
  1463. {
  1464. case DOWNLOAD_RESPONSE_DOES_NOT_EXIST:
  1465. eResponse = ANTFS_CLIENT_RESPONSE_DOWNLOAD_INVALID_INDEX;
  1466. break;
  1467. case DOWNLOAD_RESPONSE_NOT_DOWNLOADABLE:
  1468. eResponse = ANTFS_CLIENT_RESPONSE_DOWNLOAD_FILE_NOT_READABLE;
  1469. break;
  1470. case DOWNLOAD_RESPONSE_NOT_READY:
  1471. eResponse = ANTFS_CLIENT_RESPONSE_DOWNLOAD_NOT_READY;
  1472. break;
  1473. case DOWNLOAD_RESPONSE_REQUEST_INVALID:
  1474. default:
  1475. eResponse = ANTFS_CLIENT_RESPONSE_DOWNLOAD_REJECT;
  1476. break;
  1477. }
  1478. }
  1479. else if (eReturn == RETURN_SERIAL_ERROR)
  1480. {
  1481. #if defined(DEBUG_FILE)
  1482. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Download serial error.");
  1483. #endif
  1484. DSIThread_MutexLock(&stMutexCriticalSection);
  1485. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1486. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1487. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1488. }
  1489. else
  1490. {
  1491. SwitchToTransport();
  1492. }
  1493. } // ANTFS_REQUEST_DOWNLOAD_RESPONSE
  1494. break;
  1495. case ANTFS_REQUEST_UPLOAD:
  1496. {
  1497. #if defined(DEBUG_FILE)
  1498. UCHAR aucString[256];
  1499. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::ANTFSThread(): Upload request for index: %d.", stHostRequestParams.usFileIndex);
  1500. DSIDebug::ThreadWrite((char*) aucString);
  1501. #endif
  1502. // TODO: Implement uploads - for now, all upload requests are rejected
  1503. } // ANTFS_REQUEST_UPLOAD
  1504. break;
  1505. case ANTFS_REQUEST_UPLOAD_RESPONSE:
  1506. {
  1507. RETURN_STATUS eReturn;
  1508. #if defined(DEBUG_FILE)
  1509. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Uploading...");
  1510. #endif
  1511. eANTFSState = ANTFS_CLIENT_STATE_UPLOADING;
  1512. eReturn = AttemptUploadResponse();
  1513. if (eReturn == RETURN_PASS)
  1514. {
  1515. #if defined(DEBUG_FILE)
  1516. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Upload completed.");
  1517. #endif
  1518. SwitchToTransport();
  1519. eResponse = ANTFS_CLIENT_RESPONSE_UPLOAD_PASS;
  1520. }
  1521. else if (eReturn == RETURN_FAIL)
  1522. {
  1523. #if defined(DEBUG_FILE)
  1524. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Upload failed.");
  1525. #endif
  1526. SwitchToTransport();
  1527. eResponse = ANTFS_CLIENT_RESPONSE_UPLOAD_FAIL;
  1528. }
  1529. else if (eReturn == RETURN_REJECT)
  1530. {
  1531. #if defined(DEBUG_FILE)
  1532. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Upload request rejected.");
  1533. #endif
  1534. SwitchToTransport();
  1535. switch(ucRequestResponse)
  1536. {
  1537. case UPLOAD_RESPONSE_DOES_NOT_EXIST:
  1538. eResponse = ANTFS_CLIENT_RESPONSE_UPLOAD_INVALID_INDEX;
  1539. break;
  1540. case UPLOAD_RESPONSE_NOT_WRITEABLE:
  1541. eResponse = ANTFS_CLIENT_RESPONSE_UPLOAD_FILE_NOT_WRITEABLE;
  1542. break;
  1543. case UPLOAD_RESPONSE_INSUFFICIENT_SPACE:
  1544. eResponse = ANTFS_CLIENT_RESPONSE_UPLOAD_INSUFFICIENT_SPACE;
  1545. break;
  1546. case UPLOAD_RESPONSE_REQUEST_INVALID:
  1547. default:
  1548. eResponse = ANTFS_CLIENT_RESPONSE_UPLOAD_REJECT;
  1549. break;
  1550. }
  1551. }
  1552. else if (eReturn == RETURN_SERIAL_ERROR)
  1553. {
  1554. #if defined(DEBUG_FILE)
  1555. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Upload serial error.");
  1556. #endif
  1557. DSIThread_MutexLock(&stMutexCriticalSection);
  1558. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1559. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1560. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1561. }
  1562. else
  1563. {
  1564. SwitchToTransport();
  1565. }
  1566. } // ANTFS_REQUEST_UPLOAD_RESPONSE
  1567. break;
  1568. default:
  1569. break;
  1570. }
  1571. //This is where to handle the internal requests, because they can happen asyncronously.
  1572. //We will also clear the request here.
  1573. DSIThread_MutexLock(&stMutexCriticalSection);
  1574. if (eResponse != ANTFS_CLIENT_RESPONSE_NONE)
  1575. AddResponse(eResponse);
  1576. if (eANTFSRequest == ANTFS_REQUEST_CONNECTION_LOST)
  1577. {
  1578. #if defined(DEBUG_FILE)
  1579. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Connection lost.");
  1580. #endif
  1581. eANTFSRequest = ANTFS_REQUEST_NONE;
  1582. if (eANTFSState >= ANTFS_CLIENT_STATE_CONNECTED)
  1583. {
  1584. SwitchToLink();
  1585. AddResponse(ANTFS_CLIENT_RESPONSE_CONNECTION_LOST);
  1586. }
  1587. else
  1588. {
  1589. #if defined(DEBUG_FILE)
  1590. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Connection lost - ignored.");
  1591. #endif
  1592. }
  1593. }
  1594. else if (eANTFSRequest == ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1595. {
  1596. #if defined(DEBUG_FILE)
  1597. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Serial error!");
  1598. #endif
  1599. HandleSerialError();
  1600. AddResponse(ANTFS_CLIENT_RESPONSE_SERIAL_FAIL);
  1601. }
  1602. else if (eANTFSRequest == ANTFS_REQUEST_SERIAL_ERROR_HANDLED)
  1603. {
  1604. #if defined(DEBUG_FILE)
  1605. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Serial error handled");
  1606. #endif
  1607. ResetClientState();
  1608. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  1609. eANTFSRequest = ANTFS_REQUEST_INIT;
  1610. }
  1611. else
  1612. {
  1613. eANTFSRequest = ANTFS_REQUEST_NONE; //Clear any other request
  1614. }
  1615. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1616. }
  1617. }
  1618. #if defined(DEBUG_FILE)
  1619. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): Exiting thread.");
  1620. #endif
  1621. eANTFSRequest = ANTFS_REQUEST_NONE;
  1622. DSIThread_MutexLock(&stMutexCriticalSection);
  1623. bANTFSThreadRunning = FALSE;
  1624. DSIThread_CondSignal(&stCondANTFSThreadExit);
  1625. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1626. #if defined(__cplusplus)
  1627. return;
  1628. #else
  1629. ExitThread(0);
  1630. #if defined(DEBUG_FILE)
  1631. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTFSThread(): C code reaching return statement unexpectedly.");
  1632. #endif
  1633. return; // Code should not be reached.
  1634. #endif
  1635. }
  1636. /////////////////////////////////////////////////////////////////////
  1637. // Returns: TRUE if the message is for the ANT-FS channel
  1638. BOOL ANTFSClientChannel::FilterANTMessages(ANT_MESSAGE* pstMessage_, UCHAR ucANTChannel_)
  1639. {
  1640. // Some messages do not include the channel number in the response, so
  1641. // they might get processed incorrectly
  1642. if(pstMessage_->ucMessageID == MESG_RESPONSE_EVENT_ID)
  1643. {
  1644. if(pstMessage_->aucData[MESG_EVENT_ID_OFFSET] == MESG_NETWORK_KEY_ID)
  1645. {
  1646. if(pstMessage_->aucData[MESG_CHANNEL_OFFSET] == ucNetworkNumber)
  1647. return TRUE; // this is the network we are using
  1648. }
  1649. else if(pstMessage_->aucData[MESG_EVENT_ID_OFFSET] == MESG_RADIO_TX_POWER_ID)
  1650. {
  1651. return TRUE; // configured by client if per channel settings not available
  1652. }
  1653. }
  1654. else if(pstMessage_->ucMessageID == MESG_STARTUP_MESG_ID)
  1655. {
  1656. return TRUE;
  1657. }
  1658. if(ucANTChannel_ == ucChannelNumber)
  1659. return TRUE;
  1660. return FALSE;
  1661. }
  1662. ///////////////////////////////////////////////////////////////////////
  1663. // Returns: TRUE if the message has been handled by ANT-FS client, FALSE otherwise
  1664. BOOL ANTFSClientChannel::ANTProtocolEventProcess(UCHAR ucChannel_, UCHAR ucMessageCode_)
  1665. {
  1666. if ((ucMessageCode_ == MESG_RESPONSE_EVENT_ID) && (ucChannel_ == ucChannelNumber))
  1667. {
  1668. #if defined(DEBUG_FILE)
  1669. UCHAR aucString[256];
  1670. SNPRINTF((char *) aucString, 256, "ANTFSClientChannel::ANTProtocolEventProcess(): MESG_RESPONSE_EVENT_ID - 0x%02X", aucResponseBuf[1]);
  1671. DSIDebug::ThreadWrite((char *) aucString);
  1672. #endif
  1673. if (aucResponseBuf[1] == MESG_BURST_DATA_ID)
  1674. {
  1675. if (aucResponseBuf[2] != RESPONSE_NO_ERROR)
  1676. {
  1677. #if defined(DEBUG_FILE)
  1678. UCHAR aucString1[256];
  1679. SNPRINTF((char *) aucString1, 256, "ANTFSClientChannel::ANTProtocolEventProcess(): Burst transfer error: 0x%02X.", aucResponseBuf[2]);
  1680. DSIDebug::ThreadWrite((char *) aucString1);
  1681. #endif
  1682. bTxError = TRUE;
  1683. }
  1684. }
  1685. }
  1686. //else if (ucMessageCode_ == MESG_SERIAL_ERROR_ID)
  1687. //{
  1688. // #if defined(DEBUG_FILE)
  1689. // {
  1690. // UCHAR aucString[256];
  1691. // SNPRINTF((char *) aucString, 256, "ANTFSClientChannel::ANTProtocolEventProcess(): Serial Error.");
  1692. // DSIDebug::ThreadWrite((char *) aucString);
  1693. // }
  1694. // #endif
  1695. // DSIThread_MutexLock(&stMutexCriticalSection);
  1696. // *pbCancel = TRUE;
  1697. // DSIThread_CondSignal(&stCondRxEvent);
  1698. // if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1699. // eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1700. // DSIThread_CondSignal(&stCondRequest);
  1701. // DSIThread_MutexUnlock(&stMutexCriticalSection);
  1702. //}
  1703. return TRUE;
  1704. }
  1705. ///////////////////////////////////////////////////////////////////////
  1706. // Returns: TRUE if the message has been handled by ANT-FS client, FALSE otherwise
  1707. BOOL ANTFSClientChannel::ANTChannelEventProcess(UCHAR ucChannel_, UCHAR ucMessageCode_)
  1708. {
  1709. // Check that we're getting a message from the correct channel.
  1710. if((ucChannel_ != ucChannelNumber) || ((aucRxBuf[0] & CHANNEL_NUMBER_MASK) != ucChannelNumber))
  1711. {
  1712. #if defined(DEBUG_FILE)
  1713. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): Message received on wrong channel.");
  1714. #endif
  1715. return FALSE; // message can get passed on to the application
  1716. }
  1717. switch (ucMessageCode_)
  1718. {
  1719. case EVENT_RX_BROADCAST:
  1720. break; // we're not going to process broadcasts or pass them to the application
  1721. case EVENT_RX_ACKNOWLEDGED:
  1722. aucRxBuf[0] |= SEQUENCE_LAST_MESSAGE; // mark it as being the last message and process as burst
  1723. case EVENT_RX_BURST_PACKET: // fall thru
  1724. if (!bRxError)
  1725. {
  1726. if ((aucRxBuf[0] & SEQUENCE_NUMBER_ROLLOVER) == 0) // Start of a burst.
  1727. {
  1728. // Check that this is an ANT-FS message
  1729. if(aucRxBuf[ANTFS_CONNECTION_OFFSET + 1] != ANTFS_COMMAND_ID)
  1730. {
  1731. #if defined(DEBUG_FILE)
  1732. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): Invalid ANT-FS message.");
  1733. #endif
  1734. bRxError = TRUE;
  1735. }
  1736. else
  1737. {
  1738. #if defined(DEBUG_FILE)
  1739. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): Burst Rx started");
  1740. #endif
  1741. }
  1742. ulPacketCount = 1;
  1743. bReceivedCommand = FALSE;
  1744. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  1745. {
  1746. if((aucRxBuf[ANTFS_COMMAND_OFFSET + 1] == ANTFS_DOWNLOAD_ID) ||
  1747. (aucRxBuf[ANTFS_COMMAND_OFFSET + 1] == ANTFS_UPLOAD_REQUEST_ID) ||
  1748. (aucRxBuf[ANTFS_COMMAND_OFFSET + 1] == ANTFS_UPLOAD_DATA_ID)) // These should always be longer than one packet
  1749. {
  1750. #if defined(DEBUG_FILE)
  1751. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): Premature end of burst transfer.");
  1752. #endif
  1753. bRxError = TRUE;
  1754. }
  1755. else
  1756. {
  1757. #if defined(DEBUG_FILE)
  1758. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): Reception of burst complete. (0)");
  1759. #endif
  1760. bReceivedCommand = TRUE;
  1761. }
  1762. }
  1763. #if defined(DEBUG_FILE)
  1764. else
  1765. {
  1766. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): Receiving burst... (0)");
  1767. }
  1768. #endif
  1769. }
  1770. else // Other packets in the burst
  1771. {
  1772. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  1773. {
  1774. #if defined(DEBUG_FILE)
  1775. char szString[256];
  1776. SNPRINTF(szString, 256, "ANTFSClientChannel::ANTChannelEventProcess(): Reception of burst complete. (%lu).", ulPacketCount);
  1777. DSIDebug::ThreadWrite(szString);
  1778. #endif
  1779. bReceivedCommand = TRUE;
  1780. }
  1781. else
  1782. {
  1783. #if defined(DEBUG_FILE)
  1784. char szString[256];
  1785. SNPRINTF(szString, 256, "ANTFSClientChannel::ANTChannelEventProcess(): Receiving burst... (%lu).", ulPacketCount);
  1786. DSIDebug::ThreadWrite(szString);
  1787. #endif
  1788. ulPacketCount++;
  1789. }
  1790. }
  1791. // Process burst content
  1792. if(eANTFSState == ANTFS_CLIENT_STATE_BEACONING)
  1793. {
  1794. DecodeLinkCommand(&aucRxBuf[1]);
  1795. }
  1796. else if((eANTFSState >= ANTFS_CLIENT_STATE_CONNECTED) && (eANTFSState < ANTFS_CLIENT_STATE_TRANSPORT))
  1797. {
  1798. DecodeAuthenticateCommand(aucRxBuf[0], &aucRxBuf[1]);
  1799. }
  1800. else if(eANTFSState == ANTFS_CLIENT_STATE_UPLOADING)
  1801. {
  1802. UploadInputData(aucRxBuf[0], &aucRxBuf[1]);
  1803. }
  1804. else if(eANTFSState >= ANTFS_CLIENT_STATE_TRANSPORT)
  1805. {
  1806. DecodeTransportCommand(aucRxBuf[0], &aucRxBuf[1]);
  1807. }
  1808. } // if(!bRxError)
  1809. if(aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  1810. {
  1811. DSIThread_MutexLock(&stMutexCriticalSection);
  1812. bReceivedBurst = TRUE;
  1813. bNewRxEvent = TRUE;
  1814. DSIThread_CondSignal(&stCondRxEvent);
  1815. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1816. }
  1817. break;
  1818. case EVENT_TRANSFER_RX_FAILED:
  1819. DSIThread_MutexLock(&stMutexCriticalSection);
  1820. if(eANTFSRequest != ANTFS_REQUEST_NONE)
  1821. {
  1822. bRxError = TRUE; // No need to signal an error, as no request is being processed
  1823. }
  1824. bReceivedBurst = FALSE;
  1825. bReceivedCommand = FALSE;
  1826. ucLinkCommandInProgress = ANTFS_CMD_NONE; // Clear command, to allow the host to retry
  1827. DSIThread_CondSignal(&stCondRxEvent);
  1828. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1829. #if defined(DEBUG_FILE)
  1830. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): EVENT_TRANSFER_RX_FAILED");
  1831. #endif
  1832. break;
  1833. case EVENT_TRANSFER_TX_COMPLETED:
  1834. #if defined(DEBUG_FILE)
  1835. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): EVENT_TRANSFER_TX_COMPLETED");
  1836. #endif
  1837. break;
  1838. case EVENT_TRANSFER_TX_FAILED:
  1839. #if defined(DEBUG_FILE)
  1840. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): EVENT_TRANSFER_TX_FAILED");
  1841. #endif
  1842. bTxError = TRUE;
  1843. break;
  1844. case EVENT_TRANSFER_TX_START:
  1845. #if defined(DEBUG_FILE)
  1846. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): EVENT_TRANSFER_TX_START");
  1847. #endif
  1848. break;
  1849. case EVENT_TX:
  1850. LoadBeacon();
  1851. pclANT->SendBroadcastData(ucChannelNumber, aucBeacon);
  1852. #if defined(DEBUG_FILE2)
  1853. UCHAR aucString[256];
  1854. SNPRINTF((char *) aucString, 256, "ANTChannelEventProcess(): Beacon [0x%02X][0x%02X][0x%02X][0x%02X][0x%02X][0x%02X][0x%02X][0x%02X]",
  1855. aucBeacon[0], aucBeacon[1], aucBeacon[2], aucBeacon[3], aucBeacon[4], aucBeacon[5], aucBeacon[6], aucBeacon[7]);
  1856. DSIDebug::ThreadWrite((char*) aucString);
  1857. #endif
  1858. break;
  1859. case EVENT_CHANNEL_CLOSED:
  1860. #if defined(DEBUG_FILE)
  1861. DSIDebug::ThreadWrite("ANTFSClientChannel::ANTChannelEventProcess(): EVENT_CHANNEL_CLOSED");
  1862. #endif
  1863. break;
  1864. default:
  1865. break;
  1866. }
  1867. return TRUE; // message has been handled, do not pass to application
  1868. }
  1869. ///////////////////////////////////////////////////////////////////////
  1870. void ANTFSClientChannel::SetDefaultBeacon(void)
  1871. {
  1872. stInitParams.ucBeaconFrequency = ANTFS_RF_FREQ;
  1873. stInitParams.ucLinkPeriod = BEACON_PERIOD_8_HZ;
  1874. stInitParams.ulSerialNumber = 0; // Use the USB device serial number by default
  1875. stInitParams.usBeaconDeviceType = 1;
  1876. stInitParams.usBeaconDeviceManufID = 1;
  1877. stInitParams.bPairingEnabled = TRUE;
  1878. stInitParams.bUploadEnabled = FALSE;
  1879. stInitParams.bDataAvailable = FALSE;
  1880. stInitParams.ucAuthType = AUTH_COMMAND_PAIR;
  1881. stInitParams.ucBeaconTimeout = (UCHAR) (COMMAND_TIMEOUT/1000); // In seconds
  1882. stInitParams.ucPairingTimeout = (UCHAR) (AUTH_TIMEOUT/1000); // In seconds
  1883. }
  1884. ///////////////////////////////////////////////////////////////////////
  1885. void ANTFSClientChannel::ResetClientState(void)
  1886. {
  1887. // Clear all state variables, while keeping the configuration
  1888. *pbCancel = FALSE;
  1889. ulPacketCount = 0;
  1890. bTxError = FALSE;
  1891. bRxError = FALSE;
  1892. bReceivedBurst = FALSE;
  1893. bReceivedCommand = FALSE;
  1894. bNewRxEvent = FALSE;
  1895. memset(aucResponseBuf, 0, sizeof(aucResponseBuf));
  1896. memset(aucRxBuf, 0, sizeof(aucRxBuf));
  1897. ucPairingTimeout = MAX_UCHAR;
  1898. bTimeoutEvent = FALSE;
  1899. bReturnToBroadcast = FALSE;
  1900. ulHostSerialNumber = 0;
  1901. stHostFriendlyName.bNameSet = FALSE;
  1902. stHostFriendlyName.ucIndex = 0;
  1903. stHostFriendlyName.ucSize = 0;
  1904. memset(stHostFriendlyName.acFriendlyName, 0, FRIENDLY_NAME_MAX_LENGTH);
  1905. ucPassKeyIndex = 0;
  1906. ucAuthCommandType = MAX_UCHAR;
  1907. bAcceptRequest = FALSE;
  1908. memset(&stHostRequestParams, 0, sizeof(stHostRequestParams));
  1909. ucRequestResponse = MAX_UCHAR;
  1910. usTransferDataFileIndex = 0;
  1911. ulTransferFileSize = 0;
  1912. ulTransferBurstIndex = 0;
  1913. ulTransferBytesRemaining = 0;
  1914. ulTransferMaxIndex = 0;
  1915. ulTransferBlockSize = 0;
  1916. ulTransferBlockOffset = 0;
  1917. usTransferCrc = 0;
  1918. ulTransferBufferSize = 0;
  1919. ulDownloadProgress = 0;
  1920. pucDownloadData = (UCHAR*) NULL;
  1921. if(eANTFSState == ANTFS_CLIENT_STATE_OFF)
  1922. {
  1923. pucTransferBufferDynamic = (UCHAR*) NULL;
  1924. }
  1925. else
  1926. {
  1927. // Deallocate dynamically allocated memory if we had an error during a transfer
  1928. if (pucTransferBufferDynamic)
  1929. {
  1930. delete[] pucTransferBufferDynamic;
  1931. pucTransferBufferDynamic = (UCHAR*)NULL;
  1932. }
  1933. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  1934. }
  1935. eANTFSRequest = ANTFS_REQUEST_NONE;
  1936. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  1937. }
  1938. ///////////////////////////////////////////////////////////////////////
  1939. BOOL ANTFSClientChannel::ReInitDevice(void)
  1940. {
  1941. if (eANTFSState != ANTFS_CLIENT_STATE_OFF)
  1942. this->Close();
  1943. bKillThread = FALSE;
  1944. #if defined(DEBUG_FILE)
  1945. DSIDebug::ThreadWrite("ANTFSClientChannel::ReInitDevice(): Initializing");
  1946. #endif
  1947. if (hANTFSThread == NULL)
  1948. {
  1949. hANTFSThread = DSIThread_CreateThread(&ANTFSClientChannel::ANTFSThreadStart, this);
  1950. if (hANTFSThread == NULL)
  1951. return FALSE;
  1952. }
  1953. if (!bTimerRunning)
  1954. {
  1955. if (DSIThread_MutexInit(&stMutexPairingTimeout) != DSI_THREAD_ENONE)
  1956. {
  1957. return FALSE;
  1958. }
  1959. pclTimer = new DSITimer(&ANTFSClientChannel::TimerStart, this, 1000, TRUE);
  1960. if (pclTimer->NoError() == FALSE)
  1961. {
  1962. DSIThread_MutexDestroy(&stMutexPairingTimeout);
  1963. return FALSE;
  1964. }
  1965. bTimerRunning = TRUE;
  1966. }
  1967. DSIThread_MutexLock(&stMutexResponseQueue);
  1968. clResponseQueue.Clear(); // Should this be done in ResetClientState instead?
  1969. DSIThread_MutexUnlock(&stMutexResponseQueue);
  1970. DSIThread_MutexLock(&stMutexCriticalSection);
  1971. eANTFSRequest = ANTFS_REQUEST_INIT;
  1972. DSIThread_CondSignal(&stCondRequest);
  1973. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1974. return TRUE;
  1975. }
  1976. ///////////////////////////////////////////////////////////////////////
  1977. // Frequency: 1 Hz
  1978. ///////////////////////////////////////////////////////////////////////
  1979. DSI_THREAD_RETURN ANTFSClientChannel::TimerStart(void *pvParameter_)
  1980. {
  1981. #if defined(DEBUG_FILE)
  1982. DSIDebug::ThreadInit("ANTFSClient_Timer");
  1983. #endif
  1984. ((ANTFSClientChannel *)pvParameter_)->TimerCallback();
  1985. return 0;
  1986. }
  1987. ///////////////////////////////////////////////////////////////////////
  1988. void ANTFSClientChannel::TimerCallback(void)
  1989. {
  1990. #if defined(DEBUG_FILE)
  1991. DSIDebug::ThreadWrite("ANTFSClientChannel::TimerCallback(): Entering critical section.");
  1992. #endif
  1993. DSIThread_MutexLock(&stMutexPairingTimeout);
  1994. if(eANTFSState == ANTFS_CLIENT_STATE_PAIRING_WAIT_FOR_RESPONSE)
  1995. {
  1996. if((ucPairingTimeout > 0) && (ucPairingTimeout != CMD_TIMEOUT_DISABLED))
  1997. {
  1998. ucPairingTimeout--;
  1999. }
  2000. if(ucPairingTimeout == 0) // Timeout
  2001. {
  2002. #if defined(DEBUG_FILE)
  2003. DSIDebug::ThreadWrite("Timeout event.");
  2004. ucPairingTimeout = MAX_UCHAR;
  2005. bTimeoutEvent = TRUE;
  2006. #endif
  2007. }
  2008. }
  2009. DSIThread_MutexUnlock(&stMutexPairingTimeout);
  2010. #if defined(DEBUG_FILE)
  2011. DSIDebug::ThreadWrite("ANTFSClientChannel::TimerCallback(): Left critical section.");
  2012. #endif
  2013. DSIThread_MutexLock(&stMutexCriticalSection);
  2014. if((bTimeoutEvent == TRUE) && (eANTFSState == ANTFS_CLIENT_STATE_PAIRING_WAIT_FOR_RESPONSE))
  2015. {
  2016. // Reject the authentication request
  2017. eANTFSRequest = ANTFS_REQUEST_AUTHENTICATE;
  2018. DSIThread_CondSignal(&stCondRequest);
  2019. }
  2020. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2021. }
  2022. ///////////////////////////////////////////////////////////////////////
  2023. void ANTFSClientChannel::HandleSerialError(void)
  2024. {
  2025. // We ended up here because we did not receive the expected response to a serial message
  2026. // Most likely, ANT was in the wrong state, so attempt to close the channel.
  2027. // No errors raised from here, as we do not know what state we are in.
  2028. UCHAR ucChannelStatus = 0;
  2029. if(pclANT->CloseChannel(ucChannelNumber, ANT_CLOSE_TIMEOUT) == FALSE)
  2030. {
  2031. #if defined(DEBUG_FILE)
  2032. DSIDebug::ThreadWrite("ANTFSClientChannel::HandleSerialError(): Failed to close channel.");
  2033. #endif
  2034. }
  2035. if(pclANT->UnAssignChannel(ucChannelNumber, MESSAGE_TIMEOUT) == FALSE)
  2036. {
  2037. #if defined(DEBUG_FILE)
  2038. DSIDebug::ThreadWrite("ANTFSClientChannel::HandleSerialError(): Failed to unassign channel.");
  2039. #endif
  2040. }
  2041. if(pclANT->GetChannelStatus(ucChannelNumber, &ucChannelStatus, MESSAGE_TIMEOUT) == FALSE)
  2042. {
  2043. #if defined(DEBUG_FILE)
  2044. DSIDebug::ThreadWrite("ANTFSClientChannel::HandleSerialError(): Failed ANT_GetChannelStatus().");
  2045. #endif
  2046. }
  2047. #if defined(DEBUG_FILE)
  2048. else if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) != STATUS_UNASSIGNED_CHANNEL)
  2049. {
  2050. char szString[256];
  2051. SNPRINTF(szString, 256, "ANTFSClientChannel::HandleSerialError(): Channel state... 0x%x.", ucChannelStatus);
  2052. DSIDebug::ThreadWrite(szString);
  2053. }
  2054. #endif
  2055. DSIThread_MutexLock(&stMutexCriticalSection);
  2056. eANTFSRequest = ANTFS_REQUEST_SERIAL_ERROR_HANDLED; // Reset state machine
  2057. DSIThread_CondSignal(&stCondRequest);
  2058. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2059. }
  2060. ///////////////////////////////////////////////////////////////////////
  2061. void ANTFSClientChannel::AddResponse(ANTFS_CLIENT_RESPONSE eResponse_)
  2062. {
  2063. DSIThread_MutexLock(&stMutexResponseQueue);
  2064. clResponseQueue.AddResponse(eResponse_);
  2065. DSIThread_CondSignal(&stCondWaitForResponse);
  2066. DSIThread_MutexUnlock(&stMutexResponseQueue);
  2067. }
  2068. ///////////////////////////////////////////////////////////////////////
  2069. void ANTFSClientChannel::LoadBeacon(void)
  2070. {
  2071. UCHAR ucBeaconStatus2;
  2072. // Status Byte 2
  2073. if (ucLinkCommandInProgress == ANTFS_CMD_NONE)
  2074. {
  2075. if(eANTFSState < ANTFS_CLIENT_STATE_CONNECTED)
  2076. {
  2077. ucBeaconStatus2 = REMOTE_DEVICE_STATE_LINK ;
  2078. }
  2079. else if(eANTFSState < ANTFS_CLIENT_STATE_TRANSPORT)
  2080. {
  2081. ucBeaconStatus2 = REMOTE_DEVICE_STATE_AUTH;
  2082. }
  2083. else
  2084. {
  2085. ucBeaconStatus2 = REMOTE_DEVICE_STATE_TRANS;
  2086. }
  2087. }
  2088. else
  2089. {
  2090. ucBeaconStatus2 = REMOTE_DEVICE_STATE_BUSY;
  2091. }
  2092. aucBeacon[ANTFS_CONNECTION_OFFSET] = ANTFS_BEACON_ID; // ANT-FS Beacon ID
  2093. aucBeacon[STATUS1_OFFSET] = ucActiveBeaconStatus1; // Status Byte 1
  2094. aucBeacon[STATUS2_OFFSET] = ucBeaconStatus2; // Status Byte 2
  2095. aucBeacon[AUTHENTICATION_TYPE_OFFSET] = stInitParams.ucAuthType; // Authentication Type
  2096. if (eANTFSState >= ANTFS_CLIENT_STATE_CONNECTED) // AUTH & TRANS
  2097. {
  2098. // Host serial number
  2099. Convert_ULONG_To_Bytes(ulHostSerialNumber,
  2100. &aucBeacon[AUTH_HOST_SERIAL_NUMBER_OFFSET+3],
  2101. &aucBeacon[AUTH_HOST_SERIAL_NUMBER_OFFSET+2],
  2102. &aucBeacon[AUTH_HOST_SERIAL_NUMBER_OFFSET+1],
  2103. &aucBeacon[AUTH_HOST_SERIAL_NUMBER_OFFSET]);
  2104. }
  2105. else
  2106. {
  2107. // Device descriptor
  2108. Convert_USHORT_To_Bytes(stInitParams.usBeaconDeviceType,
  2109. &aucBeacon[DEVICE_TYPE_OFFSET_HIGH],
  2110. &aucBeacon[DEVICE_TYPE_OFFSET_LOW]); // Device type
  2111. Convert_USHORT_To_Bytes(stInitParams.usBeaconDeviceManufID,
  2112. &aucBeacon[MANUFACTURER_ID_OFFSET_HIGH],
  2113. &aucBeacon[MANUFACTURER_ID_OFFSET_LOW]); // Manufacturer
  2114. }
  2115. }
  2116. ///////////////////////////////////////////////////////////////////////
  2117. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::AttemptOpenBeacon(void)
  2118. {
  2119. UCHAR ucChannelStatus;
  2120. if(eANTFSState != ANTFS_CLIENT_STATE_OPENING)
  2121. {
  2122. #if defined(DEBUG_FILE)
  2123. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Device is not in correct state.");
  2124. #endif
  2125. return RETURN_FAIL;
  2126. }
  2127. if(pclANT->GetChannelStatus(ucChannelNumber, &ucChannelStatus, MESSAGE_TIMEOUT) == FALSE)
  2128. {
  2129. #if defined(DEBUG_FILE)
  2130. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_GetChannelStatus().");
  2131. #endif
  2132. return RETURN_SERIAL_ERROR;
  2133. }
  2134. if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_TRACKING_CHANNEL)
  2135. {
  2136. #if defined(DEBUG_FILE)
  2137. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): ANT-FS Broadcast mode, skipping channel initialization");
  2138. #endif
  2139. return RETURN_PASS;
  2140. }
  2141. #if defined(DEBUG_FILE)
  2142. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Full Init Begin");
  2143. #endif
  2144. if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_ASSIGNED_CHANNEL)
  2145. {
  2146. if(pclANT->UnAssignChannel(ucChannelNumber, MESSAGE_TIMEOUT) == FALSE)
  2147. {
  2148. #if defined(DEBUG_FILE)
  2149. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_UnAssignChannel().");
  2150. #endif
  2151. return RETURN_SERIAL_ERROR;
  2152. }
  2153. }
  2154. if (pclANT->SetNetworkKey(ucNetworkNumber, (UCHAR *) aucTheNetworkkey, MESSAGE_TIMEOUT) == FALSE)
  2155. {
  2156. #if defined(DEBUG_FILE)
  2157. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_SetNetworkKey().");
  2158. #endif
  2159. return RETURN_SERIAL_ERROR;
  2160. }
  2161. if (pclANT->AssignChannel(ucChannelNumber, 0x10, ucNetworkNumber, MESSAGE_TIMEOUT) == FALSE)
  2162. {
  2163. #if defined(DEBUG_FILE)
  2164. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_AssignChannel().");
  2165. #endif
  2166. return RETURN_SERIAL_ERROR;
  2167. }
  2168. if (pclANT->SetChannelPeriod(ucChannelNumber, usTheMessagePeriod, MESSAGE_TIMEOUT) == FALSE)
  2169. {
  2170. #if defined(DEBUG_FILE)
  2171. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_SetChannelPeriod().");
  2172. #endif
  2173. return RETURN_SERIAL_ERROR;
  2174. }
  2175. if (pclANT->SetChannelRFFrequency(ucChannelNumber, ucActiveBeaconFrequency, MESSAGE_TIMEOUT) == FALSE)
  2176. {
  2177. #if defined(DEBUG_FILE)
  2178. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_SetChannelRFFreq().");
  2179. #endif
  2180. return RETURN_SERIAL_ERROR;
  2181. }
  2182. if(bCustomTxPower)
  2183. {
  2184. if(pclANT->SetChannelTransmitPower(ucChannelNumber, ucLinkTxPower, MESSAGE_TIMEOUT) == FALSE)
  2185. {
  2186. #if defined(DEBUG_FILE)
  2187. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_SetChannelTransmitPower(), setting power level for all channels.");
  2188. #endif
  2189. if(pclANT->SetAllChannelsTransmitPower(ucLinkTxPower, MESSAGE_TIMEOUT) == FALSE)
  2190. {
  2191. #if defined(DEBUG_FILE)
  2192. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_SetAllChannelsTransmitPower().");
  2193. #endif
  2194. return RETURN_SERIAL_ERROR;
  2195. }
  2196. }
  2197. }
  2198. if (pclANT->SetChannelID(ucChannelNumber, usRadioChannelID, ucTheDeviceType, ucTheTransmissionType, MESSAGE_TIMEOUT) == FALSE)
  2199. {
  2200. #if defined(DEBUG_FILE)
  2201. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_SetChannelId().");
  2202. #endif
  2203. return RETURN_SERIAL_ERROR;
  2204. }
  2205. #if defined(DEBUG_FILE)
  2206. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Opening channel...");
  2207. #endif
  2208. if (pclANT->OpenChannel(ucChannelNumber, MESSAGE_TIMEOUT) == FALSE)
  2209. {
  2210. #if defined(DEBUG_FILE)
  2211. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptOpenBeacon(): Failed ANT_OpenChannel().");
  2212. #endif
  2213. return RETURN_SERIAL_ERROR;
  2214. }
  2215. return RETURN_PASS;
  2216. }
  2217. ///////////////////////////////////////////////////////////////////////
  2218. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::AttemptCloseBeacon(void)
  2219. {
  2220. if(bReturnToBroadcast == FALSE)
  2221. {
  2222. if(pclANT->CloseChannel(ucChannelNumber, ANT_CLOSE_TIMEOUT) == FALSE)
  2223. {
  2224. #if defined(DEBUG_FILE)
  2225. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptCloseBeacon(): Failed to close beacon channel");
  2226. #endif
  2227. return RETURN_SERIAL_ERROR;
  2228. }
  2229. }
  2230. bReturnToBroadcast = FALSE;
  2231. eANTFSState = ANTFS_CLIENT_STATE_IDLE;
  2232. eANTFSRequest = ANTFS_REQUEST_NONE;
  2233. return RETURN_PASS;
  2234. }
  2235. ///////////////////////////////////////////////////////////////////////
  2236. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::AttemptAuthenticateResponse(void)
  2237. {
  2238. RETURN_STATUS eReturn = RETURN_FAIL;
  2239. UCHAR aucTxAuth[8 + TX_PASSWORD_MAX_LENGTH]; // Response + auth string
  2240. UCHAR ucTxPasswordLength = 0;
  2241. UCHAR ucTxRetries;
  2242. ANTFS_DATA stHeader = {8, aucBeacon};
  2243. ANTFS_DATA stData;
  2244. ANTFS_DATA stFooter = {0, NULL};
  2245. ANTFRAMER_RETURN eTxComplete;
  2246. if((eANTFSState < ANTFS_CLIENT_STATE_CONNECTED) || (eANTFSState >= ANTFS_CLIENT_STATE_TRANSPORT))
  2247. {
  2248. #if defined(DEBUG_FILE)
  2249. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptAuthenticateResponse(): Device is not in correct state.");
  2250. #endif
  2251. return RETURN_FAIL;
  2252. }
  2253. memset(aucTxAuth,0x00,sizeof(aucTxAuth));
  2254. LoadBeacon();
  2255. aucTxAuth[ANTFS_CONNECTION_OFFSET] = ANTFS_RESPONSE_ID;
  2256. aucTxAuth[ANTFS_RESPONSE_OFFSET] = ANTFS_RESPONSE_AUTH_ID ;
  2257. if(ucAuthCommandType == AUTH_COMMAND_REQ_SERIAL_NUM)
  2258. {
  2259. aucTxAuth[AUTH_RESPONSE_OFFSET] = AUTH_RESPONSE_NA;
  2260. if(ucFriendlyNameSize != 0)
  2261. {
  2262. ucTxPasswordLength = ucFriendlyNameSize;
  2263. memcpy(&aucTxAuth[8], aucFriendlyName, ucTxPasswordLength);
  2264. }
  2265. eReturn = RETURN_NA;
  2266. }
  2267. else if(bAcceptRequest == TRUE)
  2268. {
  2269. aucTxAuth[AUTH_RESPONSE_OFFSET] = AUTH_RESPONSE_ACCEPT;
  2270. if((ucAuthCommandType == AUTH_COMMAND_PAIR) && (ucPassKeySize != 0))
  2271. {
  2272. ucTxPasswordLength = ucPassKeySize;
  2273. memcpy(&aucTxAuth[8], aucPassKey, ucTxPasswordLength);
  2274. }
  2275. eReturn = RETURN_PASS;
  2276. }
  2277. else
  2278. {
  2279. aucTxAuth[AUTH_RESPONSE_OFFSET] = AUTH_RESPONSE_REJECT;
  2280. eReturn = RETURN_REJECT;
  2281. }
  2282. aucTxAuth[AUTH_FRIENDLY_NAME_LENGTH_OFFSET] = ucTxPasswordLength;
  2283. Convert_ULONG_To_Bytes(stInitParams.ulSerialNumber,
  2284. &aucTxAuth[AUTH_REMOTE_SERIAL_NUMBER_OFFSET + 3],
  2285. &aucTxAuth[AUTH_REMOTE_SERIAL_NUMBER_OFFSET + 2],
  2286. &aucTxAuth[AUTH_REMOTE_SERIAL_NUMBER_OFFSET + 1],
  2287. &aucTxAuth[AUTH_REMOTE_SERIAL_NUMBER_OFFSET]);
  2288. stData.ulSize = ucTxPasswordLength + 8;
  2289. stData.pucData = aucTxAuth;
  2290. ucTxRetries = 8;
  2291. do{
  2292. eTxComplete = pclANT->SendANTFSClientTransfer(ucChannelNumber, &stHeader, &stData, &stFooter, ACKNOWLEDGED_TIMEOUT, NULL);
  2293. #if defined(DEBUG_FILE)
  2294. if (eTxComplete == ANTFRAMER_FAIL)
  2295. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptAuthenticateResponse(): Tx error.");
  2296. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2297. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptAuthenticateResponse(): Tx timeout.");
  2298. #endif
  2299. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  2300. if (eTxComplete != ANTFRAMER_PASS)
  2301. {
  2302. #if defined(DEBUG_FILE)
  2303. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptAuthenticateResponse(): Tx failed.");
  2304. #endif
  2305. return RETURN_FAIL;
  2306. }
  2307. return eReturn;
  2308. }
  2309. ///////////////////////////////////////////////////////////////////////
  2310. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::AttemptEraseResponse(void)
  2311. {
  2312. RETURN_STATUS eReturn = RETURN_FAIL;
  2313. UCHAR aucBuffer[8];
  2314. ANTFS_DATA stHeader = {8, aucBeacon};
  2315. ANTFS_DATA stData = {8, aucBuffer};
  2316. ANTFS_DATA stFooter = {0, NULL};
  2317. ANTFRAMER_RETURN eTxComplete;
  2318. if((eANTFSState < ANTFS_CLIENT_STATE_TRANSPORT))
  2319. {
  2320. #if defined(DEBUG_FILE)
  2321. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptEraseResponse(): Device is not in correct state.");
  2322. #endif
  2323. return RETURN_FAIL;
  2324. }
  2325. LoadBeacon();
  2326. memset(aucBuffer,0x00,sizeof(aucBuffer));
  2327. aucBuffer[ANTFS_CONNECTION_OFFSET] = ANTFS_RESPONSE_ID;
  2328. aucBuffer[ANTFS_RESPONSE_OFFSET] = ANTFS_RESPONSE_ERASE_ID;
  2329. aucBuffer[ERASE_RESPONSE_OFFSET] = ucRequestResponse;
  2330. if(ucRequestResponse == ERASE_RESPONSE_OK)
  2331. {
  2332. eReturn = RETURN_PASS;
  2333. }
  2334. else
  2335. {
  2336. eReturn = RETURN_REJECT;
  2337. }
  2338. eTxComplete = pclANT->SendANTFSClientTransfer(ucChannelNumber, &stHeader, &stData, &stFooter, ACKNOWLEDGED_TIMEOUT, NULL);
  2339. if (eTxComplete != ANTFRAMER_PASS)
  2340. {
  2341. #if defined(DEBUG_FILE)
  2342. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptEraseResponse(): Tx failed.");
  2343. if (eTxComplete == ANTFRAMER_FAIL)
  2344. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptEraseResponse(): Tx error.");
  2345. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2346. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptEraseResponse(): Tx timeout.");
  2347. #endif
  2348. return RETURN_FAIL;
  2349. }
  2350. return eReturn;
  2351. }
  2352. ///////////////////////////////////////////////////////////////////////
  2353. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::AttemptDownloadResponse()
  2354. {
  2355. RETURN_STATUS eReturn = RETURN_FAIL;
  2356. UCHAR aucDownloadHeader[24]; // Beacon + Response
  2357. UCHAR aucDownloadFooter[8]; // CRC footer
  2358. ANTFS_DATA stHeader = {24, aucDownloadHeader};
  2359. ANTFS_DATA stFooter = {8, aucDownloadFooter};
  2360. ANTFS_DATA stData;
  2361. USHORT usSavedCrc = 0;
  2362. ULONG ulSavedOffset = 0;
  2363. ANTFRAMER_RETURN eTxComplete;
  2364. UCHAR ucNoRxTicks;
  2365. BOOL bDone = FALSE;
  2366. BOOL bStatus = FALSE;
  2367. if((eANTFSState < ANTFS_CLIENT_STATE_TRANSPORT))
  2368. {
  2369. #if defined(DEBUG_FILE)
  2370. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Device is not in correct state.");
  2371. #endif
  2372. return RETURN_FAIL;
  2373. }
  2374. bReceivedCommand = FALSE;
  2375. bReceivedBurst = FALSE;
  2376. bRxError = FALSE;
  2377. do
  2378. {
  2379. if(ucRequestResponse == DOWNLOAD_RESPONSE_OK)
  2380. {
  2381. // If this is not an initial request, verify the CRC
  2382. if(stHostRequestParams.bInitialRequest == FALSE && stHostRequestParams.usCRCSeed != 0)
  2383. {
  2384. if(ulTransferBurstIndex < ulSavedOffset)
  2385. {
  2386. usTransferCrc = CRC_Calc16(pucDownloadData, ulTransferBurstIndex);
  2387. }
  2388. else
  2389. {
  2390. usTransferCrc = CRC_UpdateCRC16(usSavedCrc, &pucDownloadData[ulSavedOffset], ulTransferBurstIndex - ulSavedOffset);
  2391. }
  2392. if(usTransferCrc != stHostRequestParams.usCRCSeed)
  2393. {
  2394. #if defined(DEBUG_FILE)
  2395. UCHAR aucString[256];
  2396. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::AttemptDownloadResponse(): CRC Check Failed - Expected 0x%04X, Got 0x%04X", usTransferCrc, stHostRequestParams.usCRCSeed);
  2397. DSIDebug::ThreadWrite((char*) aucString);
  2398. #endif
  2399. ucRequestResponse = DOWNLOAD_RESPONSE_CRC_FAILED;
  2400. }
  2401. }
  2402. else // Use seed provided by host
  2403. {
  2404. usTransferCrc = stHostRequestParams.usCRCSeed;
  2405. }
  2406. }
  2407. if ((!bReceivedBurst) &&(!bReceivedCommand)) //prevents us from sending requests until the Rx bursts have stopped and been cleared.
  2408. {
  2409. bRxError = FALSE;
  2410. // Send out the download response
  2411. LoadBeacon();
  2412. memset(aucDownloadHeader, 0, sizeof(aucDownloadHeader));
  2413. memcpy(aucDownloadHeader, aucBeacon, 8);
  2414. aucDownloadHeader[ANTFS_CONNECTION_OFFSET + 8] = ANTFS_RESPONSE_ID;
  2415. aucDownloadHeader[ANTFS_RESPONSE_OFFSET + 8] = ANTFS_RESPONSE_DOWNLOAD_ID;
  2416. aucDownloadHeader[DOWNLOAD_RESPONSE_OFFSET + 8] = ucRequestResponse;
  2417. Convert_ULONG_To_Bytes(ulTransferBytesRemaining,
  2418. &aucDownloadHeader[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 3 + 8],
  2419. &aucDownloadHeader[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 2 + 8],
  2420. &aucDownloadHeader[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 1 + 8],
  2421. &aucDownloadHeader[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 8]);
  2422. Convert_ULONG_To_Bytes(ulTransferBurstIndex,
  2423. &aucDownloadHeader[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 3 + 16],
  2424. &aucDownloadHeader[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 2 + 16],
  2425. &aucDownloadHeader[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 1 + 16],
  2426. &aucDownloadHeader[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 16]);
  2427. Convert_ULONG_To_Bytes(ulTransferFileSize,
  2428. &aucDownloadHeader[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 3 + 16],
  2429. &aucDownloadHeader[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 2 + 16],
  2430. &aucDownloadHeader[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 1 + 16],
  2431. &aucDownloadHeader[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 16]);
  2432. if(ucRequestResponse == DOWNLOAD_RESPONSE_OK)
  2433. {
  2434. ULONG ulDownloadTimeout;
  2435. if(ulTransferBytesRemaining > 0)
  2436. {
  2437. usSavedCrc = usTransferCrc;
  2438. ulSavedOffset = ulTransferBurstIndex;
  2439. usTransferCrc = CRC_UpdateCRC16(usTransferCrc, &pucDownloadData[ulTransferBurstIndex], ulTransferBytesRemaining);
  2440. }
  2441. memset(aucDownloadFooter, 0, sizeof(aucDownloadFooter));
  2442. Convert_USHORT_To_Bytes(usTransferCrc,
  2443. &aucDownloadFooter[DOWNLOAD_RESPONSE_CRC_OFFSET + 1],
  2444. &aucDownloadFooter[DOWNLOAD_RESPONSE_CRC_OFFSET]);
  2445. stData.pucData = &pucDownloadData[ulTransferBurstIndex];
  2446. stData.ulSize = ulTransferBytesRemaining;
  2447. // Figure out our timeout value from the size of the transfer
  2448. ulDownloadTimeout = BROADCAST_TIMEOUT + (ulTransferBytesRemaining * 2);
  2449. if(ulTransferBytesRemaining > ((MAX_ULONG / BROADCAST_TIMEOUT)/2))
  2450. ulDownloadTimeout = MAX_ULONG - 1;
  2451. eTxComplete = pclANT->SendANTFSClientTransfer(ucChannelNumber, &stHeader, &stFooter, &stData, ulDownloadTimeout, &ulDownloadProgress);
  2452. eReturn = RETURN_PASS;
  2453. }
  2454. else
  2455. {
  2456. eTxComplete = pclANT->SendTransfer(ucChannelNumber, aucDownloadHeader, 24, ACKNOWLEDGED_TIMEOUT);
  2457. eReturn = RETURN_REJECT;
  2458. }
  2459. if (eTxComplete == ANTFRAMER_PASS)
  2460. return eReturn; // Response transmitted successfully, we are done
  2461. #if defined(DEBUG_FILE)
  2462. if (eTxComplete == ANTFRAMER_FAIL)
  2463. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Tx error sending download response.");
  2464. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2465. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Tx timeout sending download response.");
  2466. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Waiting for host to attempt retry...");
  2467. #endif
  2468. }
  2469. // Do not need to clear bReceivedBurst here because it will be done if the transfer fails or we timeout
  2470. // Now we wait for a retry from the host
  2471. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  2472. LoadBeacon(); // Reload beacon, to let host know we are ready for retry
  2473. pclANT->SendBroadcastData(ucChannelNumber, aucBeacon);
  2474. ucNoRxTicks = 5;
  2475. bStatus = FALSE;
  2476. while (bStatus == FALSE)
  2477. {
  2478. //Wait for an rxEvent before starting to check the data
  2479. //Since this event is fired for many circumstances we manage all the error checking below and
  2480. //just use this for the wait functionality.
  2481. DSIThread_MutexLock(&stMutexCriticalSection);
  2482. bNewRxEvent = FALSE;
  2483. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  2484. {
  2485. DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, MESSAGE_TIMEOUT);
  2486. }
  2487. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2488. if (*pbCancel == TRUE)
  2489. {
  2490. #if defined(DEBUG_FILE)
  2491. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Stopped.");
  2492. #endif
  2493. return RETURN_STOP;
  2494. }
  2495. if (!bReceivedBurst)
  2496. {
  2497. #if defined(DEBUG_FILE)
  2498. DSIDebug::ThreadWrite("-->Waiting for download request...");
  2499. #endif
  2500. ucNoRxTicks--;
  2501. if(ucNoRxTicks == 0)
  2502. {
  2503. #if defined(DEBUG_FILE)
  2504. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Timeout while waiting for host request retry");
  2505. #endif
  2506. return RETURN_FAIL; // Timeout
  2507. }
  2508. }
  2509. if (bReceivedCommand) //If a command has been received, process it.
  2510. {
  2511. bReceivedCommand = FALSE; //Clear these for any potential retries
  2512. bReceivedBurst = FALSE; //Clearing these for retries
  2513. // Process request
  2514. if(ucLinkCommandInProgress == ANTFS_DOWNLOAD_ID)
  2515. {
  2516. #if defined(DEBUG_FILE)
  2517. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Resuming download...");
  2518. #endif
  2519. bStatus = TRUE;
  2520. if(stHostRequestParams.usFileIndex != usTransferDataFileIndex)
  2521. {
  2522. #if defined(DEBUG_FILE)
  2523. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Invalid index requested.");
  2524. #endif
  2525. ucRequestResponse = DOWNLOAD_RESPONSE_REQUEST_INVALID;
  2526. }
  2527. if(ucRequestResponse == DOWNLOAD_RESPONSE_OK)
  2528. {
  2529. DSIThread_MutexLock(&stMutexCriticalSection);
  2530. if(stHostRequestParams.ulOffset > ulTransferFileSize)
  2531. {
  2532. #if defined(DEBUG_FILE)
  2533. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Invalid offset requested");
  2534. #endif
  2535. ucRequestResponse = DOWNLOAD_RESPONSE_REQUEST_INVALID;
  2536. }
  2537. else
  2538. {
  2539. // Adjust response parameters
  2540. ulTransferBurstIndex = stHostRequestParams.ulOffset;
  2541. ulTransferBytesRemaining = ulTransferFileSize - ulTransferBurstIndex;
  2542. ulDownloadProgress = 0;
  2543. if((stHostRequestParams.ulBlockSize != 0) && (ulTransferBytesRemaining > stHostRequestParams.ulBlockSize))
  2544. { // Host is limiting block size
  2545. ulTransferBytesRemaining = stHostRequestParams.ulBlockSize;
  2546. }
  2547. if((ulTransferBlockSize != 0) && (ulTransferBytesRemaining > ulTransferBlockSize))
  2548. { // Client is limiting block size
  2549. ulTransferBytesRemaining = ulTransferBlockSize;
  2550. }
  2551. }
  2552. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2553. } // ucRequestResponse == DOWNLOAD_RESPONSE_OK
  2554. } // ucLinkCommandInProgress == ANTFS_DOWNLOAD_ID
  2555. } // bReceivedCommand
  2556. if(bRxError)
  2557. {
  2558. #if defined(DEBUG_FILE)
  2559. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptDownloadResponse(): Rx error");
  2560. #endif
  2561. bRxError = FALSE;
  2562. }
  2563. } // Rx command loop
  2564. } while (!bDone);
  2565. return eReturn;
  2566. }
  2567. ///////////////////////////////////////////////////////////////////////
  2568. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::AttemptUploadResponse()
  2569. {
  2570. RETURN_STATUS eReturn = RETURN_FAIL;
  2571. UCHAR aucBuffer[24];
  2572. ANTFS_DATA stHeader = {sizeof(aucBeacon), aucBeacon};
  2573. ANTFS_DATA stData = {sizeof(aucBuffer), aucBuffer};
  2574. ANTFS_DATA stFooter = {0, NULL};
  2575. ANTFRAMER_RETURN eTxComplete;
  2576. if((eANTFSState < ANTFS_CLIENT_STATE_TRANSPORT))
  2577. {
  2578. #if defined(DEBUG_FILE)
  2579. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptUploadResponse(): Device is not in correct state.");
  2580. #endif
  2581. return RETURN_FAIL;
  2582. }
  2583. if(ucRequestResponse == UPLOAD_RESPONSE_OK)
  2584. {
  2585. // TODO: Uploads not supported yet, so for now, always reject
  2586. ucRequestResponse = UPLOAD_RESPONSE_REQUEST_INVALID;
  2587. }
  2588. ulTransferBlockOffset = 0;
  2589. ulTransferMaxIndex = 0;
  2590. ulTransferBlockSize = 0;
  2591. usTransferCrc = 0;
  2592. eReturn = RETURN_REJECT;
  2593. LoadBeacon();
  2594. memset(aucBuffer, 0, sizeof(aucBuffer));
  2595. aucBuffer[ANTFS_CONNECTION_OFFSET] = ANTFS_RESPONSE_ID;
  2596. aucBuffer[ANTFS_RESPONSE_OFFSET] = ANTFS_RESPONSE_UPLOAD_ID;
  2597. aucBuffer[UPLOAD_RESPONSE_OFFSET] = ucRequestResponse;
  2598. Convert_ULONG_To_Bytes(ulTransferBlockOffset,
  2599. &aucBuffer[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET + 3],
  2600. &aucBuffer[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET + 2],
  2601. &aucBuffer[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET + 1],
  2602. &aucBuffer[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET]);
  2603. Convert_ULONG_To_Bytes(ulTransferMaxIndex,
  2604. &aucBuffer[UPLOAD_RESPONSE_MAX_SIZE_OFFSET + 3],
  2605. &aucBuffer[UPLOAD_RESPONSE_MAX_SIZE_OFFSET + 2],
  2606. &aucBuffer[UPLOAD_RESPONSE_MAX_SIZE_OFFSET + 1],
  2607. &aucBuffer[UPLOAD_RESPONSE_MAX_SIZE_OFFSET]);
  2608. Convert_ULONG_To_Bytes(ulTransferBlockSize,
  2609. &aucBuffer[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 3],
  2610. &aucBuffer[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 2],
  2611. &aucBuffer[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 1],
  2612. &aucBuffer[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET]);
  2613. Convert_USHORT_To_Bytes(usTransferCrc,
  2614. &aucBuffer[UPLOAD_RESPONSE_CRC_OFFSET + 1],
  2615. &aucBuffer[UPLOAD_RESPONSE_CRC_OFFSET]);
  2616. eTxComplete = pclANT->SendANTFSClientTransfer(ucChannelNumber, &stHeader, &stFooter, &stData, MESSAGE_TIMEOUT, NULL);
  2617. if (eTxComplete != ANTFRAMER_PASS)
  2618. {
  2619. #if defined(DEBUG_FILE)
  2620. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptUploadResponse(): Tx failed.");
  2621. if (eTxComplete == ANTFRAMER_FAIL)
  2622. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptUploadResponse(): Tx error.");
  2623. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2624. DSIDebug::ThreadWrite("ANTFSClientChannel::AttemptUploadResponse(): Tx timeout.");
  2625. #endif
  2626. return RETURN_FAIL;
  2627. }
  2628. return eReturn;
  2629. }
  2630. ///////////////////////////////////////////////////////////////////////
  2631. void ANTFSClientChannel::DecodeLinkCommand(UCHAR *pucLinkCommand_)
  2632. {
  2633. UCHAR ucPeriod;
  2634. if(pucLinkCommand_[ANTFS_CONNECTION_OFFSET] != ANTFS_COMMAND_ID)
  2635. return;
  2636. switch (pucLinkCommand_[ANTFS_COMMAND_OFFSET])
  2637. {
  2638. case ANTFS_CONNECT_ID:
  2639. {
  2640. #if defined(DEBUG_FILE)
  2641. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeLinkCommand(): Received LINK request.");
  2642. #endif
  2643. ucActiveBeaconFrequency = pucLinkCommand_[TRANSPORT_CHANNEL_FREQ_OFFSET];
  2644. ucPeriod= pucLinkCommand_[TRANSPORT_CHANNEL_PERIOD];
  2645. SetANTChannelPeriod(ucPeriod);
  2646. ulHostSerialNumber = Convert_Bytes_To_ULONG(
  2647. pucLinkCommand_[HOST_ID_OFFSET+3],
  2648. pucLinkCommand_[HOST_ID_OFFSET+2],
  2649. pucLinkCommand_[HOST_ID_OFFSET+1],
  2650. pucLinkCommand_[HOST_ID_OFFSET]);
  2651. DSIThread_MutexLock(&stMutexCriticalSection);
  2652. eANTFSRequest = ANTFS_REQUEST_CONNECT;
  2653. DSIThread_CondSignal(&stCondRequest);
  2654. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2655. break;
  2656. } // CONNECT
  2657. case ANTFS_DISCONNECT_ID:
  2658. {
  2659. #if defined(DEBUG_FILE)
  2660. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeLinkCommand(): Received DISCONNECT request");
  2661. #endif
  2662. stHostDisconnectParams.ucCommandType = pucLinkCommand_[DISCONNECT_COMMAND_TYPE_OFFSET];
  2663. stHostDisconnectParams.ucTimeDuration = pucLinkCommand_[DISCONNECT_TIME_DURATION_OFFSET];
  2664. stHostDisconnectParams.ucAppSpecificDuration = pucLinkCommand_[DISCONNECT_APP_DURATION_OFFSET];
  2665. DSIThread_MutexLock(&stMutexCriticalSection);
  2666. eANTFSRequest = ANTFS_REQUEST_DISCONNECT;
  2667. DSIThread_CondSignal(&stCondRequest);
  2668. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2669. break;
  2670. } // DISCONNECT
  2671. default:
  2672. {
  2673. #if defined(DEBUG_FILE)
  2674. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeLinkCommand(): Invalid command.");
  2675. #endif
  2676. break;
  2677. }
  2678. }
  2679. }
  2680. ///////////////////////////////////////////////////////////////////////
  2681. void ANTFSClientChannel::DecodeAuthenticateCommand(UCHAR ucControlByte_, UCHAR *pucAuthCommand_)
  2682. {
  2683. if (((ucControlByte_ & ~SEQUENCE_LAST_MESSAGE) == 0) && (ucLinkCommandInProgress != ANTFS_CMD_NONE))
  2684. {
  2685. #if defined(DEBUG_FILE)
  2686. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Received new request, but client is busy processing something already.");
  2687. #endif
  2688. return;
  2689. }
  2690. if(pucAuthCommand_[ANTFS_CONNECTION_OFFSET] == ANTFS_COMMAND_ID)
  2691. {
  2692. if(pucAuthCommand_[ANTFS_COMMAND_OFFSET] == ANTFS_AUTHENTICATE_ID)
  2693. {
  2694. ucLinkCommandInProgress = ANTFS_AUTHENTICATE_ID;
  2695. ucAuthCommandType = pucAuthCommand_[AUTH_COMMAND_TYPE_OFFSET];
  2696. }
  2697. }
  2698. if((ucLinkCommandInProgress == ANTFS_AUTHENTICATE_ID))
  2699. {
  2700. if((ucControlByte_ & SEQUENCE_NUMBER_ROLLOVER) == 0) // first packet
  2701. {
  2702. ULONG ulRxHostSerialNumber = Convert_Bytes_To_ULONG(
  2703. pucAuthCommand_[HOST_ID_OFFSET+3],
  2704. pucAuthCommand_[HOST_ID_OFFSET+2],
  2705. pucAuthCommand_[HOST_ID_OFFSET+1],
  2706. pucAuthCommand_[HOST_ID_OFFSET]);
  2707. bAcceptRequest = TRUE;
  2708. if(ulRxHostSerialNumber != ulHostSerialNumber) // Check host serial number
  2709. {
  2710. #if defined(DEBUG_FILE)
  2711. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Not the expected host");
  2712. #endif
  2713. bAcceptRequest = FALSE;
  2714. }
  2715. }
  2716. switch(ucAuthCommandType)
  2717. {
  2718. case AUTH_COMMAND_REQ_SERIAL_NUM:
  2719. {
  2720. if(ucControlByte_ & SEQUENCE_LAST_MESSAGE) // wait until the burst completes
  2721. {
  2722. #if defined(DEBUG_FILE)
  2723. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Received AUTH serial number request.");
  2724. #endif
  2725. DSIThread_MutexLock(&stMutexCriticalSection);
  2726. eANTFSRequest = ANTFS_REQUEST_AUTHENTICATE;
  2727. DSIThread_CondSignal(&stCondRequest);
  2728. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2729. }
  2730. break;
  2731. } // AUTH_COMMAND_REQ_SERIAL_NUM
  2732. case AUTH_COMMAND_GOTO_TRANSPORT:
  2733. {
  2734. if(ucControlByte_ & SEQUENCE_LAST_MESSAGE) // wait until the burst completes
  2735. {
  2736. #if defined(DEBUG_FILE)
  2737. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Received AUTH pass through request.");
  2738. #endif
  2739. if(stInitParams.ucAuthType != AUTH_COMMAND_GOTO_TRANSPORT)
  2740. {
  2741. #if defined(DEBUG_FILE)
  2742. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Pass through authentication not supported.");
  2743. #endif
  2744. bAcceptRequest = FALSE;
  2745. }
  2746. DSIThread_MutexLock(&stMutexCriticalSection);
  2747. eANTFSRequest = ANTFS_REQUEST_AUTHENTICATE;
  2748. DSIThread_CondSignal(&stCondRequest);
  2749. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2750. }
  2751. break;
  2752. } // AUTH_COMMAND_GOTO_TRANSPORT
  2753. case AUTH_COMMAND_PAIR:
  2754. {
  2755. if((ucControlByte_ & SEQUENCE_NUMBER_ROLLOVER) == 0) // first packet
  2756. {
  2757. #if defined(DEBUG_FILE)
  2758. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Received AUTH pairing request.");
  2759. #endif
  2760. stHostFriendlyName.bNameSet = FALSE;
  2761. stHostFriendlyName.ucIndex = 0;
  2762. stHostFriendlyName.ucSize = pucAuthCommand_[AUTH_FRIENDLY_NAME_LENGTH_OFFSET];
  2763. if(stHostFriendlyName.ucSize > FRIENDLY_NAME_MAX_LENGTH)
  2764. stHostFriendlyName.ucSize = FRIENDLY_NAME_MAX_LENGTH;
  2765. memset(stHostFriendlyName.acFriendlyName, 0, FRIENDLY_NAME_MAX_LENGTH);
  2766. }
  2767. else // read host friendly name
  2768. {
  2769. if(stHostFriendlyName.ucIndex < FRIENDLY_NAME_MAX_LENGTH)
  2770. {
  2771. UCHAR ucNumBytes = FRIENDLY_NAME_MAX_LENGTH - stHostFriendlyName.ucIndex;
  2772. if(ucNumBytes > 8)
  2773. {
  2774. ucNumBytes = 8;
  2775. }
  2776. memcpy((UCHAR*) &stHostFriendlyName.acFriendlyName[stHostFriendlyName.ucIndex], pucAuthCommand_, ucNumBytes);
  2777. stHostFriendlyName.ucIndex += ucNumBytes;
  2778. }
  2779. }
  2780. if(ucControlByte_ & SEQUENCE_LAST_MESSAGE) // last packet
  2781. {
  2782. ENUM_ANTFS_REQUEST eTheRequest;
  2783. if(stInitParams.bPairingEnabled && (stInitParams.ucAuthType <= AUTH_COMMAND_PASSKEY) && bAcceptRequest)
  2784. {
  2785. if(stHostFriendlyName.ucSize > 0)
  2786. {
  2787. stHostFriendlyName.bNameSet = TRUE;
  2788. }
  2789. eTheRequest = ANTFS_REQUEST_PAIR;
  2790. }
  2791. else
  2792. {
  2793. #if defined(DEBUG_FILE)
  2794. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Pairing authentication not supported.");
  2795. #endif
  2796. bAcceptRequest = FALSE;
  2797. eTheRequest = ANTFS_REQUEST_AUTHENTICATE;
  2798. }
  2799. DSIThread_MutexLock(&stMutexCriticalSection);
  2800. eANTFSRequest = eTheRequest;
  2801. DSIThread_CondSignal(&stCondRequest);
  2802. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2803. }
  2804. break;
  2805. } // AUTH_COMMAND_PAIR
  2806. case AUTH_COMMAND_PASSKEY:
  2807. {
  2808. if ((ucControlByte_ & SEQUENCE_NUMBER_ROLLOVER) == 0) // initial packet
  2809. {
  2810. UCHAR ucPasswordSize = pucAuthCommand_[AUTH_PASSWORD_LENGTH_OFFSET]; // Passkey length
  2811. #if defined(DEBUG_FILE)
  2812. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Received AUTH passkey request.");
  2813. #endif
  2814. if(ucPasswordSize != ucPassKeySize)
  2815. {
  2816. bAcceptRequest = FALSE; // Reject if lengths do not match
  2817. #if defined(DEBUG_FILE)
  2818. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Incorrect string size");
  2819. #endif
  2820. }
  2821. else
  2822. {
  2823. ucPassKeyIndex = 0;
  2824. }
  2825. }
  2826. else
  2827. {
  2828. UCHAR ucCounter;
  2829. for (ucCounter = 0; ucCounter < 8; ucCounter++)
  2830. {
  2831. if (ucPassKeyIndex >= ucPassKeySize)
  2832. break;
  2833. if (aucPassKey[ucPassKeyIndex++] != pucAuthCommand_[ucCounter])
  2834. {
  2835. bAcceptRequest = FALSE; // Reject if passkeys are different
  2836. }
  2837. }
  2838. }
  2839. if (ucControlByte_ & SEQUENCE_LAST_MESSAGE) // last packet
  2840. {
  2841. if((stInitParams.ucAuthType != AUTH_COMMAND_PASSKEY) && (stInitParams.ucAuthType != AUTH_COMMAND_GOTO_TRANSPORT) && (ucPassKeySize == 0))
  2842. {
  2843. bAcceptRequest = FALSE;
  2844. #if defined(DEBUG_FILE)
  2845. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Passkey authentication not supported.");
  2846. #endif
  2847. }
  2848. if (ucPassKeyIndex < ucPassKeySize)
  2849. {
  2850. bAcceptRequest = FALSE; // Reject if we did not get the complete passkey
  2851. #if defined(DEBUG_FILE)
  2852. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Incomplete passkey.");
  2853. #endif
  2854. }
  2855. DSIThread_MutexLock(&stMutexCriticalSection);
  2856. eANTFSRequest = ANTFS_REQUEST_AUTHENTICATE;
  2857. DSIThread_CondSignal(&stCondRequest);
  2858. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2859. }
  2860. break;
  2861. } // AUTH_COMMAND_PASSKEY
  2862. default:
  2863. {
  2864. if (ucControlByte_ & SEQUENCE_LAST_MESSAGE) // last packet
  2865. {
  2866. #if defined(DEBUG_FILE)
  2867. UCHAR aucString[256];
  2868. SNPRINTF((char*) aucString, 256, "Received unknown AUTH request: %u", ucAuthCommandType);
  2869. DSIDebug::ThreadWrite((char*) aucString);
  2870. #endif
  2871. DSIThread_MutexLock(&stMutexCriticalSection);
  2872. eANTFSRequest = ANTFS_REQUEST_AUTHENTICATE;
  2873. bAcceptRequest = FALSE; // Reject unknown auth requests
  2874. DSIThread_CondSignal(&stCondRequest);
  2875. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2876. }
  2877. break;
  2878. }
  2879. }
  2880. } // ANTFS_AUTHENTICATE_ID
  2881. else if (pucAuthCommand_[ANTFS_COMMAND_OFFSET] == ANTFS_DISCONNECT_ID)
  2882. {
  2883. if (ucControlByte_ & SEQUENCE_LAST_MESSAGE) // don't do anything until the burst completes
  2884. {
  2885. #if defined(DEBUG_FILE)
  2886. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Received DISCONNECT request");
  2887. #endif
  2888. stHostDisconnectParams.ucCommandType = pucAuthCommand_[DISCONNECT_COMMAND_TYPE_OFFSET];
  2889. stHostDisconnectParams.ucTimeDuration = pucAuthCommand_[DISCONNECT_TIME_DURATION_OFFSET];
  2890. stHostDisconnectParams.ucAppSpecificDuration = pucAuthCommand_[DISCONNECT_APP_DURATION_OFFSET];
  2891. DSIThread_MutexLock(&stMutexCriticalSection);
  2892. eANTFSRequest = ANTFS_REQUEST_DISCONNECT;
  2893. DSIThread_CondSignal(&stCondRequest);
  2894. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2895. }
  2896. } // ANTFS_DISCONNECT_ID
  2897. else if (pucAuthCommand_[ANTFS_COMMAND_OFFSET] == ANTFS_PING_ID)
  2898. {
  2899. #if defined(DEBUG_FILE)
  2900. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeAuthenticateCommand(): Ping!");
  2901. #endif
  2902. DSIThread_MutexLock(&stMutexCriticalSection);
  2903. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  2904. eANTFSRequest = ANTFS_REQUEST_PING;
  2905. DSIThread_CondSignal(&stCondRequest);
  2906. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2907. } // ANTFS_PING_ID
  2908. else
  2909. {
  2910. #if defined(DEBUG_FILE)
  2911. UCHAR aucString[256];
  2912. SNPRINTF((char *) aucString, 256, "ANTFSClientChannel::DecodeAuthenticateCommand(): Received invalid request: %u", pucAuthCommand_[ANTFS_COMMAND_OFFSET]);
  2913. DSIDebug::ThreadWrite((char*) aucString);
  2914. #endif
  2915. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  2916. } // OTHER REQUESTS
  2917. }
  2918. ///////////////////////////////////////////////////////////////////////
  2919. void ANTFSClientChannel::DecodeTransportCommand(UCHAR ucControlByte_, UCHAR *pucTransCommand_)
  2920. {
  2921. if (((ucControlByte_ & ~SEQUENCE_LAST_MESSAGE) == 0) && (ucLinkCommandInProgress != ANTFS_CMD_NONE))
  2922. {
  2923. #if defined(DEBUG_FILE)
  2924. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeTransportCommand(): Received new request, but client is busy.");
  2925. #endif
  2926. return;
  2927. }
  2928. if(pucTransCommand_[ANTFS_CONNECTION_OFFSET] == ANTFS_COMMAND_ID)
  2929. {
  2930. ucLinkCommandInProgress = pucTransCommand_[ANTFS_COMMAND_OFFSET];
  2931. }
  2932. switch(ucLinkCommandInProgress)
  2933. {
  2934. case ANTFS_PING_ID:
  2935. {
  2936. #if defined(DEBUG_FILE)
  2937. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeTransportCommand(): Ping!");
  2938. #endif
  2939. DSIThread_MutexLock(&stMutexCriticalSection);
  2940. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  2941. eANTFSRequest = ANTFS_REQUEST_PING;
  2942. DSIThread_CondSignal(&stCondRequest);
  2943. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2944. } // ANTFS_PING_ID
  2945. break;
  2946. case ANTFS_DISCONNECT_ID:
  2947. {
  2948. if (ucControlByte_ & SEQUENCE_LAST_MESSAGE) // don't do anything until the burst completes
  2949. {
  2950. #if defined(DEBUG_FILE)
  2951. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeTransportCommand(): Received DISCONNECT request");
  2952. #endif
  2953. stHostDisconnectParams.ucCommandType = pucTransCommand_[DISCONNECT_COMMAND_TYPE_OFFSET];
  2954. stHostDisconnectParams.ucTimeDuration = pucTransCommand_[DISCONNECT_TIME_DURATION_OFFSET];
  2955. stHostDisconnectParams.ucAppSpecificDuration = pucTransCommand_[DISCONNECT_APP_DURATION_OFFSET];
  2956. DSIThread_MutexLock(&stMutexCriticalSection);
  2957. eANTFSRequest = ANTFS_REQUEST_DISCONNECT;
  2958. DSIThread_CondSignal(&stCondRequest);
  2959. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2960. }
  2961. } // ANTFS_DISCONNECT_ID
  2962. break;
  2963. case ANTFS_LINK_ID:
  2964. {
  2965. ULONG ulRxHostSerialNumber;
  2966. #if defined(DEBUG_FILE)
  2967. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeTransportCommand(): Received LINK request.");
  2968. #endif
  2969. ulRxHostSerialNumber = Convert_Bytes_To_ULONG(
  2970. pucTransCommand_[HOST_ID_OFFSET+3],
  2971. pucTransCommand_[HOST_ID_OFFSET+2],
  2972. pucTransCommand_[HOST_ID_OFFSET+1],
  2973. pucTransCommand_[HOST_ID_OFFSET]);
  2974. if(ulHostSerialNumber == ulRxHostSerialNumber)
  2975. {
  2976. UCHAR ucPeriod= pucTransCommand_[TRANSPORT_CHANNEL_PERIOD];
  2977. ucActiveBeaconFrequency = pucTransCommand_[TRANSPORT_CHANNEL_FREQ_OFFSET];
  2978. SetANTChannelPeriod(ucPeriod);
  2979. DSIThread_MutexLock(&stMutexCriticalSection);
  2980. eANTFSRequest = ANTFS_REQUEST_CHANGE_LINK;
  2981. DSIThread_CondSignal(&stCondRequest);
  2982. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2983. }
  2984. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  2985. } // ANTFS_LINK_ID
  2986. break;
  2987. case ANTFS_ERASE_ID:
  2988. {
  2989. if(ucControlByte_ & SEQUENCE_LAST_MESSAGE) // don't do anything unless the burst completes
  2990. {
  2991. ucRequestResponse = ERASE_RESPONSE_REJECT; // set initial request parameters
  2992. stHostRequestParams.usFileIndex = Convert_Bytes_To_USHORT(
  2993. pucTransCommand_[DATA_INDEX_OFFSET + 1],
  2994. pucTransCommand_[DATA_INDEX_OFFSET]);
  2995. stHostRequestParams.bInitialRequest = FALSE;
  2996. stHostRequestParams.ulBlockSize = 0;
  2997. stHostRequestParams.ulOffset = 0;
  2998. stHostRequestParams.usCRCSeed = 0;
  2999. #if defined(DEBUG_FILE)
  3000. {
  3001. UCHAR aucString[256];
  3002. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::DecodeTransportCommand: Received ERASE request (%hu)", stHostRequestParams.usFileIndex);
  3003. DSIDebug::ThreadWrite((char*) aucString);
  3004. }
  3005. #endif
  3006. DSIThread_MutexLock(&stMutexCriticalSection);
  3007. ucLinkCommandInProgress = ANTFS_ERASE_ID;
  3008. eANTFSRequest = ANTFS_REQUEST_ERASE;
  3009. DSIThread_CondSignal(&stCondRequest);
  3010. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3011. }
  3012. } // ANTFS_ERASE_ID
  3013. break;
  3014. case ANTFS_DOWNLOAD_ID:
  3015. {
  3016. if((ucControlByte_ & SEQUENCE_NUMBER_ROLLOVER) == 0) // first packet
  3017. {
  3018. stHostRequestParams.usFileIndex = Convert_Bytes_To_USHORT(
  3019. pucTransCommand_[DATA_INDEX_OFFSET + 1],
  3020. pucTransCommand_[DATA_INDEX_OFFSET]);
  3021. stHostRequestParams.ulOffset = Convert_Bytes_To_ULONG(
  3022. pucTransCommand_[DOWNLOAD_DATA_OFFSET_OFFSET + 3],
  3023. pucTransCommand_[DOWNLOAD_DATA_OFFSET_OFFSET + 2],
  3024. pucTransCommand_[DOWNLOAD_DATA_OFFSET_OFFSET + 1],
  3025. pucTransCommand_[DOWNLOAD_DATA_OFFSET_OFFSET]);
  3026. #if defined(DEBUG_FILE)
  3027. {
  3028. UCHAR aucString[256];
  3029. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::DecodeTransportCommand: Received DOWNLOAD request (%hu/%lu)", stHostRequestParams.usFileIndex, stHostRequestParams.ulOffset);
  3030. DSIDebug::ThreadWrite((char*) aucString);
  3031. }
  3032. #endif
  3033. }
  3034. else if (ucControlByte_ & SEQUENCE_LAST_MESSAGE) // last (second) packet
  3035. {
  3036. stHostRequestParams.usCRCSeed = Convert_Bytes_To_USHORT(
  3037. pucTransCommand_[DOWNLOAD_DATA_CRC_OFFSET + 1],
  3038. pucTransCommand_[DOWNLOAD_DATA_CRC_OFFSET]);
  3039. stHostRequestParams.ulBlockSize = Convert_Bytes_To_ULONG(
  3040. pucTransCommand_[DOWNLOAD_DATA_SIZE_OFFSET + 3],
  3041. pucTransCommand_[DOWNLOAD_DATA_SIZE_OFFSET + 2],
  3042. pucTransCommand_[DOWNLOAD_DATA_SIZE_OFFSET + 1],
  3043. pucTransCommand_[DOWNLOAD_DATA_SIZE_OFFSET]);
  3044. stHostRequestParams.bInitialRequest = pucTransCommand_[DOWNLOAD_DATA_INITIAL_OFFSET] & 0x01;
  3045. stHostRequestParams.ulMaxSize = 0;
  3046. DSIThread_MutexLock(&stMutexCriticalSection);
  3047. if(eANTFSState == ANTFS_CLIENT_STATE_TRANSPORT)
  3048. {
  3049. ucLinkCommandInProgress = ANTFS_DOWNLOAD_ID;
  3050. eANTFSRequest = ANTFS_REQUEST_DOWNLOAD;
  3051. DSIThread_CondSignal(&stCondRequest);
  3052. }
  3053. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3054. }
  3055. } // ANTFS_DOWNLOAD_ID
  3056. break;
  3057. case ANTFS_UPLOAD_REQUEST_ID:
  3058. {
  3059. if(stInitParams.bUploadEnabled) // Only process if we support uploads
  3060. {
  3061. if((ucControlByte_ & SEQUENCE_NUMBER_ROLLOVER) == 0) // first packet
  3062. {
  3063. stHostRequestParams.bInitialRequest = FALSE;
  3064. stHostRequestParams.usFileIndex = Convert_Bytes_To_USHORT(
  3065. pucTransCommand_[DATA_INDEX_OFFSET + 1],
  3066. pucTransCommand_[DATA_INDEX_OFFSET]);
  3067. stHostRequestParams.ulMaxSize = Convert_Bytes_To_ULONG(
  3068. pucTransCommand_[UPLOAD_MAX_SIZE_OFFSET + 3],
  3069. pucTransCommand_[UPLOAD_MAX_SIZE_OFFSET + 2],
  3070. pucTransCommand_[UPLOAD_MAX_SIZE_OFFSET + 1],
  3071. pucTransCommand_[UPLOAD_MAX_SIZE_OFFSET]);
  3072. }
  3073. else if (ucControlByte_ & SEQUENCE_LAST_MESSAGE) // last (second) packet
  3074. {
  3075. stHostRequestParams.ulOffset = Convert_Bytes_To_ULONG(
  3076. pucTransCommand_[DATA_OFFSET_SMALL_OFFSET + 3],
  3077. pucTransCommand_[DATA_OFFSET_SMALL_OFFSET + 2],
  3078. pucTransCommand_[DATA_OFFSET_SMALL_OFFSET + 1],
  3079. pucTransCommand_[DATA_OFFSET_SMALL_OFFSET]);
  3080. stHostRequestParams.bInitialRequest = FALSE;
  3081. stHostRequestParams.ulBlockSize = 0;
  3082. stHostRequestParams.usCRCSeed = 0;
  3083. #if defined(DEBUG_FILE)
  3084. {
  3085. UCHAR aucString[256];
  3086. SNPRINTF((char*) aucString, 256, "ANTFSClientChannel::DecodeTransportCommand: Received UPLOAD request (%hu/%lu)", stHostRequestParams.usFileIndex, stHostRequestParams.ulOffset);
  3087. DSIDebug::ThreadWrite((char*) aucString);
  3088. }
  3089. #endif
  3090. DSIThread_MutexLock(&stMutexCriticalSection);
  3091. if(eANTFSState == ANTFS_CLIENT_STATE_TRANSPORT)
  3092. {
  3093. stHostRequestParams.bInitialRequest = TRUE;
  3094. ucLinkCommandInProgress = ANTFS_UPLOAD_REQUEST_ID;
  3095. //eANTFSRequest = ANTFS_REQUEST_UPLOAD;
  3096. // TODO: Uploads not implemented, should send upload request to application
  3097. // Reject the request, for now
  3098. ucRequestResponse = UPLOAD_RESPONSE_REQUEST_INVALID;
  3099. eANTFSRequest = ANTFS_REQUEST_UPLOAD_RESPONSE;
  3100. DSIThread_CondSignal(&stCondRequest);
  3101. }
  3102. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3103. }
  3104. }
  3105. else
  3106. {
  3107. if (ucControlByte_ & SEQUENCE_LAST_MESSAGE)
  3108. {
  3109. // Reject if uploads are not supported
  3110. DSIThread_MutexLock(&stMutexCriticalSection);
  3111. ucRequestResponse = UPLOAD_RESPONSE_REQUEST_INVALID;
  3112. eANTFSRequest = ANTFS_REQUEST_UPLOAD_RESPONSE;
  3113. DSIThread_CondSignal(&stCondRequest);
  3114. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3115. }
  3116. }
  3117. } // ANTFS_UPLOAD_ID
  3118. break;
  3119. case ANTFS_UPLOAD_DATA_ID:
  3120. {
  3121. if(stInitParams.bUploadEnabled && eANTFSState == ANTFS_CLIENT_STATE_UPLOADING_WAIT_FOR_RESPONSE) // Only process if we support uploads
  3122. {
  3123. if((ucControlByte_ & SEQUENCE_NUMBER_ROLLOVER) == 0) // first packet
  3124. {
  3125. #if defined(DEBUG_FILE)
  3126. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeTransportCommand(): Received UPLOAD DATA.");
  3127. #endif
  3128. stHostRequestParams.usCRCSeed = Convert_Bytes_To_USHORT(
  3129. pucTransCommand_[UPLOAD_DATA_CRC_SEED_OFFSET + 1],
  3130. pucTransCommand_[UPLOAD_DATA_CRC_SEED_OFFSET]);
  3131. stHostRequestParams.ulOffset = Convert_Bytes_To_ULONG(
  3132. pucTransCommand_[UPLOAD_DATA_DATA_OFFSET_OFFSET + 3],
  3133. pucTransCommand_[UPLOAD_DATA_DATA_OFFSET_OFFSET + 2],
  3134. pucTransCommand_[UPLOAD_DATA_DATA_OFFSET_OFFSET + 1],
  3135. pucTransCommand_[UPLOAD_DATA_DATA_OFFSET_OFFSET]);
  3136. }
  3137. if(ucControlByte_ & SEQUENCE_LAST_MESSAGE)
  3138. {
  3139. #if defined(DEBUG_FILE)
  3140. DSIDebug::ThreadWrite("ANTFSClientChannel::DecodeTransportCommand(): Upload contains no data.");
  3141. #endif
  3142. }
  3143. }
  3144. } // ANTFS_UPLOAD_DATA_ID:
  3145. break;
  3146. default:
  3147. {
  3148. #if defined(DEBUG_FILE)
  3149. UCHAR aucString[256];
  3150. SNPRINTF((char *) aucString, 256, "ANTFSClientChannel::DecodeTransportCommand(): Received invalid request: %u", ucLinkCommandInProgress);
  3151. DSIDebug::ThreadWrite((char*) aucString);
  3152. #endif
  3153. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  3154. } // DEFAULT
  3155. break;
  3156. }
  3157. }
  3158. ///////////////////////////////////////////////////////////////////////
  3159. void ANTFSClientChannel::UploadInputData(UCHAR ucControlByte_, UCHAR* pucMesg_)
  3160. {
  3161. // TODO: Implement upload
  3162. }
  3163. ///////////////////////////////////////////////////////////////////////
  3164. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::SwitchToLink(void)
  3165. {
  3166. if(eANTFSState < ANTFS_CLIENT_STATE_IDLE)
  3167. {
  3168. #if defined(DEBUG_FILE)
  3169. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToLink(): Device is not in correct state.");
  3170. #endif
  3171. return RETURN_FAIL;
  3172. }
  3173. if(eANTFSState > ANTFS_CLIENT_STATE_IDLE)
  3174. {
  3175. // Reload beacon link configuration
  3176. ucActiveBeaconStatus1 = 0;
  3177. ucActiveBeaconStatus1 |= ((stInitParams.ucLinkPeriod & BEACON_PERIOD_MASK) << BEACON_PERIOD_SHIFT);
  3178. ucActiveBeaconStatus1 |= stInitParams.bPairingEnabled * PAIRING_AVAILABLE_FLAG_MASK;
  3179. ucActiveBeaconStatus1 |= stInitParams.bUploadEnabled * UPLOAD_ENABLED_FLAG_MASK;
  3180. ucActiveBeaconStatus1 |= stInitParams.bDataAvailable * DATA_AVAILABLE_FLAG_MASK;
  3181. ucActiveBeaconFrequency = stInitParams.ucBeaconFrequency;
  3182. if(pclANT->SetChannelRFFrequency(ucChannelNumber, ucActiveBeaconFrequency, MESSAGE_TIMEOUT) == FALSE)
  3183. {
  3184. #if defined(DEBUG_FILE)
  3185. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToLink(): Failed ANT_SetChannelRFFreq().");
  3186. #endif
  3187. return RETURN_SERIAL_ERROR;
  3188. }
  3189. SetANTChannelPeriod(stInitParams.ucLinkPeriod);
  3190. if (pclANT->SetChannelPeriod(ucChannelNumber, usTheMessagePeriod, MESSAGE_TIMEOUT) == FALSE)
  3191. {
  3192. #if defined(DEBUG_FILE)
  3193. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToLink(): Failed ANT_SetChannelPeriod().");
  3194. #endif
  3195. return RETURN_SERIAL_ERROR;
  3196. }
  3197. if(bCustomTxPower)
  3198. {
  3199. if(pclANT->SetChannelTransmitPower(ucChannelNumber, ucLinkTxPower, MESSAGE_TIMEOUT) == FALSE)
  3200. {
  3201. #if defined(DEBUG_FILE)
  3202. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToLink(): Failed ANT_SetChannelTransmitPower(), setting power level for all channels.");
  3203. #endif
  3204. if(pclANT->SetAllChannelsTransmitPower(ucLinkTxPower, MESSAGE_TIMEOUT) == FALSE)
  3205. {
  3206. #if defined(DEBUG_FILE)
  3207. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToLink(): Failed ANT_SetAllChannelsTransmitPower().");
  3208. #endif
  3209. return RETURN_SERIAL_ERROR;
  3210. }
  3211. }
  3212. }
  3213. if(bReturnToBroadcast) // No need to reload beacon if going back to broadcast
  3214. {
  3215. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  3216. return RETURN_PASS;
  3217. }
  3218. }
  3219. eANTFSState = ANTFS_CLIENT_STATE_BEACONING;
  3220. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  3221. bReceivedCommand = FALSE;
  3222. bReceivedBurst = FALSE;
  3223. bRxError = FALSE;
  3224. LoadBeacon();
  3225. if(pclANT->SendBroadcastData(ucChannelNumber, aucBeacon) == FALSE)
  3226. {
  3227. #if defined(DEBUG_FILE)
  3228. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToLink(): Failed to load beacon.");
  3229. #endif
  3230. return RETURN_SERIAL_ERROR;
  3231. }
  3232. return RETURN_PASS;
  3233. }
  3234. ///////////////////////////////////////////////////////////////////////
  3235. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::SwitchToAuthenticate(void)
  3236. {
  3237. if(eANTFSState < ANTFS_CLIENT_STATE_BEACONING)
  3238. {
  3239. #if defined(DEBUG_FILE)
  3240. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToAuthenticate(): Device is not in correct state.");
  3241. #endif
  3242. return RETURN_FAIL;
  3243. }
  3244. if(eANTFSState < ANTFS_CLIENT_STATE_CONNECTED)
  3245. {
  3246. if(pclANT->SetChannelRFFrequency(ucChannelNumber, ucActiveBeaconFrequency, MESSAGE_TIMEOUT) == FALSE)
  3247. {
  3248. #if defined(DEBUG_FILE)
  3249. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToAuthenticate(): Failed ANT_SetChannelRFFreq().");
  3250. #endif
  3251. return RETURN_SERIAL_ERROR;
  3252. }
  3253. if (pclANT->SetChannelPeriod(ucChannelNumber, usTheMessagePeriod, MESSAGE_TIMEOUT) == FALSE)
  3254. {
  3255. #if defined(DEBUG_FILE)
  3256. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToAuthenticate(): Failed ANT_SetChannelPeriod().");
  3257. #endif
  3258. return RETURN_SERIAL_ERROR;
  3259. }
  3260. if(bCustomTxPower)
  3261. {
  3262. if(pclANT->SetChannelTransmitPower(ucChannelNumber, ucSessionTxPower, MESSAGE_TIMEOUT) == FALSE)
  3263. {
  3264. #if defined(DEBUG_FILE)
  3265. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToAuthenticate(): Failed ANT_SetChannelTransmittPower(), setting power level for all channels.");
  3266. #endif
  3267. if(pclANT->SetAllChannelsTransmitPower(ucSessionTxPower, MESSAGE_TIMEOUT) == FALSE)
  3268. {
  3269. #if defined(DEBUG_FILE)
  3270. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToAuthenticate(): Failed ANT_SetAllChannelsTransmittPower().");
  3271. #endif
  3272. return RETURN_SERIAL_ERROR;
  3273. }
  3274. }
  3275. }
  3276. }
  3277. ucPairingTimeout = MAX_UCHAR;
  3278. eANTFSState = ANTFS_CLIENT_STATE_CONNECTED;
  3279. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  3280. bReceivedCommand = FALSE;
  3281. bReceivedBurst = FALSE;
  3282. bRxError = FALSE;
  3283. LoadBeacon();
  3284. if(pclANT->SendBroadcastData(ucChannelNumber, aucBeacon) == FALSE)
  3285. {
  3286. #if defined(DEBUG_FILE)
  3287. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToAuthenticate(): Failed to load beacon.");
  3288. #endif
  3289. return RETURN_SERIAL_ERROR;
  3290. }
  3291. return RETURN_PASS;
  3292. }
  3293. ///////////////////////////////////////////////////////////////////////
  3294. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::SwitchToTransport(void)
  3295. {
  3296. if(eANTFSState < ANTFS_CLIENT_STATE_CONNECTED)
  3297. {
  3298. #if defined(DEBUG_FILE)
  3299. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToTransport(): Device is not in correct state.");
  3300. #endif
  3301. return RETURN_FAIL;
  3302. }
  3303. eANTFSState = ANTFS_CLIENT_STATE_TRANSPORT;
  3304. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  3305. bReceivedCommand = FALSE;
  3306. bReceivedBurst = FALSE;
  3307. bRxError = FALSE;
  3308. LoadBeacon();
  3309. if(pclANT->SendBroadcastData(ucChannelNumber, aucBeacon) == FALSE)
  3310. {
  3311. #if defined(DEBUG_FILE)
  3312. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchToTransport(): Failed to load beacon.");
  3313. #endif
  3314. return RETURN_SERIAL_ERROR;
  3315. }
  3316. return RETURN_PASS;
  3317. }
  3318. ///////////////////////////////////////////////////////////////////////
  3319. ANTFSClientChannel::RETURN_STATUS ANTFSClientChannel::SwitchLinkParameters(void)
  3320. {
  3321. if(eANTFSState < ANTFS_CLIENT_STATE_TRANSPORT)
  3322. {
  3323. #if defined(DEBUG_FILE)
  3324. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchLinkParameters(): Device is not in correct state.");
  3325. #endif
  3326. return RETURN_FAIL;
  3327. }
  3328. if(pclANT->SetChannelRFFrequency(ucChannelNumber, ucActiveBeaconFrequency, MESSAGE_TIMEOUT) == FALSE)
  3329. {
  3330. #if defined(DEBUG_FILE)
  3331. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchLinkParameters(): Failed ANT_SetChannelRFFreq().");
  3332. #endif
  3333. return RETURN_SERIAL_ERROR;
  3334. }
  3335. if (pclANT->SetChannelPeriod(ucChannelNumber, usTheMessagePeriod, MESSAGE_TIMEOUT) == FALSE)
  3336. {
  3337. #if defined(DEBUG_FILE)
  3338. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchLinkParameters(): Failed ANT_SetChannelPeriod().");
  3339. #endif
  3340. return RETURN_SERIAL_ERROR;
  3341. }
  3342. ucLinkCommandInProgress = ANTFS_CMD_NONE;
  3343. LoadBeacon();
  3344. if(pclANT->SendBroadcastData(ucChannelNumber, aucBeacon) == FALSE)
  3345. {
  3346. #if defined(DEBUG_FILE)
  3347. DSIDebug::ThreadWrite("ANTFSClientChannel::SwitchLinkParameters(): Failed to load beacon.");
  3348. #endif
  3349. return RETURN_SERIAL_ERROR;
  3350. }
  3351. return RETURN_PASS;
  3352. }
  3353. ///////////////////////////////////////////////////////////////////////
  3354. void ANTFSClientChannel::SetANTChannelPeriod(UCHAR ucLinkPeriod_)
  3355. {
  3356. switch (ucLinkPeriod_)
  3357. {
  3358. default: // Shouldn't happen.
  3359. case BEACON_PERIOD_0_5_HZ:
  3360. usTheMessagePeriod = 65535;
  3361. break;
  3362. case BEACON_PERIOD_1_HZ:
  3363. usTheMessagePeriod = 32768;
  3364. break;
  3365. case BEACON_PERIOD_2_HZ:
  3366. usTheMessagePeriod = 16384;
  3367. break;
  3368. case BEACON_PERIOD_4_HZ:
  3369. usTheMessagePeriod = 8192;
  3370. break;
  3371. case BEACON_PERIOD_8_HZ:
  3372. usTheMessagePeriod = 4096;
  3373. break;
  3374. case BEACON_PERIOD_KEEP:
  3375. usTheMessagePeriod = usBeaconChannelPeriod;
  3376. break;
  3377. }
  3378. ucActiveBeaconStatus1 &= ~BEACON_PERIOD_MASK;
  3379. ucActiveBeaconStatus1 |= ((ucLinkPeriod_ & BEACON_PERIOD_MASK) << BEACON_PERIOD_SHIFT);
  3380. }