UpgradeTaskFSM.cpp 295 KB

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