UpgradeTaskFSM.cpp 297 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566656765686569657065716572657365746575657665776578657965806581658265836584658565866587658865896590659165926593659465956596659765986599660066016602660366046605660666076608660966106611661266136614661566166617661866196620662166226623662466256626662766286629663066316632663366346635663666376638663966406641664266436644664566466647664866496650665166526653665466556656665766586659666066616662666366646665666666676668666966706671667266736674667566766677667866796680668166826683668466856686668766886689669066916692669366946695669666976698669967006701670267036704670567066707670867096710671167126713671467156716671767186719672067216722672367246725672667276728672967306731673267336734673567366737673867396740674167426743674467456746674767486749675067516752675367546755675667576758675967606761676267636764676567666767676867696770677167726773677467756776677767786779678067816782678367846785678667876788678967906791679267936794679567966797679867996800680168026803680468056806680768086809681068116812681368146815681668176818681968206821682268236824682568266827682868296830683168326833683468356836683768386839684068416842684368446845684668476848684968506851685268536854685568566857685868596860686168626863686468656866686768686869687068716872687368746875687668776878687968806881688268836884688568866887688868896890689168926893689468956896689768986899690069016902690369046905690669076908690969106911691269136914691569166917691869196920692169226923692469256926692769286929693069316932693369346935693669376938693969406941694269436944694569466947694869496950695169526953695469556956695769586959696069616962696369646965696669676968696969706971697269736974697569766977697869796980698169826983698469856986698769886989699069916992699369946995699669976998699970007001700270037004700570067007700870097010701170127013701470157016701770187019702070217022702370247025702670277028702970307031703270337034703570367037703870397040704170427043704470457046704770487049705070517052705370547055705670577058705970607061706270637064706570667067706870697070707170727073707470757076707770787079708070817082708370847085708670877088708970907091709270937094709570967097709870997100710171027103710471057106710771087109711071117112711371147115711671177118711971207121712271237124712571267127712871297130713171327133713471357136713771387139714071417142714371447145714671477148714971507151715271537154715571567157715871597160716171627163716471657166716771687169717071717172717371747175717671777178717971807181718271837184718571867187718871897190719171927193719471957196719771987199720072017202720372047205720672077208720972107211721272137214721572167217721872197220722172227223722472257226722772287229723072317232723372347235723672377238723972407241724272437244724572467247724872497250725172527253725472557256725772587259726072617262726372647265726672677268726972707271727272737274727572767277727872797280728172827283728472857286728772887289729072917292729372947295729672977298729973007301730273037304730573067307730873097310731173127313731473157316731773187319732073217322732373247325732673277328732973307331733273337334733573367337733873397340734173427343734473457346734773487349735073517352735373547355735673577358735973607361736273637364736573667367736873697370737173727373737473757376737773787379738073817382738373847385738673877388738973907391739273937394739573967397739873997400740174027403740474057406740774087409741074117412741374147415741674177418741974207421742274237424742574267427742874297430743174327433743474357436743774387439744074417442744374447445744674477448744974507451745274537454745574567457745874597460746174627463746474657466746774687469747074717472747374747475747674777478747974807481748274837484748574867487748874897490749174927493749474957496749774987499750075017502750375047505750675077508750975107511
  1. #include "UpgradeTaskFSM.h"
  2. #include "fileutil.h"
  3. #include "iniutil.h"
  4. #include "memutil.h"
  5. #include "osutil.h"
  6. #include "RVCComm.h"
  7. #include <regex>
  8. #include "mod_UpgradeMgr.h"
  9. #include "UpgradeManager_msg_g.h"
  10. #include "CommEntityUtil.hpp"
  11. #ifdef RVC_OS_WIN
  12. #include <WinBase.h>
  13. #include <io.h>
  14. #include "XUnzip.h"
  15. #include <ShellAPI.h>
  16. #else
  17. #include <sys/utsname.h>
  18. #include "XUnZipZilb.h"
  19. #endif
  20. #include "EventCode.h"
  21. #include "json/json.h"
  22. #include "RVCEventCode.h"
  23. #ifdef DEVOPS_ON_ST /*DevOps流水线编译,ST环境*/
  24. #define QUERY_CONTINUE_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/continue-task"
  25. #define QUERY_CURR_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/current/task"
  26. #define QUERY_NEW_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/new/task"
  27. #define QUERY_PRE_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/pre-task"
  28. #define SEND_SM3_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/upload/hash"
  29. #define UPDATE_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/update/upgrade/status"
  30. #define QUERY_TASK_STATUS_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/task/status"
  31. #elif defined(DEVOPS_ON_UAT)/*DevOps流水线编译,UAT环境*/
  32. #define QUERY_CONTINUE_TASK_URL "https://upgrade.paasuat.cmbchina.cn/api/continueTask"
  33. #define QUERY_CURR_TASK_URL "https://upgrade.paasuat.cmbchina.cn/api/current/task"
  34. #define QUERY_NEW_TASK_URL "https://upgrade.paasuat.cmbchina.cn/api/new/task"
  35. #define QUERY_PRE_TASK_URL "https://upgrade.paasuat.cmbchina.cn/api/preTask"
  36. #define SEND_SM3_TASK_URL "https://upgrade.paasuat.cmbchina.cn/api/upload/hash"
  37. #define UPDATE_TASK_URL "https://upgrade.paasuat.cmbchina.cn/api/update/upgrade/status"
  38. #define QUERY_TASK_STATUS_URL "https://upgrade.paasuat.cmbchina.cn/api/task/status"
  39. #elif defined(DEVOPS_ON_PRD)/*DevOps流水线编译,PRD环境*/
  40. #define QUERY_CONTINUE_TASK_URL "https://upgrade.paas.cmbchina.cn/api/continueTask"
  41. #define QUERY_CURR_TASK_URL "https://upgrade.paas.cmbchina.cn/api/current/task"
  42. #define QUERY_NEW_TASK_URL "https://upgrade.paas.cmbchina.cn/api/new/task"
  43. #define QUERY_PRE_TASK_URL "https://upgrade.paas.cmbchina.cn/api/preTask"
  44. #define SEND_SM3_TASK_URL "https://upgrade.paas.cmbchina.cn/api/upload/hash"
  45. #define UPDATE_TASK_URL "https://upgrade.paas.cmbchina.cn/api/update/upgrade/status"
  46. #define QUERY_TASK_STATUS_URL "https://upgrade.paas.cmbchina.cn/api/task/status"
  47. #else/*DevOps流水线编译,Dev环境&本地编译等非DevOps环境*/
  48. #define QUERY_CONTINUE_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/continue-task"
  49. #define QUERY_CURR_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/current/task"
  50. #define QUERY_NEW_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/new/task"
  51. #define QUERY_PRE_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/pre-task"
  52. #define SEND_SM3_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/upload/hash"
  53. #define UPDATE_TASK_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/update/upgrade/status"
  54. #define QUERY_TASK_STATUS_URL "https://accessproxy-service.paasst.cmbchina.cn/upgrade/api/task/status"
  55. #endif
  56. namespace Task
  57. {
  58. //计算和发送文件hash
  59. struct SendSM3Task : public ITaskSp{
  60. CUpgradeTaskFSM* m_fsm;
  61. explicit SendSM3Task(CUpgradeTaskFSM* f) : m_fsm(f) {}
  62. void Process(){
  63. LOG_FUNCTION();
  64. int tryCount=3;//总共3次机会
  65. bool SendHashOK= false;
  66. while(tryCount>0){
  67. //计算hash
  68. if(m_fsm->m_fileSM3ListHash.IsNullOrEmpty()){
  69. if(getSM3Hash()){
  70. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get SM3 list succ, len=%d, fileNum = %d, SM3 hash=%s", m_fsm->m_fileSM3List.GetLength(), m_fsm->m_fileSM3Map.size(), m_fsm->m_fileSM3ListHash.GetData());
  71. }else{
  72. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_GET_SM3_HASH_FAIL, "get SM3 list fail");
  73. tryCount--;
  74. Sleep(60*1000);//等待60s再计算
  75. continue;
  76. }
  77. }
  78. //发送hash
  79. if(sendSM3Hash()){
  80. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("send SM3 list succ");
  81. m_fsm->isSendHash=true;
  82. SendHashOK = true;
  83. break;
  84. }else{
  85. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_SEND_SM3_HASH_FAIL, "send SM3 list fail");
  86. m_fsm->isSendHash=false;
  87. Sleep(60*1000);//等待60s再上传
  88. tryCount--;
  89. continue;
  90. }
  91. }
  92. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::SendSM3TaskEvent(SendHashOK));//返回处理结果
  93. }
  94. bool getSM3Hash(){
  95. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin get SM3 list");
  96. CSmallDateTime beginT = CSmallDateTime::GetNow();
  97. CSimpleStringA tempSM3ListStr = "";
  98. ErrorCodeEnum rc = m_fsm->GetSM3List(tempSM3ListStr);
  99. CSmallDateTime endT = CSmallDateTime::GetNow();
  100. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get SM3 list 耗时%d秒",(DWORD)(endT-beginT));
  101. if (rc != Error_Succeed)
  102. {
  103. LogWarn(Severity_Low, rc, ERR_TASK_GET_SM3_HASH_FAIL, "get SM3 list fail");
  104. return false;
  105. }
  106. else
  107. {
  108. //tempSM3ListStr 这个值有可能因为配置的文件夹下面没有文件,故为空
  109. if(tempSM3ListStr.IsNullOrEmpty()){
  110. m_fsm->m_fileSM3ListHash = "TerminalTempHash";
  111. return true;
  112. }else{
  113. CSimpleStringA tempSM3HashStr = "";
  114. if(Error_Succeed==m_fsm->SM3DataToStr(tempSM3ListStr,tempSM3HashStr,false)){
  115. m_fsm->m_fileSM3ListHash = tempSM3HashStr;
  116. return true;
  117. }else{
  118. LogWarn(Severity_Low, rc, ERR_TASK_GET_SM3_HASH_FAIL, "get SM3 list hash fail");
  119. return false;
  120. }
  121. }
  122. }
  123. }
  124. bool sendSM3Hash(){
  125. //LOG_FUNCTION();
  126. IHttpFunc* client;
  127. client = create_http(m_fsm->HttpsLogCallBack);
  128. //发送hash
  129. bool bRet = SendSM3TaskImpl(client);
  130. client->Destory();
  131. return bRet;
  132. }
  133. bool SendSM3TaskImpl(IHttpFunc* client){
  134. HttpStruct::SendSM3TaskReq qTempReq;
  135. HttpStruct::SendSM3TaskRet qTempRet;
  136. CSystemStaticInfo info;
  137. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  138. if (rc != Error_Succeed)
  139. {
  140. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("SendSM3Task get CSystemStaticInfo fail");
  141. return false;
  142. }
  143. Json::Value rootReq;
  144. Json::FastWriter writer;
  145. rootReq["terminal_no"] = info.strTerminalID.GetData();
  146. rootReq["software_version"] = m_fsm->m_currentVersion.ToString().GetData();
  147. rootReq["file_hash_list"] = m_fsm->m_fileSM3List.GetData();//hash集合
  148. rootReq["list_hash"] = m_fsm->m_fileSM3ListHash.GetData();//64位
  149. string jsonReq = writer.write(rootReq);
  150. qTempReq.m_reqStr = jsonReq;//请求参数
  151. qTempReq.m_url=m_fsm->m_centerUrl.sendSM3TaskUrl;//访问地址
  152. qTempReq.m_timeOut = 60;//设置传送超时时间为60s
  153. //打印发送请求
  154. if(m_fsm->m_PrintHttpLog){
  155. qTempReq.m_printDbg = true;
  156. }
  157. PROCESS_LINK_CONTEXT("LR0402506SendSM3")
  158. if(!client->Post(qTempReq, qTempRet, &nextLink)){
  159. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_SEND_SM3_HASH_FAIL,CSimpleStringA::Format("SendSM3Task http req fail,url=%s, err=%s",qTempReq.m_url.c_str(),qTempRet.m_errMsg.c_str()).GetData());
  160. return false;//失败
  161. }
  162. //打印回应报文
  163. if(m_fsm->m_PrintHttpLog){
  164. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("qTempRet=%s",qTempRet.m_retStr.c_str());
  165. }
  166. Json::Reader reader;
  167. Json::Value rootRet;
  168. if (!reader.parse(qTempRet.m_retStr, rootRet, false))
  169. {
  170. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_SEND_SM3_HASH_FAIL,CSimpleStringA::Format("SendSM3Task parse resp is fail,url=%s",qTempReq.m_url.c_str()).GetData());
  171. return false;//失败
  172. }
  173. bool isSucc = rootRet["success"].asBool();
  174. if(isSucc){
  175. if(rootRet.isMember("data")){
  176. if(rootRet["data"].asBool()){
  177. return true;//写入成功
  178. }else{
  179. return false;//写入失败
  180. }
  181. }else{
  182. return false;//写入失败
  183. }
  184. }else{
  185. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("SendSM3Task [success] is false");
  186. return false;//写入失败
  187. }
  188. }
  189. };
  190. //更新任务状态
  191. struct UpdateTaskStatus : public ITaskSp{
  192. CUpgradeTaskFSM* m_fsm;
  193. explicit UpdateTaskStatus(CUpgradeTaskFSM* f) : m_fsm(f) {}
  194. void Process(){
  195. LOG_FUNCTION();
  196. IHttpFunc* client;
  197. client = create_http(m_fsm->HttpsLogCallBack);
  198. bool isSendSucc=false;//更新是否成功标志
  199. if(UpdateTaskStatusImpl(client)){
  200. isSendSucc = true;
  201. }else{
  202. isSendSucc = false;
  203. }
  204. client->Destory();
  205. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::UpdateTaskStatusEvent(isSendSucc));//返回处理结果
  206. }
  207. bool UpdateTaskStatusImpl(IHttpFunc* client){
  208. CSystemStaticInfo info;
  209. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  210. if (rc != Error_Succeed)
  211. {
  212. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UpdateTaskStatus get CSystemStaticInfo fail,upgradeTaskId=%s",m_fsm->m_currentTask.upgradeTaskId.GetData());
  213. return false;
  214. }
  215. HttpStruct::UpdateTaskStatusReq qTempReq;
  216. HttpStruct::UpdateTaskStatusRet qTempRet;
  217. Json::Value rootReq;
  218. Json::FastWriter writer;
  219. rootReq["terminal_no"] = info.strTerminalID.GetData();
  220. rootReq["task_id"] = m_fsm->m_currentTask.upgradeTaskId.GetData();
  221. //状态为C时,为X时,或者T,版本不一致,则是回退,否则是成功
  222. //返回任务结果给服务器
  223. if(m_fsm->m_currentTask.cPendingState=="C"){
  224. rootReq["upgrade_status"] = "F";//失败
  225. }else if(m_fsm->m_currentTask.cPendingState=="X"){
  226. rootReq["upgrade_status"] = "X";//未知状态
  227. }else if(m_fsm->m_currentTask.cPendingState=="T"){
  228. if (m_fsm->m_currentTask.task_type.Compare("U") == 0) {
  229. if (m_fsm->isTerminalInstall()) {
  230. //安装状态时,达到终态就算成功
  231. rootReq["upgrade_status"] = "S";//成功
  232. }
  233. else {
  234. if (m_fsm->m_currentTask.NewVersion == m_fsm->m_currentVersion.ToString() ||
  235. m_fsm->m_currentTask.depNewVersion == m_fsm->m_currentDepVersion ||
  236. (m_fsm->m_currentTask.NewVersion == "" && m_fsm->m_currentTask.depNewVersion == "")) {
  237. //升级成功
  238. rootReq["upgrade_status"] = "S";//成功
  239. }
  240. else {
  241. //升级失败,回滚版本导致
  242. rootReq["upgrade_status"] = "F";//失败
  243. }
  244. }
  245. }
  246. else if (m_fsm->m_currentTask.task_type.Compare("R") == 0) {
  247. if (m_fsm->m_currentTask.NewVersion == m_fsm->m_currentVersion.ToString()||
  248. m_fsm->m_currentTask.depNewVersion == m_fsm->m_currentDepVersion) {
  249. //升级成功
  250. rootReq["upgrade_status"] = "S";//成功
  251. }
  252. else {
  253. //升级失败,回滚版本导致
  254. rootReq["upgrade_status"] = "F";//失败
  255. }
  256. }
  257. else {
  258. //不支持此类型,任务执行失败
  259. rootReq["upgrade_status"] = "X";//失败
  260. }
  261. }
  262. string jsonReq = writer.write(rootReq);
  263. qTempReq.m_reqStr = jsonReq;//请求参数
  264. qTempReq.m_url=m_fsm->m_centerUrl.updateTaskUrl;//访问地址
  265. //打印发送请求
  266. if(m_fsm->m_PrintHttpLog){
  267. qTempReq.m_printDbg = true;
  268. }
  269. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UpdateTaskStatus req=%s",qTempReq.m_reqStr.c_str());
  270. PROCESS_LINK_CONTEXT("LR0402506UpdateTaskStatus")
  271. if(!client->Post(qTempReq, qTempRet, &nextLink)){
  272. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_TASK_STATUS_FAIL,CSimpleStringA::Format("UpdateTaskStatus http req fail, upgradeTaskId=%s, url=%s, err=%s",m_fsm->m_currentTask.upgradeTaskId.GetData(),qTempReq.m_url.c_str(),qTempRet.m_errMsg.c_str()).GetData());
  273. return false;//失败
  274. }
  275. //打印回应报文
  276. if(m_fsm->m_PrintHttpLog){
  277. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("qTempRet=%s",qTempRet.m_retStr.c_str());
  278. }
  279. Json::Reader reader;
  280. Json::Value rootRet;
  281. if (!reader.parse(qTempRet.m_retStr, rootRet, false))
  282. {
  283. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_TASK_STATUS_FAIL,CSimpleStringA::Format("UpdateTaskStatus parse resp is fail, upgradeTaskId=%s, url=%s",m_fsm->m_currentTask.upgradeTaskId.GetData(),qTempReq.m_url.c_str()).GetData());
  284. return false;//失败
  285. }
  286. bool isSucc = rootRet["success"].asBool();
  287. if(isSucc){
  288. if(rootRet.isMember("data")){
  289. int isUpdate =rootRet["data"].asInt();
  290. //0:更新失败 1:更新成功 2:任务不属于此终端等异常情况,默认成功
  291. if(isUpdate==0){
  292. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setResultCode("RTA5673")("UpdateTaskStatus update fail: upgradeTaskId=%s",m_fsm->m_currentTask.upgradeTaskId.GetData());
  293. return false;//更新失败
  294. }else if(isUpdate==1){
  295. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UpdateTaskStatus update succ: req=%s",m_fsm->m_currentTask.upgradeTaskId.GetData(), jsonReq.c_str());
  296. return true;//更新成功
  297. }else if(isUpdate==2){
  298. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_TASK_STATUS_INVALID,CSimpleStringA::Format("UpdateTaskStatus update invalid,upgradeTaskId=%s",m_fsm->m_currentTask.upgradeTaskId.GetData()).GetData());
  299. return true;//任务异常,更新成功
  300. }else{
  301. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setResultCode("RTA5673")("UpdateTaskStatus update fail: upgradeTaskId=%s, ret=%d",m_fsm->m_currentTask.upgradeTaskId.GetData(),isUpdate);
  302. return false;//更新失败
  303. }
  304. }else{
  305. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setResultCode("RTA5673")("UpdateTaskStatus update fail: [data] is not exist ,upgradeTaskId=%s",m_fsm->m_currentTask.upgradeTaskId.GetData());
  306. return false;//更新失败
  307. }
  308. }else{
  309. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setResultCode("RTA5673")("UpdateTaskStatus update fail: [success] is false,upgradeTaskId=%s",m_fsm->m_currentTask.upgradeTaskId.GetData());
  310. return false;//更新失败
  311. }
  312. }
  313. };
  314. //查询当前任务
  315. struct QueryCurrTask : public ITaskSp{
  316. CUpgradeTaskFSM* m_fsm;
  317. explicit QueryCurrTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  318. void Process(){
  319. LOG_FUNCTION();
  320. //根据任务ID查询任务当前的策略。
  321. IHttpFunc* client;
  322. client = create_http(m_fsm->HttpsLogCallBack);
  323. bool isSendSucc=false;//查询是否成功标志
  324. bool isTask_valid=false;//任务是否有效
  325. if(QueryCurrTaskImpl(client,isTask_valid)){
  326. isSendSucc = true;
  327. }else{
  328. isSendSucc = false;
  329. }
  330. client->Destory();
  331. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::QueryCurrTaskEvent(isSendSucc,isTask_valid));//返回处理结果
  332. }
  333. bool QueryCurrTaskImpl(IHttpFunc* client,bool &isValid){
  334. HttpStruct::QueryCurrTaskReq qTempReq;
  335. HttpStruct::QueryCurrTaskRet qTempRet;
  336. CSystemStaticInfo info;
  337. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  338. if (rc != Error_Succeed)
  339. {
  340. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewTask get CSystemStaticInfo fail");
  341. return false;
  342. }
  343. //机器版本
  344. CSimpleStringA strMachineVersion = CSimpleStringA::Format("%d.%d",info.MachineVersion.GetMajor(),info.MachineVersion.GetMinor());
  345. //框架版本
  346. CSimpleStringA strInstallVersion = m_fsm->m_currentVersion.ToString();
  347. //适配器版本
  348. CSimpleStringA strDepVersion = m_fsm->m_currentDepVersion;
  349. // OS版本
  350. #ifdef RVC_OS_WIN
  351. OSVERSIONINFO osvi;
  352. ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  353. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  354. GetVersionEx(&osvi);
  355. CSimpleStringA strOSVersion = CSimpleStringA::Format("%d.%d.%d",osvi.dwMajorVersion,osvi.dwMinorVersion,osvi.dwBuildNumber);
  356. #else
  357. DWORD dwMajorVersion(0), dwMinorVersion(0), dwBuildNumber(0);
  358. struct utsname kernel_info;
  359. int ret = uname(&kernel_info);
  360. if (ret == 0) {
  361. int n = sscanf((const char*)kernel_info.release, "%d.%d", &dwMajorVersion, &dwMinorVersion);
  362. }
  363. else {
  364. Dbg("get linux kernel version fail: %s", strerror(errno));
  365. }
  366. CSimpleStringA strOSVersion = CSimpleStringA::Format("%d.%d.0", dwMajorVersion, dwMinorVersion);
  367. #endif
  368. Json::Value rootReq;
  369. Json::FastWriter writer;
  370. rootReq["terminal_no"] = info.strTerminalID.GetData();
  371. rootReq["task_id"] = m_fsm->m_currentTask.upgradeTaskId.GetData();
  372. rootReq["machine_type"] = info.strMachineType.GetData();
  373. rootReq["software_version"] = strInstallVersion.GetData();
  374. rootReq["os_version"] = strOSVersion.GetData();
  375. rootReq["machine_version"] = strMachineVersion.GetData();
  376. rootReq["dep_version"] = strDepVersion.GetData();//适配器版本
  377. string jsonReq = writer.write(rootReq);
  378. qTempReq.m_reqStr = jsonReq;//请求参数
  379. qTempReq.m_url=m_fsm->m_centerUrl.queryCurrTaskUrl;//访问地址
  380. //打印发送请求
  381. if(m_fsm->m_PrintHttpLog){
  382. qTempReq.m_printDbg = true;
  383. }
  384. PROCESS_LINK_CONTEXT("LR0402506QueryCurrTask")
  385. if(!client->Post(qTempReq, qTempRet, &nextLink)){
  386. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL,CSimpleStringA::Format("QueryCurrTask http req fail,url=%s, err=%s",qTempReq.m_url.c_str(),qTempRet.m_errMsg.c_str()).GetData());
  387. return false;//失败
  388. }
  389. //打印回应报文
  390. if(m_fsm->m_PrintHttpLog){
  391. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("qTempRet=%s",qTempRet.m_retStr.c_str());
  392. }
  393. Json::Reader reader;
  394. Json::Value rootRet;
  395. if (!reader.parse(qTempRet.m_retStr, rootRet, false))
  396. {
  397. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL,CSimpleStringA::Format("QueryCurrTask parse resp is fail,url=%s",qTempReq.m_url.c_str()).GetData());
  398. return false;//失败
  399. }
  400. bool isSucc = rootRet["success"].asBool();
  401. if(isSucc){
  402. if(rootRet.isMember("data")){
  403. if(rootRet["data"]["task_status"].asBool()){
  404. isValid = true;//任务有效
  405. if (rootRet["data"].isMember("task_type")) {
  406. m_fsm->m_currentTask.task_type = rootRet["data"]["task_type"].asString().c_str();
  407. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask query task_type=%s", m_fsm->m_currentTask.task_type.GetData());
  408. }
  409. else {
  410. m_fsm->m_currentTask.task_type = "U";//兼容旧服务端程序,无字段值时默认是升级模式
  411. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask query task_type is not exist , use default value task_type = U");
  412. }
  413. if (m_fsm->m_currentTask.task_type.Compare("U") == 0) {
  414. //升级任务
  415. if (!ParamCheck(rootRet)) {
  416. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL, CSimpleStringA::Format("QueryCurrTask paramCheck is fail, retry once again ,url=%s", qTempReq.m_url.c_str()).GetData());
  417. return false;//查询失败 升级任务参数效验失败,无法进行升级
  418. }
  419. else {
  420. CSimpleStringA packNameStr = rootRet["data"]["pack_name"].asString().c_str();
  421. if (m_fsm->m_currentTask.pack_name != packNameStr) {
  422. //异常情况
  423. CSimpleStringA errmsg = CSimpleStringA::Format("runinfo packName[%s] is not equal server packName[%s]", m_fsm->m_currentTask.pack_name.GetData(), packNameStr.GetData());
  424. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_PACKNAME_NOT_EQUAL, errmsg.GetData());
  425. }
  426. m_fsm->m_currentTask.pack_name = packNameStr;//用最新的包名进行后面的安装和下载
  427. m_fsm->m_currentTask.file_path = rootRet["data"]["file_path"].asString().c_str();
  428. m_fsm->m_currentTask.download_url = rootRet["data"]["download_url"].asString().c_str();
  429. m_fsm->m_currentTask.pack_hash = rootRet["data"]["pack_hash"].asString().c_str();
  430. m_fsm->m_currentTask.pack_length = (long)rootRet["data"]["pack_length"].asInt();
  431. m_fsm->m_currentTask.pack_version = rootRet["data"]["pack_version"].asString().c_str();
  432. m_fsm->m_currentTask.reboot_type = rootRet["data"]["reboot_type"].asInt();
  433. m_fsm->m_currentTask.stop_media_play = rootRet["data"]["stop_media_play"].asBool();
  434. //解析step json
  435. if (!ParseInstallStep(rootRet)) {
  436. m_fsm->ClearInstallStep();
  437. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL, CSimpleStringA::Format("QueryCurrTask ParseInstallStep is fail, retry once again ,url=%s", qTempReq.m_url.c_str()).GetData());
  438. return false;//解析安装步骤失败
  439. }
  440. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("current task id =%s , task_type=%s", m_fsm->m_currentTask.upgradeTaskId.GetData(), m_fsm->m_currentTask.task_type.GetData());
  441. return true;//查询成功
  442. }
  443. }
  444. else if (m_fsm->m_currentTask.task_type.Compare("R") == 0) {
  445. //回退任务
  446. if (!rollbackParamCheck(rootRet)) {
  447. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL, CSimpleStringA::Format("QueryCurrTask paramCheck is fail, retry once again ,url=%s", qTempReq.m_url.c_str()).GetData());
  448. return false;//查询失败 回退任务参数效验失败,无法进行回退
  449. }
  450. else {
  451. m_fsm->m_currentTask.pack_version = rootRet["data"]["pack_version"].asString().c_str();
  452. m_fsm->m_currentTask.reboot_type = rootRet["data"]["reboot_type"].asInt();
  453. m_fsm->m_currentTask.stop_media_play = rootRet["data"]["stop_media_play"].asBool();
  454. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("current task id =%s", m_fsm->m_currentTask.upgradeTaskId.GetData());
  455. return true;//查询成功
  456. }
  457. }
  458. else {
  459. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL, CSimpleStringA::Format("QueryCurrTask return task [task_type] is not R or U ,task_type=%s", m_fsm->m_currentTask.task_type.GetData()).GetData());
  460. return false;//查询失败
  461. }
  462. }else{
  463. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask query task is invalid, taskid=%s",m_fsm->m_currentTask.upgradeTaskId.GetData());
  464. isValid = false;//任务无效
  465. return true;//查询成功
  466. }
  467. }else{
  468. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL,CSimpleStringA::Format("QueryCurrTask return task [data] is null ,url=%s",qTempReq.m_url.c_str()).GetData());
  469. return false;//查询失败
  470. }
  471. }else{
  472. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL,CSimpleStringA::Format("QueryCurrTask [success] is false ,url=%s",qTempReq.m_url.c_str()).GetData());
  473. return false;//查询失败
  474. }
  475. }
  476. bool ParamCheck(Json::Value rootRet){
  477. CSimpleStringA packNameStr = rootRet["data"]["pack_name"].asString().c_str();
  478. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_name] is %s",packNameStr.GetData());
  479. if(packNameStr.IsNullOrEmpty()){
  480. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_name] is null");
  481. return false;
  482. }
  483. CSimpleStringA filePathStr = rootRet["data"]["file_path"].asString().c_str();
  484. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [file_path] is %s",filePathStr.GetData());
  485. if(filePathStr.IsNullOrEmpty()){
  486. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [file_path] is null");
  487. return false;
  488. }
  489. CSimpleStringA downloadUrlStr = rootRet["data"]["download_url"].asString().c_str();
  490. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [download_url] is %s",downloadUrlStr.GetData());
  491. if(downloadUrlStr.IsNullOrEmpty()){
  492. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [download_url] is null");
  493. return false;
  494. }
  495. CSimpleStringA packHashStr = rootRet["data"]["pack_hash"].asString().c_str();
  496. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_hash] is %s",packHashStr.GetData());
  497. if(packHashStr.IsNullOrEmpty()){
  498. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_hash] is null");
  499. return false;
  500. }
  501. long packLengthLong = (long)rootRet["data"]["pack_length"].asInt();
  502. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_length] is %d",packLengthLong);
  503. int rebootTypeInt = rootRet["data"]["reboot_type"].asInt();
  504. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [reboot_type] is %d",rebootTypeInt);
  505. if(rebootTypeInt!=1 && rebootTypeInt!=2 && rebootTypeInt!=0){
  506. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [reboot_type] is invalid ,reboot_type=%d",rebootTypeInt);
  507. return false;
  508. }
  509. CSimpleStringA packVersionStr = rootRet["data"]["pack_version"].asString().c_str();
  510. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_version] is %s",packVersionStr.GetData());
  511. if(!packVersionStr.IsNullOrEmpty()){
  512. int rebootType = rootRet["data"]["reboot_type"].asInt();
  513. if(rebootType == 1 || rebootType == 2){
  514. return true;
  515. }else{
  516. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_version] is not null ,[reboot_type] is not equal 1 or 2");
  517. return false;
  518. }
  519. }
  520. return true;
  521. }
  522. bool rollbackParamCheck(Json::Value rootRet) {
  523. int rebootTypeInt = rootRet["data"]["reboot_type"].asInt();
  524. if (rebootTypeInt != 1 && rebootTypeInt != 2) {
  525. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [reboot_type] is invalid ,reboot_type=%d", rebootTypeInt);
  526. return false;
  527. }
  528. CSimpleStringA packVersionStr = rootRet["data"]["pack_version"].asString().c_str();
  529. if (packVersionStr.IsNullOrEmpty()) {
  530. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask paramCheck [pack_version] is null");
  531. return false;
  532. }
  533. return true;
  534. }
  535. bool ParseInstallStep(Json::Value rootRet){
  536. //清理以前的遗留的安装步骤
  537. m_fsm->ClearInstallStep();
  538. string cmdstr = rootRet["data"]["cmd"].asString();
  539. if(cmdstr.empty()){
  540. return true;//为空则直接不解析
  541. }
  542. Json::Reader reader;
  543. Json::Value InstallRet;
  544. if (!reader.parse(cmdstr, InstallRet, false))
  545. {
  546. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CURR_TASK_FAIL,CSimpleStringA::Format("QueryCurrTask parse cmd fail,cmdstr=%s",cmdstr.c_str()).GetData());
  547. return false;//解析失败
  548. }
  549. //install_step
  550. for(int i=0;i<InstallRet.size();i++){
  551. if(!InstallRet[i]["seq"].isInt()){
  552. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][seq] is not int");
  553. return false;
  554. }
  555. int stepSeq = InstallRet[i]["seq"].asInt();
  556. if(!InstallRet[i]["cmdType"].isString()&&!InstallRet[i]["cmdType"].isNull()){
  557. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][cmdType] is not string");
  558. return false;
  559. }
  560. CSimpleStringA cmdType = InstallRet[i]["cmdType"].asString().c_str();
  561. if(cmdType.IsNullOrEmpty()){
  562. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][cmdType] is null");
  563. return false;
  564. }
  565. if(!InstallRet[i]["cmdFail"].isInt()){
  566. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][cmdFail] is not int");
  567. return false;
  568. }
  569. int cmdFail = InstallRet[i]["cmdFail"].asInt();
  570. if(!InstallRet[i]["srcPathType"].isString()&&!InstallRet[i]["srcPathType"].isNull()){
  571. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][srcPathType] is not string");
  572. return false;
  573. }
  574. CSimpleStringA srcPathType = InstallRet[i]["srcPathType"].asString().c_str();
  575. if(cmdType=="Copy"){
  576. //复制指令时,不为空
  577. if(srcPathType.IsNullOrEmpty()){
  578. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][srcPathType] is null");
  579. return false;
  580. }
  581. }
  582. if(!InstallRet[i]["srcPath"].isString()&&!InstallRet[i]["srcPath"].isNull()){
  583. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][srcPath] is not string");
  584. return false;
  585. }
  586. CSimpleStringA srcPath = InstallRet[i]["srcPath"].asString().c_str();
  587. if(cmdType=="Copy"){
  588. //复制指令时,不为空
  589. if(srcPath.IsNullOrEmpty()){
  590. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][srcPath] is null");
  591. return false;
  592. }
  593. }
  594. if(!InstallRet[i]["dstPathType"].isString()&&!InstallRet[i]["dstPathType"].isNull()){
  595. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][dstPathType] is not string");
  596. return false;
  597. }
  598. CSimpleStringA dstPathType = InstallRet[i]["dstPathType"].asString().c_str();
  599. if(cmdType=="Copy"||cmdType=="Delete"||cmdType=="Run"){
  600. if(dstPathType.IsNullOrEmpty()){
  601. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][dstPathType] is null");
  602. return false;
  603. }
  604. }
  605. if(!InstallRet[i]["dstPath"].isString()&&!InstallRet[i]["dstPath"].isNull()){
  606. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][dstPath] is not string");
  607. return false;
  608. }
  609. CSimpleStringA dstPath = InstallRet[i]["dstPath"].asString().c_str();
  610. if(cmdType=="Delete"||cmdType=="Run"){
  611. if(dstPath.IsNullOrEmpty()){
  612. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryCurrTask ParseInstallStep [cmd][dstPath] is null");
  613. return false;
  614. }
  615. }
  616. CUpgradeTaskFSM::CInstallStep* iStep= new CUpgradeTaskFSM::CInstallStep();
  617. iStep->stepSeq = stepSeq;
  618. iStep->cmdType = cmdType;
  619. iStep->cmdFail = cmdFail;
  620. iStep->srcPathType = srcPathType;
  621. iStep->srcPath = srcPath;
  622. iStep->dstPathType =dstPathType;
  623. iStep->dstPath =dstPath;
  624. m_fsm->m_currentTask.install_step.push(iStep);//加入队尾
  625. }
  626. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("install cmd size = %d",InstallRet.size());
  627. return true;
  628. }
  629. };
  630. //预查询新任务
  631. struct QueryNewPreTask : public ITaskSp{
  632. CUpgradeTaskFSM* m_fsm;
  633. explicit QueryNewPreTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  634. void Process(){
  635. //LOG_FUNCTION();
  636. IHttpFunc* client;
  637. client = create_http(m_fsm->HttpsLogCallBack);
  638. //查询新任务
  639. bool isSendSucc=false;////查询是否成功标志
  640. if(QueryNewPreTaskImpl(client)){
  641. isSendSucc = true;
  642. }else{
  643. isSendSucc = false;
  644. }
  645. client->Destory();
  646. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::QueryNewPreTaskEvent(isSendSucc));//返回处理结果
  647. }
  648. bool QueryNewPreTaskImpl(IHttpFunc* client){
  649. HttpStruct::QueryNewTaskReq qTempReq;
  650. HttpStruct::QueryNewTaskRet qTempRet;
  651. CSystemStaticInfo info;
  652. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  653. if (rc != Error_Succeed)
  654. {
  655. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewPreTask get CSystemStaticInfo fail");
  656. return false;
  657. }
  658. //机器版本
  659. CSimpleStringA strMachineVersion = CSimpleStringA::Format("%d.%d",info.MachineVersion.GetMajor(),info.MachineVersion.GetMinor());
  660. //框架版本
  661. CSimpleStringA strInstallVersion = m_fsm->m_currentVersion.ToString();
  662. //适配器版本
  663. CSimpleStringA strDepVersion = m_fsm->m_currentDepVersion;
  664. // OS版本
  665. #ifdef RVC_OS_WIN
  666. OSVERSIONINFO osvi;
  667. ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  668. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  669. GetVersionEx(&osvi);
  670. CSimpleStringA strOSVersion = CSimpleStringA::Format("%d.%d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber);
  671. #else
  672. DWORD dwMajorVersion(0), dwMinorVersion(0), dwBuildNumber(0);
  673. struct utsname kernel_info;
  674. int ret = uname(&kernel_info);
  675. if (ret == 0) {
  676. int n = sscanf((const char*)kernel_info.release, "%d.%d", &dwMajorVersion, &dwMinorVersion);
  677. }
  678. else {
  679. Dbg("get linux kernel version fail: %s", strerror(errno));
  680. }
  681. CSimpleStringA strOSVersion = CSimpleStringA::Format("%d.%d.0", dwMajorVersion, dwMinorVersion);
  682. #endif
  683. Json::Value rootReq;
  684. Json::FastWriter writer;
  685. rootReq["terminal_no"] = info.strTerminalID.GetData();
  686. rootReq["machine_type"] = info.strMachineType.GetData();
  687. rootReq["software_version"] = strInstallVersion.GetData();
  688. rootReq["os_version"] = strOSVersion.GetData();
  689. rootReq["machine_version"] = strMachineVersion.GetData();
  690. rootReq["dep_version"] = strDepVersion.GetData();//适配器版本
  691. //预查询判断加入查询何种任务。
  692. if (m_fsm->isTerminalInstall()) {
  693. rootReq["task_type"] = "I";//安装任务
  694. }
  695. else {
  696. rootReq["task_type"] = "U";//升级任务
  697. }
  698. #ifdef RVC_OS_WIN
  699. rootReq["system_type"] = "W";//表示是windows类型的机器
  700. #else
  701. rootReq["system_type"] = "L";//表示是uos类型的机器
  702. #endif
  703. string jsonReq = writer.write(rootReq);
  704. qTempReq.m_reqStr = jsonReq;//请求参数
  705. qTempReq.m_url=m_fsm->m_centerUrl.queryPreTaskUrl;//访问地址
  706. //打印发送请求
  707. if(m_fsm->m_PrintHttpLog){
  708. qTempReq.m_printDbg = true;
  709. }
  710. PROCESS_LINK_CONTEXT("LR0402506QueryNewPreTask")
  711. if(!client->Post(qTempReq, qTempRet, &nextLink)){
  712. if (qTempRet.m_sysCode == 6) {
  713. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(UpgradeMgr_PerTask_HTTP_ERR).setResultCode("RTA5651")("终端查询任务域名解析失败");
  714. }else if(qTempRet.m_sysCode == 28){
  715. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(UpgradeMgr_PerTask_HTTP_ERR).setResultCode("RTA5652")("终端查询任务连接总行服务超时");
  716. }else {
  717. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(UpgradeMgr_PerTask_HTTP_ERR).setResultCode("RTA5653")("终端查询任务失败:%s", qTempRet.m_errMsg.c_str());
  718. }
  719. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_PRE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewPreTask http req fail,url=%s, err=%s",qTempReq.m_url.c_str(),qTempRet.m_errMsg.c_str()).GetData());
  720. return false;//失败
  721. }
  722. //打印回应报文
  723. if(m_fsm->m_PrintHttpLog){
  724. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("qTempRet=%s",qTempRet.m_retStr.c_str());
  725. }
  726. Json::Reader reader;
  727. Json::Value rootRet;
  728. if (!reader.parse(qTempRet.m_retStr, rootRet, false)){
  729. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_PRE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewPreTask parse resp is fail,url=%s",qTempReq.m_url.c_str()).GetData());
  730. return false;//失败
  731. }
  732. bool isSucc = rootRet["success"].asBool();
  733. if(isSucc){
  734. if(rootRet.isMember("data")){
  735. //解析任务,可能不存在任务
  736. if(rootRet["data"]["task_id"].asString()!=""){
  737. m_fsm->m_strPreTaskId = rootRet["data"]["task_id"].asString().c_str();
  738. int need_hash = rootRet["data"]["need_hash"].asInt();
  739. if(need_hash==0){
  740. m_fsm->m_bNeedHash = false;
  741. }else{
  742. m_fsm->m_bNeedHash = true;
  743. }
  744. return true;//查询成功
  745. }else{
  746. //无任务
  747. m_fsm->m_strPreTaskId = "";
  748. m_fsm->m_bNeedHash =false;
  749. return true;//查询成功
  750. }
  751. }else{
  752. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_PRE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewPreTask return [data] is null, retry once again ,url=%s",qTempReq.m_url.c_str()).GetData());
  753. return false;//查询失败
  754. }
  755. }else{
  756. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_PRE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewPreTask [success] is false, retry once again ,url=%s",qTempReq.m_url.c_str()).GetData());
  757. return false;//查询失败
  758. }
  759. }
  760. };
  761. //继续查询新任务
  762. struct QueryNewContinueTask : public ITaskSp{
  763. CUpgradeTaskFSM* m_fsm;
  764. explicit QueryNewContinueTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  765. void Process(){
  766. LOG_FUNCTION();
  767. IHttpFunc* client;
  768. client = create_http(m_fsm->HttpsLogCallBack);
  769. //查询新任务
  770. int retParm=QueryNewContinueTaskImpl(client);
  771. client->Destory();
  772. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::QueryNewContinueTaskEvent(retParm));//返回处理结果
  773. }
  774. // 0:重试 1:成功 2:跳出
  775. int QueryNewContinueTaskImpl(IHttpFunc* client){
  776. HttpStruct::QueryNewTaskReq qTempReq;
  777. HttpStruct::QueryNewTaskRet qTempRet;
  778. CSystemStaticInfo info;
  779. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  780. if (rc != Error_Succeed)
  781. {
  782. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask get CSystemStaticInfo fail");
  783. return 0;//查询失败,继续查询
  784. }
  785. //机器版本
  786. CSimpleStringA strMachineVersion = CSimpleStringA::Format("%d.%d",info.MachineVersion.GetMajor(),info.MachineVersion.GetMinor());
  787. //框架版本
  788. CSimpleStringA strInstallVersion = m_fsm->m_currentVersion.ToString();
  789. //适配器版本
  790. CSimpleStringA strDepVersion = m_fsm->m_currentDepVersion;
  791. // OS版本
  792. #ifdef RVC_OS_WIN
  793. OSVERSIONINFO osvi;
  794. ZeroMemory(&osvi, sizeof(OSVERSIONINFO));
  795. osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
  796. GetVersionEx(&osvi);
  797. CSimpleStringA strOSVersion = CSimpleStringA::Format("%d.%d.%d", osvi.dwMajorVersion, osvi.dwMinorVersion, osvi.dwBuildNumber);
  798. #else
  799. DWORD dwMajorVersion(0), dwMinorVersion(0), dwBuildNumber(0);
  800. struct utsname kernel_info;
  801. int ret = uname(&kernel_info);
  802. if (ret == 0) {
  803. int n = sscanf((const char*)kernel_info.release, "%d.%d", &dwMajorVersion, &dwMinorVersion);
  804. }
  805. else {
  806. Dbg("get linux kernel version fail: %s", strerror(errno));
  807. }
  808. CSimpleStringA strOSVersion = CSimpleStringA::Format("%d.%d.0", dwMajorVersion, dwMinorVersion);
  809. #endif
  810. Json::Value rootReq;
  811. Json::FastWriter writer;
  812. rootReq["terminal_no"] = info.strTerminalID.GetData();
  813. rootReq["task_id"] = m_fsm->m_strPreTaskId.GetData();
  814. rootReq["machine_type"] = info.strMachineType.GetData();
  815. rootReq["software_version"] = strInstallVersion.GetData();
  816. rootReq["os_version"] = strOSVersion.GetData();
  817. rootReq["machine_version"] = strMachineVersion.GetData();
  818. rootReq["dep_version"] = strDepVersion.GetData();//适配器版本
  819. #ifdef RVC_OS_WIN
  820. rootReq["system_type"] = "W";//表示是windows类型的机器
  821. #else
  822. rootReq["system_type"] = "L";//表示是uos类型的机器
  823. #endif
  824. if(m_fsm->m_bNeedHash&&m_fsm->isSendHash){
  825. //是否发送hash码并且发送成功了
  826. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask with hash");
  827. rootReq["list_hash"] = m_fsm->m_fileSM3ListHash.GetData();
  828. }else{
  829. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask with no hash");
  830. rootReq["list_hash"] = "";
  831. }
  832. string jsonReq = writer.write(rootReq);
  833. qTempReq.m_reqStr = jsonReq;//请求参数
  834. qTempReq.m_url=m_fsm->m_centerUrl.queryContinueTaskUrl;//访问地址
  835. //打印发送请求
  836. if(m_fsm->m_PrintHttpLog){
  837. qTempReq.m_printDbg = true;
  838. }
  839. PROCESS_LINK_CONTEXT("LR0402506QueryNewContinueTask")
  840. if(!client->Post(qTempReq, qTempRet, &nextLink)){
  841. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewContinueTask http req fail,url=%s, err=%s",qTempReq.m_url.c_str(),qTempRet.m_errMsg.c_str()).GetData());
  842. return 0;//查询失败,继续查询
  843. }
  844. //打印回应报文
  845. if(m_fsm->m_PrintHttpLog){
  846. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("qTempRet=%s",qTempRet.m_retStr.c_str());
  847. }
  848. Json::Reader reader;
  849. Json::Value rootRet;
  850. if (!reader.parse(qTempRet.m_retStr, rootRet, false))
  851. {
  852. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewContinueTask parse resp is fail,url=%s",qTempReq.m_url.c_str()).GetData());
  853. return 0;//查询失败,继续查询
  854. }
  855. bool isSucc = rootRet["success"].asBool();
  856. if(isSucc){
  857. if(rootRet.isMember("data")){
  858. //解析任务,可能不存在任务,task_id为空,则表示无此任务。
  859. if(rootRet["data"]["task_id"].asString()!=""){
  860. //task_status 0-无效 1-有效 2-对比不通过 3-hash内容为空
  861. int task_status = rootRet["data"]["task_status"].asInt();
  862. if(task_status ==1){
  863. if (rootRet["data"].isMember("task_type")) {
  864. m_fsm->m_currentTask.task_type = rootRet["data"]["task_type"].asString().c_str();
  865. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask query task_type=%s", m_fsm->m_currentTask.task_type.GetData());
  866. }
  867. else {
  868. m_fsm->m_currentTask.task_type = "U";//兼容旧服务端程序,无字段值时默认是升级模式
  869. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask query task_type is not exist , use default value task_type = U");
  870. }
  871. if (m_fsm->m_currentTask.task_type.Compare("U") == 0) {
  872. //pack_status 0-准备中 1-已就绪
  873. int status = rootRet["data"]["pack_status"].asInt();
  874. if (status == 1) {
  875. if (!ParamCheck(rootRet)) {
  876. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_ERR, CSimpleStringA::Format("QueryNewContinueTask paramCheck is fail, check out server exception ,url=%s", qTempReq.m_url.c_str()).GetData());
  877. return 0;//查询失败 升级任务参数效验失败,告警码不同
  878. }
  879. else {
  880. m_fsm->m_currentTask.upgradeTaskId = rootRet["data"]["task_id"].asString().c_str();
  881. m_fsm->m_currentTask.pack_name = rootRet["data"]["pack_name"].asString().c_str();
  882. m_fsm->m_currentTask.file_path = rootRet["data"]["file_path"].asString().c_str();
  883. m_fsm->m_currentTask.download_url = rootRet["data"]["download_url"].asString().c_str();
  884. m_fsm->m_currentTask.pack_hash = rootRet["data"]["pack_hash"].asString().c_str();
  885. m_fsm->m_currentTask.pack_length = (long)rootRet["data"]["pack_length"].asInt();
  886. m_fsm->m_currentTask.pack_version = rootRet["data"]["pack_version"].asString().c_str();
  887. m_fsm->m_currentTask.reboot_type = rootRet["data"]["reboot_type"].asInt();
  888. m_fsm->m_currentTask.stop_media_play = rootRet["data"]["stop_media_play"].asBool();
  889. //解析step json
  890. if (!ParseInstallStep(rootRet)) {
  891. m_fsm->ClearInstallStep();
  892. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_ERR, CSimpleStringA::Format("QueryNewContinueTask ParseInstallStep is fail, check out server exception ,url=%s", qTempReq.m_url.c_str()).GetData());
  893. return 0;//查询失败 解析安装步骤失败,告警码不同
  894. }
  895. m_fsm->m_currentTask.createTaskVersion = m_fsm->m_currentVersion.ToString();//新策略,查询到包后,用当前版本值赋值
  896. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("new task_id =%s, task_type=%s", m_fsm->m_currentTask.upgradeTaskId.GetData(), m_fsm->m_currentTask.task_type.GetData());
  897. return 1;//查询成功
  898. }
  899. }
  900. else {
  901. //任务未准备好
  902. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask query pack is not create ,please next query,status=%d", status);
  903. m_fsm->m_currentTask.upgradeTaskId = "";
  904. return 0;//查询成功,继续查询
  905. }
  906. }
  907. else if (m_fsm->m_currentTask.task_type.Compare("R") == 0) {
  908. //回退任务
  909. if (!rollbackParamCheck(rootRet)) {
  910. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_ERR, CSimpleStringA::Format("QueryNewContinueTask paramCheck is fail, check out server exception ,url=%s", qTempReq.m_url.c_str()).GetData());
  911. return 0;//查询失败 升级任务参数效验失败,告警码不同
  912. }
  913. else {
  914. m_fsm->m_currentTask.upgradeTaskId = rootRet["data"]["task_id"].asString().c_str();
  915. m_fsm->m_currentTask.pack_version = rootRet["data"]["pack_version"].asString().c_str();
  916. m_fsm->m_currentTask.reboot_type = rootRet["data"]["reboot_type"].asInt();
  917. m_fsm->m_currentTask.stop_media_play = rootRet["data"]["stop_media_play"].asBool();
  918. m_fsm->m_currentTask.createTaskVersion = m_fsm->m_currentVersion.ToString();
  919. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("new task id =%s, task_type=%s", m_fsm->m_currentTask.upgradeTaskId.GetData(), m_fsm->m_currentTask.task_type.GetData());
  920. return 1;//查询成功
  921. }
  922. }
  923. else {
  924. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_ERR, CSimpleStringA::Format("QueryNewContinueTask query fail, task_type is not U or R ,task_type=%s", m_fsm->m_currentTask.task_type.GetData()).GetData());
  925. return 0;//查询失败,taskType 不支持处理,告警码不同
  926. }
  927. }else{
  928. //任务状态无效,对比不通过,hash内容为空
  929. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewContinueTask query fail ,task_status=%d",task_status).GetData());
  930. return 2;//查询成功,但任务不再,跳出来
  931. }
  932. }else{
  933. //返回任务为空
  934. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewContinueTask return [task id] is null").GetData());
  935. return 2;//查询成功,但任务不再,跳出来
  936. }
  937. }else{
  938. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_ERR,CSimpleStringA::Format("QueryNewContinueTask return [data] is null, check out server exception ,url=%s",qTempReq.m_url.c_str()).GetData());
  939. return 0;//查询失败,继续查询,告警码不同
  940. }
  941. }else{
  942. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewContinueTask [success] is false, retry once again ,url=%s",qTempReq.m_url.c_str()).GetData());
  943. return 0;//查询失败,继续查询
  944. }
  945. }
  946. bool ParamCheck(Json::Value rootRet){
  947. CSimpleStringA upgradeTaskIdStr = rootRet["data"]["task_id"].asString().c_str();
  948. if(upgradeTaskIdStr.IsNullOrEmpty()){
  949. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [task_id] is null");
  950. return false;
  951. }
  952. CSimpleStringA packNameStr = rootRet["data"]["pack_name"].asString().c_str();
  953. if(packNameStr.IsNullOrEmpty()){
  954. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [pack_name] is null");
  955. return false;
  956. }
  957. CSimpleStringA filePathStr = rootRet["data"]["file_path"].asString().c_str();
  958. if(filePathStr.IsNullOrEmpty()){
  959. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [file_path] is null");
  960. return false;
  961. }
  962. CSimpleStringA downloadUrlStr = rootRet["data"]["download_url"].asString().c_str();
  963. if(downloadUrlStr.IsNullOrEmpty()){
  964. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [download_url] is null");
  965. return false;
  966. }
  967. CSimpleStringA packHashStr = rootRet["data"]["pack_hash"].asString().c_str();
  968. if(packHashStr.IsNullOrEmpty()){
  969. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [pack_hash] is null");
  970. return false;
  971. }
  972. long packLengthStr = (long)rootRet["data"]["pack_length"].asInt();
  973. int rebootTypeInt = rootRet["data"]["reboot_type"].asInt();
  974. if(rebootTypeInt!=1 && rebootTypeInt!=2 && rebootTypeInt!=0){
  975. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [reboot_type] is invalid ,reboot_type=%d",rebootTypeInt);
  976. return false;
  977. }
  978. //CSimpleStringA packVersionStr = rootRet["data"]["pack_version"].asString().c_str();
  979. //if(!packVersionStr.IsNullOrEmpty()){
  980. // int rebootType = rootRet["data"]["reboot_type"].asInt();
  981. // if(rebootType == 1 || rebootType == 2){
  982. // return true;
  983. // }else{
  984. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("QueryNewContinueTask paramCheck [pack_version] is not null ,[reboot_type] is not equal 1 or 2");
  985. // return false;
  986. // }
  987. //}
  988. return true;
  989. }
  990. bool rollbackParamCheck(Json::Value rootRet) {
  991. CSimpleStringA upgradeTaskIdStr = rootRet["data"]["task_id"].asString().c_str();
  992. if(upgradeTaskIdStr.IsNullOrEmpty()){
  993. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [task_id] is null");
  994. return false;
  995. }
  996. int rebootTypeInt = rootRet["data"]["reboot_type"].asInt();
  997. if(rebootTypeInt!=1 && rebootTypeInt!=2){
  998. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [reboot_type] is invalid ,reboot_type=%d",rebootTypeInt);
  999. return false;
  1000. }
  1001. CSimpleStringA packVersionStr = rootRet["data"]["pack_version"].asString().c_str();
  1002. if (packVersionStr.IsNullOrEmpty()) {
  1003. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask paramCheck [pack_version] is null");
  1004. return false;
  1005. }
  1006. return true;
  1007. }
  1008. bool ParseInstallStep(Json::Value rootRet){
  1009. //清理以前的遗留的安装步骤
  1010. m_fsm->ClearInstallStep();
  1011. string cmdstr = rootRet["data"]["cmd"].asString();
  1012. if(cmdstr.empty()){
  1013. return true;//为空则直接不解析
  1014. }
  1015. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cmdstr =%s",cmdstr.c_str());
  1016. Json::Reader reader;
  1017. Json::Value InstallRet;
  1018. if (!reader.parse(cmdstr, InstallRet, false))
  1019. {
  1020. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_CONTINUE_NEW_TASK_FAIL,CSimpleStringA::Format("QueryNewContinueTask parse cmd fail,cmdstr=%s",cmdstr.c_str()).GetData());
  1021. return false;//解析失败
  1022. }
  1023. for(int i=0;i<InstallRet.size();i++){
  1024. if(!InstallRet[i]["seq"].isInt()){
  1025. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][seq] is not int");
  1026. return false;
  1027. }
  1028. int stepSeq = InstallRet[i]["seq"].asInt();
  1029. if(!InstallRet[i]["cmdType"].isString()&&!InstallRet[i]["cmdType"].isNull()){
  1030. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][cmdType] is not string");
  1031. return false;
  1032. }
  1033. CSimpleStringA cmdType = InstallRet[i]["cmdType"].asString().c_str();
  1034. if(cmdType.IsNullOrEmpty()){
  1035. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][cmdType] is null");
  1036. return false;
  1037. }
  1038. if(!InstallRet[i]["cmdFail"].isInt()){
  1039. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][cmdFail] is not int");
  1040. return false;
  1041. }
  1042. int cmdFail = InstallRet[i]["cmdFail"].asInt();
  1043. if(!InstallRet[i]["srcPathType"].isString()&&!InstallRet[i]["srcPathType"].isNull()){
  1044. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][srcPathType] is not string");
  1045. return false;
  1046. }
  1047. CSimpleStringA srcPathType = InstallRet[i]["srcPathType"].asString().c_str();
  1048. if(cmdType=="Copy"){
  1049. //复制指令时,不为空
  1050. if(srcPathType.IsNullOrEmpty()){
  1051. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][srcPathType] is null");
  1052. return false;
  1053. }
  1054. }
  1055. if(!InstallRet[i]["srcPath"].isString()&&!InstallRet[i]["srcPath"].isNull()){
  1056. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][srcPath] is not string");
  1057. return false;
  1058. }
  1059. CSimpleStringA srcPath = InstallRet[i]["srcPath"].asString().c_str();
  1060. if(cmdType=="Copy"){
  1061. //复制指令时,不为空
  1062. if(srcPath.IsNullOrEmpty()){
  1063. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][srcPath] is null");
  1064. return false;
  1065. }
  1066. }
  1067. if(!InstallRet[i]["dstPathType"].isString()&&!InstallRet[i]["dstPathType"].isNull()){
  1068. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][dstPathType] is not string");
  1069. return false;
  1070. }
  1071. CSimpleStringA dstPathType = InstallRet[i]["dstPathType"].asString().c_str();
  1072. if(cmdType=="Copy"||cmdType=="Delete"||cmdType=="Run"){
  1073. if(dstPathType.IsNullOrEmpty()){
  1074. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][dstPathType] is null");
  1075. return false;
  1076. }
  1077. }
  1078. if(!InstallRet[i]["dstPath"].isString()&&!InstallRet[i]["dstPath"].isNull()){
  1079. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][dstPath] is not string");
  1080. return false;
  1081. }
  1082. CSimpleStringA dstPath = InstallRet[i]["dstPath"].asString().c_str();
  1083. if(cmdType=="Delete"||cmdType=="Run"){
  1084. if(dstPath.IsNullOrEmpty()){
  1085. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryNewContinueTask ParseInstallStep [cmd][dstPath] is null");
  1086. return false;
  1087. }
  1088. }
  1089. CUpgradeTaskFSM::CInstallStep* iStep= new CUpgradeTaskFSM::CInstallStep();
  1090. iStep->stepSeq = stepSeq;
  1091. iStep->cmdType = cmdType;
  1092. iStep->cmdFail = cmdFail;
  1093. iStep->srcPathType = srcPathType;
  1094. iStep->srcPath = srcPath;
  1095. iStep->dstPathType =dstPathType;
  1096. iStep->dstPath =dstPath;
  1097. m_fsm->m_currentTask.install_step.push(iStep);//加入队尾
  1098. }
  1099. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("install cmd size = %d",InstallRet.size());
  1100. return true;
  1101. }
  1102. };
  1103. //查询任务状态
  1104. struct QueryTaskStatus : public ITaskSp{
  1105. CUpgradeTaskFSM* m_fsm;
  1106. explicit QueryTaskStatus(CUpgradeTaskFSM* f) : m_fsm(f) {}
  1107. void Process(){
  1108. //LOG_FUNCTION();
  1109. IHttpFunc* client;
  1110. client = create_http(m_fsm->HttpsLogCallBack);
  1111. //查询新任务
  1112. bool isSendSucc=false;////查询是否成功标志
  1113. bool isValid=false;//任务是否有效
  1114. if(QueryTaskStatusImpl(client,isValid)){
  1115. isSendSucc = true;
  1116. }else{
  1117. isSendSucc = false;
  1118. }
  1119. client->Destory();
  1120. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::QueryTaskStatusEvent(isSendSucc,isValid));//返回处理结果
  1121. }
  1122. bool QueryTaskStatusImpl(IHttpFunc* client,bool &isValid){
  1123. HttpStruct::QueryCurrTaskReq qTempReq;
  1124. HttpStruct::QueryCurrTaskRet qTempRet;
  1125. CSystemStaticInfo info;
  1126. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  1127. if (rc != Error_Succeed)
  1128. {
  1129. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryTaskStatus get CSystemStaticInfo fail");
  1130. return false;
  1131. }
  1132. Json::Value rootReq;
  1133. Json::FastWriter writer;
  1134. rootReq["terminal_no"] = info.strTerminalID.GetData();
  1135. rootReq["task_id"] = m_fsm->m_currentTask.upgradeTaskId.GetData();
  1136. string jsonReq = writer.write(rootReq);
  1137. qTempReq.m_reqStr = jsonReq;//请求参数
  1138. qTempReq.m_url=m_fsm->m_centerUrl.queryTaskStatus;//访问地址
  1139. //打印发送请求
  1140. if(m_fsm->m_PrintHttpLog){
  1141. qTempReq.m_printDbg = true;
  1142. }
  1143. PROCESS_LINK_CONTEXT("LR0402506QueryTaskStatus")
  1144. if(!client->Post(qTempReq, qTempRet, &nextLink)){
  1145. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_TASK_STATUS_FAIL,CSimpleStringA::Format("QueryTaskStatus http req fail,upgradeTaskId=%s, url=%s, err=%s",m_fsm->m_currentTask.upgradeTaskId.GetData(), qTempReq.m_url.c_str(), qTempRet.m_errMsg.c_str()).GetData());
  1146. return false;//失败
  1147. }
  1148. //打印回应报文
  1149. if(m_fsm->m_PrintHttpLog){
  1150. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("qTempRet=%s",qTempRet.m_retStr.c_str());
  1151. }
  1152. Json::Reader reader;
  1153. Json::Value rootRet;
  1154. if (!reader.parse(qTempRet.m_retStr, rootRet, false))
  1155. {
  1156. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_QUERY_TASK_STATUS_FAIL,CSimpleStringA::Format("QueryTaskStatus parse resp is fail,upgradeTaskId=%s, url=%s",m_fsm->m_currentTask.upgradeTaskId.GetData(), qTempReq.m_url.c_str()).GetData());
  1157. return false;//失败
  1158. }
  1159. bool isSucc = rootRet["success"].asBool();
  1160. if(isSucc){
  1161. if(rootRet.isMember("data")){
  1162. isValid =rootRet["data"].asBool();
  1163. return true;//查询成功
  1164. }else{
  1165. return false;//查询失败
  1166. }
  1167. }else{
  1168. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("QueryTaskStatus [success] is false");
  1169. return false;//查询失败
  1170. }
  1171. }
  1172. };
  1173. //下载升级包
  1174. struct DownloadPackTask : public ITaskSp{
  1175. CUpgradeTaskFSM* m_fsm;
  1176. explicit DownloadPackTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  1177. void Process(){
  1178. LOG_FUNCTION();
  1179. //下载升级包
  1180. if(m_fsm->m_currentTask.pack_name.IsNullOrEmpty()||m_fsm->m_currentTask.pack_hash.IsNullOrEmpty()){
  1181. string errMsg = CSimpleStringA::Format("DownloadPackTask fail,pack_name or pack_hash is null,upgradeTaskId=%s",m_fsm->m_currentTask.upgradeTaskId.GetData()).GetData();
  1182. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,errMsg.c_str());
  1183. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::DownloadPackTaskFailEvent(errMsg));//下载失败,返回结果,这种情况下无法继续下载
  1184. return;
  1185. }
  1186. CSimpleStringA dPackName = m_fsm->m_currentTask.pack_name;
  1187. CSimpleStringA dPakchash = m_fsm->m_currentTask.pack_hash;
  1188. long lpacklen = m_fsm->m_currentTask.pack_length;
  1189. //判断是否已经下载完成
  1190. CSimpleStringA strPath;
  1191. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetPath("Downloads", strPath);
  1192. if(Error_Succeed != rc){
  1193. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,CSimpleStringA::Format("DownloadPackTask get Downloads path fail,upgradeTaskId=%s",m_fsm->m_currentTask.upgradeTaskId.GetData()).GetData());
  1194. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::DownloadPackTaskEvent(false));//返回失败结果
  1195. return;
  1196. }
  1197. CSimpleStringA strZipFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strPath.GetData(), dPackName.GetData());
  1198. if(m_fsm->IsPackDownloaded(strZipFile.GetData(),dPakchash.GetData(),lpacklen)){
  1199. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("download pack [%s] sm3[%s] is exist ,do not download.", strZipFile.GetData(),dPakchash.GetData());
  1200. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::DownloadPackTaskEvent(true));//下载完成,返回成功结果
  1201. return;
  1202. }else{
  1203. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("download pack [%s] sm3[%s] is not exist ,prepare download.", strZipFile.GetData(),dPakchash.GetData());
  1204. }
  1205. //提前判断是否任务取消
  1206. if(m_fsm->m_bTaskCancel){
  1207. m_fsm->PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_CancelTask));//取消下载任务,返回结果
  1208. return;
  1209. }
  1210. if(m_fsm->m_bDownloadIdle){
  1211. m_fsm->PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_Download_Idle));//下载中断,返回结果
  1212. return;
  1213. }
  1214. //接着下载未完成的升级包
  1215. IHttpFunc* client;
  1216. client = create_http(m_fsm->HttpsLogCallBack);
  1217. bool isDownloadSucc=false;//是否下载成功
  1218. CSimpleStringA errMsg ;//下载失败错误信息
  1219. int ret = DownloadPack(client,strZipFile.GetData(),dPakchash,errMsg);
  1220. if(ret==0){
  1221. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::DownloadPackTaskEvent(false));//返回通讯失败结果
  1222. }else if(ret ==1){
  1223. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::DownloadPackTaskEvent(true));//返回成功结果
  1224. }else if(ret ==2){
  1225. m_fsm->PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_Download_Idle));//下载中断,返回结果
  1226. }else if(ret ==3){
  1227. m_fsm->PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_CancelTask));//取消下载任务,返回结果
  1228. }else if(ret ==4){
  1229. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::DownloadPackTaskFailEvent(errMsg.GetData()));//下载失败,任务中止
  1230. }
  1231. client->Destory();
  1232. }
  1233. //返回值 0:下载通讯失败 1:下载成功 2:下载中断 3:任务取消 4:下载失败
  1234. int DownloadPack(IHttpFunc* client,const char* dPackPath,const char* dPackHash,CSimpleStringA &errMsg){
  1235. bool isDownloadSucc = false;
  1236. CSimpleStringA dPackPathTemp = CSimpleStringA::Format("%s%s",dPackPath,".tmp");//默认临时文件是.tmp后缀
  1237. long dPackTempLen = 0;
  1238. if(ExistsFileA(dPackPathTemp)){
  1239. if(!m_fsm->GetFileLength(dPackPathTemp.GetData(),dPackTempLen)){
  1240. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,CSimpleStringA::Format("%s get download temp pack fileLen is fail", dPackPathTemp.GetData()).GetData());
  1241. errMsg = "get download temp pack fileLen is fail";
  1242. return 4;//下载失败,计算临时文件长度失败
  1243. }
  1244. }
  1245. byte* content = new byte[m_fsm->m_newEachDownloadLen];
  1246. memset(content, 0, m_fsm->m_newEachDownloadLen);
  1247. bool isContinue = true;
  1248. while(isContinue){
  1249. if(m_fsm->m_bTaskCancel){
  1250. //任务取消
  1251. delete[] content;
  1252. return 3;
  1253. }
  1254. if(m_fsm->m_bDownloadIdle){
  1255. //状态中断
  1256. delete[] content;
  1257. return 2;
  1258. }
  1259. HttpStruct::DownloadPackTaskRet resp;
  1260. resp.bfinish= false;//默认是未下载完
  1261. resp.sm3 = "";//默认是空串
  1262. if(DownloadPackTaskImpl(client,dPackTempLen,content,resp)){
  1263. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("download succ, %d , begin write %d",(int)resp.bfinish,dPackTempLen);
  1264. //成功通讯
  1265. if(!m_fsm->DownloadFileWrite(content,resp.content_length,dPackPathTemp)){
  1266. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,CSimpleStringA::Format("DownloadFileWrite write temp file fail,begin write=%d, write len=%d", dPackTempLen,resp.content_length).GetData());
  1267. errMsg = "DownloadFileWrite write temp file fail";
  1268. delete[] content;
  1269. return 4;//下载失败,写文件失败
  1270. }else{
  1271. m_fsm->DownloadProgress(dPackTempLen+resp.content_length,resp.origin_file_length);//广播下载进度
  1272. }
  1273. if(resp.bfinish){
  1274. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("最后一次下载");
  1275. //最后一次
  1276. CSimpleStringA strMD5Val;
  1277. ErrorCodeEnum rc = m_fsm->SM3FileToStr(dPackPathTemp, strMD5Val,false);
  1278. if (Error_Succeed != rc)
  1279. {
  1280. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,CSimpleStringA::Format("%s get download temp pack sm3 fail ,error:%d", dPackPathTemp.GetData(), (int)rc).GetData());
  1281. errMsg = "get download temp pack sm3 fail";
  1282. delete[] content;
  1283. return 4;//下载失败,算hash失败
  1284. }
  1285. //不区分大小写比较文件hash
  1286. #ifdef RVC_OS_WIN
  1287. if(stricmp(strMD5Val.GetData(),resp.sm3.c_str())==0){
  1288. #else
  1289. if(strcmp(strMD5Val.GetData(),resp.sm3.c_str())==0){
  1290. #endif
  1291. //移动文件到正式目录
  1292. if(ExistsFileA(dPackPath)){
  1293. if(remove(dPackPath)==0){
  1294. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete download old pack succ ", dPackPath);
  1295. if(rename(dPackPathTemp.GetData(),dPackPath)!=0){
  1296. //TODO: CrossPlaform [Gifur@2025730]
  1297. #ifdef RVC_OS_WIN
  1298. errMsg = CSimpleStringA::Format("exist old file,remove temp file fail, error=%d",(int)GetLastError()).GetData();
  1299. #else
  1300. errMsg = CSimpleStringA::Format("exist old file,remove temp file fail, error=%d", errno).GetData();
  1301. #endif
  1302. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,errMsg.GetData());
  1303. delete[] content;
  1304. return 4;//下载失败,移动文件失败
  1305. }else{
  1306. delete[] content;
  1307. return 1;//成功返回
  1308. }
  1309. }else{
  1310. //TODO: CrossPlaform [Gifur@2025730]
  1311. #ifdef RVC_OS_WIN
  1312. errMsg = CSimpleStringA::Format("delete download old pack fail ,error:%d",(int)GetLastError()).GetData();
  1313. #else
  1314. errMsg = CSimpleStringA::Format("delete download old pack fail ,error:%d", errno).GetData();
  1315. #endif
  1316. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,errMsg.GetData());
  1317. delete[] content;
  1318. return 4;//下载失败,删除旧文件失败
  1319. }
  1320. }else{
  1321. if(rename(dPackPathTemp.GetData(),dPackPath)!=0){
  1322. //TODO: CrossPlaform [Gifur@2025730]
  1323. #ifdef RVC_OS_WIN
  1324. errMsg = CSimpleStringA::Format("not exist old file,remove temp file fail, error=%d",(int)GetLastError()).GetData();
  1325. #else
  1326. errMsg = CSimpleStringA::Format("not exist old file,remove temp file fail, error=%d", errno).GetData();
  1327. #endif
  1328. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,errMsg.GetData());
  1329. delete[] content;
  1330. return 4;//移动文件失败
  1331. }else{
  1332. delete[] content;
  1333. return 1;//成功返回
  1334. }
  1335. }
  1336. }else{
  1337. errMsg = CSimpleStringA::Format("download file sm3 is not equal,local file [%s],resp [%s]",strMD5Val.GetData(),resp.sm3.c_str()).GetData();
  1338. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,CSimpleStringA::Format("download file sm3 is not equal,local file [%s],resp [%s]",strMD5Val.GetData(),resp.sm3.c_str()).GetData());
  1339. //删除临时文件,重新下载
  1340. if(remove(dPackPathTemp.GetData())==0){
  1341. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete download temp pack succ ,%s", dPackPathTemp.GetData());
  1342. }else{
  1343. //TODO: CrossPlaform [Gifur@2025730]
  1344. #ifdef RVC_OS_WIN
  1345. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete download temp pack fail ,%s error:%d", dPackPathTemp.GetData(), GetLastError());
  1346. #else
  1347. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete download temp pack fail ,%s error:%d", dPackPathTemp.GetData(), errno);
  1348. #endif
  1349. }
  1350. delete[] content;
  1351. return 4;//下载失败,hash不对
  1352. }
  1353. }else{
  1354. if(resp.content_length==0){
  1355. errMsg = "DownloadFile server content_length = 0 ,bfinish = false ,DownloadFile server return param fail";
  1356. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,"DownloadFile server content_length = 0 ,bfinish = false ,DownloadFile server return param fail");
  1357. delete[] content;
  1358. return 4;//传回零字节,但是又未结束,下载失败,等待一段时间下载
  1359. }
  1360. //继续下一次续传
  1361. dPackTempLen =dPackTempLen+resp.content_length;
  1362. memset(content, 0, m_fsm->m_newEachDownloadLen);
  1363. isContinue = true;
  1364. }
  1365. }else{
  1366. delete[] content;
  1367. return 0;//下载通讯失败,等待一段时间下载
  1368. }
  1369. }
  1370. }
  1371. bool DownloadPackTaskImpl(IHttpFunc* client,long downloaded_len,byte* respContent,HttpStruct::DownloadPackTaskRet &resp){
  1372. //注意传入参数和传出参数进行转码
  1373. CSystemStaticInfo info;
  1374. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->GetSystemStaticInfo(info);
  1375. if (rc != Error_Succeed)
  1376. {
  1377. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DownloadPackTask get CSystemStaticInfo fail");
  1378. return false;//失败
  1379. }
  1380. Json::Value rootReq;
  1381. Json::FastWriter writer;
  1382. rootReq["terminal_no"] = info.strTerminalID.GetData();
  1383. rootReq["pack_name"] = m_fsm->m_currentTask.pack_name.GetData();
  1384. rootReq["downloaded_length"] = downloaded_len;
  1385. rootReq["limit_length"] = m_fsm->m_newEachDownloadLen;
  1386. rootReq["file_path"] = m_fsm->m_currentTask.file_path.GetData();
  1387. string jsonReq = writer.write(rootReq);
  1388. CSimpleStringA downloadUrl = m_fsm->m_currentTask.download_url;
  1389. #if defined(RVC_OS_WIN) /** TODO 跟文涛确认UOS下是否需要调用 [Gifur@2025725]*/
  1390. //转码
  1391. SP::Module::Util::ConvertGBK2Utf8(jsonReq);
  1392. #endif //RVC_OS_WIN
  1393. //打印发送请求
  1394. if(m_fsm->m_PrintHttpLog){
  1395. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("jsonStr is %s",jsonReq.c_str());
  1396. }
  1397. long httpCode =0;
  1398. unordered_map<string, string> responseHeaders;
  1399. int time_out = DOWNLOAD_TIMEOUT;
  1400. fileContentArray content;
  1401. content.copyPtr = respContent;
  1402. content.maxLen = m_fsm->m_newEachDownloadLen;
  1403. int retlen = 0;
  1404. content.curLen = &retlen;
  1405. PROCESS_LINK_CONTEXT("LR0402506DownloadFile")
  1406. if(!client->DownloadFileBlock(downloadUrl.GetData(),jsonReq.c_str(), content,httpCode,responseHeaders,time_out, &nextLink)){
  1407. if (retlen > m_fsm->m_newEachDownloadLen) {
  1408. CSimpleStringA errMsg = CSimpleStringA::Format("retlen is over limit_length ,retlen= %d", retlen).GetData();
  1409. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL, CSimpleStringA::Format("DownloadPackTask http req fail, %s, url=%s", errMsg.GetData(), downloadUrl.GetData()).GetData());
  1410. }
  1411. else {
  1412. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL, CSimpleStringA::Format("DownloadPackTask http req fail,url=%s", downloadUrl.GetData()).GetData());
  1413. }
  1414. return false;//失败
  1415. }
  1416. //判断httpCode
  1417. if(httpCode!=200){
  1418. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,CSimpleStringA::Format("DownloadPackTask http req fail, url=%s, retHttpCode=%d",downloadUrl.GetData(),httpCode).GetData());
  1419. return false;//失败
  1420. }
  1421. //解析head值
  1422. unordered_map<string, string>::const_iterator iter = responseHeaders.find("Result");
  1423. if(iter==responseHeaders.end()){
  1424. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,"resp head result is null");
  1425. return false;//无params 失败
  1426. }else{
  1427. string respStr = iter->second;
  1428. #if defined(RVC_OS_WIN)
  1429. //转码:
  1430. SP::Module::Util::ConvertUtf82GBK(respStr);
  1431. #endif //RVC_OS_WIN
  1432. //打印回应报文
  1433. if(m_fsm->m_PrintHttpLog){
  1434. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("qTempRet Result=%s",respStr.c_str());
  1435. }
  1436. //解析json值
  1437. Json::Reader reader;
  1438. Json::Value rootRet;
  1439. if (!reader.parse(respStr, rootRet, false))
  1440. {
  1441. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,"DownloadPackTask head params parse resp is fail");
  1442. return false;//解析失败
  1443. }
  1444. bool isSucc = rootRet["success"].asBool();
  1445. if(isSucc){
  1446. resp.content_length = (long)rootRet["content_length"].asInt();
  1447. resp.origin_file_length = (long)rootRet["origin_file_length"].asInt();
  1448. resp.bfinish = rootRet["finish"].asBool();//是否传送完毕
  1449. resp.sm3 = rootRet["sm3"].asString();//包hash码
  1450. int retlen = *(content.curLen);
  1451. if (resp.content_length != retlen) {
  1452. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DownloadPackTask download len not equal, retlen = %d,content_length=%d", retlen, resp.content_length);
  1453. }
  1454. return true;//查询成功
  1455. }else{
  1456. CSimpleStringA msg = rootRet["message"].asString().c_str();
  1457. if(msg.IsNullOrEmpty()){
  1458. msg = "";
  1459. }
  1460. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_DOWNLOAD_PACK_FAIL,CSimpleStringA::Format("DownloadPackTask [success] is false, err=%s",msg.GetData()).GetData());
  1461. return false;//查询失败
  1462. }
  1463. }
  1464. }
  1465. };
  1466. //停止播放广告和提示音
  1467. struct StopMediaPlayTask : public ITaskSp{
  1468. CUpgradeTaskFSM* m_fsm;
  1469. explicit StopMediaPlayTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  1470. void Process(){
  1471. LOG_FUNCTION();
  1472. //判断是否需要停止广播和提示音
  1473. if(!m_fsm->m_currentTask.stop_media_play){
  1474. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::StopMediaPlayTaskEvent(true));//返回处理结果
  1475. }
  1476. else if(m_fsm->isTerminalInstall()){
  1477. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::StopMediaPlayTaskEvent(true));//返回处理结果
  1478. }
  1479. else {
  1480. //停止广告、提示音播放
  1481. // 终端置为升级状态。1:升级状态,0:非升级状态。处于升级状态时调用播放接口不生效
  1482. //ErrorCodeEnum rc = ((CUpgradeMgrEntity*)m_fsm->GetEntityBase())->NewStopMediaPlay();
  1483. //if(rc==Error_Succeed){
  1484. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("StopMediaPlayTask stop media play succ");
  1485. //}else{
  1486. // DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setResultCode("RTA5674")("StopMediaPlayTask stop media play fail");
  1487. // //m_fsm->PostEventFIFO(new CUpgradeTaskFSM::StopMediaPlayTaskEvent(false));//返回处理结果
  1488. //}
  1489. // 终端置为升级状态。1:升级状态,0:非升级状态。处于升级状态时调用播放接口不生效
  1490. ErrorCodeEnum rc = m_fsm->GetEntityBase()->GetFunction()->SetSysVar("UpdateState", "1", false);
  1491. if(rc==Error_Succeed){
  1492. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("StopMediaPlayTask set UpdateState to 1 succ");
  1493. }else{
  1494. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__).setResultCode("RTA5674")("StopMediaPlayTask set UpdateState fail");
  1495. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::StopMediaPlayTaskEvent(false));//返回处理结果
  1496. return;
  1497. }
  1498. //停止广告、提示音播放
  1499. rc = ((CUpgradeMgrEntity*)m_fsm->GetEntityBase())->NewStopMediaPlay();
  1500. if (rc == Error_Succeed) {
  1501. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("StopMediaPlayTask stop media play succ");
  1502. }
  1503. else {
  1504. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("StopMediaPlayTask stop media play fail");
  1505. //m_fsm->PostEventFIFO(new CUpgradeTaskFSM::StopMediaPlayTaskEvent(false));//返回处理结果
  1506. }
  1507. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::StopMediaPlayTaskEvent(true));//返回处理结果
  1508. }
  1509. }
  1510. };
  1511. //解压安装包
  1512. struct UnzipPackTask : public ITaskSp{
  1513. CUpgradeTaskFSM* m_fsm;
  1514. explicit UnzipPackTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  1515. void Process(){
  1516. LOG_FUNCTION();
  1517. //解压安装包
  1518. bool isUnzipSucc;//是否解压成功
  1519. if(m_fsm->UnzipPack(m_fsm->m_currentTask.pack_name.GetData())==Error_Succeed){
  1520. isUnzipSucc = true;
  1521. }else{
  1522. isUnzipSucc = false;
  1523. }
  1524. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::UnzipPackTaskEvent(isUnzipSucc));//返回处理结果
  1525. }
  1526. };
  1527. //升级包安装
  1528. struct InstallPackTask : public ITaskSp{
  1529. CUpgradeTaskFSM* m_fsm;
  1530. explicit InstallPackTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  1531. void Process(){
  1532. LOG_FUNCTION();
  1533. //升级包安装
  1534. bool isInstallSucc=false;//是否安装成功
  1535. int failStatus=0;//安装失败状态:0:失败,1:成功,-1:未知
  1536. string packName = m_fsm->m_currentTask.pack_name.GetData();
  1537. CSimpleStringA errMsg ="";
  1538. if (!m_fsm->m_TempDepUpgradeFlag) {
  1539. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("======begin ExecInstall======");
  1540. failStatus = m_fsm->ExecInstall(errMsg);
  1541. }
  1542. else {
  1543. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("======begin dep ExecInstall======");
  1544. failStatus = m_fsm->ExecDepInstall(errMsg);
  1545. }
  1546. if(failStatus == 1){
  1547. //安装成功,执行防火墙设置
  1548. if (!m_fsm->m_stopSetFirewall) {
  1549. m_fsm->SetNewVersionPathToFirewall();
  1550. }
  1551. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("======end ExecInstall:succ======");
  1552. isInstallSucc =true;
  1553. }else{
  1554. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("======end ExecInstall:fail======");
  1555. isInstallSucc =false;
  1556. }
  1557. //清理临时解压包
  1558. if(Error_Succeed==m_fsm->DeleteUnzipDir()){
  1559. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DeleteUnzipDir succ");
  1560. }else{
  1561. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DeleteUnzipDir fail");
  1562. }
  1563. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("安装完成:failStatus=%d",failStatus);
  1564. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::InstallPackTaskEvent(isInstallSucc,errMsg,failStatus));//返回处理结果
  1565. }
  1566. };
  1567. //回退版本检查
  1568. struct RollBackCheckTask : public ITaskSp {
  1569. CUpgradeTaskFSM* m_fsm;
  1570. explicit RollBackCheckTask(CUpgradeTaskFSM* f) : m_fsm(f) {}
  1571. void Process() {
  1572. LOG_FUNCTION();
  1573. //回退版本检查
  1574. string failReason = "";
  1575. CSimpleStringA strErrMsg = "";
  1576. //判定是适配器or应用回退
  1577. CSimpleStringA verDirName = m_fsm->m_currentTask.pack_version.GetData();
  1578. if(m_fsm->judgeRollbackTaskClass(verDirName,failReason,strErrMsg)==Error_Succeed)
  1579. {
  1580. if (!m_fsm->m_TempDepUpgradeFlag) {
  1581. //升级回退
  1582. if (m_fsm->RollBackVersionCheck(failReason, strErrMsg) == Error_Succeed) {
  1583. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::RollBackCheckTaskEvent(true, "", ""));
  1584. }
  1585. else {
  1586. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::RollBackCheckTaskEvent(false, strErrMsg, failReason));
  1587. }
  1588. }
  1589. else {
  1590. //适配器回退
  1591. if (m_fsm->RollBackDepVersionCheck(failReason, strErrMsg) == Error_Succeed) {
  1592. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::RollBackCheckTaskEvent(true, "", ""));
  1593. }
  1594. else {
  1595. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::RollBackCheckTaskEvent(false, strErrMsg, failReason));
  1596. }
  1597. }
  1598. }else{
  1599. m_fsm->PostEventFIFO(new CUpgradeTaskFSM::RollBackCheckTaskEvent(false, strErrMsg, failReason));
  1600. }
  1601. }
  1602. };
  1603. }
  1604. //完成
  1605. // 创建新版本目录,拷贝当前版本bin、cfg、dep、mod、imdep、res
  1606. ErrorCodeEnum CUpgradeTaskFSM::CreateNewVersion(CVersion NewSoftwareVersion,CSimpleStringA &strErrInfo)
  1607. {
  1608. // 创建安装包日志文件
  1609. CSimpleStringA rootVerPath;
  1610. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("RootVer",rootVerPath);//获取version根路径
  1611. if(rc!=Error_Succeed){
  1612. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new version fail , get RootVer path is fail");
  1613. strErrInfo = CSimpleStringA::Format("Create new version fail , get RootVer path is fail");
  1614. return Error_Bug;
  1615. }
  1616. CSimpleStringA strCurVerPath;
  1617. rc = m_pEntity->GetFunction()->GetPath("Base",strCurVerPath);//获取当前version路径
  1618. if(rc!=Error_Succeed){
  1619. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new version fail , get Base path is fail");
  1620. strErrInfo = CSimpleStringA::Format("Create new version fail , get Base path is fail");
  1621. return Error_Bug;
  1622. }
  1623. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), NewSoftwareVersion.ToString().GetData());
  1624. //auto &v = cfg->install_ini->install_version;//获取当前版本
  1625. //CVersion v;//获取当前版本
  1626. CVersion curVersion(m_currentVersion.GetMajor(), m_currentVersion.GetMinor(), m_currentVersion.GetRevision(), m_currentVersion.GetBuild());
  1627. if (curVersion == NewSoftwareVersion)
  1628. {
  1629. LogWarn(Severity_Low, Error_Bug, ERR_TASK_INSTALL_VERSION_FAIL, CSimpleStringA::Format("new ver [%s] equals current ver [%s]", NewSoftwareVersion.ToString().GetData(), curVersion.ToString().GetData()).GetData());
  1630. strErrInfo = CSimpleStringA::Format("new ver [%s] equals current ver [%s]", NewSoftwareVersion.ToString().GetData(), curVersion.ToString().GetData());
  1631. return Error_Bug;
  1632. }
  1633. if (ExistsDirA(strNewVerPath.GetData()))
  1634. {
  1635. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("new version dir [%s] already exist, delete it", strNewVerPath.GetData());
  1636. if(!RemoveDirRecursiveA(strNewVerPath.GetData())){
  1637. //TODO: CrossPlaform [Gifur@2025730]
  1638. #ifdef RVC_OS_WIN
  1639. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new version fail, delete already exist new version is fail:%d",GetLastError());
  1640. strErrInfo = CSimpleStringA::Format("Create new version fail, delete already exist new version is fail:%d",(int)GetLastError());
  1641. #else
  1642. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new version fail, delete already exist new version is fail:%d", errno);
  1643. strErrInfo = CSimpleStringA::Format("Create new version fail, delete already exist new version is fail:%d", errno);
  1644. #endif
  1645. return Error_Bug;
  1646. }
  1647. }
  1648. if (!CreateDirA(strNewVerPath.GetData(), TRUE))
  1649. {
  1650. //TODO: CrossPlaform [Gifur@2025730]
  1651. #ifdef RVC_OS_WIN
  1652. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new version dir fail: %d", GetLastError());
  1653. strErrInfo = CSimpleStringA::Format("create new version dir fail: %d", (int)GetLastError());
  1654. #else
  1655. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new version dir fail: %d", errno);
  1656. strErrInfo = CSimpleStringA::Format("create new version dir fail: %d", errno);
  1657. #endif
  1658. return Error_Unexpect;
  1659. }
  1660. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create new version dir succ, dir = %s",strNewVerPath.GetData());
  1661. //增加错误信息字段,回传给上层应用
  1662. CSimpleStringA strErrMsg ="";
  1663. CSimpleStringA strSource = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "bin", strCurVerPath.GetData());
  1664. CSimpleStringA strDest = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "bin", strNewVerPath.GetData());
  1665. if (!RecursiveCopyDir(strSource, strDest,strErrMsg))
  1666. {
  1667. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy bin dir fail");
  1668. strErrInfo = CSimpleStringA::Format("copy bin dir fail: %s", strErrMsg.GetData());
  1669. return Error_Unexpect;
  1670. }
  1671. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy bin dir succ");
  1672. strSource = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "cfg", strCurVerPath.GetData());
  1673. strDest = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "cfg", strNewVerPath.GetData());
  1674. if (!RecursiveCopyDir(strSource, strDest,strErrMsg))
  1675. {
  1676. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy cfg dir fail");
  1677. strErrInfo = CSimpleStringA::Format("copy cfg dir fail: %s", strErrMsg.GetData());
  1678. return Error_Unexpect;
  1679. }
  1680. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy cfg dir succ");
  1681. //strSource = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "dep", strCurVerPath.GetData());
  1682. //strDest = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "dep", strNewVerPath.GetData());
  1683. //if (!RecursiveCopyDir(strSource, strDest,strErrMsg))
  1684. //{
  1685. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir fail");
  1686. // strErrInfo = CSimpleStringA::Format("copy dep dir fail: %s", strErrMsg.GetData());
  1687. // return Error_Unexpect;
  1688. //}
  1689. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir succ");
  1690. strSource = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "mod", strCurVerPath.GetData());
  1691. strDest = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "mod", strNewVerPath.GetData());
  1692. if (!RecursiveCopyDir(strSource, strDest,strErrMsg))
  1693. {
  1694. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy mod dir fail");
  1695. strErrInfo = CSimpleStringA::Format("copy mod dir fail: %s", strErrMsg.GetData());
  1696. return Error_Unexpect;
  1697. }
  1698. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy mod dir succ");
  1699. strSource = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "dep", strCurVerPath.GetData());
  1700. strDest = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "dep", strNewVerPath.GetData());
  1701. // 检测dep目录是否存在
  1702. if (ExistsDirA(strSource))
  1703. {
  1704. if (!RecursiveCopyDir(strSource, strDest, strErrMsg))
  1705. {
  1706. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir fail");
  1707. strErrInfo = CSimpleStringA::Format("copy dep dir fail: %s", strErrMsg.GetData());
  1708. return Error_Unexpect;
  1709. }
  1710. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir succ");
  1711. }
  1712. else {
  1713. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("dep dir is not exist,ignore copy");
  1714. }
  1715. strSource = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "res", strCurVerPath.GetData());
  1716. strDest = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "res", strNewVerPath.GetData());
  1717. // 检测res目录是否存在
  1718. if (ExistsDirA(strSource))
  1719. {
  1720. if (!RecursiveCopyDir(strSource, strDest,strErrMsg))
  1721. {
  1722. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy res dir fail");
  1723. strErrInfo = CSimpleStringA::Format("copy res dir fail: %s", strErrMsg.GetData());
  1724. return Error_Unexpect;
  1725. }
  1726. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy res dir succ");
  1727. }
  1728. else
  1729. {
  1730. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("res dir is not exist,ignore copy");
  1731. }
  1732. strSource = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "imdep", strCurVerPath.GetData());
  1733. strDest = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "imdep", strNewVerPath.GetData());
  1734. // 检测imdep目录是否存在
  1735. if (ExistsDirA(strSource))
  1736. {
  1737. if (!RecursiveCopyDir(strSource, strDest,strErrMsg))
  1738. {
  1739. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy imdep dir fail");
  1740. strErrInfo = CSimpleStringA::Format("copy imdep dir fail: %s", strErrMsg.GetData());
  1741. return Error_Unexpect;
  1742. }
  1743. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy imdep dir succ");
  1744. }
  1745. else
  1746. {
  1747. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("imdep dir is not exist,ignore copy");
  1748. }
  1749. return Error_Succeed;
  1750. }
  1751. ErrorCodeEnum CUpgradeTaskFSM::ClearUpgradeFailVersion(CVersion NewSoftwareVersion)
  1752. {
  1753. CSimpleStringA rootVerPath;
  1754. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("RootVer",rootVerPath);//获取version根路径
  1755. if(rc!=Error_Succeed){
  1756. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailVersion fail , get RootVer path is fail");
  1757. return Error_Bug;
  1758. }
  1759. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), NewSoftwareVersion.ToString().GetData());
  1760. if (ExistsDirA(strNewVerPath.GetData()))
  1761. {
  1762. if(!RemoveDirRecursiveA(strNewVerPath.GetData())){
  1763. //TODO: CrossPlaform [Gifur@2025730]
  1764. #ifdef RVC_OS_WIN
  1765. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailVersion fail, delete temp Version [%s] is fail:%d",strNewVerPath.GetData(),(int)GetLastError());
  1766. #else
  1767. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailVersion fail, delete temp Version [%s] is fail:%d", strNewVerPath.GetData(), errno);
  1768. #endif
  1769. return Error_Bug;
  1770. }else{
  1771. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailVersion succ, delete temp Version [%s] ", strNewVerPath.GetData());
  1772. return Error_Succeed;
  1773. }
  1774. }else{
  1775. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailVersion succ, delete temp Version [%s] is not exsit ", strNewVerPath.GetData());
  1776. return Error_Succeed;
  1777. }
  1778. }
  1779. // result.log 文件内容: result=0&msg=install ok
  1780. bool CUpgradeTaskFSM::GetSysUpgradeResult(const char *pszResultLog, CSimpleStringA &eErrorCode, CSimpleStringA &strErrMsg)
  1781. {
  1782. // 设置初始值
  1783. //eErrorCode = Error_Succeed;
  1784. strErrMsg = "";
  1785. if (!ExistsFileA(pszResultLog))
  1786. {
  1787. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetSysUpgradeResult result log [%s] not exist", pszResultLog);
  1788. strErrMsg = "result.log not exist";
  1789. eErrorCode = "0";
  1790. return true;//???有些外部程序不一定会产生日志文件,故默认不产生文件,认为是成功
  1791. }
  1792. FILE *pResultLog = fopen(pszResultLog, "r");
  1793. if (pResultLog == NULL)
  1794. {
  1795. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetSysUpgradeResult open result log [%s] fail", pszResultLog);
  1796. strErrMsg = "open result.log fail";
  1797. return false;
  1798. }
  1799. char szTmp[4096] = {};
  1800. int nRead = fread_s(szTmp, sizeof(szTmp), 1, sizeof(szTmp), pResultLog);
  1801. CSimpleStringA strResult = szTmp;
  1802. fclose(pResultLog);
  1803. pResultLog = NULL;
  1804. auto arr = strResult.Split('&');
  1805. if(arr.GetCount()!=2){
  1806. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetSysUpgradeResult read result log format fail,[%s]", strResult.GetData());
  1807. strErrMsg = "read result.log format fail,eg result=0&msg=install ok";
  1808. return false;
  1809. }
  1810. for (int i = 0; i < arr.GetCount(); i++)
  1811. {
  1812. auto arr2 = arr[i].Split('=');
  1813. if (arr2.GetCount() == 2)
  1814. {
  1815. //TODO: CrossPlaform [Gifur@2025730]
  1816. #ifdef RVC_OS_WIN
  1817. if (stricmp(arr2[0], "result") == 0)
  1818. eErrorCode = arr2[1];
  1819. else if (stricmp(arr2[0], "msg") == 0)
  1820. strErrMsg = arr2[1];
  1821. #else
  1822. if (strcmp(arr2[0], "result") == 0)
  1823. eErrorCode = arr2[1];
  1824. else if (strcmp(arr2[0], "msg") == 0)
  1825. strErrMsg = arr2[1];
  1826. #endif
  1827. }
  1828. }
  1829. return true;
  1830. }
  1831. int CUpgradeTaskFSM::ExecInstall(CSimpleStringA &strErrMsg)
  1832. {
  1833. //判断是否需要生成新版本
  1834. if(!m_currentTask.pack_version.IsNullOrEmpty()){
  1835. CSimpleStringA strUpgradeVersion = m_currentTask.pack_version;
  1836. CVersion latterVersion;//要更新到的版本号
  1837. CVersion curVer = m_currentVersion;//当前版本号
  1838. // 指定版本
  1839. DWORD dwMajor(0), dwMinor(0), dwRevision(0), dwBuild(0);
  1840. int n = sscanf(strUpgradeVersion.GetData(), "%d.%d.%d.%d", &dwMajor, &dwMinor, &dwRevision, &dwBuild);
  1841. if (n != 4)
  1842. {
  1843. strErrMsg = CSimpleStringA::Format("指定升级版本号无效(%s)", strUpgradeVersion.GetData());
  1844. LogWarn(Severity_Low, Error_Unexpect, ERR_TASK_INSTALL_VERSION_FAIL, CSimpleStringA::Format("specified software version is invalid: [%s]", strUpgradeVersion.GetData()).GetData());
  1845. return 0;
  1846. }else{
  1847. latterVersion = CVersion(dwMajor, dwMinor, dwRevision, dwBuild);
  1848. }
  1849. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("latterVersion :%s",latterVersion.ToString().GetData());
  1850. if(curVer.ToString()==strUpgradeVersion){
  1851. //版本号相同,
  1852. strErrMsg = CSimpleStringA::Format("指定升级版本号与当前版本相同,无效(配置:%s, 当前:%s)", strUpgradeVersion.GetData(),curVer.ToString().GetData());
  1853. return 0;
  1854. //}else if(m_currentVersion.ToString().Compare(strUpgradeVersion)==1){
  1855. //字符串比较版本号大小改为整形数字对比
  1856. }else if(dwMajor < curVer.GetMajor()
  1857. || (dwMajor == curVer.GetMajor() && dwMinor < curVer.GetMinor())
  1858. || (dwMajor == curVer.GetMajor() && dwMinor == curVer.GetMinor() && dwRevision < curVer.GetRevision())
  1859. || (dwMajor == curVer.GetMajor() && dwMinor == curVer.GetMinor() && dwRevision == curVer.GetRevision() && dwBuild < curVer.GetBuild())){
  1860. //升级版本小于当前版本
  1861. strErrMsg = CSimpleStringA::Format("指定升级版本号无效(配置:%s,解析:%s,当前:%s)", strUpgradeVersion.GetData(),latterVersion.ToString().GetData(),curVer.ToString().GetData());
  1862. return 0;
  1863. }
  1864. //判断是否需要创建新版本
  1865. CSimpleStringA strErrInfo = "";
  1866. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin CreateNewVersion,version[%s],pack[%s]",strUpgradeVersion.GetData(),m_currentTask.pack_name.GetData());
  1867. ErrorCodeEnum rc = CreateNewVersion(latterVersion,strErrInfo);
  1868. if (rc != Error_Succeed)
  1869. {
  1870. strErrMsg = CSimpleStringA::Format("CreateNewVersion fail:%s",strErrInfo.GetData());
  1871. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateNewVersion,version[%s] fail:%s",strUpgradeVersion.GetData(),strErrInfo.GetData());
  1872. //清理临时版本文件夹
  1873. ClearUpgradeFailVersion(latterVersion);
  1874. return 0;
  1875. }else{
  1876. m_currentTask.NewVersion = strUpgradeVersion;//赋值升级版本号给内存变量
  1877. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateNewVersion,version[%s] succ , pack[%s]",strUpgradeVersion.GetData(), m_currentTask.pack_name.GetData());
  1878. }
  1879. }
  1880. //循环执行安装命令
  1881. int iRet=1;//默认是成功
  1882. if(!m_currentTask.install_step.empty()){
  1883. int iCount = m_currentTask.install_step.size();
  1884. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("install cmd size = %d",iCount);
  1885. for (int j = 0; j < iCount; j++){
  1886. CInstallStep* tmp = m_currentTask.install_step.front();
  1887. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("=====begin cmd=[%s]=====",tmp->cmdType.GetData());
  1888. //开始处理具体命令
  1889. if(tmp->cmdType=="Delete"){
  1890. bool ret = ExecDeleteCmd(strErrMsg,tmp);
  1891. if(!ret){
  1892. //报错才判断
  1893. if(tmp->cmdFail == 0 ){
  1894. iRet = 0;
  1895. break;//出错直接退出
  1896. }else if(tmp->cmdFail == 1 ){
  1897. //继续
  1898. }else{
  1899. iRet = 0;
  1900. break;//其他值也退出
  1901. }
  1902. }
  1903. }else if(tmp->cmdType=="Copy"){
  1904. bool ret = ExecCpoyCmd(strErrMsg,tmp);
  1905. if(!ret){
  1906. //报错才判断
  1907. if(tmp->cmdFail == 0){
  1908. iRet = 0;
  1909. break;//出错直接退出
  1910. }else if(tmp->cmdFail == 1){
  1911. //继续
  1912. }else{
  1913. iRet = 0;
  1914. break;//其他值也退出
  1915. }
  1916. }
  1917. }else if(tmp->cmdType=="Run"){
  1918. int retInt = ExecRunCmd(strErrMsg,tmp);
  1919. if(retInt==0||retInt==-1){
  1920. //报错才判断
  1921. if(tmp->cmdFail == 0 ){
  1922. iRet = retInt;//会有0,-1值
  1923. break;//出错直接退出,并返回体系外结果
  1924. }else if(tmp->cmdFail == 1 ){
  1925. //继续
  1926. }else{
  1927. iRet = 0;
  1928. break;//其他值也退出
  1929. }
  1930. }
  1931. }else{
  1932. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cmd=[%s] is invalid",tmp->cmdType.GetData());
  1933. iRet = 0;
  1934. break;//命令无效直接退出
  1935. }
  1936. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("=====end cmd=[%s]=====",tmp->cmdType.GetData());
  1937. m_currentTask.install_step.pop();//去除元素
  1938. delete tmp;//删除内存
  1939. }
  1940. }
  1941. return iRet;//返回处理结果
  1942. }
  1943. int CUpgradeTaskFSM::ExecRunCmd(CSimpleStringA &strErrMsg,CInstallStep* ins)
  1944. {
  1945. CSimpleStringA destPath ;//目的路径
  1946. if(ins->dstPathType=="0"){
  1947. CSimpleStringA rootPath;
  1948. if(GetRootTypePath(1,rootPath)){
  1949. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//相对路径
  1950. }else{
  1951. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 1 is fail");
  1952. return false;
  1953. }
  1954. }else if(ins->dstPathType=="1"){
  1955. destPath = ins->dstPath;//绝对路径
  1956. }else if(ins->dstPathType=="2"){
  1957. CSimpleStringA rootPath;
  1958. if(GetRootTypePath(2,rootPath)){
  1959. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//当前版本相对路径
  1960. }else{
  1961. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 2 is fail");
  1962. return false;
  1963. }
  1964. }else if(ins->dstPathType=="3"){
  1965. CSimpleStringA rootPath;
  1966. if(GetRootTypePath(3,rootPath)){
  1967. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//升级版本相对路径
  1968. }else{
  1969. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 3 is fail");
  1970. return false;
  1971. }
  1972. }else if(ins->dstPathType=="4"){
  1973. CSimpleStringA rootPath;
  1974. if(GetRootTypePath(4,rootPath)){
  1975. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//升级包相对路径
  1976. }else{
  1977. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 4 is fail");
  1978. return false;
  1979. }
  1980. }else{
  1981. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd destPathType is invalid %s",ins->dstPathType.GetData());
  1982. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPathType is invalid %s",ins->dstPathType.GetData());
  1983. return false;//不支持的路径类型
  1984. }
  1985. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd destPath is %s",destPath.GetData());
  1986. // ToRun=install.bat
  1987. if (ExistsFileA(destPath.GetData()))
  1988. {
  1989. ostuile_void_ptr store = osutil_sure_redirect_32sys_in_wow64();
  1990. string sysPathStr = destPath.GetData();
  1991. CSimpleStringA err ="";
  1992. int ret = system_on(sysPathStr,true,err);
  1993. osutil_reset_redirect_32sys_in_wow64(&store);
  1994. if(ret!=0){
  1995. //TODO: CrossPlaform [Gifur@2025730]
  1996. //程序执行异常,结果未知
  1997. #ifdef RVC_OS_WIN
  1998. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, system_on exec unknown ,err=%s",err.GetData());
  1999. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail, system_on exec unknown ,err=%s",err.GetData());
  2000. #else
  2001. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, system_on exec unknown , ret=%d, err=%s",ret, strerror(errno));
  2002. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail, system_on exec unknown ret=%d, err=%s", ret, strerror(errno));
  2003. #endif
  2004. return -1;//未知结果返回
  2005. }else{
  2006. //进一步看返回结果文件判断是否成功
  2007. CSimpleStringA strUnzipPath = GetFileDirectory(destPath.GetData());
  2008. CSimpleStringA strResultLog = strUnzipPath + SPLIT_SLASH_STR + "result.log";//结果文件
  2009. CSimpleStringA eErrorCode = "";
  2010. //获取结果文件
  2011. if (!GetSysUpgradeResult(strResultLog, eErrorCode, strErrMsg))
  2012. {
  2013. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, get result from result.log fail, %s", strErrMsg.GetData());
  2014. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail ,get result from result.log fail, %s", strErrMsg.GetData());
  2015. return 0;//失败
  2016. }
  2017. if (eErrorCode == "0")
  2018. {
  2019. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is succ,[%s] ", destPath.GetData());
  2020. return 1;//成功
  2021. }else{
  2022. //TODO: CrossPlaform [Gifur@2025730]
  2023. #ifdef RVC_OS_WIN
  2024. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail,errorCode = %s", eErrorCode.GetData());
  2025. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail,errorCode = %s", eErrorCode.GetData());
  2026. #else
  2027. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail,errorCode = %s,%s", eErrorCode.GetData(), strErrMsg.GetData());
  2028. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail,errorCode = %s,%s", eErrorCode.GetData(), strErrMsg.GetData());
  2029. #endif
  2030. return 0;//失败
  2031. }
  2032. }
  2033. }
  2034. else
  2035. {
  2036. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail, run file [%s] is not Exist",destPath.GetData());
  2037. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, run file [%s] is not Exist",destPath.GetData());
  2038. return 0;//失败
  2039. }
  2040. }
  2041. bool CUpgradeTaskFSM::ExecDeleteCmd(CSimpleStringA &strErrMsg,CInstallStep* ins)
  2042. {
  2043. //只删除同一层目录下的文件和文件夹
  2044. CSimpleStringA destPath ;//目的路径+通配符
  2045. if(ins->dstPathType=="0"){
  2046. CSimpleStringA rootPath;
  2047. if(GetRootTypePath(1,rootPath)){
  2048. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//相对路径
  2049. }else{
  2050. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPath GetRootTypePath 1 is fail");
  2051. return false;
  2052. }
  2053. }else if(ins->dstPathType=="1"){
  2054. destPath = ins->dstPath;//绝对路径
  2055. }else if(ins->dstPathType=="2"){
  2056. CSimpleStringA rootPath;
  2057. if(GetRootTypePath(2,rootPath)){
  2058. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//当前版本相对路径
  2059. }else{
  2060. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPath GetRootTypePath 2 is fail");
  2061. return false;
  2062. }
  2063. }else if(ins->dstPathType=="3"){
  2064. CSimpleStringA rootPath;
  2065. if(GetRootTypePath(3,rootPath)){
  2066. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//升级版本相对路径
  2067. }else{
  2068. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPath GetRootTypePath 3 is fail");
  2069. return false;
  2070. }
  2071. }else{
  2072. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecDeleteCmd destPathType is invalid %s",ins->dstPathType.GetData());
  2073. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPathType is invalid %s",ins->dstPathType.GetData());
  2074. return false;//不支持的路径类型
  2075. }
  2076. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecDeleteCmd destPath is %s",destPath.GetData());
  2077. if(destPath.IndexOf("*")>0){
  2078. CSimpleStringA strFileFilter = _GetFileName(destPath);
  2079. CSimpleStringA strDestDir = destPath.SubString(0, destPath.GetLength()-strFileFilter.GetLength()-1);
  2080. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match filter name [%s] in dir [%s]", strFileFilter.GetData(), strDestDir.GetData());
  2081. // 删除所有匹配文件
  2082. bool bRet = true;
  2083. auto arr = fileutil_get_sub_files(strDestDir.GetData());
  2084. if (arr != NULL)
  2085. {
  2086. for (int i = 0; i < arr->nelts; ++i)
  2087. {
  2088. char *file = ARRAY_IDX(arr, i, char*);
  2089. if (IsFileMatch(strFileFilter, _GetFileName(file)))
  2090. {
  2091. if(RemoveFileA(file)){
  2092. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file succ: [%s]", file);
  2093. }else{
  2094. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file fail: [%s]", file);
  2095. strErrMsg = CSimpleStringA::Format("delete match file fail: [%s]", file);
  2096. bRet = false;
  2097. break;
  2098. }
  2099. }
  2100. else
  2101. {
  2102. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ignore not match file: [%s]", file);
  2103. }
  2104. }
  2105. toolkit_array_free2(arr);
  2106. if(!bRet){
  2107. return bRet;
  2108. }
  2109. }
  2110. // 删除所有匹配子目录
  2111. auto subdirs = fileutil_get_sub_dirs(strDestDir.GetData());
  2112. if (subdirs != NULL)
  2113. {
  2114. for (int i = 0; i < subdirs->nelts; ++i)
  2115. {
  2116. const char *dir = ARRAY_IDX(subdirs, i, char*);
  2117. if (IsFileMatch(strFileFilter, _GetFileName(dir)))
  2118. {
  2119. if(RemoveDirRecursive(dir)){
  2120. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir succ: [%s]", dir);
  2121. }else{
  2122. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir fail: [%s]", dir);
  2123. strErrMsg = CSimpleStringA::Format("delete match dir fail: [%s]", dir);
  2124. bRet = false;
  2125. break;
  2126. }
  2127. }
  2128. else
  2129. {
  2130. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ignore not match dir: [%s]", dir);
  2131. }
  2132. }
  2133. toolkit_array_free2(subdirs);
  2134. if(!bRet){
  2135. return bRet;
  2136. }
  2137. }
  2138. return bRet;
  2139. }else{
  2140. //直接删除文件
  2141. bool bRet = true;
  2142. if (ExistsFileA(destPath.GetData()))
  2143. {
  2144. if(RemoveFileA(destPath.GetData())){
  2145. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file succ: [%s]", destPath.GetData());
  2146. }else{
  2147. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file fail: [%s]", destPath.GetData());
  2148. strErrMsg = CSimpleStringA::Format("delete match file fail: [%s]", destPath.GetData());
  2149. bRet = false;
  2150. }
  2151. return bRet;
  2152. }
  2153. else if (ExistsDirA(destPath.GetData()))
  2154. {
  2155. if(RemoveDirRecursiveA(destPath.GetData())){
  2156. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir succ: [%s]", destPath.GetData());
  2157. }else{
  2158. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir fail: [%s]", destPath.GetData());
  2159. strErrMsg = CSimpleStringA::Format("delete match dir fail: [%s]", destPath.GetData());
  2160. bRet = false;
  2161. }
  2162. return bRet;
  2163. }
  2164. else
  2165. {
  2166. //指定文件不再则忽略删除此文件
  2167. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete file [%s] not exists,ignore delete file", destPath.GetData());
  2168. return bRet;
  2169. }
  2170. }
  2171. }
  2172. bool CUpgradeTaskFSM::ExecCpoyCmd(CSimpleStringA &strErrMsg,CInstallStep* ins)
  2173. {
  2174. CSimpleStringA sourcePath ;//路径+通配符
  2175. if(ins->srcPathType=="0"){
  2176. CSimpleStringA rootPath="";
  2177. if(GetRootTypePath(1,rootPath)){
  2178. sourcePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->srcPath.GetData());//相对路径
  2179. }else{
  2180. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd sourcePath GetRootTypePath 1 is fail");
  2181. return false;
  2182. }
  2183. }else if(ins->srcPathType=="1"){
  2184. sourcePath = ins->srcPath;//绝对路径
  2185. }else if(ins->srcPathType=="2"){
  2186. CSimpleStringA rootPath="";
  2187. if(GetRootTypePath(2,rootPath)){
  2188. sourcePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->srcPath.GetData());//当前版本相对路径
  2189. }else{
  2190. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd sourcePath GetRootTypePath 2 is fail");
  2191. return false;
  2192. }
  2193. }else if(ins->srcPathType=="3"){
  2194. CSimpleStringA rootPath="";
  2195. if(GetRootTypePath(4,rootPath)){
  2196. sourcePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->srcPath.GetData());//升级包相对路径
  2197. }else{
  2198. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd sourcePath GetRootTypePath 4 is fail");
  2199. return false;
  2200. }
  2201. }else{
  2202. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd srcPathType is invalid %s",ins->srcPathType.GetData());
  2203. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd srcPathType is invalid %s",ins->srcPathType.GetData());
  2204. return false;//不支持的路径类型
  2205. }
  2206. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd sourcePath is %s",sourcePath.GetData());
  2207. CSimpleStringA destPath ;//目的路径文件夹
  2208. if(ins->dstPathType=="0"){
  2209. CSimpleStringA rootPath="";
  2210. if(GetRootTypePath(1,rootPath)){
  2211. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//相对路径
  2212. }else{
  2213. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd destPath GetRootTypePath 1 is fail");
  2214. return false;
  2215. }
  2216. }else if(ins->dstPathType=="1"){
  2217. destPath = ins->dstPath;//绝对路径
  2218. }else if(ins->dstPathType=="2"){
  2219. CSimpleStringA rootPath="";
  2220. if(GetRootTypePath(2,rootPath)){
  2221. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//当前版本相对路径
  2222. }else{
  2223. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd destPath GetRootTypePath 2 is fail");
  2224. return false;
  2225. }
  2226. }else if(ins->dstPathType=="3"){
  2227. CSimpleStringA rootPath="";
  2228. if(GetRootTypePath(3,rootPath)){
  2229. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),ins->dstPath.GetData());//升级版本相对路径
  2230. }else{
  2231. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd destPath GetRootTypePath 3 is fail");
  2232. return false;
  2233. }
  2234. //}else if(ins->dstPathType=="4"){
  2235. // CSimpleStringA rootPath="";
  2236. // if(GetRootTypePath(4,rootPath)){
  2237. // destPath = CSimpleStringA::Format("%s\\%s",rootPath.GetData(),ins->dstPath.GetData());//升级包相对路径
  2238. // }else{
  2239. // strErrMsg = CSimpleStringA::Format("ExecCpoyCmd destPath GetRootTypePath 4 is fail");
  2240. // return false;
  2241. // }
  2242. }else{
  2243. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd dstPathType is invalid %s",ins->dstPathType.GetData());
  2244. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd dstPathType is invalid %s",ins->dstPathType.GetData());
  2245. return false;//不支持的路径类型
  2246. }
  2247. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd destPath is %s",destPath.GetData());
  2248. if(sourcePath.IndexOf("*")>0){
  2249. //循环匹配拷贝
  2250. //找到要扫描的文件夹
  2251. CSimpleStringA strFileDir = GetFileDirectory(sourcePath.GetData());
  2252. //过滤通配条件
  2253. CSimpleStringA strFileFilter = _GetFileName(sourcePath.GetData());
  2254. list<CSimpleStringA> fileList;
  2255. //获取所有根文件夹下的文件相对路径:如mod\test.dll
  2256. RecursiveGetSubFiles(strFileDir.GetData(), strFileDir.GetData(), fileList);
  2257. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("find fileList =%d",fileList.size());
  2258. if(fileList.size()==0){
  2259. //判断mod文件夹未空则报错
  2260. if(sourcePath.IndexOf("mod")!=-1){
  2261. LogWarn(Severity_Low, Error_Exception, WARN_TASK_COPY_FILE_ZERO,CSimpleStringA::Format("ExecCpoyCmd sourcePath is [%s] file is 0",sourcePath.GetData()));
  2262. }
  2263. }
  2264. for(auto it = fileList.begin(); it != fileList.end(); it++)
  2265. {
  2266. if (IsFileMatch(strFileFilter, _GetFileName((const char*)(*it))))
  2267. {
  2268. CSimpleStringA strSrcName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", GetFileDirectory(sourcePath.GetData()).GetData(), (const char*)(*it));//源文件路径
  2269. CSimpleStringA strDestName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", destPath.GetData(),(const char*)(*it));//目标文件路径
  2270. //匹配成功,开始拷贝文件
  2271. CSimpleStringA errMsg = "";
  2272. ErrorCodeEnum rc = CopyFileOperate(strSrcName.GetData(),strDestName.GetData(),3,errMsg);
  2273. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] to [%s] %s", strSrcName.GetData(),strDestName.GetData(), rc == Error_Succeed ? "succeed" : "fail");
  2274. if (rc != Error_Succeed)
  2275. {
  2276. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] fail: %s",strSrcName.GetData(),errMsg.GetData());
  2277. strErrMsg = CSimpleStringA::Format("copy file [%s] fail: %s",strSrcName.GetData(),errMsg.GetData());
  2278. return false;//报错
  2279. }
  2280. }
  2281. }
  2282. }else{
  2283. //直接拷贝文件:考虑有些文件因智能对比不一定下发,故如果文件不存在,则默认忽略此策略
  2284. CSimpleStringA errMsg = "";
  2285. CSimpleStringA strDestName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", destPath.GetData(), _GetFileName(sourcePath.GetData()));
  2286. if(ExistsFileA(sourcePath.GetData())){
  2287. ErrorCodeEnum rc = CopyFileOperate(sourcePath.GetData(),strDestName.GetData(),3,errMsg);
  2288. if (rc != Error_Succeed)
  2289. {
  2290. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] fail:%s",sourcePath.GetData(),errMsg.GetData());
  2291. strErrMsg = CSimpleStringA::Format("copy file [%s] fail:%s",sourcePath.GetData(),errMsg.GetData());
  2292. return false;//报错
  2293. }
  2294. }else{
  2295. if(ins->srcPathType=="3"){
  2296. //升级包因智能升级不一定有指定的文件存在
  2297. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("拷贝指定的文件[%s]不存在,因智能升级对比过滤,故忽略此文件拷贝",sourcePath.GetData());
  2298. }else{
  2299. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] fail:file to copy not exist",sourcePath.GetData());
  2300. strErrMsg = CSimpleStringA::Format("copy file [%s] fail:file to copy not exist",sourcePath.GetData());
  2301. return false;//报错
  2302. }
  2303. }
  2304. }
  2305. return true;
  2306. }
  2307. bool CUpgradeTaskFSM::GetRootTypePath(int rootType,CSimpleStringA &Path)
  2308. {
  2309. CSimpleStringA tempPath;
  2310. if (rootType == 1) {
  2311. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Root", tempPath);//例如:C:\Run /opt/Run
  2312. if (Error_Succeed == Error) {
  2313. if (!tempPath.IsNullOrEmpty()) {
  2314. string runPathStr = tempPath.GetData();
  2315. int pos = runPathStr.find_last_of(SPLIT_SLASH);
  2316. Path = runPathStr.substr(0, pos).c_str();
  2317. }
  2318. else {
  2319. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get Root path is null");
  2320. return false;
  2321. }
  2322. }
  2323. else {
  2324. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get Root path is error");
  2325. return false;
  2326. }
  2327. }
  2328. else if (rootType == 2) {
  2329. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Base", tempPath);//例如:C:\Run\version\4.8.1.1 /opt/Run/version/4.8.1.1
  2330. if (Error_Succeed == Error) {
  2331. if (!tempPath.IsNullOrEmpty()) {
  2332. Path = tempPath;
  2333. }
  2334. else {
  2335. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get currVer path is null");
  2336. return false;
  2337. }
  2338. }
  2339. else {
  2340. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get currVer path is error");
  2341. return false;
  2342. }
  2343. }
  2344. else if (rootType == 3) {
  2345. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("RootVer", tempPath);//例如:C:\Run\version\4.9.1.1 /opt/Run/version/4.9.1.1
  2346. if (Error_Succeed == Error) {
  2347. if (!tempPath.IsNullOrEmpty()) {
  2348. Path = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", tempPath.GetData(), m_currentTask.pack_version.GetData());
  2349. }
  2350. else {
  2351. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get currVer path is null");
  2352. return false;
  2353. }
  2354. }
  2355. else {
  2356. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get upgradeVer path is error");
  2357. return false;
  2358. }
  2359. }
  2360. else if (rootType == 4) {
  2361. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Downloads", tempPath);//例如:C:\rvc\Downloads\4.8.1.1 /opt/rvc/Downloads/4.8.1.1
  2362. if (Error_Succeed == Error) {
  2363. if (!tempPath.IsNullOrEmpty()) {
  2364. CSimpleStringA strUnzipDir;
  2365. strUnzipDir = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", tempPath.GetData(), m_currentTask.pack_name.GetData());
  2366. //解压去除了.zip后缀
  2367. if (strUnzipDir.IsEndWith(".zip")) {
  2368. Path = strUnzipDir.SubString(0, strUnzipDir.GetLength() - 4);
  2369. }
  2370. else {
  2371. Path = strUnzipDir;
  2372. }
  2373. }
  2374. else {
  2375. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get upgradePack path is null");
  2376. return false;
  2377. }
  2378. }
  2379. else {
  2380. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get upgradePack path is error");
  2381. return false;
  2382. }
  2383. }
  2384. return true;
  2385. }
  2386. ErrorCodeEnum CUpgradeTaskFSM::CopyFileOperate(const char* srcPath, const char* destPath, int nCopyMode, CSimpleStringA &strErrMsg)
  2387. {
  2388. //源文件是否存在
  2389. if (!ExistsFileA(srcPath))
  2390. {
  2391. strErrMsg = CSimpleStringA::Format("CopyFileOperate file [%s] to copy not exist", srcPath);
  2392. return Error_NotExist;
  2393. }
  2394. //目标文件夹是否存在
  2395. CSimpleStringA strDestDir = GetFileDirectory(destPath);
  2396. if (!ExistsDirA(strDestDir.GetData())){
  2397. if(!CreateDirA(strDestDir.GetData(), TRUE)){
  2398. strErrMsg = CSimpleStringA::Format("CopyFileOperate create strDestDir[%s] fail", strDestDir.GetData());
  2399. return Error_Exception;
  2400. }
  2401. }
  2402. //目标文件是否存在
  2403. BOOL bDestFileExists = ExistsFileA(destPath);
  2404. if (bDestFileExists){
  2405. RemoveFileReadOnlyAttributeA(destPath);
  2406. }
  2407. BOOL bRet = TRUE;
  2408. switch (nCopyMode)
  2409. {
  2410. case 1:
  2411. {
  2412. if (bDestFileExists) {
  2413. //TODO: CrossPlaform [Gifur@2025730]
  2414. #ifdef RVC_OS_WIN
  2415. bRet = CopyFileA(srcPath, destPath, FALSE);
  2416. #else
  2417. if (fileutil_copy_file(destPath, srcPath) == 0) {
  2418. bRet = TRUE;
  2419. }
  2420. else {
  2421. bRet = FALSE;
  2422. }
  2423. #endif
  2424. }
  2425. else {
  2426. LOG_TRACE("ignore file [%s] copy for dest file not exists", srcPath);
  2427. }
  2428. }
  2429. break;
  2430. case 2:
  2431. {
  2432. if (!bDestFileExists)
  2433. //TODO: CrossPlaform [Gifur@2025730]
  2434. #ifdef RVC_OS_WIN
  2435. bRet = CopyFileA(srcPath, destPath, TRUE);
  2436. #else
  2437. if (fileutil_copy_file(destPath, srcPath) == 0) {
  2438. bRet = TRUE;
  2439. }
  2440. else {
  2441. bRet = FALSE;
  2442. }
  2443. #endif
  2444. else
  2445. LOG_TRACE("ignore file [%s] copy for dest file exists", srcPath);
  2446. }
  2447. break;
  2448. case 3:
  2449. //TODO: CrossPlaform [Gifur@2025730]
  2450. #ifdef RVC_OS_WIN
  2451. bRet = CopyFileA(srcPath, destPath, FALSE);
  2452. #else
  2453. if (fileutil_copy_file(destPath, srcPath) == 0) {
  2454. bRet = TRUE;
  2455. }
  2456. else {
  2457. bRet = FALSE;
  2458. }
  2459. #endif
  2460. break;
  2461. case 4:
  2462. {
  2463. // 不发生拷贝,只进行判断
  2464. if(bDestFileExists)
  2465. LOG_TRACE("file [%s] exist", srcPath);
  2466. else
  2467. LOG_TRACE("file [%s] not exist", srcPath);
  2468. }
  2469. break;
  2470. }
  2471. if (bRet){
  2472. RemoveFileReadOnlyAttributeA(destPath);
  2473. }else{
  2474. //TODO: CrossPlaform [Gifur@2025730]
  2475. #ifdef RVC_OS_WIN
  2476. strErrMsg = CSimpleStringA::Format("CopyFileOperate copy file fail:%d", GetLastError());
  2477. #else
  2478. strErrMsg = CSimpleStringA::Format("CopyFileOperate copy file fail:%d", errno);
  2479. #endif
  2480. }
  2481. ErrorCodeEnum rc = bRet ? Error_Succeed : Error_Unexpect;
  2482. return rc;
  2483. }
  2484. ErrorCodeEnum CUpgradeTaskFSM::SetRunSucceed(const char* pPackFile)
  2485. {
  2486. // 拷贝升级包到Upgraded目录
  2487. CSimpleStringA strDownloadPath, strUpgradedPath;
  2488. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("Downloads", strDownloadPath);
  2489. if(rc!=Error_Succeed){
  2490. return rc;
  2491. }
  2492. rc = m_pEntity->GetFunction()->GetPath("Upgraded", strUpgradedPath);
  2493. if(rc!=Error_Succeed){
  2494. return rc;
  2495. }
  2496. CSimpleStringA strSourcePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strDownloadPath, pPackFile);
  2497. CSimpleStringA strDestPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", (const char*)strUpgradedPath, pPackFile);
  2498. //TODO: CrossPlaform [Gifur@2025730]
  2499. #ifdef RVC_OS_WIN
  2500. CopyFileA(strSourcePath.GetData(), strDestPath.GetData(), FALSE);
  2501. #else
  2502. fileutil_copy_file(strDestPath.GetData(), strSourcePath.GetData());
  2503. #endif
  2504. return Error_Succeed;
  2505. }
  2506. ErrorCodeEnum CUpgradeTaskFSM::UpgradeToNewVersion()
  2507. {
  2508. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin UpgradeToNewVersion");
  2509. //获取新版本号
  2510. if(m_currentTask.NewVersion.IsNullOrEmpty()){
  2511. if(m_currentTask.pack_version.IsNullOrEmpty()){
  2512. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("task NewVersion is empty , no need to write");
  2513. return Error_Succeed;
  2514. }else{
  2515. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("NewVersion is null, pack_version=[%s] is not null,UpgradeToNewVersion fail, taskID=[%s]",m_currentTask.pack_version.GetData(), m_currentTask.upgradeTaskId.GetData());
  2516. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion fail, NewVersion is null, pack_version [%s] is not null,fail",m_currentTask.pack_version.GetData()).GetData());
  2517. return Error_Unexpect;//这种情况有问题,需要清除此任务
  2518. }
  2519. }else{
  2520. CSimpleStringA rootVerPath;
  2521. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("RootVer",rootVerPath);//获取version根路径
  2522. if(rc!=Error_Succeed){
  2523. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UpgradeToNewVersion fail , get RootVer path is fail");
  2524. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,"UpgradeToNewVersion: get RootVer path is fail");
  2525. return Error_Bug;
  2526. }
  2527. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), m_currentTask.NewVersion.GetData());
  2528. if(!ExistsDirA(strNewVerPath.GetData())){
  2529. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, strNewVerPath =%s is not exist,fail",strNewVerPath.GetData()).GetData());
  2530. return Error_Unexpect;//这种情况有问题,需要清除此任务
  2531. }
  2532. //修改运行时文件
  2533. //修改active配置
  2534. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "active.txt", rootVerPath.GetData());
  2535. if(!ExistsFileA(strActiveFile.GetData())){
  2536. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,"UpgradeToNewVersion fail, active file is not exist");
  2537. return Error_Unexpect;
  2538. }
  2539. //先做active文件备份,后面写入失败再copy回去
  2540. #ifdef RVC_OS_WIN
  2541. bool backupSucc = true;
  2542. CSimpleStringA strBackupActiveFile = CSimpleStringA::Format("%s\\back_active.txt", rootVerPath.GetData());
  2543. if(!CopyFileA(strActiveFile.GetData(),strBackupActiveFile.GetData(),FALSE)){
  2544. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, back up active.txt fail, err=%d",(int)GetLastError()).GetData());
  2545. backupSucc = false;
  2546. }
  2547. FILE* fp = fopen(strActiveFile.GetData(),"wb+");
  2548. if(fp==NULL){
  2549. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, open write active.txt fail, err=%d",(int)GetLastError()).GetData());
  2550. return Error_Unexpect;
  2551. }
  2552. int ret = fwrite(m_currentTask.NewVersion.GetData(),m_currentTask.NewVersion.GetLength(),1,fp);
  2553. if(ret!=1){
  2554. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, write active.txt fail, err=%d",(int)GetLastError()).GetData());
  2555. fclose(fp);
  2556. if(backupSucc){
  2557. if(CopyFileA(strBackupActiveFile.GetData(),strActiveFile.GetData(),FALSE)){
  2558. RemoveFileA(strBackupActiveFile.GetData());
  2559. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("back_active.txt copy to active.txt is succ");
  2560. }else{
  2561. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, back_active.txt copy to active.txt is fail, err=%d",(int)GetLastError()).GetData());
  2562. }
  2563. }
  2564. return Error_Unexpect;
  2565. }else{
  2566. fflush(fp);
  2567. int CommitRet = _commit(_fileno(fp));
  2568. if (CommitRet != 0) {
  2569. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion fail, commit write active.txt fail, err=%d", (int)GetLastError()).GetData());
  2570. }
  2571. fclose(fp);
  2572. if (ExistsFileA(strBackupActiveFile.GetData())) {
  2573. RemoveFileA(strBackupActiveFile.GetData());
  2574. }
  2575. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("write active.txt succ, version[%s]", m_currentTask.NewVersion.GetData());
  2576. }
  2577. //重新读active文件,看文件内容是什么
  2578. fp = fopen(strActiveFile.GetData(),"rb");
  2579. if(fp==NULL){
  2580. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, open read active.txt fail, err=%d",(int)GetLastError()).GetData());
  2581. return Error_Unexpect;
  2582. }
  2583. char cVer[16] = {0};
  2584. int re = fread(cVer,m_currentTask.NewVersion.GetLength(),1,fp);
  2585. if(re!=1){
  2586. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, read active.txt fail, err=%d",(int)GetLastError()).GetData());
  2587. }else{
  2588. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rewritten file active.txt, version[%s]", cVer);
  2589. }
  2590. fclose(fp);
  2591. return Error_Succeed;
  2592. #else
  2593. bool backupSucc = true;
  2594. CSimpleStringA strBackupActiveFile = CSimpleStringA::Format("%s\\back_active.txt", rootVerPath.GetData());
  2595. if (fileutil_copy_file( strBackupActiveFile.GetData(),strActiveFile.GetData())!=0) {
  2596. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion fail, back up active.txt fail, err=%d", errno).GetData());
  2597. backupSucc = false;
  2598. }
  2599. FILE* fp = fopen(strActiveFile.GetData(),"wb+");
  2600. if(fp==NULL){
  2601. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion fail, open write active.txt fail, err=%d", errno).GetData());
  2602. return Error_Unexpect;
  2603. }
  2604. int ret = fwrite(m_currentTask.NewVersion.GetData(),m_currentTask.NewVersion.GetLength(),1,fp);
  2605. if(ret!=1){
  2606. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion fail, write active.txt fail, err=%d", errno).GetData());
  2607. fclose(fp);
  2608. if (backupSucc) {
  2609. if (fileutil_copy_file(strActiveFile.GetData(), strBackupActiveFile.GetData())==0) {
  2610. RemoveFileA(strBackupActiveFile.GetData());
  2611. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("back_active.txt copy to active.txt is succ");
  2612. }else{
  2613. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion fail, back_active.txt copy to active.txt is fail, err=%d", errno).GetData());
  2614. }
  2615. }
  2616. return Error_Unexpect;
  2617. }else{
  2618. fflush(fp);
  2619. int CommitRet = fsync(fileno(fp));
  2620. if (CommitRet != 0) {
  2621. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion fail, commit write active.txt fail, err=%d", errno).GetData());
  2622. }
  2623. fclose(fp);
  2624. if (ExistsFileA(strBackupActiveFile.GetData())) {
  2625. RemoveFileA(strBackupActiveFile.GetData());
  2626. }
  2627. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("write active.txt succ, version[%s]", m_currentTask.NewVersion.GetData());
  2628. }
  2629. //重新读active文件,看文件内容是什么
  2630. fp = fopen(strActiveFile.GetData(),"rb");
  2631. if(fp==NULL){
  2632. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, open read active.txt fail, err=%d", errno).GetData());
  2633. return Error_Unexpect;
  2634. }
  2635. char cVer[16] = {0};
  2636. int re = fread(cVer,m_currentTask.NewVersion.GetLength(),1,fp);
  2637. if(re!=1){
  2638. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL,CSimpleStringA::Format("UpgradeToNewVersion fail, read active.txt fail, err=%d", errno).GetData());
  2639. }else{
  2640. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rewritten file active.txt, version[%s]", cVer);
  2641. }
  2642. fclose(fp);
  2643. return Error_Succeed;
  2644. #endif
  2645. }
  2646. }
  2647. ErrorCodeEnum CUpgradeTaskFSM::RollBackToHistoryVersion(CVersion historyVersion)
  2648. {
  2649. // 判断目标版本是否小于当前版本
  2650. CVersion v=m_currentVersion;//当前版本
  2651. CVersion curVersion(v.GetMajor(), v.GetMinor(), v.GetRevision(), v.GetBuild());
  2652. if (curVersion <= historyVersion)
  2653. {
  2654. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("history version [%s] >= current version [%s]", historyVersion.ToString().GetData(), curVersion.ToString().GetData()).GetData());
  2655. return Error_Param;
  2656. }
  2657. // 检查对应文件夹是否存在
  2658. CSimpleStringA strHisVersion = historyVersion.ToString();
  2659. CSimpleStringA rootVerPath;
  2660. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("RootVer",rootVerPath);//获取version根路径
  2661. if(rc!=Error_Succeed){
  2662. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, "RollBackToHistoryVersion fail , get RootVer path is fail");
  2663. return Error_Bug;
  2664. }
  2665. CSimpleStringA strHisVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), strHisVersion.GetData());
  2666. if (!ExistsDirA(strHisVerPath.GetData()))
  2667. {
  2668. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RollBackToHistoryVersion fail, history version dir [%s] not exists", strHisVerPath.GetData()).GetData());
  2669. return Error_NotExist;
  2670. }
  2671. //删除当时的运行缓存值
  2672. // 修改active.txt文件,可能存在失败
  2673. CSimpleStringA strActiveFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "active.txt", rootVerPath.GetData());
  2674. if(!ExistsFileA(strActiveFile.GetData())){
  2675. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, "RollBackToHistoryVersion fail, active file is not exist");
  2676. return Error_Unexpect;
  2677. }
  2678. //先做active文件备份,后面写入失败再copy回去
  2679. #ifdef RVC_OS_WIN
  2680. bool backupSucc = true;
  2681. CSimpleStringA strBackupActiveFile = CSimpleStringA::Format("%s\\Rollback_active.txt", rootVerPath.GetData());
  2682. if(!CopyFileA(strActiveFile.GetData(),strBackupActiveFile.GetData(),FALSE)){
  2683. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL,CSimpleStringA::Format("RRollBackToHistoryVersion fail, back up active.txt fail, err=%d",(int)GetLastError()).GetData());
  2684. backupSucc = false;
  2685. }
  2686. FILE* fp = fopen(strActiveFile.GetData(),"wb+");
  2687. if(fp==NULL){
  2688. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, open write active.txt fail,err=%d", (int)GetLastError()).GetData());
  2689. return Error_Unexpect;
  2690. }
  2691. int ret = fwrite(strHisVersion.GetData(),strHisVersion.GetLength(),1,fp);
  2692. if(ret!=1){
  2693. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, write active.txt fail,err=%d", (int)GetLastError()).GetData());
  2694. fclose(fp);
  2695. if(backupSucc){
  2696. if(CopyFileA(strBackupActiveFile.GetData(),strActiveFile.GetData(),FALSE)){
  2697. RemoveFileA(strBackupActiveFile.GetData());
  2698. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Rollback_active.txt copy to active.txt is succ");
  2699. }else{
  2700. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL,CSimpleStringA::Format("RRollBackToHistoryVersion fail, Rollback_active.txt copy to active.txt is fail, err=%d",(int)GetLastError()).GetData());
  2701. }
  2702. }
  2703. return Error_Unexpect;
  2704. }else{
  2705. fflush(fp);
  2706. int CommitRet = _commit(_fileno(fp));
  2707. if (CommitRet != 0) {
  2708. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, commit write active.txt fail, err=%d", (int)GetLastError()).GetData());
  2709. }
  2710. fclose(fp);
  2711. if (ExistsFileA(strBackupActiveFile.GetData())) {
  2712. RemoveFileA(strBackupActiveFile.GetData());
  2713. }
  2714. }
  2715. //重新读active文件,看文件内容是什么
  2716. fp = fopen(strActiveFile.GetData(),"rb");
  2717. if(fp==NULL){
  2718. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, open read active.txt fail,err=%d", (int)GetLastError()).GetData());
  2719. return Error_Unexpect;
  2720. }
  2721. char cVer[16] = {0};
  2722. int re = fread(cVer,strHisVersion.GetLength(),1,fp);
  2723. if(re!=1){
  2724. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, Read active.txt fail,err=%d", (int)GetLastError()).GetData());
  2725. }else{
  2726. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rewritten file active.txt, version[%s]", cVer);
  2727. }
  2728. fclose(fp);
  2729. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set rollback to history version [%s] succeed", (const char*)strHisVersion);
  2730. return Error_Succeed;
  2731. #else
  2732. bool backupSucc = true;
  2733. CSimpleStringA strBackupActiveFile = CSimpleStringA::Format("%s\\Rollback_active.txt", rootVerPath.GetData());
  2734. if (fileutil_copy_file(strBackupActiveFile.GetData(), strActiveFile.GetData())!=0) {
  2735. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, back up active.txt fail, err=%d", errno).GetData());
  2736. backupSucc = false;
  2737. }
  2738. FILE* fp = fopen(strActiveFile.GetData(),"wb+");
  2739. if(fp==NULL){
  2740. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, open write active.txt fail,err=%d", errno).GetData());
  2741. return Error_Unexpect;
  2742. }
  2743. int ret = fwrite(strHisVersion.GetData(),strHisVersion.GetLength(),1,fp);
  2744. if(ret!=1){
  2745. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, write active.txt fail,err=%d", errno).GetData());
  2746. fclose(fp);
  2747. if (backupSucc) {
  2748. if (fileutil_copy_file(strActiveFile.GetData(), strBackupActiveFile.GetData())==0) {
  2749. RemoveFileA(strBackupActiveFile.GetData());
  2750. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Rollback_active.txt copy to active.txt is succ");
  2751. }else{
  2752. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, Rollback_active.txt copy to active.txt is fail, err=%d", errno).GetData());
  2753. }
  2754. }
  2755. return Error_Unexpect;
  2756. }else{
  2757. fflush(fp);
  2758. int CommitRet = fsync(fileno(fp));
  2759. if (CommitRet != 0) {
  2760. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, commit write active.txt fail, err=%d", errno).GetData());
  2761. }
  2762. fclose(fp);
  2763. if (ExistsFileA(strBackupActiveFile.GetData())) {
  2764. RemoveFileA(strBackupActiveFile.GetData());
  2765. }
  2766. }
  2767. //重新读active文件,看文件内容是什么
  2768. fp = fopen(strActiveFile.GetData(),"rb");
  2769. if(fp==NULL){
  2770. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, open read active.txt fail,err=%d", errno).GetData());
  2771. return Error_Unexpect;
  2772. }
  2773. char cVer[16] = {0};
  2774. int re = fread(cVer,strHisVersion.GetLength(),1,fp);
  2775. if(re!=1){
  2776. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RRollBackToHistoryVersion fail, Read active.txt fail,err=%d", errno).GetData());
  2777. }else{
  2778. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("read rewritten file active.txt, version[%s]", cVer);
  2779. }
  2780. fclose(fp);
  2781. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set rollback to history version [%s] succeed", (const char*)strHisVersion);
  2782. return Error_Succeed;
  2783. #endif
  2784. }
  2785. void CUpgradeTaskFSM::RecursiveGetSubFiles(const char *pszDir, const char *pszTrimStart, list<CSimpleStringA>& retList)
  2786. {
  2787. if (!ExistsDirA(pszDir))
  2788. return;
  2789. auto arr = fileutil_get_sub_files(pszDir);
  2790. if (arr != NULL)
  2791. {
  2792. for (int i = 0; i < arr->nelts; ++i)
  2793. {
  2794. char *file = ARRAY_IDX(arr, i, char*);
  2795. #ifdef RVC_OS_WIN
  2796. //不区分大小写
  2797. if (pszTrimStart != NULL && strnicmp(file, pszTrimStart, strlen(pszTrimStart))==0) {
  2798. #else
  2799. //区分大小写
  2800. if (pszTrimStart != NULL && strncmp(file, pszTrimStart, strlen(pszTrimStart)) == 0) {
  2801. #endif
  2802. file += strlen(pszTrimStart);
  2803. if (*file == SPLIT_SLASH)
  2804. file++;
  2805. }
  2806. retList.push_back(file);
  2807. }
  2808. toolkit_array_free2(arr);
  2809. }
  2810. arr = fileutil_get_sub_dirs(pszDir);
  2811. if (arr != NULL)
  2812. {
  2813. for (int i = 0; i < arr->nelts; ++i)
  2814. {
  2815. char *dir = ARRAY_IDX(arr, i, char*);
  2816. RecursiveGetSubFiles(dir, pszTrimStart, retList);
  2817. }
  2818. toolkit_array_free2(arr);
  2819. }
  2820. }
  2821. bool CUpgradeTaskFSM::IsFileMatch(const char *pszFilter, const char *pszFileName)
  2822. {
  2823. const char *p1 = pszFilter;
  2824. const char *p2 = pszFileName;
  2825. if (p1 == NULL)
  2826. return true;
  2827. if (p2 == NULL)
  2828. return false;
  2829. if (*p1 ==NULL && *p2 ==NULL)
  2830. return true;
  2831. else if (*p1 == NULL)
  2832. return false;
  2833. else if (*p2 == NULL)
  2834. {
  2835. // 查找*p1是否全是*
  2836. while(*p1 == '*')
  2837. p1++;
  2838. if (*p1 == NULL)
  2839. return true;
  2840. else
  2841. return false;
  2842. }
  2843. if (*p1 != '*')
  2844. {
  2845. if (tolower(*p1) != tolower(*p2))
  2846. return false;
  2847. else
  2848. return IsFileMatch(p1+1, p2+1);
  2849. }
  2850. else
  2851. {
  2852. while(*++p1 == '*');
  2853. if (*p1 == NULL)
  2854. return true;
  2855. while (*p2)
  2856. {
  2857. while(tolower(*p1) != tolower(*p2) && *++p2);
  2858. if (*p2 == NULL)
  2859. return false;
  2860. if(IsFileMatch(p1+1, p2+1))
  2861. return true;
  2862. p2++;
  2863. }
  2864. return false;
  2865. }
  2866. }
  2867. bool CUpgradeTaskFSM::RecursiveCopyDir(const char *pszSourceDir, const char *pszDestDir,CSimpleStringA &strErrInfo)
  2868. {//TODO: CrossPlaform [Gifur@2025730]
  2869. array_header_t *arr;
  2870. #ifdef RVC_OS_WIN
  2871. if (!ExistsDirA(pszSourceDir))
  2872. {
  2873. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("source dir [%s] not exists: %d", pszSourceDir,GetLastError());
  2874. strErrInfo = CSimpleStringA::Format("source dir [%s] not exists: %d", pszSourceDir, GetLastError());
  2875. return false;
  2876. }
  2877. if (!ExistsDirA(pszDestDir))
  2878. {
  2879. if (!CreateDirRecursiveA(pszDestDir))
  2880. {
  2881. strErrInfo = CSimpleStringA::Format("create dir [%s] fail: %d", pszDestDir, GetLastError());
  2882. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("create dir fail: %s",strErrInfo.GetData());
  2883. return false;
  2884. }
  2885. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("create dir [%s] succ", pszDestDir);
  2886. }
  2887. #else
  2888. if (!ExistsDirA(pszSourceDir))
  2889. {
  2890. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("source dir [%s] not exists: %d", pszSourceDir, errno);
  2891. strErrInfo = CSimpleStringA::Format("source dir [%s] not exists: %d", pszSourceDir, errno);
  2892. return false;
  2893. }
  2894. if (!ExistsDirA(pszDestDir))
  2895. {
  2896. if (!CreateDirRecursiveA(pszDestDir))
  2897. {
  2898. strErrInfo = CSimpleStringA::Format("create dir [%s] fail: %d", pszDestDir, errno);
  2899. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("create dir fail: %s",strErrInfo.GetData());
  2900. return false;
  2901. }
  2902. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("create dir [%s] succ", pszDestDir);
  2903. }
  2904. #endif
  2905. arr = fileutil_get_sub_files_a(pszSourceDir);
  2906. if (arr)
  2907. {
  2908. int i;
  2909. for (i = 0; i < arr->nelts; ++i)
  2910. {
  2911. char szDestFile[256] = { 0 };
  2912. char *file = ARRAY_IDX(arr, i, char*);
  2913. strcpy(szDestFile, pszDestDir);
  2914. if (szDestFile[strlen(szDestFile) - 1] != SPLIT_SLASH)
  2915. strcat(szDestFile, SPLIT_SLASH_STR);
  2916. strcat(szDestFile, strrchr(file, SPLIT_SLASH) + 1);
  2917. //TODO: CrossPlaform [Gifur@2025730]
  2918. #ifdef RVC_OS_WIN
  2919. if (!CopyFileA(file, szDestFile, FALSE))
  2920. {
  2921. strErrInfo = CSimpleStringA::Format("copy file [%s] fail: %d", file, GetLastError());
  2922. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("copy file fail:%s",strErrInfo.GetData());
  2923. toolkit_array_free2(arr);
  2924. return false;
  2925. }
  2926. #else
  2927. if (fileutil_copy_file(szDestFile, file)!=0)
  2928. {
  2929. strErrInfo = CSimpleStringA::Format("copy file [%s] fail: %d", file, errno);
  2930. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("copy file fail:%s",strErrInfo.GetData());
  2931. toolkit_array_free2(arr);
  2932. return false;
  2933. }
  2934. #endif
  2935. }
  2936. toolkit_array_free2(arr);
  2937. }
  2938. arr = fileutil_get_sub_dirs_a(pszSourceDir);
  2939. if (arr)
  2940. {
  2941. int i;
  2942. for (i = 0; i < arr->nelts; ++i)
  2943. {
  2944. char szDestSubDir[256] = { 0 };
  2945. char *dir = ARRAY_IDX(arr, i, char*);
  2946. strcpy(szDestSubDir, pszDestDir);
  2947. if (szDestSubDir[strlen(szDestSubDir) - 1] != SPLIT_SLASH)
  2948. strcat(szDestSubDir, SPLIT_SLASH_STR);
  2949. strcat(szDestSubDir, strrchr(dir, SPLIT_SLASH) + 1);
  2950. if (!RecursiveCopyDir(dir, szDestSubDir,strErrInfo))
  2951. {
  2952. //TODO: CrossPlaform [Gifur@2025730]
  2953. #ifdef RVC_OS_WIN
  2954. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("copy dir [%s] fail: %d", dir, GetLastError());
  2955. if(strErrInfo.IsNullOrEmpty()){
  2956. strErrInfo = CSimpleStringA::Format("copy dir [%s] fail: %d", dir, GetLastError());
  2957. }
  2958. toolkit_array_free2(arr);
  2959. return false;
  2960. #else
  2961. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("copy dir [%s] fail: %d", dir, errno);
  2962. if (strErrInfo.IsNullOrEmpty()) {
  2963. strErrInfo = CSimpleStringA::Format("copy dir [%s] fail: %d", dir, errno);
  2964. }
  2965. toolkit_array_free2(arr);
  2966. return false;
  2967. #endif
  2968. }
  2969. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("copy dir [%s] succ", dir);
  2970. }
  2971. toolkit_array_free2(arr);
  2972. }
  2973. return true;
  2974. }
  2975. CSimpleStringA CUpgradeTaskFSM::GetFileDirectory(const char *pszFullPath)
  2976. {
  2977. int i=strlen(pszFullPath)-1;
  2978. for( ; i>0 && pszFullPath[i]!= SPLIT_SLASH; i--)NULL;
  2979. return CSimpleStringA(pszFullPath, i);
  2980. }
  2981. int CUpgradeTaskFSM::system_on(string cmdFilePath, bool isWait,CSimpleStringA &errMsg)
  2982. {
  2983. #ifdef RVC_OS_WIN
  2984. string cmdLine = "cmd /c ";
  2985. cmdLine.append(cmdFilePath.c_str());
  2986. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("exec bat %s",cmdLine.c_str());
  2987. return waitSystem("cmd.exe", cmdLine.c_str(), true, errMsg);
  2988. #else
  2989. //linux直接用system函数即可
  2990. return system(cmdFilePath.c_str());
  2991. #endif
  2992. }
  2993. //返回值是0则正常执行,其他都是非正常未知状态
  2994. int CUpgradeTaskFSM::waitSystem(string cmd, const char * par, bool nShow,CSimpleStringA &errMsg)
  2995. {
  2996. #ifdef RVC_OS_WIN
  2997. SHELLEXECUTEINFOA ShExecInfo = { 0 };
  2998. ShExecInfo.cbSize = sizeof(SHELLEXECUTEINFO);
  2999. ShExecInfo.fMask = SEE_MASK_NOCLOSEPROCESS;
  3000. ShExecInfo.hwnd = NULL;
  3001. ShExecInfo.lpVerb = NULL;
  3002. ShExecInfo.lpFile = cmd.c_str(); //调用的程序名
  3003. ShExecInfo.lpParameters = par; //调用程序的命令行参数
  3004. ShExecInfo.lpDirectory = NULL;
  3005. ShExecInfo.nShow = nShow ? SW_MINIMIZE: SW_HIDE; //窗口状态为隐藏
  3006. ShExecInfo.hInstApp = NULL;
  3007. if (!ShellExecuteEx(&ShExecInfo))
  3008. {
  3009. errMsg = CSimpleStringA::Format("shellExecute is fail :%d",GetLastError());
  3010. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellExecute is fail :%s",errMsg.GetData());
  3011. return -1;
  3012. }
  3013. //等到该进程结束,如果5分钟没结束,强行杀死进程
  3014. if (WaitForSingleObject(ShExecInfo.hProcess, 5*60*1000) == WAIT_TIMEOUT)
  3015. {
  3016. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellExecute is time out");
  3017. errMsg = CSimpleStringA::Format("shellExecute is time out");
  3018. if (TerminateProcess(ShExecInfo.hProcess, 0))
  3019. {
  3020. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("TerminateProcess is succ");
  3021. }else {
  3022. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("TerminateProcess is fail :%d",GetLastError());
  3023. }
  3024. if (CloseHandle(ShExecInfo.hProcess))
  3025. {
  3026. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellExecute CloseHandle is succ");
  3027. }else {
  3028. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellExecute CloseHandle is fail");
  3029. }
  3030. return -1;
  3031. }
  3032. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellExecute is succ");
  3033. DWORD exitCode = 0;
  3034. if (!GetExitCodeProcess(ShExecInfo.hProcess, &exitCode))
  3035. {
  3036. errMsg = CSimpleStringA::Format("GetExitCodeProcess is fail:%d",GetLastError());
  3037. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetExitCodeProcess is fail:%s",errMsg.GetData());
  3038. return -1;
  3039. }
  3040. if (CloseHandle(ShExecInfo.hProcess))
  3041. {
  3042. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellExecute CloseHandle is succ");
  3043. }else {
  3044. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("shellExecute CloseHandle is fail");
  3045. }
  3046. if(exitCode!=0){
  3047. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("GetExitCodeProcess ret exitCode=%d",(int)exitCode);
  3048. errMsg = CSimpleStringA::Format("GetExitCodeProcess ret exitCode=%d",(int)exitCode);
  3049. }
  3050. return exitCode;
  3051. #else
  3052. return 0;
  3053. #endif
  3054. }
  3055. ErrorCodeEnum CUpgradeTaskFSM::UnzipPack(const char* unZipPackName)
  3056. {
  3057. #ifdef RVC_OS_WIN
  3058. // 设定解压临时目录
  3059. char szOldPath[MAX_PATH] = {};
  3060. GetCurrentDirectoryA(MAX_PATH, szOldPath);
  3061. CSimpleStringA strDownloadsPath;
  3062. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("Downloads", strDownloadsPath);
  3063. if(rc!=Error_Succeed){
  3064. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unzipPack is fail,get Downloads Path is fail");
  3065. return Error_Unexpect;
  3066. }
  3067. CSimpleStringA strZipFile = CSimpleStringA::Format("%s\\%s", strDownloadsPath.GetData(), unZipPackName);
  3068. //升级包是否存在
  3069. if(!ExistsFileA(strZipFile)){
  3070. LogWarn(Severity_Low, Error_NotExist, ERR_TASK_UNZIP_PACK_FAIL, CSimpleStringA::Format("pack [%s] not exists", unZipPackName).GetData());
  3071. return Error_NotExist;
  3072. }
  3073. CSimpleStringA strUnzipDir;
  3074. strUnzipDir = CSimpleStringA::Format("%s\\%s", strDownloadsPath.GetData(), unZipPackName);
  3075. //解压去除了.zip后缀
  3076. if (strUnzipDir.IsEndWith(".zip")){
  3077. strUnzipDir = strUnzipDir.SubString(0, strUnzipDir.GetLength()-4);
  3078. }
  3079. // 解压目录是否存在,则先删除
  3080. if (ExistsDirA(strUnzipDir.GetData()))
  3081. {
  3082. if(!RemoveDirRecursiveA(strUnzipDir.GetData())){
  3083. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unzipPack is fail,delete exist temp unzip dir [%s] fail,err=%d",strUnzipDir.GetData(),GetLastError());
  3084. return Error_Unexpect;
  3085. }
  3086. }
  3087. // 创建临时解压目录
  3088. if(!CreateDirectoryA(strUnzipDir.GetData(), NULL)){
  3089. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unzipPack is fail,create temp unzip dir [%s] fail,err=%d",strUnzipDir.GetData(),GetLastError());
  3090. return Error_Unexpect;
  3091. }
  3092. //设置当前目录为主目录
  3093. if(!SetCurrentDirectoryA(strUnzipDir.GetData())){
  3094. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unzipPack is fail,SetCurrentDirectoryA dir [%s] fail,err=%d",strUnzipDir.GetData(),GetLastError());
  3095. return Error_Unexpect;
  3096. }
  3097. // 解压
  3098. HZIP hz = OpenZip((void*)(const char*)strZipFile,0,ZIP_FILENAME);
  3099. if (hz == 0)
  3100. {
  3101. SetCurrentDirectoryA(szOldPath);
  3102. LogWarn(Severity_Low, Error_Unexpect, ERR_TASK_UNZIP_PACK_FAIL, CSimpleStringA::Format("open zip file [%s] fail", unZipPackName).GetData());
  3103. return Error_Unexpect;
  3104. }
  3105. ZIPENTRY ze;
  3106. ZRESULT zrc = GetZipItemA(hz,-1,&ze);
  3107. if(zrc == ZR_OK){
  3108. int numitems=ze.index;
  3109. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("zip entry count is %d", ze.index);
  3110. for (int i=0; i<numitems; i++)
  3111. {
  3112. zrc = GetZipItemA(hz,i,&ze);
  3113. if(zrc==ZR_OK){
  3114. //兼容压缩包里面中文名称是utf8编码
  3115. if(!!toolkit_detect_utf8_str2(ze.name)){
  3116. char *pGBK = ConvertUtf8ToGBK((const char*)ze.name);
  3117. zrc = UnzipItem(hz,i,pGBK,0,ZIP_FILENAME);
  3118. if(zrc ==ZR_OK){
  3119. if (pGBK) free(pGBK);
  3120. }else{
  3121. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unzip [%s] UTF8 item fail,result=0x%X", pGBK,(int)zrc);
  3122. if (pGBK) free(pGBK);
  3123. break;
  3124. }
  3125. }else{
  3126. zrc = UnzipItem(hz,i,ze.name,0,ZIP_FILENAME);
  3127. if(zrc ==ZR_OK){
  3128. //Dbg("unzip [%s] GBK item succeed", ze.name);
  3129. }else{
  3130. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unzip [%s] GBK item fail,result=0x%X", ze.name,(int)zrc);
  3131. break;
  3132. }
  3133. }
  3134. }else{
  3135. //出错,退出
  3136. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("unzip GetZipItemA [%d] item fail,fail=0x%X",i,(int)zrc);
  3137. break;
  3138. }
  3139. }
  3140. CloseZip(hz);
  3141. }else{
  3142. CloseZip(hz);
  3143. }
  3144. // 恢复当前目录
  3145. SetCurrentDirectoryA(szOldPath);
  3146. if(zrc!=ZR_OK){
  3147. return Error_Exception;
  3148. }
  3149. return Error_Succeed;
  3150. #else
  3151. CSimpleStringA strDownloadsPath;
  3152. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("Downloads", strDownloadsPath);
  3153. if (rc != Error_Succeed) {
  3154. Dbg("unzipPack is fail,get Downloads Path is fail");
  3155. return Error_Unexpect;
  3156. }
  3157. CSimpleStringA strZipFile = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strDownloadsPath.GetData(), unZipPackName);
  3158. //升级包是否存在
  3159. if (!ExistsFileA(strZipFile)) {
  3160. LogWarn(Severity_Low, Error_NotExist, ERR_TASK_UNZIP_PACK_FAIL, CSimpleStringA::Format("pack [%s] not exists", unZipPackName).GetData());
  3161. return Error_NotExist;
  3162. }
  3163. CSimpleStringA strUnzipDir;
  3164. strUnzipDir = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strDownloadsPath.GetData(), unZipPackName);
  3165. //解压去除了.zip后缀
  3166. if (strUnzipDir.IsEndWith(".zip")) {
  3167. strUnzipDir = strUnzipDir.SubString(0, strUnzipDir.GetLength() - 4);
  3168. }
  3169. // 解压目录是否存在,存在先删除
  3170. if (ExistsDirA(strUnzipDir.GetData()))
  3171. {
  3172. if (!RemoveDirRecursiveA(strUnzipDir.GetData())) {
  3173. Dbg("unzipPack is fail,delete exist temp unzip dir [%s] fail,err=%d", strUnzipDir.GetData(), errno);
  3174. return Error_Unexpect;
  3175. }
  3176. }
  3177. // 创建临时解压目录
  3178. if (!CreateDirA(strUnzipDir.GetData(), false)) {
  3179. // 创建临时解压目录失败
  3180. Dbg("unzipPack is fail,create temp unzip dir [%s] fail,err=%d", strUnzipDir.GetData(), errno);
  3181. return Error_Unexpect;
  3182. }
  3183. if (changeFileAtt(strZipFile.GetData())!=0) {
  3184. //改变文件属性
  3185. Dbg("unzipPack is fail,changeFileAtt zip [%s] fail,err=%d", strZipFile.GetData(), errno);
  3186. return Error_Unexpect;
  3187. }
  3188. // 解压
  3189. string zipFileStr = strZipFile.GetData();
  3190. string zipTempDir = strUnzipDir.GetData();
  3191. if (UnZipToDir(zipFileStr, zipTempDir) != 0)
  3192. {
  3193. LogWarn(Severity_Low, Error_Unexpect, ERR_TASK_UNZIP_PACK_FAIL, CSimpleStringA::Format("unzip pack [%s] fail", unZipPackName));
  3194. return Error_NotExist;
  3195. }
  3196. return Error_Succeed;
  3197. #endif
  3198. }
  3199. ErrorCodeEnum CUpgradeTaskFSM::DeleteUnzipDir()
  3200. {
  3201. CSimpleStringA strDownloadsPath;
  3202. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("Downloads", strDownloadsPath);
  3203. if(rc!=Error_Succeed){
  3204. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("DeleteUnzipDir is fail,get Downloads Path is fail");
  3205. return Error_Unexpect;
  3206. }
  3207. //判断解压缩文件夹名是否存在,不存在告警
  3208. if (m_currentTask.pack_name.IsNullOrEmpty()) {
  3209. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("DeleteUnzipDir is fail ,temp unzip dir name is null");
  3210. return Error_Unexpect;
  3211. }
  3212. CSimpleStringA strUnzipDir = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", strDownloadsPath.GetData(), m_currentTask.pack_name.GetData());
  3213. //解压去除了.zip后缀
  3214. if (strUnzipDir.IsEndWith(".zip")){
  3215. strUnzipDir = strUnzipDir.SubString(0, strUnzipDir.GetLength()-4);
  3216. }
  3217. // 解压目录是否存在,存在则删除
  3218. if (ExistsDirA(strUnzipDir.GetData()))
  3219. {
  3220. if(!RemoveDirRecursiveA(strUnzipDir.GetData())){
  3221. //TODO: CrossPlaform [Gifur@2025730]
  3222. #ifdef RVC_OS_WIN
  3223. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("DeleteUnzipDir fail , temp unzip dir [%s] fail,err=%d",strUnzipDir.GetData(),(int)GetLastError());
  3224. #else
  3225. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("DeleteUnzipDir fail , temp unzip dir [%s] fail,err=%d",strUnzipDir.GetData(), errno);
  3226. #endif
  3227. return Error_Unexpect;
  3228. }else{
  3229. return Error_Succeed;
  3230. }
  3231. }
  3232. return Error_Succeed;
  3233. }
  3234. void CUpgradeTaskFSM::SendUpgradeTaskProgress(CTaskProcess &process, CSimpleStringA logCodeStr, CSimpleStringA rtaStr)
  3235. {
  3236. Json::Value root;
  3237. Json::FastWriter fw;//写入对象
  3238. if(!process.strPackName.IsNullOrEmpty()){
  3239. root["strPackName"]=process.strPackName.GetData();
  3240. }else{
  3241. root["strPackName"]="";
  3242. }
  3243. if(process.installVersion.IsNullOrEmpty()){
  3244. root["installVersion"]="0.0.0.0";
  3245. }else{
  3246. root["installVersion"]=process.installVersion.GetData();
  3247. }
  3248. //string currentBuildStr = CSimpleStringA::Format("%d.%d.%d",process.CurrentVersion.GetMajor(),process.CurrentVersion.GetMinor(),process.CurrentVersion.GetRevision()).GetData();
  3249. root["CurrentVersion"]=process.CurrentVersion.GetData();
  3250. root["CurrentBuild"]= process.CurrentVersion.GetData();
  3251. root["upgradeTimeStr"]=CSmallDateTime::GetNow().ToTimeString().GetData();
  3252. if(!process.cInstallState.IsNullOrEmpty()){
  3253. root["cInstallState"]=process.cInstallState.GetData();
  3254. }else{
  3255. root["cInstallState"]="";
  3256. }
  3257. if(!process.strInstallComment.IsNullOrEmpty()){
  3258. CSimpleStringA comment = "task:";
  3259. comment.Append(process.strInstallComment.GetData());
  3260. root["strInstallComment"]=comment.GetData();
  3261. }else{
  3262. root["strInstallComment"]="";
  3263. }
  3264. string upgradeStr = fw.write(root);
  3265. if (!upgradeStr.empty()) {
  3266. LogWarn(Severity_Low, Error_Succeed, LOG_WRAN_UPGRADE_PROGRESS, upgradeStr.c_str());
  3267. if (rtaStr.IsNullOrEmpty()) {
  3268. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(logCodeStr).setAPI("SendUpgradeTaskProgress")(upgradeStr.c_str());
  3269. }
  3270. else {
  3271. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(logCodeStr).setResultCode(rtaStr).setAPI("SendUpgradeTaskProgress")(upgradeStr.c_str());
  3272. }
  3273. }else{
  3274. LogWarn(Severity_Middle,Error_Unexpect,ERR_WRAN_UPGRADE_PROGRESS,"获取升级过程信息错误");
  3275. }
  3276. }
  3277. ErrorCodeEnum CUpgradeTaskFSM::GetRunConfigStrValue(const char *pszSection, const char *pszKey, CSimpleStringA &strValue)
  3278. {
  3279. CSmartPointer<IConfigInfo> pConfig;
  3280. ErrorCodeEnum rc = m_pEntity->GetFunction()->OpenConfig(Config_Run, pConfig);
  3281. if (rc != Error_Succeed){
  3282. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_OPEN_FAILED)("GetRunConfigStrValue get run config fail ,err=%d", (int)rc);
  3283. return rc;
  3284. }
  3285. return pConfig->ReadConfigValue(pszSection, pszKey, strValue);
  3286. }
  3287. ErrorCodeEnum CUpgradeTaskFSM::SetRunConfigStrValue(const char *pszSection, const char *pszKey, const char *pszValue)
  3288. {
  3289. CSmartPointer<IConfigInfo> pConfig;
  3290. ErrorCodeEnum rc = m_pEntity->GetFunction()->OpenConfig(Config_Run, pConfig);
  3291. if (rc != Error_Succeed){
  3292. return rc;
  3293. }
  3294. return pConfig->WriteConfigValue(pszSection, pszKey, pszValue);
  3295. }
  3296. bool CUpgradeTaskFSM::ClearCurrTaskInfo()
  3297. {
  3298. if(SetRunConfigStrValue("upgradeTask","UpgradeTaskId","")!=Error_Succeed){
  3299. return false;
  3300. }
  3301. if(SetRunConfigStrValue("upgradeTask","CreateTaskVersion","")!=Error_Succeed){
  3302. return false;
  3303. }
  3304. if(SetRunConfigStrValue("upgradeTask","PackName","")!=Error_Succeed){
  3305. return false;
  3306. }
  3307. if(SetRunConfigStrValue("upgradeTask","PendingState","")!=Error_Succeed){
  3308. return false;
  3309. }
  3310. if(SetRunConfigStrValue("upgradeTask","NewVersion","")!=Error_Succeed){
  3311. return false;
  3312. }
  3313. if (SetRunConfigStrValue("upgradeTask", "TaskType", "") != Error_Succeed) {
  3314. return false;
  3315. }
  3316. if (SetRunConfigStrValue("upgradeTask", "depNewVersion", "") != Error_Succeed) {
  3317. return false;
  3318. }
  3319. //试着删除可能临时下载的升级包文件
  3320. CSimpleStringA strPath;
  3321. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("Downloads", strPath);
  3322. if(Error_Succeed != rc){
  3323. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearCurrTaskInfo get Downloads path fail,upgradeTaskId=%s",m_currentTask.upgradeTaskId.GetData());
  3324. return false;
  3325. }
  3326. CSimpleStringA tempPackPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s%s", strPath.GetData(), m_currentTask.pack_name.GetData(),".tmp");
  3327. if(ExistsFileA(tempPackPath.GetData())){
  3328. if(!RemoveFileA(tempPackPath.GetData())){
  3329. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearCurrTaskInfo delete tempPack fail,packPath=%s",tempPackPath.GetData());
  3330. return false;
  3331. }
  3332. }
  3333. //清空内存变量
  3334. m_currentTask.upgradeTaskId = "";
  3335. m_currentTask.createTaskVersion = "";
  3336. m_currentTask.pack_name = "";
  3337. m_currentTask.cPendingState = "";
  3338. m_currentTask.NewVersion="";
  3339. m_currentTask.task_type = "";
  3340. m_currentTask.depNewVersion = "";
  3341. m_currentTask.pack_version = "";
  3342. m_currentTask.stop_media_play =false;
  3343. m_currentTask.reboot_type = 0;
  3344. m_currentTask.file_path="";
  3345. m_currentTask.download_url = "";
  3346. m_currentTask.pack_hash = "";
  3347. m_currentTask.pack_length = 0;
  3348. ClearInstallStep();
  3349. return true;
  3350. }
  3351. bool CUpgradeTaskFSM::ClearInstallStep()
  3352. {
  3353. //清理安装步骤
  3354. int iCount = m_currentTask.install_step.size();
  3355. for (int j = 0; j < iCount; j++){
  3356. CInstallStep* tmp = m_currentTask.install_step.front();
  3357. m_currentTask.install_step.pop();
  3358. delete tmp;//删除对象
  3359. }
  3360. return true;
  3361. }
  3362. ErrorCodeEnum CUpgradeTaskFSM::LoadPersistTaskInfo()
  3363. {
  3364. // 从运行配置中读出上次未处理完的任务
  3365. // 上次未安装结束
  3366. CTaskInfo taskInfo = {};
  3367. CSimpleStringA strValue;
  3368. ErrorCodeEnum rc = GetRunConfigStrValue("upgradeTask", "UpgradeTaskId", strValue);
  3369. if(rc!=Error_Succeed){
  3370. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_READ_FAILED)("recover upgrade state from run config, read [upgradeTask][UpgradeTaskId] fail ,err=%d",(int)rc);
  3371. return rc;
  3372. }
  3373. taskInfo.upgradeTaskId = strValue;
  3374. rc = GetRunConfigStrValue("upgradeTask", "CreateTaskVersion", strValue);
  3375. if(rc!=Error_Succeed){
  3376. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_READ_FAILED)("recover upgrade state from run config, read [upgradeTask][CreateTaskVersion] fail ,err=%d",(int)rc);
  3377. return rc;
  3378. }
  3379. taskInfo.createTaskVersion = strValue;
  3380. rc = GetRunConfigStrValue("upgradeTask", "PackName", strValue);
  3381. if(rc!=Error_Succeed){
  3382. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_READ_FAILED)("recover upgrade state from run config, read [upgradeTask][PackName] fail ,err=%d",(int)rc);
  3383. return rc;
  3384. }
  3385. taskInfo.pack_name = strValue;
  3386. rc = GetRunConfigStrValue("upgradeTask", "PendingState", strValue);
  3387. if(rc!=Error_Succeed){
  3388. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_READ_FAILED)("recover upgrade state from run config, read [upgradeTask][PendingState] fail ,err=%d",(int)rc);
  3389. return rc;
  3390. }
  3391. taskInfo.cPendingState = strValue;
  3392. rc = GetRunConfigStrValue("upgradeTask", "NewVersion", strValue);
  3393. if(rc!=Error_Succeed){
  3394. DbgWithLink(LOG_LEVEL_WARN,LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_READ_FAILED)("recover upgrade state from run config, read [upgradeTask][NewVersion] fail ,err=%d",(int)rc);
  3395. return rc;
  3396. }
  3397. taskInfo.NewVersion = strValue;
  3398. rc = GetRunConfigStrValue("upgradeTask", "TaskType", strValue);
  3399. if (rc != Error_Succeed) {
  3400. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_READ_FAILED)("recover upgrade state from run config, read [upgradeTask][TaskType] fail ,err=%d", (int)rc);
  3401. return rc;
  3402. }
  3403. taskInfo.task_type = strValue;
  3404. rc = GetRunConfigStrValue("upgradeTask", "depNewVersion", strValue);
  3405. if (rc != Error_Succeed) {
  3406. DbgWithLink(LOG_LEVEL_WARN, LOG_TYPE_SYSTEM).setResultCode(RTAERR_CONFIG_READ_FAILED)("recover upgrade state from run config, read [upgradeTask][depNewVersion] fail ,err=%d", (int)rc);
  3407. return rc;
  3408. }
  3409. taskInfo.depNewVersion = strValue;
  3410. //初始化其他变量
  3411. //CSimpleStringA pack_version; //升级包指定的版本号
  3412. //CSimpleStringA to_copy; //拷贝文件列表
  3413. //CSimpleStringA to_run; //体系外运行脚本文件名称
  3414. //bool reboot_type; //重启类型
  3415. //CSimpleStringA download_url; //升级包下载地址
  3416. //CSimpleStringA pack_hash; //升级包hash值
  3417. //long pack_length; //升级包长度
  3418. taskInfo.pack_version = "";
  3419. taskInfo.reboot_type = 0;
  3420. taskInfo.file_path = "";
  3421. taskInfo.download_url = "";
  3422. taskInfo.pack_hash = "";
  3423. taskInfo.pack_length = 0;
  3424. taskInfo.stop_media_play = true;
  3425. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("recover upgrade state from run config, UpgradeTaskId=%s", taskInfo.upgradeTaskId.GetData());
  3426. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("recover upgrade state from run config, CreateTaskVersion=%s",taskInfo.createTaskVersion.GetData());
  3427. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("recover upgrade state from run config, PackName=%s",taskInfo.pack_name.GetData());
  3428. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("recover upgrade state from run config, PendingState=%s",taskInfo.cPendingState.GetData());
  3429. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("recover upgrade state from run config, NewVersion=%s",taskInfo.NewVersion.GetData());
  3430. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("recover upgrade state from run config, TaskType=%s", taskInfo.task_type.GetData());
  3431. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("recover upgrade state from run config, depNewVersion=%s", taskInfo.depNewVersion.GetData());
  3432. if (!taskInfo.upgradeTaskId.IsNullOrEmpty()) {
  3433. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("recover upgrade state from run config, UpgradeTaskId=[%s] , TaskType=[%s] , PendingState=[%s] , NewVersion=[%s] , depNewVersion=[%s] , CreateTaskVersion=[%s] , PackName=[%s]",
  3434. taskInfo.upgradeTaskId.GetData(), taskInfo.task_type.GetData(), taskInfo.cPendingState.GetData(),
  3435. taskInfo.NewVersion.GetData(), taskInfo.depNewVersion.GetData(), taskInfo.createTaskVersion.GetData(), taskInfo.pack_name.GetData());
  3436. }
  3437. else {
  3438. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("recover upgrade state from run config, UpgradeTaskId=[%s] , TaskType=[%s] , PendingState=[%s] , NewVersion=[%s] , depNewVersion=[%s] , CreateTaskVersion=[%s] , PackName=[%s]",
  3439. taskInfo.upgradeTaskId.GetData(), taskInfo.task_type.GetData(), taskInfo.cPendingState.GetData(),
  3440. taskInfo.NewVersion.GetData(), taskInfo.depNewVersion.GetData(), taskInfo.createTaskVersion.GetData(), taskInfo.pack_name.GetData());
  3441. }
  3442. m_currentTask = taskInfo;
  3443. return Error_Succeed;
  3444. }
  3445. ErrorCodeEnum CUpgradeTaskFSM::loadCenterCfgInfo()
  3446. {
  3447. //访问http地址的读取
  3448. CSmartPointer<IConfigInfo> spConfig;
  3449. ErrorCodeEnum rc = m_pEntity->GetFunction()->OpenConfig(Config_CenterSetting, spConfig);
  3450. if (rc == Error_Succeed){
  3451. m_centerUrl.queryPreTaskUrl = QUERY_PRE_TASK_URL;
  3452. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read queryPreTaskUrl=%s", m_centerUrl.queryPreTaskUrl.c_str());
  3453. m_centerUrl.queryContinueTaskUrl = QUERY_CONTINUE_TASK_URL;
  3454. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read queryContinueTaskUrl=%s", m_centerUrl.queryContinueTaskUrl.c_str());
  3455. m_centerUrl.updateTaskUrl = UPDATE_TASK_URL;
  3456. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read updateTaskUrl=%s", m_centerUrl.updateTaskUrl.c_str());
  3457. m_centerUrl.queryCurrTaskUrl = QUERY_CURR_TASK_URL;
  3458. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read queryCurrTaskUrl=%s", m_centerUrl.queryCurrTaskUrl.c_str());
  3459. m_centerUrl.queryNewTaskUrl = QUERY_NEW_TASK_URL;
  3460. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read queryNewTaskUrl=%s", m_centerUrl.queryNewTaskUrl.c_str());
  3461. m_centerUrl.queryTaskStatus = QUERY_TASK_STATUS_URL;
  3462. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read queryTaskStatus=%s", m_centerUrl.queryTaskStatus.c_str());
  3463. m_centerUrl.sendSM3TaskUrl = SEND_SM3_TASK_URL;
  3464. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read sendSM3TaskUrl=%s", m_centerUrl.sendSM3TaskUrl.c_str());
  3465. CSimpleStringA str="";
  3466. //加载后端服务访问地址
  3467. //rc = spConfig->ReadConfigValue("UpgradeManager", "QueryPreTaskUrl", str);
  3468. //if(rc==Error_Succeed){
  3469. // m_centerUrl.queryPreTaskUrl = str;
  3470. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("init read CenterSetting.ini QueryPreTaskUrl=%s",str.GetData());
  3471. //}else{
  3472. // LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini QueryPreTaskUrl error");
  3473. // return rc;
  3474. //}
  3475. //rc = spConfig->ReadConfigValue("UpgradeManager", "QueryContinueTaskUrl", str);
  3476. //if(rc==Error_Succeed){
  3477. // m_centerUrl.queryContinueTaskUrl = str;
  3478. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("init read CenterSetting.ini QueryContinueTaskUrl=%s",str.GetData());
  3479. //}else{
  3480. // LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini QueryContinueTaskUrl error");
  3481. // return rc;
  3482. //}
  3483. //rc = spConfig->ReadConfigValue("UpgradeManager", "UpdateTaskUrl", str);
  3484. //if(rc==Error_Succeed){
  3485. // m_centerUrl.updateTaskUrl = str;
  3486. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("init read CenterSetting.ini UpdateTaskUrl=%s",str.GetData());
  3487. //}else{
  3488. // LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini UpdateTaskUrl error");
  3489. // return rc;
  3490. //}
  3491. //rc = spConfig->ReadConfigValue("UpgradeManager", "QueryCurrTaskUrl", str);
  3492. //if(rc==Error_Succeed){
  3493. // m_centerUrl.queryCurrTaskUrl = str;
  3494. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("init read CenterSetting.ini QueryCurrTaskUrl=%s",str.GetData());
  3495. //}else{
  3496. // LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini QueryCurrTaskUrl error");
  3497. // return rc;
  3498. //}
  3499. //rc = spConfig->ReadConfigValue("UpgradeManager", "QueryNewTaskUrl", str);
  3500. //if(rc==Error_Succeed){
  3501. // m_centerUrl.queryNewTaskUrl = str;
  3502. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("init read CenterSetting.ini QueryNewTaskUrl=%s",str.GetData());
  3503. //}else{
  3504. // LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini QueryNewTaskUrl error");
  3505. // return rc;
  3506. //}
  3507. //rc = spConfig->ReadConfigValue("UpgradeManager", "QueryTaskStatus", str);
  3508. //if(rc==Error_Succeed){
  3509. // m_centerUrl.queryTaskStatus = str;
  3510. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("init read CenterSetting.ini QueryTaskStatus=%s",str.GetData());
  3511. //}else{
  3512. // LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini QueryTaskStatus error");
  3513. // return rc;
  3514. //}
  3515. //rc = spConfig->ReadConfigValue("UpgradeManager", "SendSM3TaskUrl", str);
  3516. //if(rc==Error_Succeed){
  3517. // m_centerUrl.sendSM3TaskUrl = str;
  3518. // DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("init read CenterSetting.ini SendSM3TaskUrl=%s",str.GetData());
  3519. //}else{
  3520. // LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini SendSM3TaskUrl error");
  3521. // return rc;
  3522. //}
  3523. //新的下载分段切片长度值(单位K)
  3524. rc = spConfig->ReadConfigValue("UpgradeManager", "NewEachDownloadLen", str);
  3525. if(rc==Error_Succeed){
  3526. if(str.IsNullOrEmpty()){
  3527. m_newEachDownloadLen = DOWNLOAD_CONTENT_EACH_LEN;
  3528. }else{
  3529. if(atoi(str.GetData())>16){
  3530. m_newEachDownloadLen = atoi(str.GetData())*1024;
  3531. }else{
  3532. m_newEachDownloadLen = 16*1024;//设置默认最小值
  3533. }
  3534. }
  3535. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini NewEachDownloadLen=%d K", m_newEachDownloadLen);
  3536. }else{
  3537. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini NewEachDownloadLen error");
  3538. return rc;
  3539. }
  3540. //加载是否打印http通讯日志
  3541. int printlogInt =0;
  3542. rc = spConfig->ReadConfigValueInt("UpgradeManager", "PrintLog", printlogInt);
  3543. if(rc==Error_Succeed){
  3544. if(printlogInt==0){
  3545. m_PrintHttpLog = false;
  3546. }else{
  3547. m_PrintHttpLog = true;
  3548. }
  3549. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini PrintLog=%d",printlogInt);
  3550. }else{
  3551. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini PrintLog error");
  3552. return rc;
  3553. }
  3554. int startFlagInt =0;
  3555. rc = spConfig->ReadConfigValueInt("UpgradeManager", "StartFlag", startFlagInt);
  3556. if(rc==Error_Succeed){
  3557. if(startFlagInt==0){
  3558. m_startFlag = false;
  3559. }else{
  3560. m_startFlag = true;
  3561. }
  3562. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini StartFlag=%d",startFlagInt);
  3563. }else{
  3564. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini StartFlag error");
  3565. return rc;
  3566. }
  3567. int stopSetFirewallInt = 0;
  3568. rc = spConfig->ReadConfigValueInt("UpgradeManager", "stopSetFirewall", stopSetFirewallInt);
  3569. if (rc == Error_Succeed) {
  3570. if (stopSetFirewallInt == 0) {
  3571. m_stopSetFirewall = false;
  3572. }
  3573. else {
  3574. m_stopSetFirewall = true;
  3575. }
  3576. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read CenterSetting.ini stopSetFirewall=%d", stopSetFirewallInt);
  3577. }
  3578. else {
  3579. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini stopSetFirewall error");
  3580. return rc;
  3581. }
  3582. int queryNewTaskInterval = 0;
  3583. rc = spConfig->ReadConfigValueInt("UpgradeManager", "queryNewTaskInterval", queryNewTaskInterval);
  3584. if (rc == Error_Succeed) {
  3585. if (queryNewTaskInterval != 0) {
  3586. if (queryNewTaskInterval < 10) {
  3587. m_queryNewTaskInterval = 10;
  3588. }
  3589. else {
  3590. m_queryNewTaskInterval = queryNewTaskInterval;
  3591. }
  3592. }
  3593. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read CenterSetting.ini queryNewTaskInterval=%d", m_queryNewTaskInterval);
  3594. }
  3595. else {
  3596. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini queryNewTaskInterval error");
  3597. return rc;
  3598. }
  3599. int installStopCopyDep = 0;
  3600. rc = spConfig->ReadConfigValueInt("UpgradeManager", "installStopCopyDep", installStopCopyDep);
  3601. if (rc == Error_Succeed) {
  3602. if (installStopCopyDep == 1) {
  3603. m_installStopCopyDep = true;//配置1时停止拷贝
  3604. }
  3605. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("init read CenterSetting.ini installStopCopyDep=%d", m_installStopCopyDep);
  3606. }
  3607. else {
  3608. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini installStopCopyDep error");
  3609. return rc;
  3610. }
  3611. //加载扫描hash配置文件列表
  3612. rc = spConfig->ReadConfigValue("UpgradeManager", "relativeDir", str);
  3613. if(rc==Error_Succeed){
  3614. m_hashScanSet.relativeDir = str;
  3615. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini relativeDir=%s",str.GetData());
  3616. }else{
  3617. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini relativeDir error");
  3618. return rc;
  3619. }
  3620. rc = spConfig->ReadConfigValue("UpgradeManager", "absoluteDir", str);
  3621. if(rc==Error_Succeed){
  3622. m_hashScanSet.absoluteDir = str;
  3623. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini absoluteDir=%s",str.GetData());
  3624. }else{
  3625. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini absoluteDir error");
  3626. return rc;
  3627. }
  3628. rc = spConfig->ReadConfigValue("UpgradeManager", "currVerDir", str);
  3629. if(rc==Error_Succeed){
  3630. m_hashScanSet.currVerDir = str;
  3631. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini currVerDir=%s",str.GetData());
  3632. }else{
  3633. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini currVerDir error");
  3634. return rc;
  3635. }
  3636. rc = spConfig->ReadConfigValue("UpgradeManager", "blackFile", str);
  3637. if(rc==Error_Succeed){
  3638. m_hashScanSet.blackFile = str;
  3639. CAutoArray<CSimpleStringA> arr = str.Split('|');
  3640. for(int i=0; i< arr.GetCount(); i++)
  3641. {
  3642. m_FileBlacklist.push_back(arr[i]);
  3643. }
  3644. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini blackFile=%s",str.GetData());
  3645. }else{
  3646. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini blackFile error");
  3647. return rc;
  3648. }
  3649. rc = spConfig->ReadConfigValue("UpgradeManager", "blackDir", str);
  3650. if(rc==Error_Succeed){
  3651. m_hashScanSet.blackDir = str;
  3652. CAutoArray<CSimpleStringA> arr = str.Split('|');
  3653. for(int i=0; i< arr.GetCount(); i++)
  3654. {
  3655. m_DirBlacklist.push_back(arr[i]);
  3656. }
  3657. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("init read CenterSetting.ini blackDir=%s",str.GetData());
  3658. }else{
  3659. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini blackDir error");
  3660. return rc;
  3661. }
  3662. }else{
  3663. LogWarn(Severity_Middle, rc, ERR_TASK_READ_INI_FAIL, "get CenterSetting.ini error");
  3664. return rc;
  3665. }
  3666. return Error_Succeed;
  3667. }
  3668. //TODO: CrossPlaform [Gifur@2025730]
  3669. bool CUpgradeTaskFSM::GetFileLength(const char* filePath,long &fileLen)
  3670. {
  3671. FILE* file = fopen(filePath, "rb");
  3672. if (file!=NULL)
  3673. {
  3674. #ifdef RVC_OS_WIN
  3675. fileLen = filelength(fileno(file));
  3676. fclose(file);
  3677. return true;
  3678. #else
  3679. if (fseek(file, 0, SEEK_END)!=0) {
  3680. Dbg("GetFileLength fseek is error,%s",strerror(errno));
  3681. fclose(file);
  3682. fileLen = 0;
  3683. return false;
  3684. }
  3685. fileLen = ftell(file); ///求出当前文件指针距离文件开始的字节数
  3686. if (fileLen == -1) {
  3687. Dbg("GetFileLength ftell is error,%s", strerror(errno));
  3688. fclose(file);
  3689. fileLen = 0;
  3690. return false;
  3691. }
  3692. else {
  3693. fclose(file);
  3694. return true;
  3695. }
  3696. #endif
  3697. } else{
  3698. fileLen = 0;
  3699. return false;
  3700. }
  3701. return true;
  3702. }
  3703. ErrorCodeEnum CUpgradeTaskFSM::GetSM3List(CSimpleStringA &strSM3List)
  3704. {
  3705. if (!m_fileSM3List.IsNullOrEmpty())
  3706. {
  3707. strSM3List = m_fileSM3List;
  3708. return Error_Succeed;
  3709. }
  3710. m_fileSM3Map.clear();//生成文件hash列表前先清空
  3711. //根据配置项开始扫描文件和计算hash
  3712. ErrorCodeEnum rc = Error_Succeed ;
  3713. if(!m_hashScanSet.absoluteDir.IsNullOrEmpty()){
  3714. //绝对路径扫描
  3715. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("GetSM3List scan absoluteDir=%s",m_hashScanSet.absoluteDir.GetData());
  3716. rc = ScanSM3ByType(1,m_hashScanSet.absoluteDir);
  3717. if(rc!=Error_Succeed){
  3718. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("GetMD5List fail ,scan absoluteDir is fail");
  3719. return rc;
  3720. }
  3721. }
  3722. if(!m_hashScanSet.relativeDir.IsNullOrEmpty()){
  3723. //相对路径扫描
  3724. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("GetSM3List scan relativeDir=%s",m_hashScanSet.relativeDir.GetData());
  3725. ScanSM3ByType(2,m_hashScanSet.relativeDir);
  3726. if(rc!=Error_Succeed){
  3727. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("GetMD5List fail ,scan relativeDir is fail");
  3728. return rc;
  3729. }
  3730. }
  3731. if(!m_hashScanSet.currVerDir.IsNullOrEmpty()){
  3732. //当前版本扫描
  3733. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("GetSM3List scan currVerDir=%s",m_hashScanSet.currVerDir.GetData());
  3734. ScanSM3ByType(3,m_hashScanSet.currVerDir);
  3735. if(rc!=Error_Succeed){
  3736. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("GetMD5List fail ,scan currVerDir is fail");
  3737. return rc;
  3738. }
  3739. }
  3740. if(m_iBlackFileCount>50){
  3741. LogWarn(Severity_Low, rc, WARN_BLACK_FILE_SUM, CSimpleStringA::Format("get MD5List black file count=%d",m_iBlackFileCount));
  3742. }
  3743. m_iBlackFileCount=0;
  3744. for(auto it=m_fileSM3Map.begin(); it!=m_fileSM3Map.end(); it++)
  3745. {
  3746. strSM3List += it->first;
  3747. strSM3List += ",";
  3748. strSM3List += it->second;
  3749. strSM3List += ";";
  3750. }
  3751. m_fileSM3List = strSM3List;
  3752. return Error_Succeed;
  3753. }
  3754. ErrorCodeEnum CUpgradeTaskFSM::ScanSM3ByType(int type,CSimpleStringA pathFilter)
  3755. {
  3756. if(!pathFilter.IsNullOrEmpty()){
  3757. CSimpleStringA rootPath;//根路径
  3758. CSimpleStringA replacePath;//替换路径
  3759. CSimpleStringA currPath;//当前版本路径
  3760. CSimpleStringA tempPath ="";
  3761. ErrorCodeEnum Error =m_pEntity->GetFunction()->GetPath("Root",tempPath);//例如:C:\Run /opt/Run
  3762. if(Error_Succeed == Error){
  3763. if(!tempPath.IsNullOrEmpty()){
  3764. string runPathStr = tempPath.GetData();
  3765. int pos = runPathStr.find_last_of(SPLIT_SLASH);
  3766. rootPath = runPathStr.substr(0,pos).c_str();
  3767. }else{
  3768. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("ScanSM3ByType get Root path is null");
  3769. return Error_Null;
  3770. }
  3771. }else{
  3772. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("ScanSM3ByType get Root path is error");
  3773. return Error;
  3774. }
  3775. CSimpleStringA tempCurrPath = "";
  3776. Error =m_pEntity->GetFunction()->GetPath("Base",tempCurrPath);//例如:C:\Run\version\4.8.1.1 /opt/Run/version/4.8.1.1
  3777. if(Error_Succeed == Error){
  3778. if(!tempCurrPath.IsNullOrEmpty()){
  3779. currPath = tempCurrPath;
  3780. }else{
  3781. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("ScanSM3ByType get currVer path is null");
  3782. return Error_Null;
  3783. }
  3784. }else{
  3785. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("ScanSM3ByType get currVer path is error");
  3786. return Error;
  3787. }
  3788. //路径扫描
  3789. CAutoArray<CSimpleStringA> copyList = pathFilter.Split('|');
  3790. for(int i=0; i<copyList.GetCount(); i++)
  3791. {
  3792. CSimpleStringA file = copyList[i];
  3793. #ifdef RVC_OS_WIN
  3794. #else
  3795. //UOS把路径改为反斜杠
  3796. string replaceStr = file.GetData();
  3797. std::replace(replaceStr.begin(), replaceStr.end(), '\\', '/');
  3798. file = replaceStr.c_str();
  3799. #endif
  3800. if(file.IsNullOrEmpty()){
  3801. continue;
  3802. }else{
  3803. //构造扫描的绝对路径
  3804. CSimpleStringA scanFilter = "";
  3805. CSimpleStringA replaceDir = "";
  3806. if(type==1){
  3807. //绝对路径
  3808. scanFilter = file;
  3809. replaceDir = GetFileDirectory(file.GetData());
  3810. }else if(type==2){
  3811. //相对路径
  3812. scanFilter = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",rootPath.GetData(),file.GetData());
  3813. replaceDir = GetFileDirectory(file.GetData());
  3814. }else if(type==3){
  3815. //当前版本路径:特殊处理下替换路径为Run\version\4.8.1.1
  3816. scanFilter = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",currPath.GetData(),file.GetData());
  3817. CSimpleStringA temp = scanFilter.SubString(rootPath.GetLength()+1,scanFilter.GetLength()-(rootPath.GetLength()+1));
  3818. replaceDir = GetFileDirectory(temp.GetData());
  3819. }
  3820. //替换路径前缀
  3821. ErrorCodeEnum ret = NewSM3Folder(scanFilter,replaceDir);
  3822. if(ret!=Error_Succeed){
  3823. return ret;
  3824. }
  3825. }
  3826. }
  3827. }
  3828. return Error_Succeed;
  3829. }
  3830. ErrorCodeEnum CUpgradeTaskFSM::NewSM3Folder(CSimpleStringA scanFilter,CSimpleStringA replaceDir)
  3831. {
  3832. if(scanFilter.IsNullOrEmpty()){
  3833. return Error_Succeed;
  3834. }
  3835. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("scan file scanFilter is %s",scanFilter.GetData());
  3836. //通配符
  3837. if(scanFilter.IndexOf("*")>0){
  3838. //找到要扫描的文件夹
  3839. CSimpleStringA strFileDir = GetFileDirectory(scanFilter.GetData());
  3840. //过滤通配条件
  3841. CSimpleStringA strFileFilter = _GetFileName(scanFilter.GetData());
  3842. list<CSimpleStringA> fileList;
  3843. //获取所有根文件夹下的文件相对路径:如mod\test.dll
  3844. RecursiveGetSubFiles(strFileDir.GetData(), strFileDir.GetData(), fileList);
  3845. for(auto it = fileList.begin(); it != fileList.end(); it++)
  3846. {
  3847. CSimpleStringA tempFile = *it;
  3848. if (IsFileMatch(strFileFilter, _GetFileName(tempFile.GetData())))
  3849. {
  3850. CSimpleStringA tempFullName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",strFileDir.GetData(),tempFile.GetData());
  3851. CSimpleStringA tempFileName = _GetFileName(tempFile.GetData());
  3852. CSimpleStringA tempFileDir = GetFileDirectory(tempFile.GetData());
  3853. //整个黑名单文件过滤
  3854. bool isBlackFile = false;
  3855. list<CSimpleStringA>::iterator itorFile = m_FileBlacklist.begin();
  3856. while(itorFile!=m_FileBlacklist.end())
  3857. {
  3858. CSimpleStringA fileBlack = *itorFile;
  3859. //if(tempFileName.IsEndWith(fileBlack.GetData(),true)){
  3860. // isBlackFile=true;
  3861. // break;
  3862. //}
  3863. if(IsFileMatch(fileBlack.GetData(),tempFileName.GetData())){
  3864. isBlackFile=true;
  3865. break;
  3866. }
  3867. itorFile++;
  3868. }
  3869. if(isBlackFile){
  3870. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("BlackFile Filter don't add to md5 list, file = [%s]", tempFullName.GetData());
  3871. m_iBlackFileCount++;
  3872. continue;
  3873. }
  3874. //整个黑名单文件夹过滤
  3875. bool isBlackDir = false;
  3876. list<CSimpleStringA>::iterator itorDir = m_DirBlacklist.begin();
  3877. while(itorDir!=m_DirBlacklist.end())
  3878. {
  3879. CSimpleStringA dirBlack = *itorDir;
  3880. if(tempFileDir.IndexOf(dirBlack.GetData())>0){
  3881. isBlackDir=true;
  3882. break;
  3883. }
  3884. itorDir++;
  3885. }
  3886. if(isBlackDir){
  3887. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("BlackDir Filter don't add to md5 list, dir = [%s]", tempFullName.GetData());
  3888. continue;
  3889. }
  3890. //计算hash值,加入到hash列表
  3891. CSimpleStringA strMD5Val;
  3892. ErrorCodeEnum rErrcode = SM3FileToStr(tempFullName, strMD5Val,false);
  3893. if (Error_Succeed != rErrcode)
  3894. {
  3895. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("%s get sm3 fail ,error:%d", tempFullName.GetData(), (int)rErrcode);
  3896. return rErrcode;
  3897. }
  3898. CSimpleStringA relativePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",replaceDir.GetData(),tempFile.GetData());
  3899. //Dbg("scan file succ [%s]",relativePath.GetData());
  3900. m_fileSM3Map[relativePath] = strMD5Val;
  3901. Sleep(100);//时间片
  3902. }
  3903. }
  3904. }else{
  3905. if(ExistsFileA(scanFilter.GetData())){
  3906. //整个黑名单文件过滤
  3907. bool isBlackFile = false;
  3908. CSimpleStringA tempFileName = _GetFileName(scanFilter.GetData());//文件名称
  3909. CSimpleStringA tempFileDir = GetFileDirectory(scanFilter.GetData());//文件夹名称
  3910. list<CSimpleStringA>::iterator itorFile = m_FileBlacklist.begin();
  3911. while(itorFile!=m_FileBlacklist.end())
  3912. {
  3913. CSimpleStringA fileBlack = *itorFile;
  3914. //if(scanFilter.IsEndWith(fileBlack.GetData(),true)){
  3915. // isBlackFile=true;
  3916. // break;
  3917. //}
  3918. if(IsFileMatch(fileBlack.GetData(),tempFileName.GetData())){
  3919. isBlackFile=true;
  3920. break;
  3921. }
  3922. itorFile++;
  3923. }
  3924. if(isBlackFile){
  3925. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("BlackFile Filter don't add to md5 list, file = [%s]", scanFilter.GetData());
  3926. m_iBlackFileCount++;
  3927. }
  3928. //整个黑名单文件夹过滤
  3929. bool isBlackDir = false;
  3930. list<CSimpleStringA>::iterator itorDir = m_DirBlacklist.begin();
  3931. while(itorDir!=m_DirBlacklist.end())
  3932. {
  3933. CSimpleStringA dirBlack = *itorDir;
  3934. if(tempFileDir.IndexOf(dirBlack.GetData())>0){
  3935. isBlackDir=true;
  3936. break;
  3937. }
  3938. itorDir++;
  3939. }
  3940. if(isBlackDir){
  3941. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("BlackDir Filter don't add to md5 list, dir = [%s]", scanFilter.GetData());
  3942. }
  3943. if(!isBlackDir&&!isBlackFile){
  3944. //加入hash列表
  3945. CSimpleStringA strMD5Val;
  3946. ErrorCodeEnum rErrcode = SM3FileToStr(scanFilter, strMD5Val,false);
  3947. if (Error_Succeed != rErrcode)
  3948. {
  3949. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("%s get sm3 fail ,error:%d", scanFilter.GetData(), (int)rErrcode);
  3950. return rErrcode;
  3951. }
  3952. CSimpleStringA tempFileName = _GetFileName(scanFilter.GetData());
  3953. CSimpleStringA relativePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s",replaceDir.GetData(),tempFileName.GetData());
  3954. //Dbg("scan file succ [%s]",relativePath.GetData());
  3955. #ifdef RVC_OS_WIN
  3956. #else
  3957. Sleep(100);//加入时间碎片
  3958. #endif
  3959. m_fileSM3Map[relativePath] = strMD5Val;
  3960. }
  3961. }
  3962. }
  3963. return Error_Succeed;
  3964. }
  3965. ErrorCodeEnum CUpgradeTaskFSM::RollBackVersionCheck(string& failReason, CSimpleStringA& strErrMsg)
  3966. {
  3967. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin RollBackVersionCheck");
  3968. //获取新版本号
  3969. CSimpleStringA rootVerPath;
  3970. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("RootVer", rootVerPath);//获取version根路径
  3971. if (rc != Error_Succeed) {
  3972. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("RollBackVersionCheck fail , get RootVer path is fail");
  3973. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, "RollBackVersionCheck: get RootVer path is fail");
  3974. failReason = "C";
  3975. strErrMsg = "RollBackVersionCheck fail , get RootVer path is fail";
  3976. return Error_Bug;
  3977. }
  3978. CSimpleStringA strRollbackVersion = m_currentTask.pack_version;
  3979. CVersion curVer = m_currentVersion;//当前版本号
  3980. // 指定版本
  3981. DWORD dwMajor(0), dwMinor(0), dwRevision(0), dwBuild(0);
  3982. int n = sscanf(strRollbackVersion.GetData(), "%d.%d.%d.%d", &dwMajor, &dwMinor, &dwRevision, &dwBuild);
  3983. if (n != 4)
  3984. {
  3985. strErrMsg = CSimpleStringA::Format("指定回退版本号无效(%s)", strRollbackVersion.GetData());
  3986. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, CSimpleStringA::Format("specified Rollback version is invalid: [%s]", strRollbackVersion.GetData()).GetData());
  3987. failReason = "C";
  3988. strErrMsg = CSimpleStringA::Format("specified Rollback version is invalid: [%s]", strRollbackVersion.GetData());
  3989. return Error_Bug;
  3990. }
  3991. else {
  3992. m_currentTask.NewVersion = strRollbackVersion;//赋值回退版本号给内存变量
  3993. }
  3994. if (curVer.ToString() == strRollbackVersion) {
  3995. //版本号相同,
  3996. strErrMsg = CSimpleStringA::Format("指定回退版本号与当前版本相同,无效(配置:%s, 当前:%s)", strRollbackVersion.GetData(), curVer.ToString().GetData());
  3997. failReason = "X";
  3998. return Error_Unexpect;
  3999. }
  4000. else if (dwMajor > curVer.GetMajor()
  4001. || (dwMajor == curVer.GetMajor() && dwMinor > curVer.GetMinor())
  4002. || (dwMajor == curVer.GetMajor() && dwMinor == curVer.GetMinor() && dwRevision > curVer.GetRevision())
  4003. || (dwMajor == curVer.GetMajor() && dwMinor == curVer.GetMinor() && dwRevision == curVer.GetRevision() && dwBuild > curVer.GetBuild())) {
  4004. //升级版本大于当前版本
  4005. strErrMsg = CSimpleStringA::Format("指定回退版本号大于当前版本号(配置:%s,当前:%s)", strRollbackVersion.GetData(), curVer.ToString().GetData());
  4006. failReason = "X";
  4007. return Error_Unexpect;
  4008. }
  4009. //回退版本是否存在
  4010. CSimpleStringA strRollbackVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), m_currentTask.NewVersion.GetData());
  4011. if (!ExistsDirA(strRollbackVerPath.GetData())) {
  4012. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, CSimpleStringA::Format("RollBackVersionCheck fail, strRollbackVerPath =%s is not exist,fail", strRollbackVerPath.GetData()).GetData());
  4013. strErrMsg = CSimpleStringA::Format("指定回退版本文件夹不存在(配置:%s)", strRollbackVersion.GetData());
  4014. failReason = "X";
  4015. return Error_Unexpect;
  4016. }
  4017. return Error_Succeed;
  4018. }
  4019. void CUpgradeTaskFSM::judgeTaskClass()
  4020. {
  4021. m_TempDepUpgradeFlag = false;//每次安装前初始化。
  4022. //临时方案判定是适配器升级还是应用升级
  4023. if (isTerminalInstall()) {
  4024. //安装时判断
  4025. if (m_currentTask.pack_name.IndexOf("adapter") != -1) {
  4026. m_TempDepUpgradeFlag = true;
  4027. }
  4028. }
  4029. else {
  4030. //非安装时判断:判断只有一条拷贝dep路径的策略
  4031. if (!m_currentTask.install_step.empty()) {
  4032. int iCount = m_currentTask.install_step.size();
  4033. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("install cmd size = %d", iCount);
  4034. if (iCount == 1) {
  4035. CInstallStep* tmp = m_currentTask.install_step.front();
  4036. if (tmp->cmdType == "Copy") {
  4037. if (tmp->srcPathType == "3") {
  4038. CSimpleStringA depStr = "";
  4039. depStr.Append(SPLIT_SLASH_STR).Append("dep").Append(SPLIT_SLASH_STR); //win :\dep\ uos: /dep/
  4040. if (tmp->srcPath.IndexOf(depStr.GetData()) != -1) {
  4041. m_TempDepUpgradeFlag = true;
  4042. }
  4043. }
  4044. }
  4045. }
  4046. }
  4047. }
  4048. }
  4049. ErrorCodeEnum CUpgradeTaskFSM::judgeRollbackTaskClass(CSimpleStringA verDirName, string& failReason, CSimpleStringA& strErrMsg)
  4050. {
  4051. //搜索终端应用目录,适配器应用目录,
  4052. CSimpleStringA depPathStr(true);
  4053. CSimpleStringA verPathStr(true);
  4054. ErrorCodeEnum Err = GetEntityBase()->GetFunction()->GetPath("DepBase", depPathStr);
  4055. if (Err != Error_Succeed) {
  4056. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("judgeRollbackTaskClass fail , get DepBase path is fail");
  4057. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, "judgeRollbackTaskClass: get DepBase path is fail");
  4058. failReason = "C";
  4059. strErrMsg = "judgeRollbackTaskClass fail , get DepBase path is fail";
  4060. return Error_Bug;
  4061. }
  4062. Err = m_pEntity->GetFunction()->GetPath("RootVer", verPathStr);
  4063. if (Err != Error_Succeed) {
  4064. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("judgeRollbackTaskClass fail , get RootVer path is fail");
  4065. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, "judgeRollbackTaskClass: get RootVer path is fail");
  4066. failReason = "C";
  4067. strErrMsg = "judgeRollbackTaskClass fail , get RootVer path is fail";
  4068. return Error_Bug;
  4069. }
  4070. CSimpleStringA strDestDir = "";
  4071. if (DirIsExist(verPathStr.GetData(), verDirName.GetData())) {
  4072. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("app version dir find is ok,dir=%s", verDirName.GetData());
  4073. m_TempDepUpgradeFlag = false;//应用版本
  4074. return Error_Succeed;
  4075. }
  4076. else if (DirIsExist(depPathStr.GetData(), verDirName.GetData())) {
  4077. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("dep version dir find is ok, dir=%s", verDirName.GetData());
  4078. m_TempDepUpgradeFlag = true;//适配器版本
  4079. return Error_Succeed;
  4080. }
  4081. else {
  4082. CSimpleStringA errStr = CSimpleStringA::Format("judgeRollbackTaskClass fail, RollbackVerRootPath =[%s|%s] not find version dir [%s],fail", verPathStr.GetData(), depPathStr.GetData(), verDirName.GetData());
  4083. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, errStr.GetData());
  4084. strErrMsg = CSimpleStringA::Format("指定回退版本文件夹不存在(配置:%s)", verDirName.GetData());
  4085. failReason = "X";
  4086. return Error_Unexpect;//不存在
  4087. }
  4088. }
  4089. bool CUpgradeTaskFSM::DirIsExist(const char* scanPath, const char* dirName)
  4090. {
  4091. CSimpleStringA strDestDir = "";
  4092. if (dirName == NULL||scanPath==NULL) {
  4093. return false;
  4094. }
  4095. auto subdirs = fileutil_get_sub_dirs(scanPath);
  4096. if (subdirs != NULL)
  4097. {
  4098. bool isMatch = false;
  4099. for (int i = 0; i < subdirs->nelts; ++i)
  4100. {
  4101. const char* dir = ARRAY_IDX(subdirs, i, char*);
  4102. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("dir=%s,dirName=%s", dir, dirName);
  4103. CSimpleStringA dirNameStr = dirName;
  4104. if (dirNameStr.Compare(_GetFileName(dir))==0) {
  4105. isMatch = true;
  4106. break;
  4107. }
  4108. }
  4109. toolkit_array_free2(subdirs);
  4110. return isMatch;
  4111. }
  4112. else {
  4113. return false;
  4114. }
  4115. }
  4116. int CUpgradeTaskFSM::ExecDepInstall(CSimpleStringA& strErrMsg)
  4117. {
  4118. if (!isTerminalInstall()) {
  4119. //升级
  4120. if (!m_currentTask.pack_version.IsNullOrEmpty()) {
  4121. CSimpleStringA strUpgradeDepVersion = m_currentTask.pack_version;
  4122. CSimpleStringA latterDepVersion;//要更新到的版本号
  4123. CSimpleStringA curVer = m_currentDepVersion;//当前版本号
  4124. // 指定版本
  4125. latterDepVersion = strUpgradeDepVersion.GetData();
  4126. if (curVer == strUpgradeDepVersion) {
  4127. //版本号相同
  4128. strErrMsg = CSimpleStringA::Format("指定升级适配器版本号与当前版本相同,无效(配置:%s, 当前:%s)", strUpgradeDepVersion.GetData(), curVer.GetData());
  4129. return 0;
  4130. }
  4131. CSimpleStringA strErrInfo = "";
  4132. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin CreateNewDepVersion,version[%s],pack[%s]", strUpgradeDepVersion.GetData(), m_currentTask.pack_name.GetData());
  4133. ErrorCodeEnum rc = CreateNewDepVersion(latterDepVersion, strErrInfo);
  4134. if (rc != Error_Succeed)
  4135. {
  4136. strErrMsg = CSimpleStringA::Format("CreateNewDepVersion fail:%s", strErrInfo.GetData());
  4137. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateNewDepVersion,version[%s] fail:%s", strUpgradeDepVersion.GetData(), strErrInfo.GetData());
  4138. //清理临时版本文件夹
  4139. ClearUpgradeFailDepVersion(latterDepVersion);
  4140. return 0;
  4141. }
  4142. else {
  4143. m_currentTask.depNewVersion = strUpgradeDepVersion;//赋值升级适配器版本号给内存变量
  4144. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateNewDepVersion,version[%s] succ , pack[%s]", strUpgradeDepVersion.GetData(), m_currentTask.pack_name.GetData());
  4145. }
  4146. }
  4147. else {
  4148. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecDepInstall fail ,pack_version is null");
  4149. strErrMsg = CSimpleStringA::Format("ExecDepInstall fail ,pack_version is null");
  4150. return 0;
  4151. }
  4152. }
  4153. else {
  4154. //新安装,无值则填写默认初始版本号
  4155. if (m_currentTask.pack_version.IsNullOrEmpty()) {
  4156. m_currentTask.pack_version = "0.0.0.1";//新安装,默认初始适配器版本
  4157. m_currentTask.depNewVersion = "0.0.0.1";
  4158. }
  4159. else {
  4160. m_currentTask.depNewVersion = m_currentTask.pack_version.GetData();
  4161. }
  4162. if (m_installStopCopyDep) {
  4163. //不需要拷贝dep公共库
  4164. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("installStopCopyDep is %d , No need to copy common Dep dir", m_installStopCopyDep);
  4165. }
  4166. else {
  4167. //兼容拷贝dep公共库
  4168. CSimpleStringA strErrInfo = "";
  4169. //DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin CreateInstallDepVersion,version[%s],pack[%s]", m_currentTask.depNewVersion.GetData(), m_currentTask.pack_name.GetData());
  4170. ErrorCodeEnum rc = CreateInstallDepVersion(m_currentTask.depNewVersion, strErrInfo);
  4171. if (rc != Error_Succeed)
  4172. {
  4173. strErrMsg = CSimpleStringA::Format("CreateInstallDepVersion fail:%s", strErrInfo.GetData());
  4174. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateInstallDepVersion,version[%s] fail:%s", m_currentTask.depNewVersion.GetData(), strErrInfo.GetData());
  4175. //清理临时版本文件夹
  4176. ClearUpgradeFailDepVersion(m_currentTask.depNewVersion);
  4177. return 0;
  4178. }
  4179. else {
  4180. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CreateInstallDepVersion,version[%s] succ , pack[%s]", m_currentTask.depNewVersion.GetData(), m_currentTask.pack_name.GetData());
  4181. }
  4182. }
  4183. }
  4184. //循环执行安装命令
  4185. int iRet = 1;//默认是成功
  4186. if (!m_currentTask.install_step.empty()) {
  4187. int iCount = m_currentTask.install_step.size();
  4188. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("install Dep cmd size = %d", iCount);
  4189. for (int j = 0; j < iCount; j++) {
  4190. CInstallStep* tmp = m_currentTask.install_step.front();
  4191. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("=====begin Dep cmd=[%s]=====", tmp->cmdType.GetData());
  4192. //开始处理具体命令
  4193. if (tmp->cmdType == "Delete") {
  4194. bool ret = ExecDepDeleteCmd(strErrMsg, tmp);
  4195. if (!ret) {
  4196. //报错才判断
  4197. if (tmp->cmdFail == 0) {
  4198. iRet = 0;
  4199. break;//出错直接退出
  4200. }
  4201. else if (tmp->cmdFail == 1) {
  4202. //继续
  4203. }
  4204. else {
  4205. iRet = 0;
  4206. break;//其他值也退出
  4207. }
  4208. }
  4209. }
  4210. else if (tmp->cmdType == "Copy") {
  4211. bool ret = ExecDepCpoyCmd(strErrMsg, tmp);
  4212. if (!ret) {
  4213. //报错才判断
  4214. if (tmp->cmdFail == 0) {
  4215. iRet = 0;
  4216. break;//出错直接退出
  4217. }
  4218. else if (tmp->cmdFail == 1) {
  4219. //继续
  4220. }
  4221. else {
  4222. iRet = 0;
  4223. break;//其他值也退出
  4224. }
  4225. }
  4226. }
  4227. else if (tmp->cmdType == "Run") {
  4228. int retInt = ExecDepRunCmd(strErrMsg, tmp);
  4229. if (retInt == 0 || retInt == -1) {
  4230. //报错才判断
  4231. if (tmp->cmdFail == 0) {
  4232. iRet = retInt;//会有0,-1值
  4233. break;//出错直接退出,并返回体系外结果
  4234. }
  4235. else if (tmp->cmdFail == 1) {
  4236. //继续
  4237. }
  4238. else {
  4239. iRet = 0;
  4240. break;//其他值也退出
  4241. }
  4242. }
  4243. }
  4244. else {
  4245. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("cmd=[%s] is invalid", tmp->cmdType.GetData());
  4246. iRet = 0;
  4247. break;//命令无效直接退出
  4248. }
  4249. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("=====end Dep cmd=[%s]=====", tmp->cmdType.GetData());
  4250. m_currentTask.install_step.pop();//去除元素
  4251. delete tmp;//删除内存
  4252. }
  4253. }
  4254. return iRet;//返回处理结果
  4255. }
  4256. ErrorCodeEnum CUpgradeTaskFSM::CreateNewDepVersion(CSimpleStringA NewSoftwareVersion, CSimpleStringA& strErrInfo)
  4257. {
  4258. // 创建新适配器版本
  4259. CSimpleStringA rootDepPath;
  4260. ErrorCodeEnum rc = GetEntityBase()->GetFunction()->GetPath("DepBase", rootDepPath);//适配器根路径
  4261. if (rc != Error_Succeed) {
  4262. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new dep version fail , get DepBase path is fail");
  4263. strErrInfo = CSimpleStringA::Format("Create new dep version fail , get DepBase path is fail");
  4264. return Error_Bug;
  4265. }
  4266. CSimpleStringA strCurDepPath;
  4267. rc = m_pEntity->GetFunction()->GetPath("Dep", strCurDepPath);//获取当前适配器根路径
  4268. if (rc != Error_Succeed) {
  4269. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new dep version fail , get Dep path is fail");
  4270. strErrInfo = CSimpleStringA::Format("Create new dep version fail , get Dep path is fail");
  4271. return Error_Bug;
  4272. }
  4273. //新路径
  4274. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootDepPath.GetData(), NewSoftwareVersion.GetData());
  4275. if (m_currentDepVersion == NewSoftwareVersion)
  4276. {
  4277. LogWarn(Severity_Low, Error_Bug, ERR_TASK_INSTALL_VERSION_FAIL, CSimpleStringA::Format("new ver [%s] equals current ver [%s]", NewSoftwareVersion.GetData(), m_currentDepVersion.GetData()).GetData());
  4278. strErrInfo = CSimpleStringA::Format("new ver [%s] equals current ver [%s]", NewSoftwareVersion.GetData(), m_currentDepVersion.GetData());
  4279. return Error_Bug;
  4280. }
  4281. if (ExistsDirA(strNewVerPath.GetData()))
  4282. {
  4283. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("new version dir [%s] already exist, delete it", strNewVerPath.GetData());
  4284. if (!RemoveDirRecursiveA(strNewVerPath.GetData())) {
  4285. //TODO: CrossPlaform [Gifur@2025730]
  4286. #ifdef RVC_OS_WIN
  4287. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new dep version fail, delete already exist new dep version is fail:%d", GetLastError());
  4288. strErrInfo = CSimpleStringA::Format("Create new dep version fail, delete already exist new dep version is fail:%d", (int)GetLastError());
  4289. #else
  4290. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new dep version fail, delete already exist new dep version is fail:%d", errno);
  4291. strErrInfo = CSimpleStringA::Format("Create new dep version fail, delete already exist new dep version is fail:%d", errno);
  4292. #endif
  4293. return Error_Bug;
  4294. }
  4295. }
  4296. if (!CreateDirA(strNewVerPath.GetData(), TRUE))
  4297. {
  4298. //TODO: CrossPlaform [Gifur@2025730]
  4299. #ifdef RVC_OS_WIN
  4300. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new dep version dir fail: %d", GetLastError());
  4301. strErrInfo = CSimpleStringA::Format("create new dep version dir fail: %d", (int)GetLastError());
  4302. #else
  4303. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new dep version dir fail: %d", errno);
  4304. strErrInfo = CSimpleStringA::Format("create new dep version dir fail: %d", errno);
  4305. #endif
  4306. return Error_Unexpect;
  4307. }
  4308. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("create new dep version dir succ, dir = %s", strNewVerPath.GetData());
  4309. //增加错误信息字段,回传给上层应用
  4310. CSimpleStringA strErrMsg = "";
  4311. CSimpleStringA strSource = CSimpleStringA::Format("%s", strCurDepPath.GetData());
  4312. CSimpleStringA strDest = CSimpleStringA::Format("%s", strNewVerPath.GetData());
  4313. if (!RecursiveCopyDir(strSource, strDest, strErrMsg))
  4314. {
  4315. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir fail");
  4316. strErrInfo = CSimpleStringA::Format("copy dep dir fail: %s", strErrMsg.GetData());
  4317. return Error_Unexpect;
  4318. }
  4319. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir succ");
  4320. return Error_Succeed;
  4321. }
  4322. ErrorCodeEnum CUpgradeTaskFSM::CreateInstallDepVersion(CSimpleStringA NewSoftwareVersion, CSimpleStringA& strErrInfo)
  4323. {
  4324. LOG_FUNCTION();
  4325. CSimpleStringA strCurVerPath;
  4326. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("Base", strCurVerPath);//获取当前应用version路径
  4327. if (rc != Error_Succeed) {
  4328. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new Install dep version fail , get Base path is fail");
  4329. strErrInfo = CSimpleStringA::Format("Create new Install dep version fail , get Base path is fail");
  4330. return Error_Bug;
  4331. }
  4332. CSimpleStringA strCurrDepPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "dep", strCurVerPath.GetData());
  4333. if (ExistsDirA(strCurrDepPath.GetData())) {
  4334. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("new install common dep dir is exist, begin copy dir = %s", strCurrDepPath.GetData());
  4335. //存在则拷贝公共库dep文件
  4336. CSimpleStringA tempPath;
  4337. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("DepBase", tempPath);//例如:C:\Run\dep\ /opt/Run/dep/
  4338. if (Error_Succeed == Error) {
  4339. CSimpleStringA strNewDepPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", tempPath.GetData(), NewSoftwareVersion.GetData());
  4340. CSimpleStringA strErrMsg = "";
  4341. if (!RecursiveCopyDir(strCurrDepPath.GetData(), strNewDepPath.GetData(), strErrMsg))
  4342. {
  4343. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir fail");
  4344. strErrInfo = CSimpleStringA::Format("copy dep dir fail: %s", strErrMsg.GetData());
  4345. return Error_Unexpect;
  4346. }
  4347. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy dep dir succ");
  4348. return Error_Succeed;
  4349. }
  4350. else {
  4351. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("Create new Install dep version fail ,get DepBase path is fail");
  4352. strErrInfo = CSimpleStringA::Format("Create new Install dep version fail , get DepBase path is fail");
  4353. return Error_Bug;
  4354. }
  4355. }
  4356. else {
  4357. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("new install common dep dir is not exist, default return true,dir=%s", strCurrDepPath.GetData());
  4358. return Error_Succeed;
  4359. }
  4360. }
  4361. ErrorCodeEnum CUpgradeTaskFSM::ClearUpgradeFailDepVersion(CSimpleStringA NewSoftwareVersion)
  4362. {
  4363. CSimpleStringA rootDepPath;
  4364. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("DepBase", rootDepPath);//适配器根路径
  4365. if (rc != Error_Succeed) {
  4366. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailDepVersion fail , get DepBase path is fail");
  4367. return Error_Bug;
  4368. }
  4369. //新路径
  4370. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootDepPath.GetData(), NewSoftwareVersion.GetData());
  4371. if (ExistsDirA(strNewVerPath.GetData()))
  4372. {
  4373. if (!RemoveDirRecursiveA(strNewVerPath.GetData())) {
  4374. //TODO: CrossPlaform [Gifur@2025730]
  4375. #ifdef RVC_OS_WIN
  4376. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailDepVersion fail, delete temp Version [%s] is fail:%d", strNewVerPath.GetData(), (int)GetLastError());
  4377. #else
  4378. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailDepVersion fail, delete temp Version [%s] is fail:%d", strNewVerPath.GetData(), errno);
  4379. #endif
  4380. return Error_Bug;
  4381. }
  4382. else {
  4383. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailDepVersion succ, delete temp Version [%s] ", strNewVerPath.GetData());
  4384. return Error_Succeed;
  4385. }
  4386. }
  4387. else {
  4388. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ClearUpgradeFailDepVersion succ, delete temp Version [%s] is not exist ", strNewVerPath.GetData());
  4389. return Error_Succeed;
  4390. }
  4391. }
  4392. ErrorCodeEnum CUpgradeTaskFSM::UpgradeDepToNewVersion()
  4393. {
  4394. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin UpgradeToDepNewVersion");
  4395. //获取新版本号
  4396. if (m_currentTask.depNewVersion.IsNullOrEmpty()) {
  4397. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("NewDepVersion is null, pack_version=[%s] is not null,UpgradeToNewDepVersion fail, taskID=[%s]", m_currentTask.pack_version.GetData(), m_currentTask.upgradeTaskId.GetData());
  4398. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewDepVersion fail, NewDepVersion is null, pack_version [%s] is not null,fail", m_currentTask.pack_version.GetData()).GetData());
  4399. return Error_Unexpect;//这种情况有问题,需要清除此任务
  4400. }
  4401. else {
  4402. ErrorCodeEnum ec = Error_Exception;
  4403. if (isTerminalInstall()) {
  4404. //安装需要刷新适配器路径到框架。
  4405. ec = GetEntityBase()->GetFunction()->GetPrivilegeFunction()->RewriteDepVersion(m_currentTask.depNewVersion, true);
  4406. }
  4407. else {
  4408. ec = GetEntityBase()->GetFunction()->GetPrivilegeFunction()->RewriteDepVersion(m_currentTask.depNewVersion, false);
  4409. }
  4410. if (ec != Error_Succeed) {
  4411. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("UpgradeDepToNewVersion fail, RewriteDepVersion version %s return %s", m_currentTask.depNewVersion.GetData(),SpStrError(ec));
  4412. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_UPDATE_ACTIVE_FAIL, CSimpleStringA::Format("UpgradeToNewVersion: get RootVer path is fail", m_currentTask.depNewVersion.GetData(), SpStrError(ec)));
  4413. }
  4414. else {
  4415. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("write dep active.txt succ, version[%s]", m_currentTask.depNewVersion.GetData());
  4416. }
  4417. return ec;
  4418. }
  4419. }
  4420. ErrorCodeEnum CUpgradeTaskFSM::SwitchUpgradeDepNow()
  4421. {
  4422. //根据重启策略类型去重启
  4423. //重启策略类型(0:不启动 1:重启框架 2:重启系统)
  4424. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin SwitchUpgradeDepNow");
  4425. if (m_currentTask.reboot_type == 0) {
  4426. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reboot_type = 0 , no need Switch restart, taskID=[%s] ", m_currentTask.upgradeTaskId.GetData());
  4427. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  4428. }
  4429. else if (m_currentTask.reboot_type == 1) {
  4430. if (m_currentTask.task_type.Compare("U") == 0) {
  4431. //适配器版本升级
  4432. LogEvent(Severity_Middle, Event_DeviceAdapterUpgrade_Framework_Restart,
  4433. CSimpleStringA::Format("upgrade to new dep version [%s] succeed, request to restart framework , taskID=[%s]", m_currentTask.depNewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  4434. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  4435. }
  4436. else if (m_currentTask.task_type.Compare("R") == 0) {
  4437. //适配器版本回退
  4438. LogEvent(Severity_Middle, Event_DeviceAdapterRollBack_Framework_Restart,
  4439. CSimpleStringA::Format("rollback to old dep version [%s] succeed, request to restart framework , taskID=[%s]", m_currentTask.depNewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  4440. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  4441. }
  4442. }
  4443. else if (m_currentTask.reboot_type == 2) {
  4444. if (m_currentTask.task_type.Compare("U") == 0) {
  4445. LogEvent(Severity_Middle, Event_DeviceAdapterUpgrade_OS_Restart,
  4446. CSimpleStringA::Format("upgrade to new dep version [%s] succeed, request to restart OS, taskID=[%s]", m_currentTask.depNewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  4447. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  4448. }
  4449. else {
  4450. LogEvent(Severity_Middle, Event_DeviceAdapterRollBack_OS_Restart,
  4451. CSimpleStringA::Format("rollback to old dep version [%s] succeed, request to restart OS, taskID=[%s]", m_currentTask.depNewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  4452. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  4453. }
  4454. }
  4455. else {
  4456. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reSetType=[%d] is invalid ,upgrade to new dep version [%s] fail, taskID=[%s]", m_currentTask.reboot_type, m_currentTask.depNewVersion.GetData(), m_currentTask.upgradeTaskId.GetData());
  4457. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  4458. }
  4459. // 强制刷新日志
  4460. m_pEntity->GetFunction()->FlushLogFile();
  4461. return Error_Succeed;
  4462. }
  4463. ErrorCodeEnum CUpgradeTaskFSM::RollBackDepVersionCheck(string& failReason, CSimpleStringA& strErrMsg)
  4464. {
  4465. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin RollBackDepVersionCheck");
  4466. //获取新版本号
  4467. CSimpleStringA rootVerPath;
  4468. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("DepBase", rootVerPath);//获取version根路径
  4469. if (rc != Error_Succeed) {
  4470. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("RollBackDepVersionCheck fail , get DepBase path is fail");
  4471. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, "RollBackDepVersionCheck: get DepBase path is fail");
  4472. failReason = "C";
  4473. strErrMsg = "RollBackDepVersionCheck fail , get DepBase path is fail";
  4474. return Error_Bug;
  4475. }
  4476. CSimpleStringA strRollbackVersion = m_currentTask.pack_version;
  4477. CSimpleStringA curDepVer = m_currentDepVersion;//当前版本号
  4478. // 指定版本
  4479. m_currentTask.depNewVersion = strRollbackVersion;//赋值适配器回退版本号给内存变量
  4480. if (curDepVer == strRollbackVersion) {
  4481. //版本号相同,
  4482. strErrMsg = CSimpleStringA::Format("指定回退版本号与当前版本相同,无效(配置:%s, 当前:%s)", strRollbackVersion.GetData(), curDepVer.GetData());
  4483. failReason = "X";
  4484. return Error_Unexpect;
  4485. }
  4486. //回退版本是否存在
  4487. CSimpleStringA strRollbackVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), m_currentTask.depNewVersion.GetData());
  4488. if (!ExistsDirA(strRollbackVerPath.GetData())) {
  4489. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLLBACK_VERSION_CHECK_FAIL, CSimpleStringA::Format("RollBackVersionCheck fail, strRollbackVerPath =%s is not exist,fail", strRollbackVerPath.GetData()).GetData());
  4490. strErrMsg = CSimpleStringA::Format("指定回退版本文件夹不存在(配置:%s)", strRollbackVersion.GetData());
  4491. failReason = "X";
  4492. return Error_Unexpect;
  4493. }
  4494. return Error_Succeed;
  4495. }
  4496. ErrorCodeEnum CUpgradeTaskFSM::RollBackToHistoryDepVersion(CSimpleStringA historyDepVersion)
  4497. {
  4498. //判断是否为空
  4499. if (historyDepVersion.IsNullOrEmpty())
  4500. {
  4501. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, "RollBackToHistoryDepVersion fail , historyDepVersion is null");
  4502. return Error_Param;
  4503. }
  4504. // 判断目标版本是否等于当前版本
  4505. if (historyDepVersion == m_currentDepVersion) {
  4506. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("history version [%s] >= current version [%s]", historyDepVersion.GetData(), m_currentDepVersion.GetData()).GetData());
  4507. return Error_Param;
  4508. }
  4509. // 检查对应文件夹是否存在
  4510. CSimpleStringA rootVerPath;
  4511. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("DepBase", rootVerPath);//获取version根路径
  4512. if (rc != Error_Succeed) {
  4513. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, "RollBackToHistoryDepVersion fail , get DepBase path is fail");
  4514. return Error_Bug;
  4515. }
  4516. CSimpleStringA strHisVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), historyDepVersion.GetData());
  4517. if (!ExistsDirA(strHisVerPath.GetData()))
  4518. {
  4519. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RollBackToHistoryDepVersion fail, history version dir [%s] not exists", strHisVerPath.GetData()).GetData());
  4520. return Error_NotExist;
  4521. }
  4522. ErrorCodeEnum ec = GetEntityBase()->GetFunction()->GetPrivilegeFunction()->RewriteDepVersion(historyDepVersion, false);
  4523. if (ec != Error_Succeed) {
  4524. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("RollBackToHistoryDepVersion fail, RewriteDepVersion version %s return %s", strHisVerPath.GetData(), SpStrError(ec));
  4525. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_ROLL_BACK_ACTIVE_FAIL, CSimpleStringA::Format("RollBackToHistoryDepVersion fail, RewriteDepVersion version %s return %s", strHisVerPath.GetData(), SpStrError(ec)));
  4526. }
  4527. else {
  4528. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("set rollback to history dep version [%s] succeed", historyDepVersion.GetData());
  4529. }
  4530. return ec;
  4531. }
  4532. int CUpgradeTaskFSM::ExecDepRunCmd(CSimpleStringA& strErrMsg, CInstallStep* ins)
  4533. {
  4534. CSimpleStringA destPath;//目的路径
  4535. if (ins->dstPathType == "0") {
  4536. CSimpleStringA rootPath;
  4537. if (GetDepRootTypePath(1, rootPath)) {
  4538. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//相对路径
  4539. }
  4540. else {
  4541. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 1 is fail");
  4542. return false;
  4543. }
  4544. }
  4545. else if (ins->dstPathType == "1") {
  4546. destPath = ins->dstPath;//绝对路径
  4547. }
  4548. else if (ins->dstPathType == "2") {
  4549. CSimpleStringA rootPath;
  4550. if (GetDepRootTypePath(2, rootPath)) {
  4551. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//当前版本相对路径
  4552. }
  4553. else {
  4554. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 2 is fail");
  4555. return false;
  4556. }
  4557. }
  4558. else if (ins->dstPathType == "3") {
  4559. CSimpleStringA rootPath;
  4560. if (GetDepRootTypePath(3, rootPath)) {
  4561. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//升级版本相对路径
  4562. }
  4563. else {
  4564. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 3 is fail");
  4565. return false;
  4566. }
  4567. }
  4568. else if (ins->dstPathType == "4") {
  4569. CSimpleStringA rootPath;
  4570. if (GetDepRootTypePath(4, rootPath)) {
  4571. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//升级包相对路径
  4572. }
  4573. else {
  4574. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPath GetRootTypePath 4 is fail");
  4575. return false;
  4576. }
  4577. }
  4578. else {
  4579. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd destPathType is invalid %s", ins->dstPathType.GetData());
  4580. strErrMsg = CSimpleStringA::Format("ExecRunCmd destPathType is invalid %s", ins->dstPathType.GetData());
  4581. return false;//不支持的路径类型
  4582. }
  4583. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd destPath is %s", destPath.GetData());
  4584. // ToRun=install.bat
  4585. if (ExistsFileA(destPath.GetData()))
  4586. {
  4587. ostuile_void_ptr store = osutil_sure_redirect_32sys_in_wow64();
  4588. string sysPathStr = destPath.GetData();
  4589. CSimpleStringA err = "";
  4590. int ret = system_on(sysPathStr, true, err);
  4591. osutil_reset_redirect_32sys_in_wow64(&store);
  4592. if (ret != 0) {
  4593. //程序执行异常,结果未知
  4594. //TODO: CrossPlaform [Gifur@2025730]
  4595. #ifdef RVC_OS_WIN
  4596. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, system_on exec unknown ,err=%s", err.GetData());
  4597. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail, system_on exec unknown ,err=%s", err.GetData());
  4598. #else
  4599. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, system_on exec unknown , ret=%d, err=%s", ret, strerror(errno));
  4600. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail, system_on exec unknown ret=%d, err=%s", ret, strerror(errno));
  4601. #endif
  4602. return -1;//未知结果返回
  4603. }
  4604. else {
  4605. //进一步看返回结果文件判断是否成功
  4606. CSimpleStringA strUnzipPath = GetFileDirectory(destPath.GetData());
  4607. CSimpleStringA strResultLog = strUnzipPath + SPLIT_SLASH_STR + "result.log";//结果文件
  4608. CSimpleStringA eErrorCode = "";
  4609. //获取结果文件
  4610. if (!GetSysUpgradeResult(strResultLog, eErrorCode, strErrMsg))
  4611. {
  4612. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, get result from result.log fail, %s", strErrMsg.GetData());
  4613. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail ,get result from result.log fail, %s", strErrMsg.GetData());
  4614. return 0;//失败
  4615. }
  4616. if (eErrorCode == "0")
  4617. {
  4618. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is succ,[%s] ", destPath.GetData());
  4619. return 1;//成功
  4620. }
  4621. else {
  4622. //TODO: CrossPlaform [Gifur@2025730]
  4623. #ifdef RVC_OS_WIN
  4624. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail,errorCode = %s", eErrorCode.GetData());
  4625. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail,errorCode = %s", eErrorCode.GetData());
  4626. #else
  4627. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail,errorCode = %s,%s", eErrorCode.GetData(), strErrMsg.GetData());
  4628. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail,errorCode = %s,%s", eErrorCode.GetData(), strErrMsg.GetData());
  4629. #endif
  4630. return 0;//失败
  4631. }
  4632. }
  4633. }
  4634. else
  4635. {
  4636. strErrMsg = CSimpleStringA::Format("ExecRunCmd is fail, run file [%s] is not Exist", destPath.GetData());
  4637. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecRunCmd is fail, run file [%s] is not Exist", destPath.GetData());
  4638. return 0;//失败
  4639. }
  4640. }
  4641. bool CUpgradeTaskFSM::ExecDepDeleteCmd(CSimpleStringA& strErrMsg, CInstallStep* ins)
  4642. {
  4643. //只删除同一层目录下的文件和文件夹
  4644. CSimpleStringA destPath;//目的路径+通配符
  4645. if (ins->dstPathType == "0") {
  4646. CSimpleStringA rootPath;
  4647. if (GetDepRootTypePath(1, rootPath)) {
  4648. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//相对路径
  4649. }
  4650. else {
  4651. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPath GetRootTypePath 1 is fail");
  4652. return false;
  4653. }
  4654. }
  4655. else if (ins->dstPathType == "1") {
  4656. destPath = ins->dstPath;//绝对路径
  4657. }
  4658. else if (ins->dstPathType == "2") {
  4659. CSimpleStringA rootPath;
  4660. if (GetDepRootTypePath(2, rootPath)) {
  4661. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//当前版本相对路径
  4662. }
  4663. else {
  4664. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPath GetRootTypePath 2 is fail");
  4665. return false;
  4666. }
  4667. }
  4668. else if (ins->dstPathType == "3") {
  4669. CSimpleStringA rootPath;
  4670. if (GetDepRootTypePath(3, rootPath)) {
  4671. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//升级版本相对路径
  4672. }
  4673. else {
  4674. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPath GetRootTypePath 3 is fail");
  4675. return false;
  4676. }
  4677. }
  4678. else {
  4679. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecDeleteCmd destPathType is invalid %s", ins->dstPathType.GetData());
  4680. strErrMsg = CSimpleStringA::Format("ExecDeleteCmd destPathType is invalid %s", ins->dstPathType.GetData());
  4681. return false;//不支持的路径类型
  4682. }
  4683. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecDeleteCmd destPath is %s", destPath.GetData());
  4684. if (destPath.IndexOf("*") > 0) {
  4685. CSimpleStringA strFileFilter = _GetFileName(destPath);
  4686. CSimpleStringA strDestDir = destPath.SubString(0, destPath.GetLength() - strFileFilter.GetLength() - 1);
  4687. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match filter name [%s] in dir [%s]", strFileFilter.GetData(), strDestDir.GetData());
  4688. // 删除所有匹配文件
  4689. bool bRet = true;
  4690. auto arr = fileutil_get_sub_files(strDestDir.GetData());
  4691. if (arr != NULL)
  4692. {
  4693. for (int i = 0; i < arr->nelts; ++i)
  4694. {
  4695. char* file = ARRAY_IDX(arr, i, char*);
  4696. if (IsFileMatch(strFileFilter, _GetFileName(file)))
  4697. {
  4698. if (RemoveFileA(file)) {
  4699. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file succ: [%s]", file);
  4700. }
  4701. else {
  4702. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file fail: [%s]", file);
  4703. strErrMsg = CSimpleStringA::Format("delete match file fail: [%s]", file);
  4704. bRet = false;
  4705. break;
  4706. }
  4707. }
  4708. else
  4709. {
  4710. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ignore not match file: [%s]", file);
  4711. }
  4712. }
  4713. toolkit_array_free2(arr);
  4714. if (!bRet) {
  4715. return bRet;
  4716. }
  4717. }
  4718. // 删除所有匹配子目录
  4719. auto subdirs = fileutil_get_sub_dirs(strDestDir.GetData());
  4720. if (subdirs != NULL)
  4721. {
  4722. for (int i = 0; i < subdirs->nelts; ++i)
  4723. {
  4724. const char* dir = ARRAY_IDX(subdirs, i, char*);
  4725. if (IsFileMatch(strFileFilter, _GetFileName(dir)))
  4726. {
  4727. if (RemoveDirRecursive(dir)) {
  4728. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir succ: [%s]", dir);
  4729. }
  4730. else {
  4731. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir fail: [%s]", dir);
  4732. strErrMsg = CSimpleStringA::Format("delete match dir fail: [%s]", dir);
  4733. bRet = false;
  4734. break;
  4735. }
  4736. }
  4737. else
  4738. {
  4739. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ignore not match dir: [%s]", dir);
  4740. }
  4741. }
  4742. toolkit_array_free2(subdirs);
  4743. if (!bRet) {
  4744. return bRet;
  4745. }
  4746. }
  4747. return bRet;
  4748. }
  4749. else {
  4750. //直接删除文件
  4751. bool bRet = true;
  4752. if (ExistsFileA(destPath.GetData()))
  4753. {
  4754. if (RemoveFileA(destPath.GetData())) {
  4755. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file succ: [%s]", destPath.GetData());
  4756. }
  4757. else {
  4758. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match file fail: [%s]", destPath.GetData());
  4759. strErrMsg = CSimpleStringA::Format("delete match file fail: [%s]", destPath.GetData());
  4760. bRet = false;
  4761. }
  4762. return bRet;
  4763. }
  4764. else if (ExistsDirA(destPath.GetData()))
  4765. {
  4766. if (RemoveDirRecursiveA(destPath.GetData())) {
  4767. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir succ: [%s]", destPath.GetData());
  4768. }
  4769. else {
  4770. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete match dir fail: [%s]", destPath.GetData());
  4771. strErrMsg = CSimpleStringA::Format("delete match dir fail: [%s]", destPath.GetData());
  4772. bRet = false;
  4773. }
  4774. return bRet;
  4775. }
  4776. else
  4777. {
  4778. //指定文件不再则忽略删除此文件
  4779. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("delete file [%s] not exists,ignore delete file", destPath.GetData());
  4780. return bRet;
  4781. }
  4782. }
  4783. }
  4784. bool CUpgradeTaskFSM::ExecDepCpoyCmd(CSimpleStringA& strErrMsg, CInstallStep* ins)
  4785. {
  4786. CSimpleStringA sourcePath;//路径+通配符
  4787. if (ins->srcPathType == "0") {
  4788. CSimpleStringA rootPath = "";
  4789. if (GetDepRootTypePath(1, rootPath)) {
  4790. sourcePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->srcPath.GetData());//相对路径
  4791. }
  4792. else {
  4793. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd sourcePath GetRootTypePath 1 is fail");
  4794. return false;
  4795. }
  4796. }
  4797. else if (ins->srcPathType == "1") {
  4798. sourcePath = ins->srcPath;//绝对路径
  4799. }
  4800. else if (ins->srcPathType == "2") {
  4801. CSimpleStringA rootPath = "";
  4802. if (GetDepRootTypePath(2, rootPath)) {
  4803. sourcePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->srcPath.GetData());//当前版本相对路径
  4804. }
  4805. else {
  4806. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd sourcePath GetRootTypePath 2 is fail");
  4807. return false;
  4808. }
  4809. }
  4810. else if (ins->srcPathType == "3") {
  4811. CSimpleStringA rootPath = "";
  4812. if (GetDepRootTypePath(4, rootPath)) {
  4813. sourcePath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->srcPath.GetData());//升级包相对路径
  4814. }
  4815. else {
  4816. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd sourcePath GetRootTypePath 4 is fail");
  4817. return false;
  4818. }
  4819. }
  4820. else {
  4821. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd srcPathType is invalid %s", ins->srcPathType.GetData());
  4822. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd srcPathType is invalid %s", ins->srcPathType.GetData());
  4823. return false;//不支持的路径类型
  4824. }
  4825. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd sourcePath is %s", sourcePath.GetData());
  4826. CSimpleStringA destPath;//目的路径文件夹
  4827. if (ins->dstPathType == "0") {
  4828. CSimpleStringA rootPath = "";
  4829. if (GetDepRootTypePath(1, rootPath)) {
  4830. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//相对路径
  4831. }
  4832. else {
  4833. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd destPath GetRootTypePath 1 is fail");
  4834. return false;
  4835. }
  4836. }
  4837. else if (ins->dstPathType == "1") {
  4838. destPath = ins->dstPath;//绝对路径
  4839. }
  4840. else if (ins->dstPathType == "2") {
  4841. CSimpleStringA rootPath = "";
  4842. if (GetDepRootTypePath(2, rootPath)) {
  4843. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//当前版本相对路径
  4844. }
  4845. else {
  4846. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd destPath GetRootTypePath 2 is fail");
  4847. return false;
  4848. }
  4849. }
  4850. else if (ins->dstPathType == "3") {
  4851. CSimpleStringA rootPath = "";
  4852. if (GetDepRootTypePath(3, rootPath)) {
  4853. destPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootPath.GetData(), ins->dstPath.GetData());//升级版本相对路径
  4854. }
  4855. else {
  4856. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd destPath GetRootTypePath 3 is fail");
  4857. return false;
  4858. }
  4859. }
  4860. else {
  4861. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd dstPathType is invalid %s", ins->dstPathType.GetData());
  4862. strErrMsg = CSimpleStringA::Format("ExecCpoyCmd dstPathType is invalid %s", ins->dstPathType.GetData());
  4863. return false;//不支持的路径类型
  4864. }
  4865. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("ExecCpoyCmd destPath is %s", destPath.GetData());
  4866. if (sourcePath.IndexOf("*") > 0) {
  4867. //循环匹配拷贝
  4868. //找到要扫描的文件夹
  4869. CSimpleStringA strFileDir = GetFileDirectory(sourcePath.GetData());
  4870. //过滤通配条件
  4871. CSimpleStringA strFileFilter = _GetFileName(sourcePath.GetData());
  4872. list<CSimpleStringA> fileList;
  4873. //获取所有根文件夹下的文件相对路径:如mod\test.dll
  4874. RecursiveGetSubFiles(strFileDir.GetData(), strFileDir.GetData(), fileList);
  4875. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("find fileList =%d", fileList.size());
  4876. for (auto it = fileList.begin(); it != fileList.end(); it++)
  4877. {
  4878. if (IsFileMatch(strFileFilter, _GetFileName((const char*)(*it))))
  4879. {
  4880. CSimpleStringA strSrcName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", GetFileDirectory(sourcePath.GetData()).GetData(), (const char*)(*it));//源文件路径
  4881. CSimpleStringA strDestName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", destPath.GetData(), (const char*)(*it));//目标文件路径
  4882. //匹配成功,开始拷贝文件
  4883. CSimpleStringA errMsg = "";
  4884. ErrorCodeEnum rc = CopyFileOperate(strSrcName.GetData(), strDestName.GetData(), 3, errMsg);
  4885. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] to [%s] %s", strSrcName.GetData(),strDestName.GetData(), rc == Error_Succeed ? "succeed" : "fail");
  4886. if (rc != Error_Succeed)
  4887. {
  4888. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] fail: %s", strSrcName.GetData(), errMsg.GetData());
  4889. strErrMsg = CSimpleStringA::Format("copy file [%s] fail: %s", strSrcName.GetData(), errMsg.GetData());
  4890. return false;//报错
  4891. }
  4892. }
  4893. }
  4894. }
  4895. else {
  4896. //直接拷贝文件:考虑有些文件因智能对比不一定下发,故如果文件不存在,则默认忽略此策略
  4897. CSimpleStringA errMsg = "";
  4898. CSimpleStringA strDestName = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", destPath.GetData(), _GetFileName(sourcePath.GetData()));
  4899. if (ExistsFileA(sourcePath.GetData())) {
  4900. ErrorCodeEnum rc = CopyFileOperate(sourcePath.GetData(), strDestName.GetData(), 3, errMsg);
  4901. if (rc != Error_Succeed)
  4902. {
  4903. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] fail:%s", sourcePath.GetData(), errMsg.GetData());
  4904. strErrMsg = CSimpleStringA::Format("copy file [%s] fail:%s", sourcePath.GetData(), errMsg.GetData());
  4905. return false;//报错
  4906. }
  4907. }
  4908. else {
  4909. if (ins->srcPathType == "3") {
  4910. //升级包因智能升级不一定有指定的文件存在
  4911. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("拷贝指定的文件[%s]不存在,因智能升级对比过滤,故忽略此文件拷贝", sourcePath.GetData());
  4912. }
  4913. else {
  4914. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("copy file [%s] fail:file to copy not exist", sourcePath.GetData());
  4915. strErrMsg = CSimpleStringA::Format("copy file [%s] fail:file to copy not exist", sourcePath.GetData());
  4916. return false;//报错
  4917. }
  4918. }
  4919. }
  4920. return true;
  4921. }
  4922. bool CUpgradeTaskFSM::GetDepRootTypePath(int rootType, CSimpleStringA& Path)
  4923. {
  4924. //适配器
  4925. CSimpleStringA tempPath;
  4926. if (isTerminalInstall() && (rootType == 2 || rootType == 3)) {
  4927. //当是安装状态时,此路径取pack_version值,如果为空,默认用0.0.0.1 版本号
  4928. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("DepBase", tempPath);//例如:C:\Run\dep\4.9.1.1 /opt/Run/dep/4.9.1.1
  4929. if (Error_Succeed == Error) {
  4930. if (!tempPath.IsNullOrEmpty()) {
  4931. if (!m_currentTask.pack_version.IsNullOrEmpty()) {
  4932. Path = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", tempPath.GetData(), m_currentTask.pack_version.GetData());
  4933. }
  4934. else {
  4935. Path = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", tempPath.GetData(), "0.0.0.1");
  4936. }
  4937. return true;
  4938. }
  4939. else {
  4940. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get currDepVer path is null");
  4941. return false;
  4942. }
  4943. }
  4944. else {
  4945. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get currDepVer path is error");
  4946. return false;
  4947. }
  4948. }
  4949. if (rootType == 1) {
  4950. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Root", tempPath);//例如:C:\Run /opt/Run
  4951. if (Error_Succeed == Error) {
  4952. if (!tempPath.IsNullOrEmpty()) {
  4953. string runPathStr = tempPath.GetData();
  4954. int pos = runPathStr.find_last_of(SPLIT_SLASH);
  4955. Path = runPathStr.substr(0, pos).c_str();
  4956. }
  4957. else {
  4958. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get Root path is null");
  4959. return false;
  4960. }
  4961. }
  4962. else {
  4963. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get Root path is error");
  4964. return false;
  4965. }
  4966. }
  4967. else if (rootType == 2) {
  4968. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Dep", tempPath);//例如:C:\Run\dep\4.8.1.1 /opt/Run/dep/4.8.1.1
  4969. if (Error_Succeed == Error) {
  4970. if (!tempPath.IsNullOrEmpty()) {
  4971. Path = tempPath;
  4972. }
  4973. else {
  4974. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get currDepVer path is null");
  4975. return false;
  4976. }
  4977. }
  4978. else {
  4979. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get currDepVer path is error");
  4980. return false;
  4981. }
  4982. }
  4983. else if (rootType == 3) {
  4984. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("DepBase", tempPath);//例如:C:\Run\dep\4.9.1.1 /opt/Run/dep/4.9.1.1
  4985. if (Error_Succeed == Error) {
  4986. if (!tempPath.IsNullOrEmpty()) {
  4987. Path = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", tempPath.GetData(), m_currentTask.pack_version.GetData());
  4988. }
  4989. else {
  4990. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get upgradeVer path is null");
  4991. return false;
  4992. }
  4993. }
  4994. else {
  4995. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get upgradeVer path is error");
  4996. return false;
  4997. }
  4998. }
  4999. else if (rootType == 4) {
  5000. ErrorCodeEnum Error = m_pEntity->GetFunction()->GetPath("Downloads", tempPath);//例如:C:\rvc\Downloads\4.8.1.1 /opt/rvc/Downloads/4.8.1.1
  5001. if (Error_Succeed == Error) {
  5002. if (!tempPath.IsNullOrEmpty()) {
  5003. CSimpleStringA strUnzipDir;
  5004. strUnzipDir = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", tempPath.GetData(), m_currentTask.pack_name.GetData());
  5005. //解压去除了.zip后缀
  5006. if (strUnzipDir.IsEndWith(".zip")) {
  5007. Path = strUnzipDir.SubString(0, strUnzipDir.GetLength() - 4);
  5008. }
  5009. else {
  5010. Path = strUnzipDir;
  5011. }
  5012. }
  5013. else {
  5014. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get upgradePack path is null");
  5015. return false;
  5016. }
  5017. }
  5018. else {
  5019. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("get upgradePack path is error");
  5020. return false;
  5021. }
  5022. }
  5023. return true;
  5024. }
  5025. CSimpleStringA CUpgradeTaskFSM::getAlarmCurrVersion()
  5026. {
  5027. //一期因无法从策略获取适配器升级还是应用升级,默认返回当前应用版本数据,待二期再做区分
  5028. return m_currentVersion.ToString();
  5029. }
  5030. ErrorCodeEnum CUpgradeTaskFSM::SetNewVersionPathToFirewall()
  5031. {
  5032. if (m_TempDepUpgradeFlag) {
  5033. return Error_Succeed;//适配器升级,不设置
  5034. }
  5035. if (isTerminalInstall()) {
  5036. return Error_Succeed;//安装新设备,不设置
  5037. }
  5038. if (!m_currentTask.NewVersion.IsNullOrEmpty()) {
  5039. //升级应用版本
  5040. CSimpleStringA rootVerPath;
  5041. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetPath("RootVer", rootVerPath);//获取version根路径
  5042. if (rc != Error_Succeed) {
  5043. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("SetNewVersionPathToFirewall fail , get RootVer path is fail");
  5044. return Error_Bug;
  5045. }
  5046. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s" SPLIT_SLASH_STR "%s", rootVerPath.GetData(), m_currentTask.NewVersion.GetData());
  5047. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("SetNewVersionPathToFirewall path = %s", strNewVerPath.GetData());
  5048. return m_pEntity->GetFunction()->SetNewPathToFirewall(strNewVerPath);
  5049. }
  5050. else {
  5051. return Error_Succeed;
  5052. }
  5053. }
  5054. ErrorCodeEnum CUpgradeTaskFSM::SM3FileToStr(CSimpleStringA strFilePath, CSimpleStringA &strSM3,bool isSub)
  5055. {
  5056. if(strFilePath.IsNullOrEmpty()){
  5057. return Error_Null;
  5058. }
  5059. int nlen = strlen(strFilePath.GetData());
  5060. char* pchar = new char[nlen+1];
  5061. strcpy(pchar,strFilePath.GetData());
  5062. BYTE md5[32]={0};
  5063. try
  5064. {
  5065. if(SM3File(pchar,md5)){
  5066. delete[] pchar;
  5067. //获取16进制的字符串
  5068. if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
  5069. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("sm3 to hex str fail,file=%s",strFilePath.GetData());
  5070. return Error_Unexpect;
  5071. }
  5072. return Error_Succeed;
  5073. }else{
  5074. delete[] pchar;
  5075. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("sm3 encrypt fail ,file=%s",strFilePath.GetData());
  5076. return Error_Unexpect;
  5077. }
  5078. }
  5079. catch (...)
  5080. {
  5081. delete[] pchar;
  5082. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("sm3 encrypt exception ,file=%s",strFilePath.GetData());
  5083. return Error_Exception;
  5084. }
  5085. }
  5086. ErrorCodeEnum CUpgradeTaskFSM::SM3DataToStr(CSimpleStringA strData, CSimpleStringA &strSM3,bool isSub)
  5087. {
  5088. if(strData.IsNullOrEmpty()){
  5089. return Error_Null;
  5090. }
  5091. BYTE md5[32]={0};
  5092. try
  5093. {
  5094. if(SM3Hash((BYTE*)strData.GetData(),strlen(strData.GetData()),md5)){
  5095. //获取16进制的字符串
  5096. if(Error_Succeed!=SM3_Str(strSM3,md5,isSub)){
  5097. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("sm3 to hex str fail");
  5098. return Error_Unexpect;
  5099. }
  5100. return Error_Succeed;
  5101. }else{
  5102. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("sm3 encrypt fail");
  5103. return Error_Unexpect;
  5104. }
  5105. }
  5106. catch (...)
  5107. {
  5108. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("sm3 encrypt exception");
  5109. return Error_Exception;
  5110. }
  5111. }
  5112. ErrorCodeEnum CUpgradeTaskFSM::SM3_Str(CSimpleStringA &strSM3,BYTE * SM3Byte,bool isSub)
  5113. {
  5114. if(SM3Byte == NULL){
  5115. return Error_Exception;
  5116. }
  5117. int SM3_len=64;
  5118. if(isSub){
  5119. SM3_len=32;
  5120. }
  5121. int i;
  5122. char* file_SM3 = (char*)malloc((SM3_len + 1) * sizeof(char));
  5123. if(file_SM3 == NULL)
  5124. {
  5125. fprintf(stderr, "SM3 malloc failed.\n");
  5126. return Error_Exception;
  5127. }
  5128. memset(file_SM3, 0, (SM3_len + 1));
  5129. if(SM3_len == 32)
  5130. {
  5131. for(i=0; i<16; i++)
  5132. { //SM3截取中间16位字节,8-24位字节数,小写
  5133. sprintf(&file_SM3[i*2], "%02x", SM3Byte[i+8]);
  5134. }
  5135. }
  5136. else if(SM3_len == 64)
  5137. {
  5138. for(i=0; i<32; i++)
  5139. {
  5140. sprintf(&file_SM3[i*2], "%02x", SM3Byte[i]);
  5141. }
  5142. }
  5143. strSM3=file_SM3;
  5144. free(file_SM3);
  5145. return Error_Succeed;
  5146. }
  5147. bool CUpgradeTaskFSM::IsPackDownloaded(const char *packPath,const char* packHash,long packLen)
  5148. {
  5149. if(!ExistsFileA(packPath)){
  5150. return false;//文件不存在,下载
  5151. }
  5152. //计算hash值
  5153. CSimpleStringA strMD5Val;
  5154. ErrorCodeEnum rc = SM3FileToStr(packPath, strMD5Val,false);
  5155. if (Error_Succeed != rc)
  5156. {
  5157. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s get download pack sm3 fail ,error:%d", packPath, (int)rc);
  5158. return false;//获取hash码失败,下载
  5159. }
  5160. //不区分大小写比较文件hash
  5161. #ifdef RVC_OS_WIN
  5162. if(stricmp(strMD5Val.GetData(),packHash)==0){
  5163. #else
  5164. if(strcmp(strMD5Val.GetData(),packHash)==0){
  5165. #endif
  5166. return true;//相同,不下载
  5167. }else{
  5168. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("%s sm3[%s] is no equal upgrade sm3[%s], retry download.", packPath, strMD5Val.GetData(), packHash);
  5169. return false;//不相同,下载
  5170. }
  5171. }
  5172. bool CUpgradeTaskFSM::DownloadFileWrite(byte* content,long contentLen,const char* filePath)
  5173. {
  5174. if(contentLen==0){
  5175. return true;
  5176. }
  5177. FILE* tempFp = fopen(filePath,"ab");
  5178. if(tempFp==NULL){
  5179. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DownloadFileWrite open file fail");
  5180. return false;
  5181. }
  5182. int ret = fwrite(content,(size_t)contentLen,1,tempFp);
  5183. fclose(tempFp);
  5184. if(ret!=1){
  5185. //TODO: CrossPlaform [Gifur@2025730]
  5186. #ifdef RVC_OS_WIN
  5187. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DownloadFileWrite write file fail,ret = %d,errno = %d",ret,(int)GetLastError());
  5188. return false;
  5189. #else
  5190. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("DownloadFileWrite write file fail,ret = %d,errno = %d",ret, errno);
  5191. return false;
  5192. #endif
  5193. }
  5194. return true;
  5195. }
  5196. bool CUpgradeTaskFSM::CheckIfCanSwitchNow()
  5197. {
  5198. //对于终端初始化安装时,直接通过。
  5199. if (isTerminalInstall()) {
  5200. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow terminal new install,return true");
  5201. return true;
  5202. }
  5203. //非重启操作,则直接可以进行切换,不用等待时机
  5204. if(m_currentTask.reboot_type == 0){
  5205. return true;
  5206. }
  5207. CSimpleStringA strCustomerHandle;
  5208. CSimpleStringA strCallState;
  5209. // 无交互N、客户关注A、客户在控制终端C、柜员协助T、柜员代操作G
  5210. ErrorCodeEnum rc = m_pEntity->GetFunction()->GetSysVar("CustomerHandle", strCustomerHandle);
  5211. if(rc!=Error_Succeed){
  5212. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow get CustomerHandle value is fail ,err=%d",(int)rc);
  5213. return false;
  5214. }
  5215. // 离线O、尝试连线柜员C、免提通话H、连线失败F、断线B
  5216. rc = m_pEntity->GetFunction()->GetSysVar("CallState", strCallState);
  5217. if(rc!=Error_Succeed){
  5218. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow get CallState value is fail ,err=%d",(int)rc);
  5219. return false;
  5220. }
  5221. // M 首页
  5222. CSimpleStringA csUIState;
  5223. rc = m_pEntity->GetFunction()->GetSysVar("UIState", csUIState);
  5224. if (rc != Error_Succeed) {
  5225. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow get strUIState value is fail ,err=%d", (int)rc);
  5226. return false;
  5227. }
  5228. #ifdef RVC_OS_WIN
  5229. SYSTEMTIME st = {};
  5230. GetLocalTime(&st);
  5231. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("hour is %d",(int)st.wHour);
  5232. bool bOffworkTime = st.wHour < 8 || st.wHour >= 20;
  5233. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("hour is %d, strCustomerHandle=%s, strCallState=%s, strUIState=%s, bOffworkTime=%d",(int)st.wHour,strCustomerHandle.GetData(),strCallState.GetData(), csUIState.GetData(), (int)bOffworkTime);
  5234. #else
  5235. time_t nowTime = time(NULL);
  5236. tm nowTimeTm = { 0 };
  5237. localtime_r(&nowTime, &nowTimeTm);
  5238. //Dbg("hour is %d", (int)nowTimeTm.tm_hour);
  5239. bool bOffworkTime = nowTimeTm.tm_hour < 8 || nowTimeTm.tm_hour >= 20;
  5240. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("hour is %d, strCustomerHandle=%s, strCallState=%s, strUIState=%s, bOffworkTime=%d",(int)nowTimeTm.tm_hour,strCustomerHandle.GetData(),strCallState.GetData(), csUIState.GetData(), (int)bOffworkTime);
  5241. #endif
  5242. // 或者下班时间且非连线状态 @20151202
  5243. // 客户关注状态A,不允许切换 by zl 20191113
  5244. if (strCustomerHandle[0] == 'N' || (bOffworkTime && (strCallState[0] == 'O') && (csUIState.Compare("M") == 0))) {
  5245. return true;
  5246. }
  5247. else {
  5248. // 防止感知一直误报有人状态,加入以下特例:如果是一天当中首次从切换状态直接跳转过来的升级切换,并且当前是首页则进行切换
  5249. if (!bOffworkTime) {
  5250. CSimpleStringA strUIState;
  5251. // M 首页
  5252. rc = m_pEntity->GetFunction()->GetSysVar("UIState", strUIState);
  5253. if (rc != Error_Succeed) {
  5254. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow get strUIState value is fail ,err=%d", (int)rc);
  5255. return false;
  5256. }
  5257. #ifdef RVC_OS_WIN
  5258. CSimpleStringA nowStr = CSimpleStringA::Format("%d-%d-%d", st.wYear, st.wMonth, st.wDay).GetData();
  5259. #else
  5260. CSimpleStringA nowStr = CSimpleStringA::Format("%d-%d-%d", nowTimeTm.tm_year+1900, nowTimeTm.tm_mon+1, nowTimeTm.tm_mday).GetData();
  5261. #endif
  5262. //读取强制切换的日期
  5263. CSimpleStringA ForceSwitchDateStr = "";
  5264. ErrorCodeEnum rc = GetRunConfigStrValue("upgradeTask", "ForceSwitchDate", ForceSwitchDateStr);
  5265. if (rc != Error_Succeed) {
  5266. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow read from run config, read [upgradeTask][ForceSwitchDate] fail ,err=%d", (int)rc);
  5267. return false;
  5268. }
  5269. //首页并且切换状态直接进入
  5270. if (strUIState.Compare("M") == 0 && m_bSwitchFlag) {
  5271. //当天是否切换过
  5272. if (ForceSwitchDateStr.Compare(nowStr.GetData()) == 0) {
  5273. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow is not ForceSwitch, ForceSwitchDate[%s]", ForceSwitchDateStr.GetData());
  5274. return false;
  5275. }
  5276. else {
  5277. //直接切换,并写入强制切换日期
  5278. if (SetRunConfigStrValue("upgradeTask", "ForceSwitchDate", nowStr.GetData()) != Error_Succeed) {
  5279. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow write run config, write [upgradeTask][ForceSwitchDate] fail[%s],err=%d", nowStr.GetData(), (int)rc);
  5280. }
  5281. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow ForceSwitch, ForceSwitchDate[%s]", nowStr.GetData());
  5282. return true;
  5283. }
  5284. }
  5285. else {
  5286. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("CheckIfCanSwitchNow is not ForceSwitch, UIState=%s,m_bSwitchFlag=%d", strUIState.GetData(),(int)m_bSwitchFlag);
  5287. return false;
  5288. }
  5289. }
  5290. else {
  5291. return false;
  5292. }
  5293. }
  5294. }
  5295. ErrorCodeEnum CUpgradeTaskFSM::SwitchUpgradeNow()
  5296. {
  5297. //默认轻量包不重启系统和框架,体系外系统不重启系统,系统升级重启框架
  5298. //根据重启策略类型去重启
  5299. //重启策略类型(0:不启动 1:重启框架 2:重启系统)
  5300. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("begin SwitchUpgradeNow");
  5301. if (m_currentTask.reboot_type == 0) {
  5302. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reboot_type = 0 , no need Switch restart, taskID=[%s] ", m_currentTask.upgradeTaskId.GetData());
  5303. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  5304. }
  5305. else if (m_currentTask.reboot_type == 1) {
  5306. if (m_currentTask.NewVersion.IsNullOrEmpty()) {
  5307. //非版本升级
  5308. LogEvent(Severity_Middle, Event_Req_Framework_No_Upgrade_Restart,
  5309. CSimpleStringA::Format("upgrade to new version [%s] succeed, request to no upgrade restart framework , taskID=[%s]", m_currentVersion.ToString().GetData(), m_currentTask.upgradeTaskId.GetData()));
  5310. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  5311. }
  5312. else {
  5313. if (m_currentTask.task_type.Compare("U") == 0) {
  5314. //版本升级,健康有后续回退处理
  5315. LogEvent(Severity_Middle, Event_Req_Framework_Restart,
  5316. CSimpleStringA::Format("upgrade to new version [%s] succeed, request to restart framework , taskID=[%s]", m_currentTask.NewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  5317. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  5318. }
  5319. else if (m_currentTask.task_type.Compare("R") == 0) {
  5320. //版本回退,健康无后续回滚处理
  5321. LogEvent(Severity_Middle, Event_Req_Framework_Rollback_Restart,
  5322. CSimpleStringA::Format("rollback to old version [%s] succeed, request to restart framework , taskID=[%s]", m_currentTask.NewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  5323. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  5324. }
  5325. }
  5326. }
  5327. else if (m_currentTask.reboot_type == 2) {
  5328. if (m_currentTask.NewVersion.IsNullOrEmpty()) {
  5329. //非版本升级
  5330. LogEvent(Severity_Middle, Event_Req_OS_Restart,
  5331. CSimpleStringA::Format("SysPackInstall upgrade to new version [%s] succeed, request to restart OS, taskID=[%s]", m_currentVersion.ToString().GetData(), m_currentTask.upgradeTaskId.GetData()));
  5332. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  5333. }
  5334. else {
  5335. if (m_currentTask.task_type.Compare("U") == 0) {
  5336. LogEvent(Severity_Middle, Event_Req_OS_Upgrade_Restart,
  5337. CSimpleStringA::Format("upgrade to new version [%s] succeed, request to restart OS, taskID=[%s]", m_currentTask.NewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  5338. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  5339. }
  5340. else {
  5341. LogEvent(Severity_Middle, Event_Req_OS_Restart,
  5342. CSimpleStringA::Format("rollback to old version [%s] succeed, request to restart OS, taskID=[%s]", m_currentTask.NewVersion.GetData(), m_currentTask.upgradeTaskId.GetData()));
  5343. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_PreRestart));//进入S7
  5344. }
  5345. }
  5346. }
  5347. else {
  5348. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("reSetType=[%d] is invalid ,upgrade to new version [%s] fail, taskID=[%s]", m_currentTask.reboot_type, m_currentVersion.ToString().GetData(), m_currentTask.upgradeTaskId.GetData());
  5349. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  5350. }
  5351. // 强制刷新日志
  5352. m_pEntity->GetFunction()->FlushLogFile();
  5353. return Error_Succeed;
  5354. }
  5355. ErrorCodeEnum CUpgradeTaskFSM::DownloadProgress(long tempFileLen,long fileSumLen)
  5356. {
  5357. UpgradeDownloadProgress dProgress;
  5358. dProgress.currentFile = m_currentTask.pack_name;
  5359. dProgress.fileNum = 1;
  5360. dProgress.progress=0;
  5361. if(fileSumLen!=0){
  5362. //DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("DownloadProgress")("tempFileLen =%d , fileSumLen =%d",tempFileLen,fileSumLen);
  5363. dProgress.progress = (int)(tempFileLen/(fileSumLen/100));
  5364. if(dProgress.progress ==10||dProgress.progress ==20||dProgress.progress ==30||dProgress.progress ==40||dProgress.progress ==50
  5365. ||dProgress.progress ==60||dProgress.progress ==70||dProgress.progress ==80||dProgress.progress ==90)
  5366. {
  5367. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI("DownloadProgress")("download progress is %d ,tempFileLen =%d , fileSumLen =%d",dProgress.progress,tempFileLen,fileSumLen);
  5368. }
  5369. //Dbg("download file [%s] progress is %d",dProgress.currentFile.GetData(),dProgress.progress);
  5370. ErrorCodeEnum retError = SpSendBroadcast(GetEntityBase()->GetFunction(), SP_MSG_OF(UpgradeDownloadProgress), SP_MSG_SIG_OF(UpgradeDownloadProgress), dProgress);
  5371. return retError;
  5372. }else{
  5373. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM).setAPI("DownloadProgress")("download file progress is fail,fileSumLen=0");
  5374. return Error_Exception;
  5375. }
  5376. }
  5377. char CUpgradeTaskFSM::GetInstallState(int nState)
  5378. {
  5379. if (nState == s4)
  5380. return 'U';
  5381. else if (nState == s5)
  5382. return 'I';
  5383. else if (nState == s6)
  5384. return 'S';
  5385. else
  5386. return 'N';
  5387. }
  5388. ErrorCodeEnum CUpgradeTaskFSM::GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID, char &cInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion)
  5389. {
  5390. bInstalling = !m_currentTask.upgradeTaskId.IsNullOrEmpty();
  5391. if (bInstalling)
  5392. {
  5393. strPackFile = m_currentTask.pack_name;
  5394. strExecID = m_currentTask.upgradeTaskId;
  5395. cInstallState = GetInstallState(m_iState);
  5396. bSysInstall = false;
  5397. bLightPack = false;
  5398. //if(!m_currentTask.to_run.IsNullOrEmpty()){
  5399. // bSysInstall = true;
  5400. //}else{
  5401. // bSysInstall = false;
  5402. //}
  5403. //if(m_currentTask.pack_version.IsNullOrEmpty()){
  5404. // bLightPack = true;
  5405. //}else{
  5406. // bLightPack = false;
  5407. //}
  5408. strNewVersion = m_currentTask.NewVersion;
  5409. }
  5410. return Error_Succeed;
  5411. }
  5412. void CUpgradeTaskFSM::HttpsLogCallBack(const char* logtxt)
  5413. {
  5414. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("http dbg: %s",logtxt);
  5415. }
  5416. bool CUpgradeTaskFSM::isWork()
  5417. {
  5418. //兜底方案是判断集中配置一个参数,去除其他变量的判断,但是容易出现任务状态更新不准的情况。
  5419. if(m_startFlag){
  5420. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("upgrade startFlag is true");
  5421. return true;
  5422. }
  5423. //判断是否在设备安装状态,不受一般升级影响
  5424. if (isTerminalInstall()) {
  5425. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("isTerminalInstall get TerminalStage=N, is New Terminal Install");
  5426. return true;
  5427. }
  5428. else {
  5429. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("isTerminalInstall get TerminalStage is not N");
  5430. }
  5431. //判断升级实体是否工作
  5432. //1.当有版本升级任务时,判断是否处于Guardian升级回退过程,如果已经升级成功或者回退成功,则开始工作,否则等待中
  5433. //2.当无任务或者非版本升级任务时,判断TerminalStage这个变量,非X状态,当A状态并且进入首页,则开始工作
  5434. //升级任务
  5435. if((!m_currentTask.NewVersion.IsNullOrEmpty())&&(m_currentTask.cPendingState=="T"||m_currentTask.cPendingState=="C"||m_currentTask.cPendingState=="X")){
  5436. CSimpleStringA strRunInfo,strStartTime;
  5437. if (m_pEntity->GetFunction()->GetPath("RunInfo",strRunInfo) == Error_Succeed){
  5438. //TODO: CrossPlaform [Gifur@2025730]
  5439. #ifdef RVC_OS_WIN
  5440. strStartTime = strRunInfo + "\\runcfg\\starttime.dat";
  5441. #else
  5442. strStartTime = strRunInfo + "/runcfg/starttime.dat";
  5443. #endif
  5444. if(!ExistsFileA(strStartTime.GetData())){
  5445. return true;//完成升级过程,通过
  5446. }else{
  5447. //TODO: CrossPlaform [Gifur@2025730]
  5448. #ifdef RVC_OS_WIN
  5449. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("upgrade process is not end ,file \\runcfg\\starttime is exist");
  5450. #else
  5451. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("upgrade process is not end ,file /runcfg/starttime is exist");
  5452. #endif
  5453. return false;//继续等待
  5454. }
  5455. }else{
  5456. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("upgrade process is not end ,get runinfo path is error");
  5457. return false;//失败,继续等待
  5458. }
  5459. }
  5460. //适配器任务
  5461. if ((!m_currentTask.depNewVersion.IsNullOrEmpty()) && (m_currentTask.cPendingState == "T" || m_currentTask.cPendingState == "C" || m_currentTask.cPendingState == "X")) {
  5462. return true;//适配器无升级监护
  5463. }
  5464. //非升级任务和无任务
  5465. CSimpleStringA strValue;
  5466. if(m_pEntity->GetFunction()->GetSysVar("TerminalStage", strValue) == Error_Succeed){
  5467. if(strValue.Compare("X")==0){
  5468. m_queryIsWorkCount++;
  5469. if (m_queryIsWorkCount >= QUERY_IS_WORK_WARN_LIMIT) {
  5470. m_queryIsWorkCount = 0;
  5471. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("TerminalStage = X ,continue wait");
  5472. }
  5473. else {
  5474. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("TerminalStage = X ,continue wait");
  5475. }
  5476. return false;//状态是x继续等待
  5477. }
  5478. if(strValue.Compare("A")==0){
  5479. if(((CUpgradeMgrEntity*)m_pEntity)->m_bStartUp){
  5480. return true;//通过
  5481. }else{
  5482. m_queryIsWorkCount++;
  5483. if (m_queryIsWorkCount >= QUERY_IS_WORK_WARN_LIMIT) {
  5484. m_queryIsWorkCount = 0;
  5485. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("TerminalStage = A ,but UIState is not M , continue wait");
  5486. }
  5487. else {
  5488. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("TerminalStage = A ,but UIState is not M , continue wait");
  5489. }
  5490. return false;//准入过,但未进入首页,继续等待
  5491. }
  5492. }
  5493. return true;//剩余其他状态都可以通过
  5494. }else{
  5495. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("get TerminalStage is error , continue wait");
  5496. return false;//失败,继续等待
  5497. }
  5498. }
  5499. bool CUpgradeTaskFSM::isAuthSucc() {
  5500. CSimpleStringA strValue;
  5501. if (m_pEntity->GetFunction()->GetSysVar("TerminalStage", strValue) == Error_Succeed) {
  5502. if (strValue.Compare("A") == 0) {
  5503. return true;
  5504. }
  5505. else {
  5506. return false;
  5507. }
  5508. }
  5509. else {
  5510. return false;
  5511. }
  5512. }
  5513. bool CUpgradeTaskFSM::isTerminalInstall() {
  5514. CSimpleStringA strValue;
  5515. if (m_pEntity->GetFunction()->GetSysVar("TerminalStage", strValue) == Error_Succeed) {
  5516. if (strValue.Compare("N") == 0) {
  5517. //DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("isTerminalInstall get TerminalStage=N, is New Terminal Install");
  5518. return true; //表示安装模式
  5519. }else{
  5520. return false;
  5521. }
  5522. }
  5523. else {
  5524. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("isTerminalInstall get TerminalStage is error , default return fasle");
  5525. return false;//默认认为在升级状态
  5526. }
  5527. }
  5528. void CUpgradeTaskFSM::upgradeSuccStateBroadCast() {
  5529. UpgradeStateEvent stateEvent;
  5530. stateEvent.strPackName = m_currentTask.pack_name.GetData();
  5531. stateEvent.strExecID = m_currentTask.upgradeTaskId.GetData();
  5532. stateEvent.cInstallState = 'A';//表示成功状态
  5533. stateEvent.bSysInstall = false;
  5534. stateEvent.bLightPack = false;
  5535. stateEvent.strNewVersion = m_currentTask.NewVersion;
  5536. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
  5537. }
  5538. #ifdef RVC_OS_WIN
  5539. #else
  5540. int CUpgradeTaskFSM::changeFileAtt(const char* path)
  5541. {
  5542. struct stat attr_of_del;
  5543. if (lstat(path, &attr_of_del) == 0)
  5544. {
  5545. //修改为775属性
  5546. mode_t f_attrib = S_IRUSR | S_IWUSR | S_IXUSR | S_IRGRP | S_IWGRP | S_IXGRP | S_IROTH | S_IXOTH;
  5547. if (chmod(path, f_attrib) != 0)
  5548. {
  5549. Dbg("set file attribute is fail,errno=%d, file=%s", errno, path);
  5550. return -1;
  5551. }
  5552. return 0;
  5553. }
  5554. else {
  5555. Dbg("get file attribute is fail,errno=%d, file=%s", errno, path);
  5556. return -1;
  5557. }
  5558. }
  5559. #endif
  5560. void CUpgradeTaskFSM::OnStateTrans(int iSrcState, int iDstState)
  5561. {
  5562. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  5563. if (iSrcState >= s4 && iDstState <= s3)
  5564. {
  5565. // 发送状态广播(通知业务界面清除状态)
  5566. UpgradeStateEvent stateEvent;
  5567. stateEvent.strPackName = "";
  5568. stateEvent.strExecID = "";
  5569. stateEvent.cInstallState = 'N';
  5570. stateEvent.bSysInstall = false;
  5571. stateEvent.bLightPack = false;
  5572. stateEvent.strNewVersion = "";
  5573. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
  5574. }
  5575. else if (iDstState >= s4)
  5576. {
  5577. //assert(HasRunningUpdate());
  5578. //SetRunConfigStrValue("Install", "Pack", m_strIntallingPack);
  5579. //if (iDstState == s3)
  5580. // SetRunConfigStrValue("Install", "PendingState", "U");
  5581. //else if (iDstState == s4)
  5582. // SetRunConfigStrValue("Install", "PendingState", "I");
  5583. //else if (iDstState == s5)
  5584. // SetRunConfigStrValue("Install", "PendingState", "C");
  5585. //else if (iDstState == s6)
  5586. // SetRunConfigStrValue("Install", "PendingState", "S");
  5587. // 发送状态广播
  5588. UpgradeStateEvent stateEvent;
  5589. stateEvent.strPackName = m_currentTask.pack_name.GetData();
  5590. stateEvent.strExecID = m_currentTask.upgradeTaskId.GetData();
  5591. stateEvent.cInstallState = GetInstallState(iDstState);
  5592. stateEvent.bSysInstall = false;
  5593. stateEvent.bLightPack = false;
  5594. stateEvent.strNewVersion = m_currentTask.NewVersion;
  5595. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
  5596. }
  5597. }
  5598. void CUpgradeTaskFSM::OnSysVarEvent(const char *pszKey, const char *pszValue,const char *pszOldValue,const char *pszEntityName)
  5599. {
  5600. //监听系统变量状态
  5601. if(strcmp("CallState",pszKey)==0){
  5602. if (pszValue[0] == 'O') {
  5603. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("m_bDownloadIdle is false ");
  5604. m_bDownloadIdle = false;//可以下载
  5605. } else {
  5606. //DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM).setAPI(__FUNCTION__)("m_bDownloadIdle is true");
  5607. m_bDownloadIdle = true;//不能下载
  5608. }
  5609. }
  5610. }
  5611. ErrorCodeEnum CUpgradeTaskFSM::OnInit()
  5612. {
  5613. //初始化
  5614. ErrorCodeEnum rc = LoadPersistTaskInfo();//加载运行时文件
  5615. if(rc!=Error_Succeed){
  5616. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("LoadPersistTaskInfo fail");
  5617. return rc;
  5618. }
  5619. rc = loadCenterCfgInfo();//加载集中配置文件
  5620. if(rc!=Error_Succeed){
  5621. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("loadCenterCfgInfo fail");
  5622. return rc;
  5623. }
  5624. //加载终端启动的版本号
  5625. CSystemStaticInfo info;
  5626. rc = m_pEntity->GetFunction()->GetSystemStaticInfo(info);
  5627. if(rc!=Error_Succeed){
  5628. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("load GetSystemStaticInfo fail ");
  5629. return rc;
  5630. }else{
  5631. m_currentVersion = info.InstallVersion;//当前版本号赋值
  5632. m_currentDepVersion = info.CurrDepVersion.GetData();//当前适配器版本号,有可能为空
  5633. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("App version:%s , Dep version:%s", m_currentVersion.ToString().GetData(),m_currentDepVersion.GetData());
  5634. }
  5635. //注册callstate变量监控
  5636. rc = m_pEntity->GetFunction()->RegistSysVarEvent("CallState", this);
  5637. if(rc!=Error_Succeed){
  5638. LogWarn(Severity_Middle, rc, ERR_WRAN_REGIST_SYS_VAR_FAIL, CSimpleStringA::Format("RegistSysVarEvent CallState is fail,%d",(int)rc).GetData());
  5639. return rc;//自检失败
  5640. }else{
  5641. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("RegistSysVarEvent CallState succ");
  5642. }
  5643. AddStateHooker(this);
  5644. ((CUpgradeMgrEntity*)m_pEntity)->testActive();//添加测试active文件是否被占用的方法
  5645. return Error_Succeed;
  5646. }
  5647. ErrorCodeEnum CUpgradeTaskFSM::OnExit()
  5648. {
  5649. m_pEntity->GetFunction()->UnregistSysVarEvent("CallState");
  5650. RemoveStateHooker(this);
  5651. return Error_Succeed;
  5652. }
  5653. void CUpgradeTaskFSM::s1_on_entry()
  5654. {
  5655. //判断是否进入了首页
  5656. CSimpleStringA strValue;
  5657. m_queryIsWorkCount = 0;
  5658. if(isWork()&&((CUpgradeMgrEntity*)m_pEntity)->m_initSucc){
  5659. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("====upgrade is start up ====");
  5660. ScheduleTimer(2,1*1000);
  5661. }else{
  5662. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("last upgrade progress is not end or is not initSucc,wait 20s ");
  5663. ScheduleTimer(1, 20*1000);//等待一段时间再判断是否进入首页
  5664. }
  5665. }
  5666. void CUpgradeTaskFSM::s1_on_exit()
  5667. {
  5668. CancelTimer(1);//等待进入首页计时器
  5669. CancelTimer(2);//等待hash计算计时器
  5670. }
  5671. unsigned int CUpgradeTaskFSM::s1_on_event(FSMEvent* event)
  5672. {
  5673. if (event->iEvt == EVT_TIMER)
  5674. {
  5675. if(event->param1 == 1){
  5676. CSimpleStringA strValue;
  5677. if(isWork()&&((CUpgradeMgrEntity*)m_pEntity)->m_initSucc){
  5678. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("====upgrade is start up ====");
  5679. ScheduleTimer(2,1*1000);
  5680. }else{
  5681. DbgWithLink(LOG_LEVEL_DEBUG,LOG_TYPE_SYSTEM)("last upgrade progress is not end or is not initSucc,wait 20s ");
  5682. ScheduleTimer(1, 20*1000);//等待一段时间再判断是否进入首页
  5683. }
  5684. }else if(event->param1 ==2){
  5685. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入s2
  5686. }
  5687. }
  5688. return 0;
  5689. }
  5690. void CUpgradeTaskFSM::s2_on_entry()
  5691. {
  5692. //判断是否存在残留任务
  5693. if(m_currentTask.upgradeTaskId.IsNullOrEmpty()&&m_currentTask.cPendingState.IsNullOrEmpty()){
  5694. //直接进入查询新任务
  5695. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5696. }else{
  5697. //处理上个任务
  5698. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_DealCurrTask));
  5699. }
  5700. }
  5701. void CUpgradeTaskFSM::s2_on_exit()
  5702. {
  5703. CancelTimer(1);//更新任务状态计时器
  5704. CancelTimer(2);//查询当前任务计时器
  5705. }
  5706. unsigned int CUpgradeTaskFSM::s2_on_event(FSMEvent* event)
  5707. {
  5708. if (event->iEvt == EVT_TIMER){
  5709. if(event->param1 == 1){
  5710. Task::UpdateTaskStatus* uTask = new Task::UpdateTaskStatus(this);
  5711. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(uTask);
  5712. if(Error_Succeed!=rc){
  5713. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s2 create UpdateTaskStatus is fail,%d",(int)rc);
  5714. ScheduleTimer(1,60*1000);//继续更新任务状态
  5715. }
  5716. }else if(event->param1 == 2){
  5717. Task::QueryCurrTask* qTask = new Task::QueryCurrTask(this);
  5718. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(qTask);
  5719. if(Error_Succeed!=rc){
  5720. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s2 create QueryCurrTask is fail,%d",(int)rc);
  5721. ScheduleTimer(2,4*60*1000);//继续查询当前任务
  5722. }
  5723. }
  5724. }else if (event->iEvt == Event_DealCurrTask)
  5725. {
  5726. //处理运行时文件异常记录的情况
  5727. if (m_currentTask.task_type.IsNullOrEmpty()) {
  5728. m_currentTask.task_type = "U";//兼容旧版本字段无值的情况,默认是升级模式
  5729. }
  5730. if (m_currentTask.task_type.Compare("U") == 0) {
  5731. if (m_currentTask.upgradeTaskId.IsNullOrEmpty() || m_currentTask.createTaskVersion.IsNullOrEmpty()
  5732. || m_currentTask.cPendingState.IsNullOrEmpty() || m_currentTask.pack_name.IsNullOrEmpty()) {
  5733. //有任务时,则4个字段不能为空,否则任务有问题
  5734. CSimpleStringA errStr = CSimpleStringA::Format("current Task param is null, clear this task, upgradeTaskId=[%s],createTaskVersion=[%s],cPendingState=[%s],PackName=[%s]",
  5735. m_currentTask.upgradeTaskId.GetData(), m_currentTask.createTaskVersion.GetData(), m_currentTask.cPendingState.GetData(), m_currentTask.pack_name.GetData());
  5736. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_INVALID_RUNINFO_TASK, errStr.GetData());
  5737. if (ClearCurrTaskInfo()) {
  5738. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5739. }
  5740. else {
  5741. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("clear current task is fail 1,retry int 5's");
  5742. Sleep(5 * 1000);//等待五秒,继续清理
  5743. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_DealCurrTask));//继续处理上个任务
  5744. }
  5745. }
  5746. }
  5747. else if (m_currentTask.task_type.Compare("R") == 0) {
  5748. if (m_currentTask.upgradeTaskId.IsNullOrEmpty() || m_currentTask.createTaskVersion.IsNullOrEmpty()
  5749. || m_currentTask.cPendingState.IsNullOrEmpty()) {
  5750. //有任务时,则3个字段不能为空,否则任务有问题
  5751. CSimpleStringA errStr = CSimpleStringA::Format("current Task param is null, clear this task, upgradeTaskId=[%s],createTaskVersion=[%s],cPendingState=[%s]",
  5752. m_currentTask.upgradeTaskId.GetData(), m_currentTask.createTaskVersion.GetData(), m_currentTask.cPendingState.GetData());
  5753. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_INVALID_RUNINFO_TASK, errStr.GetData());
  5754. if (ClearCurrTaskInfo()) {
  5755. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5756. }
  5757. else {
  5758. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("clear current task is fail 1,retry int 5's");
  5759. Sleep(5 * 1000);//等待五秒,继续清理
  5760. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_DealCurrTask));//继续处理上个任务
  5761. }
  5762. }
  5763. }
  5764. else {
  5765. CSimpleStringA errStr = CSimpleStringA::Format("current Task param task_type=[%s], param is Not Supported, clear this task",m_currentTask.task_type.GetData());
  5766. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_INVALID_RUNINFO_TASK, errStr.GetData());
  5767. if (ClearCurrTaskInfo()) {
  5768. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5769. }
  5770. else {
  5771. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("clear current task is fail 1,retry int 5's");
  5772. Sleep(5 * 1000);//等待五秒,继续清理
  5773. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_DealCurrTask));//继续处理上个任务
  5774. }
  5775. }
  5776. //任务已到最终状态
  5777. if(m_currentTask.cPendingState=="T"||m_currentTask.cPendingState=="C"||m_currentTask.cPendingState=="X"){
  5778. //取消任务C和切换任务T时,更新任务状态
  5779. Task::UpdateTaskStatus* uTask = new Task::UpdateTaskStatus(this);
  5780. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(uTask);
  5781. if(Error_Succeed!=rc){
  5782. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s2 create UpdateTaskStatus is fail,%d",(int)rc);
  5783. ScheduleTimer(1,60*1000);//继续更新任务状态
  5784. }
  5785. }else{
  5786. //当前任务
  5787. if(m_currentTask.createTaskVersion!=m_currentVersion.ToString()){
  5788. //比较遗留任务的创建版本号是否和现在版本一致,不一致,则可能出现拷贝别的终端文件或者已经手工改了别的版本引发,这种情况下自动清理此次任务。
  5789. CSimpleStringA errStr = CSimpleStringA::Format("current Task createTaskVersion[%s] is not equal to currentVersion[%s],clear this task",m_currentTask.createTaskVersion.GetData(),m_currentVersion.ToString().GetData());
  5790. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_INVALID_RUNINFO_TASK,errStr.GetData());
  5791. if(ClearCurrTaskInfo()){
  5792. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5793. }else{
  5794. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("clear current task is fail 2,retry int 5's");
  5795. Sleep(5 * 1000);//等待五秒,继续清理
  5796. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_DealCurrTask));//继续处理上个任务
  5797. }
  5798. }else{
  5799. Task::QueryCurrTask* qTask = new Task::QueryCurrTask(this);
  5800. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(qTask);
  5801. if(Error_Succeed!=rc){
  5802. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s2 create QueryCurrTask is fail,%d",(int)rc);
  5803. ScheduleTimer(2,4*60*1000);//继续查询当前任务
  5804. }
  5805. }
  5806. }
  5807. }else if(event->iEvt == Event_EndUpdateTask){
  5808. //更新任务状态返回结果
  5809. UpdateTaskStatusEvent* pEvent = (UpdateTaskStatusEvent*)event;
  5810. if(pEvent->isSucc){
  5811. //告警回滚和升级成功
  5812. if(m_currentTask.cPendingState=="T"){
  5813. if (isTerminalInstall()) {
  5814. //安装情况
  5815. struct CTaskProcess up;
  5816. up.strPackName = m_currentTask.pack_name;//安装包名
  5817. if (m_currentTask.depNewVersion.IsNullOrEmpty()) {
  5818. up.installVersion = m_currentVersion.ToString();//当前的版本
  5819. up.CurrentVersion = m_currentVersion.ToString();//当前的版本
  5820. }
  5821. else {
  5822. up.installVersion = m_currentDepVersion.GetData();//安装适配器版本
  5823. up.CurrentVersion = m_currentDepVersion.GetData();//适配器当前版本
  5824. }
  5825. up.cInstallState = "A";
  5826. up.strInstallComment = "启动成功";
  5827. SendUpgradeTaskProgress(up, UpgradeMgr_START_ID);
  5828. upgradeSuccStateBroadCast();//广播告知升级成功
  5829. }
  5830. else {
  5831. //非安装情况
  5832. if (m_currentTask.NewVersion == m_currentVersion.ToString() ||
  5833. m_currentTask.depNewVersion == m_currentDepVersion ||
  5834. (m_currentTask.NewVersion == "" && m_currentTask.depNewVersion == "")) {
  5835. //安装成功
  5836. struct CTaskProcess up;
  5837. up.strPackName = m_currentTask.pack_name;//安装包名
  5838. if (m_currentTask.depNewVersion.IsNullOrEmpty()) {
  5839. up.installVersion = m_currentVersion.ToString();//当前的版本
  5840. up.CurrentVersion = m_currentVersion.ToString();//当前的版本
  5841. }
  5842. else {
  5843. up.installVersion = m_currentDepVersion.GetData();//安装适配器版本
  5844. up.CurrentVersion = m_currentDepVersion.GetData();//适配器当前版本
  5845. }
  5846. up.cInstallState = "A";
  5847. up.strInstallComment = "启动成功";
  5848. if (m_currentTask.task_type.Compare("U") == 0) {
  5849. SendUpgradeTaskProgress(up, UpgradeMgr_START_ID);
  5850. }
  5851. else {
  5852. SendUpgradeTaskProgress(up, UpgradeMgr_ROLLBACK_START_ID);
  5853. }
  5854. upgradeSuccStateBroadCast();//广播告知升级成功
  5855. }
  5856. else {
  5857. //回滚
  5858. struct CTaskProcess upRoll;
  5859. upRoll.strPackName = m_currentTask.pack_name;//安装包名
  5860. if (m_currentTask.depNewVersion.IsNullOrEmpty()) {
  5861. upRoll.installVersion = m_currentTask.NewVersion.GetData();//任务升级后的版本号
  5862. upRoll.CurrentVersion = m_currentVersion.ToString();//当前的版本
  5863. }
  5864. else {
  5865. upRoll.installVersion = m_currentTask.depNewVersion.GetData();//安装适配器版本
  5866. upRoll.CurrentVersion = m_currentDepVersion.GetData();//适配器当前版本
  5867. }
  5868. upRoll.cInstallState = "R";
  5869. upRoll.strInstallComment = "自动倒回";
  5870. if (m_currentTask.task_type.Compare("U") == 0) {
  5871. SendUpgradeTaskProgress(upRoll, UpgradeMgr_START_ID, " RTA5667");
  5872. }
  5873. else {
  5874. SendUpgradeTaskProgress(upRoll, UpgradeMgr_ROLLBACK_START_ID, " RTA5672");
  5875. }
  5876. }
  5877. }
  5878. ErrorCodeEnum rc = SetRunSucceed(m_currentTask.pack_name.GetData());
  5879. if (rc != Error_Succeed) {
  5880. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("SetRunSucceed is fail: %d", (int)rc);
  5881. }
  5882. }
  5883. //清除运行时文件和内存任务,跳转到S3
  5884. if(ClearCurrTaskInfo()){
  5885. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5886. }else{
  5887. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndUpdateTask clear current task is fail,retry int 5's");
  5888. Sleep(5 * 1000);//等待五秒,继续清理
  5889. PostEventFIFO(new UpdateTaskStatusEvent(true));//继续更新任务状态
  5890. }
  5891. }else{
  5892. ScheduleTimer(1,60*1000);//继续更新任务状态
  5893. }
  5894. }else if(event->iEvt == Event_EndQueryCurrTask){
  5895. //查询当前任务返回结果
  5896. QueryCurrTaskEvent* pEvent = (QueryCurrTaskEvent*)event;
  5897. if(pEvent->isSucc){
  5898. if(pEvent->isTask_valid){
  5899. //根据不同任务状态值,进入不同阶段。U:下载阶段,I:安装阶段,回退检查,S切换阶段
  5900. if(m_currentTask.cPendingState=="U"){
  5901. return 4;//下载
  5902. }else if(m_currentTask.cPendingState=="I"){
  5903. if (m_currentTask.task_type.Compare("R") == 0) {
  5904. //因回退不进入S阶段,故加入切换标志。
  5905. m_bSwitchFlag = true;
  5906. }
  5907. return 5;//安装,回退检查
  5908. }else if(m_currentTask.cPendingState=="S"){
  5909. //加入切换标志,用于切换时判断是否是切换进入的流程。
  5910. m_bSwitchFlag = true;
  5911. //是否是升级适配器需要根据运行时字段进行赋值
  5912. if (m_currentTask.depNewVersion.IsNullOrEmpty()) {
  5913. m_TempDepUpgradeFlag = false;
  5914. }
  5915. else {
  5916. m_TempDepUpgradeFlag = true;
  5917. }
  5918. return 6;//切换
  5919. }else{
  5920. //无效状态
  5921. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_INVALID_STATUS_FAIL,CSimpleStringA::Format("invalid pending state : %s",m_currentTask.cPendingState.GetData()).GetData());
  5922. //任务无效,清理任务
  5923. if(ClearCurrTaskInfo()){
  5924. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5925. }else{
  5926. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndQueryCurrTask clear current task is fail 2,retry int 5's");
  5927. Sleep(5 * 1000);//等待五秒,继续清理
  5928. PostEventFIFO(new QueryCurrTaskEvent(true,true));//继续清理当前任务
  5929. }
  5930. }
  5931. }else{
  5932. LogWarn(Severity_Middle, Error_Exception, ERR_TASK_INVALID_RUNINFO_TASK,CSimpleStringA::Format("current task is invalid taskid=%s, begin delete",m_currentTask.upgradeTaskId.GetData()).GetData());
  5933. //任务无效,清理任务
  5934. if(ClearCurrTaskInfo()){
  5935. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  5936. }else{
  5937. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndQueryCurrTask clear current task is fail 1,retry int 5's");
  5938. Sleep(5 * 1000);//等待五秒,继续清理
  5939. PostEventFIFO(new QueryCurrTaskEvent(true,false));//继续清理当前任务
  5940. }
  5941. }
  5942. }else{
  5943. ScheduleTimer(2,4*60*1000);//继续查询当前任务
  5944. }
  5945. }
  5946. return 0;
  5947. }
  5948. void CUpgradeTaskFSM::s3_on_entry()
  5949. {
  5950. //查询新任务:先预查询任务,传送hash,接着查询任务
  5951. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("entery S3");
  5952. m_queryNewTaskCount = 0;
  5953. m_strPreTaskId = "";
  5954. m_bNeedHash=0;
  5955. m_fileSM3List="";
  5956. m_fileSM3ListHash="";
  5957. isSendHash=false;
  5958. //预查询任务
  5959. Task::QueryNewPreTask* qTask = new Task::QueryNewPreTask(this);
  5960. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(qTask);
  5961. if(rc!=Error_Succeed){
  5962. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create QueryNewPreTask fail,wait 4 min retry ,err=%d",(int)rc);
  5963. ScheduleTimer(1, m_queryNewTaskInterval*1000);//等待4分钟后再次查询是否有新任务
  5964. }
  5965. }
  5966. void CUpgradeTaskFSM::s3_on_exit()
  5967. {
  5968. CancelTimer(1);//预查询任务计时器
  5969. CancelTimer(2);//上传hash计时器
  5970. CancelTimer(3);//继续任务计时器
  5971. m_queryNewTaskCount =0;
  5972. m_strPreTaskId = "";
  5973. m_bNeedHash=0;
  5974. m_fileSM3List="";
  5975. m_fileSM3ListHash="";
  5976. isSendHash=false;
  5977. }
  5978. unsigned int CUpgradeTaskFSM::s3_on_event(FSMEvent* event)
  5979. {
  5980. if(event->iEvt == EVT_TIMER){
  5981. if(event->param1 == 1){
  5982. //发起预查询任务
  5983. Task::QueryNewPreTask* nTask = new Task::QueryNewPreTask(this);
  5984. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(nTask);
  5985. if(rc!=Error_Succeed){
  5986. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create QueryNewPreTask fail,wait [%d]s retry ,err=%d", m_queryNewTaskInterval, (int)rc);
  5987. ScheduleTimer(1, m_queryNewTaskInterval *1000);//等待4分钟后再次查询是否有新任务
  5988. }
  5989. }else if(event->param1 == 2){
  5990. //上传hash
  5991. Task::SendSM3Task* SM3Task = new Task::SendSM3Task(this);
  5992. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(SM3Task);
  5993. if(rc!=Error_Succeed){
  5994. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create SendSM3Task fail,err=%d",(int)rc);
  5995. ScheduleTimer(2, 60*1000);//等待1分钟过后再次发送
  5996. }
  5997. }else if(event->param1 == 3){
  5998. //继续任务
  5999. Task::QueryNewContinueTask* nTask = new Task::QueryNewContinueTask(this);
  6000. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(nTask);
  6001. if(rc!=Error_Succeed){
  6002. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create QueryNewContinueTask fail,err=%d",(int)rc);
  6003. ScheduleTimer(3, m_queryNewTaskInterval *1000);//等待4分钟过后再次发送
  6004. }
  6005. }
  6006. }
  6007. else if(event->iEvt == Event_EndPrePoll){
  6008. QueryNewPreTaskEvent* pEvent = (QueryNewPreTaskEvent*)event;
  6009. if(pEvent->isSucc){
  6010. if(m_strPreTaskId.IsNullOrEmpty()){
  6011. m_queryNewTaskCount++;
  6012. if(m_queryNewTaskCount>=QUERY_NO_TASK_WARN_LIMIT){
  6013. //LogWarn(Severity_Low, Error_Exception, ERR_TASK_QUERY_NO_TASK,"QueryNewPreTask 15 times succ,no new task");
  6014. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("QueryNewPreTask 15 times succ,no new task");
  6015. m_queryNewTaskCount=0;
  6016. }
  6017. if (isAuthSucc()) {
  6018. ScheduleTimer(1, m_queryNewTaskInterval * 1000);//等待4分钟后再次查询是否有新任务
  6019. }
  6020. else {
  6021. ScheduleTimer(1, 30 * 1000);//等待30s后再次查询是否有新任务
  6022. }
  6023. }else{
  6024. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("QueryNewPreTask succ, task_id=%s, needHash=%d",m_strPreTaskId.GetData(),m_bNeedHash);
  6025. if(m_bNeedHash){
  6026. //预查询成功
  6027. if(isSendHash){
  6028. //之前发送hash成功,直接开始继续任务查询线程
  6029. Task::QueryNewContinueTask* nTask = new Task::QueryNewContinueTask(this);
  6030. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(nTask);
  6031. if(rc!=Error_Succeed){
  6032. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create QueryNewContinueTask fail,err=%d",(int)rc);
  6033. ScheduleTimer(3, 60*1000);//再次查询是否有新任务
  6034. }
  6035. }else{
  6036. //之前未发送hash成功,开始计算hash线程
  6037. Task::SendSM3Task* SM3Task = new Task::SendSM3Task(this);
  6038. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(SM3Task);
  6039. if(rc!=Error_Succeed){
  6040. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create SendSM3Task fail,err=%d",(int)rc);
  6041. ScheduleTimer(2, 60*1000);//等待一分钟过后再次发送
  6042. }
  6043. }
  6044. }else{
  6045. //不需要hash,直接继续查询新任务
  6046. Task::QueryNewContinueTask* nTask = new Task::QueryNewContinueTask(this);
  6047. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(nTask);
  6048. if(rc!=Error_Succeed){
  6049. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create QueryNewContinueTask fail,err=%d",(int)rc);
  6050. ScheduleTimer(3, 60*1000);//等待4分钟后再次查询是否有新任务
  6051. }
  6052. }
  6053. }
  6054. }else{
  6055. //预查询失败
  6056. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("QueryNewPreTask fail,wait 4 min retry");
  6057. if (isAuthSucc()) {
  6058. ScheduleTimer(1, m_queryNewTaskInterval * 1000);//等待4分钟后再次预查询是否有新任务
  6059. }
  6060. else {
  6061. ScheduleTimer(1, 30 * 1000);//等待30s后再次查询是否有新任务
  6062. }
  6063. }
  6064. }
  6065. else if(event->iEvt == Event_EndSendSM3Task){
  6066. Task::QueryNewContinueTask* nTask = new Task::QueryNewContinueTask(this);
  6067. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(nTask);
  6068. if(rc!=Error_Succeed){
  6069. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s3 create QueryNewContinueTask fail,err=%d",(int)rc);
  6070. ScheduleTimer(3, 60*1000);//等待4分钟后再次查询是否有新任务
  6071. }
  6072. }
  6073. else if(event->iEvt == Event_EndContinuePoll){
  6074. QueryNewContinueTaskEvent* pEvent = (QueryNewContinueTaskEvent*)event;
  6075. if(pEvent->retParam == 0){
  6076. //查询失败,继续查询
  6077. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("QueryNewContinueTask fail,wait [%d]s retry",m_queryNewTaskInterval);
  6078. if (isAuthSucc()) {
  6079. ScheduleTimer(3, m_queryNewTaskInterval * 1000);//等待4分钟后再次查询继续新任务
  6080. }
  6081. else {
  6082. ScheduleTimer(3, 30 * 1000);//等待30s后再次查询继续新任务
  6083. }
  6084. }else if(pEvent->retParam == 1){
  6085. //查询成功,跳到下一个状态,写入运行时文件
  6086. bool isWriteOk=false;
  6087. while(true){
  6088. if(SetRunConfigStrValue("upgradeTask","NewVersion","")!=Error_Succeed){
  6089. break;
  6090. }
  6091. if (SetRunConfigStrValue("upgradeTask", "depNewVersion", "") != Error_Succeed) {
  6092. break;
  6093. }
  6094. if (SetRunConfigStrValue("upgradeTask", "CreateTaskVersion", m_currentTask.createTaskVersion.GetData()) != Error_Succeed) {
  6095. break;
  6096. }
  6097. if (SetRunConfigStrValue("upgradeTask", "UpgradeTaskId", m_currentTask.upgradeTaskId.GetData()) != Error_Succeed) {
  6098. break;
  6099. }
  6100. if (SetRunConfigStrValue("upgradeTask", "taskType", m_currentTask.task_type.GetData()) != Error_Succeed) {
  6101. break;
  6102. }
  6103. if (m_currentTask.task_type.Compare("U") == 0) {
  6104. if (SetRunConfigStrValue("upgradeTask", "PendingState", "U") != Error_Succeed) {
  6105. break;
  6106. }
  6107. if (SetRunConfigStrValue("upgradeTask", "PackName", m_currentTask.pack_name.GetData()) != Error_Succeed) {
  6108. break;
  6109. }
  6110. }else if(m_currentTask.task_type.Compare("R") == 0){
  6111. if (SetRunConfigStrValue("upgradeTask", "PendingState", "I") != Error_Succeed) {
  6112. break;
  6113. }
  6114. if (SetRunConfigStrValue("upgradeTask", "PackName", "") != Error_Succeed) {
  6115. break;
  6116. }
  6117. }
  6118. isWriteOk=true;
  6119. break;
  6120. }
  6121. if(isWriteOk){
  6122. if (m_currentTask.task_type.Compare("U") == 0) {
  6123. //升级下载
  6124. struct CTaskProcess up;
  6125. up.strPackName = m_currentTask.pack_name;//安装包名
  6126. up.installVersion = m_currentTask.pack_version;//安装的版本
  6127. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6128. up.cInstallState = "D";
  6129. up.strInstallComment = "正在下载";
  6130. SendUpgradeTaskProgress(up,UpgradeMgr_DOWNLOAD_ID);
  6131. m_currentTask.cPendingState = "U";//改变内存变量为U
  6132. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDownload));//进入S4
  6133. }
  6134. else {
  6135. //回退
  6136. struct CTaskProcess up;
  6137. up.strPackName = m_currentTask.pack_name;//安装包名
  6138. up.installVersion = m_currentTask.pack_version;//安装的版本
  6139. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6140. up.cInstallState = "B";
  6141. up.strInstallComment = "正在回退";
  6142. SendUpgradeTaskProgress(up,UpgradeMgr_ROLLBACK_ID);
  6143. m_currentTask.cPendingState = "I";//改变内存变量为I
  6144. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartInstall));//进入S5
  6145. }
  6146. }else{
  6147. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndPoll write runinfo is fail,retry int 5's");
  6148. Sleep(5 * 1000);//等待五秒,继续创建
  6149. PostEventFIFO(new QueryNewContinueTaskEvent(1));//继续创建任务
  6150. }
  6151. }else{
  6152. //跳出,重新预检查任务
  6153. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("QueryNewContinueTask fail,jump to PreTask");
  6154. if (isAuthSucc()) {
  6155. ScheduleTimer(1, m_queryNewTaskInterval * 1000);//等待4分钟后再次预查询是否有新任务
  6156. }
  6157. else {
  6158. ScheduleTimer(1, 30 * 1000);//等待30s后再次预查询是否有新任务
  6159. }
  6160. }
  6161. }
  6162. return 0;
  6163. }
  6164. void CUpgradeTaskFSM::s4_on_entry()
  6165. {
  6166. //下载升级包
  6167. m_bTaskCancel = false;//默认任务非取消
  6168. m_downloadBreakOffCount = 0;
  6169. if(m_bDownloadIdle){
  6170. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("CallState is not O , download is break off , wait a moment");
  6171. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_Download_Idle));//进入不可下载状态
  6172. }else{
  6173. Task::DownloadPackTask* dTask = new Task::DownloadPackTask(this);
  6174. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(dTask);
  6175. if(rc!=Error_Succeed){
  6176. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s4 create DownloadPackTask fail,err=%d",(int)rc);
  6177. ScheduleTimer(1, 15*1000);//等待15秒再试
  6178. }
  6179. }
  6180. ScheduleTimer(2, 30*1000);//启动查询任务是否有效
  6181. }
  6182. void CUpgradeTaskFSM::s4_on_exit()
  6183. {
  6184. CancelTimer(1);//正常下载计时器
  6185. CancelTimer(2);//查询任务是否取消计时器
  6186. CancelTimer(3);//外部状态中止下载计时器
  6187. }
  6188. unsigned int CUpgradeTaskFSM::s4_on_event(FSMEvent* event)
  6189. {
  6190. if (event->iEvt == EVT_TIMER)
  6191. {
  6192. if(event->param1 == 1){
  6193. Task::DownloadPackTask* dTask = new Task::DownloadPackTask(this);
  6194. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(dTask);
  6195. if(rc!=Error_Succeed){
  6196. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s4 create DownloadPackTask fail,err=%d",(int)rc);
  6197. ScheduleTimer(1, 15*1000);//再次下载升级包
  6198. }
  6199. }else if(event->param1 == 2){
  6200. Task::QueryTaskStatus* dTask = new Task::QueryTaskStatus(this);
  6201. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(dTask);
  6202. if(rc!=Error_Succeed){
  6203. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s4 create QueryTaskStatus fail,err=%d",(int)rc);
  6204. ScheduleTimer(2, 30*1000);//再次查询任务是否有效
  6205. }
  6206. }else if(event->param1 == 3){
  6207. //无效状态
  6208. if(m_bDownloadIdle){
  6209. if (m_downloadBreakOffCount > 60) {
  6210. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("CallState is not O , download is break off , wait a moment");
  6211. m_downloadBreakOffCount = 0;
  6212. }
  6213. else {
  6214. m_downloadBreakOffCount++;
  6215. }
  6216. ScheduleTimer(3,10*1000);//等待下次是否空闲
  6217. }else{
  6218. Task::DownloadPackTask* dTask = new Task::DownloadPackTask(this);
  6219. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(dTask);
  6220. if(rc!=Error_Succeed){
  6221. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s4 create DownloadPackTask fail,err=%d",(int)rc);
  6222. ScheduleTimer(3, 10*1000);//等待下次是否空闲
  6223. }
  6224. }
  6225. }
  6226. }else if (event->iEvt == Event_EndQueryTaskStatus){
  6227. QueryTaskStatusEvent* pEvent = (QueryTaskStatusEvent*)event;
  6228. if(pEvent->isSucc){
  6229. if(pEvent->isTask_valid){
  6230. ScheduleTimer(2, 30*1000);//有效,再次查询任务是否有效
  6231. m_bTaskCancel = false;
  6232. }else{
  6233. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("query upgradeTask is cancel,set m_bTaskCancel=true");
  6234. m_bTaskCancel = true;//取消下载,结束查询任务是否有效计时器
  6235. }
  6236. }else{
  6237. ScheduleTimer(2, 30*1000);//通讯失败,再次查询任务是否有效
  6238. }
  6239. }else if (event->iEvt == Event_EndDownload){
  6240. DownloadPackTaskEvent* pEvent = (DownloadPackTaskEvent*)event;
  6241. if(pEvent->isSucc){
  6242. if(SetRunConfigStrValue("upgradeTask","PendingState","I")==Error_Succeed){
  6243. //下载成功
  6244. m_currentTask.cPendingState = "I";//改变内存变量为I
  6245. if(m_currentTask.pack_version.IsNullOrEmpty()){
  6246. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(UpgradeMgr_OUTSIDE_SYSTEM_DOWNLOAD_ID)("体系外下载成功");
  6247. }else{
  6248. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(UpgradeMgr_VERSION_DOWNLOAD_ID)("终端版本下载成功");
  6249. }
  6250. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartInstall));//进入S5
  6251. }else{
  6252. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndDownload write runinfo is fail,retry in 5's");
  6253. Sleep(5 * 1000);//等待五秒
  6254. PostEventFIFO(new DownloadPackTaskEvent(true));//继续写运行时文件
  6255. }
  6256. }else{
  6257. if (m_currentTask.pack_version.IsNullOrEmpty()) {
  6258. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(UpgradeMgr_OUTSIDE_SYSTEM_DOWNLOAD_ID).setResultCode("RTA5634")("体系外下载失败,通讯报错");
  6259. }
  6260. else {
  6261. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER).setLogCode(UpgradeMgr_VERSION_DOWNLOAD_ID).setResultCode("RTA5633")("终端版本下载失败,通讯报错");
  6262. }
  6263. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("download pack is fail , wait for 2 min retry download");
  6264. ScheduleTimer(1, 120*1000);//下载失败,等2分钟,再次下载任务
  6265. }
  6266. }else if (event->iEvt == Event_Download_Fail){
  6267. DownloadPackTaskFailEvent* pEvent = (DownloadPackTaskFailEvent*)event;
  6268. //写入运行时文件,状态为C
  6269. if(SetRunConfigStrValue("upgradeTask","PendingState","C")==Error_Succeed){
  6270. //告警下载失败
  6271. struct CTaskProcess up;
  6272. up.strPackName = m_currentTask.pack_name;//安装包名
  6273. up.installVersion = m_currentTask.pack_version;//安装的版本
  6274. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6275. up.cInstallState = "C";
  6276. up.strInstallComment = pEvent->errMsg.c_str();
  6277. SendUpgradeTaskProgress(up,UpgradeMgr_DOWNLOAD_ID,"RTA5660");
  6278. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("upgradeTask download is fail ,prepare to s2");
  6279. m_currentTask.cPendingState = "C";//改变内存变量C
  6280. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  6281. }else{
  6282. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_Download_Fail write runinfo is fail,retry in 5's");
  6283. Sleep(5 * 1000);//等待五秒
  6284. PostEventFIFO(new DownloadPackTaskFailEvent(pEvent->errMsg));//继续写运行时文件
  6285. }
  6286. }else if (event->iEvt == Event_Download_Idle){
  6287. ScheduleTimer(3,10*1000);//等待下次是否空闲
  6288. }else if (event->iEvt == Event_CancelTask){
  6289. //取消任务,清理任务
  6290. if(ClearCurrTaskInfo()){
  6291. //告警下载取消
  6292. struct CTaskProcess up;
  6293. up.strPackName = m_currentTask.pack_name;//安装包名
  6294. up.installVersion = m_currentTask.pack_version;//安装的版本
  6295. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6296. up.cInstallState = "C";
  6297. up.strInstallComment = "任务策略已取消";
  6298. SendUpgradeTaskProgress(up,UpgradeMgr_DOWNLOAD_ID,"RTA5661");
  6299. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("upgradeTask is cancel,prepare to s3");
  6300. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  6301. }else{
  6302. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("clear current task is fail 1,retry in 5's");
  6303. Sleep(5 * 1000);//等待五秒,继续清理
  6304. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_CancelTask));//继续取消任务
  6305. }
  6306. }
  6307. return 0;
  6308. }
  6309. void CUpgradeTaskFSM::s5_on_entry()
  6310. {
  6311. //停止播放广告和音频
  6312. Task::StopMediaPlayTask* nTask = new Task::StopMediaPlayTask(this);
  6313. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(nTask);
  6314. if(rc!=Error_Succeed){
  6315. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s5 create StopMediaPlayTask fail,err=%d",(int)rc);
  6316. ScheduleTimer(1, 30*1000);//等待30秒再试
  6317. }
  6318. }
  6319. void CUpgradeTaskFSM::s5_on_exit()
  6320. {
  6321. CancelTimer(1);//停止广播和声音计时器
  6322. CancelTimer(2);//解压计时器
  6323. CancelTimer(3);//安装计时器
  6324. CancelTimer(4);//等待跳转切换计时器
  6325. CancelTimer(5);//回退检查计时器
  6326. }
  6327. unsigned int CUpgradeTaskFSM::s5_on_event(FSMEvent* event)
  6328. {
  6329. if(event->iEvt == EVT_TIMER){
  6330. if(event->param1 == 1){
  6331. //再次发起任务查询
  6332. Task::StopMediaPlayTask* nTask = new Task::StopMediaPlayTask(this);
  6333. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(nTask);
  6334. if(rc!=Error_Succeed){
  6335. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s5 create StopMediaPlayTask fail,err=%d",(int)rc);
  6336. ScheduleTimer(1, 30*1000);//等待30秒再停止播放广告和音频
  6337. }
  6338. }else if(event->param1 == 2){
  6339. Task::UnzipPackTask* iTask = new Task::UnzipPackTask(this);
  6340. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(iTask);
  6341. if(rc!=Error_Succeed){
  6342. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s5 create UnzipPackTask fail,err=%d",(int)rc);
  6343. ScheduleTimer(2, 30*1000);//等待30秒再解压包
  6344. }
  6345. }else if(event->param1 == 3){
  6346. BOOL doInstall = true;
  6347. if(!m_startFlag) //非强制安装
  6348. {
  6349. if (m_currentTask.pack_version.GetLength() == 0) //体系外升级
  6350. {
  6351. CSimpleStringA strState;
  6352. ErrorCodeEnum err = m_pEntity->GetFunction()->GetSysVar("UIState", strState);
  6353. if (err == Error_Succeed && strState != CSimpleStringA("M")) //非主界面不安装
  6354. {
  6355. doInstall = false;
  6356. }
  6357. }
  6358. }
  6359. //对于终端初始化安装时,直接通过。
  6360. if (isTerminalInstall()) {
  6361. doInstall = true;
  6362. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 create InstallPackTask terminal new install");
  6363. }
  6364. if (doInstall)
  6365. {
  6366. //解压后判断是什么类型升级
  6367. judgeTaskClass();
  6368. Task::InstallPackTask* iTask = new Task::InstallPackTask(this);
  6369. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(iTask);
  6370. if (rc != Error_Succeed) {
  6371. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 create InstallPackTask fail,err=%d", (int)rc);
  6372. ScheduleTimer(3, 30 * 1000);//等待30秒再安装
  6373. }
  6374. }
  6375. else {
  6376. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 UIState is not M , create InstallPackTask wait for the next time");
  6377. ScheduleTimer(3, 30 * 1000);//等待30秒再安装
  6378. }
  6379. }
  6380. else if (event->param1 == 4) {
  6381. //已安装成功,直接进入切换状态
  6382. DbgWithLink(LOG_LEVEL_DEBUG, LOG_TYPE_SYSTEM)("s5 create InstallPackTask succ, enter into S6");
  6383. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartSwitch));//进入S6
  6384. }
  6385. else if (event->param1 == 5) {
  6386. Task::RollBackCheckTask* iTask = new Task::RollBackCheckTask(this);
  6387. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(iTask);
  6388. if (rc != Error_Succeed) {
  6389. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 create RollBackCheckTask fail,err=%d", (int)rc);
  6390. ScheduleTimer(5, 30 * 1000);//等待30秒再次检查
  6391. }
  6392. }
  6393. }
  6394. else if(event->iEvt == Event_EndStopMediaPlayTask){
  6395. StopMediaPlayTaskEvent* pEvent = (StopMediaPlayTaskEvent*)event;
  6396. if(pEvent->isSucc){
  6397. if (m_currentTask.task_type.Compare("U") == 0) {
  6398. //开始解压
  6399. Task::UnzipPackTask* iTask = new Task::UnzipPackTask(this);
  6400. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(iTask);
  6401. if (rc != Error_Succeed) {
  6402. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 create UnzipPackTask fail,err=%d", (int)rc);
  6403. ScheduleTimer(2, 30 * 1000);//等待30秒再解压包
  6404. }
  6405. }
  6406. else if (m_currentTask.task_type.Compare("R") == 0) {
  6407. //回退检查
  6408. Task::RollBackCheckTask* iTask = new Task::RollBackCheckTask(this);
  6409. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(iTask);
  6410. if (rc != Error_Succeed) {
  6411. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 create RollBackCheckTask fail,err=%d", (int)rc);
  6412. ScheduleTimer(5, 30 * 1000);//等待30秒再检查
  6413. }
  6414. }
  6415. }else{
  6416. ScheduleTimer(1, 30*1000);//等待30秒再停止播放广告和音频
  6417. }
  6418. }
  6419. else if (event->iEvt == Event_EndUnzipPackTask){
  6420. UnzipPackTaskEvent* pEvent = (UnzipPackTaskEvent*)event;
  6421. if(pEvent->isSucc){
  6422. //开始安装
  6423. BOOL doInstall = true;
  6424. if (!m_startFlag) //非强制安装
  6425. {
  6426. if (m_currentTask.pack_version.GetLength() == 0) //体系外升级
  6427. {
  6428. CSimpleStringA strState;
  6429. ErrorCodeEnum err = m_pEntity->GetFunction()->GetSysVar("UIState", strState);
  6430. if (err == Error_Succeed && strState != CSimpleStringA("M")) //非主界面不安装
  6431. {
  6432. doInstall = false;
  6433. }
  6434. }
  6435. }
  6436. //对于终端初始化安装时,直接通过。
  6437. if (isTerminalInstall()) {
  6438. doInstall = true;
  6439. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 create InstallPackTask terminal new install");
  6440. }
  6441. if (doInstall)
  6442. {
  6443. //解压后判断是什么类型升级
  6444. judgeTaskClass();
  6445. Task::InstallPackTask* iTask = new Task::InstallPackTask(this);
  6446. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(iTask);
  6447. if (rc != Error_Succeed) {
  6448. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 create InstallPackTask fail,err=%d", (int)rc);
  6449. ScheduleTimer(3, 30 * 1000);//等待30秒再安装
  6450. }
  6451. }
  6452. else{
  6453. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("s5 UIState is not M , create InstallPackTask wait for the next time");
  6454. ScheduleTimer(3, 30 * 1000);//等待30秒再安装
  6455. }
  6456. }else{
  6457. if(m_currentTask.stop_media_play){
  6458. //恢复广告和音频播放
  6459. ErrorCodeEnum rc = m_pEntity->GetFunction()->SetSysVar("UpdateState", "0", true);
  6460. if(Error_Succeed != rc){
  6461. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("unzip fail set UpdateState to 0 fail, err=%d",(int)rc);
  6462. }
  6463. }
  6464. //解压失败,写入运行时文件,状态为C
  6465. if(SetRunConfigStrValue("upgradeTask","PendingState","C")==Error_Succeed){
  6466. //告警下载取消
  6467. struct CTaskProcess up;
  6468. up.strPackName = m_currentTask.pack_name;//安装包名
  6469. up.installVersion = m_currentTask.pack_version;//安装的版本
  6470. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6471. up.cInstallState = "C";
  6472. up.strInstallComment = "解压失败";
  6473. SendUpgradeTaskProgress(up,UpgradeMgr_INSTALL_ID,"RTA5662");
  6474. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("unzip pack is fail ,prepare to s2");
  6475. m_currentTask.cPendingState = "C";//改变内存变量C
  6476. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  6477. }else{
  6478. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndUnzipPackTask write runinfo is fail,retry int 5's");
  6479. Sleep(5 * 1000);//等待五秒
  6480. PostEventFIFO(new UnzipPackTaskEvent(false));//继续写运行时文件
  6481. }
  6482. }
  6483. }
  6484. else if (event->iEvt == Event_EndInstallPackTask){
  6485. if(m_currentTask.stop_media_play){
  6486. //恢复广告和音频播放
  6487. ErrorCodeEnum rc = m_pEntity->GetFunction()->SetSysVar("UpdateState", "0", true);
  6488. if(Error_Succeed != rc){
  6489. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("EndInstallPackTask set UpdateState to 0 fail, err=%d",(int)rc);
  6490. }
  6491. }
  6492. InstallPackTaskEvent* pEvent = (InstallPackTaskEvent*)event;
  6493. if(pEvent->isSucc){
  6494. //安装成功进入切换,设置状态为S和新版本号
  6495. //写入运行时文件
  6496. bool isWriteOk=false;
  6497. while(true){
  6498. if (!m_TempDepUpgradeFlag) {
  6499. if (SetRunConfigStrValue("upgradeTask", "NewVersion", m_currentTask.NewVersion.GetData()) != Error_Succeed) {
  6500. break;
  6501. }
  6502. }
  6503. else {
  6504. if (SetRunConfigStrValue("upgradeTask", "depNewVersion", m_currentTask.depNewVersion.GetData()) != Error_Succeed) {
  6505. break;
  6506. }
  6507. }
  6508. if(SetRunConfigStrValue("upgradeTask","PendingState","S")!=Error_Succeed){
  6509. break;
  6510. }
  6511. isWriteOk=true;
  6512. break;
  6513. }
  6514. if(isWriteOk){
  6515. //告警安装
  6516. struct CTaskProcess up;
  6517. up.strPackName = m_currentTask.pack_name;//安装包名
  6518. if(!m_currentTask.NewVersion.IsNullOrEmpty()){
  6519. up.installVersion = m_currentTask.NewVersion;//任务升级后的版本号
  6520. }else if(!m_currentTask.depNewVersion.IsNullOrEmpty()){
  6521. up.installVersion = m_currentTask.depNewVersion;//升级后的适配器版本号
  6522. }else {
  6523. up.installVersion = m_currentVersion.ToString();//当前版本号
  6524. }
  6525. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6526. up.cInstallState = "W";
  6527. up.strInstallComment = "安装成功";
  6528. SendUpgradeTaskProgress(up,UpgradeMgr_INSTALL_ID);
  6529. m_currentTask.cPendingState = "S";//改变内存变量S
  6530. //modify by lwt 修改为非兜底升级时等待一段时间再进入切换状态。
  6531. if (m_startFlag) {
  6532. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartSwitch));//进入S6
  6533. }
  6534. else {
  6535. ScheduleTimer(4, 20 * 1000);//等待20秒再进入切换状态
  6536. }
  6537. }else{
  6538. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndInstallPackTask write runinfo is fail,retry int 5's");
  6539. Sleep(5 * 1000);//等待五秒
  6540. PostEventFIFO(new InstallPackTaskEvent(true,"",0));//继续写运行时文件
  6541. }
  6542. }else{
  6543. if(pEvent->failStatus==0){
  6544. //安装失败,设置状态为C
  6545. if(SetRunConfigStrValue("upgradeTask","PendingState","C")==Error_Succeed){
  6546. //告警下载取消
  6547. struct CTaskProcess up;
  6548. up.strPackName = m_currentTask.pack_name;//安装包名
  6549. up.installVersion = m_currentTask.pack_version;//安装的版本
  6550. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6551. up.cInstallState = "C";
  6552. up.strInstallComment = pEvent->errMsg;//错误信息
  6553. SendUpgradeTaskProgress(up,UpgradeMgr_INSTALL_ID,"RTA5663");
  6554. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("install pack is fail ,prepare to s2");
  6555. m_currentTask.cPendingState = "C";//改变内存变量C
  6556. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  6557. }else{
  6558. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndInstallPackTask write runinfo is fail,retry int 5's");
  6559. Sleep(5 * 1000);//等待五秒
  6560. PostEventFIFO(new InstallPackTaskEvent(false,pEvent->errMsg,0));//继续写运行时文件
  6561. }
  6562. }else if(pEvent->failStatus==-1){
  6563. //安装失败,设置状态为X 未知
  6564. if(SetRunConfigStrValue("upgradeTask","PendingState","X")==Error_Succeed){
  6565. //告警下载取消
  6566. struct CTaskProcess up;
  6567. up.strPackName = m_currentTask.pack_name;//安装包名
  6568. up.installVersion = m_currentTask.pack_version;//安装的版本
  6569. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6570. up.cInstallState = "X";
  6571. up.strInstallComment = pEvent->errMsg;//错误信息
  6572. SendUpgradeTaskProgress(up,UpgradeMgr_INSTALL_ID,"RTA5664");
  6573. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("install pack is fail ,prepare to s2");
  6574. m_currentTask.cPendingState = "X";//改变内存变量X
  6575. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  6576. }else{
  6577. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("Event_EndInstallPackTask write runinfo is fail,retry int 5's");
  6578. Sleep(5 * 1000);//等待五秒
  6579. PostEventFIFO(new InstallPackTaskEvent(false,pEvent->errMsg,-1));//继续写运行时文件
  6580. }
  6581. }
  6582. }
  6583. }
  6584. else if (event->iEvt == Event_EndRollBackCheckTask) {
  6585. if (m_currentTask.stop_media_play) {
  6586. //恢复广告和音频播放
  6587. ErrorCodeEnum rc = m_pEntity->GetFunction()->SetSysVar("UpdateState", "0", true);
  6588. if (Error_Succeed != rc) {
  6589. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("EndInstallPackTask set UpdateState to 0 fail, err=%d", (int)rc);
  6590. }
  6591. }
  6592. RollBackCheckTaskEvent* pEvent = (RollBackCheckTaskEvent*)event;
  6593. if (pEvent->isSucc) {
  6594. bool isWriteOk = false;
  6595. while (true) {
  6596. if (!m_TempDepUpgradeFlag) {
  6597. if (SetRunConfigStrValue("upgradeTask", "NewVersion", m_currentTask.NewVersion.GetData()) != Error_Succeed) {
  6598. break;
  6599. }
  6600. }
  6601. else {
  6602. if (SetRunConfigStrValue("upgradeTask", "depNewVersion", m_currentTask.depNewVersion.GetData()) != Error_Succeed) {
  6603. break;
  6604. }
  6605. }
  6606. isWriteOk = true;
  6607. break;
  6608. }
  6609. if (isWriteOk) {
  6610. m_currentTask.cPendingState = "S";//改变内存变量S
  6611. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("终端回退版本检查成功");
  6612. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartSwitch));//进入S6
  6613. }
  6614. else {
  6615. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Event_EndRollBackCheckTask write runinfo is fail,retry int 5's");
  6616. Sleep(5 * 1000);//等待五秒
  6617. PostEventFIFO(new RollBackCheckTaskEvent(true, "", ""));//继续写运行时文件
  6618. }
  6619. }
  6620. else {
  6621. //失败
  6622. if (pEvent->failReason.compare("X") != 0) {
  6623. //回退失败,设置状态为C
  6624. if (SetRunConfigStrValue("upgradeTask", "PendingState", "C") == Error_Succeed) {
  6625. //回滚失败取消
  6626. struct CTaskProcess up;
  6627. up.strPackName = "";//安装包名
  6628. up.installVersion = m_currentTask.pack_version;//回退的版本
  6629. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6630. up.cInstallState = "C";
  6631. up.strInstallComment = pEvent->errMsg.GetData();//错误信息
  6632. SendUpgradeTaskProgress(up,UpgradeMgr_ROLLBACK_ID,"RTA5668");
  6633. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("RollBackCheck is fail ,prepare to s2");
  6634. m_currentTask.cPendingState = "C";//改变内存变量C
  6635. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("终端回退版本检查失败");
  6636. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  6637. }
  6638. else {
  6639. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Event_EndRollBackCheckTask write runinfo is fail,retry int 5's");
  6640. Sleep(5 * 1000);//等待五秒
  6641. PostEventFIFO(new RollBackCheckTaskEvent(false, pEvent->errMsg, "C"));//继续写运行时文件
  6642. }
  6643. }
  6644. else if (pEvent->failReason.compare("X")==0) {
  6645. //安装失败,设置状态为X 未知
  6646. if (SetRunConfigStrValue("upgradeTask", "PendingState", "X") == Error_Succeed) {
  6647. //告警下载取消
  6648. struct CTaskProcess up;
  6649. up.strPackName = "";//安装包名
  6650. up.installVersion = m_currentTask.pack_version;//回退的版本
  6651. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6652. up.cInstallState = "X";
  6653. up.strInstallComment = pEvent->errMsg.GetData();//错误信息
  6654. SendUpgradeTaskProgress(up,UpgradeMgr_ROLLBACK_ID,"RTA5669");
  6655. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("RollBackCheck is fail ,prepare to s2");
  6656. m_currentTask.cPendingState = "X";//改变内存变量X
  6657. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_USER)("终端回退版本检查失败");
  6658. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  6659. }
  6660. else {
  6661. DbgWithLink(LOG_LEVEL_INFO, LOG_TYPE_SYSTEM)("Event_EndRollBackCheckTask write runinfo is fail,retry int 5's");
  6662. Sleep(5 * 1000);//等待五秒
  6663. PostEventFIFO(new RollBackCheckTaskEvent(false, pEvent->errMsg, "X"));//继续写运行时文件
  6664. }
  6665. }
  6666. }
  6667. }
  6668. return 0;
  6669. }
  6670. void CUpgradeTaskFSM::s6_on_entry()
  6671. {
  6672. //进入切换处理流程:不同类型包判断不同
  6673. //时间长,需要判断后台是否取消任务
  6674. if (CheckIfCanSwitchNow())
  6675. {
  6676. ScheduleTimer(2,1000);//进行版本切换操作
  6677. }else{
  6678. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("not applicable time to switch, retry in 20's");
  6679. ScheduleTimer(1, 20*1000);//继续等待
  6680. }
  6681. ScheduleTimer(3, 30*1000);//启动查询任务是否有效
  6682. }
  6683. void CUpgradeTaskFSM::s6_on_exit()
  6684. {
  6685. CancelTimer(1);//判断是否切换时间计时器
  6686. CancelTimer(2);//切换运行时文件写入计时器
  6687. CancelTimer(3);//任务取消计时器
  6688. CancelTimer(4);//切换重启写入active文件计时器
  6689. CancelTimer(5);//active文件写入失败写运行时计时器
  6690. m_bSwitchFlag = false;//切换标志重置。
  6691. }
  6692. unsigned int CUpgradeTaskFSM::s6_on_event(FSMEvent* event)
  6693. {
  6694. if (event->iEvt == EVT_TIMER)
  6695. {
  6696. if(event->param1==1)
  6697. {
  6698. //进入切换处理流程:不同类型包判断不同
  6699. if (CheckIfCanSwitchNow())
  6700. {
  6701. ScheduleTimer(2,1000);//进行版本切换操作
  6702. }else{
  6703. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("not applicable time to switch, retry in 20's");
  6704. ScheduleTimer(1, 20*1000);//继续等待切换时机
  6705. }
  6706. }else if(event->param1==2){
  6707. //修改运行时文件状态T
  6708. if(SetRunConfigStrValue("upgradeTask","PendingState","T")==Error_Succeed){
  6709. //告警切换
  6710. struct CTaskProcess up;
  6711. up.strPackName = m_currentTask.pack_name;//安装包名
  6712. if (!m_currentTask.NewVersion.IsNullOrEmpty()) {
  6713. up.installVersion = m_currentTask.NewVersion;//任务升级后的版本号
  6714. }else if (!m_currentTask.depNewVersion.IsNullOrEmpty()) {
  6715. up.installVersion = m_currentTask.depNewVersion;//升级后的适配器版本号
  6716. }else {
  6717. up.installVersion = m_currentVersion.ToString();//当前版本号
  6718. }
  6719. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6720. up.cInstallState = "S";
  6721. up.strInstallComment = "已设为启动版本";
  6722. if (m_currentTask.task_type.Compare("U")==0) {
  6723. SendUpgradeTaskProgress(up,UpgradeMgr_SWTICH_ID);
  6724. }
  6725. else{
  6726. SendUpgradeTaskProgress(up,UpgradeMgr_ROLLBACK_SWTICH_ID);
  6727. }
  6728. m_currentTask.cPendingState = "T";//改变内存变量
  6729. ScheduleTimer(4,5*1000);//等待告警上报,准备修改active文件,进行切换
  6730. }else{
  6731. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("switch write runinfo is fail,retry in 10's");
  6732. ScheduleTimer(1, 10*1000);//继续等待下次切换机会再重试
  6733. }
  6734. }else if(event->param1==3){
  6735. Task::QueryTaskStatus* dTask = new Task::QueryTaskStatus(this);
  6736. ErrorCodeEnum rc = m_pEntity->GetFunction()->PostThreadPoolTask(dTask);
  6737. if(rc!=Error_Succeed){
  6738. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("s6 create QueryTaskStatus fail,err=%d",(int)rc);
  6739. ScheduleTimer(3, 30*1000);//再次查询任务是否有效
  6740. }
  6741. }else if(event->param1==4){
  6742. //修改active文件
  6743. if (!m_TempDepUpgradeFlag) {
  6744. if (UpgradeToNewVersion() == Error_Succeed) {
  6745. SwitchUpgradeNow();//切换版本
  6746. }
  6747. else {
  6748. ScheduleTimer(5, 3 * 1000);//UpgradeToNewVersion执行失败后写运行时计时器
  6749. }
  6750. }
  6751. else {
  6752. if (UpgradeDepToNewVersion() == Error_Succeed) {
  6753. SwitchUpgradeDepNow();//适配器切换版本
  6754. }
  6755. else {
  6756. ScheduleTimer(5, 3 * 1000);//UpgradeToDepNewVersion执行失败后写运行时计时器
  6757. }
  6758. }
  6759. }else if(event->param1==5){
  6760. if(SetRunConfigStrValue("upgradeTask","PendingState","C")==Error_Succeed){
  6761. //告警切换失败
  6762. struct CTaskProcess up;
  6763. up.strPackName = m_currentTask.pack_name;//安装包名
  6764. if (!m_currentTask.NewVersion.IsNullOrEmpty()) {
  6765. up.installVersion = m_currentTask.NewVersion;//任务升级后的版本号
  6766. }else if (!m_currentTask.depNewVersion.IsNullOrEmpty()) {
  6767. up.installVersion = m_currentTask.depNewVersion;//升级后的适配器版本号
  6768. }else {
  6769. up.installVersion = m_currentVersion.ToString();//当前版本号
  6770. }
  6771. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6772. up.cInstallState = "C";
  6773. up.strInstallComment = "更新active文件失败";//错误信息
  6774. if (m_currentTask.task_type.Compare("U")==0) {
  6775. SendUpgradeTaskProgress(up,UpgradeMgr_SWTICH_ID,"RTA5665");
  6776. }
  6777. else {
  6778. SendUpgradeTaskProgress(up, UpgradeMgr_ROLLBACK_SWTICH_ID, "RTA5670");
  6779. }
  6780. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("UpgradeToNewVersion is fail ,prepare to s2");
  6781. m_currentTask.cPendingState = "C";//改变内存变量C
  6782. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartDealTask));//进入S2
  6783. }else{
  6784. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("UpgradeToNewVersion fail, write runinfo is fail,retry in 10's");
  6785. ScheduleTimer(5,10*1000);//继续重试写入运行时
  6786. }
  6787. }
  6788. }else if (event->iEvt == Event_EndQueryTaskStatus){
  6789. QueryTaskStatusEvent* pEvent = (QueryTaskStatusEvent*)event;
  6790. if(pEvent->isSucc){
  6791. if(pEvent->isTask_valid){
  6792. ScheduleTimer(3, 30*1000);//有效,再次查询任务是否有效
  6793. }else{
  6794. //无效,清理任务
  6795. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("query upgradeTask is cancel");
  6796. if(ClearCurrTaskInfo()){
  6797. //告警下载取消
  6798. struct CTaskProcess up;
  6799. up.strPackName = m_currentTask.pack_name;//安装包名
  6800. up.installVersion = m_currentTask.pack_version;//安装的版本
  6801. up.CurrentVersion = getAlarmCurrVersion();//当前的版本
  6802. up.cInstallState = "C";
  6803. up.strInstallComment = "任务策略已取消";
  6804. if (m_currentTask.task_type.Compare("U")==0) {
  6805. SendUpgradeTaskProgress(up, UpgradeMgr_SWTICH_ID,"RTA5666");
  6806. }
  6807. else {
  6808. SendUpgradeTaskProgress(up, UpgradeMgr_ROLLBACK_SWTICH_ID, "RTA5671");
  6809. }
  6810. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("upgradeTask is cancel,prepare to s3");
  6811. PostEventFIFO(new FSMEvent(CUpgradeTaskFSM::Event_StartPoll));//进入S3
  6812. }else{
  6813. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("clear current task is fail 1,retry in 5's");
  6814. Sleep(5 * 1000);//等待五秒,继续清理
  6815. PostEventFIFO(new QueryTaskStatusEvent(true,false));//继续取消任务
  6816. }
  6817. }
  6818. }else{
  6819. ScheduleTimer(3, 30*1000);//通讯失败,再次查询任务是否有效
  6820. }
  6821. }
  6822. return 0;
  6823. }
  6824. void CUpgradeTaskFSM::s7_on_entry()
  6825. {
  6826. DbgWithLink(LOG_LEVEL_INFO,LOG_TYPE_SYSTEM)("enter disable state , wait framework restart or OS restart");
  6827. }
  6828. void CUpgradeTaskFSM::s7_on_exit()
  6829. {
  6830. }
  6831. unsigned int CUpgradeTaskFSM::s7_on_event(FSMEvent* event)
  6832. {
  6833. return 0;
  6834. }