123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337 |
- #include "stdafx.h"
- #include "SpBase.h"
- #include "UpgradeMgrFSM.h"
- #include "mod_UpgradeMgr.h"
- #include "mod_UpgradeMgr.h"
- #include <algorithm>
- //#include "EventCode.h"
- #include "fileutil.h"
- #include "WMIDeviceQuery.h"
- #include "UpgradeManager_msg_g.h"
- #include "..\mod_pinpad\PinPad_client_g.h"
- using namespace PinPad;
- #include "..\..\DeviceAdapter\DevInc\DeviceBaseClass.h"
- #include "..\EventCode.h"
- // 上报状态机(并负责轮询升级)
- #define UPGRADE_POLL_INTERVAL 30000 // 升级轮询间隔
- //struct testTask : public ITaskSp
- //{
- // CReportMgrFSM* fsm;
- // explicit testTask(CReportMgrFSM* f) : fsm(f) {}
- //
- // /*void Process()
- // {
- // int terminalNo = 1015010014;
- // int blockId = 1;
- // while (terminalNo < 1015010114)
- // {
- // fsm->m_pConnection->SendMD5ListReq(CSimpleStringA::Format("%d",terminalNo),CSimpleStringA::Format("%d",blockId));
- // terminalNo++;
- // }
- // }*/
- //};
- void CReportMgrFSM::OnStateTrans(int iSrcState, int iDstState)
- {
- Dbg("ReportMgrFSM trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
- }
- void CReportMgrFSM::s1_on_entry()
- {
- ScheduleTimer(1, UPGRADE_POLL_INTERVAL);
- }
- void CReportMgrFSM::s1_on_exit()
- {
- CancelTimer(1);
- }
-
- unsigned int CReportMgrFSM::s1_on_event(FSMEvent* event)
- {
- if (event->iEvt == Event_ReportState)
- {
- ReportStateEvent *pEvent = (ReportStateEvent*)event;
- CReportInfo info(pEvent->strPackName, pEvent->InstallVersion, pEvent->dwErrorCode, pEvent->cInstallState, pEvent->strComment);
- m_PendingReports.push_back(info);
-
- PostEventFIFO(new FSMEvent(Event_StartConnect));
- }
- else if (event->iEvt == Event_SwitchConfirm)
- {
- m_strToConfirmExecID = ((SwitchConfirmEvent*)event)->strPackExecID;
- PostEventFIFO(new FSMEvent(Event_StartConnect));
- }
- else if (event->iEvt == Event_QueryExecInfo)
- {
- m_strToQueryExecID = ((QueryExecInfoEvent*)event)->strPackExecID;
- PostEventFIFO(new FSMEvent(Event_StartConnect));
- }
- else if (event->iEvt == Event_StartPoll)
- {
- Dbg("report fsm receive Event_StartPoll 1");
- m_bNeedPoll = true;
- PostEventFIFO(new FSMEvent(Event_StartConnect));
- }
- else if (event->iEvt == EVT_TIMER)
- {
- Dbg("EVT_TIMER, m_bNeedPoll set true");
- m_bNeedPoll = true;
- PostEventFIFO(new FSMEvent(Event_StartConnect));
- }
- else if (event->iEvt == Event_ReportSysCustomVer)
- {
- auto pEvent = (ReportSysCustomVerEvent *)event;
- m_strSysCustomPack = pEvent->strPackName;
- m_strSysCustomVer = pEvent->strSysCustomVer;
- m_strFWID = pEvent->strFWID;
- m_strSysPatchName = pEvent->strSysPatchName;
- PostEventFIFO(new FSMEvent(Event_StartConnect));
- }
- else if (event->iEvt == Event_SendMD5list)
- {
- auto pEvent = (SendMD5ListEvent *)event;
- m_strBlockId = pEvent->BlockId;
- PostEventFIFO(new FSMEvent(Event_StartConnect));
- }
- return 0;
- }
-
- void CReportMgrFSM::s2_on_entry()
- {
- if (SecureClientConnect() != Error_Succeed)
- {
- ScheduleTimer(1, 30000);
- Dbg("connect to upgrade service fail, retry in 30 seconds");
- }
- else
- {
- PostEventFIFO(new FSMEvent(Event_Connected));
- }
- }
-
- void CReportMgrFSM::s2_on_exit()
- {
- //解决sonar扫描
- }
-
- unsigned int CReportMgrFSM::s2_on_event(FSMEvent* event)
- {
- if (event->iEvt == Event_ReportState)
- {
- ReportStateEvent *pEvent = (ReportStateEvent*)event;
- CReportInfo info(pEvent->strPackName, pEvent->InstallVersion, pEvent->dwErrorCode, pEvent->cInstallState, pEvent->strComment);
- m_PendingReports.push_back(info);
- }
- else if (event->iEvt == Event_SwitchConfirm)
- {
- m_strToConfirmExecID = ((SwitchConfirmEvent*)event)->strPackExecID;
- }
- else if (event->iEvt == Event_QueryExecInfo)
- {
- m_strToQueryExecID = ((QueryExecInfoEvent*)event)->strPackExecID;
- }
- else if (event->iEvt == Event_StartPoll)
- {
- Dbg("report fsm receive Event_StartPoll 2");
- m_bNeedPoll = true;
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if (SecureClientConnect() != Error_Succeed)
- {
- ScheduleTimer(1, 30000);
- Dbg("connect to upgrade service fail, retry in 30 seconds");
- }
- else
- {
- PostEventFIFO(new FSMEvent(Event_Connected));
- }
- }
- else if (event->iEvt == Event_ReportSysCustomVer)
- {
- auto pEvent = (ReportSysCustomVerEvent *)event;
- m_strSysCustomPack = pEvent->strPackName;
- m_strSysCustomVer = pEvent->strSysCustomVer;
- m_strFWID = pEvent->strFWID;
- m_strSysPatchName = pEvent->strSysPatchName;
- }
- else if (event->iEvt == Event_SendMD5list)
- {
- auto pEvent = (SendMD5ListEvent *)event;
- m_strBlockId = pEvent->BlockId;
- }
-
- return 0;
- }
- void CReportMgrFSM::s3_on_entry()
- {
- // 检查持久化队列
- for(auto it = m_PendingReports.begin(); it!= m_PendingReports.end(); it++)
- {
- m_pConnection->ReportUpgradeResult(it->strPackName, it->InstallVersion, it->cInstallState, it->dwStateTime, it->dwFailCode, it->strComment);
- }
- m_PendingReports.clear();
-
- if (!m_strToConfirmExecID.IsNullOrEmpty())
- {
- m_pConnection->SendConfirmUpgradeReq(m_strToConfirmExecID);
- m_strToConfirmExecID.Clear();
- }
-
- if (!m_strToQueryExecID.IsNullOrEmpty())
- {
- m_pConnection->SendQueryExecInfoReq(m_strToQueryExecID);
- m_strToQueryExecID.Clear();
- }
- if (!m_strSysCustomPack.IsNullOrEmpty()
- && (!m_strSysCustomVer.IsNullOrEmpty() || !m_strFWID.IsNullOrEmpty() || !m_strSysPatchName.IsNullOrEmpty()))
- {
- m_pConnection->ReportSysCustomVer(m_strSysCustomPack, m_strSysCustomVer, m_strFWID, m_strSysPatchName);
- m_strSysCustomPack.Clear();
- m_strSysCustomVer.Clear();
- m_strFWID.Clear();
- m_strSysPatchName.Clear();
- }
- if (m_bNeedPoll)
- {
- Dbg("SendPollUpgradeReq 1");
- m_pConnection->SendPollUpgradeReq();
- m_bNeedPoll = false;
- }
- if (!m_strBlockId.IsNullOrEmpty())
- {
- m_pConnection->SendMD5ListReq(m_strBlockId);
- m_strBlockId.Clear();
- }
- m_LastReqTime = CSmallDateTime::GetNow();
- ScheduleTimer(1, 3000);
- /*testTask* task = new testTask(this);
- GetEntityBase()->GetFunction()->PostThreadPoolTask(task);*/
- }
- void CReportMgrFSM::s3_on_exit()
- {
- SecureClientRelease();
- }
- unsigned int CReportMgrFSM::s3_on_event(FSMEvent* event)
- {
- if (event->iEvt == Event_ReportState)
- {
- ReportStateEvent *pEvent = (ReportStateEvent*)event;
- m_pConnection->ReportUpgradeResult(pEvent->strPackName, pEvent->InstallVersion, pEvent->cInstallState, CSmallDateTime::GetNow(), pEvent->dwErrorCode, pEvent->strComment);
- m_LastReqTime = CSmallDateTime::GetNow();
- }
- else if (event->iEvt == Event_StartPoll)
- {
- // 防止过度查询
- if (CSmallDateTime::GetNow() - m_LastPollTime >= UPGRADE_POLL_INTERVAL / 1000)
- {
- Dbg("SendPollUpgradeReq 2");
- m_pConnection->SendPollUpgradeReq();
- m_LastPollTime = CSmallDateTime::GetNow();
- m_LastReqTime = CSmallDateTime::GetNow();
- }
- }
- else if (event->iEvt == Event_SendMD5list)
- {
- auto pEvent = (SendMD5ListEvent *)event;
- m_strBlockId = pEvent->BlockId;
- m_pConnection->SendMD5ListReq(m_strBlockId);
- m_strBlockId.Clear();
- m_LastReqTime = CSmallDateTime::GetNow();
- }
- else if (event->iEvt == Event_SwitchConfirm)
- {
- // 切换确认
- auto pEvent = (SwitchConfirmEvent*)event;
- m_pConnection->SendConfirmUpgradeReq(pEvent->strPackExecID);
- m_LastReqTime = CSmallDateTime::GetNow();
- }
- else if (event->iEvt == CUpgradeMgrFSM::Event_EndPoll
- || event->iEvt == CUpgradeMgrFSM::Event_EndConfirm
- || event->iEvt == CUpgradeMgrFSM::Event_EndQueryExec
- || event->iEvt == CUpgradeMgrFSM::Event_CancelUpgrade
- /*|| event->iEvt == CUpgradeMgrFSM::Event_ContinueMD5
- || event->iEvt == CUpgradeMgrFSM::Event_StartMD5
- || event->iEvt == CUpgradeMgrFSM::Event_EndMD5
- || event->iEvt == CUpgradeMgrFSM::Event_RetryMD5*/)
- {
- // 转交给外层状态机处理
- event->IncRef(); // 防止被释放
- m_pOuterFSM->PostEventFIFO(event);
- }
- else if (event->iEvt == Event_QueryExecInfo)
- {
- auto pEvent = (QueryExecInfoEvent*)event;
- m_pConnection->SendQueryExecInfoReq(pEvent->strPackExecID);
- m_LastReqTime = CSmallDateTime::GetNow();
- }
- else if (event->iEvt == Event_ReportSysCustomVer)
- {
- auto pEvent = (ReportSysCustomVerEvent *)event;
- m_pConnection->ReportSysCustomVer(pEvent->strPackName, pEvent->strSysCustomVer, pEvent->strFWID, pEvent->strSysPatchName);
- m_LastReqTime = CSmallDateTime::GetNow();
- }
- else if (event->iEvt == EVT_TIMER)
- {
- if ((DWORD)CSmallDateTime::GetNow() - (DWORD)m_LastReqTime >= 90)
- {
- // 空闲5秒以上
- PostEventFIFO(new FSMEvent(Event_Disconnected));
- }
- else
- {
- ScheduleTimer(1, 3000);
- }
- }
- return 0;
- }
- ErrorCodeEnum CReportMgrFSM::SecureClientConnect()
- {
- if (m_pConnection != NULL)
- {
- if (m_pConnection->IsConnectionOK())
- return Error_Succeed;
- else
- SecureClientRelease();
- }
-
- m_pConnection = new UpgradeConnect::CUpgradeMgrConn(m_pEntity, this);
- if (m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK())
- return Error_Succeed;
- SecureClientRelease();
- return Error_PeerReject;
- }
- ErrorCodeEnum CReportMgrFSM::SecureClientRelease()
- {
- if (m_pConnection != NULL)
- {
- m_pConnection->Close();
- m_pConnection->DecRefCount();
- m_pConnection = NULL;
- }
- return Error_Succeed;
- }
- CUpgradeMgrFSM::CUpgradeMgrFSM()
- {
- m_ReportFSM.SetOuterFSM(this);
- }
- ErrorCodeEnum CUpgradeMgrFSM::OnInit()
- {
- m_ReportFSM.Init(m_pEntity);
- AddStateHooker(this);
- //modify by lwt 20200610 增加hash文件黑名单和白名单配置文件读取
- CSmartPointer<IConfigInfo> spConfig;
- auto rc = m_pEntity->GetFunction()->OpenConfig(Config_Software, spConfig);
- if (rc == Error_Succeed){
- CSimpleStringA str="";
- ErrorCodeEnum Error = spConfig->ReadConfigValue("HashFilter", "DepWhitelist", str);
- if(Error==Error_Succeed){
- CAutoArray<CSimpleStringA> arr = str.Split('|');
- for(int i=0; i< arr.GetCount(); i++)
- {
- m_DepWhitelist.push_back(arr[i]);
- }
- Dbg("init read [HashFilter] DepWhitelist=%s",str);
- }else{
- LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini DepWhitelist error");
- }
- Error = spConfig->ReadConfigValue("HashFilter", "DirBlacklist", str);
- if(Error==Error_Succeed){
- CAutoArray<CSimpleStringA> arr = str.Split('|');
- for(int i=0; i< arr.GetCount(); i++)
- {
- m_DirBlacklist.push_back(arr[i]);
- }
- Dbg("init read [HashFilter] DirBlacklist=%s",str);
- }else{
- LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini DirBlacklist error");
- }
- Error = spConfig->ReadConfigValue("HashFilter", "FileBlacklist", str);
- if(Error==Error_Succeed){
- CAutoArray<CSimpleStringA> arr = str.Split('|');
- for(int i=0; i< arr.GetCount(); i++)
- {
- m_FileBlacklist.push_back(arr[i]);
- }
- Dbg("init read [HashFilter] FileBlacklist=%s",str);
- }else{
- LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini FileBlacklist error");
- }
- }else{
- LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini error");
- }
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::SendMD5ListEvent("1"));
- return Error_Succeed;
- }
- ErrorCodeEnum CUpgradeMgrFSM::OnExit()
- {
- RemoveStateHooker(this);
- return Error_Succeed;
- }
- char CUpgradeMgrFSM::GetInstallState(int nState)
- {
- if (nState == s3)
- return 'U';
- else if (nState == s4)
- return 'I';
- else if (nState == s5)
- return 'C';
- else if (nState == s6)
- return 'S';
- else
- return 'N';
- }
- void CUpgradeMgrFSM::OnStateTrans(int iSrcState, int iDstState)
- {
- Dbg("state trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
- if (iSrcState >= s3 && iDstState <= s2)
- {
- // 完成或取消升级,清空运行时配置
- Dbg("clear upgrade info from runcfg");
- SetRunConfigStrValue("Install", "Pack", "");
- SetRunConfigStrValue("Install", "TriggerType", "");
- SetRunConfigHexIntValue("Install", "TriggerTime", 0);
- SetRunConfigStrValue("Install", "PendingState", "");
- SetRunConfigStrValue("Install", "bSysInstall", "");
- SetRunConfigStrValue("Install", "PackExecID", "");
- SetRunConfigStrValue("Install", "NewVersion", "");
- SetRunConfigHexIntValue("Install", "DownloadTime", 0);
- // 发送广播(通知业务界面清除状态)
- UpgradeStateEvent stateEvent;
- stateEvent.strPackName = "";
- stateEvent.strExecID = "";
- stateEvent.cInstallState = 'N';
- stateEvent.bSysInstall = false;
- stateEvent.bLightPack = false;
- stateEvent.strNewVersion = "";
- SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
- }
- else if (iDstState >= s3)
- {
- assert(HasRunningUpdate());
- SetRunConfigStrValue("Install", "Pack", m_strIntallingPack);
- if (iDstState == s3)
- SetRunConfigStrValue("Install", "PendingState", "U");
- else if (iDstState == s4)
- SetRunConfigStrValue("Install", "PendingState", "I");
- else if (iDstState == s5)
- SetRunConfigStrValue("Install", "PendingState", "C");
- else if (iDstState == s6)
- SetRunConfigStrValue("Install", "PendingState", "S");
- // 发送广播
- UpgradeStateEvent stateEvent;
- stateEvent.strPackName = m_strIntallingPack;
- auto &pack = m_PendingPacks[m_strIntallingPack];
- stateEvent.strExecID = pack.strPackExecID;
- stateEvent.cInstallState = GetInstallState(iDstState);
- stateEvent.bSysInstall = pack.bSysInstall;
- stateEvent.bLightPack = false;
- stateEvent.strNewVersion = m_strNewVersion;
- SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
- }
- }
- bool CUpgradeMgrFSM::IsPackDownloaded(const char *pPackName)
- {
- CSimpleStringA strPath;
- auto rc = m_pEntity->GetFunction()->GetPath("Downloads", strPath);
- assert(rc == Error_Succeed);
-
- CSimpleStringA strZipFile = CSimpleStringA::Format("%s\\%s", (const char*)strPath, pPackName);
- //此处什么场景下会发生?
- if (!strZipFile.IsEndWith(".zip") && !strZipFile.IsEndWith(".cab"))
- strZipFile += ".zip";
- DWORD attr = GetFileAttributesA(strZipFile);
- return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
- }
- bool CUpgradeMgrFSM::IsPackCancelled(const char *pPackName)
- {
- auto it = find_if(m_CancelledPacks.begin(), m_CancelledPacks.end(), [pPackName](CSimpleStringA &str)
- {
- return stricmp((const char*)str, pPackName) == 0;
- });
- return it != m_CancelledPacks.end();
- }
- bool CUpgradeMgrFSM::IsPackInstalled(const char *pPackName)
- {
- /*bool bInstalled = find_if(
- m_InstallFailPacks.begin(),
- m_InstallFailPacks.end(),
- [pPackName](CSimpleStringA &str) { return stricmp((const char*)str, pPackName) == 0; }) != m_InstallFailPacks.end();
- if (bInstalled)
- return true;*/
- //判断是否是已升级的系统外升级包
- auto pPrivFunc = m_pEntity->GetFunction()->GetPrivilegeFunction();
- auto rc = pPrivFunc->IsInstalledSysPack(pPackName);
- if (rc == Error_Succeed)
- {
- Dbg("%s is been installed", pPackName);
- return true;
- }
- //连续3次安装失败,则忽略;其他情况继续尝试安装
- return IsPackFialOver3Times(pPackName);
-
- //zl 20181016去掉根据内存和log判断是否已安装,以便处理出现升级回退情时继续重新升级。
- //// 根据安装日志文件判断是否曾经安装过(跟踪轻量包)
- //auto pPrivFunc = m_pEntity->GetFunction()->GetPrivilegeFunction();
- //pPrivFunc->IsPackInstalled(pPackName, bInstalled);
- //return bInstalled;
- }
- bool CUpgradeMgrFSM::IsPackFialOver3Times(const char *pPackName)
- {
- bool bFialOver3Times = false;
- CSimpleStringA strFailedPackInfo;
- //从安装包失败列表中查找
- if (Error_Succeed == GetInstallFailedPackInfo(strFailedPackInfo))
- {
- Dbg("strFailPackNameList[%s},pPackName[%s]",strFailedPackInfo.GetData(),pPackName);
- int nStartPos = strFailedPackInfo.IndexOf(pPackName);
- if (nStartPos != -1)
- {
- CAutoArray<CSimpleStringA> arr = strFailedPackInfo.Split(';');
- for(int i=0; i< arr.GetCount(); i++)
- {
- int nPos = arr[i].IndexOf(",");
- CSimpleStringA strName = arr[i].SubString(0,nPos);
- CSimpleStringA strConut = arr[i].SubString(nPos+1, arr[i].GetLength()-nPos-1);
- if (strName.IsStartWith(pPackName))
- {
- if (atoi(strConut) >= 3)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- /*CSimpleStringA strPackName;
- UINT64 nPackCnts;
- auto rc = GetInstallFailedPackName(strPackName);
- if (Error_Succeed != rc)
- {
- LogError(Severity_Middle, rc , 0, "not exist PackName sysvar");
- return false;
- }
- else
- {
- Dbg("installingName[%s},failName[%s]",pPackName,strPackName);
- if (0 == strPackName.Compare(pPackName, true))
- {
- rc = GetPackInstallFailedCnts(nPackCnts);
- if (Error_Succeed != rc)
- {
- LogError(Severity_Middle, rc , 0, "not exist PackInstallFailCnts sysvar");
- return false;
- }
- else
- {
- if (3 == nPackCnts)
- {
- bFialOver3Times = true;
- Dbg("%s is install failed 3 times, ignore it", pPackName);
- return true;
- }
- }
- }
- else
- {
- SetInstallFailedPackName(pPackName);
- SetPackInstallFailedCnts(0);
- }
- }*/
- return false;
- }
- ErrorCodeEnum CUpgradeMgrFSM::LoadPersistInfo()
- {
- // 从运行配置中读出重启前安装包信息
- CSimpleStringA strValue;
- //手动安装包信息
- auto rc = GetRunConfigStrValue("Install", "ManualPackList", strValue);
- if (rc == Error_Succeed && strValue.GetLength() >0)
- {
- CAutoArray<CSimpleStringA> arr = strValue.Split(',');
- for(int i=0; i<arr.GetCount(); i++)
- {
- m_ManualPendingPacks.push_back(arr[i]);
- }
- }
- else
- {
- Dbg("ManualPackList in run config is null");
- }
- rc = GetRunConfigStrValue("Install", "Pack", strValue);
- if (rc == Error_Succeed && strValue.GetLength() >0)
- {
- // 上次未安装结束
- CPendingPackInfo info = {};
- info.strPackName = strValue;
- info.nLevel = 2;
- rc = GetRunConfigStrValue("Install", "TriggerType", strValue);
- if (rc == Error_Succeed && strValue.GetLength() == 1)
- info.cTriggerType = strValue[0];
- else
- info.cTriggerType = 'I';
- UINT64 nValue(0);
- rc = GetRunConfigHexIntValue("Install", "TriggerTime", nValue);
- if (rc == Error_Succeed && nValue >0)
- info.dwTriggerTimer = nValue;
- else
- info.dwTriggerTimer = 0;
- rc = GetRunConfigStrValue("Install", "PendingState", strValue);
- if (rc == Error_Succeed && strValue.GetLength() >0)
- info.cPendingState = strValue[0];
- else
- info.cPendingState = 'N';
- rc = GetRunConfigStrValue("Install", "bSysInstall", strValue);
- if (rc == Error_Succeed && strValue.GetLength() >0)
- info.bSysInstall = atol(strValue) == 1;
- else
- info.bSysInstall = false;
- rc = GetRunConfigStrValue("Install", "PackExecID", strValue);
- if (rc == Error_Succeed && strValue.GetLength() >0)
- info.strPackExecID = strValue;
- else
- info.strPackExecID = "";
- rc = GetRunConfigStrValue("Install", "NewVersion", strValue);
- if (rc == Error_Succeed && strValue.GetLength() >0)
- m_strNewVersion = strValue;
- else
- m_strNewVersion = "";
-
- rc = GetRunConfigHexIntValue("Install", "DownloadTime", nValue);
- if (rc == Error_Succeed && nValue >0)
- m_dwDownloadTime = nValue;
- else
- m_dwDownloadTime = 0;
- Dbg("recover upgrade state from run config, install pack: {%s}", info.strPackName);
- m_strIntallingPack = info.strPackName;
- m_PendingPacks[m_strIntallingPack] = info;
- }
- return Error_Succeed;
- }
- void CUpgradeMgrFSM::s1_on_entry()
- {
- }
- void CUpgradeMgrFSM::s1_on_exit()
- {
- }
- unsigned int CUpgradeMgrFSM::s1_on_event(FSMEvent* event)
- {
- // 准入成功
- if (event->iEvt == Event_EntryPermit)
- {
- // 检查安装版本状态
- auto pFunc = m_pEntity->GetFunction();
- auto pPrivilegeFunc = pFunc->GetPrivilegeFunction();
- assert(pPrivilegeFunc != NULL);
- CSystemStaticInfo info;
- auto rc = pFunc->GetSystemStaticInfo(info);
- if (rc == Error_Succeed)
- {
- if ((info.InstallState == Install_SetToStart) // 已设置启动,表明是第一次启动
- || (info.InstallState == Install_Upgraded)) // 已升级状态 表示当前是回滚
- {
- bool bRollback = (info.InstallState == Install_Upgraded);
- rc = pPrivilegeFunc->SetRunSucceed();
- if (rc != Error_Succeed)
- {
- LogError(Severity_Middle, rc , 0, "call SetRunSucceed() error");
- return 1;
- }
-
- if (bRollback)
- {
- // 上报后一版本状态为倒回
- auto pPrivFunc = pFunc->GetPrivilegeFunction();
- assert(pPrivilegeFunc != NULL);
- CSystemStaticInfo latterVerInfo;
- rc = pPrivFunc->GetSystemStaticInfoForVersion(info.LatterInstallVersion, latterVerInfo);
- if (rc == Error_Succeed)
- {
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(latterVerInfo.InstallPack, latterVerInfo.InstallVersion, 0, 'R', "自动倒回"));
- LogEvent(Severity_Low, 0, CSimpleStringA::Format("rollback to history version [%s] succeed", (const char*)info.InstallVersion.ToString()));
- //回退信息写入运行时配置
- if (!latterVerInfo.InstallPack.IsNullOrEmpty())
- {
- UpdateInstallFailedPackInfo(latterVerInfo.InstallPack);
- }
- else
- {
- Dbg("latterVerInfo.InstallPack is IsNullOrEmpty return true, can't UpdateInstallFailedPackInfo");
- }
- }
- else
- {
- Dbg("GetSystemStaticInfoForVersion for latterVerInfo failed");
- }
- }
- else
- LogEvent(Severity_Low, 0, CSimpleStringA::Format("upgrade to new version [%s] succeed", (const char*)info.InstallVersion.ToString()));
- // 上报后台状态
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(info.InstallPack, info.InstallVersion, 0, 'A', "启动成功"));
- }
-
- // 加载持久化上报信息
- LoadPersistInfo();
- //获取手动升级任务安装结果
- UpdateManualTaskState();
- if (HasRunningUpdate())
- {
- auto &pack = m_PendingPacks[m_strIntallingPack];
- if (pack.cPendingState == 'U' || pack.cPendingState == 'N')
- return 3;
- else if (pack.cPendingState == 'D' || pack.cPendingState == 'I')
- return 4;
- else if (pack.cPendingState == 'C')
- return 5;
- else if (pack.cPendingState == 'S')
- return 6;
- }
- }
- else
- {
- LogError(Severity_Middle, rc, 0, "GetSystemStaticInfo() fail");
- }
- }
- return 0;
- }
- void CUpgradeMgrFSM::s2_on_entry()
- {
- m_strIntallingPack.Clear();
- m_nIntallingPackType = 1;
- m_strNewVersion = "";
- Dbg("send Event_StartPoll 2");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- void CUpgradeMgrFSM::s2_on_exit()
- {
- }
- unsigned int CUpgradeMgrFSM::s2_on_event(FSMEvent* event)
- {
- if (event->iEvt == Event_StartPoll)
- {
- Dbg("send Event_StartPoll 3");
- m_ReportFSM.PostEventFIFO(new FSMEvent(CReportMgrFSM::Event_StartPoll));
- }
- else if (event->iEvt == Event_EndPoll)
- {
- RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
- UpgradeConnect::PollUpgrade_Ret5 *pRet = (UpgradeConnect::PollUpgrade_Ret5*)pEvent->GetRetData();
- int nArrayNum = pEvent->GetArrayNum();
- if (nArrayNum >0 && pRet != NULL)
- {
- CSimpleStringA strInstalledPacks;
- CSimpleStringA strCanceledPacks;
- Dbg("nArrayNum=%d",nArrayNum);
- for(int i=0; i<nArrayNum; i++)
- {
- CSimpleStringA packName = pRet[i].UpgradePack;
- CSimpleStringA newPackName = pRet[i].NewUpgradePack;
- Dbg("poll ans:packName[%s], newPackName[%s], ExecID[%s].", pRet[i].UpgradePack, pRet[i].NewUpgradePack, pRet[i].PackExecID);
- if (!newPackName.IsNullOrEmpty())
- {
- packName = newPackName;
- }
- //此处什么场景下会发生?升级包注册时可以不是zip或cab格式?
- if (!packName.IsEndWith(".zip", true) && !packName.IsEndWith(".cab", true))
- packName += ".zip";
-
- if (IsPackInstalled(packName.GetData()))
- {
- if (!strInstalledPacks.IsNullOrEmpty())
- strInstalledPacks += "、";
- strInstalledPacks += packName.SubString(0, packName.GetLength()-4);
- //Dbg("upgrade pack [%s] already installed, ignore it", (const char*)packName);
- continue; // 已安装
- }
- else if (IsPackCancelled(packName))
- {
- if (!strCanceledPacks.IsNullOrEmpty())
- strCanceledPacks += "、";
- strCanceledPacks += packName.SubString(0, packName.GetLength()-4);
- //Dbg("upgrade pack [%s] already cancelled, ignore it", (const char*)packName);
- continue; // 已取消
- }
-
- if (newPackName.IsNullOrEmpty())
- {
- Dbg("PackName:%s, PackExecID:%s, no need to upgrade.", pRet[i].UpgradePack, pRet[i].PackExecID);
- continue;
- }
- if (m_PendingPacks.find(packName) == m_PendingPacks.end())
- {
- Dbg("find new install pack: [%s]", (const char*)packName);
- //add by zl 20181015,开始升级前同步时间
- ((CUpgradeMgrEntity*)m_pEntity)->SyncTime();
- Dbg("Sync Time from server");
- CPendingPackInfo info = {};
- info.strPackName = packName;
- info.nLevel = pRet[i].Level;
- info.cTriggerType = pRet[i].TriggerType;
- info.dwTriggerTimer = pRet[i].TriggerTimer;
- info.cPendingState = IsPackDownloaded(packName) ? 'D' : 'N';
- info.bSysInstall = false;
- info.strPackExecID = pRet[i].PackExecID;
- m_PendingPacks[packName] = info;
- }
- }
- if (!strInstalledPacks.IsNullOrEmpty())
- Dbg("ignore installed packs %s", (const char*)strInstalledPacks);
- if (!strCanceledPacks.IsNullOrEmpty())
- Dbg("ignore cancelled packs: %s", (const char*)strCanceledPacks);
- }
- else
- {
- Dbg("no available upgrade pack");
- }
- //广播手动升级任务消息
- //如果任务个数不为0,则提示有升级任务;为0,则不提示
- ManualTaskEvent taskEvent;
- taskEvent.nTaskNum = m_ManualPendingPacks.size();
- Dbg("manaul task num[%d]", m_ManualPendingPacks.size());
- if (!m_ManualPendingPacks.empty())
- {
- CSimpleStringA strManualPacks;
- list<CSimpleStringA>::iterator itor;
- itor = m_ManualPendingPacks.begin();
- while(itor!=m_ManualPendingPacks.end())
- {
- strManualPacks += *itor++;
- if (itor != m_ManualPendingPacks.end())
- {
- strManualPacks += ",";
- }
- }
- //持久化手动升级任务列表
- SetRunConfigStrValue("Install", "ManualPackList", strManualPacks);
- }
- SpSendBroadcast(m_pEntity->GetFunction(), eMsg_ManualTaskEvent, eMsgSig_ManualTaskEvent, taskEvent);
- // 优先升级筛选
- if (m_PendingPacks.size() >0)
- {
- // 根据优先级找出优先升级的安装包
- auto findPack = GetPriorInstallPack();
- Dbg("priority install pack: [%s]", (const char*)findPack->strPackName);
- m_strIntallingPack = findPack->strPackName;
- if (3 == findPack->nLevel)
- {
- m_nIntallingPackType = 2;
- }
- SetRunConfigStrValue("Install", "Pack", m_strIntallingPack);
- SetRunConfigStrValue("Install", "TriggerType", CSimpleStringA::Format("%c", findPack->cTriggerType));
- SetRunConfigHexIntValue("Install", "TriggerTime", findPack->dwTriggerTimer);
- SetRunConfigStrValue("Install", "PendingState", CSimpleStringA::Format("%c", findPack->cPendingState));
- SetRunConfigStrValue("Install", "PackExecID", findPack->strPackExecID);
- SetRunConfigStrValue("Install", "NewVersion", "");
- m_strNewVersion = "";
- if (findPack->cPendingState == 'N') // 未下载
- {
- // 通知下载实体下载
- Dbg("request download pack: [%s]", (const char*)findPack->strPackName);
- ((CUpgradeMgrEntity*)m_pEntity)->DownloadPack(findPack->strPackName);
- PostEventFIFO(new FSMEvent(Event_StartDownload));
- WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("request download pack: [%s]", (const char*)m_strIntallingPack));
- // 设置下载时间
- m_dwDownloadTime = CSmallDateTime::GetNow();
- SetRunConfigHexIntValue("Install", "DownloadTime", m_dwDownloadTime);
-
- // 上报下载状态到服务器 xkm@20160412
- CSystemStaticInfo ssInfo;
- m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, 0, 'D', "正在下载"));
- }
- else if (findPack->cPendingState == 'U') // 下载中
- {
- Dbg("request download pack again: [%s]", (const char*)findPack->strPackName);
- ((CUpgradeMgrEntity*)m_pEntity)->DownloadPack(findPack->strPackName);
- PostEventFIFO(new FSMEvent(Event_StartDownload));
- }
- else if (findPack->cPendingState == 'D') // 已下载
- {
- PostEventFIFO(new FSMEvent(Event_StartInstall));
- WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("start install pack: [%s]", (const char*)m_strIntallingPack));
- }
- else if (findPack->cPendingState == 'C') // 等待确认
- {
- // 重新回到等待确认状态
- PostEventFIFO(new FSMEvent(Event_WaitConfirm));
- }
- else
- {
- LogError(Severity_Middle, Error_Bug, 0, CSimpleStringA::Format("invalid pending state :%c", (const char*)findPack->cPendingState));
- }
- }
- }
- else if (event->iEvt == Event_CancelUpgrade)
- {
- // 紧急取消安装
- RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
- CancelUpgradePacks(pEvent);
- }
- return 0;
- }
- void CUpgradeMgrFSM::PushCancelUpgradePack(const CSimpleStringA &strPackName)
- {
- Dbg("local cancel install package: [%s]", (const char*)strPackName);
- if (!IsPackCancelled(strPackName))
- m_CancelledPacks.push_back(strPackName);
- }
- bool CUpgradeMgrFSM::HasDownloadingPack()
- {
- if (m_strIntallingPack.IsNullOrEmpty())
- return false;
- auto &pack = m_PendingPacks[m_strIntallingPack];
- return pack.cPendingState == 'U';
- }
- bool CUpgradeMgrFSM::HasRunningUpdate()
- {
- return !m_strIntallingPack.IsNullOrEmpty();
- }
- ErrorCodeEnum CUpgradeMgrFSM::RegistLocalPack(const CSimpleStringA &strPackName)
- {
- Dbg("regist local pack [%s]", (const char*)strPackName);
- // xkm@20161122: 本地不存在时自动启动指定下载
- //// 检查对应文件是否存在
- //CSimpleStringA strDownloadsPath;
- //auto rc = m_pEntity->GetFunction()->GetPath("Downloads", strDownloadsPath);
- //assert(rc == Error_Succeed);
- //CSimpleStringA strZipFile = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)strPackName);
- //if (!ExistsFileA(strZipFile))
- //{
- // LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("pack [%s] not exists", (const char*)strPackName));
- // return Error_NotExist;
- //}
- // xkm@20161122: 注册本地升级忽略安装历史
- //if (IsPackInstalled(strPackName))
- //{
- // LogError(Severity_Low, Error_Cancel, 0, (const char*)CSimpleStringA::Format("pack [%s] already installed", (const char*)strPackName));
- // return Error_AlreadyExist; // 已安装
- //}
- //else if (IsPackCancelled(strPackName))
- //{
- // LogError(Severity_Low, Error_Cancel, 0, (const char*)CSimpleStringA::Format("pack [%s] already canceled", (const char*)strPackName));
- // return Error_Cancel; // 已取消
- //}
- if (m_PendingPacks.find(strPackName) == m_PendingPacks.end())
- {
- CPendingPackInfo info = {};
- info.strPackName = strPackName;
- info.nLevel = 2;
- info.cTriggerType = 'I';
- info.dwTriggerTimer = 0;
- info.cPendingState = 'U';
- info.bSysInstall = false;
- info.strPackExecID = "";
- m_PendingPacks[strPackName] = info;
- Dbg("add local pack [%s] succeed", (const char*)strPackName);
- PostEventFIFO(new RvcCommRetEvent(Event_EndPoll, NULL, 0));
- }
- return Error_Succeed;
- }
- DWORD CUpgradeMgrFSM::RegistManualPack(const CSimpleStringA &strPackName)
- {
- Dbg("regist Manual pack [%s]", (const char*)strPackName);
- bool bManual= find_if(
- m_ManualPendingPacks.begin(),
- m_ManualPendingPacks.end(),
- [strPackName](CSimpleStringA &str) { return str.Compare(strPackName) == 0; }) != m_ManualPendingPacks.end();
- if (!bManual)
- {
- Dbg("[%s]不是手动安装包", strPackName);
- return NOT_MANUALPACK_ERR;
- }
-
- if (m_PendingPacks.find(strPackName) == m_PendingPacks.end())
- {
- CPendingPackInfo info = {};
- info.strPackName = strPackName;
- info.nLevel = 3;//手动升级级别为3
- info.cTriggerType = 'I';
- info.dwTriggerTimer = 0;
- info.cPendingState = 'D';
- info.bSysInstall = true; //系统外升级
- info.strPackExecID = "";
- m_PendingPacks[strPackName] = info;
- Dbg("add Manual pack [%s] succeed", (const char*)strPackName);
- PostEventFIFO(new RvcCommRetEvent(Event_EndPoll, NULL, 0));
- }
- else
- {
- //返回错误码
- Dbg("终端正在升级,请稍后再进行手动升级");
- return IS_UPDATEING_ERR;
- }
- return ERROR_SUCCESS;
- }
- DWORD CUpgradeMgrFSM::GetManualPacks(CSimpleStringA &strManualPacks)
- {
- /*if (NULL == strManualPacks)
- {
- return NULL_PARAM_ERR;
- }*/
- if (!m_ManualPendingPacks.empty())
- {
- list<CSimpleStringA>::iterator itor;
- itor = m_ManualPendingPacks.begin();
- while(itor!=m_ManualPendingPacks.end())
- {
- strManualPacks += *itor++;
- if (itor != m_ManualPendingPacks.end())
- {
- strManualPacks += ",";
- }
- }
- }
- else
- {
- strManualPacks = NULL;
- return NO_EXIST_MANUAL_PACK_ERR;
- }
- return ERROR_SUCCESS;
- }
- ErrorCodeEnum ;
- void CUpgradeMgrFSM::CancelUpgradePacks(RvcCommRetEvent *pEvent)
- {
- UpgradeConnect::PollUpgrade_Ret4 *pRet = (UpgradeConnect::PollUpgrade_Ret4*)pEvent->GetRetData();
- int nArrayNum = pEvent->GetArrayNum();
- if (nArrayNum >0 && pRet != NULL)
- {
- for(int i=0; i<nArrayNum; i++)
- {
- CSimpleStringA strPack = pRet[i].CancelPack;
- Dbg("cancel install package: [%s]", (const char*)strPack);
-
- if (!IsPackCancelled(strPack))
- m_CancelledPacks.push_back(strPack);
- }
- }
- }
- // 找出优先级高的,或者立刻执行的,或者触发时间早的升级包
- CUpgradeMgrFSM::CPendingPackInfo* CUpgradeMgrFSM::GetPriorInstallPack()
- {
- if (m_PendingPacks.size() ==0)
- return NULL;
- auto pFindPack = &(m_PendingPacks.begin()->second);
- for(auto it=++m_PendingPacks.begin(); it!=m_PendingPacks.end(); it++)
- {
- auto &curPack = it->second;
- if ((curPack.nLevel > pFindPack->nLevel)
- || (curPack.nLevel == pFindPack->nLevel && pFindPack->cTriggerType != 'I'
- && (curPack.cTriggerType == 'I' || curPack.dwTriggerTimer < pFindPack->dwTriggerTimer) ))
- {
- pFindPack = &curPack;
- continue;
- }
- }
- return pFindPack;
- }
- void CUpgradeMgrFSM::s3_on_entry()
- {
- if (m_dwDownloadTime == 0)
- {
- Dbg("reset begin download time");
- m_dwDownloadTime = CSmallDateTime::GetNow();
- SetRunConfigHexIntValue("Install", "DownloadTime", m_dwDownloadTime);
- }
- // 启动定时器检查文件是否下载
- ScheduleTimer(1, 30000);
- }
- void CUpgradeMgrFSM::s3_on_exit()
- {
- }
- unsigned int CUpgradeMgrFSM::s3_on_event(FSMEvent* event)
- {
- if (event->iEvt == Event_EndDownload) // 下载完成事件
- {
- DownloadedEvent *pEvent = (DownloadedEvent*)event;
- auto &strFileName = pEvent->strFileName;
- auto it = m_PendingPacks.find(strFileName);
- if (it != m_PendingPacks.end())
- it->second.cPendingState = 'D'; // 修改升级状态
- if (strFileName == m_strIntallingPack)
- {
- if (pEvent->ErrorCode == Error_Succeed)
- {
- // 开始安装
- WriteInstallLog(m_strIntallingPack, "pack download succeed");
- PostEventFIFO(new FSMEvent(Event_StartInstall));
- }
- else
- {
- // 取消安装
- WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("cancel update for download error: %s(%d)", (const char*)pEvent->strErrorMsg, pEvent->ErrorCode));
- // 上报安装失败
- CSystemStaticInfo ssInfo;
- m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
- CSimpleStringA strErrMsg = "下载失败,";
- strErrMsg += pEvent->strErrorMsg;
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_Unexpect, 'C', strErrMsg));
- m_PendingPacks.erase(m_strIntallingPack);
- m_strIntallingPack.Clear();
- Dbg("send Event_StartPoll 4");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- }
- }
- else if (event->iEvt == EVT_TIMER)
- {
- // 定时检测下载是否完成
- // QueryDownloadState 接口返回状态值:1:正在下载 2:排队中 3:下载完成 4:无下载任务并且没有文件
- if(IsPackDownloaded(m_strIntallingPack))
- {
- auto &pack = m_PendingPacks[m_strIntallingPack];
- if (pack.cPendingState == 'U')
- {
- Dbg("finish downloading pack [%s] detected", (const char*)m_strIntallingPack);
- pack.cPendingState = 'D';
- }
- // 开始安装
- PostEventFIFO(new FSMEvent(Event_StartInstall));
- }
- else if (m_dwDownloadTime >0 && ((DWORD)CSmallDateTime::GetNow()) - m_dwDownloadTime >= 3600*24*14) // 14天后超时
- {
- // 等待下载超时后主动取消
- WriteInstallLog(m_strIntallingPack, "cancel update for wait download timeout");
- Dbg("cancel update pack [%s] for wait download timeout", (const char*)m_strIntallingPack);
- // 上报安装失败
- CSystemStaticInfo ssInfo;
- m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_TimeOut, 'C', "下载超时"));
- m_PendingPacks.erase(m_strIntallingPack);
- m_strIntallingPack.Clear();
- Dbg("send Event_StartPoll 5");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- else if (CheckIfInstallCancelled())
- {
- // 远程或本地手工取消安装
- Dbg("send Event_StartPoll 6");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- else
- {
- // 查询后台升级策略是否过期或失效
- assert(m_PendingPacks.find(m_strIntallingPack) != m_PendingPacks.end());
- auto &pack = m_PendingPacks[m_strIntallingPack];
- if (!pack.strPackExecID.IsNullOrEmpty())
- {
- Dbg("query upgrade exec info, id:%s", (const char*)pack.strPackExecID);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::QueryExecInfoEvent(pack.strPackExecID));
- }
- // 继续监视
- Dbg("pack [%s] not downloaded", (const char*)m_strIntallingPack);
- ScheduleTimer(1, 30000);
- }
- }
- else if (event->iEvt == Event_EndQueryExec)
- {
- // 查询策略是否过期
- auto pEvent = (RvcCommRetEvent*)event;
- assert(pEvent->GetArrayNum() == 1);
- auto pRet = (UpgradeConnect::QueryExec_Ret *)pEvent->GetRetData();
- assert(pRet != NULL);
- auto strExecID = pRet->PackExecID;
- auto &info = m_PendingPacks[m_strIntallingPack];
- if (info.strPackExecID == strExecID)
- {
- if (pRet->HasCancelled)
- {
- Dbg("exec [%s] of pack [%s] cancalled", (const char*)strExecID, (const char*)m_strIntallingPack);
- // 等待下载超时后主动取消
- WriteInstallLog(m_strIntallingPack, "cancel update for exec expired");
- // 上报安装失败
- CSystemStaticInfo ssInfo;
- m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_Cancel, 'C', "升级策略已过期"));
- // 通知下载实体取消下载
- Dbg("cancel download pack [%s]", (const char*)m_strIntallingPack);
- ((CUpgradeMgrEntity*)m_pEntity)->CancelDownloadPack(m_strIntallingPack);
- m_PendingPacks.erase(m_strIntallingPack);
- m_strIntallingPack.Clear();
- Dbg("send Event_StartPoll 7");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- else
- {
- Dbg("exec [%s] of pack [%s] is valid", (const char*)strExecID, (const char*)m_strIntallingPack);
- }
- }
- else
- {
- Dbg("return exec [%s] is not installing pack [%s]", (const char*)strExecID, (const char*)m_strIntallingPack);
- }
- }
- else if (event->iEvt == Event_CancelUpgrade)
- {
- // 紧急取消安装
- RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
- CancelUpgradePacks(pEvent);
- }
- return 0;
- }
- void CUpgradeMgrFSM::s4_on_entry()
- {
- // 检查安装是否紧急取消
- if (!CheckIfInstallCancelled())
- {
- //add by zl 20190221,停止广告、提示音播放
- ((CUpgradeMgrEntity*)m_pEntity)->StopMediaPlay();
- Dbg("stop media play");
-
- // 终端置为升级状态。1:升级状态,0:非升级状态。处于升级状态时调用播放接口不生效
- auto pFunc = m_pEntity->GetFunction();
- assert(pFunc != NULL);
- auto rc = pFunc->SetSysVar("UpdateState", "1", false);
- assert(rc == Error_Succeed);
- // 通知升级实体开始安装
- Dbg("push pack [%s] to UpgradeRun entity", (const char*)m_strIntallingPack);
- rc = ((CUpgradeMgrEntity*) m_pEntity)->PushUpdateTask(m_strIntallingPack, m_nIntallingPackType);
- if (rc != Error_Succeed)
- {
- ScheduleTimer(1, 5000);
- }
- }
- else
- {
- Dbg("send Event_StartPoll 8");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- }
- void CUpgradeMgrFSM::s4_on_exit()
- {
- }
- bool CUpgradeMgrFSM::WriteInstallLog(const char *pszPackName, const char *pszLogText)
- {
- CSimpleStringA strPath;
- m_pEntity->GetFunction()->GetPath("RunInfo", strPath);
- // 创建安装包日志文件
- CSimpleStringA strInstallLogDir = CSimpleStringA::Format("%s\\InstallLog", (const char*)strPath);
- if (!ExistsDirA(strInstallLogDir))
- CreateDirectoryA(strInstallLogDir, NULL);
- // 写安装包日志
- CSimpleStringA strInstallLog = CSimpleStringA::Format("%s\\%s.log", (const char*)strInstallLogDir, pszPackName);
- FILE *pInstallLog = fopen(strInstallLog, "a+");
- if (pInstallLog == NULL)
- {
- LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("open install log file [%s] to write fail", pszPackName));
- return false;
- }
-
- fprintf_s(pInstallLog, "[%s] %s\r\n", (const char*)CSmallDateTime::GetNow().ToTimeString(), pszLogText);
- fflush(pInstallLog);
- fclose(pInstallLog);
- return true;
- }
- bool CUpgradeMgrFSM::RemoveInstallLog(const char *pszPackName)
- {
- return RemoveFileA(pszPackName);
- }
- unsigned int CUpgradeMgrFSM::s4_on_event(FSMEvent* event)
- {
- if (event->iEvt == EVT_TIMER)
- {
- if (event->param1 == 1)
- {
- // 通知升级实体开始安装
- Dbg("retry push pack [%s] to UpgradeRun entity", (const char*)m_strIntallingPack);
- auto rc = ((CUpgradeMgrEntity*)m_pEntity)->PushUpdateTask(m_strIntallingPack, m_nIntallingPackType);
- if (rc != Error_Succeed)
- {
- ScheduleTimer(1, 5000);
- }
- }
- else if (event->param1 == 2)
- {
- if (CheckIfCanSwitchNow())
- {
- PostEventFIFO(new FSMEvent(Event_PreRestart));
- // 系统重启事件
- LogEvent(Severity_Middle, Event_Req_OS_Restart, "install sys pack ok, request to restart OS");
- // 强制刷新日志
- m_pEntity->GetFunction()->FlushLogFile();
- }
- else
- {
- ScheduleTimer(2, 5000);
- }
- }
- }
- else if (event->iEvt == Event_InstallCheck)
- {
- // 检查执行逻辑
- UpgradeRunCheckEvent *pEvent = (UpgradeRunCheckEvent*) event;
-
- Dbg("receive pack [%s] upgrade check event, result: [%d], comment:[%s]", (const char*)pEvent->strPackName, pEvent->nErrorCode, (const char*)pEvent->strComment);
- if (pEvent->strPackName == m_strIntallingPack)
- {
- // 上报检查结果
- CSystemStaticInfo ssInfo;
- m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
- char cState = pEvent->nErrorCode == Error_Succeed ? 'I' : 'C';
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, pEvent->nErrorCode, cState, pEvent->strComment));
- // 检查结果写入安装日志
- WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("Upgrade check finished, result: [%d], comment:%s", pEvent->nErrorCode, (const char*)pEvent->strComment));
- if (pEvent->nErrorCode == Error_Succeed)
- {
- // 开始执行
- Dbg("pack [%s] upgrade check pass, install now", (const char*)m_strIntallingPack);
- }
- else
- {
- // 执行取消
- Dbg("pack [%s] upgraded cancelled, check not pass, %s", (const char*)m_strIntallingPack, (const char*)pEvent->strComment);
- m_InstallFailPacks.push_back(m_strIntallingPack);
-
- //安装错误写入运行时配置
- UpdateInstallFailedPackInfo(m_strIntallingPack);
-
- // 从待安装列表中删除
- m_PendingPacks.erase(m_strIntallingPack);
- //RemoveInstallLog(m_strIntallingPack);//删除安装或检查失败的包日志文件
- m_strIntallingPack.Clear();
- m_strNewVersion = "";
- Dbg("send Event_StartPoll 9");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- }
- }
- else if (event->iEvt == Event_EndInstall)
- {
- UpgradeRunDoneEvent *pEvent = (UpgradeRunDoneEvent*)event;
- Dbg("pack [%s] upgrade done, result: [%d], comment: %s, NewVersion:[%s]", (const char *)pEvent->strPackName, pEvent->nErrorCode, (const char*)pEvent->strComment, (const char*)pEvent->strNewVersion);
- assert(pEvent->strPackName == m_strIntallingPack);
- if (pEvent->strPackName == m_strIntallingPack)
- {
- // 上报安装结果
- CSystemStaticInfo ssInfo;
- m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
- bool bUpgradeSuc = pEvent->nErrorCode == Error_Succeed;
-
- char cState = 'N';
- if (pEvent->strComment.IndexOf("手动升级") >= 0)
- {
- //同一个手动升级包第一次自动执行
- cState = 'I';
- }
- else
- {
- cState = bUpgradeSuc ? ((pEvent->bLightPack || pEvent->bSysInstall) ? 'A' : 'W') : 'C';
- }
-
- // 安装结果写入安装日志
- WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("Upgrade install finished, result: [%d], comment: %s", pEvent->nErrorCode, (const char*)pEvent->strComment));
- CVersion reportVer;
- if (pEvent->bLightPack || pEvent->bSysInstall) // 轻量安装和系统外安装都不改变框架版本
- reportVer = ssInfo.InstallVersion;
- else
- reportVer = ssInfo.LatterInstallVersion;
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, reportVer, pEvent->nErrorCode, cState, pEvent->strComment));
- // xkm@20161114: 系统定制升级包需上报系统定制版本
- // 系统补丁包和固件升级包也需上报
- if (pEvent->bSysInstall
- && (!pEvent->strNewVersion.IsNullOrEmpty() || !pEvent->strFWID.IsNullOrEmpty() || !pEvent->strSysPatchName.IsNullOrEmpty())
- && bUpgradeSuc)
- {
- Dbg("report system custom version:[%s], FWID:[%s], SysPatchName:[%s]", (const char*)pEvent->strNewVersion, (const char*)pEvent->strFWID, (const char*)pEvent->strSysPatchName);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportSysCustomVerEvent(m_strIntallingPack, pEvent->strNewVersion, pEvent->strFWID, pEvent->strSysPatchName));
- }
- if (bUpgradeSuc)
- {
- if (pEvent->bLightPack || pEvent->bSysInstall)
- {
- // 轻量包或系统外升级
- if (pEvent->bLightPack)
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- else
- {
- //区分第一次升级和第二次升级;第一次添加到m_ManualPendingPacks,第二次从m_ManualPendingPacks删除
- //如果是手动升级,则添加到手动升级列表
- if (pEvent->strComment.IndexOf("手动升级") >= 0)
- {
- //同一个手动升级包第一次自动执行
- CSimpleStringA temp = m_strIntallingPack;
- bool bExist= find_if(
- m_ManualPendingPacks.begin(),
- m_ManualPendingPacks.end(),
- [temp](CSimpleStringA &str) { return str.Compare(temp) == 0; }) != m_ManualPendingPacks.end();
- if (!bExist)
- {
- m_ManualPendingPacks.push_back(m_strIntallingPack);
- Dbg("pushback[%s] to m_ManualPendingPacks", m_strIntallingPack);
- }
-
- Dbg("send Event_StartPoll 10");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- else
- {
- //系统外升级或同一个手动升级包第二次由用户桌面手动执行
- CSimpleStringA temp = m_strIntallingPack;
- bool bManual= find_if(
- m_ManualPendingPacks.begin(),
- m_ManualPendingPacks.end(),
- [temp](CSimpleStringA &str) { return str.Compare(temp) == 0; }) != m_ManualPendingPacks.end();
- if (bManual)
- {
- //检查安装结果,并上报
- IsManualInstallOk(m_strIntallingPack);
- Dbg("[%s]手动升级完成,从待手动升级列表删除", m_strIntallingPack);
- m_ManualPendingPacks.remove(m_strIntallingPack);
- //更新运行时手动升级任务
- if (!m_ManualPendingPacks.empty())
- {
- CSimpleStringA strManualPacks;
- list<CSimpleStringA>::iterator itor;
- itor = m_ManualPendingPacks.begin();
- while(itor!=m_ManualPendingPacks.end())
- {
- strManualPacks += *itor++;
- if (itor != m_ManualPendingPacks.end())
- {
- strManualPacks += ",";
- }
- }
- SetRunConfigStrValue("Install", "ManualPackList", strManualPacks);
- }
- else
- {
- SetRunConfigStrValue("Install", "ManualPackList", "");
- }
- }
- else
- {
- Dbg("未在待手动升级列表中找到[%s]", m_strIntallingPack);
- }
- // xkm@20161128: 为保证定制上报成功,通过定时器重启
- ScheduleTimer(2, 5000);
- }
- }
- m_PendingPacks.erase(m_strIntallingPack);
- m_strIntallingPack.Clear();
- m_strNewVersion = "";
- }
- else
- {
- auto &pack = m_PendingPacks[m_strIntallingPack];
- pack.bSysInstall = pEvent->bSysInstall;
- SetRunConfigStrValue("Install", "bSysInstall", pack.bSysInstall ? "1" : "0");
- SetRunConfigStrValue("Install", "NewVersion", pEvent->strNewVersion);
- m_strNewVersion = pEvent->strNewVersion;
- PostEventFIFO(new FSMEvent(Event_WaitConfirm));
- }
- }
- else
- {
- // 安装失败
- m_InstallFailPacks.push_back(m_strIntallingPack);
- //RemoveInstallLog(m_strIntallingPack);//删除安装或检查失败的包日志文件
-
- //安装错误写入运行时配置
- UpdateInstallFailedPackInfo(m_strIntallingPack);
-
- // 从待安装列表中删除
- m_PendingPacks.erase(m_strIntallingPack);
- m_strIntallingPack.Clear();
- m_strNewVersion = "";
- Dbg("send Event_StartPoll 11");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- }
- }
- else if (event->iEvt == Event_CancelUpgrade)
- {
- // 紧急取消安装
- RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
- CancelUpgradePacks(pEvent);
- }
- return 0;
- }
- bool CUpgradeMgrFSM::CheckIfInstallCancelled()
- {
- if (IsPackCancelled(m_strIntallingPack))
- {
- // 取消上报
- CSystemStaticInfo ssInfo;
- m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_Cancel, 'C', "取消安装"));
- Dbg("cancel upgrade pack [%s] now", (const char*)m_strIntallingPack);
- WriteInstallLog(m_strIntallingPack, "cancel update pack");
- if (!m_strNewVersion.IsNullOrEmpty())
- {
- Dbg("delete temp new version dir");
- CSimpleStringA strRootVerPath;
- m_pEntity->GetFunction()->GetPath("RootVer", strRootVerPath);
- CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", (const char*)strRootVerPath, (const char*)m_strNewVersion);
- if (ExistsDirA(strNewVerPath))
- {
- RemoveDirRecursiveA(strNewVerPath);
- }
- }
- // 从待安装列表删除
- m_PendingPacks.erase(m_strIntallingPack);
- m_strIntallingPack.Clear();
- m_strNewVersion = "";
- return true;
- }
- return false;
- }
- ErrorCodeEnum CUpgradeMgrFSM::SwitchUpgradeNow()
- {
- Dbg("switch to framework new version now");
- assert(m_PendingPacks.find(m_strIntallingPack) != m_PendingPacks.end());
- auto &pack = m_PendingPacks[m_strIntallingPack];
- // 上报状态
- CSystemStaticInfo ssInfo;
- auto pFunc = m_pEntity->GetFunction();
- pFunc->GetSystemStaticInfo(ssInfo);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.LatterInstallVersion, 0, 'S', "已设为启动版本"));
- // 立刻切换
- auto pPrivFunc = pFunc->GetPrivilegeFunction();
- auto rc = pPrivFunc->UpgradeToNewVersion(pack.bSysInstall);
- if (rc == Error_Succeed)
- {
- PostEventFIFO(new FSMEvent(Event_PreRestart));
- //add by zl 20190827 升级完成清空运行时配置,防止事件延迟导致状态机没有收到重庆事件就已重启框架
- // 完成升级,清空运行时配置
- Dbg("clear upgrade info from runcfg");
- SetRunConfigStrValue("Install", "Pack", "");
- SetRunConfigStrValue("Install", "TriggerType", "");
- SetRunConfigHexIntValue("Install", "TriggerTime", 0);
- SetRunConfigStrValue("Install", "PendingState", "");
- SetRunConfigStrValue("Install", "bSysInstall", "");
- SetRunConfigStrValue("Install", "PackExecID", "");
- SetRunConfigStrValue("Install", "NewVersion", "");
- SetRunConfigHexIntValue("Install", "DownloadTime", 0);
- // 发送广播(通知业务界面清除状态)
- UpgradeStateEvent stateEvent;
- stateEvent.strPackName = "";
- stateEvent.strExecID = "";
- stateEvent.cInstallState = 'N';
- stateEvent.bSysInstall = false;
- stateEvent.bLightPack = false;
- stateEvent.strNewVersion = "";
- SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
- if (!pack.bSysInstall)
- {
- // 框架重启事件
- LogEvent(Severity_Middle, Event_Req_Framework_Restart,
- CSimpleStringA::Format("upgrade to new version [%s] succeed, request to restart framework",
- ssInfo.LatterInstallVersion.ToString()));
- //pPrivFunc->Reboot(RebootTrigger_FrameUpgrade, RebootWay_Framework);
- }
- else
- {
- // 系统重启事件
- LogEvent(Severity_Middle, Event_Req_OS_Restart,
- CSimpleStringA::Format("upgrade to new version [%s] succeed, request to restart OS",
- ssInfo.LatterInstallVersion.ToString()));
- // pPrivFunc->PushSysInstallPack(m_strInstallPack);
- }
- // 强制刷新日志
- m_pEntity->GetFunction()->FlushLogFile();
- }
- else
- {
- // 升级失败
- Dbg("send Event_StartPoll 12");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- return rc;
- }
- bool CUpgradeMgrFSM::CheckIfPackConfirmed()
- {
- // 所有升级包切换版本前都需要到后台查询当前执行策略状态,减少事后回滚机会
- // xkm@20140827: 对于远程或本地手工升级,不需要后台确认,直接跳过
- assert(m_PendingPacks.find(m_strIntallingPack) != m_PendingPacks.end());
- auto &pack = m_PendingPacks[m_strIntallingPack];
- //char cTriggerType = pack.cTriggerType;
- //if (cTriggerType == 'I' ||
- // (cTriggerType == 'T' && pack.dwTriggerTimer <= (DWORD)CSmallDateTime::GetNow()))
- //{
- // return true;
- //}
- //else if (cTriggerType == 'C' && pack.dwTriggerTimer <= (DWORD)CSmallDateTime::GetNow())
- //{
- // // 请求确认
- // m_ReportFSM.PostEventFIFO(new CReportMgrFSM::SwitchConfirmEvent(pack.strPackExecID));
- // return false;
- //}
- //else
- //{
- // // 启动切换检查定时器
- // ScheduleTimer(1, 30000);
- // return false;
- //}
- if (pack.strPackExecID == "" || pack.bSysInstall)
- {
- Dbg("pack %s no need to confirm", (const char*)pack.strPackName);
- return true;
- }
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::SwitchConfirmEvent(pack.strPackExecID));
- return false;
- }
- void CUpgradeMgrFSM::s5_on_entry()
- {
- // 检查安装是否紧急取消
- if (!CheckIfInstallCancelled())
- {
- if (CheckIfPackConfirmed())
- {
- PostEventFIFO(new FSMEvent(Event_StartSwitch));
- }
- }
- else
- {
- Dbg("send Event_StartPoll 13");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- }
- void CUpgradeMgrFSM::s5_on_exit()
- {
- }
- unsigned int CUpgradeMgrFSM::s5_on_event(FSMEvent* event)
- {
- if (event->iEvt == EVT_TIMER)
- {
- if (CheckIfInstallCancelled())
- {
- Dbg("send Event_StartPoll 14");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- else if (CheckIfPackConfirmed())
- {
- PostEventFIFO(new FSMEvent(Event_StartSwitch));
- }
- }
- else if (event->iEvt == Event_EndConfirm)
- {
- auto pEvent = (RvcCommRetEvent*)event;
- assert(pEvent->GetArrayNum() == 1);
-
- auto pRet = (UpgradeConnect::Confirm_Ret *)pEvent->GetRetData();
- assert(pRet != NULL);
-
- auto strExecID = pRet->PackExecID;
- auto &info = m_PendingPacks[m_strIntallingPack];
- if (info.strPackExecID == strExecID)
- {
- if(pRet->HasConfirmed)
- {
- Dbg("pack [%s] confirmed, switch version now", (const char*)m_strIntallingPack);
- PostEventFIFO(new FSMEvent(Event_StartSwitch));
- }
- else if (pRet->HasCancelled)
- {
- Dbg("pack [%s] cancalled", (const char*)m_strIntallingPack);
-
- if (!IsPackCancelled(m_strIntallingPack))
- m_CancelledPacks.push_back(m_strIntallingPack);
- ScheduleTimer(1, 1000);
- }
- else
- {
- //if (info.cTriggerType == 'C')
- Dbg("pack [%s] not confirmed, exec id: [%s]", (const char*)m_strIntallingPack, (const char*)strExecID);
- ScheduleTimer(1, 30000);
- }
- }
- else
- {
- Dbg("recv confirm of not installing pack: %s", (const char*)strExecID);
- }
- }
- else if (event->iEvt == Event_CancelUpgrade)
- {
- // 紧急取消安装
- RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
- CancelUpgradePacks(pEvent);
-
- Dbg("send Event_StartPoll 15");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- return 0;
- }
- bool CUpgradeMgrFSM::CheckIfCanSwitchNow()
- {
- auto pFunc = m_pEntity->GetFunction();
- assert(pFunc != NULL);
- CSimpleStringA strCustomerHandle;
- CSimpleStringA strRunState;
- CSimpleStringA strCallState;
- // 无交互N、客户关注A、客户在控制终端C、柜员协助T、柜员代操作G
- auto rc = pFunc->GetSysVar("CustomerHandle", strCustomerHandle);
- assert(rc == Error_Succeed);
- //rc = pFunc->GetSysVar("RunState", strRunState);
- //assert(rc == Error_Succeed);
- // 离线O、尝试连线柜员C、免提通话H、连线失败F、断线B
- rc = pFunc->GetSysVar("CallState", strCallState);
- assert(rc == Error_Succeed);
- SYSTEMTIME st = {};
- GetLocalTime(&st);
- bool bOffworkTime = st.wHour < 8 || st.wHour >= 18;
- Dbg("strCustomerHandle=%s, strCallState=%s, bOffworkTime=%d",strCustomerHandle,strCallState,bOffworkTime);
- // 或者下班时间且非连线状态 @20151202
- // 客户关注状态A,不允许切换 by zl 20191113
- /*return strCustomerHandle[0] == 'N' || strCustomerHandle[0] == 'A'
- || (bOffworkTime && strCallState[0] == 'O');*/
- return strCustomerHandle[0] == 'N' || (bOffworkTime && strCallState[0] == 'O');
- }
- void CUpgradeMgrFSM::s6_on_entry()
- {
- if (CheckIfInstallCancelled())
- {
- Dbg("send Event_StartPoll 16");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- else if (CheckIfCanSwitchNow())
- {
- // 检查当前是否可以切换(无客户)
- SwitchUpgradeNow();
- }
- else
- {
- Dbg("not applicable time to switch, retry in 10's");
- ScheduleTimer(1, 10000);
- }
- }
- void CUpgradeMgrFSM::s6_on_exit()
- {
- m_strIntallingPack.Clear();
- m_strNewVersion = "";
- }
- unsigned int CUpgradeMgrFSM::s6_on_event(FSMEvent* event)
- {
- if (event->iEvt == EVT_TIMER)
- {
- if (CheckIfInstallCancelled())
- {
- Dbg("send Event_StartPoll 17");
- PostEventFIFO(new FSMEvent(Event_StartPoll));
- }
- else if (CheckIfCanSwitchNow())
- {
- // 检查当前是否可以切换(无客户)
- SwitchUpgradeNow();
- }
- else
- {
- Dbg("not applicable time to switch, retry in 10's");
- ScheduleTimer(1, 10000);
- }
- }
- else if (event->iEvt == Event_SwitchNow)
- {
- // 客户经理主动切换版本
- SwitchUpgradeNow();
- }
- return 0;
- }
- ErrorCodeEnum CUpgradeMgrFSM::SetRunConfigStrValue(const char *pszSection, const char *pszKey, const char *pszValue)
- {
- auto pFunc = m_pEntity->GetFunction();
- CSmartPointer<IConfigInfo> pConfig;
- auto rc = pFunc->OpenConfig(Config_Run, pConfig);
- if (rc != Error_Succeed)
- return rc;
- return pConfig->WriteConfigValue(pszSection, pszKey, pszValue);
- }
- ErrorCodeEnum CUpgradeMgrFSM::GetRunConfigStrValue(const char *pszSection, const char *pszKey, CSimpleStringA &strValue)
- {
- auto pFunc = m_pEntity->GetFunction();
- CSmartPointer<IConfigInfo> pConfig;
- auto rc = pFunc->OpenConfig(Config_Run, pConfig);
- if (rc != Error_Succeed)
- return rc;
- return pConfig->ReadConfigValue(pszSection, pszKey, strValue);
- }
- ErrorCodeEnum CUpgradeMgrFSM::SetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 nValue)
- {
- auto pFunc = m_pEntity->GetFunction();
- CSmartPointer<IConfigInfo> pConfig;
- auto rc = pFunc->OpenConfig(Config_Run, pConfig);
- if (rc != Error_Succeed)
- return rc;
- return pConfig->WriteConfigValueHexInt(pszSection, pszKey, nValue);
- }
- ErrorCodeEnum CUpgradeMgrFSM::GetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 &nValue)
- {
- auto pFunc = m_pEntity->GetFunction();
- CSmartPointer<IConfigInfo> pConfig;
- auto rc = pFunc->OpenConfig(Config_Run, pConfig);
- if (rc != Error_Succeed)
- return rc;
- return pConfig->ReadConfigValueHexInt(pszSection, pszKey, nValue);
- }
- ErrorCodeEnum CUpgradeMgrFSM::GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID,
- char &cInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion)
- {
- bInstalling = !m_strIntallingPack.IsNullOrEmpty();
- if (bInstalling)
- {
- strPackFile = m_strIntallingPack;
- auto &pack = m_PendingPacks[m_strIntallingPack];
- strExecID = pack.strPackExecID;
- cInstallState = GetInstallState(m_iState);
- bSysInstall = pack.bSysInstall;
- bLightPack = false;
- strNewVersion = m_strNewVersion;
- }
- return Error_Succeed;
- }
- //获取手动升级任务安装结果
- bool CUpgradeMgrFSM::UpdateManualTaskState()
- {
- if (!m_ManualPendingPacks.empty())
- {
- list<CSimpleStringA>::iterator itor;
- itor = m_ManualPendingPacks.begin();
- while(itor!=m_ManualPendingPacks.end())
- {
- CSimpleStringA strPackName = *itor;
- if (IsManualInstallOk(*itor))
- {
- Dbg("[%s]手动升级完成,从待手动升级列表删除", strPackName);
- m_ManualPendingPacks.remove(strPackName);
- }
- itor++;
- }
- //更新运行时手动升级任务
- if (!m_ManualPendingPacks.empty())
- {
- CSimpleStringA strManualPacks;
- list<CSimpleStringA>::iterator itor;
- itor = m_ManualPendingPacks.begin();
- while(itor!=m_ManualPendingPacks.end())
- {
- strManualPacks += *itor++;
- if (itor != m_ManualPendingPacks.end())
- {
- strManualPacks += ",";
- }
- }
- SetRunConfigStrValue("Install", "ManualPackList", strManualPacks);
- }
- else
- {
- SetRunConfigStrValue("Install", "ManualPackList", "");
- }
- }
- return true;
- }
- //手动升级包是否安装完成
- bool CUpgradeMgrFSM::IsManualInstallOk(CSimpleStringA strPackName)
- {
- CSimpleStringA strErrMsg;
- CSimpleStringA strVersion;
- CSimpleStringA strPatchName = GetSysPatchNameFromPackName(strPackName);
- if (strPackName.IsNullOrEmpty())
- {
- return false;
- }
- if (strPackName.IsStartWith("SP"))
- {
- //系统补丁
- if (!GetServerPackUpgradeResult(strPackName, strVersion, strErrMsg))
- {
- Dbg("get [%s] ServerPack upgrade result fail, %s", (const char*)strPackName, (const char*)strErrMsg);
- strErrMsg = "";
- return false;
- }
- else
- {
- // 系统补丁包和固件升级包也需上报
- Dbg("report SysPatchName:[%s]", strPackName);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportSysCustomVerEvent(strPackName, "", "", strVersion));
- }
- }
- else if (strPackName.IsStartWith("FP"))
- {
- //固件
- if (!GetFirmwarePackUpgradeResult(strPackName, strVersion, strErrMsg))
- {
- Dbg("get [%s] FirmwarePack upgrade result fail, %s", (const char*)strPackName, (const char*)strErrMsg);
- strErrMsg = "";
- return false;
- }
- else
- {
- Dbg("report FirmwarePack:[%s]", strPackName);
- m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportSysCustomVerEvent(strPackName, "", strVersion, ""));
- }
- }
- else if (strPackName.IsStartWith("SCP"))
- {
- //系统定制
- return false;
- }
- else
- {
- Dbg("unknow pack name[%s]", (const char*)strPackName);
- return false;
- }
- return true;
- }
- CSimpleStringA CUpgradeMgrFSM::GetSysPatchNameFromPackName(const char *pszPackName)
- {
- //操作系统补丁升级包名格式:SP_日期_补丁名
- CSimpleStringA str = pszPackName;
- CSimpleStringA strSysPatchName = "";
- if (!str.IsNullOrEmpty())
- {
- auto arr = str.Split('_');
- if (arr.GetCount() == 3)
- {
- strSysPatchName = arr[2];
- }
- else
- {
- Dbg("升级包名[%s]不符合规范, 格式必须是:[包类型_日期_补丁名]", str);
- }
- }
- return strSysPatchName;
- }
- //获取系统补丁升级结果
- bool CUpgradeMgrFSM::GetServerPackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg)
- {
- char szTmp[4096] = {};
- int nTmpBufLen = 4096;//win7补丁可能有200多个,每个补丁(如KB3105211加分隔符;)需要10个字节
- if (!QueryWMIDevice(PatchList, "HotFixID", szTmp, &nTmpBufLen))
- {
- Dbg("query sys patch list fail");
- return false;
- }
- CSimpleStringA strPatchList = szTmp;
- CSimpleStringA strPatchName = GetSysPatchNameFromPackName(pszPackageName);
- if (strPatchName.IsNullOrEmpty())
- {
- return false;
- }
- if (strPatchList.IndexOf(strPatchName.GetData()) < 0 )
- {
- Dbg("can not find PatchName[%s] from PatchList[%s]", strPatchName, strPatchList);
- return false;
- }
- Dbg("find PatchName[%s] from PatchList[%s]", strPatchName, strPatchList);
- strVerison = strPatchName;
- return true;
- }
- //获取固件升级结果
- bool CUpgradeMgrFSM::GetFirmwarePackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg)
- {
- // 根据PinPad实体状态确定是否连接密码键盘
- bool bUpdate = false;
- strVerison = "";
- auto pPinPadClient = new PinPadService_ClientBase(this->GetEntityBase());
- if (pPinPadClient->Connect() != Error_Succeed)
- {
- Dbg("connect PinPad fail, assume no pinpad");
- strErrMsg = "connect PinPad fail, assume no pinpad";
- }
- else
- {
- PinPadService_GetDevInfo_Req req = {};
- PinPadService_GetDevInfo_Ans ans = {};
- auto rc = pPinPadClient->GetDevInfo(req, ans, 3000);
- if (rc == Error_Succeed)
- {
- if (ans.state == DEVICE_STATUS_NORMAL)
- {
- Dbg("pinpad model: %s", (const char*)ans.model);
- // CM = V2.0#PM = V1.0#MID = 75500001#PID = 12345678#FWID = V1234567#Vendor = nantian
- // 密码键盘ID,PID,8到16字节; 设备ID,MID,8到16字节; 固件版本号,FWID,8字节
- CSimpleStringA str = ans.model;
- CSimpleStringA strFWID;
- if (!str.IsNullOrEmpty())
- {
- auto arr = str.Split('#');
- if (arr.GetCount() > 0)
- {
- for (int i = 0; i < arr.GetCount(); i++)
- {
- auto arr2 = arr[i].Split('=');
- if (arr2.GetCount() != 2)
- continue;
- if(!strnicmp((LPCTSTR)arr2[0], "FWID", strlen("FWID")))
- {
- strFWID = arr2[1];
- if (!strFWID.IsNullOrEmpty())
- {
- CSimpleStringA strPackName = pszPackageName;
- if (strPackName.IndexOf(strFWID.GetData()) > 0)
- {
- Dbg("find FWID[%] from strPackName[%s]", strFWID, strPackName);
- strVerison = strFWID;
- bUpdate = true;
- }
- else
- {
- Dbg("can not find FWID[%] from strPackName[%s]", strFWID, strPackName);
- strErrMsg = "can not find FWID from PackName";
- bUpdate = false;
- }
- }
- break;
- }
- }
- }
- }
- }
- else
- {
- Dbg("pinpad not exist, state: %d", ans.state);
- strErrMsg = "pinpad not exist";
- }
- }
- else
- {
- Dbg("PinPad::GetDevInfo() fail: 0x%X", rc);
- strErrMsg = "PinPad::GetDevInfo() fail";
- }
- pPinPadClient->GetFunction()->CloseSession();
- }
- pPinPadClient->SafeDelete();
- pPinPadClient = NULL;
- return bUpdate;
- }
- ErrorCodeEnum CUpgradeMgrFSM::GetPackInstallFailedCnts(UINT64 &nCnts)
- {
- return GetRunConfigHexIntValue("Install","FailCnts", nCnts);
- }
- ErrorCodeEnum CUpgradeMgrFSM::SetPackInstallFailedCnts(UINT64 nCnts)
- {
- return SetRunConfigHexIntValue("Install", "FailCnts", nCnts);
- }
- ErrorCodeEnum CUpgradeMgrFSM::GetInstallFailedPackName(CSimpleStringA &strPackName)
- {
- return GetRunConfigStrValue("Install","FailPackName", strPackName);
- }
- ErrorCodeEnum CUpgradeMgrFSM::SetInstallFailedPackName(CSimpleStringA strPackName)
- {
- return SetRunConfigStrValue("Install", "FailPackName", strPackName);
- }
- ErrorCodeEnum CUpgradeMgrFSM::SetInstallFailedPackInfo(CSimpleStringA strPackName)
- {
- return SetRunConfigStrValue("Install", "FailedPackInfo", strPackName);
- }
- ErrorCodeEnum CUpgradeMgrFSM::GetInstallFailedPackInfo(CSimpleStringA &strPackName)
- {
- return GetRunConfigStrValue("Install", "FailedPackInfo", strPackName);
- }
- //错误升级包信息格式:包1名称,包1失败次数;包2名称,包2失败次数2;.... 例如:pack1,1;pack2,2;....
- ErrorCodeEnum CUpgradeMgrFSM::UpdateInstallFailedPackInfo(CSimpleStringA strPackName)
- {
- CSimpleStringA strPackInfoList;
- CSimpleStringA strNewPackInfoList;
- Dbg("UpdateInstallFailedPackInfo, %s",strPackName);
- if (Error_Succeed != GetInstallFailedPackInfo(strPackInfoList)
- || strPackInfoList.IsNullOrEmpty())
- {
- strNewPackInfoList = strPackName;
- strNewPackInfoList += ",";
- strNewPackInfoList += "1";
- strNewPackInfoList += ";";
- }
- else
- {
- Dbg("old FailedPackInfo is [%s]", strPackInfoList);
- //add by zl 20191113 运行时最长支持65000,大于这个长度直接清空
- if (strPackInfoList.GetLength() >= 65000)
- {
- strPackInfoList = "";
- }
- if (strPackInfoList.IndexOf(strPackName) == -1)
- {
- //add by zl 20191113 记录所有历史包
- strNewPackInfoList = strPackInfoList;
- strNewPackInfoList += strPackName;
- strNewPackInfoList += ",";
- strNewPackInfoList += "1";
- strNewPackInfoList += ";";
- }
- else
- {
- CAutoArray<CSimpleStringA> arr = strPackInfoList.Split(';');
- for(int i=0; i< arr.GetCount(); i++)
- {
- Dbg("arr[%d]=[%s]", i,arr[i]);
- int nPos = arr[i].IndexOf(",");
- CSimpleStringA strName = arr[i].SubString(0,nPos);
- CSimpleStringA strConut = arr[i].SubString(nPos+1, arr[i].GetLength()-nPos-1);
- Dbg("strName=%s, strConut=%s", strName,strConut);
- if (0 == strName.Compare(strPackName, false))
- //if (strName.IsStartWith(strPackName))
- {
- int nNewCount = atoi(strConut);
- nNewCount++;
- char buf[16] = {0};
- CSimpleStringA strNew = strName;
- strNew += ",";
- itoa(nNewCount,buf,10);
- strNew += buf;
- arr[i] = strNew;
- Dbg("strNew=%s", strNew);
- }
- strNewPackInfoList += arr[i];
- strNewPackInfoList += ";";
- }
- }
- }
- Dbg("new FailedPackInfo is [%s]", strNewPackInfoList);
- return SetInstallFailedPackInfo(strNewPackInfoList);
- }
|