#include #include #include #include #include "gfativaflash.h" #define _TEST_VERBOSITY(v, n) (((v) == -1) && ((n) <= 2)) || (((v) >= 0) && ((v) >= (n))) ///////////////////////////////////////////////////////////////////////////// static std::string _formatString(const char *fmt, ...) { int n; std::string s; char *p = NULL; va_list ap; va_start(ap, fmt); n = ::vasprintf(&p, fmt, ap); va_end(ap); if(n >= 0) { s = p; free(p); } return s; } ///////////////////////////////////////////////////////////////////////////// TivaFlash::TivaFlash(QObject *pParent) : QObject(pParent), m_nExitCode(0), m_slvIdIsNodeAddr(false), m_verbosity(-1), m_imgSizeFile(0), m_imgCRC32File(0.0) { setObjectName("TivaFlash"); for(int i = 0; i < _MAX_SLAVE_COUNT; i++) { m_materialEeprom.append(""); m_serialEeprom.append(""); m_imgSizeBoot.append(0); m_imgCRC32Boot.append(0.0); m_imgMaterialBoot.append(""); m_imgBuildBoot.append(""); m_imgSizeApp.append(0); m_imgCRC32App.append(0.0); m_imgMaterialApp.append(""); m_imgBuildApp.append(""); } } TivaFlash::~TivaFlash(void) { } ///////////////////////////////////////////////////////////////////////////// bool TivaFlash::execFlashUtil(int nSlvID, const char *pszOpt) { FILE* pd; char buffer[256]; m_nExitCode = -1; std::string strCmd = _formatString("\"%s\" %s --plugin-mode 2>&1", m_tivaFlashUtilPath.c_str(), pszOpt); if((pd = popen(strCmd.c_str(), "r"))) { while(fgets(buffer, sizeof buffer, pd)) { onCmdOutput(nSlvID, buffer); } pclose(pd); } return m_nExitCode == 0; } ///////////////////////////////////////////////////////////////////////////// std::string TivaFlash::getSlavIDs(int nSlvID, int nMbID) const { std::string ret; if(m_slvIdIsNodeAddr) ret = _formatString("--node-addr=%d", nSlvID); else ret = _formatString("--stat-num=%d", nSlvID); if(_IS_VALID_MB_ID(nMbID)) ret += _formatString(" --mb-slave-id=%d", nMbID); return ret; } ///////////////////////////////////////////////////////////////////////////// int TivaFlash::parseTaggedString(const char *pszIn, std::string &sOut) const { static const std::string strRegEx = "<([0-9]+)>(.+)<\\/\\1>"; static std::regex reg(strRegEx, std::regex_constants::ECMAScript | std::regex_constants::optimize); std::cmatch res; if(pszIn && *pszIn) { try { if(regex_search(pszIn, res, reg)) { size_t nSize = res.size(); if(nSize == 3) { std::string strTagName = res[1].str(); sOut = res[2].str(); return atoi(strTagName.c_str()); } } } catch(...) { } } return -1; } ///////////////////////////////////////////////////////////////////////////// void TivaFlash::onCmdOutput(int nSlvID, const char *pszOut) { std::string s; int nTag = parseTaggedString(pszOut, s); if(nTag >= 0) { QString qs = QString::fromStdString(s); switch(nTag) { ///////////////////////////////////////////////////////////////////// case PLUGIN_TAG_EXIT_CODE: m_nExitCode = qs.toInt(); // emit execOut(nSlvID, nTag, qs); break; ///////////////////////////////////////////////////////////////////// case PLUGIN_TAG_STATUS: if(_TEST_VERBOSITY(m_verbosity, PLUGIN_TAG_STATUS)) emit execOut(nSlvID, nTag, qs); break; case PLUGIN_TAG_INFO: if(_TEST_VERBOSITY(m_verbosity, PLUGIN_TAG_INFO)) emit execOut(nSlvID, nTag, qs); break; case PLUGIN_TAG_ERROR: if(_TEST_VERBOSITY(m_verbosity, PLUGIN_TAG_ERROR)) emit execOut(nSlvID, nTag, qs); break; ///////////////////////////////////////////////////////////////////// case PLUGIN_TAG_IMG_MATERIAL_EEPROM: setMaterialEeprom(nSlvID, qs); break; case PLUGIN_TAG_IMG_SERIAL_EEPROM: setSerialEeprom(nSlvID, qs); break; ///////////////////////////////////////////////////////////////////// case PLUGIN_TAG_IMG_LENGTH_BOOT: setImgSizeBoot(nSlvID, qs.toUInt()); break; case PLUGIN_TAG_IMG_CRC32_BOOT: setImgCRC32Boot(nSlvID, qs.toDouble()); break; case PLUGIN_TAG_IMG_BUILD_BOOT: setImgBuildBoot(nSlvID, qs); break; case PLUGIN_TAG_IMG_MATERIAL_BOOT: setImgMaterialBoot(nSlvID, qs); break; ///////////////////////////////////////////////////////////////////// case PLUGIN_TAG_IMG_LENGTH_APP: setImgSizeApp(nSlvID, qs.toUInt()); break; case PLUGIN_TAG_IMG_CRC32_APP: setImgCRC32App(nSlvID, qs.toDouble()); break; case PLUGIN_TAG_IMG_MATERIAL_APP: setImgBuildApp(nSlvID, qs); break; case PLUGIN_TAG_IMG_BUILD_APP: setImgMaterialApp(nSlvID, qs); break; ///////////////////////////////////////////////////////////////////// case PLUGIN_TAG_IMG_LENGTH_FILE: setImgSizeFile(qs.toUInt()); break; case PLUGIN_TAG_IMG_CRC32_FILE: setImgCRC32File(qs.toDouble()); break; case PLUGIN_TAG_IMG_MATERIAL_FILE: setImgMaterialFile(qs); break; case PLUGIN_TAG_IMG_BUILD_FILE: setImgBuildFile(qs); break; } } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// bool TivaFlash::getMatSer(int nSlvID, int nMbID) { if(!_IS_VALID_SLV_ID(nSlvID)) return -1; setMaterialEeprom(nSlvID, ""); setSerialEeprom(nSlvID, ""); std::string strOpt = _formatString("--show-mat-ser --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str()); return execFlashUtil(nSlvID, strOpt.c_str()); } bool TivaFlash::getTargetImgInfo(int nSlvID, int nMbID) { if(!_IS_VALID_SLV_ID(nSlvID)) return -1; setImgSizeBoot(nSlvID, 0); setImgCRC32Boot(nSlvID, 0.0); setImgMaterialBoot(nSlvID, ""); setImgBuildBoot(nSlvID, ""); setImgSizeApp(nSlvID, 0); setImgCRC32App(nSlvID, 0.0); setImgMaterialApp(nSlvID, ""); setImgBuildApp(nSlvID, ""); std::string strOpt = _formatString("--show-dev-img-info --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str()); return execFlashUtil(nSlvID, strOpt.c_str()); } bool TivaFlash::pingTarget(int nSlvID, int nMbID) { if(!_IS_VALID_SLV_ID(nSlvID)) return -1; std::string strOpt = _formatString("--ping-target --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str()); return execFlashUtil(nSlvID, strOpt.c_str()); } bool TivaFlash::startBootloader(int nSlvID, int nMbID) { if(!_IS_VALID_SLV_ID(nSlvID)) return -1; std::string strOpt = _formatString("--start-boot --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str()); return execFlashUtil(nSlvID, strOpt.c_str()); } bool TivaFlash::resetBootloader(int nSlvID, int nMbID) { if(!_IS_VALID_SLV_ID(nSlvID)) return -1; std::string strOpt = _formatString("--reset-boot --itf-name=\"%s\" %s", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str()); return execFlashUtil(nSlvID, strOpt.c_str()); } bool TivaFlash::validateImgFile(int nSlvID, int nMbID) { if(!_IS_VALID_SLV_ID(nSlvID)) return -1; std::string strOpt = _formatString("--validate-img --itf-name=\"%s\" %s \"%s\"", m_itfName.c_str(), getSlavIDs(nSlvID, nMbID).c_str(), m_imgFile.c_str()); return execFlashUtil(nSlvID, strOpt.c_str()); } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// QString TivaFlash::tivaFlashUtilPath(void) const { return QString::fromStdString(m_tivaFlashUtilPath); } void TivaFlash::setTivaFlashUtilPath(const QString &val) { m_tivaFlashUtilPath = val.toStdString(); } ///////////////////////////////////////////////////////////////////////////// QString TivaFlash::itfName(void) const { return QString::fromStdString(m_itfName); } void TivaFlash::setItfName(const QString &val) { m_itfName = val.toStdString(); } ///////////////////////////////////////////////////////////////////////////// bool TivaFlash::slvIdIsNodeAddr(void) const { return m_slvIdIsNodeAddr; } void TivaFlash::setSlvIdIsNodeAddr(bool val) { if(m_slvIdIsNodeAddr != val) { m_slvIdIsNodeAddr = val; } } ///////////////////////////////////////////////////////////////////////////// int TivaFlash::verbosity(void) const { return m_verbosity; } void TivaFlash::setVerbosity(int val) { if(val < 0) val = 0; else if(val > 3) val = 3; if(m_verbosity != val) { m_verbosity = val; emit verbosityChanged(m_verbosity); } } ///////////////////////////////////////////////////////////////////////////// QString TivaFlash::imgFile(void) const { return QString::fromStdString(m_imgFile); } void TivaFlash::setImgFile(const QString &val) { m_imgFile = val.toStdString(); } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// const QStringList& TivaFlash::materialEeprom(void) const { return m_materialEeprom; } void TivaFlash::setMaterialEeprom(int nSlvID, const QString &val) { if(m_materialEeprom[nSlvID] != val) { m_materialEeprom[nSlvID] = val; emit materialEepromChanged(); } } ///////////////////////////////////////////////////////////////////////////// const QStringList& TivaFlash::serialEeprom(void) const { return m_serialEeprom; } void TivaFlash::setSerialEeprom(int nSlvID, const QString &val) { if(m_serialEeprom[nSlvID] != val) { m_serialEeprom[nSlvID] = val; emit serialEepromChanged(); } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// const QList& TivaFlash::imgSizeBoot(void) const { return m_imgSizeBoot; } void TivaFlash::setImgSizeBoot(int nSlvID, int val) { if(m_imgSizeBoot[nSlvID] != val) { m_imgSizeBoot[nSlvID] = val; emit imgSizeBootChanged(); } } ///////////////////////////////////////////////////////////////////////////// const QList& TivaFlash::imgCRC32Boot(void) const { return m_imgCRC32Boot; } void TivaFlash::setImgCRC32Boot(int nSlvID, double val) { if(m_imgCRC32Boot[nSlvID] != val) { m_imgCRC32Boot[nSlvID] = val; emit imgCRC32BootChanged(); } } ///////////////////////////////////////////////////////////////////////////// const QStringList& TivaFlash::imgMaterialBoot(void) const { return m_imgMaterialBoot; } void TivaFlash::setImgMaterialBoot(int nSlvID, const QString &val) { if(m_imgMaterialBoot[nSlvID] != val) { m_imgMaterialBoot[nSlvID] = val; emit imgMaterialBootChanged(); } } ///////////////////////////////////////////////////////////////////////////// const QStringList& TivaFlash::imgBuildBoot(void) const { return m_imgBuildBoot; } void TivaFlash::setImgBuildBoot(int nSlvID, const QString &val) { if(m_imgBuildBoot[nSlvID] != val) { m_imgBuildBoot[nSlvID] = val; emit imgBuildBootChanged(); } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// const QList& TivaFlash::imgSizeApp(void) const { return m_imgSizeApp; } void TivaFlash::setImgSizeApp(int nSlvID, int val) { if(m_imgSizeApp[nSlvID] != val) { m_imgSizeApp[nSlvID] = val; emit imgSizeAppChanged(); } } ///////////////////////////////////////////////////////////////////////////// const QList& TivaFlash::imgCRC32App(void) const { return m_imgCRC32App; } void TivaFlash::setImgCRC32App(int nSlvID, double val) { if(m_imgCRC32App[nSlvID] != val) { m_imgCRC32App[nSlvID] = val; emit imgCRC32AppChanged(); } } ///////////////////////////////////////////////////////////////////////////// const QStringList& TivaFlash::imgMaterialApp(void) const { return m_imgMaterialApp; } void TivaFlash::setImgMaterialApp(int nSlvID, const QString &val) { if(m_imgMaterialApp[nSlvID] != val) { m_imgMaterialApp[nSlvID] = val; emit imgMaterialAppChanged(); } } ///////////////////////////////////////////////////////////////////////////// const QStringList& TivaFlash::imgBuildApp(void) const { return m_imgBuildApp; } void TivaFlash::setImgBuildApp(int nSlvID, const QString &val) { if(m_imgBuildApp[nSlvID] != val) { m_imgBuildApp[nSlvID] = val; emit imgBuildAppChanged(); } } ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////// int TivaFlash::imgSizeFile(void) const { return m_imgSizeFile; } void TivaFlash::setImgSizeFile(int val) { if(m_imgSizeFile != val) { m_imgSizeFile = val; emit imgSizeFileChanged(m_imgSizeFile); } } ///////////////////////////////////////////////////////////////////////////// double TivaFlash::imgCRC32File(void) const { return m_imgCRC32File; } void TivaFlash::setImgCRC32File(double val) { if(m_imgCRC32File != val) { m_imgCRC32File = val; emit imgCRC32FileChanged(m_imgCRC32File); } } ///////////////////////////////////////////////////////////////////////////// const QString& TivaFlash::imgMaterialFile(void) const { return m_imgMaterialFile; } void TivaFlash::setImgMaterialFile(const QString &val) { if(m_imgMaterialFile != val) { m_imgMaterialFile = val; emit materialFileChanged(m_imgMaterialFile); } } ///////////////////////////////////////////////////////////////////////////// const QString& TivaFlash::imgBuildFile(void) const { return m_imgBuildFile; } void TivaFlash::setImgBuildFile(const QString &val) { if(m_imgBuildFile != val) { m_imgBuildFile = val; emit imgBuildFileChanged(m_imgBuildFile); } }