antfs_host_channel.cpp 219 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912
  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_host_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. #define TRANSPORT_NETWORK ((UCHAR) 0)
  30. #define TRANSPORT_MESSAGE_PERIOD ((USHORT) 4096) // 8Hz default
  31. #define TRANSPORT_MESSAGE_PERIOD_CODE ((UCHAR) 4) // 8Hz default
  32. #define TRANSPORT_SEARCH_TIMEOUT ((UCHAR) 3) // ~7.5 Seconds.
  33. #define HOST_SERIAL_NUMBER ((ULONG) 1) // Use if not configured otherwise
  34. #define UPLOAD_PROGRESS_CHECK_BYTES ((ULONG) 64) // 64 bytes (8 packets) have to be transfered in order to reset the UL loop timeout.
  35. #define ADDRESS_TYPE_PAGE_OFFSET 3
  36. #define ADDRESS_TYPE_PAGE_SMALL_0 ((UCHAR) 0x00) // Default; page contains both data offset and data block size.
  37. #define ADDRESS_TYPE_PAGE_SMALL_1 ((UCHAR) 0x01) // Contains total remaining data length (in response)
  38. #define ADDRESS_TYPE_PAGE_BIG_0 ((UCHAR) 0x02) // Page contains data offset.
  39. #define ADDRESS_TYPE_PAGE_BIG_1 ((UCHAR) 0x03) // Page contains data block size.
  40. #define ADDRESS_TYPE_PAGE_BIG_2 ((UCHAR) 0x04) // Page contains total remaining data length (in response)
  41. #define DATA_OFFSET_OFFSET_LOW 4
  42. #define DATA_OFFSET_OFFSET_HIGH 5
  43. #define DATA_BLOCK_SIZE_OFFSET_LOW 6
  44. #define DATA_BLOCK_SIZE_OFFSET_HIGH 7
  45. // ID Command
  46. #define ID_OFFSET_0 4
  47. #define ID_OFFSET_1 5
  48. #define ID_OFFSET_2 6
  49. #define ID_OFFSET_3 7
  50. #define ANTFS_RESPONSE_RETRIES 5
  51. #define SEND_DIRECT_BURST_TIMEOUT 15000 // 15 seconds.
  52. #define STRIKE_COUNT 2
  53. #define MAX_STALE_COUNT ((UCHAR) 100)
  54. #define MAJOR_STALE_COUNT ((UCHAR) 50)
  55. #define MINOR_STALE_COUNT ((UCHAR) 25)
  56. #define MIN_STALE_COUNT ((UCHAR) 1)
  57. typedef void (*CALLBACK_T)(void);
  58. static const UCHAR caucNetworkKey[] = NETWORK_KEY;
  59. static const char acSwVersion[] = SW_VER;
  60. //////////////////////////////////////////////////////////////////////////////////
  61. // Private Function Prototypes
  62. //////////////////////////////////////////////////////////////////////////////////
  63. static int ListCompare(const void *puvKeyVal, const void *puvDatum);
  64. static int DeviceParametersItemCompare(const void *pvItem1, const void *pvItem2);
  65. //////////////////////////////////////////////////////////////////////////////////
  66. // Public Functions
  67. //////////////////////////////////////////////////////////////////////////////////
  68. ANTFSHostChannel::ANTFSHostChannel()
  69. {
  70. bInitFailed = FALSE;
  71. bIgnoreListRunning = FALSE;
  72. bPingEnabled = FALSE;
  73. bRequestPageEnabled = FALSE;
  74. // Default channel configuration
  75. ucChannelNumber = ANTFS_CHANNEL;
  76. ucNetworkNumber = ANTFS_NETWORK;
  77. ucTheDeviceType = ANTFS_DEVICE_TYPE;
  78. ucTheTransmissionType = ANTFS_TRANSMISSION_TYPE;
  79. usTheMessagePeriod = ANTFS_MESSAGE_PERIOD;
  80. ucTheProxThreshold = 0; //0=Disabled
  81. memcpy(aucTheNetworkkey,caucNetworkKey,8);
  82. cfgParams.ul_cfg_auth_timeout = AUTH_TIMEOUT;
  83. cfgParams.ul_cfg_burst_check_timeout = BURST_CHECK_TIMEOUT;
  84. cfgParams.ul_cfg_erase_timeout = ERASE_TIMEOUT;
  85. cfgParams.ul_cfg_upload_request_timeout = UPLOAD_REQUEST_TIMEOUT;
  86. cfgParams.ul_cfg_upload_response_timeout = UPLOAD_RESPONSE_TIMEOUT;
  87. // Host configuration
  88. ulHostSerialNumber = HOST_SERIAL_NUMBER;
  89. hANTFSThread = (DSI_THREAD_ID)NULL; // Handle for the ANTFS thread
  90. bKillThread = FALSE;
  91. bANTFSThreadRunning = FALSE;
  92. pclANT = (DSIFramerANT*) NULL;
  93. pbCancel = &bCancel;
  94. *pbCancel = FALSE;
  95. // Ignore List variables
  96. usListIndex = 0;
  97. memset(astIgnoreList, 0, sizeof(astIgnoreList));
  98. pclQueueTimer = (DSITimer*)NULL;
  99. ucTransportFrequencyStaleCount = 0;
  100. ucCurrentTransportFreqElement = 0;
  101. memset(aucFrequencyTable, 0, sizeof(aucFrequencyTable));
  102. memset(asDeviceParametersList, 0, sizeof(asDeviceParametersList));
  103. usDeviceListSize = 0;
  104. eANTFSState = ANTFS_HOST_STATE_OFF;
  105. ResetHostState();
  106. // Debugging is initialized by DSIANTDevice (or ANTDevice in Managed Lib)
  107. if (DSIThread_MutexInit(&stMutexCriticalSection) != DSI_THREAD_ENONE)
  108. {
  109. bInitFailed = TRUE;
  110. }
  111. if (DSIThread_MutexInit(&stMutexResponseQueue) != DSI_THREAD_ENONE)
  112. {
  113. bInitFailed = TRUE;
  114. }
  115. if (DSIThread_CondInit(&stCondANTFSThreadExit) != DSI_THREAD_ENONE)
  116. {
  117. bInitFailed = TRUE;
  118. }
  119. if (DSIThread_CondInit(&stCondRequest) != DSI_THREAD_ENONE)
  120. {
  121. bInitFailed = TRUE;
  122. }
  123. if (DSIThread_CondInit(&stCondRxEvent) != DSI_THREAD_ENONE)
  124. {
  125. bInitFailed = TRUE;
  126. }
  127. if (DSIThread_CondInit(&stCondWaitForResponse) != DSI_THREAD_ENONE)
  128. {
  129. bInitFailed = TRUE;
  130. }
  131. //If the init fails, something is broken and nothing is going to work anyway
  132. //additionally the logging isn't even setup at this point
  133. //so we throw an exception so we know something is broken right away
  134. if(bInitFailed == TRUE)
  135. throw "ANTFS constructor: init failed";
  136. }
  137. ///////////////////////////////////////////////////////////////////////
  138. ANTFSHostChannel::~ANTFSHostChannel()
  139. {
  140. this->Close();
  141. if (bInitFailed == FALSE)
  142. {
  143. DSIThread_MutexDestroy(&stMutexCriticalSection);
  144. DSIThread_MutexDestroy(&stMutexResponseQueue);
  145. DSIThread_CondDestroy(&stCondANTFSThreadExit);
  146. DSIThread_CondDestroy(&stCondRequest);
  147. DSIThread_CondDestroy(&stCondRxEvent);
  148. DSIThread_CondDestroy(&stCondWaitForResponse);
  149. }
  150. }
  151. ///////////////////////////////////////////////////////////////////////
  152. UCHAR ANTFSHostChannel::GetVersion(UCHAR *pucVersionString_, UCHAR ucBufferSize_)
  153. {
  154. ucBufferSize_--; // Allow for NULL cstring termination.
  155. if (ucBufferSize_ > sizeof(acSwVersion))
  156. ucBufferSize_ = sizeof(acSwVersion);
  157. memcpy(pucVersionString_, acSwVersion, ucBufferSize_);
  158. pucVersionString_[ucBufferSize_++] = '\0'; // Terminate the string and increment the size.
  159. return ucBufferSize_; // Return the string size, including the terminator.
  160. }
  161. ///////////////////////////////////////////////////////////////////////
  162. BOOL ANTFSHostChannel::Init(DSIFramerANT* pclANT_, UCHAR ucChannel_)
  163. {
  164. if (bInitFailed == TRUE)
  165. {
  166. #if defined(DEBUG_FILE)
  167. DSIDebug::ThreadWrite("ANTFSHostChannel::Init(): bInitFailed == TRUE");
  168. #endif
  169. return FALSE;
  170. }
  171. ucChannelNumber = ucChannel_;
  172. pclANT = pclANT_;
  173. if(pclANT)
  174. {
  175. if(pclANT->GetCancelParameter() == (BOOL*) NULL) // If cancel parameter has not been configured in framer, use internal
  176. pclANT->SetCancelParameter(pbCancel);
  177. else // Use cancel parameter configured in framer
  178. pbCancel = pclANT->GetCancelParameter();
  179. }
  180. return ReInitDevice();
  181. }
  182. ///////////////////////////////////////////////////////////////////////
  183. void ANTFSHostChannel::GetCurrentConfig( ANTFS_CONFIG_PARAMETERS* pCfg_ )
  184. {
  185. memcpy( pCfg_, &cfgParams, sizeof( ANTFS_CONFIG_PARAMETERS ) );
  186. }
  187. ///////////////////////////////////////////////////////////////////////
  188. BOOL ANTFSHostChannel::SetCurrentConfig( const ANTFS_CONFIG_PARAMETERS* pCfg_ )
  189. {
  190. // validation can be performed here
  191. BOOL bValid = TRUE;
  192. // ignore the entire set if invalid
  193. if( bValid )
  194. {
  195. memcpy( &cfgParams, pCfg_, sizeof( ANTFS_CONFIG_PARAMETERS ) );
  196. }
  197. return bValid;
  198. }
  199. ///////////////////////////////////////////////////////////////////////
  200. void ANTFSHostChannel::SetChannelID(UCHAR ucDeviceType_, UCHAR ucTransmissionType_)
  201. {
  202. ucTheDeviceType = ucDeviceType_;
  203. ucTheTransmissionType = ucTransmissionType_;
  204. }
  205. ///////////////////////////////////////////////////////////////////////
  206. void ANTFSHostChannel::SetChannelPeriod(USHORT usChannelPeriod_)
  207. {
  208. usTheMessagePeriod = usChannelPeriod_;
  209. }
  210. ///////////////////////////////////////////////////////////////////////
  211. void ANTFSHostChannel::SetNetworkKey(UCHAR ucNetwork_, UCHAR ucNetworkkey[])
  212. {
  213. ucNetworkNumber = ucNetwork_;
  214. memcpy(aucTheNetworkkey,ucNetworkkey,8);
  215. }
  216. ///////////////////////////////////////////////////////////////////////
  217. void ANTFSHostChannel::SetProximitySearch(UCHAR ucSearchThreshold_)
  218. {
  219. if(ucSearchThreshold_ > 10) //Depending on device higher thresholds can be ignored, cause undefined consequences, or even have different functionality assigned to them, so we cap at the valid 0-10 range
  220. {
  221. #if defined(DEBUG_FILE)
  222. DSIDebug::ThreadWrite("ANTFSHostChannel::SetProximitySearch(): User threshold value > 10, setting to max 10.");
  223. #endif
  224. ucSearchThreshold_ = 10;
  225. }
  226. ucTheProxThreshold = ucSearchThreshold_;
  227. }
  228. ///////////////////////////////////////////////////////////////////////
  229. void ANTFSHostChannel::SetSerialNumber(ULONG ulSerialNumber_)
  230. {
  231. ulHostSerialNumber = ulSerialNumber_;
  232. }
  233. ///////////////////////////////////////////////////////////////////////
  234. BOOL ANTFSHostChannel::GetEnabled()
  235. {
  236. if(eANTFSState < ANTFS_HOST_STATE_DISCONNECTING)
  237. {
  238. return FALSE;
  239. }
  240. return TRUE;
  241. }
  242. ///////////////////////////////////////////////////////////////////////
  243. ANTFS_HOST_STATE ANTFSHostChannel::GetStatus(void)
  244. {
  245. return eANTFSState;
  246. }
  247. ///////////////////////////////////////////////////////////////////////
  248. UCHAR ANTFSHostChannel::GetConnectedDeviceBeaconAntfsState(void)
  249. {
  250. if (eANTFSState < ANTFS_HOST_STATE_CONNECTED)
  251. return REMOTE_DEVICE_BEACON_NOT_FOUND;
  252. else
  253. return ucFoundDeviceState;
  254. }
  255. ///////////////////////////////////////////////////////////////////////
  256. void ANTFSHostChannel::Close(void)
  257. {
  258. #if defined(DEBUG_FILE)
  259. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Closing ANTFS...");
  260. #endif
  261. DSIThread_MutexLock(&stMutexCriticalSection);
  262. // Stop the threads.
  263. bKillThread = TRUE;
  264. *pbCancel = TRUE;
  265. #if defined(DEBUG_FILE)
  266. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): SetEvent(stCondWaitForResponse).");
  267. #endif
  268. DSIThread_MutexLock(&stMutexResponseQueue);
  269. DSIThread_CondSignal(&stCondWaitForResponse);
  270. clResponseQueue.Clear();
  271. DSIThread_MutexUnlock(&stMutexResponseQueue);
  272. if (hANTFSThread)
  273. {
  274. if (bANTFSThreadRunning == TRUE)
  275. {
  276. #if defined(DEBUG_FILE)
  277. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): SetEvent(stCondRequest).");
  278. #endif
  279. DSIThread_CondSignal(&stCondRequest);
  280. #if defined(DEBUG_FILE)
  281. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): SetEvent(stCondRxEvent).");
  282. #endif
  283. DSIThread_CondSignal(&stCondRxEvent);
  284. #if defined(DEBUG_FILE)
  285. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Killing thread.");
  286. #endif
  287. if (DSIThread_CondTimedWait(&stCondANTFSThreadExit, &stMutexCriticalSection, 9000) != DSI_THREAD_ENONE)
  288. {
  289. #if defined(DEBUG_FILE)
  290. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Thread not dead.");
  291. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Forcing thread termination...");
  292. #endif
  293. DSIThread_DestroyThread(hANTFSThread);
  294. }
  295. else
  296. {
  297. #if defined(DEBUG_FILE)
  298. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Thread terminated successfully.");
  299. #endif
  300. }
  301. }
  302. DSIThread_ReleaseThreadID(hANTFSThread);
  303. hANTFSThread = (DSI_THREAD_ID)NULL;
  304. }
  305. DSIThread_MutexUnlock(&stMutexCriticalSection);
  306. if (bIgnoreListRunning)
  307. {
  308. #if defined(DEBUG_FILE)
  309. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Deleting Timer Queue Timer...");
  310. #endif
  311. delete pclQueueTimer;
  312. pclQueueTimer = (DSITimer*)NULL;
  313. DSIThread_MutexDestroy(&stMutexIgnoreListAccess);
  314. #if defined(DEBUG_FILE)
  315. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Deleted Timer Queue Timer.");
  316. #endif
  317. bIgnoreListRunning = FALSE;
  318. }
  319. eANTFSState = ANTFS_HOST_STATE_OFF;
  320. // Threads are cleaned, revert Cancel status.
  321. *pbCancel = FALSE;
  322. #if defined(DEBUG_FILE)
  323. DSIDebug::ThreadWrite("ANTFSHostChannel::Close(): Closed.");
  324. #endif
  325. if (pucTransferBufferDynamic)
  326. {
  327. if (pucTransferBuffer == pucTransferBufferDynamic)
  328. pucTransferBuffer = (UCHAR*)NULL;
  329. delete[] pucTransferBufferDynamic;
  330. pucTransferBufferDynamic = (UCHAR*)NULL;
  331. }
  332. }
  333. ///////////////////////////////////////////////////////////////////////
  334. void ANTFSHostChannel::Cancel(void)
  335. {
  336. DSIThread_MutexLock(&stMutexCriticalSection);
  337. *pbCancel = TRUE;
  338. DSIThread_CondSignal(&stCondRxEvent);
  339. DSIThread_CondSignal(&stCondRequest);
  340. DSIThread_MutexUnlock(&stMutexCriticalSection);
  341. return;
  342. }
  343. ///////////////////////////////////////////////////////////////////////
  344. void ANTFSHostChannel::ProcessDeviceNotification(ANT_DEVICE_NOTIFICATION eCode_, void* pvParameter_)
  345. {
  346. DSIThread_MutexLock(&stMutexCriticalSection);
  347. if(eCode_ != ANT_DEVICE_NOTIFICATION_RESET &&
  348. eCode_ != ANT_DEVICE_NOTIFICATION_SHUTDOWN)
  349. {
  350. #if defined(DEBUG_FILE)
  351. UCHAR aucString[256];
  352. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::ProcessDeviceNotification(): Unknown code %0", eCode_);
  353. DSIDebug::ThreadWrite((char *) aucString);
  354. #endif
  355. DSIThread_MutexUnlock(&stMutexCriticalSection);
  356. return;
  357. }
  358. if(eANTFSState <= ANTFS_HOST_STATE_IDLE)
  359. {
  360. // We do not need to do anything, since ANT-FS is already in idle state
  361. DSIThread_MutexUnlock(&stMutexCriticalSection);
  362. return;
  363. }
  364. if(pvParameter_ != NULL)
  365. {
  366. //Currently we don't have any codes we have data with, so log and ignore if we see something
  367. #if defined(DEBUG_FILE)
  368. DSIDebug::ThreadWrite("ANTFSHostChannel::ProcessDeviceNotification(): Received unrecognized pvParameter data");
  369. #endif
  370. }
  371. #if defined(DEBUG_FILE)
  372. DSIDebug::ThreadWrite("ANTFSHostChannel::ProcessDeviceNotification(): Resetting state...");
  373. #endif
  374. *pbCancel = TRUE;
  375. eANTFSRequest = ANTFS_REQUEST_INIT;
  376. DSIThread_CondSignal(&stCondRxEvent);
  377. DSIThread_CondSignal(&stCondRequest);
  378. DSIThread_MutexUnlock(&stMutexCriticalSection);
  379. return;
  380. }
  381. ///////////////////////////////////////////////////////////////////////
  382. #define MESG_CHANNEL_OFFSET 0
  383. #define MESG_EVENT_ID_OFFSET 1
  384. #define MESG_EVENT_CODE_OFFSET 2
  385. void ANTFSHostChannel::ProcessMessage(ANT_MESSAGE* pstMessage_, USHORT usMesgSize_)
  386. {
  387. UCHAR ucANTChannel;
  388. BOOL bProcessed = FALSE;
  389. // Check that we are in the correct state to receive messages
  390. if(!GetEnabled())
  391. return; // Only process ANT messages if ANT-FS is on
  392. if (usMesgSize_ < DSI_FRAMER_TIMEDOUT) //if the return isn't DSI_FRAMER_TIMEDOUT or DSI_FRAMER_ERROR
  393. {
  394. ucANTChannel = pstMessage_->aucData[MESG_CHANNEL_OFFSET] & CHANNEL_NUMBER_MASK;
  395. if(!FilterANTMessages(pstMessage_, ucANTChannel))
  396. return; // Wrong channel, do not process
  397. switch (pstMessage_->ucMessageID)
  398. {
  399. case MESG_RESPONSE_EVENT_ID:
  400. if (pstMessage_->aucData[MESG_EVENT_ID_OFFSET] != MESG_EVENT_ID) // this is a response
  401. {
  402. memcpy(aucResponseBuf, pstMessage_->aucData, MESG_RESPONSE_EVENT_SIZE);
  403. bProcessed = ANTProtocolEventProcess(ucANTChannel, MESG_RESPONSE_EVENT_ID);
  404. }
  405. else // this is an event
  406. {
  407. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  408. bProcessed = ANTChannelEventProcess(ucANTChannel, pstMessage_->aucData[MESG_EVENT_CODE_OFFSET]); // pass through any events not handled here
  409. }
  410. break;
  411. case MESG_BROADCAST_DATA_ID:
  412. //Call channel event function with Broadcast message code
  413. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  414. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_BROADCAST);
  415. break;
  416. case MESG_ACKNOWLEDGED_DATA_ID:
  417. //Call channel event function with Acknowledged message code
  418. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  419. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_ACKNOWLEDGED);
  420. break;
  421. case MESG_BURST_DATA_ID:
  422. //Call channel event function with Burst message code
  423. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  424. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_BURST_PACKET);
  425. break;
  426. case MESG_EXT_BROADCAST_DATA_ID:
  427. //Call channel event function with Broadcast message code
  428. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  429. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_EXT_BROADCAST);
  430. break;
  431. case MESG_EXT_ACKNOWLEDGED_DATA_ID:
  432. //Call channel event function with Acknowledged message code
  433. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  434. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_EXT_ACKNOWLEDGED);
  435. break;
  436. case MESG_EXT_BURST_DATA_ID:
  437. //Call channel event function with Burst message code
  438. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  439. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_EXT_BURST_PACKET);
  440. break;
  441. case MESG_RSSI_BROADCAST_DATA_ID:
  442. //Call channel event function with Broadcast message code
  443. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  444. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_RSSI_BROADCAST);
  445. break;
  446. case MESG_RSSI_ACKNOWLEDGED_DATA_ID:
  447. //Call channel event function with Acknowledged message code
  448. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  449. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_RSSI_ACKNOWLEDGED);
  450. break;
  451. case MESG_RSSI_BURST_DATA_ID:
  452. //Call channel event function with Burst message code
  453. memcpy(aucRxBuf, pstMessage_->aucData, usMesgSize_);
  454. bProcessed = ANTChannelEventProcess(ucANTChannel, EVENT_RX_RSSI_BURST_PACKET);
  455. break;
  456. default:
  457. memcpy(aucResponseBuf, pstMessage_->aucData, usMesgSize_);
  458. bProcessed = ANTProtocolEventProcess(ucANTChannel, pstMessage_->ucMessageID );
  459. break;
  460. }
  461. }
  462. return;
  463. }
  464. //////////////////////////////////////////////////////////////////////////////////
  465. // ANTFS Link Layer
  466. //////////////////////////////////////////////////////////////////////////////////
  467. ///////////////////////////////////////////////////////////////////////
  468. USHORT ANTFSHostChannel::AddSearchDevice(ANTFS_DEVICE_PARAMETERS *pstDeviceSearchMask_, ANTFS_DEVICE_PARAMETERS *pstDeviceParameters_)
  469. {
  470. USHORT usHandle;
  471. if (usDeviceListSize >= SEARCH_DEVICE_LIST_MAX_SIZE)
  472. return 0;
  473. // Find the next available handle.
  474. usHandle = 0;
  475. while (usHandle < usDeviceListSize)
  476. {
  477. if ((usHandle + 1) < asDeviceParametersList[usHandle].usHandle)
  478. break;
  479. usHandle++;
  480. }
  481. asDeviceParametersList[usDeviceListSize].usHandle = ++usHandle;
  482. asDeviceParametersList[usDeviceListSize].sDeviceParameters = *pstDeviceParameters_;
  483. asDeviceParametersList[usDeviceListSize].sDeviceSearchMask = *pstDeviceSearchMask_;
  484. usDeviceListSize++;
  485. qsort(asDeviceParametersList, usDeviceListSize, sizeof(DEVICE_PARAMETERS_ITEM), &DeviceParametersItemCompare);
  486. return usHandle;
  487. }
  488. ///////////////////////////////////////////////////////////////////////
  489. void ANTFSHostChannel::RemoveSearchDevice(USHORT usHandle_)
  490. {
  491. DEVICE_PARAMETERS_ITEM sDeviceParametersItem;
  492. DEVICE_PARAMETERS_ITEM *psDeviceParametersItem;
  493. if (usDeviceListSize == 0)
  494. return; // Nothing to do.
  495. // Find the handle in the list.
  496. sDeviceParametersItem.usHandle = usHandle_;
  497. psDeviceParametersItem =(DEVICE_PARAMETERS_ITEM *) bsearch(&sDeviceParametersItem, asDeviceParametersList, usDeviceListSize, sizeof(DEVICE_PARAMETERS_ITEM), &DeviceParametersItemCompare);
  498. if (psDeviceParametersItem != NULL)
  499. {
  500. psDeviceParametersItem->usHandle = MAX_USHORT; // Make it invalid.
  501. qsort(asDeviceParametersList, usDeviceListSize, sizeof(DEVICE_PARAMETERS_ITEM), &DeviceParametersItemCompare); // Sorting will cause the large invalid item to drop to the end of the list.
  502. usDeviceListSize--; // Reduce the size to reflect that the item has been removed.
  503. }
  504. }
  505. ///////////////////////////////////////////////////////////////////////
  506. void ANTFSHostChannel::ClearSearchDeviceList(void)
  507. {
  508. usDeviceListSize = 0;
  509. }
  510. ///////////////////////////////////////////////////////////////////////
  511. ANTFS_RETURN ANTFSHostChannel::SearchForDevice(UCHAR ucSearchRadioFrequency_, UCHAR ucConnectedRadioFrequency_, USHORT usRadioChannelID_, BOOL bUseRequestPage_)
  512. {
  513. DSIThread_MutexLock(&stMutexCriticalSection);
  514. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  515. {
  516. #if defined(DEBUG_FILE)
  517. DSIDebug::ThreadWrite("ANTFSHostChannel::SearchForDevice(): Request Busy.");
  518. #endif
  519. DSIThread_MutexUnlock(&stMutexCriticalSection);
  520. return ANTFS_RETURN_BUSY;
  521. }
  522. if (eANTFSState != ANTFS_HOST_STATE_IDLE)
  523. {
  524. #if defined(DEBUG_FILE)
  525. DSIDebug::ThreadWrite("ANTFSHostChannel::SearchForDevice(): Not in correct state.");
  526. #endif
  527. DSIThread_MutexUnlock(&stMutexCriticalSection);
  528. return ANTFS_RETURN_FAIL;
  529. }
  530. #if defined(DEBUG_FILE)
  531. DSIDebug::ThreadWrite("ANTFSHostChannel::SearchForDevice(): Search starting...");
  532. #endif
  533. ucSearchRadioFrequency = ucSearchRadioFrequency_;
  534. ucTransportFrequencySelection = ucConnectedRadioFrequency_;
  535. usRadioChannelID = usRadioChannelID_;
  536. bRequestPageEnabled = bUseRequestPage_;
  537. eANTFSRequest = ANTFS_REQUEST_SEARCH;
  538. DSIThread_CondSignal(&stCondRequest);
  539. DSIThread_MutexUnlock(&stMutexCriticalSection);
  540. #if defined(DEBUG_FILE)
  541. DSIDebug::ThreadWrite("ANTFSHostChannel::SearchForDevice(): Search request pending...");
  542. #endif
  543. return ANTFS_RETURN_PASS;
  544. }
  545. ///////////////////////////////////////////////////////////////////////
  546. BOOL ANTFSHostChannel::GetFoundDeviceParameters(ANTFS_DEVICE_PARAMETERS *pstDeviceParameters_, UCHAR *aucFriendlyName_, UCHAR *pucBufferSize_)
  547. {
  548. if (eANTFSState < ANTFS_HOST_STATE_CONNECTED)
  549. {
  550. #if defined(DEBUG_FILE)
  551. DSIDebug::ThreadWrite("ANTFSHostChannel::GetFoundDeviceParameters(): Not in correct state.");
  552. #endif
  553. return FALSE;
  554. }
  555. memcpy(pstDeviceParameters_, &stFoundDeviceParameters, sizeof(ANTFS_DEVICE_PARAMETERS));
  556. if (ucRemoteFriendlyNameLength < *pucBufferSize_)
  557. *pucBufferSize_ = ucRemoteFriendlyNameLength;
  558. memcpy(aucFriendlyName_, aucRemoteFriendlyName, *pucBufferSize_);
  559. *pucBufferSize_ = ucRemoteFriendlyNameLength;
  560. return TRUE;
  561. }
  562. ///////////////////////////////////////////////////////////////////////
  563. BOOL ANTFSHostChannel::GetFoundDeviceChannelID(USHORT *pusDeviceNumber_, UCHAR *pucDeviceType_, UCHAR *pucTransmitType_)
  564. {
  565. if (eANTFSState < ANTFS_HOST_STATE_CONNECTED)
  566. {
  567. #if defined(DEBUG_FILE)
  568. DSIDebug::ThreadWrite("ANTFSHostChannel::GetFoundDeviceChannelID(): Not in correct state.");
  569. #endif
  570. return FALSE;
  571. }
  572. if(pusDeviceNumber_ != NULL)
  573. *pusDeviceNumber_ = usFoundANTFSDeviceID;
  574. if(pucDeviceType_ != NULL)
  575. *pucDeviceType_ = ucFoundANTDeviceType;
  576. if(pucTransmitType_ != NULL)
  577. *pucTransmitType_ = ucFoundANTTransmitType;
  578. return TRUE;
  579. }
  580. ///////////////////////////////////////////////////////////////////////
  581. ANTFS_RETURN ANTFSHostChannel::RequestSession(UCHAR ucBroadcastRadioFrequency_, UCHAR ucConnectRadioFrequency_)
  582. {
  583. DSIThread_MutexLock(&stMutexCriticalSection);
  584. if(eANTFSRequest != ANTFS_REQUEST_NONE)
  585. {
  586. #if defined(DEBUG_FILE)
  587. DSIDebug::ThreadWrite("ANTFSHostChannel::RequestSession(): Request Busy.");
  588. #endif
  589. DSIThread_MutexUnlock(&stMutexCriticalSection);
  590. return ANTFS_RETURN_BUSY;
  591. }
  592. if(eANTFSState != ANTFS_HOST_STATE_IDLE)
  593. {
  594. #if defined(DEBUG_FILE)
  595. DSIDebug::ThreadWrite("ANTFSHostChannel::RequestSession(): Not in correct state.");
  596. #endif
  597. DSIThread_MutexUnlock(&stMutexCriticalSection);
  598. return ANTFS_RETURN_BUSY;
  599. }
  600. ucTransportLayerRadioFrequency = ucConnectRadioFrequency_;
  601. ucSearchRadioFrequency = ucBroadcastRadioFrequency_;
  602. eANTFSRequest = ANTFS_REQUEST_SESSION_REQ;
  603. DSIThread_CondSignal(&stCondRequest);
  604. DSIThread_MutexUnlock(&stMutexCriticalSection);
  605. #if defined(DEBUG_FILE)
  606. DSIDebug::ThreadWrite("ANTFSHostChannel::RequestSession(): Request for ANT-FS session pending...");
  607. #endif
  608. return ANTFS_RETURN_PASS;
  609. }
  610. ///////////////////////////////////////////////////////////////////////
  611. ANTFS_RETURN ANTFSHostChannel::Authenticate(UCHAR ucAuthenticationType_, UCHAR *pucAuthenticationString_, UCHAR ucLength_, UCHAR *pucResponseBuffer_, UCHAR *pucResponseBufferSize_, ULONG ulResponseTimeout_)
  612. {
  613. DSIThread_MutexLock(&stMutexCriticalSection);
  614. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  615. {
  616. #if defined(DEBUG_FILE)
  617. DSIDebug::ThreadWrite("ANTFSHostChannel::Authenticate(): Request Busy.");
  618. #endif
  619. DSIThread_MutexUnlock(&stMutexCriticalSection);
  620. return ANTFS_RETURN_BUSY;
  621. }
  622. if (eANTFSState != ANTFS_HOST_STATE_CONNECTED)
  623. {
  624. #if defined(DEBUG_FILE)
  625. DSIDebug::ThreadWrite("ANTFSHostChannel::Authenticate(): Not in correct state.");
  626. #endif
  627. DSIThread_MutexUnlock(&stMutexCriticalSection);
  628. return ANTFS_RETURN_FAIL;
  629. }
  630. ucTxPasswordLength = ucLength_;
  631. ucAuthType = ucAuthenticationType_;
  632. pucResponseBuffer = pucResponseBuffer_;
  633. pucResponseBufferSize = pucResponseBufferSize_;
  634. ulAuthResponseTimeout = ulResponseTimeout_;
  635. memcpy(aucTxPassword, pucAuthenticationString_, ucTxPasswordLength);
  636. //may switch on Auth type here...
  637. eANTFSRequest = ANTFS_REQUEST_AUTHENTICATE;
  638. DSIThread_CondSignal(&stCondRequest);
  639. DSIThread_MutexUnlock(&stMutexCriticalSection);
  640. return ANTFS_RETURN_PASS;
  641. }
  642. ///////////////////////////////////////////////////////////////////////
  643. ANTFS_RETURN ANTFSHostChannel::Disconnect(USHORT usBlackoutTime_, UCHAR ucDisconnectType_, UCHAR ucTimeDuration_, UCHAR ucAppSpecificDuration_)
  644. {
  645. ANTFS_RETURN eReturn = ANTFS_RETURN_PASS;
  646. DSIThread_MutexLock(&stMutexCriticalSection);
  647. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  648. {
  649. #if defined(DEBUG_FILE)
  650. DSIDebug::ThreadWrite("ANTFSHostChannel::Disconnect(): Request Busy.");
  651. #endif
  652. DSIThread_MutexUnlock(&stMutexCriticalSection);
  653. return ANTFS_RETURN_BUSY;
  654. }
  655. if (eANTFSState < ANTFS_HOST_STATE_REQUESTING_SESSION)
  656. {
  657. #if defined(DEBUG_FILE)
  658. DSIDebug::ThreadWrite("ANTFSHostChannel::Disconnect(): Already disconnected.");
  659. #endif
  660. DSIThread_MutexUnlock(&stMutexCriticalSection);
  661. return ANTFS_RETURN_FAIL;
  662. }
  663. if (eANTFSState >= ANTFS_HOST_STATE_CONNECTED)
  664. {
  665. if (usBlackoutTime_ )
  666. {
  667. if(Blackout(usFoundANTFSDeviceID, usFoundANTFSManufacturerID, usFoundANTFSDeviceType, usBlackoutTime_) == FALSE)
  668. {
  669. #if defined(DEBUG_FILE)
  670. DSIDebug::ThreadWrite("ANTFSHostChannel::Disconnect(): Failed adding device to ignore list.");
  671. #endif
  672. eReturn = ANTFS_RETURN_FAIL;
  673. }
  674. }
  675. }
  676. ucDisconnectType = ucDisconnectType_;
  677. ucUndiscoverableTimeDuration = ucTimeDuration_;
  678. ucUndiscoverableAppSpecificDuration = ucAppSpecificDuration_;
  679. eANTFSRequest = ANTFS_REQUEST_DISCONNECT;
  680. DSIThread_CondSignal(&stCondRequest);
  681. DSIThread_MutexUnlock(&stMutexCriticalSection);
  682. return eReturn;
  683. }
  684. ///////////////////////////////////////////////////////////////////////
  685. ANTFS_RETURN ANTFSHostChannel::SwitchFrequency(UCHAR ucRadioFrequency_, UCHAR ucChannelPeriod_)
  686. {
  687. DSIThread_MutexLock(&stMutexCriticalSection);
  688. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  689. {
  690. #if defined(DEBUG_FILE)
  691. DSIDebug::ThreadWrite("ANTFSHostChannel::SwitchFrequency(): Request Busy.");
  692. #endif
  693. DSIThread_MutexUnlock(&stMutexCriticalSection);
  694. return ANTFS_RETURN_BUSY;
  695. }
  696. if (eANTFSState != ANTFS_HOST_STATE_TRANSPORT)
  697. {
  698. #if defined(DEBUG_FILE)
  699. DSIDebug::ThreadWrite("ANTFSHostChannel::SwitchFrequency(): Not in transport state.");
  700. #endif
  701. DSIThread_MutexUnlock(&stMutexCriticalSection);
  702. return ANTFS_RETURN_FAIL;
  703. }
  704. ucTransportFrequencySelection = ucRadioFrequency_;
  705. ucTransportChannelPeriodSelection = ucChannelPeriod_;
  706. eANTFSRequest = ANTFS_REQUEST_CHANGE_LINK;
  707. DSIThread_CondSignal(&stCondRequest);
  708. DSIThread_MutexUnlock(&stMutexCriticalSection);
  709. return ANTFS_RETURN_PASS;
  710. }
  711. ///////////////////////////////////////////////////////////////////////
  712. ANTFS_RETURN ANTFSHostChannel::Download(USHORT usFileIndex_, ULONG ulDataOffset_, ULONG ulMaxDataLength_, ULONG ulMaxBlockSize_)
  713. {
  714. DSIThread_MutexLock(&stMutexCriticalSection);
  715. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  716. {
  717. #if defined(DEBUG_FILE)
  718. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Request Busy.");
  719. #endif
  720. DSIThread_MutexUnlock(&stMutexCriticalSection);
  721. return ANTFS_RETURN_BUSY;
  722. }
  723. if (eANTFSState != ANTFS_HOST_STATE_TRANSPORT)
  724. {
  725. #if defined(DEBUG_FILE)
  726. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Not in transport state.");
  727. #endif
  728. DSIThread_MutexUnlock(&stMutexCriticalSection);
  729. return ANTFS_RETURN_FAIL;
  730. }
  731. bTransfer = FALSE;
  732. ulTransferTotalBytesRemaining = 0;
  733. ulTransferBytesInBlock = 0;
  734. usTransferDataFileIndex = usFileIndex_;
  735. ulTransferDataOffset = ulDataOffset_;
  736. ulTransferByteSize = ulMaxDataLength_;
  737. ulHostBlockSize = ulMaxBlockSize_;
  738. #if defined(DEBUG_FILE)
  739. {
  740. char szString[256];
  741. SNPRINTF(szString, 256, "ANTFSHostChannel::Download():\n usTransferDataFileIndex = %u.\n ulTransferDataOffset = %lu.\n ulTransferByteSize = %lu.",
  742. usTransferDataFileIndex, ulTransferDataOffset, ulTransferByteSize);
  743. DSIDebug::ThreadWrite(szString);
  744. }
  745. #endif
  746. if ((usFoundANTFSManufacturerID == 1) && (usFoundANTFSDeviceType == 782))
  747. bLargeData = FALSE; // !! Forerunner 50 only -- obsolete for all other devices.
  748. else
  749. bLargeData = TRUE;
  750. eANTFSRequest = ANTFS_REQUEST_DOWNLOAD;
  751. DSIThread_CondSignal(&stCondRequest);
  752. DSIThread_MutexUnlock(&stMutexCriticalSection);
  753. return ANTFS_RETURN_PASS;
  754. }
  755. ///////////////////////////////////////////////////////////////////////
  756. ANTFS_RETURN ANTFSHostChannel::Upload(USHORT usFileIndex_, ULONG ulDataOffset_, ULONG ulDataLength_, void *pvData_, BOOL bForceOffset_, ULONG ulMaxBlockSize_)
  757. {
  758. DSIThread_MutexLock(&stMutexCriticalSection);
  759. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  760. {
  761. #if defined(DEBUG_FILE)
  762. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Request Busy.");
  763. #endif
  764. DSIThread_MutexUnlock(&stMutexCriticalSection);
  765. return ANTFS_RETURN_BUSY;
  766. }
  767. if (eANTFSState != ANTFS_HOST_STATE_TRANSPORT)
  768. {
  769. #if defined(DEBUG_FILE)
  770. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Not in transport state.");
  771. #endif
  772. DSIThread_MutexUnlock(&stMutexCriticalSection);
  773. return ANTFS_RETURN_FAIL;
  774. }
  775. bTransfer = FALSE;
  776. ulUploadIndexProgress = 0;
  777. usTransferDataFileIndex = usFileIndex_;
  778. ulTransferDataOffset = ulDataOffset_;
  779. ulTransferByteSize = ulDataLength_;
  780. pucUploadData = (UCHAR*)pvData_;
  781. bForceUploadOffset = bForceOffset_;
  782. if (ulDataOffset_)
  783. bForceUploadOffset = TRUE; //Force the offset if the app is giving us an initial offset other than 0.
  784. if(ulMaxBlockSize_ == 0)
  785. ulHostBlockSize = MAX_ULONG;
  786. else
  787. ulHostBlockSize = ulMaxBlockSize_;
  788. #if 0
  789. if ((ulTransferDataOffset > MAX_USHORT) || (ulTransferByteSize > MAX_USHORT))
  790. bLargeData = TRUE;
  791. else
  792. bLargeData = FALSE;
  793. #endif
  794. bLargeData = TRUE;
  795. eANTFSRequest = ANTFS_REQUEST_UPLOAD;
  796. DSIThread_CondSignal(&stCondRequest);
  797. DSIThread_MutexUnlock(&stMutexCriticalSection);
  798. return ANTFS_RETURN_PASS;
  799. }
  800. ///////////////////////////////////////////////////////////////////////
  801. ANTFS_RETURN ANTFSHostChannel::ManualTransfer(USHORT usFileIndex_, ULONG ulDataOffset_, ULONG ulDataLength_, void *pvData_)
  802. {
  803. DSIThread_MutexLock(&stMutexCriticalSection);
  804. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  805. {
  806. #if defined(DEBUG_FILE)
  807. DSIDebug::ThreadWrite("ANTFSHostChannel::ManualTransfer(): Request Busy.");
  808. #endif
  809. DSIThread_MutexUnlock(&stMutexCriticalSection);
  810. return ANTFS_RETURN_BUSY;
  811. }
  812. if (eANTFSState != ANTFS_HOST_STATE_TRANSPORT)
  813. {
  814. #if defined(DEBUG_FILE)
  815. DSIDebug::ThreadWrite("ANTFSHostChannel::ManualTransfer(): Not in transport state.");
  816. #endif
  817. DSIThread_MutexUnlock(&stMutexCriticalSection);
  818. return ANTFS_RETURN_FAIL;
  819. }
  820. if (ulDataOffset_ >= DIRECT_TRANSFER_SIZE)
  821. {
  822. #if defined(DEBUG_FILE)
  823. DSIDebug::ThreadWrite("ANTFSHostChannel::ManualTransfer(): ulDataOffset) too large.");
  824. #endif
  825. DSIThread_MutexUnlock(&stMutexCriticalSection);
  826. return ANTFS_RETURN_FAIL;
  827. }
  828. if ((ulDataLength_ > DIRECT_TRANSFER_SIZE) || (ulDataLength_ == 0))
  829. {
  830. #if defined(DEBUG_FILE)
  831. DSIDebug::ThreadWrite("ANTFSHostChannel::ManualTransfer(): ulDataLength_ is 0 or too large.");
  832. #endif
  833. DSIThread_MutexUnlock(&stMutexCriticalSection);
  834. return ANTFS_RETURN_FAIL;
  835. }
  836. memcpy(&aucSendDirectBuffer[8], pvData_, ulDataLength_); //pvData_ must be little endian when sent to device!
  837. memset(&aucSendDirectBuffer[ulDataLength_ + 8], 0, 8 - (ulDataLength_ % 8) ); // Clear the rest of the last payload packet.
  838. #if defined(DEBUG_FILE)
  839. {
  840. char acString[256];
  841. SNPRINTF(acString, 256, "ANTFSHostChannel::ManualTransfer(): ulDataLength_ = %lu; ulDataOffset_ = %lu.", ulDataLength_, ulDataOffset_);
  842. DSIDebug::ThreadWrite(acString);
  843. }
  844. #endif
  845. bTransfer = FALSE;
  846. ulUploadIndexProgress = 0;
  847. ulTransferTotalBytesRemaining = 0;
  848. usTransferDataFileIndex = usFileIndex_;
  849. ulTransferDataOffset = ulDataOffset_;
  850. ulTransferByteSize = ulDataLength_;
  851. bLargeData = FALSE;
  852. eANTFSRequest = ANTFS_REQUEST_MANUAL_TRANSFER;
  853. DSIThread_CondSignal(&stCondRequest);
  854. DSIThread_MutexUnlock(&stMutexCriticalSection);
  855. return ANTFS_RETURN_PASS;
  856. }
  857. ///////////////////////////////////////////////////////////////////////
  858. BOOL ANTFSHostChannel::GetDownloadStatus(ULONG *pulByteProgress_, ULONG *pulTotalLength_)
  859. {
  860. ULONG ulOffset;
  861. if (ulTransferTotalBytesRemaining == 0)
  862. {
  863. #if defined(DEBUG_FILE)
  864. DSIDebug::ThreadWrite("ANTFSHostChannel::GetDownloadStatus(): Download not in progress.");
  865. #endif
  866. *pulTotalLength_ = 10;
  867. *pulByteProgress_ = 0;
  868. return FALSE;
  869. }
  870. *pulTotalLength_ = ulTransferTotalBytesRemaining;
  871. // The first 8 / 16 bytes of the array form the response packet.
  872. // Subtract them from the length count.
  873. if (bLargeData)
  874. ulOffset = 16;
  875. else
  876. ulOffset = 8;
  877. if (ulTransferArrayIndex >= ulOffset)
  878. *pulByteProgress_ = ulTransferArrayIndex - ulOffset;
  879. else
  880. *pulByteProgress_ = 0;
  881. *pulByteProgress_ += ulTransferDataOffset;
  882. if (*pulByteProgress_ > *pulTotalLength_) //the index can go beyond the total length because we transfer with 8 byte packets
  883. *pulByteProgress_ = *pulTotalLength_;
  884. return TRUE;
  885. }
  886. ///////////////////////////////////////////////////////////////////////
  887. BOOL ANTFSHostChannel::GetTransferData(ULONG *pulDataSize_ , void *pvData_)
  888. {
  889. ULONG ulLength;
  890. int iOffset;
  891. if ((!bTransfer) || (pucTransferBufferDynamic == NULL))
  892. {
  893. #if defined(DEBUG_FILE)
  894. DSIDebug::ThreadWrite("ANTFSHostChannel::GetTransferData(): No valid data.");
  895. #endif
  896. return FALSE;
  897. }
  898. if (bLargeData)
  899. iOffset = 16;
  900. else
  901. iOffset = 8;
  902. ulLength = ulTransferArrayIndex - iOffset;
  903. if (ulLength > ulTransferTotalBytesRemaining)
  904. ulLength = ulTransferTotalBytesRemaining;
  905. if (pvData_ != NULL)
  906. memcpy(pvData_, pucTransferBufferDynamic + iOffset, ulLength);
  907. if (pulDataSize_ != NULL)
  908. *pulDataSize_ = ulLength;
  909. return TRUE;
  910. }
  911. ///////////////////////////////////////////////////////////////////////
  912. BOOL ANTFSHostChannel::RecoverTransferData(ULONG *pulDataSize_ , void *pvData_)
  913. {
  914. ULONG ulLength;
  915. int iOffset;
  916. if (pucTransferBufferDynamic == NULL)
  917. {
  918. #if defined(DEBUG_FILE)
  919. DSIDebug::ThreadWrite("ANTFSHostChannel::GetTransferData(): No valid data.");
  920. #endif
  921. return FALSE;
  922. }
  923. if (bLargeData)
  924. iOffset = 16;
  925. else
  926. iOffset = 8;
  927. ulLength = ulTransferArrayIndex - iOffset;
  928. if (ulLength > ulTransferTotalBytesRemaining)
  929. ulLength = ulTransferTotalBytesRemaining;
  930. if (pvData_ != NULL)
  931. memcpy(pvData_, pucTransferBufferDynamic + iOffset, ulLength);
  932. if (pulDataSize_ != NULL)
  933. *pulDataSize_ = ulLength;
  934. return TRUE;
  935. }
  936. ///////////////////////////////////////////////////////////////////////
  937. BOOL ANTFSHostChannel::GetUploadStatus(ULONG *pulByteProgress_, ULONG *pulTotalLength_)
  938. {
  939. if (ulTransferByteSize == 0)
  940. {
  941. #if defined(DEBUG_FILE)
  942. DSIDebug::ThreadWrite("ANTFSHostChannel::GetDownloadStatus(): Upload not in progress.");
  943. #endif
  944. *pulTotalLength_ = 10;
  945. *pulByteProgress_ = 0;
  946. return FALSE;
  947. }
  948. *pulTotalLength_ = ulTransferByteSize;
  949. *pulByteProgress_ = ulUploadIndexProgress;
  950. if (*pulByteProgress_ >= 16) //take off the extra 8 byte overhead
  951. *pulByteProgress_ = *pulByteProgress_ - 16;
  952. return TRUE;
  953. }
  954. ///////////////////////////////////////////////////////////////////////
  955. ANTFS_RETURN ANTFSHostChannel::EraseData(USHORT usDataFileIndex_)
  956. {
  957. DSIThread_MutexLock(&stMutexCriticalSection);
  958. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  959. {
  960. #if defined(DEBUG_FILE)
  961. DSIDebug::ThreadWrite("ANTFSHostChannel::EraseData(): Request Busy.");
  962. #endif
  963. DSIThread_MutexUnlock(&stMutexCriticalSection);
  964. return ANTFS_RETURN_BUSY;
  965. }
  966. if (eANTFSState != ANTFS_HOST_STATE_TRANSPORT)
  967. {
  968. #if defined(DEBUG_FILE)
  969. DSIDebug::ThreadWrite("ANTFSHostChannel::EraseData(): Not in transport state.");
  970. #endif
  971. DSIThread_MutexUnlock(&stMutexCriticalSection);
  972. return ANTFS_RETURN_FAIL;
  973. }
  974. bTransfer = FALSE;
  975. usTransferDataFileIndex = usDataFileIndex_;
  976. eANTFSRequest = ANTFS_REQUEST_ERASE;
  977. DSIThread_CondSignal(&stCondRequest);
  978. DSIThread_MutexUnlock(&stMutexCriticalSection);
  979. return ANTFS_RETURN_PASS;
  980. }
  981. //////////////////////////////////////////////////////////////////////
  982. BOOL ANTFSHostChannel::EnablePing(BOOL bEnable_)
  983. {
  984. bPingEnabled = bEnable_;
  985. return TRUE;
  986. }
  987. //////////////////////////////////////////////////////////////////////////////////
  988. // Private Functions
  989. //////////////////////////////////////////////////////////////////////////////////
  990. DSI_THREAD_RETURN ANTFSHostChannel::ANTFSThreadStart(void *pvParameter_)
  991. {
  992. #if defined(DEBUG_FILE)
  993. DSIDebug::ThreadInit("ANTFSHost");
  994. #endif
  995. ((ANTFSHostChannel *)pvParameter_)->ANTFSThread();
  996. return 0;
  997. }
  998. ///////////////////////////////////////////////////////////////////////
  999. // ANTFS Task Thread
  1000. ///////////////////////////////////////////////////////////////////////
  1001. void ANTFSHostChannel::ANTFSThread(void)
  1002. {
  1003. ANTFS_HOST_RESPONSE eResponse;
  1004. BOOL bPingNow = FALSE;
  1005. bANTFSThreadRunning = TRUE;
  1006. while (bKillThread == FALSE)
  1007. {
  1008. eResponse = ANTFS_HOST_RESPONSE_NONE;
  1009. #if defined(DEBUG_FILE)
  1010. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Awaiting Requests...");
  1011. #endif
  1012. DSIThread_MutexLock(&stMutexCriticalSection);
  1013. if (*pbCancel == TRUE) //If there was a cancel, then return ANTFS_HOST_RESPONSE_CANCEL_DONE when we've reached this point
  1014. {
  1015. *pbCancel = FALSE;
  1016. if (eANTFSRequest != ANTFS_REQUEST_INIT && eANTFSRequest != ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1017. {
  1018. eANTFSRequest = ANTFS_REQUEST_NONE; //Clear any other request
  1019. }
  1020. AddResponse(ANTFS_HOST_RESPONSE_CANCEL_DONE);
  1021. }
  1022. if ((eANTFSRequest == ANTFS_REQUEST_NONE) && (bKillThread == FALSE))
  1023. {
  1024. UCHAR ucResult = DSIThread_CondTimedWait(&stCondRequest, &stMutexCriticalSection, 2000);
  1025. if (ucResult != DSI_THREAD_ENONE)
  1026. {
  1027. #if defined(DEBUG_FILE)
  1028. if(ucResult == DSI_THREAD_EOTHER)
  1029. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): CondTimedWait() Failed!");
  1030. #endif
  1031. if ((eANTFSRequest == ANTFS_REQUEST_NONE) && (bPingEnabled == TRUE) && (eANTFSState >= ANTFS_HOST_STATE_CONNECTED))
  1032. {
  1033. bPingNow = TRUE;
  1034. #if defined(DEBUG_FILE)
  1035. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Requesting Ping.");
  1036. #endif
  1037. }
  1038. }
  1039. }
  1040. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1041. if (bPingNow == TRUE)
  1042. {
  1043. bPingNow = FALSE;
  1044. Ping();
  1045. }
  1046. if (bKillThread)
  1047. break;
  1048. if (eANTFSRequest != ANTFS_REQUEST_NONE)
  1049. {
  1050. #if defined(DEBUG_FILE)
  1051. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Request received");
  1052. #endif
  1053. switch (eANTFSRequest)
  1054. {
  1055. case ANTFS_REQUEST_INIT:
  1056. {
  1057. #if defined(DEBUG_FILE)
  1058. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Idle.");
  1059. #endif
  1060. ResetHostState();
  1061. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1062. eResponse = ANTFS_HOST_RESPONSE_INIT_PASS;
  1063. } // ANTFS_REQUEST_INIT
  1064. break;
  1065. case ANTFS_REQUEST_SESSION:
  1066. {
  1067. RETURN_STATUS eReturn;
  1068. #if defined(DEBUG_FILE)
  1069. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Requesting ANT-FS session...");
  1070. #endif
  1071. eANTFSState = ANTFS_HOST_STATE_REQUESTING_SESSION;
  1072. eReturn = AttemptRequestSession();
  1073. if (eReturn == RETURN_SERIAL_ERROR)
  1074. {
  1075. #if defined(DEBUG_FILE)
  1076. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Session request serial error.");
  1077. #endif
  1078. DSIThread_MutexLock(&stMutexCriticalSection);
  1079. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1080. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1081. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1082. }
  1083. else if (eReturn == RETURN_FAIL)
  1084. {
  1085. #if defined(DEBUG_FILE)
  1086. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Session request failed.");
  1087. #endif
  1088. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1089. eResponse = ANTFS_HOST_RESPONSE_REQUEST_SESSION_FAIL;
  1090. }
  1091. else if (eReturn == RETURN_STOP)
  1092. {
  1093. #if defined(DEBUG_FILE)
  1094. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Session request stopped.");
  1095. #endif
  1096. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1097. }
  1098. else if (eReturn == RETURN_PASS)
  1099. {
  1100. RETURN_STATUS eConnectStatus;
  1101. #if defined(DEBUG_FILE)
  1102. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connecting...");
  1103. #endif
  1104. eConnectStatus = AttemptConnect();
  1105. if (eConnectStatus == RETURN_FAIL)
  1106. {
  1107. #if defined(DEBUG_FILE)
  1108. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connect failed.");
  1109. #endif
  1110. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1111. eResponse = ANTFS_HOST_RESPONSE_REQUEST_SESSION_FAIL;
  1112. }
  1113. else if (eConnectStatus == RETURN_SERIAL_ERROR)
  1114. {
  1115. #if defined(DEBUG_FILE)
  1116. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connect rejected.");
  1117. #endif
  1118. DSIThread_MutexLock(&stMutexCriticalSection);
  1119. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1120. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1121. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1122. }
  1123. else if (eConnectStatus == RETURN_PASS)
  1124. {
  1125. // No need to request the serial number in this case, as
  1126. // we already know we are connected to the right device
  1127. eANTFSState = ANTFS_HOST_STATE_CONNECTED;
  1128. eResponse = ANTFS_HOST_RESPONSE_CONNECT_PASS;
  1129. }
  1130. else if (eConnectStatus == RETURN_STOP)
  1131. {
  1132. #if defined(DEBUG_FILE)
  1133. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Session request stopped.");
  1134. #endif
  1135. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1136. }
  1137. else
  1138. {
  1139. #if defined(DEBUG_FILE)
  1140. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Session request failed.");
  1141. #endif
  1142. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1143. eResponse = ANTFS_HOST_RESPONSE_REQUEST_SESSION_FAIL;
  1144. }
  1145. }
  1146. } // ANTFS_REQUEST_SESSION
  1147. break;
  1148. case ANTFS_REQUEST_SEARCH:
  1149. {
  1150. RETURN_STATUS eReturn;
  1151. eANTFSState = ANTFS_HOST_STATE_SEARCHING;
  1152. #if defined(DEBUG_FILE)
  1153. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Searching...");
  1154. #endif
  1155. eReturn = AttemptSearch();
  1156. if (eReturn == RETURN_SERIAL_ERROR)
  1157. {
  1158. #if defined(DEBUG_FILE)
  1159. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Search serial error.");
  1160. #endif
  1161. DSIThread_MutexLock(&stMutexCriticalSection);
  1162. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1163. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1164. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1165. }
  1166. else if (eReturn == RETURN_FAIL)
  1167. {
  1168. #if defined(DEBUG_FILE)
  1169. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Search failed.");
  1170. #endif
  1171. //eANTFSRequest should still be ANTFS_REQUEST_SEARCH; // Keep searching.
  1172. //stay in searching state so we keep retrying
  1173. }
  1174. else if (eReturn == RETURN_STOP)
  1175. {
  1176. RETURN_STATUS eDisconnectStatus;
  1177. eDisconnectStatus = AttemptDisconnect();
  1178. if(eDisconnectStatus == RETURN_PASS)
  1179. {
  1180. #if defined(DEBUG_FILE)
  1181. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Search stopped.");
  1182. #endif
  1183. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1184. //eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS;
  1185. }
  1186. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1187. {
  1188. #if defined(DEBUG_FILE)
  1189. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error stopping search.");
  1190. #endif
  1191. DSIThread_MutexLock(&stMutexCriticalSection);
  1192. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1193. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1194. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1195. }
  1196. }
  1197. else if (eReturn == RETURN_PASS)
  1198. {
  1199. RETURN_STATUS eConnectStatus;
  1200. #if defined(DEBUG_FILE)
  1201. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connecting...");
  1202. #endif
  1203. eConnectStatus = AttemptConnect();
  1204. if (eConnectStatus == RETURN_FAIL)
  1205. {
  1206. #if defined(DEBUG_FILE)
  1207. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connect failed.");
  1208. #endif
  1209. //eANTFSRequest should still be ANTFS_REQUEST_SEARCH; // Keep searching.
  1210. eANTFSState = ANTFS_HOST_STATE_SEARCHING;
  1211. }
  1212. else if (eConnectStatus == RETURN_SERIAL_ERROR)
  1213. {
  1214. #if defined(DEBUG_FILE)
  1215. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connect rejected.");
  1216. #endif
  1217. DSIThread_MutexLock(&stMutexCriticalSection);
  1218. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1219. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1220. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1221. }
  1222. else
  1223. {
  1224. #if defined(DEBUG_FILE)
  1225. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Requesting ID...");
  1226. #endif
  1227. //eANTFSState = ANTFS_HOST_STATE_REQUESTING_ID;
  1228. //We do not want to set the state to connected until after we have sucessfully retrived the serial number
  1229. ucTxPasswordLength = 0;
  1230. ucAuthType = AUTH_COMMAND_REQ_SERIAL_NUM;
  1231. pucResponseBuffer = aucRemoteFriendlyName;
  1232. ucRemoteFriendlyNameLength = sizeof(aucRemoteFriendlyName);
  1233. pucResponseBufferSize = &ucRemoteFriendlyNameLength;
  1234. ulAuthResponseTimeout = cfgParams.ul_cfg_auth_timeout;
  1235. eReturn = AttemptAuthenticate();
  1236. if ((eReturn == RETURN_PASS) || (eReturn == RETURN_NA))
  1237. {
  1238. #if defined(DEBUG_FILE)
  1239. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connected.");
  1240. #endif
  1241. //grab and save the remote serial number
  1242. stFoundDeviceParameters.ulDeviceID = Convert_Bytes_To_ULONG(aucTransferBufferFixed[AUTH_REMOTE_SERIAL_NUMBER_OFFSET + 3],
  1243. aucTransferBufferFixed[AUTH_REMOTE_SERIAL_NUMBER_OFFSET + 2],
  1244. aucTransferBufferFixed[AUTH_REMOTE_SERIAL_NUMBER_OFFSET + 1],
  1245. aucTransferBufferFixed[AUTH_REMOTE_SERIAL_NUMBER_OFFSET]);
  1246. if (IsDeviceMatched(&stFoundDeviceParameters, FALSE) == TRUE)
  1247. {
  1248. eANTFSState = ANTFS_HOST_STATE_CONNECTED;
  1249. eResponse = ANTFS_HOST_RESPONSE_CONNECT_PASS;
  1250. }
  1251. else
  1252. {
  1253. //possibly blackout the device for a period of time here.
  1254. #if defined(DEBUG_FILE)
  1255. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Full ID is not a match.");
  1256. #endif
  1257. RETURN_STATUS eDisconnectStatus;
  1258. eDisconnectStatus = AttemptDisconnect();
  1259. if(eDisconnectStatus == RETURN_PASS)
  1260. {
  1261. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1262. }
  1263. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1264. {
  1265. #if defined(DEBUG_FILE)
  1266. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error stopping search.");
  1267. #endif
  1268. DSIThread_MutexLock(&stMutexCriticalSection);
  1269. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1270. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1271. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1272. }
  1273. }
  1274. }
  1275. else if (eReturn == RETURN_STOP)
  1276. {
  1277. RETURN_STATUS eDisconnectStatus;
  1278. #if defined(DEBUG_FILE)
  1279. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Search stopped.");
  1280. #endif
  1281. eDisconnectStatus = AttemptDisconnect();
  1282. if(eDisconnectStatus == RETURN_PASS)
  1283. {
  1284. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1285. //eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS;
  1286. }
  1287. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1288. {
  1289. #if defined(DEBUG_FILE)
  1290. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error getting ID.");
  1291. #endif
  1292. DSIThread_MutexLock(&stMutexCriticalSection);
  1293. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1294. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1295. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1296. }
  1297. }
  1298. else
  1299. {
  1300. RETURN_STATUS eDisconnectStatus;
  1301. #if defined(DEBUG_FILE)
  1302. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Get ID failed.");
  1303. #endif
  1304. eDisconnectStatus = AttemptDisconnect();
  1305. if(eDisconnectStatus == RETURN_PASS)
  1306. {
  1307. // Keep searching.
  1308. eANTFSState = ANTFS_HOST_STATE_SEARCHING;
  1309. }
  1310. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1311. {
  1312. #if defined(DEBUG_FILE)
  1313. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error getting ID.");
  1314. #endif
  1315. DSIThread_MutexLock(&stMutexCriticalSection);
  1316. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1317. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1318. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1319. }
  1320. }
  1321. }
  1322. }
  1323. }
  1324. break;
  1325. case ANTFS_REQUEST_AUTHENTICATE:
  1326. {
  1327. RETURN_STATUS eReturn;
  1328. eANTFSState = ANTFS_HOST_STATE_AUTHENTICATING;
  1329. eResponse = ANTFS_HOST_RESPONSE_AUTHENTICATE_FAIL; //set a default response
  1330. #if defined(DEBUG_FILE)
  1331. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Authenticating...");
  1332. #endif
  1333. eReturn = AttemptAuthenticate();
  1334. pucResponseBuffer = (UCHAR*)NULL; //clear response buffer pointers
  1335. pucResponseBufferSize = (UCHAR*)NULL;
  1336. if (eReturn == RETURN_PASS)
  1337. {
  1338. #if defined(DEBUG_FILE)
  1339. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Authentication passed.");
  1340. #endif
  1341. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1342. eResponse = ANTFS_HOST_RESPONSE_AUTHENTICATE_PASS;
  1343. }
  1344. else if (eReturn == RETURN_FAIL)
  1345. {
  1346. #if defined(DEBUG_FILE)
  1347. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Authentication failed.");
  1348. #endif
  1349. IncFreqStaleCount(MAJOR_STALE_COUNT);
  1350. eANTFSState = ANTFS_HOST_STATE_CONNECTED;
  1351. eResponse = ANTFS_HOST_RESPONSE_AUTHENTICATE_FAIL;
  1352. }
  1353. else if (eReturn == RETURN_REJECT)
  1354. {
  1355. #if defined(DEBUG_FILE)
  1356. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Authentication rejected.");
  1357. #endif
  1358. eANTFSState = ANTFS_HOST_STATE_CONNECTED;
  1359. eResponse = ANTFS_HOST_RESPONSE_AUTHENTICATE_REJECT;
  1360. }
  1361. else if (eReturn == RETURN_SERIAL_ERROR)
  1362. {
  1363. #if defined(DEBUG_FILE)
  1364. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Authentication serial error.");
  1365. #endif
  1366. DSIThread_MutexLock(&stMutexCriticalSection);
  1367. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1368. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1369. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1370. }
  1371. else if (eReturn == RETURN_STOP)
  1372. {
  1373. #if defined(DEBUG_FILE)
  1374. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Authentication stopped.");
  1375. #endif
  1376. eANTFSState = ANTFS_HOST_STATE_CONNECTED;
  1377. //eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS);
  1378. }
  1379. else //RETURN_NA
  1380. {
  1381. #if defined(DEBUG_FILE)
  1382. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Authentication NA.");
  1383. #endif
  1384. eANTFSState = ANTFS_HOST_STATE_CONNECTED;
  1385. eResponse = ANTFS_HOST_RESPONSE_AUTHENTICATE_NA;
  1386. }
  1387. }
  1388. break;
  1389. case ANTFS_REQUEST_DISCONNECT:
  1390. {
  1391. RETURN_STATUS eReturn;
  1392. eANTFSState = ANTFS_HOST_STATE_DISCONNECTING;
  1393. #if defined(DEBUG_FILE)
  1394. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Disconnecting...");
  1395. #endif
  1396. eReturn = AttemptDisconnect();
  1397. if(eReturn == RETURN_PASS)
  1398. {
  1399. #if defined(DEBUG_FILE)
  1400. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Disconnect passed.");
  1401. #endif
  1402. ResetHostState();
  1403. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1404. eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS;
  1405. }
  1406. else if(eReturn == RETURN_SERIAL_ERROR)
  1407. {
  1408. #if defined(DEBUG_FILE)
  1409. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Disconnect serial error.");
  1410. #endif
  1411. DSIThread_MutexLock(&stMutexCriticalSection);
  1412. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1413. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1414. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1415. }
  1416. }
  1417. break;
  1418. case ANTFS_REQUEST_CHANGE_LINK:
  1419. {
  1420. RETURN_STATUS eReturn;
  1421. #if defined(DEBUG_FILE)
  1422. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Changing radio frequency and channel period...");
  1423. #endif
  1424. eReturn = AttemptSwitchFrequency();
  1425. if (eReturn == RETURN_SERIAL_ERROR)
  1426. {
  1427. #if defined(DEBUG_FILE)
  1428. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error while changing radio frequency/period.");
  1429. #endif
  1430. DSIThread_MutexLock(&stMutexCriticalSection);
  1431. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1432. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1433. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1434. }
  1435. else if (eReturn == RETURN_FAIL)
  1436. {
  1437. RETURN_STATUS eDisconnectStatus;
  1438. #if defined(DEBUG_FILE)
  1439. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Changing radio frequency and channel period failed.");
  1440. #endif
  1441. // We end up here if we lost the beacon when trying to change the channel parameters, so
  1442. // disconnect, so we can try to connect to it again
  1443. eDisconnectStatus = AttemptDisconnect();
  1444. if(eDisconnectStatus == RETURN_PASS)
  1445. {
  1446. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1447. eResponse = ANTFS_HOST_RESPONSE_CONNECTION_LOST;
  1448. }
  1449. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1450. {
  1451. #if defined(DEBUG_FILE)
  1452. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error changing channel parameters.");
  1453. #endif
  1454. DSIThread_MutexLock(&stMutexCriticalSection);
  1455. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1456. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1457. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1458. }
  1459. }
  1460. else if (eReturn == RETURN_PASS)
  1461. {
  1462. eANTFSRequest = ANTFS_REQUEST_NONE;
  1463. #if defined(DEBUG_FILE)
  1464. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Changed radio frequency and channel period.");
  1465. #endif
  1466. // TODO: Do we need a response as well?
  1467. }
  1468. } // ANTFS_REQUEST_CHANGE_LINK
  1469. break;
  1470. case ANTFS_REQUEST_DOWNLOAD:
  1471. {
  1472. RETURN_STATUS eReturn;
  1473. eANTFSState = ANTFS_HOST_STATE_DOWNLOADING;
  1474. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_FAIL; //Set a default response
  1475. #if defined(DEBUG_FILE)
  1476. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Downloading...");
  1477. #endif
  1478. eReturn = AttemptDownload();
  1479. if (eReturn == RETURN_PASS)
  1480. {
  1481. #if defined(DEBUG_FILE)
  1482. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Download complete.");
  1483. #endif
  1484. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1485. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_PASS;
  1486. }
  1487. else if (eReturn == RETURN_FAIL)
  1488. {
  1489. RETURN_STATUS eDisconnectStatus;
  1490. #if defined(DEBUG_FILE)
  1491. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Download failed.");
  1492. #endif
  1493. IncFreqStaleCount(MINOR_STALE_COUNT);
  1494. eDisconnectStatus = AttemptDisconnect();
  1495. if(eDisconnectStatus == RETURN_PASS)
  1496. {
  1497. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1498. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_FAIL;
  1499. }
  1500. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1501. {
  1502. #if defined(DEBUG_FILE)
  1503. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error on failed download.");
  1504. #endif
  1505. DSIThread_MutexLock(&stMutexCriticalSection);
  1506. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1507. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1508. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1509. }
  1510. }
  1511. else if (eReturn == RETURN_REJECT)
  1512. {
  1513. #if defined(DEBUG_FILE)
  1514. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Download rejected.");
  1515. #endif
  1516. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1517. switch (ucRejectCode)
  1518. {
  1519. case DOWNLOAD_RESPONSE_DOES_NOT_EXIST:
  1520. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_INVALID_INDEX;
  1521. break;
  1522. case DOWNLOAD_RESPONSE_NOT_DOWNLOADABLE:
  1523. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_FILE_NOT_READABLE;
  1524. break;
  1525. case DOWNLOAD_RESPONSE_NOT_READY:
  1526. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_NOT_READY;
  1527. break;
  1528. case DOWNLOAD_RESPONSE_CRC_FAILED:
  1529. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_CRC_REJECTED;
  1530. break;
  1531. case DOWNLOAD_RESPONSE_REQUEST_INVALID:
  1532. default:
  1533. eResponse = ANTFS_HOST_RESPONSE_DOWNLOAD_REJECT;
  1534. break;
  1535. }
  1536. }
  1537. else if (eReturn == RETURN_SERIAL_ERROR)
  1538. {
  1539. #if defined(DEBUG_FILE)
  1540. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Download serial error.");
  1541. #endif
  1542. DSIThread_MutexLock(&stMutexCriticalSection);
  1543. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1544. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1545. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1546. }
  1547. else
  1548. {
  1549. #if defined(DEBUG_FILE)
  1550. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Download stopped.");
  1551. #endif
  1552. //Disconnect();
  1553. //eANTFSState = ANTFS_HOST_STATE_IDLE;
  1554. //eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS;
  1555. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1556. }
  1557. }
  1558. break;
  1559. case ANTFS_REQUEST_UPLOAD:
  1560. {
  1561. RETURN_STATUS eReturn;
  1562. eANTFSState = ANTFS_HOST_STATE_UPLOADING;
  1563. eResponse = ANTFS_HOST_RESPONSE_UPLOAD_FAIL; //Set a default response
  1564. #if defined(DEBUG_FILE)
  1565. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Requesting upload.");
  1566. #endif
  1567. eReturn = UploadLoop();
  1568. if (eReturn == RETURN_PASS)
  1569. {
  1570. #if defined(DEBUG_FILE)
  1571. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Upload complete.");
  1572. #endif
  1573. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1574. eResponse = ANTFS_HOST_RESPONSE_UPLOAD_PASS;
  1575. }
  1576. else if (eReturn == RETURN_FAIL)
  1577. {
  1578. RETURN_STATUS eDisconnectStatus;
  1579. #if defined(DEBUG_FILE)
  1580. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Upload failed.");
  1581. #endif
  1582. IncFreqStaleCount(MINOR_STALE_COUNT);
  1583. eDisconnectStatus = AttemptDisconnect();
  1584. if(eDisconnectStatus == RETURN_PASS)
  1585. {
  1586. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1587. eResponse = ANTFS_HOST_RESPONSE_UPLOAD_FAIL;
  1588. }
  1589. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1590. {
  1591. #if defined(DEBUG_FILE)
  1592. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error on failed upload.");
  1593. #endif
  1594. DSIThread_MutexLock(&stMutexCriticalSection);
  1595. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1596. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1597. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1598. }
  1599. }
  1600. else if (eReturn == RETURN_REJECT)
  1601. {
  1602. #if defined(DEBUG_FILE)
  1603. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Upload rejected.");
  1604. #endif
  1605. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1606. switch (ucRejectCode)
  1607. {
  1608. case UPLOAD_RESPONSE_DOES_NOT_EXIST:
  1609. eResponse = ANTFS_HOST_RESPONSE_UPLOAD_INVALID_INDEX;
  1610. break;
  1611. case UPLOAD_RESPONSE_NOT_WRITEABLE:
  1612. eResponse = ANTFS_HOST_RESPONSE_UPLOAD_FILE_NOT_WRITEABLE;
  1613. break;
  1614. case UPLOAD_RESPONSE_INSUFFICIENT_SPACE:
  1615. eResponse = ANTFS_HOST_RESPONSE_UPLOAD_INSUFFICIENT_SPACE;
  1616. break;
  1617. default:
  1618. eResponse = ANTFS_HOST_RESPONSE_UPLOAD_REJECT;
  1619. break;
  1620. }
  1621. }
  1622. else if (eReturn == RETURN_SERIAL_ERROR)
  1623. {
  1624. #if defined(DEBUG_FILE)
  1625. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Upload serial error.");
  1626. #endif
  1627. DSIThread_MutexLock(&stMutexCriticalSection);
  1628. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1629. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1630. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1631. }
  1632. else
  1633. {
  1634. #if defined(DEBUG_FILE)
  1635. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Upload stopped.");
  1636. #endif
  1637. //Disconnect();
  1638. //eANTFSState = ANTFS_HOST_STATE_IDLE;
  1639. //eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS;
  1640. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1641. }
  1642. }
  1643. break;
  1644. case ANTFS_REQUEST_MANUAL_TRANSFER:
  1645. {
  1646. RETURN_STATUS eReturn;
  1647. eANTFSState = ANTFS_HOST_STATE_SENDING;
  1648. #if defined(DEBUG_FILE)
  1649. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Sending Direct.");
  1650. #endif
  1651. eReturn = AttemptManualTransfer();
  1652. if (eReturn == RETURN_PASS)
  1653. {
  1654. #if defined(DEBUG_FILE)
  1655. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): ManualTransfer complete.");
  1656. #endif
  1657. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1658. eResponse = ANTFS_HOST_RESPONSE_MANUAL_TRANSFER_PASS;
  1659. }
  1660. else if (eReturn == RETURN_FAIL)
  1661. {
  1662. RETURN_STATUS eDisconnectStatus;
  1663. #if defined(DEBUG_FILE)
  1664. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): ManualTransfer failed to retrieve data.");
  1665. #endif
  1666. IncFreqStaleCount(MINOR_STALE_COUNT);
  1667. eDisconnectStatus = AttemptDisconnect();
  1668. if(eDisconnectStatus == RETURN_PASS)
  1669. {
  1670. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1671. eResponse = ANTFS_HOST_RESPONSE_MANUAL_TRANSFER_RESPONSE_FAIL;
  1672. }
  1673. else if(eDisconnectStatus == RETURN_SERIAL_ERROR)
  1674. {
  1675. #if defined(DEBUG_FILE)
  1676. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error on failed manual transfer.");
  1677. #endif
  1678. DSIThread_MutexLock(&stMutexCriticalSection);
  1679. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1680. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1681. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1682. }
  1683. }
  1684. else if (eReturn == RETURN_REJECT)
  1685. {
  1686. #if defined(DEBUG_FILE)
  1687. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): ManualTransfer rejected due to Tx error.");
  1688. #endif
  1689. IncFreqStaleCount(MIN_STALE_COUNT); //Have to check if a NAK on the Garmin protocol will cause this condition
  1690. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1691. eResponse = ANTFS_HOST_RESPONSE_MANUAL_TRANSFER_TRANSMIT_FAIL;
  1692. }
  1693. else if (eReturn == RETURN_SERIAL_ERROR)
  1694. {
  1695. #if defined(DEBUG_FILE)
  1696. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): ManualTransfer serial error.");
  1697. #endif
  1698. DSIThread_MutexLock(&stMutexCriticalSection);
  1699. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1700. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1701. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1702. }
  1703. else
  1704. {
  1705. #if defined(DEBUG_FILE)
  1706. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): ManualTransfer stopped.");
  1707. #endif
  1708. //Disconnect();
  1709. //eANTFSState = ANTFS_HOST_STATE_IDLE;
  1710. //eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS;
  1711. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1712. }
  1713. }
  1714. break;
  1715. case ANTFS_REQUEST_ERASE:
  1716. {
  1717. RETURN_STATUS eReturn;
  1718. eANTFSState = ANTFS_HOST_STATE_ERASING;
  1719. eResponse = ANTFS_HOST_RESPONSE_ERASE_FAIL; //Set default response
  1720. #if defined(DEBUG_FILE)
  1721. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Erasing...");
  1722. #endif
  1723. eReturn = AttemptErase();
  1724. if (eReturn == RETURN_PASS)
  1725. {
  1726. #if defined(DEBUG_FILE)
  1727. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Erase complete.");
  1728. #endif
  1729. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1730. eResponse = ANTFS_HOST_RESPONSE_ERASE_PASS;
  1731. }
  1732. else if (eReturn == RETURN_FAIL)
  1733. {
  1734. #if defined(DEBUG_FILE)
  1735. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Erase fail.");
  1736. #endif
  1737. IncFreqStaleCount(MINOR_STALE_COUNT);
  1738. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1739. eResponse = ANTFS_HOST_RESPONSE_ERASE_FAIL;
  1740. }
  1741. else if (eReturn == RETURN_SERIAL_ERROR)
  1742. {
  1743. #if defined(DEBUG_FILE)
  1744. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Erase serial error.");
  1745. #endif
  1746. DSIThread_MutexLock(&stMutexCriticalSection);
  1747. if (eANTFSRequest < ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1748. eANTFSRequest = ANTFS_REQUEST_HANDLE_SERIAL_ERROR;
  1749. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1750. }
  1751. else
  1752. {
  1753. #if defined(DEBUG_FILE)
  1754. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Erase stopped.");
  1755. #endif
  1756. //Disconnect();
  1757. //eANTFSState = ANTFS_HOST_STATE_IDLE;
  1758. //eResponse = ANTFS_HOST_RESPONSE_DISCONNECT_PASS;
  1759. eANTFSState = ANTFS_HOST_STATE_TRANSPORT;
  1760. }
  1761. }
  1762. break;
  1763. default:
  1764. break;
  1765. }
  1766. //This is where to handle the internal requests, because they can happen asyncronously.
  1767. //We will also clear the request here.
  1768. DSIThread_MutexLock(&stMutexCriticalSection);
  1769. if (eResponse != ANTFS_HOST_RESPONSE_NONE)
  1770. AddResponse(eResponse);
  1771. if (eANTFSRequest == ANTFS_REQUEST_CONNECTION_LOST)
  1772. {
  1773. #if defined(DEBUG_FILE)
  1774. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connection lost.");
  1775. #endif
  1776. eANTFSRequest = ANTFS_REQUEST_NONE;
  1777. bFoundDevice = FALSE;
  1778. if (eANTFSState >= ANTFS_HOST_STATE_CONNECTED)
  1779. {
  1780. #if defined(DEBUG_FILE)
  1781. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Connection lost - ignored.");
  1782. #endif
  1783. ResetHostState();
  1784. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1785. AddResponse(ANTFS_HOST_RESPONSE_CONNECTION_LOST);
  1786. }
  1787. }
  1788. else if (eANTFSRequest == ANTFS_REQUEST_HANDLE_SERIAL_ERROR)
  1789. {
  1790. #if defined(DEBUG_FILE)
  1791. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error!");
  1792. #endif
  1793. HandleSerialError();
  1794. AddResponse(ANTFS_HOST_RESPONSE_SERIAL_FAIL);
  1795. }
  1796. else if (eANTFSRequest == ANTFS_REQUEST_SERIAL_ERROR_HANDLED)
  1797. {
  1798. #if defined(DEBUG_FILE)
  1799. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Serial error handled");
  1800. #endif
  1801. ResetHostState();
  1802. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1803. eANTFSRequest = ANTFS_REQUEST_INIT;
  1804. }
  1805. else
  1806. {
  1807. if (eANTFSState == ANTFS_HOST_STATE_SEARCHING)
  1808. eANTFSRequest = ANTFS_REQUEST_SEARCH; //Set the search request if we're still in search mode
  1809. else
  1810. eANTFSRequest = ANTFS_REQUEST_NONE; //Clear any other request
  1811. }
  1812. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1813. }
  1814. }//while()
  1815. #if defined(DEBUG_FILE)
  1816. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): Exiting thread.");
  1817. #endif
  1818. eANTFSRequest = ANTFS_REQUEST_NONE;
  1819. DSIThread_MutexLock(&stMutexCriticalSection);
  1820. bANTFSThreadRunning = FALSE;
  1821. DSIThread_CondSignal(&stCondANTFSThreadExit);
  1822. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1823. #if defined(__cplusplus)
  1824. return;
  1825. #else
  1826. ExitThread(0);
  1827. #if defined(DEBUG_FILE)
  1828. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTFSThread(): C code reaching return statement unexpectedly.");
  1829. #endif
  1830. return; // Code should not be reached.
  1831. #endif
  1832. }
  1833. /////////////////////////////////////////////////////////////////////
  1834. // Returns: TRUE if the message is for the ANT-FS channel
  1835. BOOL ANTFSHostChannel::FilterANTMessages(ANT_MESSAGE* pstMessage_, UCHAR ucANTChannel_)
  1836. {
  1837. // Some messages do not include the channel number in the response, so
  1838. // they might get processed incorrectly
  1839. if(pstMessage_->ucMessageID == MESG_RESPONSE_EVENT_ID)
  1840. {
  1841. if(pstMessage_->aucData[MESG_EVENT_ID_OFFSET] == MESG_NETWORK_KEY_ID)
  1842. {
  1843. if(pstMessage_->aucData[MESG_CHANNEL_OFFSET] == ucNetworkNumber)
  1844. return TRUE; // this is the network we are using
  1845. }
  1846. }
  1847. else if(pstMessage_->ucMessageID == MESG_STARTUP_MESG_ID)
  1848. {
  1849. return TRUE;
  1850. }
  1851. if(ucANTChannel_ == ucChannelNumber)
  1852. return TRUE;
  1853. return FALSE;
  1854. }
  1855. ///////////////////////////////////////////////////////////////////////
  1856. BOOL ANTFSHostChannel::ReportDownloadProgress(void)
  1857. {
  1858. // We used to perodically signal our transfer progress, we dont do that anymore.
  1859. return TRUE;
  1860. }
  1861. ///////////////////////////////////////////////////////////////////////
  1862. void ANTFSHostChannel::ResetHostState(void)
  1863. {
  1864. // Clear all state variables, while keeping the configuration:
  1865. // channel parameters, search list, ignore list, frequency table selection
  1866. *pbCancel = FALSE;
  1867. bForceFullInit = FALSE;
  1868. memset(aucResponseBuf, 0, sizeof(aucResponseBuf));
  1869. memset(aucRxBuf, 0, sizeof(aucRxBuf));
  1870. memset(aucTxBuf, 0, sizeof(aucTxBuf));
  1871. memset(aucRemoteFriendlyName, 0, sizeof(aucRemoteFriendlyName));
  1872. ucRemoteFriendlyNameLength = 0;
  1873. ucAuthType = 0;
  1874. memset(aucTxPassword, 0, sizeof(aucTxPassword));
  1875. ucTxPasswordLength = 0;
  1876. ucPasswordLength = 0;
  1877. ucDisconnectType = DISCONNECT_COMMAND_LINK;
  1878. ucUndiscoverableTimeDuration = 0;
  1879. ucUndiscoverableAppSpecificDuration = 0;
  1880. pucUploadData = (UCHAR*)NULL;
  1881. pucResponseBuffer = (UCHAR*)NULL;
  1882. pucResponseBufferSize = (UCHAR*)NULL;
  1883. ulAuthResponseTimeout = cfgParams.ul_cfg_auth_timeout; // reset to explicit configuration from SetCurrentConfig
  1884. ucChannelStatus = 0;
  1885. ulFoundBeaconHostID = 0;
  1886. usFoundBeaconPeriod = 0;
  1887. usFoundANTFSDeviceID = 0;
  1888. usFoundANTFSManufacturerID = 0;
  1889. usFoundANTFSDeviceType = 0;
  1890. ucFoundANTDeviceType = 0;
  1891. ucFoundANTTransmitType = 0;
  1892. bFoundDeviceHasData = FALSE;
  1893. bFoundDeviceUploadEnabled = FALSE;
  1894. bFoundDeviceInPairingMode = FALSE;
  1895. ucFoundDeviceAuthenticationType = 0;
  1896. ucFoundDeviceState = REMOTE_DEVICE_BEACON_NOT_FOUND;
  1897. bFoundDevice = FALSE;
  1898. bFoundDeviceIsValid = FALSE;
  1899. bNewRxEvent = FALSE;
  1900. stFoundDeviceParameters.usDeviceType = 0;
  1901. stFoundDeviceParameters.usManufacturerID = 0;
  1902. stFoundDeviceParameters.ucAuthenticationType = 0;
  1903. stFoundDeviceParameters.ucStatusByte1 = 0;
  1904. stFoundDeviceParameters.ucStatusByte2 = 0;
  1905. // Download Data
  1906. pucTransferBuffer = (UCHAR*)NULL;
  1907. memset(aucTransferBufferFixed, 0, sizeof(aucTransferBufferFixed));
  1908. memset(aucSendDirectBuffer, 0, sizeof(aucSendDirectBuffer));
  1909. ulTransferArrayIndex = 0;
  1910. ulPacketCount = 0;
  1911. ulUploadIndexProgress = 0;
  1912. bTxError = FALSE;
  1913. bRxError = FALSE;
  1914. bReceivedBurst = FALSE;
  1915. bReceivedResponse = FALSE;
  1916. ulTransferTotalBytesRemaining = 0;
  1917. ulTransferBytesInBlock = 0;
  1918. bTransfer = FALSE;
  1919. usRadioChannelID = 0;
  1920. ucTransportFrequencySelection = ANTFS_AUTO_FREQUENCY_SELECTION;
  1921. ucTransportLayerRadioFrequency = 0;
  1922. ucSearchRadioFrequency = ANTFS_RF_FREQ;
  1923. usTransferDataFileIndex = 0;
  1924. ulTransferDataOffset = 0;
  1925. ulTransferByteSize = 0;
  1926. ulTransferBufferSize = 0;
  1927. bLargeData = FALSE;
  1928. ulHostBlockSize = MAX_ULONG;
  1929. ucLinkResponseRetries = 0;
  1930. ucStrikes = STRIKE_COUNT;
  1931. if(eANTFSState == ANTFS_HOST_STATE_OFF)
  1932. {
  1933. pucTransferBufferDynamic = (UCHAR*) NULL;
  1934. }
  1935. else
  1936. {
  1937. eANTFSState = ANTFS_HOST_STATE_IDLE;
  1938. if (pucTransferBufferDynamic)
  1939. {
  1940. if (pucTransferBuffer == pucTransferBufferDynamic)
  1941. pucTransferBuffer = (UCHAR*)NULL;
  1942. delete[] pucTransferBufferDynamic;
  1943. pucTransferBufferDynamic = (UCHAR*)NULL;
  1944. }
  1945. }
  1946. eANTFSRequest = ANTFS_REQUEST_NONE;
  1947. }
  1948. ///////////////////////////////////////////////////////////////////////
  1949. BOOL ANTFSHostChannel::ReInitDevice(void)
  1950. {
  1951. if (eANTFSState != ANTFS_HOST_STATE_OFF)
  1952. this->Close();
  1953. bKillThread = FALSE;
  1954. #if defined(DEBUG_FILE)
  1955. DSIDebug::ThreadWrite("ANTFSHostChannel::ReInitDevice(): Initializing");
  1956. #endif
  1957. if (hANTFSThread == NULL)
  1958. {
  1959. hANTFSThread = DSIThread_CreateThread(&ANTFSHostChannel::ANTFSThreadStart, this);
  1960. if (hANTFSThread == NULL)
  1961. return FALSE;
  1962. }
  1963. if (!bIgnoreListRunning)
  1964. {
  1965. if (DSIThread_MutexInit(&stMutexIgnoreListAccess) != DSI_THREAD_ENONE)
  1966. {
  1967. return FALSE;
  1968. }
  1969. usListIndex = 0;
  1970. bTimerThreadInitDone = FALSE;
  1971. pclQueueTimer = new DSITimer(&ANTFSHostChannel::QueueTimerStart, this, 1000, TRUE);
  1972. if (pclQueueTimer->NoError() == FALSE)
  1973. {
  1974. DSIThread_MutexDestroy(&stMutexIgnoreListAccess);
  1975. return FALSE;
  1976. }
  1977. bIgnoreListRunning = TRUE;
  1978. }
  1979. PopulateTransportFreqTable();
  1980. DSIThread_MutexLock(&stMutexResponseQueue);
  1981. clResponseQueue.Clear();
  1982. DSIThread_MutexUnlock(&stMutexResponseQueue);
  1983. DSIThread_MutexLock(&stMutexCriticalSection);
  1984. eANTFSRequest = ANTFS_REQUEST_INIT;
  1985. DSIThread_CondSignal(&stCondRequest);
  1986. DSIThread_MutexUnlock(&stMutexCriticalSection);
  1987. return TRUE;
  1988. }
  1989. ///////////////////////////////////////////////////////////////////////
  1990. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptSearch(void)
  1991. {
  1992. IGNORE_LIST_ITEM stListItem;
  1993. IGNORE_LIST_ITEM *pstListItem;
  1994. BOOL bFullInit = TRUE;
  1995. BOOL bFoundBroadcastDevice = FALSE;
  1996. UCHAR ucFirstMesgRetries;
  1997. BOOL bFirstMesgResult;
  1998. while (eANTFSState == ANTFS_HOST_STATE_SEARCHING)
  1999. {
  2000. //if (!bFullInit)
  2001. {
  2002. if(pclANT->GetChannelStatus(ucChannelNumber, &ucChannelStatus, MESSAGE_TIMEOUT) == FALSE)
  2003. {
  2004. #if defined(DEBUG_FILE)
  2005. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptOpenBeacon(): Failed ANT_GetChannelStatus().");
  2006. #endif
  2007. return RETURN_SERIAL_ERROR;
  2008. }
  2009. if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_TRACKING_CHANNEL ||
  2010. (ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_SEARCHING_CHANNEL)
  2011. {
  2012. if (pclANT->CloseChannel(ucChannelNumber, ANT_CLOSE_TIMEOUT) == FALSE)
  2013. return RETURN_SERIAL_ERROR;
  2014. //TODO?? Don't we have to wait for close event? Wouldn't unassign call below fail with wrong state?
  2015. if(bFullInit || bForceFullInit)
  2016. {
  2017. if(pclANT->UnAssignChannel(ucChannelNumber) == FALSE)
  2018. return RETURN_SERIAL_ERROR;
  2019. bFullInit = TRUE;
  2020. }
  2021. }
  2022. else if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_UNASSIGNED_CHANNEL)
  2023. {
  2024. bFullInit = TRUE;
  2025. }
  2026. else if(bFullInit) // Status = ASSIGNED, we only need to unassign it for full configuration
  2027. {
  2028. if(pclANT->UnAssignChannel(ucChannelNumber) == FALSE)
  2029. return RETURN_SERIAL_ERROR;
  2030. bFullInit = TRUE;
  2031. }
  2032. }
  2033. if (bFullInit)
  2034. {
  2035. #if defined(DEBUG_FILE)
  2036. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Full Init Begin");
  2037. #endif
  2038. //if (pclANT->ResetSystem() == FALSE)
  2039. // return RETURN_SERIAL_ERROR;
  2040. //DSIThread_Sleep(1000);
  2041. ucFirstMesgRetries = 0;
  2042. while (((bFirstMesgResult = pclANT->SetNetworkKey(ucNetworkNumber, (UCHAR *) aucTheNetworkkey,MESSAGE_TIMEOUT)) == FALSE) && (ucFirstMesgRetries++ < ANTFS_RESPONSE_RETRIES));
  2043. #if defined(DEBUG_FILE)
  2044. if (ucFirstMesgRetries)
  2045. {
  2046. UCHAR aucString[256];
  2047. SNPRINTF((char*)&aucString[0], 256, "ANTFSHostChannel::AttemptSearch(): %d message retries on first message.", ucFirstMesgRetries);
  2048. DSIDebug::ThreadWrite((char*)aucString);
  2049. }
  2050. #endif
  2051. if (!bFirstMesgResult)
  2052. {
  2053. #if defined(DEBUG_FILE)
  2054. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_SetNetworkKey().");
  2055. #endif
  2056. return RETURN_SERIAL_ERROR;
  2057. }
  2058. if (pclANT->AssignChannel(ucChannelNumber, 0x00, ucNetworkNumber, MESSAGE_TIMEOUT) == FALSE)
  2059. {
  2060. #if defined(DEBUG_FILE)
  2061. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_AssignChannel().");
  2062. #endif
  2063. return RETURN_SERIAL_ERROR;
  2064. }
  2065. if (pclANT->SetChannelPeriod(ucChannelNumber, usTheMessagePeriod, MESSAGE_TIMEOUT) == FALSE)
  2066. {
  2067. #if defined(DEBUG_FILE)
  2068. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_SetChannelPeriod().");
  2069. #endif
  2070. return RETURN_SERIAL_ERROR;
  2071. }
  2072. if (pclANT->SetChannelSearchTimeout(ucChannelNumber, ANTFS_SEARCH_TIMEOUT, MESSAGE_TIMEOUT) == FALSE)
  2073. {
  2074. #if defined(DEBUG_FILE)
  2075. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_SetChannelSearchTimeout().");
  2076. #endif
  2077. return RETURN_SERIAL_ERROR;
  2078. }
  2079. if (pclANT->SetChannelRFFrequency(ucChannelNumber, ucSearchRadioFrequency, MESSAGE_TIMEOUT) == FALSE)
  2080. {
  2081. #if defined(DEBUG_FILE)
  2082. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_SetChannelRFFreq().");
  2083. #endif
  2084. return RETURN_SERIAL_ERROR;
  2085. }
  2086. if (pclANT->SetFastSearch(ucChannelNumber, MESSAGE_TIMEOUT) == FALSE)
  2087. {
  2088. #if defined(DEBUG_FILE)
  2089. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed SetFastSearch().");
  2090. #endif
  2091. return RETURN_SERIAL_ERROR;
  2092. }
  2093. bForceFullInit = FALSE;
  2094. bFullInit = FALSE;
  2095. #if defined(DEBUG_FILE)
  2096. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Full Init Complete");
  2097. #endif
  2098. }
  2099. //This gets reset everytime a device is found, so we need to set it every time
  2100. if(ucTheProxThreshold > 0) //Don't need to send if it is default=0, saves sending command on unsupported devices, plus a little time
  2101. {
  2102. if(pclANT->SetProximitySearch(ucChannelNumber, ucTheProxThreshold, MESSAGE_TIMEOUT) == FALSE)
  2103. {
  2104. #if defined(DEBUG_FILE)
  2105. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_SetProximitySearch(). Ignored, possibly unsupported device.");
  2106. #endif
  2107. //don't return RETURN_SERIAL_ERROR, Ignore failure so we don't fail on unsupported devices and maintain compatibility.
  2108. }
  2109. }
  2110. //This gets reset everytime a device is found, so we need to set it every time
  2111. if (pclANT->SetChannelID(ucChannelNumber, usRadioChannelID, ucTheDeviceType, ucTheTransmissionType, MESSAGE_TIMEOUT) == FALSE)
  2112. {
  2113. #if defined(DEBUG_FILE)
  2114. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_SetChannelId().");
  2115. #endif
  2116. return RETURN_SERIAL_ERROR;
  2117. }
  2118. #if defined(DEBUG_FILE)
  2119. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Opening channel...");
  2120. #endif
  2121. bFoundDevice = FALSE;
  2122. bFoundBroadcastDevice = FALSE;
  2123. if (pclANT->OpenChannel(ucChannelNumber,MESSAGE_TIMEOUT) == FALSE)
  2124. {
  2125. #if defined(DEBUG_FILE)
  2126. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Failed ANT_OpenChannel().");
  2127. #endif
  2128. return RETURN_SERIAL_ERROR;
  2129. }
  2130. bNewRxEvent = FALSE;
  2131. do
  2132. {
  2133. #if defined(DEBUG_FILE)
  2134. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Waiting for search results...");
  2135. #endif
  2136. if (pclANT->GetChannelStatus(ucChannelNumber, &ucChannelStatus, MESSAGE_TIMEOUT) == FALSE)
  2137. {
  2138. #if defined(DEBUG_FILE)
  2139. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Cannot get ANT Channel Status.");
  2140. #endif
  2141. return RETURN_FAIL;
  2142. //return RETURN_SERIAL_ERROR;
  2143. }
  2144. if (ucChannelStatus < STATUS_SEARCHING_CHANNEL)
  2145. {
  2146. #if defined(DEBUG_FILE)
  2147. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): ANT Channel not searching or tracking.");
  2148. #endif
  2149. return RETURN_FAIL;
  2150. }
  2151. DSIThread_MutexLock(&stMutexCriticalSection);
  2152. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  2153. {
  2154. DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, SEARCH_STATUS_CHECK_TIMEOUT);
  2155. }
  2156. bNewRxEvent = FALSE;
  2157. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2158. if (*pbCancel == TRUE)
  2159. {
  2160. #if defined(DEBUG_FILE)
  2161. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Searching stopped.");
  2162. #endif
  2163. return RETURN_STOP;
  2164. }
  2165. } while (!bFoundDevice);
  2166. #if defined(DEBUG_FILE)
  2167. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Search results loop exitted.");
  2168. #endif
  2169. if (!bFoundDeviceIsValid)
  2170. {
  2171. #if defined(DEBUG_FILE)
  2172. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Device not sending a valid beacon.");
  2173. #endif
  2174. if(!bRequestPageEnabled)
  2175. continue;
  2176. #if defined(DEBUG_FILE)
  2177. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Attempting to request an ANT-FS session...");
  2178. #endif
  2179. if(AttemptRequestSession() != RETURN_PASS)
  2180. continue;
  2181. bFoundBroadcastDevice = TRUE;
  2182. }
  2183. if (ucFoundDeviceState != REMOTE_DEVICE_STATE_LINK)
  2184. {
  2185. #if defined(DEBUG_FILE)
  2186. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Device not in link mode.");
  2187. #endif
  2188. continue;
  2189. }
  2190. if (pclANT->GetChannelID(ucChannelNumber, (USHORT*)&usFoundANTFSDeviceID, (UCHAR*)&ucFoundANTDeviceType, (UCHAR*)&ucFoundANTTransmitType, MESSAGE_TIMEOUT) == FALSE)
  2191. {
  2192. #if defined(DEBUG_FILE)
  2193. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Cannot get ANT ID.");
  2194. #endif
  2195. continue;
  2196. }
  2197. // Save 2 bytes of the DeviceID
  2198. stFoundDeviceParameters.ulDeviceID &= 0xFFFF0000;
  2199. stFoundDeviceParameters.ulDeviceID |= (0x0000FFFF&usFoundANTFSDeviceID);
  2200. // Check the Ignore list
  2201. stListItem.usID = usFoundANTFSDeviceID;
  2202. stListItem.usManufacturerID = usFoundANTFSManufacturerID;
  2203. stListItem.usDeviceType = usFoundANTFSDeviceType;
  2204. DSIThread_MutexLock(&stMutexIgnoreListAccess);
  2205. pstListItem = (IGNORE_LIST_ITEM *) bsearch(&stListItem, astIgnoreList, usListIndex, sizeof(IGNORE_LIST_ITEM), &ListCompare);
  2206. DSIThread_MutexUnlock(&stMutexIgnoreListAccess);
  2207. if (pstListItem != NULL)
  2208. {
  2209. #if defined(DEBUG_FILE)
  2210. UCHAR aucString[256];
  2211. SNPRINTF((char *)&aucString[0], 256, "ANTFSHostChannel::AttemptSearch(): Device %u is on the ignore list.", usFoundANTFSDeviceID);
  2212. DSIDebug::ThreadWrite((char *)aucString);
  2213. #endif
  2214. if(bFoundBroadcastDevice)
  2215. {
  2216. ucDisconnectType = DISCONNECT_COMMAND_BROADCAST; // Device is in ignore list, let it go back to broadcast
  2217. if(AttemptDisconnect() == RETURN_SERIAL_ERROR)
  2218. return RETURN_SERIAL_ERROR;
  2219. eANTFSState = ANTFS_HOST_STATE_SEARCHING;
  2220. }
  2221. continue; // Continue searching if the device is on the ignore list.
  2222. }
  2223. #if defined(DEBUG_FILE)
  2224. {
  2225. UCHAR aucString[256];
  2226. SNPRINTF((char *)aucString,256, "ANTFSHostChannel::AttemptSearch(): Found device %u (Manufacturer ID: %u Device Type: %u Beacon Period: %u/32768 Authentication Type: %u).", usFoundANTFSDeviceID, usFoundANTFSManufacturerID, usFoundANTFSDeviceType, usFoundBeaconPeriod, ucFoundDeviceAuthenticationType);
  2227. DSIDebug::ThreadWrite((char *)aucString);
  2228. if (bFoundDeviceHasData)
  2229. DSIDebug::ThreadWrite(" Device has data.");
  2230. else
  2231. DSIDebug::ThreadWrite(" Device has no data.");
  2232. if (bFoundDeviceUploadEnabled)
  2233. DSIDebug::ThreadWrite(" Device has upload enabled.");
  2234. else
  2235. DSIDebug::ThreadWrite(" Device has upload disabled.");
  2236. if (bFoundDeviceInPairingMode)
  2237. DSIDebug::ThreadWrite(" Device is in pairing mode.");
  2238. else
  2239. DSIDebug::ThreadWrite(" Device is not in pairing mode.");
  2240. }
  2241. #endif
  2242. // Check for a match.
  2243. if (IsDeviceMatched(&stFoundDeviceParameters, TRUE) == TRUE)
  2244. {
  2245. // We have found a match.
  2246. #if defined(DEBUG_FILE)
  2247. DSIDebug::ThreadWrite(" Device is a match.");
  2248. #endif
  2249. return RETURN_PASS;
  2250. }
  2251. #if defined(DEBUG_FILE)
  2252. DSIDebug::ThreadWrite(" Device is not a match.");
  2253. #endif
  2254. // No match; continue searching.
  2255. if(bFoundBroadcastDevice)
  2256. {
  2257. ucDisconnectType = DISCONNECT_COMMAND_BROADCAST; // Device is in ignore list, let it go back to broadcast
  2258. if(AttemptDisconnect() == RETURN_SERIAL_ERROR)
  2259. return RETURN_SERIAL_ERROR;
  2260. eANTFSState = ANTFS_HOST_STATE_SEARCHING;
  2261. }
  2262. }
  2263. #if defined(DEBUG_FILE)
  2264. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSearch(): Not searching anymore.");
  2265. #endif
  2266. return RETURN_FAIL;
  2267. }
  2268. ///////////////////////////////////////////////////////////////////////
  2269. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptConnect(void)
  2270. {
  2271. UCHAR ucTxRetries;
  2272. ANTFRAMER_RETURN eTxComplete;
  2273. UCHAR ucRxRetries = 3;
  2274. BOOL bStatus = FALSE;
  2275. if (!bFoundDevice)
  2276. {
  2277. #if defined(DEBUG_FILE)
  2278. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Device not found.");
  2279. #endif
  2280. return RETURN_FAIL;
  2281. }
  2282. if (ucTransportFrequencySelection == ANTFS_AUTO_FREQUENCY_SELECTION)
  2283. ucTransportLayerRadioFrequency = CheckForNewTransportFreq();
  2284. else
  2285. ucTransportLayerRadioFrequency = ucTransportFrequencySelection;
  2286. // Set up the command
  2287. memset(aucTxBuf,0x00,sizeof(aucTxBuf));
  2288. aucTxBuf[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  2289. aucTxBuf[ANTFS_COMMAND_OFFSET] = ANTFS_CONNECT_ID;
  2290. aucTxBuf[TRANSPORT_CHANNEL_FREQ_OFFSET] = ucTransportLayerRadioFrequency;
  2291. aucTxBuf[TRANSPORT_CHANNEL_PERIOD] = TRANSPORT_MESSAGE_PERIOD_CODE;
  2292. Convert_ULONG_To_Bytes(ulHostSerialNumber,
  2293. &aucTxBuf[HOST_ID_OFFSET+3],
  2294. &aucTxBuf[HOST_ID_OFFSET+2],
  2295. &aucTxBuf[HOST_ID_OFFSET+1],
  2296. &aucTxBuf[HOST_ID_OFFSET]);
  2297. #if defined(ACCESS_POINT)
  2298. ucTxRetries = 2;
  2299. #else
  2300. ucTxRetries = 8;
  2301. #endif
  2302. do
  2303. {
  2304. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucTxBuf,CONNECT_TIMEOUT);
  2305. #if defined(DEBUG_FILE)
  2306. if (eTxComplete == ANTFRAMER_FAIL)
  2307. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Tx error.");
  2308. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2309. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Tx timeout.");
  2310. #endif
  2311. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  2312. if (eTxComplete != ANTFRAMER_PASS)
  2313. {
  2314. #if defined(DEBUG_FILE)
  2315. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Failed sending connect command.");
  2316. #endif
  2317. return RETURN_FAIL;
  2318. }
  2319. if (pclANT->SetChannelPeriod(ucChannelNumber, TRANSPORT_MESSAGE_PERIOD, MESSAGE_TIMEOUT) == FALSE)
  2320. {
  2321. #if defined(DEBUG_FILE)
  2322. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Failed ANT_SetChannelPeriod().");
  2323. #endif
  2324. return RETURN_SERIAL_ERROR;
  2325. }
  2326. if (pclANT->SetChannelSearchTimeout(ucChannelNumber, TRANSPORT_SEARCH_TIMEOUT, MESSAGE_TIMEOUT) == FALSE)
  2327. {
  2328. #if defined(DEBUG_FILE)
  2329. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Failed ANT_SetChannelSearchTimeout().");
  2330. #endif
  2331. return RETURN_SERIAL_ERROR;
  2332. }
  2333. if (pclANT->SetChannelRFFrequency(ucChannelNumber, ucTransportLayerRadioFrequency, MESSAGE_TIMEOUT) == FALSE)
  2334. {
  2335. #if defined(DEBUG_FILE)
  2336. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Failed ANT_SetChannelRFFreq().");
  2337. #endif
  2338. return RETURN_SERIAL_ERROR;
  2339. }
  2340. do
  2341. {
  2342. bNewRxEvent = FALSE;
  2343. DSIThread_MutexLock(&stMutexCriticalSection);
  2344. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  2345. {
  2346. UCHAR ucResult = DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, BROADCAST_TIMEOUT);
  2347. if (ucResult != DSI_THREAD_ENONE)
  2348. {
  2349. #if defined(DEBUG_FILE)
  2350. if(ucResult == DSI_THREAD_EOTHER)
  2351. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): CondTimedWait() Failed!");
  2352. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Rx timeout.");
  2353. #endif
  2354. IncFreqStaleCount(MAX_STALE_COUNT);
  2355. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2356. return RETURN_FAIL;
  2357. }
  2358. }
  2359. bNewRxEvent = FALSE;
  2360. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2361. if (*pbCancel == TRUE)
  2362. {
  2363. #if defined(DEBUG_FILE)
  2364. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Stopped.");
  2365. #endif
  2366. return RETURN_STOP;
  2367. }
  2368. if (ucFoundDeviceState == REMOTE_DEVICE_STATE_AUTH)
  2369. bStatus = TRUE;
  2370. } while ((bStatus == FALSE) && ucRxRetries--);
  2371. if (!bStatus)
  2372. {
  2373. #if defined(DEBUG_FILE)
  2374. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Remote not in auth mode.");
  2375. #endif
  2376. IncFreqStaleCount(MINOR_STALE_COUNT); // Investigate why this happens, may be that the remote is not updating it's beacon fast enough.
  2377. return RETURN_FAIL;
  2378. }
  2379. if (ulFoundBeaconHostID != ulHostSerialNumber)
  2380. {
  2381. #if defined(DEBUG_FILE)
  2382. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Remote connected to another host.");
  2383. #endif
  2384. IncFreqStaleCount(MAX_STALE_COUNT);
  2385. return RETURN_FAIL;
  2386. }
  2387. return RETURN_PASS;
  2388. }
  2389. ///////////////////////////////////////////////////////////////////////
  2390. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptRequestSession(void)
  2391. {
  2392. ANTFRAMER_RETURN eTxComplete;
  2393. UCHAR ucTxRetries = 4;
  2394. UCHAR ucRxRetries = 3;
  2395. BOOL bStatus = FALSE;
  2396. USHORT usBroadcastANTDeviceID = 0;
  2397. UCHAR ucBroadcastANTDeviceType = 0;
  2398. UCHAR ucBroadcastANTTransmitType = 0;
  2399. if (pclANT->GetChannelStatus(ucChannelNumber, &ucChannelStatus, MESSAGE_TIMEOUT) == FALSE)
  2400. {
  2401. #if defined(DEBUG_FILE)
  2402. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): Cannot get ANT Channel Status.");
  2403. #endif
  2404. return RETURN_FAIL;
  2405. }
  2406. if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) != STATUS_TRACKING_CHANNEL)
  2407. {
  2408. #if defined(DEBUG_FILE)
  2409. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): Not connected to a broadcast device");
  2410. #endif
  2411. return RETURN_FAIL;
  2412. }
  2413. if (pclANT->GetChannelID(ucChannelNumber, (USHORT*)&usBroadcastANTDeviceID, (UCHAR*)&ucBroadcastANTDeviceType, (UCHAR*)&ucBroadcastANTTransmitType, MESSAGE_TIMEOUT) == FALSE)
  2414. {
  2415. #if defined(DEBUG_FILE)
  2416. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): Cannot get ANT ID.");
  2417. #endif
  2418. }
  2419. else
  2420. {
  2421. #if defined(DEBUG_FILE)
  2422. UCHAR aucString[256];
  2423. SNPRINTF((char *)aucString,256, "ANTFSHostChannel::AttemptRequestSession(): Broadcast Device (Device Number: %u Device Type: %u Tx Type: %u).", usBroadcastANTDeviceID, ucBroadcastANTDeviceType, ucBroadcastANTTransmitType);
  2424. DSIDebug::ThreadWrite((char *)aucString);
  2425. #endif
  2426. }
  2427. memset(aucTxBuf, REQUEST_PAGE_INVALID, sizeof(aucTxBuf));
  2428. aucTxBuf[ANTFS_CONNECTION_OFFSET] = ANTFS_REQUEST_PAGE_ID ;
  2429. aucTxBuf[REQUEST_TX_RESPONSE_OFFSET] = (UCHAR) 0x00;
  2430. aucTxBuf[REQUEST_PAGE_NUMBER_OFFSET] = ANTFS_BEACON_ID;
  2431. aucTxBuf[REQUEST_COMMAND_TYPE_OFFSET] = ANTFS_REQUEST_SESSION;
  2432. // Send request session message
  2433. do
  2434. {
  2435. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucTxBuf, REQUEST_TIMEOUT);
  2436. #if defined(DEBUG_FILE)
  2437. if (eTxComplete == ANTFRAMER_FAIL)
  2438. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): Tx error.");
  2439. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2440. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): Tx timeout.");
  2441. #endif
  2442. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  2443. if (eTxComplete != ANTFRAMER_PASS)
  2444. {
  2445. #if defined(DEBUG_FILE)
  2446. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): Failed sending request page.");
  2447. #endif
  2448. return RETURN_FAIL;
  2449. }
  2450. // Wait for beacon
  2451. do
  2452. {
  2453. bFoundDevice = FALSE;
  2454. bNewRxEvent = FALSE;
  2455. DSIThread_MutexLock(&stMutexCriticalSection);
  2456. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  2457. {
  2458. UCHAR ucResult = DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, BROADCAST_TIMEOUT);
  2459. if(ucResult != DSI_THREAD_ENONE)
  2460. {
  2461. #if defined(DEBUG_FILE)
  2462. if(ucResult == DSI_THREAD_EOTHER)
  2463. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): CondTimedWait() Failed!");
  2464. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): Rx timeout.");
  2465. #endif
  2466. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2467. return RETURN_FAIL;
  2468. }
  2469. }
  2470. bNewRxEvent = FALSE;
  2471. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2472. if (*pbCancel == TRUE)
  2473. {
  2474. #if defined(DEBUG_FILE)
  2475. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): ANT-FS session request stopped.");
  2476. #endif
  2477. return RETURN_STOP;
  2478. }
  2479. if(bFoundDevice && bFoundDeviceIsValid)
  2480. bStatus = TRUE;
  2481. } while ((bStatus == FALSE) && ucRxRetries--);
  2482. if(!bStatus)
  2483. {
  2484. #if defined(DEBUG_FILE)
  2485. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptRequestSession(): No response to ANT-FS Session request.");
  2486. #endif
  2487. // TODO: Possibly blackout the device for a period of time here? Might need a different blacklist for this, as we should
  2488. // blacklist based on the Channel ID instead of beacon parameters
  2489. // Blackout(usFoundANTFSDeviceID, 0, 0, 300); // 5 min?
  2490. return RETURN_FAIL;
  2491. }
  2492. if(eANTFSState != ANTFS_HOST_STATE_SEARCHING)
  2493. {
  2494. usFoundANTFSDeviceID = usBroadcastANTDeviceID; // TODO: Is this needed? Assumes device ID is derived from serial number
  2495. ucFoundANTDeviceType = ucBroadcastANTDeviceType;
  2496. ucFoundANTTransmitType = ucBroadcastANTTransmitType;
  2497. }
  2498. return RETURN_PASS;
  2499. }
  2500. ///////////////////////////////////////////////////////////////////////
  2501. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptDisconnect(void)
  2502. {
  2503. UCHAR ucTxRetries;
  2504. ANTFRAMER_RETURN eTxComplete;
  2505. if(eANTFSState != ANTFS_HOST_STATE_SEARCHING)
  2506. eANTFSState = ANTFS_HOST_STATE_DISCONNECTING;
  2507. //We change the state here so that we don't get connection lost responses after we've decided to disconnect
  2508. if (bFoundDevice)
  2509. {
  2510. bFoundDevice = FALSE;
  2511. memset(aucTxBuf, 0x00, sizeof(aucTxBuf));
  2512. aucTxBuf[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  2513. aucTxBuf[ANTFS_COMMAND_OFFSET] = ANTFS_DISCONNECT_ID;
  2514. aucTxBuf[DISCONNECT_COMMAND_TYPE_OFFSET] = ucDisconnectType;
  2515. aucTxBuf[DISCONNECT_TIME_DURATION_OFFSET] = ucUndiscoverableTimeDuration;
  2516. aucTxBuf[DISCONNECT_APP_DURATION_OFFSET] = ucUndiscoverableAppSpecificDuration;
  2517. ucTxRetries = 8;
  2518. do
  2519. {
  2520. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucTxBuf, DISCONNECT_TIMEOUT);
  2521. #if defined(DEBUG_FILE)
  2522. if (eTxComplete == ANTFRAMER_FAIL)
  2523. DSIDebug::ThreadWrite("ANTFSHostChannel::Disconnect(): Tx error.");
  2524. else if(eTxComplete == ANTFRAMER_TIMEOUT)
  2525. DSIDebug::ThreadWrite("ANTFSHostChannel::Disconnect(): Tx timeout.");
  2526. #endif
  2527. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  2528. }
  2529. if(ucDisconnectType == DISCONNECT_COMMAND_BROADCAST)
  2530. {
  2531. // Go back to link frequency and channel period
  2532. if (pclANT->SetChannelPeriod(ucChannelNumber, usTheMessagePeriod, MESSAGE_TIMEOUT) == FALSE)
  2533. {
  2534. #if defined(DEBUG_FILE)
  2535. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDisconnect(): Failed ANT_SetChannelPeriod().");
  2536. #endif
  2537. return RETURN_SERIAL_ERROR;
  2538. }
  2539. if (pclANT->SetChannelRFFrequency(ucChannelNumber, ucSearchRadioFrequency, MESSAGE_TIMEOUT) == FALSE)
  2540. {
  2541. #if defined(DEBUG_FILE)
  2542. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDisconnect(): Failed ANT_SetChannelRFFreq().");
  2543. #endif
  2544. return RETURN_SERIAL_ERROR;
  2545. }
  2546. }
  2547. else // DISCONNECT_TYPE_LINK or custom disconnect types
  2548. {
  2549. if(ucDisconnectType != DISCONNECT_COMMAND_LINK)
  2550. {
  2551. #if defined(DEBUG_FILE)
  2552. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDisconnect(): Application specific disconnect type.");
  2553. #endif
  2554. // Disconnect anyway, and close the channel
  2555. }
  2556. // Close the channel
  2557. if(pclANT->GetChannelStatus(ucChannelNumber, &ucChannelStatus, MESSAGE_TIMEOUT) == FALSE)
  2558. {
  2559. #if defined(DEBUG_FILE)
  2560. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDisconnect(): Failed ANT_GetChannelStatus().");
  2561. #endif
  2562. return RETURN_SERIAL_ERROR;
  2563. }
  2564. if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_TRACKING_CHANNEL ||
  2565. (ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_SEARCHING_CHANNEL)
  2566. {
  2567. if (pclANT->CloseChannel(ucChannelNumber, ANT_CLOSE_TIMEOUT) == FALSE)
  2568. return RETURN_SERIAL_ERROR;
  2569. if (pclANT->UnAssignChannel(ucChannelNumber, MESSAGE_TIMEOUT) == FALSE)
  2570. return RETURN_SERIAL_ERROR;
  2571. }
  2572. else if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) == STATUS_ASSIGNED_CHANNEL)
  2573. {
  2574. if (pclANT->UnAssignChannel(ucChannelNumber, MESSAGE_TIMEOUT) == FALSE)
  2575. return RETURN_SERIAL_ERROR;
  2576. }
  2577. }
  2578. usFoundANTFSManufacturerID = 0;
  2579. usFoundANTFSDeviceType = 0;
  2580. bFoundDeviceHasData = FALSE;
  2581. ucDisconnectType = DISCONNECT_COMMAND_LINK;
  2582. ucUndiscoverableTimeDuration = 0;
  2583. ucUndiscoverableAppSpecificDuration = 0;
  2584. return RETURN_PASS;
  2585. }
  2586. ///////////////////////////////////////////////////////////////////////
  2587. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptSwitchFrequency()
  2588. {
  2589. USHORT usTransportChannelPeriod;
  2590. UCHAR ucTxRetries;
  2591. ANTFRAMER_RETURN eTxComplete;
  2592. UCHAR ucRxRetries = 3;
  2593. BOOL bStatus = FALSE;
  2594. if (ucTransportFrequencySelection == ANTFS_AUTO_FREQUENCY_SELECTION)
  2595. ucTransportLayerRadioFrequency = CheckForNewTransportFreq();
  2596. else
  2597. ucTransportLayerRadioFrequency = ucTransportFrequencySelection;
  2598. switch(ucTransportChannelPeriodSelection)
  2599. {
  2600. case BEACON_PERIOD_0_5_HZ:
  2601. usTransportChannelPeriod = 65535;
  2602. break;
  2603. case BEACON_PERIOD_1_HZ:
  2604. usTransportChannelPeriod = 32768;
  2605. break;
  2606. case BEACON_PERIOD_2_HZ:
  2607. usTransportChannelPeriod = 16384;
  2608. break;
  2609. case BEACON_PERIOD_4_HZ:
  2610. usTransportChannelPeriod = 8192;
  2611. break;
  2612. case BEACON_PERIOD_8_HZ:
  2613. default:
  2614. usTransportChannelPeriod = 4096;
  2615. break;
  2616. }
  2617. // Set up the command
  2618. memset(aucTxBuf,0x00,sizeof(aucTxBuf));
  2619. aucTxBuf[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  2620. aucTxBuf[ANTFS_COMMAND_OFFSET] = ANTFS_LINK_ID;
  2621. aucTxBuf[TRANSPORT_CHANNEL_FREQ_OFFSET] = ucTransportLayerRadioFrequency;
  2622. aucTxBuf[TRANSPORT_CHANNEL_PERIOD] = ucTransportChannelPeriodSelection;
  2623. Convert_ULONG_To_Bytes(ulHostSerialNumber,
  2624. &aucTxBuf[HOST_ID_OFFSET+3],
  2625. &aucTxBuf[HOST_ID_OFFSET+2],
  2626. &aucTxBuf[HOST_ID_OFFSET+1],
  2627. &aucTxBuf[HOST_ID_OFFSET]);
  2628. ucTxRetries = 8;
  2629. do
  2630. {
  2631. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucTxBuf,CONNECT_TIMEOUT);
  2632. #if defined(DEBUG_FILE)
  2633. if (eTxComplete == ANTFRAMER_FAIL)
  2634. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Tx error.");
  2635. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2636. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Tx timeout.");
  2637. #endif
  2638. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  2639. if (eTxComplete != ANTFRAMER_PASS)
  2640. {
  2641. #if defined(DEBUG_FILE)
  2642. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Failed sending connect command.");
  2643. #endif
  2644. return RETURN_FAIL;
  2645. }
  2646. // Change channel parameters
  2647. if(ucTransportChannelPeriodSelection != BEACON_PERIOD_KEEP)
  2648. {
  2649. if (pclANT->SetChannelPeriod(ucChannelNumber, usTransportChannelPeriod, MESSAGE_TIMEOUT) == FALSE)
  2650. {
  2651. #if defined(DEBUG_FILE)
  2652. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Failed ANT_SetChannelPeriod().");
  2653. #endif
  2654. return RETURN_SERIAL_ERROR;
  2655. }
  2656. }
  2657. if (pclANT->SetChannelRFFrequency(ucChannelNumber, ucTransportLayerRadioFrequency, MESSAGE_TIMEOUT) == FALSE)
  2658. {
  2659. #if defined(DEBUG_FILE)
  2660. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Failed ANT_SetChannelRFFreq().");
  2661. #endif
  2662. return RETURN_SERIAL_ERROR;
  2663. }
  2664. // Check that the remote device changed its channel parameters as well, and that it is still a valid beacon
  2665. do
  2666. {
  2667. bNewRxEvent = FALSE;
  2668. DSIThread_MutexLock(&stMutexCriticalSection);
  2669. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  2670. {
  2671. UCHAR ucResult = DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, BROADCAST_TIMEOUT);
  2672. if (ucResult != DSI_THREAD_ENONE)
  2673. {
  2674. #if defined(DEBUG_FILE)
  2675. if(ucResult == DSI_THREAD_EOTHER)
  2676. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): CondTimedWait() Failed!");
  2677. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Rx timeout.");
  2678. #endif
  2679. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2680. return RETURN_FAIL;
  2681. }
  2682. }
  2683. bNewRxEvent = FALSE;
  2684. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2685. if (*pbCancel == TRUE)
  2686. {
  2687. #if defined(DEBUG_FILE)
  2688. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Stopped.");
  2689. #endif
  2690. return RETURN_STOP;
  2691. }
  2692. if (ucFoundDeviceState == REMOTE_DEVICE_STATE_TRANS)
  2693. bStatus = TRUE;
  2694. } while ((bStatus == FALSE) && ucRxRetries--);
  2695. if (!bStatus)
  2696. {
  2697. #if defined(DEBUG_FILE)
  2698. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptSwitchFrequency(): Remote device not in transport state.");
  2699. #endif
  2700. IncFreqStaleCount(MINOR_STALE_COUNT); // Investigate why this happens, may be that the remote is not updating it's beacon fast enough.
  2701. return RETURN_FAIL;
  2702. }
  2703. return RETURN_PASS;
  2704. }
  2705. ///////////////////////////////////////////////////////////////////////
  2706. void ANTFSHostChannel::Ping(void)
  2707. {
  2708. UCHAR ucTxRetries;
  2709. ANTFRAMER_RETURN eTxComplete;
  2710. if (eANTFSState == ANTFS_HOST_STATE_TRANSPORT || eANTFSState == ANTFS_HOST_STATE_CONNECTED)
  2711. {
  2712. memset(aucTxBuf, 0x00, sizeof(aucTxBuf));
  2713. aucTxBuf[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  2714. aucTxBuf[ANTFS_COMMAND_OFFSET] = ANTFS_PING_ID;
  2715. ucTxRetries = 8;
  2716. do
  2717. {
  2718. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucTxBuf, PING_TIMEOUT);
  2719. #if defined(DEBUG_FILE)
  2720. if (eTxComplete == ANTFRAMER_FAIL)
  2721. DSIDebug::ThreadWrite("ANTFSHostChannel::Ping(): Tx error.");
  2722. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2723. DSIDebug::ThreadWrite("ANTFSHostChannel::Ping(): Tx timeout.");
  2724. #endif
  2725. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  2726. }
  2727. }
  2728. ///////////////////////////////////////////////////////////////////////
  2729. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptDownload(void)
  2730. {
  2731. UCHAR ucNoRxTicks = 4;
  2732. UCHAR ucAttemptTick = 15;
  2733. UCHAR aucDownloadRequest[16];
  2734. ANTFRAMER_RETURN eTxComplete;
  2735. UCHAR ucCRCReset = 1;
  2736. USHORT usCRCCalc = 0;
  2737. ULONG ulDataOffset;
  2738. BOOL bDone = FALSE;
  2739. ULONG ulLastTransferArrayIndex = 0;
  2740. ULONG ulLastUpdateTime;
  2741. /* //The found device state may not have updated yet if we attempt to ul/dl right after authentication
  2742. if ((ucFoundDeviceState != REMOTE_DEVICE_STATE_TRANS) && (ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY))
  2743. return RETURN_FAIL; // Not in the correct mode.
  2744. */
  2745. ulTransferArrayIndex = 0;
  2746. bReceivedResponse = FALSE;
  2747. bReceivedBurst = FALSE;
  2748. #if defined(DEBUG_FILE)
  2749. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Starting download...");
  2750. #endif
  2751. ulLastUpdateTime = DSIThread_GetSystemTime();
  2752. do
  2753. {
  2754. ulDataOffset = ulTransferDataOffset;
  2755. if (bLargeData)
  2756. {
  2757. if (ulTransferArrayIndex >= 16) // The first 16 bytes of the array form the response packet so don't include it in the data offset.
  2758. ulDataOffset += (ulTransferArrayIndex - 16);
  2759. }
  2760. else
  2761. {
  2762. if (ulTransferArrayIndex >= 8) // The first 8 bytes of the array form the response packet so don't include it in the data offset.
  2763. ulDataOffset += (ulTransferArrayIndex - 8);
  2764. }
  2765. if ((!bReceivedBurst) &&(!bReceivedResponse)) //prevents us from sending requests until the Rx bursts have stopped and been cleared.
  2766. {
  2767. if (ucAttemptTick-- == 0)
  2768. {
  2769. #if defined(DEBUG_FILE)
  2770. DSIDebug::ThreadWrite("Download(): Maximum retry attempts reached.");
  2771. #endif
  2772. return RETURN_FAIL;
  2773. }
  2774. bRxError = FALSE;
  2775. DSIThread_MutexLock(&stMutexCriticalSection);
  2776. bNewRxEvent = FALSE;
  2777. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  2778. {
  2779. UCHAR waitResult = DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, DOWNLOAD_RESYNC_TIMEOUT);
  2780. if(waitResult != DSI_THREAD_ENONE)
  2781. {
  2782. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2783. if (waitResult == DSI_THREAD_ETIMEDOUT)
  2784. {
  2785. #if defined(DEBUG_FILE)
  2786. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDownload(): Fail syncing with remote device.");
  2787. #endif
  2788. return RETURN_FAIL; //if we've waited 10 seconds and there is no incoming messages, something is wrong with the connection
  2789. }
  2790. else
  2791. {
  2792. #if defined(DEBUG_FILE)
  2793. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDownload(): CondTimedWait() Failed!");
  2794. #endif
  2795. return RETURN_FAIL; //If the condition variable is broken, we are in trouble
  2796. }
  2797. }
  2798. if (bNewRxEvent == FALSE) //The way our threads are setup, we should never see this anymore now that the locks are fixed
  2799. {
  2800. #if defined(DEBUG_FILE)
  2801. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDownload():: CondTimedWait false alarm signal.");
  2802. #endif
  2803. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2804. continue;
  2805. }
  2806. }
  2807. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2808. if (*pbCancel == TRUE)
  2809. {
  2810. #if defined(DEBUG_FILE)
  2811. DSIDebug::ThreadWrite("AttemptDownload(): Stopped.");
  2812. #endif
  2813. return RETURN_STOP;
  2814. }
  2815. // Send out the download command.
  2816. memset(aucDownloadRequest, 0x00, sizeof(aucDownloadRequest));
  2817. if (bLargeData)
  2818. {
  2819. aucDownloadRequest[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  2820. aucDownloadRequest[ANTFS_COMMAND_OFFSET] = ANTFS_DOWNLOAD_BIG_ID;
  2821. Convert_USHORT_To_Bytes(usTransferDataFileIndex,
  2822. &aucDownloadRequest[DATA_INDEX_OFFSET + 1],
  2823. &aucDownloadRequest[DATA_INDEX_OFFSET]);
  2824. Convert_ULONG_To_Bytes(ulDataOffset,
  2825. &aucDownloadRequest[DOWNLOAD_DATA_OFFSET_OFFSET + 3],
  2826. &aucDownloadRequest[DOWNLOAD_DATA_OFFSET_OFFSET + 2],
  2827. &aucDownloadRequest[DOWNLOAD_DATA_OFFSET_OFFSET + 1],
  2828. &aucDownloadRequest[DOWNLOAD_DATA_OFFSET_OFFSET]);
  2829. if (ulTransferByteSize) //if the max file size is set
  2830. {
  2831. ULONG ulLocalBlockSize = MAX_ULONG;
  2832. if(ulHostBlockSize)
  2833. ulLocalBlockSize = ulHostBlockSize;
  2834. //We need to set the block size to the max transfer size - the offset.
  2835. if(ulLocalBlockSize > (ulTransferByteSize - (ulDataOffset - ulTransferDataOffset)))
  2836. ulLocalBlockSize = ulTransferByteSize - (ulDataOffset - ulTransferDataOffset);
  2837. Convert_ULONG_To_Bytes(ulLocalBlockSize,
  2838. &aucDownloadRequest[DOWNLOAD_MAX_BLOCK_SIZE_OFFSET + 3],
  2839. &aucDownloadRequest[DOWNLOAD_MAX_BLOCK_SIZE_OFFSET + 2],
  2840. &aucDownloadRequest[DOWNLOAD_MAX_BLOCK_SIZE_OFFSET + 1],
  2841. &aucDownloadRequest[DOWNLOAD_MAX_BLOCK_SIZE_OFFSET]);
  2842. }
  2843. if (ulTransferArrayIndex > 16) // If the download has progressed at least to the first data packet
  2844. ucCRCReset = 0; // Clear the Initial download request bit, so the client knows to start checking our CRC
  2845. aucDownloadRequest[DOWNLOAD_INITIAL_REQUEST_OFFSET] = ucCRCReset; // Set initial request byte
  2846. if (ucCRCReset == 0) // If this is not the initial request
  2847. { // Calculate and send a non-zero CRC value
  2848. if (pucTransferBuffer != NULL) // Just to make sure the transfer buffer has been created
  2849. usCRCCalc = CRC_Calc16(&pucTransferBuffer[16], ulDataOffset-ulTransferDataOffset); //CRC_UpdateCRC16
  2850. else
  2851. usCRCCalc = 0;
  2852. Convert_USHORT_To_Bytes(usCRCCalc,
  2853. &aucDownloadRequest[DOWNLOAD_CRC_SEED_OFFSET + 1],
  2854. &aucDownloadRequest[DOWNLOAD_CRC_SEED_OFFSET]);
  2855. }
  2856. }
  2857. else
  2858. {
  2859. aucDownloadRequest[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  2860. aucDownloadRequest[ANTFS_COMMAND_OFFSET] = ANTFS_DOWNLOAD_SMALL_ID;
  2861. Convert_USHORT_To_Bytes(usTransferDataFileIndex,
  2862. &aucDownloadRequest[DATA_INDEX_OFFSET + 1],
  2863. &aucDownloadRequest[DATA_INDEX_OFFSET]);
  2864. Convert_ULONG_To_Bytes(ulDataOffset,
  2865. (UCHAR*)NULL,
  2866. (UCHAR*)NULL,
  2867. &aucDownloadRequest[DATA_OFFSET_SMALL_OFFSET + 1],
  2868. &aucDownloadRequest[DATA_OFFSET_SMALL_OFFSET]);
  2869. Convert_ULONG_To_Bytes(ulTransferByteSize,
  2870. (UCHAR*)NULL,
  2871. (UCHAR*)NULL,
  2872. &aucDownloadRequest[MAX_BLOCK_SIZE_SMALL_OFFSET + 1],
  2873. &aucDownloadRequest[MAX_BLOCK_SIZE_SMALL_OFFSET]);
  2874. }
  2875. if (bLargeData)
  2876. eTxComplete = pclANT->SendANTFSTransfer(ucChannelNumber, (UCHAR*)NULL, (UCHAR*)NULL, aucDownloadRequest, 16, ACKNOWLEDGED_TIMEOUT, (ULONG*)NULL);
  2877. else
  2878. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucDownloadRequest, ACKNOWLEDGED_TIMEOUT);
  2879. #if defined(DEBUG_FILE)
  2880. if (eTxComplete == ANTFRAMER_FAIL)
  2881. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Tx error sending download command.");
  2882. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  2883. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Tx timeout sending download command.");
  2884. #endif
  2885. //Removed the immediate retries to avoid the issue of queing up requests while we get a response for a request we thought we failed.
  2886. if (eTxComplete == ANTFRAMER_TIMEOUT)
  2887. return RETURN_FAIL;
  2888. }
  2889. //Do not need to clear bReceivedBurst here because it will be done if the transfer fails, if we timeout, or receive a broadcast.
  2890. //Now we wait for a response...
  2891. while (bDone == FALSE)
  2892. {
  2893. //Wait for an rxEvent before starting to check the data
  2894. //Since this event is fired for many circumstances we manage all the error checking below and
  2895. //just use this for the wait functionality.
  2896. DSIThread_MutexLock(&stMutexCriticalSection);
  2897. bNewRxEvent = FALSE;
  2898. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  2899. {
  2900. DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, cfgParams.ul_cfg_burst_check_timeout);
  2901. }
  2902. DSIThread_MutexUnlock(&stMutexCriticalSection);
  2903. if (*pbCancel == TRUE)
  2904. {
  2905. #if defined(DEBUG_FILE)
  2906. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDownload(): Stopped.");
  2907. #endif
  2908. return RETURN_STOP;
  2909. }
  2910. if (bReceivedResponse) //If a response has been received, process it.
  2911. {
  2912. bReceivedResponse = FALSE; //Clear these for any potential retries
  2913. bReceivedBurst = FALSE; //Clearing this here allows for quicker retries, otherwise we would have to wait for an incoming broadcast to clear it
  2914. bDone = TRUE; //Mark that we are done... for now.
  2915. if ((pucTransferBufferDynamic) && ((pucTransferBufferDynamic[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_DOWNLOAD_SMALL_ID) || (pucTransferBufferDynamic[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_DOWNLOAD_BIG_ID)))
  2916. {
  2917. /*if (pucTransferBufferDynamic[DOWNLOAD_RESPONSE_OFFSET] == DOWNLOAD_RESPONSE_CRC_FAILED)
  2918. {
  2919. #if defined(DEBUG_FILE)
  2920. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): CRC failed, download failed.");
  2921. #endif
  2922. //clear variables to retry from start.
  2923. ucCRCReset = 1;
  2924. usCRCCalc = 0;
  2925. ulTransferArrayIndex = 16;
  2926. ulLastTransferArrayIndex = 16;
  2927. bDone = FALSE;
  2928. break;
  2929. }
  2930. else*/ //Removed Failed CRC response check and automatic retry. Return the response to the application and allow it to decide to retry or to skip the file. Can also use Recover Transfer data to recover partial files as this seems to be the most way a corrupted file will fail.
  2931. if (pucTransferBufferDynamic[DOWNLOAD_RESPONSE_OFFSET] != DOWNLOAD_RESPONSE_OK)
  2932. {
  2933. #if defined(DEBUG_FILE)
  2934. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Download request rejected.");
  2935. #endif
  2936. ucRejectCode = pucTransferBufferDynamic[DOWNLOAD_RESPONSE_OFFSET];
  2937. return RETURN_REJECT;
  2938. }
  2939. }
  2940. else
  2941. {
  2942. #if defined(DEBUG_FILE)
  2943. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Unknown response during download.");
  2944. #endif
  2945. return RETURN_FAIL;
  2946. }
  2947. // Check if we need to check the CRC
  2948. if ((bLargeData) &&
  2949. (((ulDataOffset - ulTransferDataOffset) + ulTransferBytesInBlock + 8) < ulTransferArrayIndex)) //if there is one more packet beyond the data, we will process the CRC
  2950. {
  2951. ULONG ulCRCLocation, ulLength;
  2952. USHORT usReceivedCRC;
  2953. USHORT usCalcCRC;
  2954. ulLength = (ulDataOffset - ulTransferDataOffset) + ulTransferBytesInBlock; //find length of actual data that needs to be CRC checked
  2955. ulCRCLocation = ulTransferArrayIndex - 2; //Assume that the CRC will be the last 2 bytes of the last packet received
  2956. ulTransferArrayIndex = ulLength + 16; //correct ulTransferArrayIndex in case we are downloading in odd blocks.
  2957. usReceivedCRC = pucTransferBufferDynamic[ulCRCLocation];
  2958. usReceivedCRC |= ((USHORT)pucTransferBufferDynamic[ulCRCLocation + 1] << 8);
  2959. usCalcCRC = CRC_Calc16(&pucTransferBufferDynamic[16], ulLength); //Calculate the CRC of the received data from the start, this should always be what the CRC value is based in becaue we pass in the initial seed
  2960. if (usCalcCRC != usReceivedCRC)
  2961. {
  2962. #if defined(DEBUG_FILE)
  2963. char cBuffer[256];
  2964. SNPRINTF(cBuffer, 256, "ANTFSHostChannel::Download(): Failed CRC Check. Expected %d, Got %d",usCalcCRC, usReceivedCRC);
  2965. DSIDebug::ThreadWrite(cBuffer);
  2966. #endif
  2967. //return RETURN_FAIL;
  2968. //clear variables to retry from start.
  2969. ucCRCReset = 1;
  2970. usCRCCalc = 0;
  2971. ulTransferArrayIndex = 16;
  2972. ulLastTransferArrayIndex = 16;
  2973. bDone = FALSE;
  2974. }
  2975. }
  2976. //Catch the cases where we get a completed transfer but there are more packets coming
  2977. if ((bLargeData) && ((ulTransferArrayIndex -16) < ulTransferTotalBytesRemaining))
  2978. bDone = FALSE;
  2979. ucNoRxTicks = 4; // Reset counter
  2980. ucAttemptTick = 15; // Reset counter for attempts of request
  2981. break;
  2982. }
  2983. if (bRxError)
  2984. {
  2985. #if defined(DEBUG_FILE)
  2986. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Rx error.");
  2987. #endif
  2988. break;
  2989. }
  2990. if (ulTransferArrayIndex > ulLastTransferArrayIndex)
  2991. {
  2992. ulLastUpdateTime = DSIThread_GetSystemTime();
  2993. ulLastTransferArrayIndex = ulTransferArrayIndex;
  2994. }
  2995. else
  2996. {
  2997. if ((DSIThread_GetSystemTime() - ulLastUpdateTime) > DOWNLOAD_LOOP_TIMEOUT)
  2998. {
  2999. #if defined(DEBUG_FILE)
  3000. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Timeout receiving packets.");
  3001. #endif
  3002. return RETURN_FAIL;
  3003. }
  3004. }
  3005. if (!bReceivedBurst)
  3006. {
  3007. if ((ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY) && (ucNoRxTicks > 0))
  3008. ucNoRxTicks--;
  3009. if (ucNoRxTicks == 0)
  3010. {
  3011. ucNoRxTicks = 4;
  3012. #if defined(DEBUG_FILE)
  3013. DSIDebug::ThreadWrite("ANTFSHostChannel::Download(): Not receiving packets.");
  3014. #endif
  3015. break; //If device didn't respond the command didn't make it through or the client device is ignoring it
  3016. }
  3017. }
  3018. else
  3019. {
  3020. ucNoRxTicks = 4; // Reset counter
  3021. ucAttemptTick = 15; // Reset counter for attempts of request
  3022. }
  3023. ReportDownloadProgress();
  3024. } //while();
  3025. } while (!bDone);
  3026. bTransfer = TRUE;
  3027. // Wait for resync.
  3028. DSIThread_MutexLock(&stMutexCriticalSection);
  3029. bNewRxEvent = FALSE;
  3030. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  3031. {
  3032. UCHAR ucResult = DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, BROADCAST_TIMEOUT);
  3033. if (ucResult != DSI_THREAD_ENONE)
  3034. {
  3035. #if defined(DEBUG_FILE)
  3036. if(ucResult == DSI_THREAD_EOTHER)
  3037. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDownload(): CondTimedWait() Failed!");
  3038. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptDownload(): Rx timeout.");
  3039. #endif
  3040. }
  3041. }
  3042. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3043. return RETURN_PASS;
  3044. }
  3045. ///////////////////////////////////////////////////////////////////////
  3046. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::UploadLoop(void)
  3047. {
  3048. RETURN_STATUS eReturn;
  3049. ULONG ulLastProgressValue = 0;
  3050. ULONG ulLastTimeWeGotDataThru = DSIThread_GetSystemTime();
  3051. UCHAR ucFreshRetries = 3;
  3052. #if defined(DEBUG_FILE)
  3053. UCHAR aucString[256];
  3054. #endif
  3055. do
  3056. {
  3057. //ulLastProgressValue = ulUploadIndexProgress; // figure out where we thought we left off last time, this may be a little further than we actually got, but it will be okay unless the USB device has a massive amount of buffering or is broken.
  3058. eReturn = AttemptUpload();
  3059. switch (eReturn)
  3060. {
  3061. case RETURN_PASS:
  3062. if ((ulUploadIndexProgress - UPLOAD_DATA_MESG_OVERHEAD) >= ulTransferByteSize)
  3063. return RETURN_PASS;
  3064. if (ulUploadIndexProgress > ulLastProgressValue + UPLOAD_DATA_MESG_OVERHEAD) //we use the overhead size for the check here because the return was a pass
  3065. {
  3066. bForceUploadOffset = FALSE; //clear the force offset if we get any data across
  3067. ulLastTimeWeGotDataThru = DSIThread_GetSystemTime();
  3068. ulLastProgressValue = ulUploadIndexProgress;
  3069. }
  3070. #if defined(DEBUG_FILE)
  3071. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::UploadLoop(): RETURN_PASS - %lu", ulLastTimeWeGotDataThru);
  3072. DSIDebug::ThreadWrite((char *) aucString);
  3073. #endif
  3074. break;
  3075. case RETURN_FAIL:
  3076. if (ulUploadIndexProgress > ulLastProgressValue + UPLOAD_PROGRESS_CHECK_BYTES) //we use the larger check size here because the return was a failure
  3077. {
  3078. bForceUploadOffset = FALSE; //clear the force offset if we get any data across
  3079. ulLastTimeWeGotDataThru = DSIThread_GetSystemTime();
  3080. ulLastProgressValue = ulUploadIndexProgress;
  3081. }
  3082. #if defined(DEBUG_FILE)
  3083. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::UploadLoop(): RETURN_FAIL - %lu", ulLastTimeWeGotDataThru);
  3084. DSIDebug::ThreadWrite((char *) aucString);
  3085. #endif
  3086. break;
  3087. case RETURN_NA: //This means we have failed CRC or the device returned a weird last offset.
  3088. if (ucFreshRetries--)
  3089. {
  3090. ulUploadIndexProgress = 0; //reset the progress
  3091. ulLastProgressValue = 0;
  3092. ulLastTimeWeGotDataThru = DSIThread_GetSystemTime(); //reset the time
  3093. }
  3094. else
  3095. {
  3096. return RETURN_FAIL;
  3097. }
  3098. break;
  3099. case RETURN_REJECT:
  3100. case RETURN_STOP:
  3101. default:
  3102. return eReturn;
  3103. break;
  3104. }
  3105. }
  3106. while ((DSIThread_GetSystemTime() - ulLastTimeWeGotDataThru) < UPLOAD_LOOP_TIMEOUT);
  3107. return RETURN_FAIL;
  3108. }
  3109. ///////////////////////////////////////////////////////////////////////
  3110. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptUpload(void)
  3111. {
  3112. BOOL bStatus = FALSE;
  3113. UCHAR aucTxUpload[16];
  3114. UCHAR aucUploadHeader[8];
  3115. UCHAR aucUploadFooter[8];
  3116. ULONG ulStartTime;
  3117. UCHAR ucTxRetries;
  3118. ANTFRAMER_RETURN eTxComplete;
  3119. ULONG ulLastOffset;
  3120. ULONG ulMaxBlockSize, ulMaxFileSize, ulUploadTimeout, ulLocalTransferSize;
  3121. ANTFRAMER_RETURN eReturn;
  3122. USHORT usReceivedCRC;
  3123. USHORT usCalculatedCRC;
  3124. USHORT usCRCSeed;
  3125. UCHAR ucUploadResponse;
  3126. //The found device state may not have updated yet if we attempt to ul/dl right after authentication, but addition of ULLoop allows us to do this check
  3127. if ((ucFoundDeviceState != REMOTE_DEVICE_STATE_TRANS) && (ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY))
  3128. {
  3129. #if defined(DEBUG_FILE)
  3130. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Remote device is not in correct state.");
  3131. #endif
  3132. return RETURN_FAIL; // Not in the correct mode.
  3133. }
  3134. //ulTransferArrayIndex = 0;
  3135. bReceivedResponse = FALSE;
  3136. bRxError = FALSE;
  3137. bReceivedBurst = FALSE;
  3138. memset(aucTxUpload, 0x00, sizeof(aucTxUpload));
  3139. ULONG ulMaxTransferIndex = ulTransferDataOffset + ulTransferByteSize;
  3140. // Build upload request
  3141. aucTxUpload[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  3142. aucTxUpload[ANTFS_COMMAND_OFFSET] = ANTFS_UPLOAD_BIG_ID;
  3143. Convert_USHORT_To_Bytes(usTransferDataFileIndex,
  3144. &aucTxUpload[DATA_INDEX_OFFSET + 1],
  3145. &aucTxUpload[DATA_INDEX_OFFSET]);
  3146. Convert_ULONG_To_Bytes(ulMaxTransferIndex,
  3147. &aucTxUpload[UPLOAD_MAX_SIZE_OFFSET + 3],
  3148. &aucTxUpload[UPLOAD_MAX_SIZE_OFFSET + 2],
  3149. &aucTxUpload[UPLOAD_MAX_SIZE_OFFSET + 1],
  3150. &aucTxUpload[UPLOAD_MAX_SIZE_OFFSET]);
  3151. if (bForceUploadOffset)
  3152. {
  3153. Convert_ULONG_To_Bytes(ulTransferDataOffset,
  3154. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET + 3],
  3155. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET + 2],
  3156. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET + 1],
  3157. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET]);
  3158. }
  3159. else
  3160. {
  3161. Convert_ULONG_To_Bytes(MAX_ULONG,
  3162. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET + 3],
  3163. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET + 2],
  3164. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET + 1],
  3165. &aucTxUpload[UPLOAD_DATA_OFFSET_OFFSET]);
  3166. }
  3167. bNewRxEvent = FALSE;
  3168. ucTxRetries = 8;
  3169. // Try sending upload request until success or retries exhausted.
  3170. eTxComplete = pclANT->SendANTFSTransfer(ucChannelNumber, (UCHAR*)NULL, (UCHAR*)NULL, aucTxUpload, 16, ACKNOWLEDGED_TIMEOUT, (ULONG*)NULL);
  3171. #if defined(DEBUG_FILE)
  3172. if (eTxComplete == ANTFRAMER_FAIL)
  3173. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Tx error sending upload command.");
  3174. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  3175. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Tx timeout sending upload command.");
  3176. #endif
  3177. //Removed the immediate retries to avoid the issue of queing up requests while we get a response for a request we thought we failed.
  3178. if (eTxComplete == ANTFRAMER_TIMEOUT)
  3179. {
  3180. #if defined(DEBUG_FILE)
  3181. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Tx failed.");
  3182. #endif
  3183. return RETURN_FAIL;
  3184. }
  3185. ulStartTime = DSIThread_GetSystemTime();
  3186. ucLinkResponseRetries = ANTFS_RESPONSE_RETRIES;
  3187. do
  3188. {
  3189. DSIThread_MutexLock(&stMutexCriticalSection);
  3190. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  3191. {
  3192. DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, 1000);
  3193. }
  3194. bNewRxEvent = FALSE;
  3195. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3196. if (*pbCancel == TRUE)
  3197. {
  3198. #if defined(DEBUG_FILE)
  3199. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptUpload(): Stopped.");
  3200. #endif
  3201. return RETURN_STOP;
  3202. }
  3203. if (bReceivedResponse)
  3204. {
  3205. if (aucTransferBufferFixed[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_UPLOAD_ID)
  3206. {
  3207. if (aucTransferBufferFixed[UPLOAD_RESPONSE_OFFSET] == UPLOAD_RESPONSE_OK)
  3208. {
  3209. bStatus = TRUE;
  3210. bLargeData = TRUE;
  3211. }
  3212. else
  3213. {
  3214. ucRejectCode = aucTransferBufferFixed[UPLOAD_RESPONSE_OFFSET];
  3215. return RETURN_REJECT;
  3216. }
  3217. }
  3218. }
  3219. #if defined(DEBUG_FILE)
  3220. else
  3221. {
  3222. DSIDebug::ThreadWrite("-->Requesting Upload...");
  3223. }
  3224. #endif
  3225. if ((bRxError) || (ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY))
  3226. {
  3227. #if defined(DEBUG_FILE)
  3228. if (bRxError)
  3229. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Rx error");
  3230. else
  3231. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Non-busy beacon");
  3232. #endif
  3233. if (ucLinkResponseRetries == 0)
  3234. {
  3235. #if defined(DEBUG_FILE)
  3236. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Retries exhausted");
  3237. #endif
  3238. return RETURN_FAIL;
  3239. }
  3240. else
  3241. {
  3242. #if defined(DEBUG_FILE)
  3243. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Retrying");
  3244. #endif
  3245. ucLinkResponseRetries--;
  3246. bRxError = FALSE;
  3247. }
  3248. }
  3249. } while ((bStatus == FALSE) && ((DSIThread_GetSystemTime() - ulStartTime) < cfgParams.ul_cfg_upload_request_timeout));
  3250. if (!bStatus)
  3251. {
  3252. #if defined(DEBUG_FILE)
  3253. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Timeout.");
  3254. #endif
  3255. return RETURN_FAIL;
  3256. }
  3257. bReceivedResponse = FALSE;
  3258. bNewRxEvent = FALSE;
  3259. ucUploadResponse = aucTransferBufferFixed[UPLOAD_RESPONSE_OFFSET];
  3260. if (!ucUploadResponse) // it's accepting the upload
  3261. {
  3262. // Decode response
  3263. ulLastOffset = Convert_Bytes_To_ULONG(aucTransferBufferFixed[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET + 3],
  3264. aucTransferBufferFixed[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET + 2],
  3265. aucTransferBufferFixed[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET + 1],
  3266. aucTransferBufferFixed[UPLOAD_RESPONSE_LAST_OFFSET_OFFSET]);
  3267. ulMaxBlockSize = Convert_Bytes_To_ULONG(aucTransferBufferFixed[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 3],
  3268. aucTransferBufferFixed[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 2],
  3269. aucTransferBufferFixed[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 1],
  3270. aucTransferBufferFixed[UPLOAD_RESPONSE_BLOCK_SIZE_OFFSET]);
  3271. usReceivedCRC = Convert_Bytes_To_USHORT(aucTransferBufferFixed[UPLOAD_RESPONSE_CRC_OFFSET + 1],
  3272. aucTransferBufferFixed[UPLOAD_RESPONSE_CRC_OFFSET]);
  3273. ulMaxFileSize = Convert_Bytes_To_ULONG(aucTransferBufferFixed[UPLOAD_RESPONSE_MAX_SIZE_OFFSET + 3],
  3274. aucTransferBufferFixed[UPLOAD_RESPONSE_MAX_SIZE_OFFSET + 2],
  3275. aucTransferBufferFixed[UPLOAD_RESPONSE_MAX_SIZE_OFFSET + 1],
  3276. aucTransferBufferFixed[UPLOAD_RESPONSE_MAX_SIZE_OFFSET]);
  3277. if (ulMaxTransferIndex > ulMaxFileSize)
  3278. {
  3279. #if defined(DEBUG_FILE)
  3280. UCHAR aucString[256];
  3281. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::Upload(): Upload size larger than Max Files size (%lu > %lu)", ulMaxTransferIndex, ulMaxFileSize);
  3282. DSIDebug::ThreadWrite((char *) aucString);
  3283. #endif
  3284. ucRejectCode = UPLOAD_RESPONSE_INSUFFICIENT_SPACE;
  3285. return RETURN_REJECT;
  3286. }
  3287. if (bForceUploadOffset) // Override the suggested offset if we are instructed to.
  3288. {
  3289. usCRCSeed = 0;
  3290. ulLastOffset = ulTransferDataOffset; // We will start from where the app told us to
  3291. ulLocalTransferSize = ulTransferByteSize; // and set the next transfer size to the correct value
  3292. }
  3293. else //we need to check the CRC
  3294. {
  3295. if (ulLastOffset > ulMaxTransferIndex) //The last offset we got is larger than our max, we are just going to force a restart from where we want to start
  3296. {
  3297. #if defined(DEBUG_FILE)
  3298. UCHAR aucString[256];
  3299. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::Upload(): ulLastOffset from device is larger than our max offset - Restarting UL (%lu > %lu)", ulLastOffset, ulMaxTransferIndex);
  3300. DSIDebug::ThreadWrite((char *) aucString);
  3301. #endif
  3302. bForceUploadOffset = TRUE;
  3303. return RETURN_NA;
  3304. }
  3305. usCRCSeed = CRC_Calc16(&pucUploadData[0], ulLastOffset-ulTransferDataOffset);
  3306. if (usCRCSeed != usReceivedCRC)
  3307. {
  3308. #if defined(DEBUG_FILE)
  3309. UCHAR aucString[256];
  3310. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::Upload(): CRC Check Failed - Wanted 0x%04X, Got 0x%04X", usCRCSeed, usReceivedCRC);
  3311. DSIDebug::ThreadWrite((char *) aucString);
  3312. #endif
  3313. //We are going to send no data.
  3314. ulLocalTransferSize = 0;
  3315. //ulLastOffset = 0; // we're going to start over if the client doesn't have the right CRC
  3316. bForceUploadOffset = TRUE;
  3317. return RETURN_NA;
  3318. }
  3319. }
  3320. if (ulLastOffset == ulMaxTransferIndex)
  3321. {
  3322. #if defined(DEBUG_FILE)
  3323. UCHAR aucString[256];
  3324. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::Upload(): We think we were sucessful Last time - UploadIndexProgress %lu, TransferByteSize %lu", ulUploadIndexProgress, ulTransferByteSize);
  3325. DSIDebug::ThreadWrite((char *) aucString);
  3326. #endif
  3327. //ulLastOffset = 0;
  3328. ulLocalTransferSize = 0; //Send only the header and try if we think we actually completed last time and only failed getting the response
  3329. }
  3330. else
  3331. {
  3332. if (ulLastOffset > ulTransferDataOffset) //Check if there is a suggested last offset and if it's further along than our original offset
  3333. {
  3334. ulLocalTransferSize = ulTransferByteSize - (ulLastOffset - ulTransferDataOffset); //If it is then we'll use it, and adjust the size of the next burst
  3335. }
  3336. else
  3337. {
  3338. ulLastOffset = ulTransferDataOffset; // We will start from where the app told us to
  3339. ulLocalTransferSize = ulTransferByteSize; // and set the next transfer size to the correct value
  3340. }
  3341. }
  3342. if (ulLocalTransferSize) // If we are actually sending data
  3343. ulUploadIndexProgress = ulLastOffset - ulTransferDataOffset; // Set the progress to start at the offset
  3344. else
  3345. ulUploadIndexProgress = ulTransferByteSize; // Otherwise, we are already complete and just need confirmation, so set the progress back to the transfer size (the transfer function will keep trying to add to this because of the header)
  3346. if(ulMaxBlockSize < ulLocalTransferSize) // Check if we exceed the max client block size
  3347. ulLocalTransferSize = ulMaxBlockSize;
  3348. if(ulHostBlockSize < ulLocalTransferSize) // Check if we exceed the max host block size
  3349. ulLocalTransferSize = ulHostBlockSize;
  3350. if (ulLocalTransferSize)
  3351. {
  3352. usCalculatedCRC = CRC_Calc16(&pucUploadData[0], (ulLastOffset - ulTransferDataOffset) + ulLocalTransferSize); // compute the CRC from the start up till the end of the current block
  3353. }
  3354. else
  3355. {
  3356. usCalculatedCRC = usCRCSeed;
  3357. }
  3358. //Add CRC to the footer.
  3359. memset(aucUploadFooter,0x00,sizeof(aucUploadFooter));
  3360. Convert_USHORT_To_Bytes(usCalculatedCRC,
  3361. &aucUploadFooter[UPLOAD_DATA_CRC_OFFSET + 1],
  3362. &aucUploadFooter[UPLOAD_DATA_CRC_OFFSET]); //put CRC at the end of the footer packet
  3363. // figure out our timeout value from the size of the transfer
  3364. if (ulLocalTransferSize > ((MAX_ULONG - BROADCAST_TIMEOUT) / 2))
  3365. ulUploadTimeout = (MAX_ULONG - 1);
  3366. else
  3367. ulUploadTimeout = BROADCAST_TIMEOUT + (ulTransferByteSize * 2);
  3368. // fill in the header information
  3369. aucUploadHeader[0] = ANTFS_COMMAND_RESPONSE_ID;
  3370. aucUploadHeader[1] = ANTFS_UPLOAD_DATA_ID;
  3371. Convert_USHORT_To_Bytes(usCRCSeed,
  3372. &aucUploadHeader[UPLOAD_DATA_CRC_SEED_OFFSET + 1],
  3373. &aucUploadHeader[UPLOAD_DATA_CRC_SEED_OFFSET]);
  3374. if (ulLastOffset == ulMaxTransferIndex) // If we think we are done, we will fill zero into the offset but we send no data.
  3375. { // This should result in only a complete to the embedded application.
  3376. Convert_ULONG_To_Bytes(0,
  3377. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET + 3],
  3378. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET + 2],
  3379. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET + 1],
  3380. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET]);
  3381. }
  3382. else
  3383. {
  3384. Convert_ULONG_To_Bytes(ulLastOffset,
  3385. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET + 3],
  3386. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET + 2],
  3387. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET + 1],
  3388. &aucUploadHeader[UPLOAD_DATA_DATA_OFFSET_OFFSET]);
  3389. }
  3390. //Send the Burst
  3391. eReturn = pclANT->SendANTFSTransfer(ucChannelNumber, aucUploadHeader, aucUploadFooter, &pucUploadData[ulLastOffset], ulLocalTransferSize, ulUploadTimeout, &ulUploadIndexProgress);
  3392. if (eReturn == ANTFRAMER_FAIL)
  3393. {
  3394. #if defined(DEBUG_FILE)
  3395. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Failed Actual Upload Transfer.");
  3396. #endif
  3397. //fall thru to catch response
  3398. }
  3399. else if (eReturn == ANTFRAMER_CANCELLED)
  3400. {
  3401. #if defined(DEBUG_FILE)
  3402. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptUpload(): Stopped2.");
  3403. #endif
  3404. return RETURN_STOP;
  3405. }
  3406. else if (eReturn == ANTFRAMER_TIMEOUT)
  3407. {
  3408. #if defined(DEBUG_FILE)
  3409. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptUpload(): Tx transfer timeout.");
  3410. #endif
  3411. return RETURN_FAIL;
  3412. }
  3413. }
  3414. else
  3415. {
  3416. #if defined(DEBUG_FILE)
  3417. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Rejected by Client.");
  3418. #endif
  3419. ucRejectCode = ucUploadResponse;
  3420. return RETURN_FAIL;
  3421. }
  3422. ulStartTime = DSIThread_GetSystemTime();
  3423. //ResetEvent(hEventRx);
  3424. ucLinkResponseRetries = ANTFS_RESPONSE_RETRIES;
  3425. bStatus = FALSE;
  3426. do
  3427. {
  3428. DSIThread_MutexLock(&stMutexCriticalSection);
  3429. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  3430. {
  3431. DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, 1000);
  3432. }
  3433. bNewRxEvent = FALSE;
  3434. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3435. if (*pbCancel == TRUE)
  3436. {
  3437. #if defined(DEBUG_FILE)
  3438. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptUpload(): Stopped.");
  3439. #endif
  3440. return RETURN_STOP;
  3441. }
  3442. if ((bRxError) || (ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY))
  3443. {
  3444. #if defined(DEBUG_FILE)
  3445. if (bRxError)
  3446. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Rx error2");
  3447. else
  3448. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Non-busy beacon2");
  3449. #endif
  3450. if (ucLinkResponseRetries == 0)
  3451. {
  3452. #if defined(DEBUG_FILE)
  3453. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Retries exhausted2");
  3454. #endif
  3455. return RETURN_FAIL;
  3456. }
  3457. else
  3458. {
  3459. #if defined(DEBUG_FILE)
  3460. DSIDebug::ThreadWrite("ANTFSHostChannel::Upload(): Retrying2");
  3461. #endif
  3462. ucLinkResponseRetries--;
  3463. bRxError = FALSE;
  3464. }
  3465. }
  3466. if (bReceivedResponse)
  3467. {
  3468. if (aucTransferBufferFixed[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_UPLOAD_COMPLETE_ID)
  3469. {
  3470. if (aucTransferBufferFixed[UPLOAD_RESPONSE_OFFSET] == UPLOAD_RESPONSE_OK)
  3471. {
  3472. return RETURN_PASS;
  3473. }
  3474. else
  3475. {
  3476. return RETURN_FAIL;
  3477. }
  3478. }
  3479. }
  3480. #if defined(DEBUG_FILE)
  3481. else
  3482. {
  3483. DSIDebug::ThreadWrite("-->Waiting for upload complete...");
  3484. }
  3485. #endif
  3486. } while ((bStatus == FALSE) && ((DSIThread_GetSystemTime() - ulStartTime) < cfgParams.ul_cfg_upload_response_timeout));
  3487. return RETURN_FAIL;
  3488. }
  3489. ///////////////////////////////////////////////////////////////////////
  3490. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptManualTransfer(void)
  3491. {
  3492. ULONG ulBlockSize;
  3493. USHORT usBlockOffset;
  3494. // Convert bytes remaining to blocks remaining.
  3495. if (ulTransferByteSize % 8)
  3496. ulBlockSize = 1; // There is a remainder, so we need to send one extra payload to hold all the data.
  3497. else
  3498. ulBlockSize = 0;
  3499. ulBlockSize += ulTransferByteSize / 8;
  3500. // Convert byte offset to block offset.
  3501. usBlockOffset = (USHORT) ulTransferDataOffset / 8; // The remainder must be dropped when computing offset.
  3502. ulTransferArrayIndex = 0;
  3503. bReceivedBurst = FALSE;
  3504. bReceivedResponse = FALSE;
  3505. if (*pbCancel == TRUE)
  3506. return RETURN_STOP;
  3507. aucSendDirectBuffer[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  3508. aucSendDirectBuffer[ANTFS_COMMAND_OFFSET] = ANTFS_SEND_DIRECT_ID;
  3509. Convert_USHORT_To_Bytes(usTransferDataFileIndex,
  3510. &aucSendDirectBuffer[DATA_INDEX_OFFSET + 1],
  3511. &aucSendDirectBuffer[DATA_INDEX_OFFSET]);
  3512. Convert_USHORT_To_Bytes(usBlockOffset,
  3513. &aucSendDirectBuffer[DATA_OFFSET_OFFSET_HIGH],
  3514. &aucSendDirectBuffer[DATA_OFFSET_OFFSET_LOW]);
  3515. Convert_ULONG_To_Bytes(ulBlockSize - 1, // Size - 1, as per the spec.
  3516. (UCHAR*)NULL,
  3517. (UCHAR*)NULL,
  3518. &aucSendDirectBuffer[DATA_BLOCK_SIZE_OFFSET_HIGH],
  3519. &aucSendDirectBuffer[DATA_BLOCK_SIZE_OFFSET_LOW]);
  3520. #if 0
  3521. #if defined(DEBUG_FILE)
  3522. {
  3523. char acString[256];
  3524. int iCounter;
  3525. ULONG ulCounter2;
  3526. SNPRINTF(acString, 256, "ANTFSHostChannel::ManualTransfer(): ulTransferByteSize = %lu; ulTransferDataOffset = %lu.", ulTransferByteSize, ulTransferDataOffset);
  3527. DSIDebug::ThreadWrite(acString);
  3528. for (ulCounter2 = 0; ulCounter2 < ulBlockSize; ulCounter2++)
  3529. {
  3530. SNPRINTF(acString, 256, "ANTFSHostChannel::ManualTransfer(): <%lu>", ulCounter2);
  3531. for (iCounter = 0; iCounter < 8; iCounter++)
  3532. {
  3533. char acSubString[10];
  3534. SNPRINTF(acSubString, 10, "[%02X]", aucSendDirectBuffer[ulCounter2 + (ULONG) iCounter]);
  3535. SNPRINTF(acString, 256, "%s%s", acString, acSubString);
  3536. }
  3537. DSIDebug::ThreadWrite(acString);
  3538. }
  3539. }
  3540. #endif
  3541. #endif
  3542. ulUploadIndexProgress = 0;
  3543. //pclANT->SendANTFSTransfer(ucChannelNumber, aucSendDirectBuffer, &aucSendDirectBuffer[8], (ulBlockSize + 1)*8, 120000, &ulUploadIndexProgress); // Size + 1 to account for the command payload.
  3544. pclANT->SendANTFSTransfer(ucChannelNumber, (UCHAR*)NULL, (UCHAR*)NULL, aucSendDirectBuffer, (ulBlockSize + 1)*8, 120000, &ulUploadIndexProgress); // Size + 1 to account for the command payload.
  3545. //pclANT->SendTransfer(ANTFS_CHANNEL, aucSendDirectBuffer, (ulBlockSize + 1)*8,120000); // Size + 1 to account for the command payload.
  3546. //allow the receive function to return with error, even if the Tx transfer failed
  3547. return Receive();
  3548. }
  3549. ///////////////////////////////////////////////////////////////////////
  3550. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::Receive(void)
  3551. {
  3552. ULONG ulNoRxTime = DSIThread_GetSystemTime();
  3553. UCHAR ucTicks = ucTransportBeaconTicks;
  3554. bNewRxEvent = FALSE;
  3555. eANTFSState = ANTFS_HOST_STATE_RECEIVING;
  3556. while (bKillThread == FALSE)
  3557. {
  3558. DSIThread_MutexLock(&stMutexCriticalSection);
  3559. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  3560. {
  3561. UCHAR ucResult = DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, cfgParams.ul_cfg_burst_check_timeout);
  3562. if (ucResult != DSI_THREAD_ENONE)
  3563. {
  3564. #if defined(DEBUG_FILE)
  3565. if(ucResult == DSI_THREAD_EOTHER)
  3566. DSIDebug::ThreadWrite("ANTFSHostChannel::Receive(): CondTimedWait() Failed!");
  3567. #endif
  3568. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3569. return RETURN_FAIL;
  3570. }
  3571. }
  3572. bNewRxEvent = FALSE;
  3573. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3574. if (*pbCancel == TRUE)
  3575. {
  3576. #if defined(DEBUG_FILE)
  3577. DSIDebug::ThreadWrite("ANTFSHostChannel::Receive(): Stopped.");
  3578. #endif
  3579. return RETURN_STOP;
  3580. }
  3581. if ((ucFoundDeviceState == REMOTE_DEVICE_STATE_TRANS) && ((ucTransportBeaconTicks - ucTicks) >= 5))
  3582. return RETURN_REJECT;
  3583. if (bReceivedResponse && (ucFoundDeviceState == REMOTE_DEVICE_STATE_TRANS))
  3584. {
  3585. if (pucTransferBufferDynamic == NULL)
  3586. {
  3587. #if defined(DEBUG_FILE)
  3588. DSIDebug::ThreadWrite("ANTFSHostChannel::Receive(): pucTransferBufferDynamic is an undefined pointer.");
  3589. #endif
  3590. break;
  3591. }
  3592. if (pucTransferBufferDynamic[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_SEND_DIRECT_ID)
  3593. {
  3594. // If the index, offset and block size bytes are all 0xFF, then the
  3595. // sent data was rejected by the remote device.
  3596. int i;
  3597. BOOL bAll0xFF = TRUE;
  3598. for (i = 2; i < 8; i++)
  3599. {
  3600. if (pucTransferBufferDynamic[i] != 0xFF)
  3601. {
  3602. bAll0xFF = FALSE;
  3603. break;
  3604. }
  3605. }
  3606. if (bAll0xFF)
  3607. return RETURN_REJECT;
  3608. bTransfer = TRUE;
  3609. return RETURN_PASS;
  3610. }
  3611. else
  3612. {
  3613. #if defined(DEBUG_FILE)
  3614. char acString[256];
  3615. SNPRINTF(acString, 256, "ANTFSHostChannel::Receive(): Bad response: [%02X][%02X][%02X][%02X][%02X][%02X][%02X][%02X]",
  3616. pucTransferBufferDynamic[0],
  3617. pucTransferBufferDynamic[1],
  3618. pucTransferBufferDynamic[2],
  3619. pucTransferBufferDynamic[3],
  3620. pucTransferBufferDynamic[4],
  3621. pucTransferBufferDynamic[5],
  3622. pucTransferBufferDynamic[6],
  3623. pucTransferBufferDynamic[7]);
  3624. DSIDebug::ThreadWrite(acString);
  3625. #endif
  3626. break;
  3627. }
  3628. }
  3629. if (bRxError)
  3630. bRxError = FALSE;
  3631. if (!bReceivedBurst)
  3632. {
  3633. if ((DSIThread_GetSystemTime() - ulNoRxTime) > SEND_DIRECT_BURST_TIMEOUT)
  3634. {
  3635. #if defined(DEBUG_FILE)
  3636. DSIDebug::ThreadWrite("ANTFSHostChannel::Receive(): Timeout waiting for burst packets.");
  3637. #endif
  3638. break;
  3639. }
  3640. else
  3641. {
  3642. #if defined(DEBUG_FILE)
  3643. DSIDebug::ThreadWrite("ANTFSHostChannel::Receive(): Burst packet not received. Retrying...");
  3644. #endif
  3645. }
  3646. }
  3647. else
  3648. {
  3649. ulNoRxTime = DSIThread_GetSystemTime(); // Reset timeout.
  3650. #if defined(DEBUG_FILE)
  3651. DSIDebug::ThreadWrite("ANTFSHostChannel::Receive(): Burst packets received. Reset timeout.");
  3652. #endif
  3653. }
  3654. ReportDownloadProgress();
  3655. }
  3656. return RETURN_FAIL; // Shouldn't reach here.
  3657. }
  3658. ///////////////////////////////////////////////////////////////////////
  3659. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptErase(void)
  3660. {
  3661. BOOL bStatus = FALSE;
  3662. ULONG ulStartTime;
  3663. UCHAR ucTxRetries;
  3664. ANTFRAMER_RETURN eTxComplete;
  3665. if ((ucFoundDeviceState != REMOTE_DEVICE_STATE_TRANS) && (ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY))
  3666. {
  3667. #if defined(DEBUG_FILE)
  3668. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Remote device is not in transport state.");
  3669. #endif
  3670. return RETURN_FAIL; // Not in the correct mode.
  3671. }
  3672. ulTransferArrayIndex = 0;
  3673. bReceivedResponse = FALSE;
  3674. bRxError = FALSE;
  3675. memset(aucTxBuf,0x00,sizeof(aucTxBuf));
  3676. aucTxBuf[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  3677. aucTxBuf[ANTFS_COMMAND_OFFSET] = ANTFS_ERASE_ID;
  3678. Convert_USHORT_To_Bytes(usTransferDataFileIndex,
  3679. &aucTxBuf[DATA_INDEX_OFFSET+1],
  3680. &aucTxBuf[DATA_INDEX_OFFSET]);
  3681. ucTxRetries = 8;
  3682. do
  3683. {
  3684. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucTxBuf, ACKNOWLEDGED_TIMEOUT);
  3685. #if defined(DEBUG_FILE)
  3686. if (eTxComplete == ANTFRAMER_FAIL)
  3687. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Tx error.");
  3688. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  3689. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptConnect(): Tx timeout.");
  3690. #endif
  3691. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  3692. if (eTxComplete != ANTFRAMER_PASS)
  3693. return RETURN_FAIL;
  3694. ulStartTime = DSIThread_GetSystemTime();
  3695. bNewRxEvent = FALSE;
  3696. ucLinkResponseRetries = ANTFS_RESPONSE_RETRIES;
  3697. do
  3698. {
  3699. DSIThread_MutexLock(&stMutexCriticalSection);
  3700. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  3701. {
  3702. DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, ERASE_WAIT_TIMEOUT);
  3703. }
  3704. bNewRxEvent = FALSE;
  3705. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3706. if (*pbCancel == TRUE)
  3707. {
  3708. #if defined(DEBUG_FILE)
  3709. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Stopped.");
  3710. #endif
  3711. return RETURN_STOP;
  3712. }
  3713. if (bReceivedResponse)
  3714. {
  3715. if (aucTransferBufferFixed[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_ERASE_ID)
  3716. {
  3717. if (aucTransferBufferFixed[ERASE_RESPONSE_OFFSET] == ERASE_RESPONSE_OK)
  3718. {
  3719. #if defined(DEBUG_FILE)
  3720. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Erase successful.");
  3721. #endif
  3722. bStatus = TRUE;
  3723. }
  3724. else
  3725. {
  3726. #if defined(DEBUG_FILE)
  3727. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Request rejected.");
  3728. #endif
  3729. bRxError = TRUE;
  3730. }
  3731. }
  3732. }
  3733. #if defined(DEBUG_FILE)
  3734. else
  3735. {
  3736. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Erasing...");
  3737. }
  3738. #endif
  3739. if (bRxError)
  3740. {
  3741. #if defined(DEBUG_FILE)
  3742. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Erase failed.");
  3743. #endif
  3744. if (ucLinkResponseRetries == 0)
  3745. {
  3746. #if defined(DEBUG_FILE)
  3747. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Retries exhausted");
  3748. #endif
  3749. break;
  3750. }
  3751. else
  3752. {
  3753. #if defined(DEBUG_FILE)
  3754. DSIDebug::ThreadWrite("ANTFSHostChannel::Erase(): Retrying");
  3755. #endif
  3756. ucLinkResponseRetries--;
  3757. bRxError = FALSE;
  3758. }
  3759. }
  3760. } while ((bStatus == FALSE) && ((DSIThread_GetSystemTime() - ulStartTime) < cfgParams.ul_cfg_erase_timeout));
  3761. if (!bStatus)
  3762. return RETURN_FAIL;
  3763. return RETURN_PASS;
  3764. }
  3765. ///////////////////////////////////////////////////////////////////////
  3766. ANTFSHostChannel::RETURN_STATUS ANTFSHostChannel::AttemptAuthenticate(void)
  3767. {
  3768. BOOL bStatus = FALSE;
  3769. UCHAR aucTxAuth[8 + TX_PASSWORD_MAX_LENGTH + 8];
  3770. ULONG ulStartTime;
  3771. UCHAR ucTxRetries;
  3772. ANTFRAMER_RETURN eTxComplete;
  3773. BOOL bBeaconStateIncorrect = FALSE;
  3774. if ((ucFoundDeviceState != REMOTE_DEVICE_STATE_AUTH) && (ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY))
  3775. {
  3776. #if defined(DEBUG_FILE)
  3777. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Remote device is not in auth state.");
  3778. #endif
  3779. return RETURN_FAIL; // Not in the correct mode.
  3780. }
  3781. ulTransferArrayIndex = 0;
  3782. bReceivedResponse = FALSE;
  3783. bRxError = FALSE;
  3784. memset(aucTxAuth,0x00,sizeof(aucTxAuth));
  3785. aucTxAuth[ANTFS_CONNECTION_OFFSET] = ANTFS_COMMAND_RESPONSE_ID;
  3786. aucTxAuth[ANTFS_COMMAND_OFFSET] = ANTFS_AUTHENTICATE_ID;
  3787. aucTxAuth[AUTH_COMMAND_TYPE_OFFSET] = ucAuthType;
  3788. aucTxAuth[AUTH_FRIENDLY_NAME_LENGTH_OFFSET] = ucTxPasswordLength;
  3789. Convert_ULONG_To_Bytes(ulHostSerialNumber,
  3790. &aucTxAuth[AUTH_HOST_SERIAL_NUMBER_OFFSET+3],
  3791. &aucTxAuth[AUTH_HOST_SERIAL_NUMBER_OFFSET+2],
  3792. &aucTxAuth[AUTH_HOST_SERIAL_NUMBER_OFFSET+1],
  3793. &aucTxAuth[AUTH_HOST_SERIAL_NUMBER_OFFSET]);
  3794. memcpy(&aucTxAuth[8], aucTxPassword, ucTxPasswordLength);
  3795. if (ucTxPasswordLength) //Do this to not leave the last 8 bytes of the password in the host Tx buffer, but we are not required to do it.
  3796. ucTxPasswordLength = ucTxPasswordLength + 8;
  3797. #if defined(ACCESS_POINT)
  3798. ucTxRetries = 2;
  3799. #else
  3800. ucTxRetries = 8;
  3801. #endif
  3802. do
  3803. {
  3804. if (ucTxPasswordLength)
  3805. eTxComplete = pclANT->SendANTFSTransfer(ucChannelNumber, (UCHAR*)NULL, (UCHAR*)NULL, aucTxAuth, 8+ ucTxPasswordLength, ACKNOWLEDGED_TIMEOUT, (ULONG*)NULL);
  3806. else
  3807. eTxComplete = pclANT->SendAcknowledgedData(ucChannelNumber, aucTxAuth, ACKNOWLEDGED_TIMEOUT);
  3808. #if defined(DEBUG_FILE)
  3809. if (eTxComplete == ANTFRAMER_FAIL)
  3810. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Tx error.");
  3811. else if (eTxComplete == ANTFRAMER_TIMEOUT)
  3812. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Tx timeout.");
  3813. #endif
  3814. } while (eTxComplete == ANTFRAMER_FAIL && --ucTxRetries);
  3815. if (eTxComplete != ANTFRAMER_PASS)
  3816. {
  3817. #if defined(DEBUG_FILE)
  3818. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Tx failed.");
  3819. #endif
  3820. return RETURN_FAIL;
  3821. }
  3822. ulStartTime = DSIThread_GetSystemTime();
  3823. bNewRxEvent = FALSE;
  3824. #if defined(ACCESS_POINT)
  3825. ucLinkResponseRetries = 3; // Current client implementation does not retry, so there is no point in keeping waiting longer
  3826. #else
  3827. ucLinkResponseRetries = ANTFS_RESPONSE_RETRIES;
  3828. #endif
  3829. do
  3830. {
  3831. DSIThread_MutexLock(&stMutexCriticalSection);
  3832. if ((bNewRxEvent == FALSE) && (*pbCancel == FALSE))
  3833. {
  3834. DSIThread_CondTimedWait(&stCondRxEvent, &stMutexCriticalSection, BROADCAST_TIMEOUT);
  3835. }
  3836. bNewRxEvent = FALSE;
  3837. DSIThread_MutexUnlock(&stMutexCriticalSection);
  3838. if (*pbCancel == TRUE)
  3839. {
  3840. #if defined(DEBUG_FILE)
  3841. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Stopped.");
  3842. #endif
  3843. return RETURN_STOP;
  3844. }
  3845. if (ucFoundDeviceState != REMOTE_DEVICE_STATE_BUSY)
  3846. {
  3847. if (!((usFoundANTFSManufacturerID == 2) && (usFoundANTFSDeviceType == 1))) //Make an exception for the FR405
  3848. bBeaconStateIncorrect = TRUE;
  3849. }
  3850. if (bRxError || bBeaconStateIncorrect)
  3851. {
  3852. #if defined(DEBUG_FILE)
  3853. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Rx error.");
  3854. #endif
  3855. if (ucLinkResponseRetries == 0)
  3856. {
  3857. #if defined(DEBUG_FILE)
  3858. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Retries exhausted");
  3859. #endif
  3860. return RETURN_FAIL;
  3861. }
  3862. else
  3863. {
  3864. #if defined(DEBUG_FILE)
  3865. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Retrying"); // Not actual retries, just wait longer!
  3866. #endif
  3867. ucLinkResponseRetries--;
  3868. bRxError = FALSE;
  3869. bBeaconStateIncorrect = FALSE;
  3870. }
  3871. }
  3872. if (bReceivedResponse)
  3873. {
  3874. if (aucTransferBufferFixed[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_AUTH_ID)
  3875. {
  3876. if ((pucResponseBuffer != NULL) && (pucResponseBufferSize != NULL))
  3877. {
  3878. if (*pucResponseBufferSize > aucTransferBufferFixed[AUTH_PASSWORD_LENGTH_OFFSET]) //set the reponse length up to the max
  3879. *pucResponseBufferSize = aucTransferBufferFixed[AUTH_PASSWORD_LENGTH_OFFSET];
  3880. memcpy(pucResponseBuffer, &aucTransferBufferFixed[8], *pucResponseBufferSize);
  3881. }
  3882. if (aucTransferBufferFixed[AUTH_RESPONSE_OFFSET] == AUTH_RESPONSE_ACCEPT)
  3883. {
  3884. bStatus = TRUE;
  3885. }
  3886. else if (aucTransferBufferFixed[AUTH_RESPONSE_OFFSET] == AUTH_RESPONSE_REJECT)
  3887. {
  3888. return RETURN_REJECT;
  3889. }
  3890. else
  3891. {
  3892. return RETURN_NA;
  3893. }
  3894. }
  3895. }
  3896. #if defined(DEBUG_FILE)
  3897. else
  3898. {
  3899. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Authenticating...");
  3900. }
  3901. #endif
  3902. } while ((bStatus == FALSE) && ((DSIThread_GetSystemTime() - ulStartTime) < ulAuthResponseTimeout));
  3903. if (!bStatus)
  3904. {
  3905. #if defined(DEBUG_FILE)
  3906. DSIDebug::ThreadWrite("ANTFSHostChannel::AttemptAuthenticate(): Timeout.");
  3907. #endif
  3908. return RETURN_FAIL;
  3909. }
  3910. return RETURN_PASS;
  3911. }
  3912. ///////////////////////////////////////////////////////////////////////
  3913. // Returns: TRUE if the message has been handled by ANT-FS host, FALSE otherwise
  3914. BOOL ANTFSHostChannel::ANTProtocolEventProcess(UCHAR ucChannel_, UCHAR ucMessageCode_)
  3915. {
  3916. #if 0
  3917. #if defined(DEBUG_FILE)
  3918. UCHAR aucString[256];
  3919. SNPRINTF((char *)aucString,256, "ANTProtocolEventProcess(): Message 0x%02X", ucMessageCode_);
  3920. DSIDebug::ThreadWrite((char *)aucString);
  3921. #endif
  3922. #endif
  3923. if ((ucMessageCode_ == MESG_RESPONSE_EVENT_ID) && (ucChannel_ == ucChannelNumber))
  3924. {
  3925. #if defined(DEBUG_FILE)
  3926. UCHAR aucString[256];
  3927. SNPRINTF((char *) aucString, 256, "ANTFSHostChannel::ANTProtocolEventProcess(): MESG_RESPONSE_EVENT_ID - 0x%02X", aucResponseBuf[1]);
  3928. DSIDebug::ThreadWrite((char *) aucString);
  3929. #endif
  3930. if (aucResponseBuf[1] == MESG_BURST_DATA_ID)
  3931. {
  3932. if (aucResponseBuf[2] != RESPONSE_NO_ERROR)
  3933. {
  3934. #if defined(DEBUG_FILE)
  3935. UCHAR aucString1[256];
  3936. SNPRINTF((char *) aucString1, 256, "ANTFSHostChannel::ANTProtocolEventProcess(): Burst transfer error: 0x%02X.", aucResponseBuf[2]);
  3937. DSIDebug::ThreadWrite((char *) aucString1);
  3938. #endif
  3939. bTxError = TRUE;
  3940. }
  3941. }
  3942. else if (aucResponseBuf[1] == MESG_ACKNOWLEDGED_DATA_ID)
  3943. {
  3944. if(aucResponseBuf[2] == TRANSFER_IN_PROGRESS)
  3945. {
  3946. #if defined(DEBUG_FILE)
  3947. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTProtocolEventProcess(): TRANSFER_IN_PROGRESS");
  3948. #endif
  3949. bForceFullInit = TRUE; // Force assign/unassign channel as per "Uplink communication failure on slave channel" issue documented on AP2 Module Rev History
  3950. }
  3951. }
  3952. }
  3953. return TRUE;
  3954. }
  3955. ///////////////////////////////////////////////////////////////////////
  3956. // Returns: TRUE if the message has been handled by ANT-FS host, FALSE otherwise
  3957. BOOL ANTFSHostChannel::ANTChannelEventProcess(UCHAR ucChannel_, UCHAR ucMessageCode_)
  3958. {
  3959. BOOL bIsValid = TRUE;
  3960. #if 0
  3961. #if defined(DEBUG_FILE)
  3962. UCHAR aucString[256];
  3963. SNPRINTF((char *)aucString,256, "ANTFSHostChannel::ANTChannelEventProcess(): Channel %u, Event 0x%02X", ucChannel_, ucMessageCode_);
  3964. DSIDebug::ThreadWrite((char *)aucString);
  3965. #endif
  3966. #endif
  3967. // Check that we're getting a message from the correct channel.
  3968. if((ucChannel_ != ucChannelNumber) || ((aucRxBuf[0] & CHANNEL_NUMBER_MASK) != ucChannelNumber))
  3969. {
  3970. #if defined(DEBUG_FILE)
  3971. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Message received on wrong channel.");
  3972. #endif
  3973. return FALSE;
  3974. }
  3975. switch (ucMessageCode_)
  3976. {
  3977. case EVENT_RX_BROADCAST:
  3978. case EVENT_RX_ACKNOWLEDGED:
  3979. #if 0
  3980. #if defined(DEBUG_FILE)
  3981. {
  3982. UCHAR aucString[256];
  3983. if (ucMessageCode_ == EVENT_RX_BROADCAST)
  3984. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_RX_BROADCAST");
  3985. else
  3986. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_RX_ACKNOWLEDGED");
  3987. SNPRINTF((char *)aucString,256, "ANTFSHostChannel::ANTChannelEventProcess(): Broadcast message received: {0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}.", aucRxBuf[0], aucRxBuf[1], aucRxBuf[2], aucRxBuf[3], aucRxBuf[4], aucRxBuf[5], aucRxBuf[6], aucRxBuf[7], aucRxBuf[8]);
  3988. DSIDebug::ThreadWrite((char *)aucString);
  3989. }
  3990. #endif
  3991. #endif
  3992. if (aucRxBuf[ANTFS_CONNECTION_OFFSET + 1] != ANTFS_BEACON_ID)
  3993. {
  3994. #if defined(DEBUG_FILE)
  3995. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Invalid connection ID.");
  3996. #endif
  3997. bIsValid = FALSE;
  3998. }
  3999. else if (!bFoundDevice)
  4000. {
  4001. //Decode the payload
  4002. usFoundBeaconPeriod = 0;
  4003. switch (aucRxBuf[STATUS1_OFFSET + 1] & BEACON_PERIOD_MASK)
  4004. {
  4005. case BEACON_PERIOD_0_5_HZ:
  4006. usFoundBeaconPeriod = 65535;
  4007. break;
  4008. case BEACON_PERIOD_1_HZ:
  4009. usFoundBeaconPeriod = 32768;
  4010. break;
  4011. case BEACON_PERIOD_2_HZ:
  4012. usFoundBeaconPeriod = 16384;
  4013. break;
  4014. case BEACON_PERIOD_4_HZ:
  4015. usFoundBeaconPeriod = 8192;
  4016. break;
  4017. case BEACON_PERIOD_8_HZ:
  4018. usFoundBeaconPeriod = 4096;
  4019. break;
  4020. case BEACON_PERIOD_KEEP: // Custom period
  4021. usFoundBeaconPeriod = usTheMessagePeriod;
  4022. break;
  4023. default:
  4024. #if defined(DEBUG_FILE)
  4025. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Invalid beacon period.");
  4026. #endif
  4027. bIsValid = FALSE;
  4028. break;
  4029. }
  4030. if (bIsValid)
  4031. {
  4032. if (aucRxBuf[STATUS1_OFFSET + 1] & DATA_AVAILABLE_FLAG_MASK)
  4033. bFoundDeviceHasData = TRUE;
  4034. else
  4035. bFoundDeviceHasData = FALSE;
  4036. if (aucRxBuf[STATUS1_OFFSET + 1] & UPLOAD_ENABLED_FLAG_MASK)
  4037. bFoundDeviceUploadEnabled = TRUE;
  4038. else
  4039. bFoundDeviceUploadEnabled = FALSE;
  4040. if (aucRxBuf[STATUS1_OFFSET + 1] & PAIRING_AVAILABLE_FLAG_MASK)
  4041. bFoundDeviceInPairingMode = TRUE;
  4042. else
  4043. bFoundDeviceInPairingMode = FALSE;
  4044. ucFoundDeviceAuthenticationType = aucRxBuf[AUTHENTICATION_TYPE_OFFSET + 1];
  4045. usFoundANTFSDeviceType = Convert_Bytes_To_USHORT(aucRxBuf[DEVICE_TYPE_OFFSET_HIGH + 1], aucRxBuf[DEVICE_TYPE_OFFSET_LOW + 1]);
  4046. usFoundANTFSManufacturerID = Convert_Bytes_To_USHORT(aucRxBuf[MANUFACTURER_ID_OFFSET_HIGH + 1], aucRxBuf[MANUFACTURER_ID_OFFSET_LOW + 1]);
  4047. //Set Found device paramenters here
  4048. stFoundDeviceParameters.usDeviceType = Convert_Bytes_To_USHORT(aucRxBuf[DEVICE_TYPE_OFFSET_HIGH + 1], aucRxBuf[DEVICE_TYPE_OFFSET_LOW + 1]);
  4049. stFoundDeviceParameters.usManufacturerID = Convert_Bytes_To_USHORT(aucRxBuf[MANUFACTURER_ID_OFFSET_HIGH + 1], aucRxBuf[MANUFACTURER_ID_OFFSET_LOW + 1]);
  4050. stFoundDeviceParameters.ucAuthenticationType = aucRxBuf[AUTHENTICATION_TYPE_OFFSET + 1];
  4051. stFoundDeviceParameters.ucStatusByte1 = aucRxBuf[STATUS1_OFFSET + 1];
  4052. stFoundDeviceParameters.ucStatusByte2 = aucRxBuf[STATUS2_OFFSET + 1];
  4053. }
  4054. }
  4055. if (bIsValid)
  4056. {
  4057. USHORT usDeviceType;
  4058. USHORT usManufID;
  4059. ULONG ulHostID;
  4060. UCHAR ucCurrentDeviceState = (aucRxBuf[STATUS2_OFFSET + 1] & REMOTE_DEVICE_STATE_MASK);
  4061. usDeviceType = Convert_Bytes_To_USHORT(aucRxBuf[DEVICE_TYPE_OFFSET_HIGH + 1], aucRxBuf[DEVICE_TYPE_OFFSET_LOW + 1]);
  4062. usManufID = Convert_Bytes_To_USHORT(aucRxBuf[MANUFACTURER_ID_OFFSET_HIGH + 1], aucRxBuf[MANUFACTURER_ID_OFFSET_LOW + 1]);
  4063. ulHostID = Convert_Bytes_To_ULONG ( aucRxBuf[HOST_ID_OFFSET + 4],
  4064. aucRxBuf[HOST_ID_OFFSET + 3],
  4065. aucRxBuf[HOST_ID_OFFSET + 2],
  4066. aucRxBuf[HOST_ID_OFFSET + 1]);
  4067. if (ucCurrentDeviceState > REMOTE_DEVICE_STATE_LAST_VALID)
  4068. {
  4069. bIsValid = FALSE;
  4070. #if defined(DEBUG_FILE)
  4071. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Current device state not in valid range.");
  4072. #endif
  4073. }
  4074. else if ((ulHostSerialNumber != ulHostID) && ((usDeviceType != usFoundANTFSDeviceType) || (usManufID != usFoundANTFSManufacturerID)))
  4075. {
  4076. bIsValid = FALSE;
  4077. #if defined(DEBUG_FILE)
  4078. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Host ID does not match this host and the device descriptors match do not match the found device.");
  4079. #endif
  4080. }
  4081. else if (((eANTFSState >= ANTFS_HOST_STATE_TRANSPORT) && (ucCurrentDeviceState < REMOTE_DEVICE_STATE_TRANS)) ||
  4082. ((eANTFSState >= ANTFS_HOST_STATE_CONNECTED) && (ucCurrentDeviceState == REMOTE_DEVICE_STATE_LINK)))
  4083. {
  4084. if (ucStrikes > 0)
  4085. {
  4086. ucStrikes--;
  4087. if (ucStrikes == 0)
  4088. {
  4089. #if defined(DEBUG_FILE)
  4090. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Device state changed; connection lost.");
  4091. #endif
  4092. pclANT->CloseChannel(ucChannelNumber,ANT_CLOSE_TIMEOUT);
  4093. bIsValid = FALSE;
  4094. DSIThread_MutexLock(&stMutexCriticalSection);
  4095. if (eANTFSRequest < ANTFS_REQUEST_CONNECTION_LOST)
  4096. {
  4097. eANTFSRequest = ANTFS_REQUEST_CONNECTION_LOST;
  4098. DSIThread_CondSignal(&stCondRequest);
  4099. }
  4100. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4101. }
  4102. #if defined(DEBUG_FILE)
  4103. else
  4104. {
  4105. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Device state changed; retrying.");
  4106. }
  4107. #endif
  4108. }
  4109. }
  4110. else
  4111. {
  4112. ucStrikes = STRIKE_COUNT;
  4113. ucFoundDeviceState = ucCurrentDeviceState;
  4114. if (ucFoundDeviceState == REMOTE_DEVICE_STATE_TRANS)
  4115. {
  4116. ucTransportBeaconTicks++;
  4117. stFoundDeviceParameters.ucStatusByte1 = aucRxBuf[STATUS1_OFFSET + 1];
  4118. }
  4119. if (ucFoundDeviceState == REMOTE_DEVICE_STATE_AUTH)
  4120. {
  4121. ulFoundBeaconHostID = Convert_Bytes_To_ULONG(aucRxBuf[HOST_ID_OFFSET + 4],
  4122. aucRxBuf[HOST_ID_OFFSET + 3],
  4123. aucRxBuf[HOST_ID_OFFSET + 2],
  4124. aucRxBuf[HOST_ID_OFFSET + 1]);
  4125. }
  4126. }
  4127. }
  4128. DSIThread_MutexLock(&stMutexCriticalSection);
  4129. bFoundDevice = TRUE; // Signal that we have found the device.
  4130. bReceivedBurst = FALSE;
  4131. bFoundDeviceIsValid = bIsValid;
  4132. bNewRxEvent = TRUE;
  4133. DSIThread_CondSignal(&stCondRxEvent);
  4134. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4135. break;
  4136. case EVENT_RX_BURST_PACKET:
  4137. #if 0
  4138. #if defined(DEBUG_FILE)
  4139. {
  4140. UCHAR aucString[256];
  4141. SNPRINTF((char *)aucString, 256, "ANTFSHostChannel::ANTChannelEventProcess(): EVENT_RX_BURST_PACKET received (%lu:%lu): {0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X}.", ulPacketCount, ulTransferArrayIndex / 8, aucRxBuf[0], aucRxBuf[1], aucRxBuf[2], aucRxBuf[3], aucRxBuf[4], aucRxBuf[5], aucRxBuf[6], aucRxBuf[7], aucRxBuf[8]);
  4142. DSIDebug::ThreadWrite((char *)aucString);
  4143. }
  4144. #endif
  4145. #endif
  4146. if (!bRxError)
  4147. {
  4148. if ((aucRxBuf[0] & SEQUENCE_NUMBER_MASK) == 0) // Start of a burst.
  4149. {
  4150. // Check that the message is coming from an ANTFS Device.
  4151. if (aucRxBuf[ANTFS_CONNECTION_OFFSET + 1] != ANTFS_BEACON_ID)
  4152. {
  4153. #if defined(DEBUG_FILE)
  4154. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Invalid ID.");
  4155. #endif
  4156. bRxError = TRUE;
  4157. }
  4158. else
  4159. {
  4160. #if defined(DEBUG_FILE)
  4161. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Burst started");
  4162. #endif
  4163. ucFoundDeviceState = (aucRxBuf[STATUS2_OFFSET + 1] & REMOTE_DEVICE_STATE_MASK);
  4164. }
  4165. ulPacketCount = 1;
  4166. bReceivedResponse = FALSE;
  4167. }
  4168. else
  4169. {
  4170. // Response packet.
  4171. if (ulPacketCount == 1)
  4172. {
  4173. // Check that the response contains the correct ID
  4174. if (aucRxBuf[ANTFS_CONNECTION_OFFSET + 1] != ANTFS_COMMAND_RESPONSE_ID)
  4175. {
  4176. #if defined(DEBUG_FILE)
  4177. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Invalid ID2.");
  4178. #endif
  4179. bRxError = TRUE;
  4180. }
  4181. else
  4182. {
  4183. switch (aucRxBuf[ANTFS_COMMAND_OFFSET + 1])
  4184. {
  4185. case ANTFS_RESPONSE_AUTH_ID:
  4186. case ANTFS_RESPONSE_UPLOAD_ID:
  4187. case ANTFS_RESPONSE_ERASE_ID:
  4188. case ANTFS_RESPONSE_UPLOAD_COMPLETE_ID:
  4189. memcpy(aucTransferBufferFixed, &aucRxBuf[1], 8); // Always copy the response to the start of the transfer buffer.
  4190. memset(&aucTransferBufferFixed[8],0x00,sizeof(aucTransferBufferFixed)-8); //Zero the rest of the receive buffer
  4191. ulTransferArrayIndex = 8;
  4192. pucTransferBuffer = aucTransferBufferFixed;
  4193. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  4194. {
  4195. bReceivedResponse = TRUE;
  4196. #if defined(DEBUG_FILE)
  4197. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Reception of burst complete. (0)");
  4198. #endif
  4199. }
  4200. #if defined(DEBUG_FILE)
  4201. else
  4202. {
  4203. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Receiving burst... (0)");
  4204. }
  4205. #endif
  4206. ulTransferBufferSize = sizeof(aucTransferBufferFixed);
  4207. break;
  4208. case ANTFS_RESPONSE_DOWNLOAD_SMALL_ID:
  4209. if (ulTransferArrayIndex == 0)
  4210. {
  4211. ulTransferTotalBytesRemaining = Convert_Bytes_To_ULONG(0,
  4212. 0,
  4213. aucRxBuf[DATA_BLOCK_SIZE_OFFSET_HIGH + 1],
  4214. aucRxBuf[DATA_BLOCK_SIZE_OFFSET_LOW + 1]);
  4215. ulTransferBufferSize = ulTransferTotalBytesRemaining + 16 + 8; //+16 for the extra packet at the start and rounding up to the next 8 byte packet, + 8 for additional buffering
  4216. if (pucTransferBufferDynamic)
  4217. delete[] pucTransferBufferDynamic;
  4218. try
  4219. {
  4220. pucTransferBufferDynamic = new UCHAR[ulTransferBufferSize];
  4221. if (pucTransferBufferDynamic == NULL)
  4222. throw "Memory allocation failure!";
  4223. }
  4224. catch(...)
  4225. {
  4226. bRxError = TRUE;
  4227. pucTransferBuffer = (UCHAR*) NULL;
  4228. #if defined(DEBUG_FILE)
  4229. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Unable to allocate memory for download. (1)");
  4230. #endif
  4231. break;
  4232. }
  4233. ulTransferArrayIndex = 8;
  4234. pucTransferBuffer = pucTransferBufferDynamic;
  4235. }
  4236. if (pucTransferBufferDynamic == NULL)
  4237. {
  4238. #if defined(DEBUG_FILE)
  4239. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): pucTransferBufferDynamic is NULL, ignoring transfer Packet");
  4240. #endif
  4241. break;
  4242. }
  4243. memcpy(pucTransferBufferDynamic, &aucRxBuf[1], 8); // Always copy the response to the start of the download buffer.
  4244. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  4245. {
  4246. bReceivedResponse = TRUE;
  4247. #if defined(DEBUG_FILE)
  4248. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Reception of burst complete. (1)");
  4249. #endif
  4250. }
  4251. #if defined(DEBUG_FILE)
  4252. else
  4253. {
  4254. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Receiving burst... (1)");
  4255. }
  4256. #endif
  4257. break;
  4258. case ANTFS_RESPONSE_DOWNLOAD_BIG_ID:
  4259. if (ulTransferArrayIndex == 0) //if this is the first packed of the this download session
  4260. {
  4261. ulTransferArrayIndex = 8;
  4262. pucTransferBuffer = aucTransferBufferFixed; //Use the fixed buffer for now
  4263. ulTransferBufferSize = sizeof(aucTransferBufferFixed);
  4264. }
  4265. ulTransferBytesInBlock = Convert_Bytes_To_ULONG(aucRxBuf[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 3 + 1],
  4266. aucRxBuf[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 2 + 1],
  4267. aucRxBuf[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 1 + 1],
  4268. aucRxBuf[DOWNLOAD_RESPONSE_BLOCK_SIZE_OFFSET + 1]);
  4269. memcpy(pucTransferBuffer, &aucRxBuf[1], 8); // Always copy the response to the start of the transfer buffer.
  4270. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  4271. {
  4272. bRxError = TRUE;
  4273. #if defined(DEBUG_FILE)
  4274. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Premature end of burst transfer.");
  4275. #endif
  4276. }
  4277. break;
  4278. case ANTFS_RESPONSE_SEND_DIRECT_ID:
  4279. {
  4280. ulTransferArrayIndex = Convert_Bytes_To_ULONG(0,
  4281. 0,
  4282. aucRxBuf[DATA_OFFSET_OFFSET_HIGH + 1],
  4283. aucRxBuf[DATA_OFFSET_OFFSET_LOW + 1]);
  4284. ulTransferArrayIndex *= 8;
  4285. ulTransferTotalBytesRemaining = Convert_Bytes_To_ULONG(0,
  4286. 0,
  4287. aucRxBuf[DATA_BLOCK_SIZE_OFFSET_HIGH + 1],
  4288. aucRxBuf[DATA_BLOCK_SIZE_OFFSET_LOW + 1]);
  4289. ulTransferTotalBytesRemaining *= 8;
  4290. }
  4291. if (ulTransferArrayIndex == 0)
  4292. {
  4293. ulTransferBufferSize = DIRECT_TRANSFER_SIZE + 16;
  4294. if (pucTransferBufferDynamic)
  4295. delete[] pucTransferBufferDynamic;
  4296. try
  4297. {
  4298. pucTransferBufferDynamic = new UCHAR[ulTransferBufferSize];
  4299. if (pucTransferBufferDynamic == NULL)
  4300. throw "Memory allocation failure!";
  4301. }
  4302. catch(...)
  4303. {
  4304. bRxError = TRUE;
  4305. pucTransferBuffer = (UCHAR*) NULL;
  4306. #if defined(DEBUG_FILE)
  4307. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Unable to allocate memory for download. (3)");
  4308. #endif
  4309. break;
  4310. }
  4311. pucTransferBuffer = pucTransferBufferDynamic;
  4312. }
  4313. else if (pucTransferBufferDynamic == NULL)
  4314. {
  4315. bRxError = TRUE;
  4316. #if defined(DEBUG_FILE)
  4317. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): ANTFS_RESPONSE_SEND_DIRECT index error.");
  4318. #endif
  4319. break;
  4320. }
  4321. ulTransferArrayIndex += 8; // This offset due to the response packet.
  4322. memcpy(pucTransferBufferDynamic, &aucRxBuf[1], 8); // Always copy the response to the start of the download buffer.
  4323. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  4324. {
  4325. bReceivedResponse = TRUE;
  4326. #if defined(DEBUG_FILE)
  4327. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Reception of burst complete. (2)");
  4328. #endif
  4329. }
  4330. #if defined(DEBUG_FILE)
  4331. else
  4332. {
  4333. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Receiving burst... (1)");
  4334. }
  4335. #endif
  4336. break;
  4337. default:
  4338. bRxError = TRUE; // BIG DL/UL and everything else are not handled right now so we fail out here.
  4339. break;
  4340. }
  4341. }
  4342. }
  4343. else if ((ulPacketCount == 2) && (pucTransferBuffer != NULL) && (pucTransferBuffer[ANTFS_COMMAND_OFFSET] == ANTFS_RESPONSE_DOWNLOAD_BIG_ID) &&(pucTransferBuffer[ANTFS_CONNECTION_OFFSET] == ANTFS_COMMAND_RESPONSE_ID))
  4344. {
  4345. if (ulTransferArrayIndex == 8)
  4346. {
  4347. ulTransferTotalBytesRemaining = Convert_Bytes_To_ULONG(aucRxBuf[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 3 + 1],
  4348. aucRxBuf[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 2 + 1],
  4349. aucRxBuf[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 1 + 1],
  4350. aucRxBuf[DOWNLOAD_RESPONSE_FILE_SIZE_OFFSET + 1]);
  4351. if (ulTransferByteSize && ulTransferByteSize < ulTransferTotalBytesRemaining) //If our desired download size is less than the file size, set it.
  4352. ulTransferTotalBytesRemaining = ulTransferByteSize;
  4353. ulTransferBufferSize = ulTransferTotalBytesRemaining + 24 + 8 + 8;//+24 for the extra packets at the start and rounding up to the next 8 byte packet, + 8 for additional buffering +8 more for CRC
  4354. #if defined(DEBUG_FILE)
  4355. {
  4356. char szString[256];
  4357. SNPRINTF(szString, 256, "ANTFSHostChannel::ANTChannelEventProcess(): ulTransferBufferSize = %lu.", ulTransferBufferSize);
  4358. DSIDebug::ThreadWrite(szString);
  4359. }
  4360. #endif
  4361. if (pucTransferBufferDynamic)
  4362. delete[] pucTransferBufferDynamic;
  4363. try
  4364. {
  4365. pucTransferBufferDynamic = new UCHAR[ulTransferBufferSize];
  4366. if (pucTransferBufferDynamic == NULL)
  4367. throw "Memory allocation failure!";
  4368. }
  4369. catch(...)
  4370. {
  4371. bRxError = TRUE;
  4372. ulTransferArrayIndex = 0;
  4373. pucTransferBuffer = (UCHAR*) NULL;
  4374. #if defined(DEBUG_FILE)
  4375. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Unable to allocate memory for download. (2)");
  4376. #endif
  4377. break;
  4378. }
  4379. memcpy(pucTransferBufferDynamic, aucTransferBufferFixed, 8); // Copy over the first packet that we saved temporarily.
  4380. ulTransferArrayIndex = 16;
  4381. pucTransferBuffer = pucTransferBufferDynamic;
  4382. }
  4383. if (pucTransferBufferDynamic == NULL)
  4384. {
  4385. #if defined(DEBUG_FILE)
  4386. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): pucTransferBufferDynamic is NULL, ignoring transfer Packet");
  4387. #endif
  4388. break;
  4389. }
  4390. memcpy(&pucTransferBuffer[8], &aucRxBuf[1], 8); // Always copy the response to the start of the transfer buffer.
  4391. ULONG ulReceivedDataOffset = Convert_Bytes_To_ULONG(aucRxBuf[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 3 + 1],
  4392. aucRxBuf[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 2 + 1],
  4393. aucRxBuf[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 1 + 1],
  4394. aucRxBuf[DOWNLOAD_RESPONSE_DATA_OFFSET_OFFSET + 1]);
  4395. if ((pucTransferBuffer[DOWNLOAD_RESPONSE_OFFSET] == DOWNLOAD_RESPONSE_OK) && ((ulReceivedDataOffset - ulTransferDataOffset) != (ulTransferArrayIndex - 16)))
  4396. {
  4397. bRxError = TRUE;
  4398. ulTransferArrayIndex = 0;
  4399. pucTransferBuffer = (UCHAR*) NULL;
  4400. #if defined(DEBUG_FILE)
  4401. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): DL offset does not match desired DL offset.");
  4402. #endif
  4403. break;
  4404. }
  4405. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  4406. {
  4407. bReceivedResponse = TRUE;
  4408. #if defined(DEBUG_FILE)
  4409. {
  4410. char szString[256];
  4411. SNPRINTF(szString, 256, "ANTFSHostChannel::ANTChannelEventProcess(): Reception of burst complete. (%lu).", ulTransferArrayIndex);
  4412. DSIDebug::ThreadWrite(szString);
  4413. }
  4414. #endif
  4415. }
  4416. }
  4417. else if ((ulTransferArrayIndex + 8) <= ulTransferBufferSize)
  4418. {
  4419. #if defined(DEBUG_FILE_V1)
  4420. {
  4421. char szString[256];
  4422. SNPRINTF(szString, 256, "ANTFSHostChannel::ANTChannelEventProcess(): ulTransferArrayIndex = %lu.", ulTransferArrayIndex);
  4423. DSIDebug::ThreadWrite(szString);
  4424. }
  4425. #endif
  4426. if (pucTransferBuffer == NULL)
  4427. {
  4428. #if defined(DEBUG_FILE)
  4429. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): pucTransferBuffer is NULL, ignoring transfer Packet");
  4430. #endif
  4431. break;
  4432. }
  4433. memcpy(&pucTransferBuffer[ulTransferArrayIndex], &aucRxBuf[1], 8);
  4434. ulTransferArrayIndex += 8;
  4435. if (aucRxBuf[0] & SEQUENCE_LAST_MESSAGE)
  4436. {
  4437. bReceivedResponse = TRUE;
  4438. #if defined(DEBUG_FILE)
  4439. {
  4440. char szString[256];
  4441. SNPRINTF(szString, 256, "ANTFSHostChannel::ANTChannelEventProcess(): Reception of burst complete. (%lu).", ulTransferArrayIndex);
  4442. DSIDebug::ThreadWrite(szString);
  4443. }
  4444. #endif
  4445. }
  4446. #if defined(DEBUG_FILE_V1)
  4447. else
  4448. {
  4449. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Receiving burst... (n)");
  4450. }
  4451. #endif
  4452. }
  4453. else
  4454. {
  4455. #if defined(DEBUG_FILE)
  4456. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): Burst Buffer Overflow");
  4457. #endif
  4458. bRxError = TRUE;
  4459. }
  4460. ulPacketCount++;
  4461. }
  4462. }
  4463. DSIThread_MutexLock(&stMutexCriticalSection);
  4464. bReceivedBurst = TRUE;
  4465. bNewRxEvent = TRUE;
  4466. DSIThread_CondSignal(&stCondRxEvent);
  4467. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4468. #if defined(DEBUG_FILE_V1)
  4469. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): bReceivedBurst = TRUE");
  4470. #endif
  4471. break;
  4472. case EVENT_TRANSFER_RX_FAILED:
  4473. DSIThread_MutexLock(&stMutexCriticalSection);
  4474. bRxError = TRUE;
  4475. bReceivedBurst = FALSE;
  4476. bReceivedResponse = FALSE;
  4477. DSIThread_CondSignal(&stCondRxEvent);
  4478. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4479. #if defined(DEBUG_FILE)
  4480. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_TRANSFER_RX_FAILED");
  4481. #endif
  4482. break;
  4483. case EVENT_RX_FAIL_GO_TO_SEARCH:
  4484. if (eANTFSState == ANTFS_HOST_STATE_SEARCHING)
  4485. {
  4486. DSIThread_MutexLock(&stMutexCriticalSection);
  4487. bFoundDeviceIsValid = FALSE; // Signal that this is not a valid beacon
  4488. bFoundDevice = TRUE; // Signal that we have found the device to allow the search loop to exit.
  4489. bReceivedBurst = FALSE;
  4490. bNewRxEvent = TRUE;
  4491. DSIThread_CondSignal(&stCondRxEvent);
  4492. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4493. #if defined(DEBUG_FILE)
  4494. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_RX_FAIL_GO_TO_SEARCH");
  4495. #endif
  4496. }
  4497. break;
  4498. case EVENT_RX_SEARCH_TIMEOUT:
  4499. #if defined(DEBUG_FILE)
  4500. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_RX_SEARCH_TIMEOUT");
  4501. #endif
  4502. if (eANTFSState == ANTFS_HOST_STATE_SEARCHING)
  4503. {
  4504. pclANT->OpenChannel(ucChannelNumber); // Re-open the channel.
  4505. }
  4506. else if ((eANTFSState == ANTFS_HOST_STATE_DOWNLOADING) || (eANTFSState == ANTFS_HOST_STATE_UPLOADING) || (eANTFSState == ANTFS_HOST_STATE_SENDING) || (eANTFSState == ANTFS_HOST_STATE_RECEIVING))
  4507. {
  4508. pclANT->OpenChannel(ucChannelNumber); // Re-open the channel.
  4509. DSIThread_MutexLock(&stMutexCriticalSection);
  4510. bRxError = TRUE;
  4511. bTxError = TRUE; // Can no longer transmit data, either.
  4512. bReceivedBurst = FALSE;
  4513. DSIThread_CondSignal(&stCondRxEvent);
  4514. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4515. }
  4516. else if (eANTFSState >= ANTFS_HOST_STATE_CONNECTED)
  4517. {
  4518. DSIThread_MutexLock(&stMutexCriticalSection);
  4519. if (eANTFSRequest < ANTFS_REQUEST_CONNECTION_LOST)
  4520. {
  4521. eANTFSRequest = ANTFS_REQUEST_CONNECTION_LOST;
  4522. DSIThread_CondSignal(&stCondRequest);
  4523. }
  4524. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4525. }
  4526. // any other states we don't report the timeout
  4527. break;
  4528. case EVENT_TRANSFER_TX_COMPLETED:
  4529. #if defined(DEBUG_FILE)
  4530. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_TRANSFER_TX_COMPLETED");
  4531. #endif
  4532. break;
  4533. case EVENT_TRANSFER_TX_FAILED:
  4534. bTxError = TRUE;
  4535. #if defined(DEBUG_FILE)
  4536. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_TRANSFER_TX_FAILED");
  4537. #endif
  4538. break;
  4539. case EVENT_TRANSFER_TX_START:
  4540. #if defined(DEBUG_FILE)
  4541. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_TRANSFER_TX_START");
  4542. #endif
  4543. break;
  4544. case EVENT_CHANNEL_CLOSED:
  4545. #if defined(DEBUG_FILE)
  4546. DSIDebug::ThreadWrite("ANTFSHostChannel::ANTChannelEventProcess(): EVENT_CHANNEL_CLOSED");
  4547. #endif
  4548. break;
  4549. default:
  4550. break;
  4551. }
  4552. return TRUE; // message has been handled, do not pass to application
  4553. }
  4554. ///////////////////////////////////////////////////////////////////////
  4555. BOOL ANTFSHostChannel::Blackout(ULONG ulDeviceID_, USHORT usManufacturerID_, USHORT usDeviceType_, USHORT usBlackoutTime_)
  4556. //BOOL ANTFSHost::IgnoreDevice(USHORT usBlackoutTime_)
  4557. {
  4558. BOOL bRetVal = TRUE;
  4559. IGNORE_LIST_ITEM stListItem;
  4560. IGNORE_LIST_ITEM *pstListItem;
  4561. if (usBlackoutTime_ == 0)
  4562. return FALSE;
  4563. DSIThread_MutexLock(&stMutexIgnoreListAccess);
  4564. stListItem.usID = (USHORT)ulDeviceID_;
  4565. stListItem.usManufacturerID = usManufacturerID_;
  4566. stListItem.usDeviceType = usDeviceType_;
  4567. pstListItem = (IGNORE_LIST_ITEM *) bsearch(&stListItem, astIgnoreList, usListIndex, sizeof(IGNORE_LIST_ITEM), &ListCompare);
  4568. if (pstListItem != NULL)
  4569. {
  4570. #if defined(DEBUG_FILE)
  4571. DSIDebug::ThreadPrintf("ANTFSHostChannel::Blackout(): Device [%u-%u-%u](t:%u) on list already, setting time to %u",
  4572. pstListItem->usID, pstListItem->usManufacturerID, pstListItem->usDeviceType, pstListItem->usTimeout, usBlackoutTime_);
  4573. #endif
  4574. // If the ID is already in the list, set the blackout time.
  4575. pstListItem->usTimeout = usBlackoutTime_;
  4576. }
  4577. else
  4578. {
  4579. // If the ID is not on the list, add it.
  4580. if (usListIndex < MAX_IGNORE_LIST_SIZE)
  4581. {
  4582. astIgnoreList[usListIndex].usID = (USHORT)ulDeviceID_;
  4583. astIgnoreList[usListIndex].usManufacturerID = usManufacturerID_;
  4584. astIgnoreList[usListIndex].usDeviceType = usDeviceType_;
  4585. astIgnoreList[usListIndex].usTimeout = usBlackoutTime_;
  4586. #if defined(DEBUG_FILE)
  4587. DSIDebug::ThreadPrintf("ANTFSHostChannel::Blackout(): Adding Device [%u-%u-%u](t:%u) at index %u",
  4588. astIgnoreList[usListIndex].usID, astIgnoreList[usListIndex].usManufacturerID, astIgnoreList[usListIndex].usDeviceType, astIgnoreList[usListIndex].usTimeout, usListIndex);
  4589. #endif
  4590. usListIndex++;
  4591. qsort(astIgnoreList, usListIndex, sizeof(IGNORE_LIST_ITEM), &ListCompare);
  4592. }
  4593. else
  4594. {
  4595. #if defined(DEBUG_FILE)
  4596. DSIDebug::ThreadPrintf("ANTFSHostChannel::Blackout(): Adding Device Error: List is full");
  4597. #endif
  4598. bRetVal = FALSE; // Can't add any more devices to the list.
  4599. }
  4600. }
  4601. DSIThread_MutexUnlock(&stMutexIgnoreListAccess);
  4602. return bRetVal;
  4603. }
  4604. ///////////////////////////////////////////////////////////////////////
  4605. BOOL ANTFSHostChannel::RemoveBlackout(ULONG ulDeviceID_, USHORT usManufacturerID_, USHORT usDeviceType_)
  4606. {
  4607. #if defined(DEBUG_FILE)
  4608. DSIDebug::ThreadPrintf("ANTFSHostChannel::RemoveBlackout(): Request to remove [%lu-%u-%u](t:%u)", ulDeviceID_, usManufacturerID_, usDeviceType_);
  4609. #endif
  4610. BOOL bRetVal = TRUE;
  4611. IGNORE_LIST_ITEM stListItem;
  4612. IGNORE_LIST_ITEM *pstListItem;
  4613. DSIThread_MutexLock(&stMutexIgnoreListAccess);
  4614. stListItem.usID = (USHORT)ulDeviceID_;
  4615. stListItem.usManufacturerID = usManufacturerID_;
  4616. stListItem.usDeviceType = usDeviceType_;
  4617. pstListItem = (IGNORE_LIST_ITEM *) bsearch(&stListItem, astIgnoreList, usListIndex, sizeof(IGNORE_LIST_ITEM), &ListCompare);
  4618. if (pstListItem != NULL)
  4619. {
  4620. USHORT usIndex = (USHORT)((pstListItem - astIgnoreList) / sizeof(IGNORE_LIST_ITEM));
  4621. #if defined(DEBUG_FILE)
  4622. DSIDebug::ThreadPrintf("ANTFSHostChannel::RemoveBlackout(): Removing Device [%u-%u-%u](t:%u) at index %u", pstListItem->usID, pstListItem->usManufacturerID, pstListItem->usDeviceType, pstListItem->usTimeout, usIndex);
  4623. #endif
  4624. // If the ID is found on the list, set the blackout time to 0 and remove it.
  4625. astIgnoreList[usIndex].usTimeout = 0;
  4626. for (usIndex = usIndex + 1; usIndex < usListIndex; usIndex++)
  4627. astIgnoreList[usIndex - 1] = astIgnoreList[usIndex];
  4628. usListIndex--;
  4629. }
  4630. else
  4631. {
  4632. #if defined(DEBUG_FILE)
  4633. DSIDebug::ThreadWrite("ANTFSHostChannel::RemoveBlackout(): Remove device error: not found.");
  4634. #endif
  4635. // If the ID is not on the list.
  4636. bRetVal = FALSE;
  4637. }
  4638. DSIThread_MutexUnlock(&stMutexIgnoreListAccess);
  4639. return bRetVal;
  4640. }
  4641. ///////////////////////////////////////////////////////////////////////
  4642. void ANTFSHostChannel::ClearBlackoutList(void)
  4643. {
  4644. DSIThread_MutexLock(&stMutexIgnoreListAccess);
  4645. usListIndex = 0;
  4646. #if defined(DEBUG_FILE)
  4647. DSIDebug::ThreadWrite("ANTFSHostChannel::ClearBlackoutList(): Blackout list cleared.");
  4648. #endif
  4649. DSIThread_MutexUnlock(&stMutexIgnoreListAccess);
  4650. }
  4651. ///////////////////////////////////////////////////////////////////////
  4652. static int ListCompare(const void *puvKeyVal, const void *puvDatum)
  4653. {
  4654. IGNORE_LIST_ITEM* pstKeyVal = (IGNORE_LIST_ITEM *) puvKeyVal;
  4655. IGNORE_LIST_ITEM* pstDatum = (IGNORE_LIST_ITEM *) puvDatum;
  4656. LONG diff;
  4657. if( (diff = (LONG)pstKeyVal->usID - pstDatum->usID) != 0
  4658. || (diff = (LONG)pstKeyVal->usDeviceType - pstDatum->usDeviceType) != 0
  4659. || (diff = (LONG)pstKeyVal->usManufacturerID - pstDatum->usManufacturerID) != 0)
  4660. {
  4661. return (diff < 0)? -1 : 1;
  4662. }
  4663. return 0; //Otherwise they are equal
  4664. }
  4665. ///////////////////////////////////////////////////////////////////////
  4666. // Frequency: 1 Hz
  4667. ///////////////////////////////////////////////////////////////////////
  4668. DSI_THREAD_RETURN ANTFSHostChannel::QueueTimerStart(void *pvParameter_)
  4669. {
  4670. ((ANTFSHostChannel *)pvParameter_)->QueueTimerCallback();
  4671. return 0;
  4672. }
  4673. ///////////////////////////////////////////////////////////////////////
  4674. void ANTFSHostChannel::QueueTimerCallback(void)
  4675. {
  4676. USHORT usCounter = 0;
  4677. if(!bTimerThreadInitDone)
  4678. {
  4679. #if defined(DEBUG_FILE)
  4680. DSIDebug::ThreadInit("Timer");
  4681. #endif
  4682. bTimerThreadInitDone = TRUE;
  4683. }
  4684. #if defined(DEBUG_FILE)
  4685. DSIDebug::ThreadWrite("ANTFSHostChannel::TimerQueueTimerCall(): Entering ignore list critical section.");
  4686. #endif
  4687. DSIThread_MutexLock(&stMutexIgnoreListAccess);
  4688. #if defined(DEBUG_FILE)
  4689. DSIDebug::ThreadWrite("Updating ignore list...");
  4690. #endif
  4691. while (usCounter < usListIndex)
  4692. {
  4693. if ((astIgnoreList[usCounter].usTimeout > 0) && (astIgnoreList[usCounter].usTimeout != MAX_USHORT))
  4694. astIgnoreList[usCounter].usTimeout--;
  4695. if (astIgnoreList[usCounter].usTimeout == 0) // Remove the List Item if it has expired.
  4696. {
  4697. USHORT usInsideLoopCounter;
  4698. #if defined(DEBUG_FILE)
  4699. {
  4700. UCHAR aucString[256];
  4701. SNPRINTF((char *)aucString,256, "Removing %u %u %u from ignore list.", astIgnoreList[usCounter].usID, astIgnoreList[usCounter].usDeviceType, astIgnoreList[usCounter].usManufacturerID);
  4702. DSIDebug::ThreadWrite((char *)aucString);
  4703. }
  4704. #endif
  4705. for (usInsideLoopCounter = usCounter + 1; usInsideLoopCounter < usListIndex; usInsideLoopCounter++)
  4706. astIgnoreList[usInsideLoopCounter - 1] = astIgnoreList[usInsideLoopCounter];
  4707. usListIndex--;
  4708. }
  4709. else
  4710. {
  4711. #if defined(DEBUG_FILE)
  4712. {
  4713. UCHAR aucString[256];
  4714. SNPRINTF((char *)aucString,256, "%u %u %u is on ignore list. %us left.", astIgnoreList[usCounter].usID, astIgnoreList[usCounter].usDeviceType, astIgnoreList[usCounter].usManufacturerID, astIgnoreList[usCounter].usTimeout);
  4715. DSIDebug::ThreadWrite((char *)aucString);
  4716. }
  4717. #endif
  4718. usCounter++;
  4719. }
  4720. }
  4721. DSIThread_MutexUnlock(&stMutexIgnoreListAccess);
  4722. #if defined(DEBUG_FILE)
  4723. DSIDebug::ThreadWrite("ANTFSHostChannel::TimerQueueTimerCall(): Left ignore list critical section.");
  4724. #endif
  4725. /*
  4726. if (++usSerialWatchdog > ANTFS_SERIAL_WATCHDOG_COUNT)
  4727. {
  4728. usSerialWatchdog = 0;
  4729. HandleSerialError(); //! This is where we would want to potentially add a serial watch dog, if it makes sense to do so in the future
  4730. }
  4731. */
  4732. }
  4733. ///////////////////////////////////////////////////////////////////////
  4734. void ANTFSHostChannel::HandleSerialError(void)
  4735. {
  4736. // We ended up here because we did not receive the expected response to a serial message
  4737. // Most likely, ANT was in the wrong state, so attempt to close the channel.
  4738. // No errors raised from here, as we do not know what state we are in.
  4739. UCHAR ucChannelStatus = 0;
  4740. if(pclANT->CloseChannel(ucChannelNumber, ANT_CLOSE_TIMEOUT) != TRUE)
  4741. {
  4742. #if defined(DEBUG_FILE)
  4743. DSIDebug::ThreadWrite("ANTFSHostChannel::HandleSerialError(): Failed to close channel.");
  4744. #endif
  4745. }
  4746. if(pclANT->UnAssignChannel(ucChannelNumber, MESSAGE_TIMEOUT) != TRUE)
  4747. {
  4748. #if defined(DEBUG_FILE)
  4749. DSIDebug::ThreadWrite("ANTFSHostChannel::HandleSerialError(): Failed to unassign channel.");
  4750. #endif
  4751. }
  4752. // The following is just for information purposes...
  4753. if(pclANT->GetChannelStatus(ucChannelNumber, &ucChannelStatus, MESSAGE_TIMEOUT) == FALSE)
  4754. {
  4755. #if defined(DEBUG_FILE)
  4756. DSIDebug::ThreadWrite("ANTFSHostChannel::HandleSerialError(): Failed ANT_GetChannelStatus().");
  4757. #endif
  4758. }
  4759. #if defined(DEBUG_FILE)
  4760. else if ((ucChannelStatus & STATUS_CHANNEL_STATE_MASK) != STATUS_UNASSIGNED_CHANNEL)
  4761. {
  4762. char szString[256];
  4763. SNPRINTF(szString, 256, "ANTFSHostChannel::HandleSerialError(): Channel state... 0x%x.", ucChannelStatus);
  4764. DSIDebug::ThreadWrite(szString);
  4765. }
  4766. #endif
  4767. DSIThread_MutexLock(&stMutexCriticalSection);
  4768. eANTFSRequest = ANTFS_REQUEST_SERIAL_ERROR_HANDLED; // Reset state machine next
  4769. DSIThread_CondSignal(&stCondRequest);
  4770. DSIThread_MutexUnlock(&stMutexCriticalSection);
  4771. }
  4772. ///////////////////////////////////////////////////////////////////////
  4773. // This function is called to increment the frequency stale counter
  4774. ///////////////////////////////////////////////////////////////////////
  4775. void ANTFSHostChannel::IncFreqStaleCount(UCHAR ucInc)
  4776. {
  4777. USHORT usTemp = (USHORT)ucTransportFrequencyStaleCount + (USHORT)ucInc;
  4778. if (usTemp >= MAX_STALE_COUNT)
  4779. ucTransportFrequencyStaleCount = MAX_STALE_COUNT;
  4780. else
  4781. ucTransportFrequencyStaleCount = (UCHAR)usTemp;
  4782. }
  4783. ///////////////////////////////////////////////////////////////////////
  4784. // This function is called to populate/re-populate the frequency table randomly
  4785. ///////////////////////////////////////////////////////////////////////
  4786. void ANTFSHostChannel::PopulateTransportFreqTable(void)
  4787. {
  4788. UCHAR aucTemp[TRANSPORT_FREQUENCY_LIST_SIZE];
  4789. int i = 0;
  4790. ucTransportFrequencyStaleCount = 0;
  4791. ucCurrentTransportFreqElement = 0;
  4792. srand((unsigned int)time((time_t*)NULL));
  4793. memcpy (aucTemp, aucTransportFrequencyList, TRANSPORT_FREQUENCY_LIST_SIZE);
  4794. for (i=0; i < TRANSPORT_FREQUENCY_LIST_SIZE; i++)
  4795. {
  4796. UCHAR ucNewValue = (UCHAR)( 1 + (rand() % (TRANSPORT_FREQUENCY_LIST_SIZE-1)) );
  4797. UCHAR j = 0;
  4798. while (ucNewValue)
  4799. {
  4800. j = (j+1)%TRANSPORT_FREQUENCY_LIST_SIZE;
  4801. if (aucTemp[j] != 0xFF)
  4802. {
  4803. ucNewValue--;
  4804. }
  4805. }
  4806. aucFrequencyTable[i] = aucTemp[j];
  4807. aucTemp[j] = 0xFF;
  4808. }
  4809. }
  4810. ///////////////////////////////////////////////////////////////////////
  4811. // This function is called to check if we need to change frequencies and return the new frequncy
  4812. ///////////////////////////////////////////////////////////////////////
  4813. UCHAR ANTFSHostChannel::CheckForNewTransportFreq(void)
  4814. {
  4815. if (ucTransportFrequencyStaleCount >= MAX_STALE_COUNT)
  4816. {
  4817. ucCurrentTransportFreqElement = ((ucCurrentTransportFreqElement+1) % TRANSPORT_FREQUENCY_LIST_SIZE);
  4818. ucTransportFrequencyStaleCount = 0;
  4819. }
  4820. return aucFrequencyTable[ucCurrentTransportFreqElement];
  4821. }
  4822. ///////////////////////////////////////////////////////////////////////
  4823. static int DeviceParametersItemCompare(const void *pvItem1, const void *pvItem2)
  4824. {
  4825. if (((DEVICE_PARAMETERS_ITEM *) pvItem1)->usHandle < ((DEVICE_PARAMETERS_ITEM *) pvItem2)->usHandle)
  4826. return -1;
  4827. if (((DEVICE_PARAMETERS_ITEM *) pvItem1)->usHandle == ((DEVICE_PARAMETERS_ITEM *) pvItem2)->usHandle)
  4828. return 0;
  4829. return 1;
  4830. }
  4831. ///////////////////////////////////////////////////////////////////////
  4832. BOOL ANTFSHostChannel::IsDeviceMatched(ANTFS_DEVICE_PARAMETERS *psDeviceParameters_, BOOL bPartialID_)
  4833. {
  4834. USHORT usDeviceIndex = 0;
  4835. ULONG ulIDMask;
  4836. if (usDeviceListSize == 0)
  4837. return TRUE; // There are no devices in the list, so any device is a match.
  4838. if(bPartialID_)
  4839. ulIDMask = MAX_USHORT;
  4840. else
  4841. ulIDMask = MAX_ULONG;
  4842. while (usDeviceIndex < usDeviceListSize)
  4843. {
  4844. DEVICE_PARAMETERS_ITEM *psSearchDeviceItem = &asDeviceParametersList[usDeviceIndex];
  4845. if (
  4846. ((psDeviceParameters_->ulDeviceID & psSearchDeviceItem->sDeviceSearchMask.ulDeviceID & ulIDMask) == (psSearchDeviceItem->sDeviceParameters.ulDeviceID & psSearchDeviceItem->sDeviceSearchMask.ulDeviceID & ulIDMask))
  4847. &&
  4848. ((psDeviceParameters_->usManufacturerID & psSearchDeviceItem->sDeviceSearchMask.usManufacturerID) == (psSearchDeviceItem->sDeviceParameters.usManufacturerID & psSearchDeviceItem->sDeviceSearchMask.usManufacturerID))
  4849. &&
  4850. ((psDeviceParameters_->usDeviceType & psSearchDeviceItem->sDeviceSearchMask.usDeviceType) == (psSearchDeviceItem->sDeviceParameters.usDeviceType & psSearchDeviceItem->sDeviceSearchMask.usDeviceType))
  4851. &&
  4852. ((psDeviceParameters_->ucAuthenticationType & psSearchDeviceItem->sDeviceSearchMask.ucAuthenticationType) == (psSearchDeviceItem->sDeviceParameters.ucAuthenticationType & psSearchDeviceItem->sDeviceSearchMask.ucAuthenticationType))
  4853. &&
  4854. ((psDeviceParameters_->ucStatusByte1 & psSearchDeviceItem->sDeviceSearchMask.ucStatusByte1) == (psSearchDeviceItem->sDeviceParameters.ucStatusByte1 & psSearchDeviceItem->sDeviceSearchMask.ucStatusByte1))
  4855. &&
  4856. ((psDeviceParameters_->ucStatusByte2 & psSearchDeviceItem->sDeviceSearchMask.ucStatusByte2) == (psSearchDeviceItem->sDeviceParameters.ucStatusByte2 & psSearchDeviceItem->sDeviceSearchMask.ucStatusByte2))
  4857. )
  4858. return TRUE;
  4859. usDeviceIndex++;
  4860. }
  4861. return FALSE;
  4862. }
  4863. ///////////////////////////////////////////////////////////////////////
  4864. void ANTFSHostChannel::AddResponse(ANTFS_HOST_RESPONSE eResponse_)
  4865. {
  4866. DSIThread_MutexLock(&stMutexResponseQueue);
  4867. clResponseQueue.AddResponse(eResponse_);
  4868. DSIThread_CondSignal(&stCondWaitForResponse);
  4869. DSIThread_MutexUnlock(&stMutexResponseQueue);
  4870. }
  4871. ///////////////////////////////////////////////////////////////////////
  4872. //Returns a response if there is one ready, otherwise waits the specified time for one to occur
  4873. ANTFS_HOST_RESPONSE ANTFSHostChannel::WaitForResponse(ULONG ulMilliseconds_)
  4874. {
  4875. ANTFS_HOST_RESPONSE stResponse = ANTFS_HOST_RESPONSE_NONE;
  4876. if (bKillThread == TRUE)
  4877. return ANTFS_HOST_RESPONSE_NONE;
  4878. //Wait for response
  4879. DSIThread_MutexLock(&stMutexResponseQueue);
  4880. if(clResponseQueue.isEmpty())
  4881. {
  4882. UCHAR ucResult = DSIThread_CondTimedWait(&stCondWaitForResponse, &stMutexResponseQueue, ulMilliseconds_);
  4883. switch(ucResult)
  4884. {
  4885. case DSI_THREAD_ENONE:
  4886. stResponse = clResponseQueue.GetResponse();
  4887. break;
  4888. case DSI_THREAD_ETIMEDOUT:
  4889. stResponse = ANTFS_HOST_RESPONSE_NONE;
  4890. break;
  4891. case DSI_THREAD_EOTHER:
  4892. #if defined(DEBUG_FILE)
  4893. DSIDebug::ThreadWrite("ANTFSHostChannel::WaitForResponse(): CondTimedWait() Failed!");
  4894. #endif
  4895. stResponse = ANTFS_HOST_RESPONSE_NONE;
  4896. break;
  4897. default:
  4898. #if defined(DEBUG_FILE)
  4899. DSIDebug::ThreadWrite("ANTFSHostChannel::WaitForResponse(): Error Unknown...");
  4900. #endif
  4901. stResponse = ANTFS_HOST_RESPONSE_NONE;
  4902. break;
  4903. }
  4904. }
  4905. else
  4906. {
  4907. stResponse = clResponseQueue.GetResponse();
  4908. }
  4909. DSIThread_MutexUnlock(&stMutexResponseQueue);
  4910. return stResponse;
  4911. }