2 ******************************************************************************
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @addtogroup GCSPlugins GCS Plugins
8 * @addtogroup UAVTalkPlugin UAVTalk Plugin
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
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
30 #include "uavobjectmanager.h"
31 #include "uavtalk_global.h"
36 #include <QMutexLocker>
39 #include <QtNetwork/QUdpSocket>
41 class UAVTALK_EXPORT UAVTalk
: public QObject
{
44 friend class IODeviceReader
;
47 static const quint16 ALL_INSTANCES
= 0xFFFF;
51 quint32 txObjectBytes
;
56 quint32 rxObjectBytes
;
63 UAVTalk(QIODevice
*iodev
, UAVObjectManager
*objMngr
);
69 bool sendObject(UAVObject
*obj
, bool acked
, bool allInstances
);
70 bool sendObjectRequest(UAVObject
*obj
, bool allInstances
);
71 void cancelTransaction(UAVObject
*obj
);
74 void transactionCompleted(UAVObject
*obj
, bool success
);
77 void processInputStream();
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;
110 STATE_SYNC
, STATE_TYPE
, STATE_SIZE
, STATE_OBJID
, STATE_INSTID
, STATE_DATA
, STATE_CS
, STATE_COMPLETE
, STATE_ERROR
114 QPointer
<QIODevice
> io
;
116 UAVObjectManager
*objMngr
;
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
134 quint8 rxTmpBuffer
[4];
139 quint16 rxPacketLength
;
144 QUdpSocket
*udpSocketTx
;
145 QUdpSocket
*udpSocketRx
;
146 QByteArray rxDataArray
;
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
);