LP-106 Setup Wizard refresh : Add dual servo setup (dual aileron or
[librepilot.git] / ground / gcs / src / plugins / uavtalk / uavtalk.h
blobecda5e3f5cda95c40d0906e5a80e1552910894a0
1 /**
2 ******************************************************************************
4 * @file uavtalk.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
7 * @{
8 * @addtogroup UAVTalkPlugin UAVTalk Plugin
9 * @{
10 * @brief The UAVTalk protocol plugin
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #ifndef UAVTALK_H
28 #define UAVTALK_H
30 #include "uavobjectmanager.h"
31 #include "uavtalk_global.h"
33 #include <QtCore>
34 #include <QIODevice>
35 #include <QMutex>
36 #include <QMutexLocker>
37 #include <QMap>
38 #include <QThread>
39 #include <QtNetwork/QUdpSocket>
41 class UAVTALK_EXPORT UAVTalk : public QObject {
42 Q_OBJECT
44 friend class IODeviceReader;
46 public:
47 static const quint16 ALL_INSTANCES = 0xFFFF;
49 typedef struct {
50 quint32 txBytes;
51 quint32 txObjectBytes;
52 quint32 txObjects;
53 quint32 txErrors;
55 quint32 rxBytes;
56 quint32 rxObjectBytes;
57 quint32 rxObjects;
58 quint32 rxErrors;
59 quint32 rxSyncErrors;
60 quint32 rxCrcErrors;
61 } ComStats;
63 UAVTalk(QIODevice *iodev, UAVObjectManager *objMngr);
64 ~UAVTalk();
66 ComStats getStats();
67 void resetStats();
69 bool sendObject(UAVObject *obj, bool acked, bool allInstances);
70 bool sendObjectRequest(UAVObject *obj, bool allInstances);
71 void cancelTransaction(UAVObject *obj);
73 signals:
74 void transactionCompleted(UAVObject *obj, bool success);
76 private slots:
77 void processInputStream();
78 void dummyUDPRead();
80 private:
82 typedef struct {
83 quint8 respType;
84 quint32 respObjId;
85 quint16 respInstId;
86 } Transaction;
88 // Constants
89 static const int TYPE_MASK = 0xF8;
90 static const int TYPE_VER = 0x20;
91 static const int TYPE_OBJ = (TYPE_VER | 0x00);
92 static const int TYPE_OBJ_REQ = (TYPE_VER | 0x01);
93 static const int TYPE_OBJ_ACK = (TYPE_VER | 0x02);
94 static const int TYPE_ACK = (TYPE_VER | 0x03);
95 static const int TYPE_NACK = (TYPE_VER | 0x04);
97 // header : sync(1), type (1), size(2), object ID(4), instance ID(2)
98 static const int HEADER_LENGTH = 10;
100 static const int MAX_PAYLOAD_LENGTH = 256;
102 static const int CHECKSUM_LENGTH = 1;
104 static const int MAX_PACKET_LENGTH = (HEADER_LENGTH + MAX_PAYLOAD_LENGTH + CHECKSUM_LENGTH);
106 static const int TX_BUFFER_SIZE = 2 * 1024;
108 // Types
109 typedef enum {
110 STATE_SYNC, STATE_TYPE, STATE_SIZE, STATE_OBJID, STATE_INSTID, STATE_DATA, STATE_CS, STATE_COMPLETE, STATE_ERROR
111 } RxStateType;
113 // Variables
114 QPointer<QIODevice> io;
116 UAVObjectManager *objMngr;
118 ComStats stats;
120 QMutex mutex;
122 QMap<quint32, QMap<quint32, Transaction *> *> transMap;
124 quint8 rxBuffer[MAX_PACKET_LENGTH];
126 quint8 txBuffer[MAX_PACKET_LENGTH];
128 // Variables used by the receive state machine
129 // state machine variables
130 qint32 rxCount;
131 qint32 packetSize;
132 RxStateType rxState;
133 // data variables
134 quint8 rxTmpBuffer[4];
135 quint8 rxType;
136 quint32 rxObjId;
137 quint16 rxInstId;
138 quint16 rxLength;
139 quint16 rxPacketLength;
140 quint8 rxCSPacket;
141 quint8 rxCS;
143 bool useUDPMirror;
144 QUdpSocket *udpSocketTx;
145 QUdpSocket *udpSocketRx;
146 QByteArray rxDataArray;
148 // Methods
149 bool objectTransaction(quint8 type, quint32 objId, quint16 instId, UAVObject *obj);
150 bool processInputByte(quint8 rxbyte);
151 bool receiveObject(quint8 type, quint32 objId, quint16 instId, quint8 *data, qint32 length);
152 UAVObject *updateObject(quint32 objId, quint16 instId, quint8 *data);
153 void updateAck(quint8 type, quint32 objId, quint16 instId, UAVObject *obj);
154 void updateNack(quint32 objId, quint16 instId, UAVObject *obj);
155 bool transmitObject(quint8 type, quint32 objId, quint16 instId, UAVObject *obj);
156 bool transmitSingleObject(quint8 type, quint32 objId, quint16 instId, UAVObject *obj);
158 Transaction *findTransaction(quint32 objId, quint16 instId);
159 void openTransaction(quint8 type, quint32 objId, quint16 instId);
160 void closeTransaction(Transaction *trans);
161 void closeAllTransactions();
163 const char *typeToString(quint8 type);
166 #endif // UAVTALK_H