antfs_client_channel.cpp 158 KB

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