#include "System.h" #include #include #include #include #include #include #include #include #include //#include "version.h" const QStringList System::allowedJsonFiles = std::initializer_list{ QString("DC2C_conf.json"), QString("DC2C_cash.json"), QString("DC2C_device.json"), QString("DC2C_print01.json"), QString("DC2C_print02.json"), QString("DC2C_print03.json"), QString("DC2C_print04.json"), QString("DC2C_print05.json"), QString("DC2C_print06.json"), QString("DC2C_print07.json"), QString("DC2C_print08.json"), QString("DC2C_print09.json"), QString("DC2C_print10.json"), QString("DC2C_print11.json"), QString("DC2C_print12.json"), QString("DC2C_print13.json"), QString("DC2C_print14.json"), QString("DC2C_print15.json"), QString("DC2C_print16.json"), QString("DC2C_print17.json"), QString("DC2C_print18.json"), QString("DC2C_print19.json"), QString("DC2C_print20.json"), QString("DC2C_print21.json"), QString("DC2C_print22.json"), QString("DC2C_print23.json"), QString("DC2C_print24.json"), QString("DC2C_print25.json"), QString("DC2C_print26.json"), QString("DC2C_print27.json"), QString("DC2C_print28.json"), QString("DC2C_print29.json"), QString("DC2C_print30.json"), QString("DC2C_print31.json"), QString("DC2C_print32.json") }; QString System::errorMsg = ""; System::System(QObject *parent) : QObject(parent) { } /******************************************************************************** * static function to return error message. * */ QString System::getErrorMessage() { return errorMsg; } /******************************************************************************** * static function to check if a writable SD-card is in the cardslot. * * This function only checks a certain path or a mount entry. It does not mount * a SD-card, this must be provided by the OS (automounter). * * returns 1 if a SD-card is available, 0 otherwise. */ bool System::checkForSDCard() { #if defined (ARCH_DesktopLinux) // DEBUG / TEST: return true; #endif if (!test_mount(getSDCardDeviceName(), getSDCardMountPath())) { qDebug() << "check for SD-card failed: \n" << " device name is: " << getSDCardDeviceName() << " mount path is: " << getSDCardMountPath(); return false; } return true; } /******************************************************************************** * static function for umount sd-card. * */ bool System::umountSDCard() { #if defined (ARCH_DesktopLinux) // DEBUG / TEST: return true; #endif QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); QString commandString = "umount " + getSDCardMountPath(); process.start(commandString); if (!process.waitForStarted()) { errorMsg = "System::umountSDCard(): ERROR: waitForStarted()"; return false; } if (!process.waitForFinished(600000)) { errorMsg = "System::umountSDCard(): ERROR: " + process.errorString(); qDebug() << errorMsg; return false; } else { QByteArray bytes = process.readAll(); QStringList lines = QString(bytes).split("\n"); foreach (QString line, lines) { qDebug() << "System::umountSDCard() line: " << line; } } return true; } /******************************************************************************** * static function for umount usb-stick. * */ bool System::umountUSBStick() { #if defined (ARCH_DesktopLinux) // DEBUG / TEST: return true; #endif QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); QString commandString = "umount " + getUSBMountPath(); process.start(commandString); if (!process.waitForStarted()) { errorMsg = "System::umountUSBStick(): ERROR: waitForStarted()"; return false; } if (!process.waitForFinished(600000)) { errorMsg = "System::umountUSBStick(): ERROR: " + process.errorString(); qDebug() << errorMsg; return false; } else { QByteArray bytes = process.readAll(); QStringList lines = QString(bytes).split("\n"); foreach (QString line, lines) { qDebug() << "System::umountUSBStick() line: " << line; } } return true; } /***************************************************************************** * test, if a certain device is already mounted * * e.g. a mounted SD-card on PTU4: * - device is '/dev/mmcblk0p1' * - mount path is '/media/mmcblk0p1' * * e.g. a mounted USB-Stick on PTU4: * - device is '/dev/sda' * - mount path is '/media/sda' */ bool System::test_mount(const QString& dev, const QString& mountpoint) { bool mounted = false; QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); qDebug() << "System::test_mount() dev = " << dev; qDebug() << "System::test_mount() mountpoint = " << mountpoint; //normalize mountpoint: (mountpoint is without trailing '/') QString tmp_mountpoint = QString(mountpoint).remove(QRegExp("/*$")); QString commandString = "mount"; process.start(commandString); if (!process.waitForStarted()) { errorMsg = "System::test_mount(): ERROR: waitForStarted()"; return false; } if (!process.waitForFinished(600000)) { errorMsg = "System::test_mount(): ERROR: " + process.errorString(); qDebug() << errorMsg; return false; } else { QByteArray bytes = process.readAll(); QStringList lines = QString(bytes).split("\n"); foreach (QString line, lines) { //qDebug() << "System::test_mount() line: " << line; //if (line.contains(dev)) qDebug() << "System::test_mount() line contains dev"; //if (line.contains(tmp_mountpoint)) qDebug() << "System::test_mount() line contains mountpoint"; if (line.contains(dev) && line.contains(tmp_mountpoint)) { qDebug() << "System::test_mount(): " << dev << " is mounted on " << tmp_mountpoint; mounted = true; } } } return mounted; } /***************************************************************************** * test, if a certain directory is existing and readable */ bool System::test_dir(const QString& dir) { QFileInfo fileInfo(dir); if (! fileInfo.isDir() ) { return false; } return true; } bool System::checkForUSBStick() { #if defined (ARCH_DesktopLinux) // DEBUG / TEST: if (QFileInfo(getUSBMountPath()).isDir()) return true; else return false; #endif if (getUSBMountPath().isEmpty()) { return false; } return true; } /** * @brief SupportSystem::getUSBMountPath * @return path where a USB storage device is mounted. * * Note, do not return an empty string ("") here because a calling method (which could not be * identified till now) is relying on this! */ QString System::getUSBMountPath() { #if defined (ARCH_DesktopLinux) // DEBUG / TEST: return QDir::homePath().append("/APconfigTest/USB"); #endif QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); QStringList mountLine; qDebug() << "System::getUSBMountPath()"; QRegExp devRegExp = QRegExp("dev/sd*", Qt::CaseSensitive, QRegExp::WildcardUnix); QRegExp mountRegExp = QRegExp("media/sd*", Qt::CaseSensitive, QRegExp::WildcardUnix); QString commandString = "mount"; process.start(commandString); if (!process.waitForStarted()) { errorMsg = "System::getUSBMountPath(): ERROR: waitForStarted()"; return ""; } if (!process.waitForFinished(600000)) { errorMsg = "System::getUSBMountPath(): ERROR: " + process.errorString(); qDebug() << errorMsg; return ""; } else { QByteArray bytes = process.readAll(); QStringList lines = QString(bytes).split("\n"); foreach (QString line, lines) { qDebug() << "System::getUSBMountPath() line: " << line; if (line.contains(devRegExp) && line.contains(mountRegExp)) { qDebug() << " -> this line is a usb storage device mount" << line; mountLine = line.split(' '); if (mountLine.size() > 3) { qDebug() << "System::getUSBMountPath(): " << mountLine.at(0) << " is mounted on " << mountLine.at(2); return mountLine.at(2); } } } } qDebug() << "System::getUSBMountPath() no mounted usb device found!"; return ""; } /******************************************************************************** * static function to check if a mounted sd-card is writable. * * Note: the caller must ensure (e.g. by calling checkForSDCard()) that a * sd-card is mounted. * */ bool System::isSDCardWritable() { QFileInfo fi(getSDCardMountPath()); if(fi.isDir() && fi.isWritable()) { return true; } else { return false; } } /******************************************************************************** * static function to check if a mounted usb-stick is writable. * * Note: the caller must ensure (e.g. by calling checkForUSBStick()) that a * usb-stick is mounted. * */ bool System::isUSBStickWritable() { QFileInfo fi(getUSBMountPath()); if(fi.isDir() && fi.isWritable()) { return true; } else { return false; } } /******************************************************************************** * wrapper function for getting free space on a sd-card on ptu * */ quint32 System::getFreeDiskSpaceSDCard() { #if defined (ARCH_DesktopLinux) return getFreeDiskSpace("/home/siegert/server1home"); #endif return getFreeDiskSpace(getSDCardMountPath()); } /******************************************************************************** * wrapper function for getting free space on a usb storage device on ptu * */ quint32 System::getFreeDiskSpaceUSBStick() { #if defined (ARCH_DesktopLinux) return getFreeDiskSpace("/home/siegert/server1home"); #endif return getFreeDiskSpace(getUSBMountPath()); } /******************************************************************************** * static function to read free disk space * * example output of 'df' command on ptu: * # df * Filesystem 1K-blocks Used Available Use% Mounted on * ubi0:rootfs 111704 78940 32764 71% / * devtmpfs 62072 0 62072 0% /dev * tmpfs 62332 0 62332 0% /dev/shm * tmpfs 62332 416 61916 1% /tmp * tmpfs 62332 24 62308 1% /run * ubi0:data 222260 5892 216368 3% /opt * tmpfs 62332 0 62332 0% /media * /dev/mmcblk0p1 3878912 87360 3791552 3% /media/mmcblk0p1 * * in this case, if we want to get the free space on the sd-card the methode should * be called: * getFreeDiskSpace("/dev/mmcblk0p1") * the result should be: * 3791552 * */ quint32 System::getFreeDiskSpace(const QString& dev) { quint32 availableSpace = 0; QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); //qDebug() << "System::getFreeDiskSpace() dev = " << dev; QString commandString = "df"; process.start(commandString); if (!process.waitForStarted()) { errorMsg = "System::getFreeDiskSpace(): ERROR: waitForStarted()"; return availableSpace; } if (!process.waitForFinished(600000)) { errorMsg = "System::getFreeDiskSpace(): ERROR: " + process.errorString(); qDebug() << errorMsg; return availableSpace; } else { QByteArray bytes = process.readAll(); QStringList lines = QString(bytes).split("\n"); foreach (QString line, lines) { //qDebug() << "System::getFreeDiskSpace() line: " << line; if (line.contains(dev)) { //qDebug() << "System::getFreeDiskSpace() line contains dev"; QStringList processResultLine = line.split(' ', QString::SkipEmptyParts); if (processResultLine.size() >= 4) { bool ok; availableSpace = processResultLine.at(3).toLong(&ok); if (!ok) { return 0; } qDebug() << "System::getFreeDiskSpace() availableSpace = " << availableSpace; } else { return availableSpace; } } } } return availableSpace; } /******************************************************************************** * static function to create a backup of logfiles * * 'targzfile' is an absolute filename for ar tar.gz file wich will be created * 'filelistfile' is an absolute filename of a file containing all names of files * which should be backed up. * * returns: * 1 on success * 0 on failure * */ quint8 System::createLogFileBackup(const QString & targzfile, const QString &filelistfile) { QProcess process; process.setProcessChannelMode(QProcess::MergedChannels); qDebug() << "System::createLogFileBackup() targzfile = " << targzfile; qDebug() << "System::createLogFileBackup() filelistfile = " << filelistfile; QString commandString = "tar hczvf " + targzfile + " -T " + filelistfile; qDebug() << "System::createLogFileBackup() tar command:" << commandString; process.start(commandString); if (!process.waitForStarted()) { errorMsg = "System::createLogFileBackup(): ERROR: waitForStarted()"; return 1; } if (!process.waitForFinished(600000)) { errorMsg = "System::createLogFileBackup(): ERROR: " + process.errorString(); qDebug() << errorMsg; return 1; } else { // DEBUG //QByteArray bytes = process.readAll(); //QStringList lines = QString(bytes).split("\n"); //foreach (QString line, lines) { // qDebug() << "System::createLogFileBackup() line: " << line; //} } QString resultMessage; QProcess::ExitStatus exitStatus = process.exitStatus(); int exitCode = process.exitCode(); if (exitStatus == QProcess::CrashExit) { resultMessage = "local backup program crashed"; qCritical() << resultMessage; return 1; } else /** * note for exit codes of process tar: * see: http://www.gnu.org/software/tar/manual/html_section/tar_19.html#Synopsis * * tar returns exitCode 2 if some files in logFileList could not be read (e.g. if they are not existing) * however, tar-archive is created although! * This means success for our needs! */ if ( (exitCode != 0) && (exitCode != 2) && (exitStatus == QProcess::NormalExit) ) { resultMessage = "Local Backup file \"" + targzfile + "\" with exit code = " + QString::number(exitCode); qCritical() << resultMessage; return 1; } else { resultMessage = "Local Backup file \"" + targzfile + "\" was created successfully."; qCritical() << resultMessage; return 0; } return 0; } /***************************************************************************** * get SD-card mount path. */ QString System::getSDCardMountPath() { #if defined (ARCH_PTU4) return "/media/mmcblk0p1"; #elif defined (ARCH_PTU5) return "/media/mmcblk0p1"; #elif defined (ARCH_DesktopLinux) // DEBUG / TEST: return "/home/siegert/APconfigTest"; #endif return ""; } /***************************************************************************** * get SD-card device name. */ QString System::getSDCardDeviceName() { #if defined (ARCH_PTU4) return "/dev/mmcblk0p1"; #elif defined (ARCH_PTU5) return "/dev/mmcblk0p1"; #elif defined (ARCH_DesktopLinux) return "/dev/sdf1"; #endif return ""; } /******************************************************************************** * static function to build and return a unique device id. * This id consists currently only of the mac-address of network device eth0. */ QString System::getUniqueDeviceId() { QString filename = "/sys/class/net/eth0/address"; QString result; QFile file(filename); if(!file.open(QIODevice::ReadOnly)) { qCritical() << "System::getUniqueDeviceId() could not open file: " << filename; result = "0"; } else { QTextStream in(&file); result = in.readLine(20); } file.close(); result.remove(':'); return result; } QString System::readStringFromFile(const QString & filename) { #if defined (ARCH_PTU4) QFileInfo fileinfo(filename); #else QFileInfo fileinfo(QDir::homePath() + filename); #endif if (! fileinfo.isReadable() ) { qDebug() << "System::readStringFromFile(): \"" << filename << "\" is not readable"; return ""; } #if defined (ARCH_PTU4) QFile file(filename); #else QFile file(QDir::homePath() + filename); #endif if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { qDebug() << "System::readStringFromFile() cannot open file: " << filename; return ""; } QTextStream in(&file); QString stringValue = in.readLine(100); qDebug() << "System::readStringFromFile() stringValue = " << stringValue; file.close(); return stringValue; } int System::readIntFromFile(const QString & filename) { bool ok; int result = (int)readStringFromFile(filename).toInt(&ok); if (!ok) { result = 0; } return result; } /**************************************************************************** * read ptu internal data * */ QString System::getPTU4SerialNumber() { return readStringFromFile("/sys/bus/i2c/devices/0-0050/SNO"); } QString System::getPTU4MACAddress() { return readStringFromFile("/sys/bus/i2c/devices/0-0050/MAC"); }