UpgradeMgrFSM.cpp 66 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337
  1. #include "stdafx.h"
  2. #include "SpBase.h"
  3. #include "UpgradeMgrFSM.h"
  4. #include "mod_UpgradeMgr.h"
  5. #include "mod_UpgradeMgr.h"
  6. #include <algorithm>
  7. //#include "EventCode.h"
  8. #include "fileutil.h"
  9. #include "WMIDeviceQuery.h"
  10. #include "UpgradeManager_msg_g.h"
  11. #include "..\mod_pinpad\PinPad_client_g.h"
  12. using namespace PinPad;
  13. #include "..\..\DeviceAdapter\DevInc\DeviceBaseClass.h"
  14. #include "..\EventCode.h"
  15. // 上报状态机(并负责轮询升级)
  16. #define UPGRADE_POLL_INTERVAL 30000 // 升级轮询间隔
  17. //struct testTask : public ITaskSp
  18. //{
  19. // CReportMgrFSM* fsm;
  20. // explicit testTask(CReportMgrFSM* f) : fsm(f) {}
  21. //
  22. // /*void Process()
  23. // {
  24. // int terminalNo = 1015010014;
  25. // int blockId = 1;
  26. // while (terminalNo < 1015010114)
  27. // {
  28. // fsm->m_pConnection->SendMD5ListReq(CSimpleStringA::Format("%d",terminalNo),CSimpleStringA::Format("%d",blockId));
  29. // terminalNo++;
  30. // }
  31. // }*/
  32. //};
  33. void CReportMgrFSM::OnStateTrans(int iSrcState, int iDstState)
  34. {
  35. Dbg("ReportMgrFSM trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  36. }
  37. void CReportMgrFSM::s1_on_entry()
  38. {
  39. ScheduleTimer(1, UPGRADE_POLL_INTERVAL);
  40. }
  41. void CReportMgrFSM::s1_on_exit()
  42. {
  43. CancelTimer(1);
  44. }
  45. unsigned int CReportMgrFSM::s1_on_event(FSMEvent* event)
  46. {
  47. if (event->iEvt == Event_ReportState)
  48. {
  49. ReportStateEvent *pEvent = (ReportStateEvent*)event;
  50. CReportInfo info(pEvent->strPackName, pEvent->InstallVersion, pEvent->dwErrorCode, pEvent->cInstallState, pEvent->strComment);
  51. m_PendingReports.push_back(info);
  52. PostEventFIFO(new FSMEvent(Event_StartConnect));
  53. }
  54. else if (event->iEvt == Event_SwitchConfirm)
  55. {
  56. m_strToConfirmExecID = ((SwitchConfirmEvent*)event)->strPackExecID;
  57. PostEventFIFO(new FSMEvent(Event_StartConnect));
  58. }
  59. else if (event->iEvt == Event_QueryExecInfo)
  60. {
  61. m_strToQueryExecID = ((QueryExecInfoEvent*)event)->strPackExecID;
  62. PostEventFIFO(new FSMEvent(Event_StartConnect));
  63. }
  64. else if (event->iEvt == Event_StartPoll)
  65. {
  66. Dbg("report fsm receive Event_StartPoll 1");
  67. m_bNeedPoll = true;
  68. PostEventFIFO(new FSMEvent(Event_StartConnect));
  69. }
  70. else if (event->iEvt == EVT_TIMER)
  71. {
  72. Dbg("EVT_TIMER, m_bNeedPoll set true");
  73. m_bNeedPoll = true;
  74. PostEventFIFO(new FSMEvent(Event_StartConnect));
  75. }
  76. else if (event->iEvt == Event_ReportSysCustomVer)
  77. {
  78. auto pEvent = (ReportSysCustomVerEvent *)event;
  79. m_strSysCustomPack = pEvent->strPackName;
  80. m_strSysCustomVer = pEvent->strSysCustomVer;
  81. m_strFWID = pEvent->strFWID;
  82. m_strSysPatchName = pEvent->strSysPatchName;
  83. PostEventFIFO(new FSMEvent(Event_StartConnect));
  84. }
  85. else if (event->iEvt == Event_SendMD5list)
  86. {
  87. auto pEvent = (SendMD5ListEvent *)event;
  88. m_strBlockId = pEvent->BlockId;
  89. PostEventFIFO(new FSMEvent(Event_StartConnect));
  90. }
  91. return 0;
  92. }
  93. void CReportMgrFSM::s2_on_entry()
  94. {
  95. if (SecureClientConnect() != Error_Succeed)
  96. {
  97. ScheduleTimer(1, 30000);
  98. Dbg("connect to upgrade service fail, retry in 30 seconds");
  99. }
  100. else
  101. {
  102. PostEventFIFO(new FSMEvent(Event_Connected));
  103. }
  104. }
  105. void CReportMgrFSM::s2_on_exit()
  106. {
  107. //解决sonar扫描
  108. }
  109. unsigned int CReportMgrFSM::s2_on_event(FSMEvent* event)
  110. {
  111. if (event->iEvt == Event_ReportState)
  112. {
  113. ReportStateEvent *pEvent = (ReportStateEvent*)event;
  114. CReportInfo info(pEvent->strPackName, pEvent->InstallVersion, pEvent->dwErrorCode, pEvent->cInstallState, pEvent->strComment);
  115. m_PendingReports.push_back(info);
  116. }
  117. else if (event->iEvt == Event_SwitchConfirm)
  118. {
  119. m_strToConfirmExecID = ((SwitchConfirmEvent*)event)->strPackExecID;
  120. }
  121. else if (event->iEvt == Event_QueryExecInfo)
  122. {
  123. m_strToQueryExecID = ((QueryExecInfoEvent*)event)->strPackExecID;
  124. }
  125. else if (event->iEvt == Event_StartPoll)
  126. {
  127. Dbg("report fsm receive Event_StartPoll 2");
  128. m_bNeedPoll = true;
  129. }
  130. else if (event->iEvt == EVT_TIMER)
  131. {
  132. if (SecureClientConnect() != Error_Succeed)
  133. {
  134. ScheduleTimer(1, 30000);
  135. Dbg("connect to upgrade service fail, retry in 30 seconds");
  136. }
  137. else
  138. {
  139. PostEventFIFO(new FSMEvent(Event_Connected));
  140. }
  141. }
  142. else if (event->iEvt == Event_ReportSysCustomVer)
  143. {
  144. auto pEvent = (ReportSysCustomVerEvent *)event;
  145. m_strSysCustomPack = pEvent->strPackName;
  146. m_strSysCustomVer = pEvent->strSysCustomVer;
  147. m_strFWID = pEvent->strFWID;
  148. m_strSysPatchName = pEvent->strSysPatchName;
  149. }
  150. else if (event->iEvt == Event_SendMD5list)
  151. {
  152. auto pEvent = (SendMD5ListEvent *)event;
  153. m_strBlockId = pEvent->BlockId;
  154. }
  155. return 0;
  156. }
  157. void CReportMgrFSM::s3_on_entry()
  158. {
  159. // 检查持久化队列
  160. for(auto it = m_PendingReports.begin(); it!= m_PendingReports.end(); it++)
  161. {
  162. m_pConnection->ReportUpgradeResult(it->strPackName, it->InstallVersion, it->cInstallState, it->dwStateTime, it->dwFailCode, it->strComment);
  163. }
  164. m_PendingReports.clear();
  165. if (!m_strToConfirmExecID.IsNullOrEmpty())
  166. {
  167. m_pConnection->SendConfirmUpgradeReq(m_strToConfirmExecID);
  168. m_strToConfirmExecID.Clear();
  169. }
  170. if (!m_strToQueryExecID.IsNullOrEmpty())
  171. {
  172. m_pConnection->SendQueryExecInfoReq(m_strToQueryExecID);
  173. m_strToQueryExecID.Clear();
  174. }
  175. if (!m_strSysCustomPack.IsNullOrEmpty()
  176. && (!m_strSysCustomVer.IsNullOrEmpty() || !m_strFWID.IsNullOrEmpty() || !m_strSysPatchName.IsNullOrEmpty()))
  177. {
  178. m_pConnection->ReportSysCustomVer(m_strSysCustomPack, m_strSysCustomVer, m_strFWID, m_strSysPatchName);
  179. m_strSysCustomPack.Clear();
  180. m_strSysCustomVer.Clear();
  181. m_strFWID.Clear();
  182. m_strSysPatchName.Clear();
  183. }
  184. if (m_bNeedPoll)
  185. {
  186. Dbg("SendPollUpgradeReq 1");
  187. m_pConnection->SendPollUpgradeReq();
  188. m_bNeedPoll = false;
  189. }
  190. if (!m_strBlockId.IsNullOrEmpty())
  191. {
  192. m_pConnection->SendMD5ListReq(m_strBlockId);
  193. m_strBlockId.Clear();
  194. }
  195. m_LastReqTime = CSmallDateTime::GetNow();
  196. ScheduleTimer(1, 3000);
  197. /*testTask* task = new testTask(this);
  198. GetEntityBase()->GetFunction()->PostThreadPoolTask(task);*/
  199. }
  200. void CReportMgrFSM::s3_on_exit()
  201. {
  202. SecureClientRelease();
  203. }
  204. unsigned int CReportMgrFSM::s3_on_event(FSMEvent* event)
  205. {
  206. if (event->iEvt == Event_ReportState)
  207. {
  208. ReportStateEvent *pEvent = (ReportStateEvent*)event;
  209. m_pConnection->ReportUpgradeResult(pEvent->strPackName, pEvent->InstallVersion, pEvent->cInstallState, CSmallDateTime::GetNow(), pEvent->dwErrorCode, pEvent->strComment);
  210. m_LastReqTime = CSmallDateTime::GetNow();
  211. }
  212. else if (event->iEvt == Event_StartPoll)
  213. {
  214. // 防止过度查询
  215. if (CSmallDateTime::GetNow() - m_LastPollTime >= UPGRADE_POLL_INTERVAL / 1000)
  216. {
  217. Dbg("SendPollUpgradeReq 2");
  218. m_pConnection->SendPollUpgradeReq();
  219. m_LastPollTime = CSmallDateTime::GetNow();
  220. m_LastReqTime = CSmallDateTime::GetNow();
  221. }
  222. }
  223. else if (event->iEvt == Event_SendMD5list)
  224. {
  225. auto pEvent = (SendMD5ListEvent *)event;
  226. m_strBlockId = pEvent->BlockId;
  227. m_pConnection->SendMD5ListReq(m_strBlockId);
  228. m_strBlockId.Clear();
  229. m_LastReqTime = CSmallDateTime::GetNow();
  230. }
  231. else if (event->iEvt == Event_SwitchConfirm)
  232. {
  233. // 切换确认
  234. auto pEvent = (SwitchConfirmEvent*)event;
  235. m_pConnection->SendConfirmUpgradeReq(pEvent->strPackExecID);
  236. m_LastReqTime = CSmallDateTime::GetNow();
  237. }
  238. else if (event->iEvt == CUpgradeMgrFSM::Event_EndPoll
  239. || event->iEvt == CUpgradeMgrFSM::Event_EndConfirm
  240. || event->iEvt == CUpgradeMgrFSM::Event_EndQueryExec
  241. || event->iEvt == CUpgradeMgrFSM::Event_CancelUpgrade
  242. /*|| event->iEvt == CUpgradeMgrFSM::Event_ContinueMD5
  243. || event->iEvt == CUpgradeMgrFSM::Event_StartMD5
  244. || event->iEvt == CUpgradeMgrFSM::Event_EndMD5
  245. || event->iEvt == CUpgradeMgrFSM::Event_RetryMD5*/)
  246. {
  247. // 转交给外层状态机处理
  248. event->IncRef(); // 防止被释放
  249. m_pOuterFSM->PostEventFIFO(event);
  250. }
  251. else if (event->iEvt == Event_QueryExecInfo)
  252. {
  253. auto pEvent = (QueryExecInfoEvent*)event;
  254. m_pConnection->SendQueryExecInfoReq(pEvent->strPackExecID);
  255. m_LastReqTime = CSmallDateTime::GetNow();
  256. }
  257. else if (event->iEvt == Event_ReportSysCustomVer)
  258. {
  259. auto pEvent = (ReportSysCustomVerEvent *)event;
  260. m_pConnection->ReportSysCustomVer(pEvent->strPackName, pEvent->strSysCustomVer, pEvent->strFWID, pEvent->strSysPatchName);
  261. m_LastReqTime = CSmallDateTime::GetNow();
  262. }
  263. else if (event->iEvt == EVT_TIMER)
  264. {
  265. if ((DWORD)CSmallDateTime::GetNow() - (DWORD)m_LastReqTime >= 90)
  266. {
  267. // 空闲5秒以上
  268. PostEventFIFO(new FSMEvent(Event_Disconnected));
  269. }
  270. else
  271. {
  272. ScheduleTimer(1, 3000);
  273. }
  274. }
  275. return 0;
  276. }
  277. ErrorCodeEnum CReportMgrFSM::SecureClientConnect()
  278. {
  279. if (m_pConnection != NULL)
  280. {
  281. if (m_pConnection->IsConnectionOK())
  282. return Error_Succeed;
  283. else
  284. SecureClientRelease();
  285. }
  286. m_pConnection = new UpgradeConnect::CUpgradeMgrConn(m_pEntity, this);
  287. if (m_pConnection->ConnectFromCentralSetting() && m_pConnection->IsConnectionOK())
  288. return Error_Succeed;
  289. SecureClientRelease();
  290. return Error_PeerReject;
  291. }
  292. ErrorCodeEnum CReportMgrFSM::SecureClientRelease()
  293. {
  294. if (m_pConnection != NULL)
  295. {
  296. m_pConnection->Close();
  297. m_pConnection->DecRefCount();
  298. m_pConnection = NULL;
  299. }
  300. return Error_Succeed;
  301. }
  302. CUpgradeMgrFSM::CUpgradeMgrFSM()
  303. {
  304. m_ReportFSM.SetOuterFSM(this);
  305. }
  306. ErrorCodeEnum CUpgradeMgrFSM::OnInit()
  307. {
  308. m_ReportFSM.Init(m_pEntity);
  309. AddStateHooker(this);
  310. //modify by lwt 20200610 增加hash文件黑名单和白名单配置文件读取
  311. CSmartPointer<IConfigInfo> spConfig;
  312. auto rc = m_pEntity->GetFunction()->OpenConfig(Config_Software, spConfig);
  313. if (rc == Error_Succeed){
  314. CSimpleStringA str="";
  315. ErrorCodeEnum Error = spConfig->ReadConfigValue("HashFilter", "DepWhitelist", str);
  316. if(Error==Error_Succeed){
  317. CAutoArray<CSimpleStringA> arr = str.Split('|');
  318. for(int i=0; i< arr.GetCount(); i++)
  319. {
  320. m_DepWhitelist.push_back(arr[i]);
  321. }
  322. Dbg("init read [HashFilter] DepWhitelist=%s",str);
  323. }else{
  324. LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini DepWhitelist error");
  325. }
  326. Error = spConfig->ReadConfigValue("HashFilter", "DirBlacklist", str);
  327. if(Error==Error_Succeed){
  328. CAutoArray<CSimpleStringA> arr = str.Split('|');
  329. for(int i=0; i< arr.GetCount(); i++)
  330. {
  331. m_DirBlacklist.push_back(arr[i]);
  332. }
  333. Dbg("init read [HashFilter] DirBlacklist=%s",str);
  334. }else{
  335. LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini DirBlacklist error");
  336. }
  337. Error = spConfig->ReadConfigValue("HashFilter", "FileBlacklist", str);
  338. if(Error==Error_Succeed){
  339. CAutoArray<CSimpleStringA> arr = str.Split('|');
  340. for(int i=0; i< arr.GetCount(); i++)
  341. {
  342. m_FileBlacklist.push_back(arr[i]);
  343. }
  344. Dbg("init read [HashFilter] FileBlacklist=%s",str);
  345. }else{
  346. LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini FileBlacklist error");
  347. }
  348. }else{
  349. LogWarn(Severity_Middle, rc, ERR_READ_INI_FAIL, "get upgradeMgr.ini error");
  350. }
  351. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::SendMD5ListEvent("1"));
  352. return Error_Succeed;
  353. }
  354. ErrorCodeEnum CUpgradeMgrFSM::OnExit()
  355. {
  356. RemoveStateHooker(this);
  357. return Error_Succeed;
  358. }
  359. char CUpgradeMgrFSM::GetInstallState(int nState)
  360. {
  361. if (nState == s3)
  362. return 'U';
  363. else if (nState == s4)
  364. return 'I';
  365. else if (nState == s5)
  366. return 'C';
  367. else if (nState == s6)
  368. return 'S';
  369. else
  370. return 'N';
  371. }
  372. void CUpgradeMgrFSM::OnStateTrans(int iSrcState, int iDstState)
  373. {
  374. Dbg("state trans from %s to %s", GetStateName(iSrcState), GetStateName(iDstState));
  375. if (iSrcState >= s3 && iDstState <= s2)
  376. {
  377. // 完成或取消升级,清空运行时配置
  378. Dbg("clear upgrade info from runcfg");
  379. SetRunConfigStrValue("Install", "Pack", "");
  380. SetRunConfigStrValue("Install", "TriggerType", "");
  381. SetRunConfigHexIntValue("Install", "TriggerTime", 0);
  382. SetRunConfigStrValue("Install", "PendingState", "");
  383. SetRunConfigStrValue("Install", "bSysInstall", "");
  384. SetRunConfigStrValue("Install", "PackExecID", "");
  385. SetRunConfigStrValue("Install", "NewVersion", "");
  386. SetRunConfigHexIntValue("Install", "DownloadTime", 0);
  387. // 发送广播(通知业务界面清除状态)
  388. UpgradeStateEvent stateEvent;
  389. stateEvent.strPackName = "";
  390. stateEvent.strExecID = "";
  391. stateEvent.cInstallState = 'N';
  392. stateEvent.bSysInstall = false;
  393. stateEvent.bLightPack = false;
  394. stateEvent.strNewVersion = "";
  395. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
  396. }
  397. else if (iDstState >= s3)
  398. {
  399. assert(HasRunningUpdate());
  400. SetRunConfigStrValue("Install", "Pack", m_strIntallingPack);
  401. if (iDstState == s3)
  402. SetRunConfigStrValue("Install", "PendingState", "U");
  403. else if (iDstState == s4)
  404. SetRunConfigStrValue("Install", "PendingState", "I");
  405. else if (iDstState == s5)
  406. SetRunConfigStrValue("Install", "PendingState", "C");
  407. else if (iDstState == s6)
  408. SetRunConfigStrValue("Install", "PendingState", "S");
  409. // 发送广播
  410. UpgradeStateEvent stateEvent;
  411. stateEvent.strPackName = m_strIntallingPack;
  412. auto &pack = m_PendingPacks[m_strIntallingPack];
  413. stateEvent.strExecID = pack.strPackExecID;
  414. stateEvent.cInstallState = GetInstallState(iDstState);
  415. stateEvent.bSysInstall = pack.bSysInstall;
  416. stateEvent.bLightPack = false;
  417. stateEvent.strNewVersion = m_strNewVersion;
  418. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
  419. }
  420. }
  421. bool CUpgradeMgrFSM::IsPackDownloaded(const char *pPackName)
  422. {
  423. CSimpleStringA strPath;
  424. auto rc = m_pEntity->GetFunction()->GetPath("Downloads", strPath);
  425. assert(rc == Error_Succeed);
  426. CSimpleStringA strZipFile = CSimpleStringA::Format("%s\\%s", (const char*)strPath, pPackName);
  427. //此处什么场景下会发生?
  428. if (!strZipFile.IsEndWith(".zip") && !strZipFile.IsEndWith(".cab"))
  429. strZipFile += ".zip";
  430. DWORD attr = GetFileAttributesA(strZipFile);
  431. return (attr != INVALID_FILE_ATTRIBUTES) && !(attr & FILE_ATTRIBUTE_DIRECTORY);
  432. }
  433. bool CUpgradeMgrFSM::IsPackCancelled(const char *pPackName)
  434. {
  435. auto it = find_if(m_CancelledPacks.begin(), m_CancelledPacks.end(), [pPackName](CSimpleStringA &str)
  436. {
  437. return stricmp((const char*)str, pPackName) == 0;
  438. });
  439. return it != m_CancelledPacks.end();
  440. }
  441. bool CUpgradeMgrFSM::IsPackInstalled(const char *pPackName)
  442. {
  443. /*bool bInstalled = find_if(
  444. m_InstallFailPacks.begin(),
  445. m_InstallFailPacks.end(),
  446. [pPackName](CSimpleStringA &str) { return stricmp((const char*)str, pPackName) == 0; }) != m_InstallFailPacks.end();
  447. if (bInstalled)
  448. return true;*/
  449. //判断是否是已升级的系统外升级包
  450. auto pPrivFunc = m_pEntity->GetFunction()->GetPrivilegeFunction();
  451. auto rc = pPrivFunc->IsInstalledSysPack(pPackName);
  452. if (rc == Error_Succeed)
  453. {
  454. Dbg("%s is been installed", pPackName);
  455. return true;
  456. }
  457. //连续3次安装失败,则忽略;其他情况继续尝试安装
  458. return IsPackFialOver3Times(pPackName);
  459. //zl 20181016去掉根据内存和log判断是否已安装,以便处理出现升级回退情时继续重新升级。
  460. //// 根据安装日志文件判断是否曾经安装过(跟踪轻量包)
  461. //auto pPrivFunc = m_pEntity->GetFunction()->GetPrivilegeFunction();
  462. //pPrivFunc->IsPackInstalled(pPackName, bInstalled);
  463. //return bInstalled;
  464. }
  465. bool CUpgradeMgrFSM::IsPackFialOver3Times(const char *pPackName)
  466. {
  467. bool bFialOver3Times = false;
  468. CSimpleStringA strFailedPackInfo;
  469. //从安装包失败列表中查找
  470. if (Error_Succeed == GetInstallFailedPackInfo(strFailedPackInfo))
  471. {
  472. Dbg("strFailPackNameList[%s},pPackName[%s]",strFailedPackInfo.GetData(),pPackName);
  473. int nStartPos = strFailedPackInfo.IndexOf(pPackName);
  474. if (nStartPos != -1)
  475. {
  476. CAutoArray<CSimpleStringA> arr = strFailedPackInfo.Split(';');
  477. for(int i=0; i< arr.GetCount(); i++)
  478. {
  479. int nPos = arr[i].IndexOf(",");
  480. CSimpleStringA strName = arr[i].SubString(0,nPos);
  481. CSimpleStringA strConut = arr[i].SubString(nPos+1, arr[i].GetLength()-nPos-1);
  482. if (strName.IsStartWith(pPackName))
  483. {
  484. if (atoi(strConut) >= 3)
  485. {
  486. return true;
  487. }
  488. }
  489. }
  490. }
  491. return false;
  492. }
  493. /*CSimpleStringA strPackName;
  494. UINT64 nPackCnts;
  495. auto rc = GetInstallFailedPackName(strPackName);
  496. if (Error_Succeed != rc)
  497. {
  498. LogError(Severity_Middle, rc , 0, "not exist PackName sysvar");
  499. return false;
  500. }
  501. else
  502. {
  503. Dbg("installingName[%s},failName[%s]",pPackName,strPackName);
  504. if (0 == strPackName.Compare(pPackName, true))
  505. {
  506. rc = GetPackInstallFailedCnts(nPackCnts);
  507. if (Error_Succeed != rc)
  508. {
  509. LogError(Severity_Middle, rc , 0, "not exist PackInstallFailCnts sysvar");
  510. return false;
  511. }
  512. else
  513. {
  514. if (3 == nPackCnts)
  515. {
  516. bFialOver3Times = true;
  517. Dbg("%s is install failed 3 times, ignore it", pPackName);
  518. return true;
  519. }
  520. }
  521. }
  522. else
  523. {
  524. SetInstallFailedPackName(pPackName);
  525. SetPackInstallFailedCnts(0);
  526. }
  527. }*/
  528. return false;
  529. }
  530. ErrorCodeEnum CUpgradeMgrFSM::LoadPersistInfo()
  531. {
  532. // 从运行配置中读出重启前安装包信息
  533. CSimpleStringA strValue;
  534. //手动安装包信息
  535. auto rc = GetRunConfigStrValue("Install", "ManualPackList", strValue);
  536. if (rc == Error_Succeed && strValue.GetLength() >0)
  537. {
  538. CAutoArray<CSimpleStringA> arr = strValue.Split(',');
  539. for(int i=0; i<arr.GetCount(); i++)
  540. {
  541. m_ManualPendingPacks.push_back(arr[i]);
  542. }
  543. }
  544. else
  545. {
  546. Dbg("ManualPackList in run config is null");
  547. }
  548. rc = GetRunConfigStrValue("Install", "Pack", strValue);
  549. if (rc == Error_Succeed && strValue.GetLength() >0)
  550. {
  551. // 上次未安装结束
  552. CPendingPackInfo info = {};
  553. info.strPackName = strValue;
  554. info.nLevel = 2;
  555. rc = GetRunConfigStrValue("Install", "TriggerType", strValue);
  556. if (rc == Error_Succeed && strValue.GetLength() == 1)
  557. info.cTriggerType = strValue[0];
  558. else
  559. info.cTriggerType = 'I';
  560. UINT64 nValue(0);
  561. rc = GetRunConfigHexIntValue("Install", "TriggerTime", nValue);
  562. if (rc == Error_Succeed && nValue >0)
  563. info.dwTriggerTimer = nValue;
  564. else
  565. info.dwTriggerTimer = 0;
  566. rc = GetRunConfigStrValue("Install", "PendingState", strValue);
  567. if (rc == Error_Succeed && strValue.GetLength() >0)
  568. info.cPendingState = strValue[0];
  569. else
  570. info.cPendingState = 'N';
  571. rc = GetRunConfigStrValue("Install", "bSysInstall", strValue);
  572. if (rc == Error_Succeed && strValue.GetLength() >0)
  573. info.bSysInstall = atol(strValue) == 1;
  574. else
  575. info.bSysInstall = false;
  576. rc = GetRunConfigStrValue("Install", "PackExecID", strValue);
  577. if (rc == Error_Succeed && strValue.GetLength() >0)
  578. info.strPackExecID = strValue;
  579. else
  580. info.strPackExecID = "";
  581. rc = GetRunConfigStrValue("Install", "NewVersion", strValue);
  582. if (rc == Error_Succeed && strValue.GetLength() >0)
  583. m_strNewVersion = strValue;
  584. else
  585. m_strNewVersion = "";
  586. rc = GetRunConfigHexIntValue("Install", "DownloadTime", nValue);
  587. if (rc == Error_Succeed && nValue >0)
  588. m_dwDownloadTime = nValue;
  589. else
  590. m_dwDownloadTime = 0;
  591. Dbg("recover upgrade state from run config, install pack: {%s}", info.strPackName);
  592. m_strIntallingPack = info.strPackName;
  593. m_PendingPacks[m_strIntallingPack] = info;
  594. }
  595. return Error_Succeed;
  596. }
  597. void CUpgradeMgrFSM::s1_on_entry()
  598. {
  599. }
  600. void CUpgradeMgrFSM::s1_on_exit()
  601. {
  602. }
  603. unsigned int CUpgradeMgrFSM::s1_on_event(FSMEvent* event)
  604. {
  605. // 准入成功
  606. if (event->iEvt == Event_EntryPermit)
  607. {
  608. // 检查安装版本状态
  609. auto pFunc = m_pEntity->GetFunction();
  610. auto pPrivilegeFunc = pFunc->GetPrivilegeFunction();
  611. assert(pPrivilegeFunc != NULL);
  612. CSystemStaticInfo info;
  613. auto rc = pFunc->GetSystemStaticInfo(info);
  614. if (rc == Error_Succeed)
  615. {
  616. if ((info.InstallState == Install_SetToStart) // 已设置启动,表明是第一次启动
  617. || (info.InstallState == Install_Upgraded)) // 已升级状态 表示当前是回滚
  618. {
  619. bool bRollback = (info.InstallState == Install_Upgraded);
  620. rc = pPrivilegeFunc->SetRunSucceed();
  621. if (rc != Error_Succeed)
  622. {
  623. LogError(Severity_Middle, rc , 0, "call SetRunSucceed() error");
  624. return 1;
  625. }
  626. if (bRollback)
  627. {
  628. // 上报后一版本状态为倒回
  629. auto pPrivFunc = pFunc->GetPrivilegeFunction();
  630. assert(pPrivilegeFunc != NULL);
  631. CSystemStaticInfo latterVerInfo;
  632. rc = pPrivFunc->GetSystemStaticInfoForVersion(info.LatterInstallVersion, latterVerInfo);
  633. if (rc == Error_Succeed)
  634. {
  635. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(latterVerInfo.InstallPack, latterVerInfo.InstallVersion, 0, 'R', "自动倒回"));
  636. LogEvent(Severity_Low, 0, CSimpleStringA::Format("rollback to history version [%s] succeed", (const char*)info.InstallVersion.ToString()));
  637. //回退信息写入运行时配置
  638. if (!latterVerInfo.InstallPack.IsNullOrEmpty())
  639. {
  640. UpdateInstallFailedPackInfo(latterVerInfo.InstallPack);
  641. }
  642. else
  643. {
  644. Dbg("latterVerInfo.InstallPack is IsNullOrEmpty return true, can't UpdateInstallFailedPackInfo");
  645. }
  646. }
  647. else
  648. {
  649. Dbg("GetSystemStaticInfoForVersion for latterVerInfo failed");
  650. }
  651. }
  652. else
  653. LogEvent(Severity_Low, 0, CSimpleStringA::Format("upgrade to new version [%s] succeed", (const char*)info.InstallVersion.ToString()));
  654. // 上报后台状态
  655. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(info.InstallPack, info.InstallVersion, 0, 'A', "启动成功"));
  656. }
  657. // 加载持久化上报信息
  658. LoadPersistInfo();
  659. //获取手动升级任务安装结果
  660. UpdateManualTaskState();
  661. if (HasRunningUpdate())
  662. {
  663. auto &pack = m_PendingPacks[m_strIntallingPack];
  664. if (pack.cPendingState == 'U' || pack.cPendingState == 'N')
  665. return 3;
  666. else if (pack.cPendingState == 'D' || pack.cPendingState == 'I')
  667. return 4;
  668. else if (pack.cPendingState == 'C')
  669. return 5;
  670. else if (pack.cPendingState == 'S')
  671. return 6;
  672. }
  673. }
  674. else
  675. {
  676. LogError(Severity_Middle, rc, 0, "GetSystemStaticInfo() fail");
  677. }
  678. }
  679. return 0;
  680. }
  681. void CUpgradeMgrFSM::s2_on_entry()
  682. {
  683. m_strIntallingPack.Clear();
  684. m_nIntallingPackType = 1;
  685. m_strNewVersion = "";
  686. Dbg("send Event_StartPoll 2");
  687. PostEventFIFO(new FSMEvent(Event_StartPoll));
  688. }
  689. void CUpgradeMgrFSM::s2_on_exit()
  690. {
  691. }
  692. unsigned int CUpgradeMgrFSM::s2_on_event(FSMEvent* event)
  693. {
  694. if (event->iEvt == Event_StartPoll)
  695. {
  696. Dbg("send Event_StartPoll 3");
  697. m_ReportFSM.PostEventFIFO(new FSMEvent(CReportMgrFSM::Event_StartPoll));
  698. }
  699. else if (event->iEvt == Event_EndPoll)
  700. {
  701. RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
  702. UpgradeConnect::PollUpgrade_Ret5 *pRet = (UpgradeConnect::PollUpgrade_Ret5*)pEvent->GetRetData();
  703. int nArrayNum = pEvent->GetArrayNum();
  704. if (nArrayNum >0 && pRet != NULL)
  705. {
  706. CSimpleStringA strInstalledPacks;
  707. CSimpleStringA strCanceledPacks;
  708. Dbg("nArrayNum=%d",nArrayNum);
  709. for(int i=0; i<nArrayNum; i++)
  710. {
  711. CSimpleStringA packName = pRet[i].UpgradePack;
  712. CSimpleStringA newPackName = pRet[i].NewUpgradePack;
  713. Dbg("poll ans:packName[%s], newPackName[%s], ExecID[%s].", pRet[i].UpgradePack, pRet[i].NewUpgradePack, pRet[i].PackExecID);
  714. if (!newPackName.IsNullOrEmpty())
  715. {
  716. packName = newPackName;
  717. }
  718. //此处什么场景下会发生?升级包注册时可以不是zip或cab格式?
  719. if (!packName.IsEndWith(".zip", true) && !packName.IsEndWith(".cab", true))
  720. packName += ".zip";
  721. if (IsPackInstalled(packName.GetData()))
  722. {
  723. if (!strInstalledPacks.IsNullOrEmpty())
  724. strInstalledPacks += "、";
  725. strInstalledPacks += packName.SubString(0, packName.GetLength()-4);
  726. //Dbg("upgrade pack [%s] already installed, ignore it", (const char*)packName);
  727. continue; // 已安装
  728. }
  729. else if (IsPackCancelled(packName))
  730. {
  731. if (!strCanceledPacks.IsNullOrEmpty())
  732. strCanceledPacks += "、";
  733. strCanceledPacks += packName.SubString(0, packName.GetLength()-4);
  734. //Dbg("upgrade pack [%s] already cancelled, ignore it", (const char*)packName);
  735. continue; // 已取消
  736. }
  737. if (newPackName.IsNullOrEmpty())
  738. {
  739. Dbg("PackName:%s, PackExecID:%s, no need to upgrade.", pRet[i].UpgradePack, pRet[i].PackExecID);
  740. continue;
  741. }
  742. if (m_PendingPacks.find(packName) == m_PendingPacks.end())
  743. {
  744. Dbg("find new install pack: [%s]", (const char*)packName);
  745. //add by zl 20181015,开始升级前同步时间
  746. ((CUpgradeMgrEntity*)m_pEntity)->SyncTime();
  747. Dbg("Sync Time from server");
  748. CPendingPackInfo info = {};
  749. info.strPackName = packName;
  750. info.nLevel = pRet[i].Level;
  751. info.cTriggerType = pRet[i].TriggerType;
  752. info.dwTriggerTimer = pRet[i].TriggerTimer;
  753. info.cPendingState = IsPackDownloaded(packName) ? 'D' : 'N';
  754. info.bSysInstall = false;
  755. info.strPackExecID = pRet[i].PackExecID;
  756. m_PendingPacks[packName] = info;
  757. }
  758. }
  759. if (!strInstalledPacks.IsNullOrEmpty())
  760. Dbg("ignore installed packs %s", (const char*)strInstalledPacks);
  761. if (!strCanceledPacks.IsNullOrEmpty())
  762. Dbg("ignore cancelled packs: %s", (const char*)strCanceledPacks);
  763. }
  764. else
  765. {
  766. Dbg("no available upgrade pack");
  767. }
  768. //广播手动升级任务消息
  769. //如果任务个数不为0,则提示有升级任务;为0,则不提示
  770. ManualTaskEvent taskEvent;
  771. taskEvent.nTaskNum = m_ManualPendingPacks.size();
  772. Dbg("manaul task num[%d]", m_ManualPendingPacks.size());
  773. if (!m_ManualPendingPacks.empty())
  774. {
  775. CSimpleStringA strManualPacks;
  776. list<CSimpleStringA>::iterator itor;
  777. itor = m_ManualPendingPacks.begin();
  778. while(itor!=m_ManualPendingPacks.end())
  779. {
  780. strManualPacks += *itor++;
  781. if (itor != m_ManualPendingPacks.end())
  782. {
  783. strManualPacks += ",";
  784. }
  785. }
  786. //持久化手动升级任务列表
  787. SetRunConfigStrValue("Install", "ManualPackList", strManualPacks);
  788. }
  789. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_ManualTaskEvent, eMsgSig_ManualTaskEvent, taskEvent);
  790. // 优先升级筛选
  791. if (m_PendingPacks.size() >0)
  792. {
  793. // 根据优先级找出优先升级的安装包
  794. auto findPack = GetPriorInstallPack();
  795. Dbg("priority install pack: [%s]", (const char*)findPack->strPackName);
  796. m_strIntallingPack = findPack->strPackName;
  797. if (3 == findPack->nLevel)
  798. {
  799. m_nIntallingPackType = 2;
  800. }
  801. SetRunConfigStrValue("Install", "Pack", m_strIntallingPack);
  802. SetRunConfigStrValue("Install", "TriggerType", CSimpleStringA::Format("%c", findPack->cTriggerType));
  803. SetRunConfigHexIntValue("Install", "TriggerTime", findPack->dwTriggerTimer);
  804. SetRunConfigStrValue("Install", "PendingState", CSimpleStringA::Format("%c", findPack->cPendingState));
  805. SetRunConfigStrValue("Install", "PackExecID", findPack->strPackExecID);
  806. SetRunConfigStrValue("Install", "NewVersion", "");
  807. m_strNewVersion = "";
  808. if (findPack->cPendingState == 'N') // 未下载
  809. {
  810. // 通知下载实体下载
  811. Dbg("request download pack: [%s]", (const char*)findPack->strPackName);
  812. ((CUpgradeMgrEntity*)m_pEntity)->DownloadPack(findPack->strPackName);
  813. PostEventFIFO(new FSMEvent(Event_StartDownload));
  814. WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("request download pack: [%s]", (const char*)m_strIntallingPack));
  815. // 设置下载时间
  816. m_dwDownloadTime = CSmallDateTime::GetNow();
  817. SetRunConfigHexIntValue("Install", "DownloadTime", m_dwDownloadTime);
  818. // 上报下载状态到服务器 xkm@20160412
  819. CSystemStaticInfo ssInfo;
  820. m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
  821. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, 0, 'D', "正在下载"));
  822. }
  823. else if (findPack->cPendingState == 'U') // 下载中
  824. {
  825. Dbg("request download pack again: [%s]", (const char*)findPack->strPackName);
  826. ((CUpgradeMgrEntity*)m_pEntity)->DownloadPack(findPack->strPackName);
  827. PostEventFIFO(new FSMEvent(Event_StartDownload));
  828. }
  829. else if (findPack->cPendingState == 'D') // 已下载
  830. {
  831. PostEventFIFO(new FSMEvent(Event_StartInstall));
  832. WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("start install pack: [%s]", (const char*)m_strIntallingPack));
  833. }
  834. else if (findPack->cPendingState == 'C') // 等待确认
  835. {
  836. // 重新回到等待确认状态
  837. PostEventFIFO(new FSMEvent(Event_WaitConfirm));
  838. }
  839. else
  840. {
  841. LogError(Severity_Middle, Error_Bug, 0, CSimpleStringA::Format("invalid pending state :%c", (const char*)findPack->cPendingState));
  842. }
  843. }
  844. }
  845. else if (event->iEvt == Event_CancelUpgrade)
  846. {
  847. // 紧急取消安装
  848. RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
  849. CancelUpgradePacks(pEvent);
  850. }
  851. return 0;
  852. }
  853. void CUpgradeMgrFSM::PushCancelUpgradePack(const CSimpleStringA &strPackName)
  854. {
  855. Dbg("local cancel install package: [%s]", (const char*)strPackName);
  856. if (!IsPackCancelled(strPackName))
  857. m_CancelledPacks.push_back(strPackName);
  858. }
  859. bool CUpgradeMgrFSM::HasDownloadingPack()
  860. {
  861. if (m_strIntallingPack.IsNullOrEmpty())
  862. return false;
  863. auto &pack = m_PendingPacks[m_strIntallingPack];
  864. return pack.cPendingState == 'U';
  865. }
  866. bool CUpgradeMgrFSM::HasRunningUpdate()
  867. {
  868. return !m_strIntallingPack.IsNullOrEmpty();
  869. }
  870. ErrorCodeEnum CUpgradeMgrFSM::RegistLocalPack(const CSimpleStringA &strPackName)
  871. {
  872. Dbg("regist local pack [%s]", (const char*)strPackName);
  873. // xkm@20161122: 本地不存在时自动启动指定下载
  874. //// 检查对应文件是否存在
  875. //CSimpleStringA strDownloadsPath;
  876. //auto rc = m_pEntity->GetFunction()->GetPath("Downloads", strDownloadsPath);
  877. //assert(rc == Error_Succeed);
  878. //CSimpleStringA strZipFile = CSimpleStringA::Format("%s\\%s", (const char*)strDownloadsPath, (const char*)strPackName);
  879. //if (!ExistsFileA(strZipFile))
  880. //{
  881. // LogError(Severity_Low, Error_NotExist, 0, CSimpleStringA::Format("pack [%s] not exists", (const char*)strPackName));
  882. // return Error_NotExist;
  883. //}
  884. // xkm@20161122: 注册本地升级忽略安装历史
  885. //if (IsPackInstalled(strPackName))
  886. //{
  887. // LogError(Severity_Low, Error_Cancel, 0, (const char*)CSimpleStringA::Format("pack [%s] already installed", (const char*)strPackName));
  888. // return Error_AlreadyExist; // 已安装
  889. //}
  890. //else if (IsPackCancelled(strPackName))
  891. //{
  892. // LogError(Severity_Low, Error_Cancel, 0, (const char*)CSimpleStringA::Format("pack [%s] already canceled", (const char*)strPackName));
  893. // return Error_Cancel; // 已取消
  894. //}
  895. if (m_PendingPacks.find(strPackName) == m_PendingPacks.end())
  896. {
  897. CPendingPackInfo info = {};
  898. info.strPackName = strPackName;
  899. info.nLevel = 2;
  900. info.cTriggerType = 'I';
  901. info.dwTriggerTimer = 0;
  902. info.cPendingState = 'U';
  903. info.bSysInstall = false;
  904. info.strPackExecID = "";
  905. m_PendingPacks[strPackName] = info;
  906. Dbg("add local pack [%s] succeed", (const char*)strPackName);
  907. PostEventFIFO(new RvcCommRetEvent(Event_EndPoll, NULL, 0));
  908. }
  909. return Error_Succeed;
  910. }
  911. DWORD CUpgradeMgrFSM::RegistManualPack(const CSimpleStringA &strPackName)
  912. {
  913. Dbg("regist Manual pack [%s]", (const char*)strPackName);
  914. bool bManual= find_if(
  915. m_ManualPendingPacks.begin(),
  916. m_ManualPendingPacks.end(),
  917. [strPackName](CSimpleStringA &str) { return str.Compare(strPackName) == 0; }) != m_ManualPendingPacks.end();
  918. if (!bManual)
  919. {
  920. Dbg("[%s]不是手动安装包", strPackName);
  921. return NOT_MANUALPACK_ERR;
  922. }
  923. if (m_PendingPacks.find(strPackName) == m_PendingPacks.end())
  924. {
  925. CPendingPackInfo info = {};
  926. info.strPackName = strPackName;
  927. info.nLevel = 3;//手动升级级别为3
  928. info.cTriggerType = 'I';
  929. info.dwTriggerTimer = 0;
  930. info.cPendingState = 'D';
  931. info.bSysInstall = true; //系统外升级
  932. info.strPackExecID = "";
  933. m_PendingPacks[strPackName] = info;
  934. Dbg("add Manual pack [%s] succeed", (const char*)strPackName);
  935. PostEventFIFO(new RvcCommRetEvent(Event_EndPoll, NULL, 0));
  936. }
  937. else
  938. {
  939. //返回错误码
  940. Dbg("终端正在升级,请稍后再进行手动升级");
  941. return IS_UPDATEING_ERR;
  942. }
  943. return ERROR_SUCCESS;
  944. }
  945. DWORD CUpgradeMgrFSM::GetManualPacks(CSimpleStringA &strManualPacks)
  946. {
  947. /*if (NULL == strManualPacks)
  948. {
  949. return NULL_PARAM_ERR;
  950. }*/
  951. if (!m_ManualPendingPacks.empty())
  952. {
  953. list<CSimpleStringA>::iterator itor;
  954. itor = m_ManualPendingPacks.begin();
  955. while(itor!=m_ManualPendingPacks.end())
  956. {
  957. strManualPacks += *itor++;
  958. if (itor != m_ManualPendingPacks.end())
  959. {
  960. strManualPacks += ",";
  961. }
  962. }
  963. }
  964. else
  965. {
  966. strManualPacks = NULL;
  967. return NO_EXIST_MANUAL_PACK_ERR;
  968. }
  969. return ERROR_SUCCESS;
  970. }
  971. ErrorCodeEnum ;
  972. void CUpgradeMgrFSM::CancelUpgradePacks(RvcCommRetEvent *pEvent)
  973. {
  974. UpgradeConnect::PollUpgrade_Ret4 *pRet = (UpgradeConnect::PollUpgrade_Ret4*)pEvent->GetRetData();
  975. int nArrayNum = pEvent->GetArrayNum();
  976. if (nArrayNum >0 && pRet != NULL)
  977. {
  978. for(int i=0; i<nArrayNum; i++)
  979. {
  980. CSimpleStringA strPack = pRet[i].CancelPack;
  981. Dbg("cancel install package: [%s]", (const char*)strPack);
  982. if (!IsPackCancelled(strPack))
  983. m_CancelledPacks.push_back(strPack);
  984. }
  985. }
  986. }
  987. // 找出优先级高的,或者立刻执行的,或者触发时间早的升级包
  988. CUpgradeMgrFSM::CPendingPackInfo* CUpgradeMgrFSM::GetPriorInstallPack()
  989. {
  990. if (m_PendingPacks.size() ==0)
  991. return NULL;
  992. auto pFindPack = &(m_PendingPacks.begin()->second);
  993. for(auto it=++m_PendingPacks.begin(); it!=m_PendingPacks.end(); it++)
  994. {
  995. auto &curPack = it->second;
  996. if ((curPack.nLevel > pFindPack->nLevel)
  997. || (curPack.nLevel == pFindPack->nLevel && pFindPack->cTriggerType != 'I'
  998. && (curPack.cTriggerType == 'I' || curPack.dwTriggerTimer < pFindPack->dwTriggerTimer) ))
  999. {
  1000. pFindPack = &curPack;
  1001. continue;
  1002. }
  1003. }
  1004. return pFindPack;
  1005. }
  1006. void CUpgradeMgrFSM::s3_on_entry()
  1007. {
  1008. if (m_dwDownloadTime == 0)
  1009. {
  1010. Dbg("reset begin download time");
  1011. m_dwDownloadTime = CSmallDateTime::GetNow();
  1012. SetRunConfigHexIntValue("Install", "DownloadTime", m_dwDownloadTime);
  1013. }
  1014. // 启动定时器检查文件是否下载
  1015. ScheduleTimer(1, 30000);
  1016. }
  1017. void CUpgradeMgrFSM::s3_on_exit()
  1018. {
  1019. }
  1020. unsigned int CUpgradeMgrFSM::s3_on_event(FSMEvent* event)
  1021. {
  1022. if (event->iEvt == Event_EndDownload) // 下载完成事件
  1023. {
  1024. DownloadedEvent *pEvent = (DownloadedEvent*)event;
  1025. auto &strFileName = pEvent->strFileName;
  1026. auto it = m_PendingPacks.find(strFileName);
  1027. if (it != m_PendingPacks.end())
  1028. it->second.cPendingState = 'D'; // 修改升级状态
  1029. if (strFileName == m_strIntallingPack)
  1030. {
  1031. if (pEvent->ErrorCode == Error_Succeed)
  1032. {
  1033. // 开始安装
  1034. WriteInstallLog(m_strIntallingPack, "pack download succeed");
  1035. PostEventFIFO(new FSMEvent(Event_StartInstall));
  1036. }
  1037. else
  1038. {
  1039. // 取消安装
  1040. WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("cancel update for download error: %s(%d)", (const char*)pEvent->strErrorMsg, pEvent->ErrorCode));
  1041. // 上报安装失败
  1042. CSystemStaticInfo ssInfo;
  1043. m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
  1044. CSimpleStringA strErrMsg = "下载失败,";
  1045. strErrMsg += pEvent->strErrorMsg;
  1046. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_Unexpect, 'C', strErrMsg));
  1047. m_PendingPacks.erase(m_strIntallingPack);
  1048. m_strIntallingPack.Clear();
  1049. Dbg("send Event_StartPoll 4");
  1050. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1051. }
  1052. }
  1053. }
  1054. else if (event->iEvt == EVT_TIMER)
  1055. {
  1056. // 定时检测下载是否完成
  1057. // QueryDownloadState 接口返回状态值:1:正在下载 2:排队中 3:下载完成 4:无下载任务并且没有文件
  1058. if(IsPackDownloaded(m_strIntallingPack))
  1059. {
  1060. auto &pack = m_PendingPacks[m_strIntallingPack];
  1061. if (pack.cPendingState == 'U')
  1062. {
  1063. Dbg("finish downloading pack [%s] detected", (const char*)m_strIntallingPack);
  1064. pack.cPendingState = 'D';
  1065. }
  1066. // 开始安装
  1067. PostEventFIFO(new FSMEvent(Event_StartInstall));
  1068. }
  1069. else if (m_dwDownloadTime >0 && ((DWORD)CSmallDateTime::GetNow()) - m_dwDownloadTime >= 3600*24*14) // 14天后超时
  1070. {
  1071. // 等待下载超时后主动取消
  1072. WriteInstallLog(m_strIntallingPack, "cancel update for wait download timeout");
  1073. Dbg("cancel update pack [%s] for wait download timeout", (const char*)m_strIntallingPack);
  1074. // 上报安装失败
  1075. CSystemStaticInfo ssInfo;
  1076. m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
  1077. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_TimeOut, 'C', "下载超时"));
  1078. m_PendingPacks.erase(m_strIntallingPack);
  1079. m_strIntallingPack.Clear();
  1080. Dbg("send Event_StartPoll 5");
  1081. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1082. }
  1083. else if (CheckIfInstallCancelled())
  1084. {
  1085. // 远程或本地手工取消安装
  1086. Dbg("send Event_StartPoll 6");
  1087. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1088. }
  1089. else
  1090. {
  1091. // 查询后台升级策略是否过期或失效
  1092. assert(m_PendingPacks.find(m_strIntallingPack) != m_PendingPacks.end());
  1093. auto &pack = m_PendingPacks[m_strIntallingPack];
  1094. if (!pack.strPackExecID.IsNullOrEmpty())
  1095. {
  1096. Dbg("query upgrade exec info, id:%s", (const char*)pack.strPackExecID);
  1097. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::QueryExecInfoEvent(pack.strPackExecID));
  1098. }
  1099. // 继续监视
  1100. Dbg("pack [%s] not downloaded", (const char*)m_strIntallingPack);
  1101. ScheduleTimer(1, 30000);
  1102. }
  1103. }
  1104. else if (event->iEvt == Event_EndQueryExec)
  1105. {
  1106. // 查询策略是否过期
  1107. auto pEvent = (RvcCommRetEvent*)event;
  1108. assert(pEvent->GetArrayNum() == 1);
  1109. auto pRet = (UpgradeConnect::QueryExec_Ret *)pEvent->GetRetData();
  1110. assert(pRet != NULL);
  1111. auto strExecID = pRet->PackExecID;
  1112. auto &info = m_PendingPacks[m_strIntallingPack];
  1113. if (info.strPackExecID == strExecID)
  1114. {
  1115. if (pRet->HasCancelled)
  1116. {
  1117. Dbg("exec [%s] of pack [%s] cancalled", (const char*)strExecID, (const char*)m_strIntallingPack);
  1118. // 等待下载超时后主动取消
  1119. WriteInstallLog(m_strIntallingPack, "cancel update for exec expired");
  1120. // 上报安装失败
  1121. CSystemStaticInfo ssInfo;
  1122. m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
  1123. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_Cancel, 'C', "升级策略已过期"));
  1124. // 通知下载实体取消下载
  1125. Dbg("cancel download pack [%s]", (const char*)m_strIntallingPack);
  1126. ((CUpgradeMgrEntity*)m_pEntity)->CancelDownloadPack(m_strIntallingPack);
  1127. m_PendingPacks.erase(m_strIntallingPack);
  1128. m_strIntallingPack.Clear();
  1129. Dbg("send Event_StartPoll 7");
  1130. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1131. }
  1132. else
  1133. {
  1134. Dbg("exec [%s] of pack [%s] is valid", (const char*)strExecID, (const char*)m_strIntallingPack);
  1135. }
  1136. }
  1137. else
  1138. {
  1139. Dbg("return exec [%s] is not installing pack [%s]", (const char*)strExecID, (const char*)m_strIntallingPack);
  1140. }
  1141. }
  1142. else if (event->iEvt == Event_CancelUpgrade)
  1143. {
  1144. // 紧急取消安装
  1145. RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
  1146. CancelUpgradePacks(pEvent);
  1147. }
  1148. return 0;
  1149. }
  1150. void CUpgradeMgrFSM::s4_on_entry()
  1151. {
  1152. // 检查安装是否紧急取消
  1153. if (!CheckIfInstallCancelled())
  1154. {
  1155. //add by zl 20190221,停止广告、提示音播放
  1156. ((CUpgradeMgrEntity*)m_pEntity)->StopMediaPlay();
  1157. Dbg("stop media play");
  1158. // 终端置为升级状态。1:升级状态,0:非升级状态。处于升级状态时调用播放接口不生效
  1159. auto pFunc = m_pEntity->GetFunction();
  1160. assert(pFunc != NULL);
  1161. auto rc = pFunc->SetSysVar("UpdateState", "1", false);
  1162. assert(rc == Error_Succeed);
  1163. // 通知升级实体开始安装
  1164. Dbg("push pack [%s] to UpgradeRun entity", (const char*)m_strIntallingPack);
  1165. rc = ((CUpgradeMgrEntity*) m_pEntity)->PushUpdateTask(m_strIntallingPack, m_nIntallingPackType);
  1166. if (rc != Error_Succeed)
  1167. {
  1168. ScheduleTimer(1, 5000);
  1169. }
  1170. }
  1171. else
  1172. {
  1173. Dbg("send Event_StartPoll 8");
  1174. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1175. }
  1176. }
  1177. void CUpgradeMgrFSM::s4_on_exit()
  1178. {
  1179. }
  1180. bool CUpgradeMgrFSM::WriteInstallLog(const char *pszPackName, const char *pszLogText)
  1181. {
  1182. CSimpleStringA strPath;
  1183. m_pEntity->GetFunction()->GetPath("RunInfo", strPath);
  1184. // 创建安装包日志文件
  1185. CSimpleStringA strInstallLogDir = CSimpleStringA::Format("%s\\InstallLog", (const char*)strPath);
  1186. if (!ExistsDirA(strInstallLogDir))
  1187. CreateDirectoryA(strInstallLogDir, NULL);
  1188. // 写安装包日志
  1189. CSimpleStringA strInstallLog = CSimpleStringA::Format("%s\\%s.log", (const char*)strInstallLogDir, pszPackName);
  1190. FILE *pInstallLog = fopen(strInstallLog, "a+");
  1191. if (pInstallLog == NULL)
  1192. {
  1193. LogError(Severity_Low, Error_Unexpect, 0, CSimpleStringA::Format("open install log file [%s] to write fail", pszPackName));
  1194. return false;
  1195. }
  1196. fprintf_s(pInstallLog, "[%s] %s\r\n", (const char*)CSmallDateTime::GetNow().ToTimeString(), pszLogText);
  1197. fflush(pInstallLog);
  1198. fclose(pInstallLog);
  1199. return true;
  1200. }
  1201. bool CUpgradeMgrFSM::RemoveInstallLog(const char *pszPackName)
  1202. {
  1203. return RemoveFileA(pszPackName);
  1204. }
  1205. unsigned int CUpgradeMgrFSM::s4_on_event(FSMEvent* event)
  1206. {
  1207. if (event->iEvt == EVT_TIMER)
  1208. {
  1209. if (event->param1 == 1)
  1210. {
  1211. // 通知升级实体开始安装
  1212. Dbg("retry push pack [%s] to UpgradeRun entity", (const char*)m_strIntallingPack);
  1213. auto rc = ((CUpgradeMgrEntity*)m_pEntity)->PushUpdateTask(m_strIntallingPack, m_nIntallingPackType);
  1214. if (rc != Error_Succeed)
  1215. {
  1216. ScheduleTimer(1, 5000);
  1217. }
  1218. }
  1219. else if (event->param1 == 2)
  1220. {
  1221. if (CheckIfCanSwitchNow())
  1222. {
  1223. PostEventFIFO(new FSMEvent(Event_PreRestart));
  1224. // 系统重启事件
  1225. LogEvent(Severity_Middle, Event_Req_OS_Restart, "install sys pack ok, request to restart OS");
  1226. // 强制刷新日志
  1227. m_pEntity->GetFunction()->FlushLogFile();
  1228. }
  1229. else
  1230. {
  1231. ScheduleTimer(2, 5000);
  1232. }
  1233. }
  1234. }
  1235. else if (event->iEvt == Event_InstallCheck)
  1236. {
  1237. // 检查执行逻辑
  1238. UpgradeRunCheckEvent *pEvent = (UpgradeRunCheckEvent*) event;
  1239. Dbg("receive pack [%s] upgrade check event, result: [%d], comment:[%s]", (const char*)pEvent->strPackName, pEvent->nErrorCode, (const char*)pEvent->strComment);
  1240. if (pEvent->strPackName == m_strIntallingPack)
  1241. {
  1242. // 上报检查结果
  1243. CSystemStaticInfo ssInfo;
  1244. m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
  1245. char cState = pEvent->nErrorCode == Error_Succeed ? 'I' : 'C';
  1246. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, pEvent->nErrorCode, cState, pEvent->strComment));
  1247. // 检查结果写入安装日志
  1248. WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("Upgrade check finished, result: [%d], comment:%s", pEvent->nErrorCode, (const char*)pEvent->strComment));
  1249. if (pEvent->nErrorCode == Error_Succeed)
  1250. {
  1251. // 开始执行
  1252. Dbg("pack [%s] upgrade check pass, install now", (const char*)m_strIntallingPack);
  1253. }
  1254. else
  1255. {
  1256. // 执行取消
  1257. Dbg("pack [%s] upgraded cancelled, check not pass, %s", (const char*)m_strIntallingPack, (const char*)pEvent->strComment);
  1258. m_InstallFailPacks.push_back(m_strIntallingPack);
  1259. //安装错误写入运行时配置
  1260. UpdateInstallFailedPackInfo(m_strIntallingPack);
  1261. // 从待安装列表中删除
  1262. m_PendingPacks.erase(m_strIntallingPack);
  1263. //RemoveInstallLog(m_strIntallingPack);//删除安装或检查失败的包日志文件
  1264. m_strIntallingPack.Clear();
  1265. m_strNewVersion = "";
  1266. Dbg("send Event_StartPoll 9");
  1267. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1268. }
  1269. }
  1270. }
  1271. else if (event->iEvt == Event_EndInstall)
  1272. {
  1273. UpgradeRunDoneEvent *pEvent = (UpgradeRunDoneEvent*)event;
  1274. 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);
  1275. assert(pEvent->strPackName == m_strIntallingPack);
  1276. if (pEvent->strPackName == m_strIntallingPack)
  1277. {
  1278. // 上报安装结果
  1279. CSystemStaticInfo ssInfo;
  1280. m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
  1281. bool bUpgradeSuc = pEvent->nErrorCode == Error_Succeed;
  1282. char cState = 'N';
  1283. if (pEvent->strComment.IndexOf("手动升级") >= 0)
  1284. {
  1285. //同一个手动升级包第一次自动执行
  1286. cState = 'I';
  1287. }
  1288. else
  1289. {
  1290. cState = bUpgradeSuc ? ((pEvent->bLightPack || pEvent->bSysInstall) ? 'A' : 'W') : 'C';
  1291. }
  1292. // 安装结果写入安装日志
  1293. WriteInstallLog(m_strIntallingPack, CSimpleStringA::Format("Upgrade install finished, result: [%d], comment: %s", pEvent->nErrorCode, (const char*)pEvent->strComment));
  1294. CVersion reportVer;
  1295. if (pEvent->bLightPack || pEvent->bSysInstall) // 轻量安装和系统外安装都不改变框架版本
  1296. reportVer = ssInfo.InstallVersion;
  1297. else
  1298. reportVer = ssInfo.LatterInstallVersion;
  1299. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, reportVer, pEvent->nErrorCode, cState, pEvent->strComment));
  1300. // xkm@20161114: 系统定制升级包需上报系统定制版本
  1301. // 系统补丁包和固件升级包也需上报
  1302. if (pEvent->bSysInstall
  1303. && (!pEvent->strNewVersion.IsNullOrEmpty() || !pEvent->strFWID.IsNullOrEmpty() || !pEvent->strSysPatchName.IsNullOrEmpty())
  1304. && bUpgradeSuc)
  1305. {
  1306. Dbg("report system custom version:[%s], FWID:[%s], SysPatchName:[%s]", (const char*)pEvent->strNewVersion, (const char*)pEvent->strFWID, (const char*)pEvent->strSysPatchName);
  1307. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportSysCustomVerEvent(m_strIntallingPack, pEvent->strNewVersion, pEvent->strFWID, pEvent->strSysPatchName));
  1308. }
  1309. if (bUpgradeSuc)
  1310. {
  1311. if (pEvent->bLightPack || pEvent->bSysInstall)
  1312. {
  1313. // 轻量包或系统外升级
  1314. if (pEvent->bLightPack)
  1315. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1316. else
  1317. {
  1318. //区分第一次升级和第二次升级;第一次添加到m_ManualPendingPacks,第二次从m_ManualPendingPacks删除
  1319. //如果是手动升级,则添加到手动升级列表
  1320. if (pEvent->strComment.IndexOf("手动升级") >= 0)
  1321. {
  1322. //同一个手动升级包第一次自动执行
  1323. CSimpleStringA temp = m_strIntallingPack;
  1324. bool bExist= find_if(
  1325. m_ManualPendingPacks.begin(),
  1326. m_ManualPendingPacks.end(),
  1327. [temp](CSimpleStringA &str) { return str.Compare(temp) == 0; }) != m_ManualPendingPacks.end();
  1328. if (!bExist)
  1329. {
  1330. m_ManualPendingPacks.push_back(m_strIntallingPack);
  1331. Dbg("pushback[%s] to m_ManualPendingPacks", m_strIntallingPack);
  1332. }
  1333. Dbg("send Event_StartPoll 10");
  1334. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1335. }
  1336. else
  1337. {
  1338. //系统外升级或同一个手动升级包第二次由用户桌面手动执行
  1339. CSimpleStringA temp = m_strIntallingPack;
  1340. bool bManual= find_if(
  1341. m_ManualPendingPacks.begin(),
  1342. m_ManualPendingPacks.end(),
  1343. [temp](CSimpleStringA &str) { return str.Compare(temp) == 0; }) != m_ManualPendingPacks.end();
  1344. if (bManual)
  1345. {
  1346. //检查安装结果,并上报
  1347. IsManualInstallOk(m_strIntallingPack);
  1348. Dbg("[%s]手动升级完成,从待手动升级列表删除", m_strIntallingPack);
  1349. m_ManualPendingPacks.remove(m_strIntallingPack);
  1350. //更新运行时手动升级任务
  1351. if (!m_ManualPendingPacks.empty())
  1352. {
  1353. CSimpleStringA strManualPacks;
  1354. list<CSimpleStringA>::iterator itor;
  1355. itor = m_ManualPendingPacks.begin();
  1356. while(itor!=m_ManualPendingPacks.end())
  1357. {
  1358. strManualPacks += *itor++;
  1359. if (itor != m_ManualPendingPacks.end())
  1360. {
  1361. strManualPacks += ",";
  1362. }
  1363. }
  1364. SetRunConfigStrValue("Install", "ManualPackList", strManualPacks);
  1365. }
  1366. else
  1367. {
  1368. SetRunConfigStrValue("Install", "ManualPackList", "");
  1369. }
  1370. }
  1371. else
  1372. {
  1373. Dbg("未在待手动升级列表中找到[%s]", m_strIntallingPack);
  1374. }
  1375. // xkm@20161128: 为保证定制上报成功,通过定时器重启
  1376. ScheduleTimer(2, 5000);
  1377. }
  1378. }
  1379. m_PendingPacks.erase(m_strIntallingPack);
  1380. m_strIntallingPack.Clear();
  1381. m_strNewVersion = "";
  1382. }
  1383. else
  1384. {
  1385. auto &pack = m_PendingPacks[m_strIntallingPack];
  1386. pack.bSysInstall = pEvent->bSysInstall;
  1387. SetRunConfigStrValue("Install", "bSysInstall", pack.bSysInstall ? "1" : "0");
  1388. SetRunConfigStrValue("Install", "NewVersion", pEvent->strNewVersion);
  1389. m_strNewVersion = pEvent->strNewVersion;
  1390. PostEventFIFO(new FSMEvent(Event_WaitConfirm));
  1391. }
  1392. }
  1393. else
  1394. {
  1395. // 安装失败
  1396. m_InstallFailPacks.push_back(m_strIntallingPack);
  1397. //RemoveInstallLog(m_strIntallingPack);//删除安装或检查失败的包日志文件
  1398. //安装错误写入运行时配置
  1399. UpdateInstallFailedPackInfo(m_strIntallingPack);
  1400. // 从待安装列表中删除
  1401. m_PendingPacks.erase(m_strIntallingPack);
  1402. m_strIntallingPack.Clear();
  1403. m_strNewVersion = "";
  1404. Dbg("send Event_StartPoll 11");
  1405. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1406. }
  1407. }
  1408. }
  1409. else if (event->iEvt == Event_CancelUpgrade)
  1410. {
  1411. // 紧急取消安装
  1412. RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
  1413. CancelUpgradePacks(pEvent);
  1414. }
  1415. return 0;
  1416. }
  1417. bool CUpgradeMgrFSM::CheckIfInstallCancelled()
  1418. {
  1419. if (IsPackCancelled(m_strIntallingPack))
  1420. {
  1421. // 取消上报
  1422. CSystemStaticInfo ssInfo;
  1423. m_pEntity->GetFunction()->GetSystemStaticInfo(ssInfo);
  1424. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.InstallVersion, Error_Cancel, 'C', "取消安装"));
  1425. Dbg("cancel upgrade pack [%s] now", (const char*)m_strIntallingPack);
  1426. WriteInstallLog(m_strIntallingPack, "cancel update pack");
  1427. if (!m_strNewVersion.IsNullOrEmpty())
  1428. {
  1429. Dbg("delete temp new version dir");
  1430. CSimpleStringA strRootVerPath;
  1431. m_pEntity->GetFunction()->GetPath("RootVer", strRootVerPath);
  1432. CSimpleStringA strNewVerPath = CSimpleStringA::Format("%s\\%s", (const char*)strRootVerPath, (const char*)m_strNewVersion);
  1433. if (ExistsDirA(strNewVerPath))
  1434. {
  1435. RemoveDirRecursiveA(strNewVerPath);
  1436. }
  1437. }
  1438. // 从待安装列表删除
  1439. m_PendingPacks.erase(m_strIntallingPack);
  1440. m_strIntallingPack.Clear();
  1441. m_strNewVersion = "";
  1442. return true;
  1443. }
  1444. return false;
  1445. }
  1446. ErrorCodeEnum CUpgradeMgrFSM::SwitchUpgradeNow()
  1447. {
  1448. Dbg("switch to framework new version now");
  1449. assert(m_PendingPacks.find(m_strIntallingPack) != m_PendingPacks.end());
  1450. auto &pack = m_PendingPacks[m_strIntallingPack];
  1451. // 上报状态
  1452. CSystemStaticInfo ssInfo;
  1453. auto pFunc = m_pEntity->GetFunction();
  1454. pFunc->GetSystemStaticInfo(ssInfo);
  1455. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportStateEvent(m_strIntallingPack, ssInfo.LatterInstallVersion, 0, 'S', "已设为启动版本"));
  1456. // 立刻切换
  1457. auto pPrivFunc = pFunc->GetPrivilegeFunction();
  1458. auto rc = pPrivFunc->UpgradeToNewVersion(pack.bSysInstall);
  1459. if (rc == Error_Succeed)
  1460. {
  1461. PostEventFIFO(new FSMEvent(Event_PreRestart));
  1462. //add by zl 20190827 升级完成清空运行时配置,防止事件延迟导致状态机没有收到重庆事件就已重启框架
  1463. // 完成升级,清空运行时配置
  1464. Dbg("clear upgrade info from runcfg");
  1465. SetRunConfigStrValue("Install", "Pack", "");
  1466. SetRunConfigStrValue("Install", "TriggerType", "");
  1467. SetRunConfigHexIntValue("Install", "TriggerTime", 0);
  1468. SetRunConfigStrValue("Install", "PendingState", "");
  1469. SetRunConfigStrValue("Install", "bSysInstall", "");
  1470. SetRunConfigStrValue("Install", "PackExecID", "");
  1471. SetRunConfigStrValue("Install", "NewVersion", "");
  1472. SetRunConfigHexIntValue("Install", "DownloadTime", 0);
  1473. // 发送广播(通知业务界面清除状态)
  1474. UpgradeStateEvent stateEvent;
  1475. stateEvent.strPackName = "";
  1476. stateEvent.strExecID = "";
  1477. stateEvent.cInstallState = 'N';
  1478. stateEvent.bSysInstall = false;
  1479. stateEvent.bLightPack = false;
  1480. stateEvent.strNewVersion = "";
  1481. SpSendBroadcast(m_pEntity->GetFunction(), eMsg_UpgradeStateEvent, eMsgSig_UpgradeStateEvent, stateEvent);
  1482. if (!pack.bSysInstall)
  1483. {
  1484. // 框架重启事件
  1485. LogEvent(Severity_Middle, Event_Req_Framework_Restart,
  1486. CSimpleStringA::Format("upgrade to new version [%s] succeed, request to restart framework",
  1487. ssInfo.LatterInstallVersion.ToString()));
  1488. //pPrivFunc->Reboot(RebootTrigger_FrameUpgrade, RebootWay_Framework);
  1489. }
  1490. else
  1491. {
  1492. // 系统重启事件
  1493. LogEvent(Severity_Middle, Event_Req_OS_Restart,
  1494. CSimpleStringA::Format("upgrade to new version [%s] succeed, request to restart OS",
  1495. ssInfo.LatterInstallVersion.ToString()));
  1496. // pPrivFunc->PushSysInstallPack(m_strInstallPack);
  1497. }
  1498. // 强制刷新日志
  1499. m_pEntity->GetFunction()->FlushLogFile();
  1500. }
  1501. else
  1502. {
  1503. // 升级失败
  1504. Dbg("send Event_StartPoll 12");
  1505. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1506. }
  1507. return rc;
  1508. }
  1509. bool CUpgradeMgrFSM::CheckIfPackConfirmed()
  1510. {
  1511. // 所有升级包切换版本前都需要到后台查询当前执行策略状态,减少事后回滚机会
  1512. // xkm@20140827: 对于远程或本地手工升级,不需要后台确认,直接跳过
  1513. assert(m_PendingPacks.find(m_strIntallingPack) != m_PendingPacks.end());
  1514. auto &pack = m_PendingPacks[m_strIntallingPack];
  1515. //char cTriggerType = pack.cTriggerType;
  1516. //if (cTriggerType == 'I' ||
  1517. // (cTriggerType == 'T' && pack.dwTriggerTimer <= (DWORD)CSmallDateTime::GetNow()))
  1518. //{
  1519. // return true;
  1520. //}
  1521. //else if (cTriggerType == 'C' && pack.dwTriggerTimer <= (DWORD)CSmallDateTime::GetNow())
  1522. //{
  1523. // // 请求确认
  1524. // m_ReportFSM.PostEventFIFO(new CReportMgrFSM::SwitchConfirmEvent(pack.strPackExecID));
  1525. // return false;
  1526. //}
  1527. //else
  1528. //{
  1529. // // 启动切换检查定时器
  1530. // ScheduleTimer(1, 30000);
  1531. // return false;
  1532. //}
  1533. if (pack.strPackExecID == "" || pack.bSysInstall)
  1534. {
  1535. Dbg("pack %s no need to confirm", (const char*)pack.strPackName);
  1536. return true;
  1537. }
  1538. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::SwitchConfirmEvent(pack.strPackExecID));
  1539. return false;
  1540. }
  1541. void CUpgradeMgrFSM::s5_on_entry()
  1542. {
  1543. // 检查安装是否紧急取消
  1544. if (!CheckIfInstallCancelled())
  1545. {
  1546. if (CheckIfPackConfirmed())
  1547. {
  1548. PostEventFIFO(new FSMEvent(Event_StartSwitch));
  1549. }
  1550. }
  1551. else
  1552. {
  1553. Dbg("send Event_StartPoll 13");
  1554. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1555. }
  1556. }
  1557. void CUpgradeMgrFSM::s5_on_exit()
  1558. {
  1559. }
  1560. unsigned int CUpgradeMgrFSM::s5_on_event(FSMEvent* event)
  1561. {
  1562. if (event->iEvt == EVT_TIMER)
  1563. {
  1564. if (CheckIfInstallCancelled())
  1565. {
  1566. Dbg("send Event_StartPoll 14");
  1567. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1568. }
  1569. else if (CheckIfPackConfirmed())
  1570. {
  1571. PostEventFIFO(new FSMEvent(Event_StartSwitch));
  1572. }
  1573. }
  1574. else if (event->iEvt == Event_EndConfirm)
  1575. {
  1576. auto pEvent = (RvcCommRetEvent*)event;
  1577. assert(pEvent->GetArrayNum() == 1);
  1578. auto pRet = (UpgradeConnect::Confirm_Ret *)pEvent->GetRetData();
  1579. assert(pRet != NULL);
  1580. auto strExecID = pRet->PackExecID;
  1581. auto &info = m_PendingPacks[m_strIntallingPack];
  1582. if (info.strPackExecID == strExecID)
  1583. {
  1584. if(pRet->HasConfirmed)
  1585. {
  1586. Dbg("pack [%s] confirmed, switch version now", (const char*)m_strIntallingPack);
  1587. PostEventFIFO(new FSMEvent(Event_StartSwitch));
  1588. }
  1589. else if (pRet->HasCancelled)
  1590. {
  1591. Dbg("pack [%s] cancalled", (const char*)m_strIntallingPack);
  1592. if (!IsPackCancelled(m_strIntallingPack))
  1593. m_CancelledPacks.push_back(m_strIntallingPack);
  1594. ScheduleTimer(1, 1000);
  1595. }
  1596. else
  1597. {
  1598. //if (info.cTriggerType == 'C')
  1599. Dbg("pack [%s] not confirmed, exec id: [%s]", (const char*)m_strIntallingPack, (const char*)strExecID);
  1600. ScheduleTimer(1, 30000);
  1601. }
  1602. }
  1603. else
  1604. {
  1605. Dbg("recv confirm of not installing pack: %s", (const char*)strExecID);
  1606. }
  1607. }
  1608. else if (event->iEvt == Event_CancelUpgrade)
  1609. {
  1610. // 紧急取消安装
  1611. RvcCommRetEvent *pEvent = (RvcCommRetEvent*)event;
  1612. CancelUpgradePacks(pEvent);
  1613. Dbg("send Event_StartPoll 15");
  1614. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1615. }
  1616. return 0;
  1617. }
  1618. bool CUpgradeMgrFSM::CheckIfCanSwitchNow()
  1619. {
  1620. auto pFunc = m_pEntity->GetFunction();
  1621. assert(pFunc != NULL);
  1622. CSimpleStringA strCustomerHandle;
  1623. CSimpleStringA strRunState;
  1624. CSimpleStringA strCallState;
  1625. // 无交互N、客户关注A、客户在控制终端C、柜员协助T、柜员代操作G
  1626. auto rc = pFunc->GetSysVar("CustomerHandle", strCustomerHandle);
  1627. assert(rc == Error_Succeed);
  1628. //rc = pFunc->GetSysVar("RunState", strRunState);
  1629. //assert(rc == Error_Succeed);
  1630. // 离线O、尝试连线柜员C、免提通话H、连线失败F、断线B
  1631. rc = pFunc->GetSysVar("CallState", strCallState);
  1632. assert(rc == Error_Succeed);
  1633. SYSTEMTIME st = {};
  1634. GetLocalTime(&st);
  1635. bool bOffworkTime = st.wHour < 8 || st.wHour >= 18;
  1636. Dbg("strCustomerHandle=%s, strCallState=%s, bOffworkTime=%d",strCustomerHandle,strCallState,bOffworkTime);
  1637. // 或者下班时间且非连线状态 @20151202
  1638. // 客户关注状态A,不允许切换 by zl 20191113
  1639. /*return strCustomerHandle[0] == 'N' || strCustomerHandle[0] == 'A'
  1640. || (bOffworkTime && strCallState[0] == 'O');*/
  1641. return strCustomerHandle[0] == 'N' || (bOffworkTime && strCallState[0] == 'O');
  1642. }
  1643. void CUpgradeMgrFSM::s6_on_entry()
  1644. {
  1645. if (CheckIfInstallCancelled())
  1646. {
  1647. Dbg("send Event_StartPoll 16");
  1648. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1649. }
  1650. else if (CheckIfCanSwitchNow())
  1651. {
  1652. // 检查当前是否可以切换(无客户)
  1653. SwitchUpgradeNow();
  1654. }
  1655. else
  1656. {
  1657. Dbg("not applicable time to switch, retry in 10's");
  1658. ScheduleTimer(1, 10000);
  1659. }
  1660. }
  1661. void CUpgradeMgrFSM::s6_on_exit()
  1662. {
  1663. m_strIntallingPack.Clear();
  1664. m_strNewVersion = "";
  1665. }
  1666. unsigned int CUpgradeMgrFSM::s6_on_event(FSMEvent* event)
  1667. {
  1668. if (event->iEvt == EVT_TIMER)
  1669. {
  1670. if (CheckIfInstallCancelled())
  1671. {
  1672. Dbg("send Event_StartPoll 17");
  1673. PostEventFIFO(new FSMEvent(Event_StartPoll));
  1674. }
  1675. else if (CheckIfCanSwitchNow())
  1676. {
  1677. // 检查当前是否可以切换(无客户)
  1678. SwitchUpgradeNow();
  1679. }
  1680. else
  1681. {
  1682. Dbg("not applicable time to switch, retry in 10's");
  1683. ScheduleTimer(1, 10000);
  1684. }
  1685. }
  1686. else if (event->iEvt == Event_SwitchNow)
  1687. {
  1688. // 客户经理主动切换版本
  1689. SwitchUpgradeNow();
  1690. }
  1691. return 0;
  1692. }
  1693. ErrorCodeEnum CUpgradeMgrFSM::SetRunConfigStrValue(const char *pszSection, const char *pszKey, const char *pszValue)
  1694. {
  1695. auto pFunc = m_pEntity->GetFunction();
  1696. CSmartPointer<IConfigInfo> pConfig;
  1697. auto rc = pFunc->OpenConfig(Config_Run, pConfig);
  1698. if (rc != Error_Succeed)
  1699. return rc;
  1700. return pConfig->WriteConfigValue(pszSection, pszKey, pszValue);
  1701. }
  1702. ErrorCodeEnum CUpgradeMgrFSM::GetRunConfigStrValue(const char *pszSection, const char *pszKey, CSimpleStringA &strValue)
  1703. {
  1704. auto pFunc = m_pEntity->GetFunction();
  1705. CSmartPointer<IConfigInfo> pConfig;
  1706. auto rc = pFunc->OpenConfig(Config_Run, pConfig);
  1707. if (rc != Error_Succeed)
  1708. return rc;
  1709. return pConfig->ReadConfigValue(pszSection, pszKey, strValue);
  1710. }
  1711. ErrorCodeEnum CUpgradeMgrFSM::SetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 nValue)
  1712. {
  1713. auto pFunc = m_pEntity->GetFunction();
  1714. CSmartPointer<IConfigInfo> pConfig;
  1715. auto rc = pFunc->OpenConfig(Config_Run, pConfig);
  1716. if (rc != Error_Succeed)
  1717. return rc;
  1718. return pConfig->WriteConfigValueHexInt(pszSection, pszKey, nValue);
  1719. }
  1720. ErrorCodeEnum CUpgradeMgrFSM::GetRunConfigHexIntValue(const char *pszSection, const char *pszKey, UINT64 &nValue)
  1721. {
  1722. auto pFunc = m_pEntity->GetFunction();
  1723. CSmartPointer<IConfigInfo> pConfig;
  1724. auto rc = pFunc->OpenConfig(Config_Run, pConfig);
  1725. if (rc != Error_Succeed)
  1726. return rc;
  1727. return pConfig->ReadConfigValueHexInt(pszSection, pszKey, nValue);
  1728. }
  1729. ErrorCodeEnum CUpgradeMgrFSM::GetUpgradeState(bool &bInstalling, CSimpleStringA &strPackFile, CSimpleStringA &strExecID,
  1730. char &cInstallState, bool &bSysInstall, bool &bLightPack, CSimpleStringA &strNewVersion)
  1731. {
  1732. bInstalling = !m_strIntallingPack.IsNullOrEmpty();
  1733. if (bInstalling)
  1734. {
  1735. strPackFile = m_strIntallingPack;
  1736. auto &pack = m_PendingPacks[m_strIntallingPack];
  1737. strExecID = pack.strPackExecID;
  1738. cInstallState = GetInstallState(m_iState);
  1739. bSysInstall = pack.bSysInstall;
  1740. bLightPack = false;
  1741. strNewVersion = m_strNewVersion;
  1742. }
  1743. return Error_Succeed;
  1744. }
  1745. //获取手动升级任务安装结果
  1746. bool CUpgradeMgrFSM::UpdateManualTaskState()
  1747. {
  1748. if (!m_ManualPendingPacks.empty())
  1749. {
  1750. list<CSimpleStringA>::iterator itor;
  1751. itor = m_ManualPendingPacks.begin();
  1752. while(itor!=m_ManualPendingPacks.end())
  1753. {
  1754. CSimpleStringA strPackName = *itor;
  1755. if (IsManualInstallOk(*itor))
  1756. {
  1757. Dbg("[%s]手动升级完成,从待手动升级列表删除", strPackName);
  1758. m_ManualPendingPacks.remove(strPackName);
  1759. }
  1760. itor++;
  1761. }
  1762. //更新运行时手动升级任务
  1763. if (!m_ManualPendingPacks.empty())
  1764. {
  1765. CSimpleStringA strManualPacks;
  1766. list<CSimpleStringA>::iterator itor;
  1767. itor = m_ManualPendingPacks.begin();
  1768. while(itor!=m_ManualPendingPacks.end())
  1769. {
  1770. strManualPacks += *itor++;
  1771. if (itor != m_ManualPendingPacks.end())
  1772. {
  1773. strManualPacks += ",";
  1774. }
  1775. }
  1776. SetRunConfigStrValue("Install", "ManualPackList", strManualPacks);
  1777. }
  1778. else
  1779. {
  1780. SetRunConfigStrValue("Install", "ManualPackList", "");
  1781. }
  1782. }
  1783. return true;
  1784. }
  1785. //手动升级包是否安装完成
  1786. bool CUpgradeMgrFSM::IsManualInstallOk(CSimpleStringA strPackName)
  1787. {
  1788. CSimpleStringA strErrMsg;
  1789. CSimpleStringA strVersion;
  1790. CSimpleStringA strPatchName = GetSysPatchNameFromPackName(strPackName);
  1791. if (strPackName.IsNullOrEmpty())
  1792. {
  1793. return false;
  1794. }
  1795. if (strPackName.IsStartWith("SP"))
  1796. {
  1797. //系统补丁
  1798. if (!GetServerPackUpgradeResult(strPackName, strVersion, strErrMsg))
  1799. {
  1800. Dbg("get [%s] ServerPack upgrade result fail, %s", (const char*)strPackName, (const char*)strErrMsg);
  1801. strErrMsg = "";
  1802. return false;
  1803. }
  1804. else
  1805. {
  1806. // 系统补丁包和固件升级包也需上报
  1807. Dbg("report SysPatchName:[%s]", strPackName);
  1808. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportSysCustomVerEvent(strPackName, "", "", strVersion));
  1809. }
  1810. }
  1811. else if (strPackName.IsStartWith("FP"))
  1812. {
  1813. //固件
  1814. if (!GetFirmwarePackUpgradeResult(strPackName, strVersion, strErrMsg))
  1815. {
  1816. Dbg("get [%s] FirmwarePack upgrade result fail, %s", (const char*)strPackName, (const char*)strErrMsg);
  1817. strErrMsg = "";
  1818. return false;
  1819. }
  1820. else
  1821. {
  1822. Dbg("report FirmwarePack:[%s]", strPackName);
  1823. m_ReportFSM.PostEventFIFO(new CReportMgrFSM::ReportSysCustomVerEvent(strPackName, "", strVersion, ""));
  1824. }
  1825. }
  1826. else if (strPackName.IsStartWith("SCP"))
  1827. {
  1828. //系统定制
  1829. return false;
  1830. }
  1831. else
  1832. {
  1833. Dbg("unknow pack name[%s]", (const char*)strPackName);
  1834. return false;
  1835. }
  1836. return true;
  1837. }
  1838. CSimpleStringA CUpgradeMgrFSM::GetSysPatchNameFromPackName(const char *pszPackName)
  1839. {
  1840. //操作系统补丁升级包名格式:SP_日期_补丁名
  1841. CSimpleStringA str = pszPackName;
  1842. CSimpleStringA strSysPatchName = "";
  1843. if (!str.IsNullOrEmpty())
  1844. {
  1845. auto arr = str.Split('_');
  1846. if (arr.GetCount() == 3)
  1847. {
  1848. strSysPatchName = arr[2];
  1849. }
  1850. else
  1851. {
  1852. Dbg("升级包名[%s]不符合规范, 格式必须是:[包类型_日期_补丁名]", str);
  1853. }
  1854. }
  1855. return strSysPatchName;
  1856. }
  1857. //获取系统补丁升级结果
  1858. bool CUpgradeMgrFSM::GetServerPackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg)
  1859. {
  1860. char szTmp[4096] = {};
  1861. int nTmpBufLen = 4096;//win7补丁可能有200多个,每个补丁(如KB3105211加分隔符;)需要10个字节
  1862. if (!QueryWMIDevice(PatchList, "HotFixID", szTmp, &nTmpBufLen))
  1863. {
  1864. Dbg("query sys patch list fail");
  1865. return false;
  1866. }
  1867. CSimpleStringA strPatchList = szTmp;
  1868. CSimpleStringA strPatchName = GetSysPatchNameFromPackName(pszPackageName);
  1869. if (strPatchName.IsNullOrEmpty())
  1870. {
  1871. return false;
  1872. }
  1873. if (strPatchList.IndexOf(strPatchName.GetData()) < 0 )
  1874. {
  1875. Dbg("can not find PatchName[%s] from PatchList[%s]", strPatchName, strPatchList);
  1876. return false;
  1877. }
  1878. Dbg("find PatchName[%s] from PatchList[%s]", strPatchName, strPatchList);
  1879. strVerison = strPatchName;
  1880. return true;
  1881. }
  1882. //获取固件升级结果
  1883. bool CUpgradeMgrFSM::GetFirmwarePackUpgradeResult(const char *pszPackageName, CSimpleStringA &strVerison, CSimpleStringA &strErrMsg)
  1884. {
  1885. // 根据PinPad实体状态确定是否连接密码键盘
  1886. bool bUpdate = false;
  1887. strVerison = "";
  1888. auto pPinPadClient = new PinPadService_ClientBase(this->GetEntityBase());
  1889. if (pPinPadClient->Connect() != Error_Succeed)
  1890. {
  1891. Dbg("connect PinPad fail, assume no pinpad");
  1892. strErrMsg = "connect PinPad fail, assume no pinpad";
  1893. }
  1894. else
  1895. {
  1896. PinPadService_GetDevInfo_Req req = {};
  1897. PinPadService_GetDevInfo_Ans ans = {};
  1898. auto rc = pPinPadClient->GetDevInfo(req, ans, 3000);
  1899. if (rc == Error_Succeed)
  1900. {
  1901. if (ans.state == DEVICE_STATUS_NORMAL)
  1902. {
  1903. Dbg("pinpad model: %s", (const char*)ans.model);
  1904. // CM = V2.0#PM = V1.0#MID = 75500001#PID = 12345678#FWID = V1234567#Vendor = nantian
  1905. // 密码键盘ID,PID,8到16字节; 设备ID,MID,8到16字节; 固件版本号,FWID,8字节
  1906. CSimpleStringA str = ans.model;
  1907. CSimpleStringA strFWID;
  1908. if (!str.IsNullOrEmpty())
  1909. {
  1910. auto arr = str.Split('#');
  1911. if (arr.GetCount() > 0)
  1912. {
  1913. for (int i = 0; i < arr.GetCount(); i++)
  1914. {
  1915. auto arr2 = arr[i].Split('=');
  1916. if (arr2.GetCount() != 2)
  1917. continue;
  1918. if(!strnicmp((LPCTSTR)arr2[0], "FWID", strlen("FWID")))
  1919. {
  1920. strFWID = arr2[1];
  1921. if (!strFWID.IsNullOrEmpty())
  1922. {
  1923. CSimpleStringA strPackName = pszPackageName;
  1924. if (strPackName.IndexOf(strFWID.GetData()) > 0)
  1925. {
  1926. Dbg("find FWID[%] from strPackName[%s]", strFWID, strPackName);
  1927. strVerison = strFWID;
  1928. bUpdate = true;
  1929. }
  1930. else
  1931. {
  1932. Dbg("can not find FWID[%] from strPackName[%s]", strFWID, strPackName);
  1933. strErrMsg = "can not find FWID from PackName";
  1934. bUpdate = false;
  1935. }
  1936. }
  1937. break;
  1938. }
  1939. }
  1940. }
  1941. }
  1942. }
  1943. else
  1944. {
  1945. Dbg("pinpad not exist, state: %d", ans.state);
  1946. strErrMsg = "pinpad not exist";
  1947. }
  1948. }
  1949. else
  1950. {
  1951. Dbg("PinPad::GetDevInfo() fail: 0x%X", rc);
  1952. strErrMsg = "PinPad::GetDevInfo() fail";
  1953. }
  1954. pPinPadClient->GetFunction()->CloseSession();
  1955. }
  1956. pPinPadClient->SafeDelete();
  1957. pPinPadClient = NULL;
  1958. return bUpdate;
  1959. }
  1960. ErrorCodeEnum CUpgradeMgrFSM::GetPackInstallFailedCnts(UINT64 &nCnts)
  1961. {
  1962. return GetRunConfigHexIntValue("Install","FailCnts", nCnts);
  1963. }
  1964. ErrorCodeEnum CUpgradeMgrFSM::SetPackInstallFailedCnts(UINT64 nCnts)
  1965. {
  1966. return SetRunConfigHexIntValue("Install", "FailCnts", nCnts);
  1967. }
  1968. ErrorCodeEnum CUpgradeMgrFSM::GetInstallFailedPackName(CSimpleStringA &strPackName)
  1969. {
  1970. return GetRunConfigStrValue("Install","FailPackName", strPackName);
  1971. }
  1972. ErrorCodeEnum CUpgradeMgrFSM::SetInstallFailedPackName(CSimpleStringA strPackName)
  1973. {
  1974. return SetRunConfigStrValue("Install", "FailPackName", strPackName);
  1975. }
  1976. ErrorCodeEnum CUpgradeMgrFSM::SetInstallFailedPackInfo(CSimpleStringA strPackName)
  1977. {
  1978. return SetRunConfigStrValue("Install", "FailedPackInfo", strPackName);
  1979. }
  1980. ErrorCodeEnum CUpgradeMgrFSM::GetInstallFailedPackInfo(CSimpleStringA &strPackName)
  1981. {
  1982. return GetRunConfigStrValue("Install", "FailedPackInfo", strPackName);
  1983. }
  1984. //错误升级包信息格式:包1名称,包1失败次数;包2名称,包2失败次数2;.... 例如:pack1,1;pack2,2;....
  1985. ErrorCodeEnum CUpgradeMgrFSM::UpdateInstallFailedPackInfo(CSimpleStringA strPackName)
  1986. {
  1987. CSimpleStringA strPackInfoList;
  1988. CSimpleStringA strNewPackInfoList;
  1989. Dbg("UpdateInstallFailedPackInfo, %s",strPackName);
  1990. if (Error_Succeed != GetInstallFailedPackInfo(strPackInfoList)
  1991. || strPackInfoList.IsNullOrEmpty())
  1992. {
  1993. strNewPackInfoList = strPackName;
  1994. strNewPackInfoList += ",";
  1995. strNewPackInfoList += "1";
  1996. strNewPackInfoList += ";";
  1997. }
  1998. else
  1999. {
  2000. Dbg("old FailedPackInfo is [%s]", strPackInfoList);
  2001. //add by zl 20191113 运行时最长支持65000,大于这个长度直接清空
  2002. if (strPackInfoList.GetLength() >= 65000)
  2003. {
  2004. strPackInfoList = "";
  2005. }
  2006. if (strPackInfoList.IndexOf(strPackName) == -1)
  2007. {
  2008. //add by zl 20191113 记录所有历史包
  2009. strNewPackInfoList = strPackInfoList;
  2010. strNewPackInfoList += strPackName;
  2011. strNewPackInfoList += ",";
  2012. strNewPackInfoList += "1";
  2013. strNewPackInfoList += ";";
  2014. }
  2015. else
  2016. {
  2017. CAutoArray<CSimpleStringA> arr = strPackInfoList.Split(';');
  2018. for(int i=0; i< arr.GetCount(); i++)
  2019. {
  2020. Dbg("arr[%d]=[%s]", i,arr[i]);
  2021. int nPos = arr[i].IndexOf(",");
  2022. CSimpleStringA strName = arr[i].SubString(0,nPos);
  2023. CSimpleStringA strConut = arr[i].SubString(nPos+1, arr[i].GetLength()-nPos-1);
  2024. Dbg("strName=%s, strConut=%s", strName,strConut);
  2025. if (0 == strName.Compare(strPackName, false))
  2026. //if (strName.IsStartWith(strPackName))
  2027. {
  2028. int nNewCount = atoi(strConut);
  2029. nNewCount++;
  2030. char buf[16] = {0};
  2031. CSimpleStringA strNew = strName;
  2032. strNew += ",";
  2033. itoa(nNewCount,buf,10);
  2034. strNew += buf;
  2035. arr[i] = strNew;
  2036. Dbg("strNew=%s", strNew);
  2037. }
  2038. strNewPackInfoList += arr[i];
  2039. strNewPackInfoList += ";";
  2040. }
  2041. }
  2042. }
  2043. Dbg("new FailedPackInfo is [%s]", strNewPackInfoList);
  2044. return SetInstallFailedPackInfo(strNewPackInfoList);
  2045. }