From 5c9701753b4eef96f4175eff6ef9659826b74c15 Mon Sep 17 00:00:00 2001 From: peabody124 Date: Tue, 21 Sep 2010 07:07:39 +0000 Subject: [PATCH] OP-24 Ground/Logging: Made logging module use QIODevice passed to UAVTalk which inserts timestamps and object sizes. Should be trivial to implement read functionality. Need to confirm that QIODevice writes the data in the correct size though so timestamps are in the write place for sure. git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@1700 ebee16cc-31ac-478f-84a7-5cbb03baadba --- ground/src/plugins/logging/logfile.cpp | 49 ++++++++++++++++++++ ground/src/plugins/logging/logfile.h | 30 ++++++++++++ ground/src/plugins/logging/logging.pro | 7 ++- ground/src/plugins/logging/loggingplugin.cpp | 39 ++++------------ ground/src/plugins/logging/loggingplugin.h | 8 ++-- 5 files changed, 98 insertions(+), 35 deletions(-) create mode 100644 ground/src/plugins/logging/logfile.cpp create mode 100644 ground/src/plugins/logging/logfile.h diff --git a/ground/src/plugins/logging/logfile.cpp b/ground/src/plugins/logging/logfile.cpp new file mode 100644 index 000000000..9b33e732c --- /dev/null +++ b/ground/src/plugins/logging/logfile.cpp @@ -0,0 +1,49 @@ +#include "logfile.h" +#include + +LogFile::LogFile(QObject *parent) : + QIODevice(parent) +{ +} + +bool LogFile::open(OpenMode mode) { + + // start a timer for playback + myTime.restart(); + + // TODO: support openning for read or write to determine playback or not + Q_ASSERT(mode == QIODevice::WriteOnly); + if(file.open(mode) == FALSE) + { + qDebug() << "Unable to open " << file.fileName() << " for logging"; + return false; + } + + // TODO: Write a header at the beginng describing objects so that in future + // they can be read back if ID's change + + // Must call parent function for QIODevice to pass calls to writeData + QIODevice::open(mode); + + return true; +} + +void LogFile::close() +{ + file.close(); + QIODevice::close(); +} + +qint64 LogFile::writeData(const char * data, qint64 dataSize) { + quint32 timeStamp = myTime.elapsed(); + + file.write((char *) &timeStamp,sizeof(timeStamp)); + file.write((char *) &dataSize, sizeof(dataSize)); + + qint64 written = file.write(data, dataSize); + if(written != -1) + emit bytesWritten(written); + + //qDebug() << "Wrote " << dataSize << " bytes at " << timeStamp << " ms"; + return dataSize; +} diff --git a/ground/src/plugins/logging/logfile.h b/ground/src/plugins/logging/logfile.h new file mode 100644 index 000000000..708fa6c46 --- /dev/null +++ b/ground/src/plugins/logging/logfile.h @@ -0,0 +1,30 @@ +#ifndef LOGFILE_H +#define LOGFILE_H + +#include +#include +#include +#include + +class LogFile : public QIODevice +{ + Q_OBJECT +public: + explicit LogFile(QObject *parent = 0); + qint64 bytesAvailable() { return 0; }; + qint64 bytesToWrite() { return file.bytesToWrite(); }; + bool open(OpenMode mode); + void setFileName(QString name) { file.setFileName(name); }; + void close(); + qint64 writeData(const char * data, qint64 dataSize); + qint64 readData(char * /*data*/, qint64 /*maxlen*/) {return 0; } + +signals: +public slots: +private: +protected: + QTime myTime; + QFile file; +}; + +#endif // LOGFILE_H diff --git a/ground/src/plugins/logging/logging.pro b/ground/src/plugins/logging/logging.pro index c910d3017..577b8668c 100644 --- a/ground/src/plugins/logging/logging.pro +++ b/ground/src/plugins/logging/logging.pro @@ -4,8 +4,10 @@ DEFINES += LOGGING_LIBRARY QT += svg include(../../openpilotgcsplugin.pri) include(../../plugins/uavobjects/uavobjects.pri) +include(../../plugins/uavtalk/uavtalk.pri) include(logging_dependencies.pri) -HEADERS += loggingplugin.h +HEADERS += loggingplugin.h \ + logfile.h # logginggadgetwidget.h \ # loggingdialog.h \ # logginggadget.h \ @@ -13,7 +15,8 @@ HEADERS += loggingplugin.h # logginggadgetconfiguration.h # logginggadgetoptionspage.h -SOURCES += loggingplugin.cpp +SOURCES += loggingplugin.cpp \ + logfile.cpp # logginggadgetwidget.cpp \ # loggingdialog.cpp \ # logginggadget.cpp \ diff --git a/ground/src/plugins/logging/loggingplugin.cpp b/ground/src/plugins/logging/loggingplugin.cpp index 2cc359123..ff16cdaa7 100644 --- a/ground/src/plugins/logging/loggingplugin.cpp +++ b/ground/src/plugins/logging/loggingplugin.cpp @@ -43,6 +43,7 @@ #include #include #include +#include /** * Sets the file to use for logging and takes the parent plugin @@ -52,15 +53,13 @@ */ bool LoggingThread::openFile(QString file, LoggingPlugin * parent) { - // TODO: Write a header at the beginng describing objects so that in future - // they can be read back if ID's change logFile.setFileName(file); - if(logFile.open(QIODevice::WriteOnly) == FALSE) - { - qDebug() << "Unable to open " << file << " for logging"; - return false; - } + logFile.open(QIODevice::WriteOnly); + ExtensionSystem::PluginManager *pm = ExtensionSystem::PluginManager::instance(); + UAVObjectManager *objManager = pm->getObject(); + + uavTalk = new UAVTalk(&logFile, objManager); connect(parent,SIGNAL(stopLoggingSignal()),this,SLOT(stopLogging())); return true; @@ -74,28 +73,9 @@ bool LoggingThread::openFile(QString file, LoggingPlugin * parent) */ void LoggingThread::objectUpdated(UAVObject * obj) { - quint32 timeStamp = myTime.elapsed(); - quint32 objSize = obj->getNumBytes(); - quint32 objId = obj->getObjID(); - quint32 objInst = obj->getInstID(); - - quint8 * buffer = new quint8[objSize+16]; - - if(buffer == NULL) - return; - - obj->pack(&buffer[16]); - memcpy(buffer,&timeStamp,4); - memcpy(&buffer[4],&objSize,4); - memcpy(&buffer[8],&objId,4); - memcpy(&buffer[12],&objInst,4); - QWriteLocker locker(&lock); - qint64 written = logFile.write((char *) buffer,objSize+8); - - delete(buffer); - - //qDebug() << obj->getName() << " logged at " << timeStamp << " size: " << objSize << " written " << written; + if(!uavTalk->sendObject(obj,false,false) ) + qDebug() << "Error logging " << obj->getName(); }; /** @@ -123,7 +103,6 @@ void LoggingThread::run() } } - myTime.restart(); exec(); } @@ -135,7 +114,7 @@ void LoggingThread::stopLogging() { QWriteLocker locker(&lock); logFile.close(); - qDebug() << "File " << logFile.fileName() << " closed"; + qDebug() << "File closed"; quit(); } diff --git a/ground/src/plugins/logging/loggingplugin.h b/ground/src/plugins/logging/loggingplugin.h index 6d533513b..44493f789 100644 --- a/ground/src/plugins/logging/loggingplugin.h +++ b/ground/src/plugins/logging/loggingplugin.h @@ -29,8 +29,10 @@ #include #include +#include +#include + #include -#include #include class LoggingPlugin; @@ -49,9 +51,9 @@ public slots: protected: void run(); - QFile logFile; - QTime myTime; QReadWriteLock lock; + LogFile logFile; + UAVTalk * uavTalk; }; class LoggingPlugin : public ExtensionSystem::IPlugin