File indexing completed on 2024-04-21 04:34:29

0001 /*
0002  * This file is part of KDevelop project
0003  * Copyright 2016 Patrick José Pereira <patrickelectric@gmail.com>
0004  * Copyright 2010 Denis Martinez
0005  * Copyright 2010 Martin Peres
0006  *
0007  * This program is free software; you can redistribute it and/or modify
0008  * it under the terms of the GNU Library General Public License as
0009  * published by the Free Software Foundation; either version 2 of the
0010  * License, or (at your option) any later version.
0011  *
0012  * This program is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0015  * GNU General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU General Public
0018  * License along with this program; if not, write to the
0019  * Free Software Foundation, Inc.,
0020  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
0021  */
0022 
0023 #include "toolkit.h"
0024 
0025 #include <QStringList>
0026 #include <QDir>
0027 #include <QFile>
0028 #include <QDebug>
0029 
0030 #include <interfaces/isession.h>
0031 #include <interfaces/icore.h>
0032 #include <KConfigGroup>
0033 
0034 Q_LOGGING_CATEGORY(TkMsg, "Kdev.embedded.tk.msg")
0035 
0036 using namespace KDevelop;
0037 
0038 Toolkit::Toolkit()
0039 {
0040     KConfigGroup settings = ICore::self()->activeSession()->config()->group("Embedded");
0041     qCDebug(TkMsg) << "Toolkit settings" << settings.groupList();
0042     m_arduinoFolder = new QFile(settings.readEntry("arduinoFolder", QString()));
0043     m_arduinoPath = getPath(m_arduinoFolder->fileName());
0044     m_avrdudeMcuList = settings.readEntry("avrdudeMCUList", QStringList());
0045 }
0046 
0047 Toolkit::~Toolkit()
0048 {
0049     delete m_arduinoFolder;
0050 }
0051 
0052 Toolkit& Toolkit::instance()
0053 {
0054     static Toolkit self;
0055     return self;
0056 }
0057 
0058 QString Toolkit::arduinoPath()
0059 {
0060     return m_arduinoPath;
0061 }
0062 
0063 QString Toolkit::boardFile(QString path)
0064 {
0065     return getPath(path + boardFilePath());
0066 }
0067 
0068 QString Toolkit::boardFile()
0069 {
0070     return boardFile(arduinoPath());
0071 }
0072 
0073 QString Toolkit::toolkitVersion(QString path)
0074 {
0075     QFile file(QDir(path).filePath(QStringLiteral("revisions.txt")));
0076     if (!file.open(QFile::ReadOnly))
0077     {
0078         qCDebug(TkMsg) << "It's not possible to open revision.txt";
0079         return QString();
0080     }
0081 
0082     QTextStream reader(&file);
0083     QString arduinoVersion = reader.readLine();
0084     while (arduinoVersion == QChar::fromLatin1('\n') && ! reader.atEnd())
0085     {
0086         arduinoVersion = reader.readLine();
0087         qCDebug(TkMsg) << "Arduino version " << arduinoVersion;
0088     }
0089     QList<QString> list = arduinoVersion.split(QChar::fromLatin1(' '));
0090     if (list.size() >= 2)
0091     {
0092         return  list.at(1).trimmed();
0093     }
0094     return QString();
0095 }
0096 
0097 QString Toolkit::toolkitVersion()
0098 {
0099     return toolkitVersion(arduinoPath());
0100 }
0101 
0102 QString Toolkit::avrdudeConfigPath()
0103 {
0104     return QStringLiteral("/hardware/tools/avr/etc/avrdude.conf");
0105 }
0106 QString Toolkit::avrConfigFile()
0107 {
0108     return getPath(arduinoPath() + avrdudeConfigPath());
0109 }
0110 
0111 QString Toolkit::avrdudePath()
0112 {
0113     return QString(avrProgramPath() + QStringLiteral("/avrdude"));
0114 }
0115 
0116 QString Toolkit::getAvrdudeFile()
0117 {
0118     return getPath(arduinoPath() + avrProgramPath() + QStringLiteral("/avrdude"));
0119 }
0120 
0121 QString Toolkit::getOpenocdFile()
0122 {
0123     return QStandardPaths::findExecutable(QStringLiteral("openocd"));
0124 }
0125 
0126 QStringList Toolkit::avrdudeMcuList()
0127 {
0128     return m_avrdudeMcuList;
0129 }
0130 
0131 QString Toolkit::boardFilePath()
0132 {
0133     return QStringLiteral("/hardware/arduino/avr/boards.txt");
0134 }
0135 
0136 QString Toolkit::avrProgramPath()
0137 {
0138     return QStringLiteral("/hardware/tools/avr/bin");
0139 }
0140 
0141 bool Toolkit::isValidArduinoPath(QString path)
0142 {
0143     QString version = Toolkit::toolkitVersion(path);
0144     QStringList versionList = version.split(QChar::fromLatin1('.'));
0145     if (versionList.size() != 3)
0146     {
0147         return false;
0148     }
0149 
0150     QStringList validVersionList = QStringLiteral(ARDUINO_SDK_MIN_VERSION_NAME).split(QChar::fromLatin1('.'));
0151     if (versionList[0].toInt() >= validVersionList[0].toInt()
0152         && versionList[1].toInt() >= validVersionList[1].toInt()
0153         && versionList[2].toInt() >= validVersionList[2].toInt()
0154     )
0155     {
0156         return true;
0157     }
0158     return false;
0159 }
0160 
0161 QString Toolkit::getPath(QString path)
0162 {
0163     return QFile(path).exists() ? path : QString();
0164 }
0165 
0166 bool Toolkit::setArduinoPath(QString path)
0167 {
0168     if (QFile(path).exists())
0169     {
0170         m_arduinoFolder = new QFile(path);
0171         m_arduinoPath = getPath(m_arduinoFolder->fileName());
0172         return true;
0173     }
0174     return false;
0175 }
0176