| 1 | /**************************************************************************** | 
|---|
| 2 | ** | 
|---|
| 3 | ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). | 
|---|
| 4 | ** All rights reserved. | 
|---|
| 5 | ** Contact: Nokia Corporation (qt-info@nokia.com) | 
|---|
| 6 | ** | 
|---|
| 7 | ** This file is part of the tools applications of the Qt Toolkit. | 
|---|
| 8 | ** | 
|---|
| 9 | ** $QT_BEGIN_LICENSE:LGPL$ | 
|---|
| 10 | ** Commercial Usage | 
|---|
| 11 | ** Licensees holding valid Qt Commercial licenses may use this file in | 
|---|
| 12 | ** accordance with the Qt Commercial License Agreement provided with the | 
|---|
| 13 | ** Software or, alternatively, in accordance with the terms contained in | 
|---|
| 14 | ** a written agreement between you and Nokia. | 
|---|
| 15 | ** | 
|---|
| 16 | ** GNU Lesser General Public License Usage | 
|---|
| 17 | ** Alternatively, this file may be used under the terms of the GNU Lesser | 
|---|
| 18 | ** General Public License version 2.1 as published by the Free Software | 
|---|
| 19 | ** Foundation and appearing in the file LICENSE.LGPL included in the | 
|---|
| 20 | ** packaging of this file.  Please review the following information to | 
|---|
| 21 | ** ensure the GNU Lesser General Public License version 2.1 requirements | 
|---|
| 22 | ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. | 
|---|
| 23 | ** | 
|---|
| 24 | ** In addition, as a special exception, Nokia gives you certain additional | 
|---|
| 25 | ** rights.  These rights are described in the Nokia Qt LGPL Exception | 
|---|
| 26 | ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. | 
|---|
| 27 | ** | 
|---|
| 28 | ** GNU General Public License Usage | 
|---|
| 29 | ** Alternatively, this file may be used under the terms of the GNU | 
|---|
| 30 | ** General Public License version 3.0 as published by the Free Software | 
|---|
| 31 | ** Foundation and appearing in the file LICENSE.GPL included in the | 
|---|
| 32 | ** packaging of this file.  Please review the following information to | 
|---|
| 33 | ** ensure the GNU General Public License version 3.0 requirements will be | 
|---|
| 34 | ** met: http://www.gnu.org/copyleft/gpl.html. | 
|---|
| 35 | ** | 
|---|
| 36 | ** If you have questions regarding the use of this file, please contact | 
|---|
| 37 | ** Nokia at qt-info@nokia.com. | 
|---|
| 38 | ** $QT_END_LICENSE$ | 
|---|
| 39 | ** | 
|---|
| 40 | ****************************************************************************/ | 
|---|
| 41 |  | 
|---|
| 42 | #ifndef TRKDEVICE_H | 
|---|
| 43 | #define TRKDEVICE_H | 
|---|
| 44 |  | 
|---|
| 45 | #include "callback.h" | 
|---|
| 46 |  | 
|---|
| 47 | #include <QtCore/QObject> | 
|---|
| 48 | #include <QtCore/QVariant> | 
|---|
| 49 | #include <QtCore/QByteArray> | 
|---|
| 50 | #include <QtCore/QSharedPointer> | 
|---|
| 51 |  | 
|---|
| 52 | QT_BEGIN_NAMESPACE | 
|---|
| 53 | class QIODevice; | 
|---|
| 54 | QT_END_NAMESPACE | 
|---|
| 55 |  | 
|---|
| 56 | namespace trk { | 
|---|
| 57 |  | 
|---|
| 58 | struct TrkResult; | 
|---|
| 59 | struct TrkMessage; | 
|---|
| 60 | struct TrkDevicePrivate; | 
|---|
| 61 |  | 
|---|
| 62 | /* TrkDevice: Implements a Windows COM or Linux device for | 
|---|
| 63 | * Trk communications. Provides synchronous write and asynchronous | 
|---|
| 64 | * read operation. | 
|---|
| 65 | * The serialFrames property specifies whether packets are encapsulated in | 
|---|
| 66 | * "0x90 <length>" frames, which is currently the case for serial ports. | 
|---|
| 67 | * Contains a write message queue allowing | 
|---|
| 68 | * for queueing messages with a notification callback. If the message receives | 
|---|
| 69 | * an ACK, the callback is invoked. | 
|---|
| 70 | * The special message TRK_WRITE_QUEUE_NOOP_CODE code can be used for synchronisation. | 
|---|
| 71 | * The respective  message will not be sent, the callback is just invoked. */ | 
|---|
| 72 |  | 
|---|
| 73 | enum { TRK_WRITE_QUEUE_NOOP_CODE = 0x7f }; | 
|---|
| 74 |  | 
|---|
| 75 | typedef trk::Callback<const TrkResult &> TrkCallback; | 
|---|
| 76 |  | 
|---|
| 77 | class TrkDevice : public QObject | 
|---|
| 78 | { | 
|---|
| 79 | Q_OBJECT | 
|---|
| 80 | Q_PROPERTY(bool serialFrame READ serialFrame WRITE setSerialFrame) | 
|---|
| 81 | Q_PROPERTY(bool verbose READ verbose WRITE setVerbose) | 
|---|
| 82 | public: | 
|---|
| 83 | explicit TrkDevice(QObject *parent = 0); | 
|---|
| 84 | virtual ~TrkDevice(); | 
|---|
| 85 |  | 
|---|
| 86 | bool open(const QString &port, QString *errorMessage); | 
|---|
| 87 | bool isOpen() const; | 
|---|
| 88 |  | 
|---|
| 89 | QString errorString() const; | 
|---|
| 90 |  | 
|---|
| 91 | bool serialFrame() const; | 
|---|
| 92 | void setSerialFrame(bool f); | 
|---|
| 93 |  | 
|---|
| 94 | int verbose() const; | 
|---|
| 95 | void setVerbose(int b); | 
|---|
| 96 |  | 
|---|
| 97 | // Enqueue a message with a notification callback. | 
|---|
| 98 | void sendTrkMessage(unsigned char code, | 
|---|
| 99 | TrkCallback callBack = TrkCallback(), | 
|---|
| 100 | const QByteArray &data = QByteArray(), | 
|---|
| 101 | const QVariant &cookie = QVariant()); | 
|---|
| 102 |  | 
|---|
| 103 | // Enqeue an initial ping | 
|---|
| 104 | void sendTrkInitialPing(); | 
|---|
| 105 |  | 
|---|
| 106 | // Send an Ack synchronously, bypassing the queue | 
|---|
| 107 | bool sendTrkAck(unsigned char token); | 
|---|
| 108 |  | 
|---|
| 109 | signals: | 
|---|
| 110 | void messageReceived(const trk::TrkResult &result); | 
|---|
| 111 | // Emitted with the contents of messages enclosed in 07e, not for log output | 
|---|
| 112 | void rawDataReceived(const QByteArray &data); | 
|---|
| 113 | void error(const QString &msg); | 
|---|
| 114 | void logMessage(const QString &msg); | 
|---|
| 115 |  | 
|---|
| 116 | private slots: | 
|---|
| 117 | void slotMessageReceived(const trk::TrkResult &result, const QByteArray &a); | 
|---|
| 118 |  | 
|---|
| 119 | protected slots: | 
|---|
| 120 | void emitError(const QString &msg); | 
|---|
| 121 | void emitLogMessage(const QString &msg); | 
|---|
| 122 |  | 
|---|
| 123 | public slots: | 
|---|
| 124 | void close(); | 
|---|
| 125 |  | 
|---|
| 126 | private: | 
|---|
| 127 | void readMessages(); | 
|---|
| 128 | TrkDevicePrivate *d; | 
|---|
| 129 | }; | 
|---|
| 130 |  | 
|---|
| 131 | } // namespace trk | 
|---|
| 132 |  | 
|---|
| 133 | #endif // TRKDEVICE_H | 
|---|