gfativaflash.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  1. #include <stdio.h>
  2. #include <stdarg.h>
  3. #include <string>
  4. #include <regex>
  5. #include "gfativaflash.h"
  6. #define _TEST_VERBOSITY(v, n) (((v) == -1) && ((n) <= 2)) || (((v) >= 0) && ((v) >= (n)))
  7. /////////////////////////////////////////////////////////////////////////////
  8. static std::string _formatString(const char *fmt, ...)
  9. {
  10. int n;
  11. std::string s;
  12. char *p = NULL;
  13. va_list ap;
  14. va_start(ap, fmt);
  15. n = ::vasprintf(&p, fmt, ap);
  16. va_end(ap);
  17. if(n >= 0)
  18. {
  19. s = p;
  20. free(p);
  21. }
  22. return s;
  23. }
  24. /////////////////////////////////////////////////////////////////////////////
  25. TivaFlash::TivaFlash(QObject *pParent) : QObject(pParent),
  26. m_nExitCode(0),
  27. m_slvIdIsNodeAddr(false),
  28. m_verbosity(-1),
  29. m_imgSizeFile(0),
  30. m_imgCRC32File(0.0)
  31. {
  32. setObjectName("TivaFlash");
  33. for(int i = 0; i < _MAX_SLAVE_COUNT; i++)
  34. {
  35. m_materialEeprom.append("");
  36. m_serialEeprom.append("");
  37. m_imgSizeBoot.append(0);
  38. m_imgCRC32Boot.append(0.0);
  39. m_imgMaterialBoot.append("");
  40. m_imgBuildBoot.append("");
  41. m_imgSizeApp.append(0);
  42. m_imgCRC32App.append(0.0);
  43. m_imgMaterialApp.append("");
  44. m_imgBuildApp.append("");
  45. }
  46. }
  47. TivaFlash::~TivaFlash(void)
  48. {
  49. }
  50. /////////////////////////////////////////////////////////////////////////////
  51. bool TivaFlash::execFlashUtil(int nSlvID, const char *pszOpt)
  52. {
  53. FILE* pd;
  54. char buffer[256];
  55. m_nExitCode = -1;
  56. std::string strCmd = _formatString("\"%s\" %s --plugin-mode 2>&1", m_tivaFlashUtilPath.c_str(), pszOpt);
  57. if((pd = popen(strCmd.c_str(), "r")))
  58. {
  59. while(fgets(buffer, sizeof buffer, pd))
  60. {
  61. onCmdOutput(nSlvID, buffer);
  62. }
  63. pclose(pd);
  64. }
  65. return m_nExitCode == 0;
  66. }
  67. /////////////////////////////////////////////////////////////////////////////
  68. std::string TivaFlash::getSlavIDs(int nSlvID, int nMbID) const
  69. {
  70. std::string ret;
  71. if(m_slvIdIsNodeAddr)
  72. ret = _formatString("--node-addr=%d", nSlvID);
  73. else
  74. ret = _formatString("--stat-num=%d", nSlvID);
  75. if(_IS_VALID_MB_ID(nMbID))
  76. ret += _formatString(" --mb-slave-id=%d", nMbID);
  77. return ret;
  78. }
  79. /////////////////////////////////////////////////////////////////////////////
  80. int TivaFlash::parseTaggedString(const char *pszIn, std::string &sOut) const
  81. {
  82. static const std::string strRegEx = "<([0-9]+)>(.+)<\\/\\1>";
  83. static std::regex reg(strRegEx, std::regex_constants::ECMAScript | std::regex_constants::optimize);
  84. std::cmatch res;
  85. if(pszIn && *pszIn)
  86. {
  87. try
  88. {
  89. if(regex_search(pszIn, res, reg))
  90. {
  91. size_t nSize = res.size();
  92. if(nSize == 3)
  93. {
  94. std::string strTagName = res[1].str();
  95. sOut = res[2].str();
  96. return atoi(strTagName.c_str());
  97. }
  98. }
  99. }
  100. catch(...)
  101. {
  102. }
  103. }
  104. return -1;
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. void TivaFlash::onCmdOutput(int nSlvID, const char *pszOut)
  108. {
  109. std::string s;
  110. int nTag = parseTaggedString(pszOut, s);
  111. if(nTag >= 0)
  112. {
  113. QString qs = QString::fromStdString(s);
  114. switch(nTag)
  115. {
  116. /////////////////////////////////////////////////////////////////////
  117. case PLUGIN_TAG_EXIT_CODE:
  118. m_nExitCode = qs.toInt();
  119. // emit execOut(nSlvID, nTag, qs);
  120. break;
  121. /////////////////////////////////////////////////////////////////////
  122. case PLUGIN_TAG_STATUS:
  123. if(_TEST_VERBOSITY(m_verbosity, PLUGIN_TAG_STATUS))
  124. emit execOut(nSlvID, nTag, qs);
  125. break;
  126. case PLUGIN_TAG_INFO:
  127. if(_TEST_VERBOSITY(m_verbosity, PLUGIN_TAG_INFO))
  128. emit execOut(nSlvID, nTag, qs);
  129. break;
  130. case PLUGIN_TAG_ERROR:
  131. if(_TEST_VERBOSITY(m_verbosity, PLUGIN_TAG_ERROR))
  132. emit execOut(nSlvID, nTag, qs);
  133. break;
  134. /////////////////////////////////////////////////////////////////////
  135. case PLUGIN_TAG_IMG_MATERIAL_EEPROM:
  136. setMaterialEeprom(nSlvID, qs);
  137. break;
  138. case PLUGIN_TAG_IMG_SERIAL_EEPROM:
  139. setSerialEeprom(nSlvID, qs);
  140. break;
  141. /////////////////////////////////////////////////////////////////////
  142. case PLUGIN_TAG_IMG_LENGTH_BOOT:
  143. setImgSizeBoot(nSlvID, qs.toUInt());
  144. break;
  145. case PLUGIN_TAG_IMG_CRC32_BOOT:
  146. setImgCRC32Boot(nSlvID, qs.toDouble());
  147. break;
  148. case PLUGIN_TAG_IMG_BUILD_BOOT:
  149. setImgBuildBoot(nSlvID, qs);
  150. break;
  151. case PLUGIN_TAG_IMG_MATERIAL_BOOT:
  152. setImgMaterialBoot(nSlvID, qs);
  153. break;
  154. /////////////////////////////////////////////////////////////////////
  155. case PLUGIN_TAG_IMG_LENGTH_APP:
  156. setImgSizeApp(nSlvID, qs.toUInt());
  157. break;
  158. case PLUGIN_TAG_IMG_CRC32_APP:
  159. setImgCRC32App(nSlvID, qs.toDouble());
  160. break;
  161. case PLUGIN_TAG_IMG_MATERIAL_APP:
  162. setImgBuildApp(nSlvID, qs);
  163. break;
  164. case PLUGIN_TAG_IMG_BUILD_APP:
  165. setImgMaterialApp(nSlvID, qs);
  166. break;
  167. /////////////////////////////////////////////////////////////////////
  168. case PLUGIN_TAG_IMG_LENGTH_FILE:
  169. setImgSizeFile(qs.toUInt());
  170. break;
  171. case PLUGIN_TAG_IMG_CRC32_FILE:
  172. setImgCRC32File(qs.toDouble());
  173. break;
  174. case PLUGIN_TAG_IMG_MATERIAL_FILE:
  175. setImgMaterialFile(qs);
  176. break;
  177. case PLUGIN_TAG_IMG_BUILD_FILE:
  178. setImgBuildFile(qs);
  179. break;
  180. }
  181. }
  182. }
  183. /////////////////////////////////////////////////////////////////////////////
  184. /////////////////////////////////////////////////////////////////////////////
  185. /////////////////////////////////////////////////////////////////////////////
  186. bool TivaFlash::getMatSer(int nSlvID, int nMbID)
  187. {
  188. if(!_IS_VALID_SLV_ID(nSlvID))
  189. return -1;
  190. setMaterialEeprom(nSlvID, "");
  191. setSerialEeprom(nSlvID, "");
  192. std::string strOpt = _formatString("--show-mat-ser --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str());
  193. return execFlashUtil(nSlvID, strOpt.c_str());
  194. }
  195. bool TivaFlash::getTargetImgInfo(int nSlvID, int nMbID)
  196. {
  197. if(!_IS_VALID_SLV_ID(nSlvID))
  198. return -1;
  199. setImgSizeBoot(nSlvID, 0);
  200. setImgCRC32Boot(nSlvID, 0.0);
  201. setImgMaterialBoot(nSlvID, "");
  202. setImgBuildBoot(nSlvID, "");
  203. setImgSizeApp(nSlvID, 0);
  204. setImgCRC32App(nSlvID, 0.0);
  205. setImgMaterialApp(nSlvID, "");
  206. setImgBuildApp(nSlvID, "");
  207. std::string strOpt = _formatString("--show-dev-img-info --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str());
  208. return execFlashUtil(nSlvID, strOpt.c_str());
  209. }
  210. bool TivaFlash::pingTarget(int nSlvID, int nMbID)
  211. {
  212. if(!_IS_VALID_SLV_ID(nSlvID))
  213. return -1;
  214. std::string strOpt = _formatString("--ping-target --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str());
  215. return execFlashUtil(nSlvID, strOpt.c_str());
  216. }
  217. bool TivaFlash::startBootloader(int nSlvID, int nMbID)
  218. {
  219. if(!_IS_VALID_SLV_ID(nSlvID))
  220. return -1;
  221. std::string strOpt = _formatString("--start-boot --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str());
  222. return execFlashUtil(nSlvID, strOpt.c_str());
  223. }
  224. bool TivaFlash::resetBootloader(int nSlvID, int nMbID)
  225. {
  226. if(!_IS_VALID_SLV_ID(nSlvID))
  227. return -1;
  228. std::string strOpt = _formatString("--reset-boot --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str());
  229. return execFlashUtil(nSlvID, strOpt.c_str());
  230. }
  231. bool TivaFlash::validateImgFile(int nSlvID, int nMbID)
  232. {
  233. if(!_IS_VALID_SLV_ID(nSlvID))
  234. return -1;
  235. std::string strOpt = _formatString("--validate-img --itf-name=\"%s\" %s \"%s\"", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str(), m_imgFile.c_str());
  236. return execFlashUtil(nSlvID, strOpt.c_str());
  237. }
  238. /////////////////////////////////////////////////////////////////////////////
  239. /////////////////////////////////////////////////////////////////////////////
  240. /////////////////////////////////////////////////////////////////////////////
  241. QString TivaFlash::tivaFlashUtilPath(void) const
  242. {
  243. return QString::fromStdString(m_tivaFlashUtilPath);
  244. }
  245. void TivaFlash::setTivaFlashUtilPath(const QString &val)
  246. {
  247. m_tivaFlashUtilPath = val.toStdString();
  248. }
  249. /////////////////////////////////////////////////////////////////////////////
  250. QString TivaFlash::itfName(void) const
  251. {
  252. return QString::fromStdString(m_itfName);
  253. }
  254. void TivaFlash::setItfName(const QString &val)
  255. {
  256. m_itfName = val.toStdString();
  257. }
  258. /////////////////////////////////////////////////////////////////////////////
  259. bool TivaFlash::slvIdIsNodeAddr(void) const
  260. {
  261. return m_slvIdIsNodeAddr;
  262. }
  263. void TivaFlash::setSlvIdIsNodeAddr(bool val)
  264. {
  265. if(m_slvIdIsNodeAddr != val)
  266. {
  267. m_slvIdIsNodeAddr = val;
  268. }
  269. }
  270. /////////////////////////////////////////////////////////////////////////////
  271. int TivaFlash::verbosity(void) const
  272. {
  273. return m_verbosity;
  274. }
  275. void TivaFlash::setVerbosity(int val)
  276. {
  277. if(val < 0)
  278. val = 0;
  279. else if(val > 3)
  280. val = 3;
  281. if(m_verbosity != val)
  282. {
  283. m_verbosity = val;
  284. emit verbosityChanged(m_verbosity);
  285. }
  286. }
  287. /////////////////////////////////////////////////////////////////////////////
  288. QString TivaFlash::imgFile(void) const
  289. {
  290. return QString::fromStdString(m_imgFile);
  291. }
  292. void TivaFlash::setImgFile(const QString &val)
  293. {
  294. m_imgFile = val.toStdString();
  295. }
  296. /////////////////////////////////////////////////////////////////////////////
  297. /////////////////////////////////////////////////////////////////////////////
  298. /////////////////////////////////////////////////////////////////////////////
  299. const QStringList& TivaFlash::materialEeprom(void) const
  300. {
  301. return m_materialEeprom;
  302. }
  303. void TivaFlash::setMaterialEeprom(int nSlvID, const QString &val)
  304. {
  305. if(m_materialEeprom[nSlvID] != val)
  306. {
  307. m_materialEeprom[nSlvID] = val;
  308. emit materialEepromChanged();
  309. }
  310. }
  311. /////////////////////////////////////////////////////////////////////////////
  312. const QStringList& TivaFlash::serialEeprom(void) const
  313. {
  314. return m_serialEeprom;
  315. }
  316. void TivaFlash::setSerialEeprom(int nSlvID, const QString &val)
  317. {
  318. if(m_serialEeprom[nSlvID] != val)
  319. {
  320. m_serialEeprom[nSlvID] = val;
  321. emit serialEepromChanged();
  322. }
  323. }
  324. /////////////////////////////////////////////////////////////////////////////
  325. /////////////////////////////////////////////////////////////////////////////
  326. /////////////////////////////////////////////////////////////////////////////
  327. const QList<int>& TivaFlash::imgSizeBoot(void) const
  328. {
  329. return m_imgSizeBoot;
  330. }
  331. void TivaFlash::setImgSizeBoot(int nSlvID, int val)
  332. {
  333. if(m_imgSizeBoot[nSlvID] != val)
  334. {
  335. m_imgSizeBoot[nSlvID] = val;
  336. emit imgSizeBootChanged();
  337. }
  338. }
  339. /////////////////////////////////////////////////////////////////////////////
  340. const QList<double>& TivaFlash::imgCRC32Boot(void) const
  341. {
  342. return m_imgCRC32Boot;
  343. }
  344. void TivaFlash::setImgCRC32Boot(int nSlvID, double val)
  345. {
  346. if(m_imgCRC32Boot[nSlvID] != val)
  347. {
  348. m_imgCRC32Boot[nSlvID] = val;
  349. emit imgCRC32BootChanged();
  350. }
  351. }
  352. /////////////////////////////////////////////////////////////////////////////
  353. const QStringList& TivaFlash::imgMaterialBoot(void) const
  354. {
  355. return m_imgMaterialBoot;
  356. }
  357. void TivaFlash::setImgMaterialBoot(int nSlvID, const QString &val)
  358. {
  359. if(m_imgMaterialBoot[nSlvID] != val)
  360. {
  361. m_imgMaterialBoot[nSlvID] = val;
  362. emit imgMaterialBootChanged();
  363. }
  364. }
  365. /////////////////////////////////////////////////////////////////////////////
  366. const QStringList& TivaFlash::imgBuildBoot(void) const
  367. {
  368. return m_imgBuildBoot;
  369. }
  370. void TivaFlash::setImgBuildBoot(int nSlvID, const QString &val)
  371. {
  372. if(m_imgBuildBoot[nSlvID] != val)
  373. {
  374. m_imgBuildBoot[nSlvID] = val;
  375. emit imgBuildBootChanged();
  376. }
  377. }
  378. /////////////////////////////////////////////////////////////////////////////
  379. /////////////////////////////////////////////////////////////////////////////
  380. /////////////////////////////////////////////////////////////////////////////
  381. const QList<int>& TivaFlash::imgSizeApp(void) const
  382. {
  383. return m_imgSizeApp;
  384. }
  385. void TivaFlash::setImgSizeApp(int nSlvID, int val)
  386. {
  387. if(m_imgSizeApp[nSlvID] != val)
  388. {
  389. m_imgSizeApp[nSlvID] = val;
  390. emit imgSizeAppChanged();
  391. }
  392. }
  393. /////////////////////////////////////////////////////////////////////////////
  394. const QList<double>& TivaFlash::imgCRC32App(void) const
  395. {
  396. return m_imgCRC32App;
  397. }
  398. void TivaFlash::setImgCRC32App(int nSlvID, double val)
  399. {
  400. if(m_imgCRC32App[nSlvID] != val)
  401. {
  402. m_imgCRC32App[nSlvID] = val;
  403. emit imgCRC32AppChanged();
  404. }
  405. }
  406. /////////////////////////////////////////////////////////////////////////////
  407. const QStringList& TivaFlash::imgMaterialApp(void) const
  408. {
  409. return m_imgMaterialApp;
  410. }
  411. void TivaFlash::setImgMaterialApp(int nSlvID, const QString &val)
  412. {
  413. if(m_imgMaterialApp[nSlvID] != val)
  414. {
  415. m_imgMaterialApp[nSlvID] = val;
  416. emit imgMaterialAppChanged();
  417. }
  418. }
  419. /////////////////////////////////////////////////////////////////////////////
  420. const QStringList& TivaFlash::imgBuildApp(void) const
  421. {
  422. return m_imgBuildApp;
  423. }
  424. void TivaFlash::setImgBuildApp(int nSlvID, const QString &val)
  425. {
  426. if(m_imgBuildApp[nSlvID] != val)
  427. {
  428. m_imgBuildApp[nSlvID] = val;
  429. emit imgBuildAppChanged();
  430. }
  431. }
  432. /////////////////////////////////////////////////////////////////////////////
  433. /////////////////////////////////////////////////////////////////////////////
  434. /////////////////////////////////////////////////////////////////////////////
  435. int TivaFlash::imgSizeFile(void) const
  436. {
  437. return m_imgSizeFile;
  438. }
  439. void TivaFlash::setImgSizeFile(int val)
  440. {
  441. if(m_imgSizeFile != val)
  442. {
  443. m_imgSizeFile = val;
  444. emit imgSizeFileChanged(m_imgSizeFile);
  445. }
  446. }
  447. /////////////////////////////////////////////////////////////////////////////
  448. double TivaFlash::imgCRC32File(void) const
  449. {
  450. return m_imgCRC32File;
  451. }
  452. void TivaFlash::setImgCRC32File(double val)
  453. {
  454. if(m_imgCRC32File != val)
  455. {
  456. m_imgCRC32File = val;
  457. emit imgCRC32FileChanged(m_imgCRC32File);
  458. }
  459. }
  460. /////////////////////////////////////////////////////////////////////////////
  461. const QString& TivaFlash::imgMaterialFile(void) const
  462. {
  463. return m_imgMaterialFile;
  464. }
  465. void TivaFlash::setImgMaterialFile(const QString &val)
  466. {
  467. if(m_imgMaterialFile != val)
  468. {
  469. m_imgMaterialFile = val;
  470. emit materialFileChanged(m_imgMaterialFile);
  471. }
  472. }
  473. /////////////////////////////////////////////////////////////////////////////
  474. const QString& TivaFlash::imgBuildFile(void) const
  475. {
  476. return m_imgBuildFile;
  477. }
  478. void TivaFlash::setImgBuildFile(const QString &val)
  479. {
  480. if(m_imgBuildFile != val)
  481. {
  482. m_imgBuildFile = val;
  483. emit imgBuildFileChanged(m_imgBuildFile);
  484. }
  485. }