dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
uavtalk.h
Go to the documentation of this file.
1 
16 /*
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation; either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful, but
23  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
24  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
25  * for more details.
26  *
27  * You should have received a copy of the GNU General Public License along
28  * with this program; if not, see <http://www.gnu.org/licenses/>
29  *
30  * Additional note on redistribution: The copyright and license notices above
31  * must be maintained in each individual source file that is a derivative work
32  * of this source file; otherwise redistribution is prohibited.
33  */
34 
35 #ifndef UAVTALK_H
36 #define UAVTALK_H
37 
38 #include <QtCore>
39 #include <QIODevice>
40 #include <QMap>
41 #include <QSemaphore>
43 #include "uavtalk_global.h"
44 #include <QtNetwork/QUdpSocket>
45 
46 class UAVTALK_EXPORT UAVTalk : public QObject
47 {
48  Q_OBJECT
49 
50 public:
51  struct ComStats
52  {
53  quint32 txBytes;
54  quint32 rxBytes;
55  quint32 txObjectBytes;
56  quint32 rxObjectBytes;
57  quint32 rxObjects;
58  quint32 txObjects;
59  quint32 txErrors;
60  quint32 rxErrors;
61  };
62 
63  UAVTalk(QIODevice *iodev, UAVObjectManager *objMngr, bool canBlock = true);
64  ~UAVTalk();
65  bool sendObject(UAVObject *obj, bool acked, bool allInstances);
66  bool sendObjectRequest(UAVObject *obj, bool allInstances);
67  bool requestFile(quint32 fileId, quint32 offset);
68 
69  ComStats getStats();
70 
71  bool processInput();
72 
73 signals:
74  // The only signals we send to the upper level are when we
75  // either receive an ACK or a NACK for a request.
76  void ackReceived(UAVObject *obj);
77  void nackReceived(UAVObject *obj);
78 
79  // Or when we get some file data
80  void fileDataReceived(quint32 fileId, quint32 offset, quint8 *data,
81  quint32 dataLen, bool eof, bool lastInSeq);
82 
83 private slots:
84  void processInputStream(void);
85 
86 protected:
87  // Constants
88  static const int VER_MASK = 0x70;
89  static const int TYPE_MASK = 0x0f;
90 
91  static const int TYPE_VER = 0x20;
92  static const int TYPE_OBJ = 0x00;
93  static const int TYPE_OBJ_REQ = 0x01;
94  static const int TYPE_OBJ_ACK = 0x02;
95  static const int TYPE_ACK = 0x03;
96  static const int TYPE_NACK = 0x04;
97  static const int TYPE_FILEREQ = 0x08;
98  static const int TYPE_FILEDATA = 0x09;
99 
100  static const int MIN_HEADER_LENGTH = 8; // sync(1), type (1), size(2), object ID(4)
101  static const int MAX_HEADER_LENGTH = MIN_HEADER_LENGTH + 2; // instance ID(2, not used in single objs)
102 
103  static const int CHECKSUM_LENGTH = 1;
104 
105  static const int MAX_PACKET_LENGTH = 256;
106 
107  static const int MAX_PAYLOAD_LENGTH = (MAX_PACKET_LENGTH - CHECKSUM_LENGTH - MAX_HEADER_LENGTH);
108 
109  static const quint16 ALL_INSTANCES = 0xFFFF;
110  static const quint16 OBJID_NOTFOUND = 0x0000;
111 
112  static const int TX_BACKLOG_SIZE = 2 * 1024;
113  static const quint8 crc_table[256];
114 
115 #pragma pack(push)
116 #pragma pack(1)
117  struct UAVTalkHeader {
118  quint8 sync;
119  quint8 type;
120  quint8 size;
121  quint8 resv;
122  quint32 objId;
123  };
124 
126  quint32 offset;
127  quint8 flags;
128  };
129 
130  static const quint8 FILEDATA_FLAG_EOF = 0x01;
131  static const quint8 FILEDATA_FLAG_LAST = 0x02;
132 #pragma pack(pop)
133 
134  // Variables
135  QPointer<QIODevice> io;
137  bool canBlock;
138 
139  // This is a tradeoff between the frequency of the need to
140  // compact/copy left and buffer size.
141  quint8 rxBuffer[MAX_PACKET_LENGTH * 12];
142  quint8 txBuffer[MAX_PACKET_LENGTH];
143 
144  // Variables used by the receive state machine
145 
146  quint32 startOffset;
147  quint32 filledBytes;
148 
150 
151  // Methods
152  bool objectTransaction(UAVObject *obj, quint8 type, bool allInstances);
153  bool receiveObject(quint8 type, quint32 objId, quint16 instId,
154  quint8 *data, quint32 length);
155  bool receiveFileChunk(quint32 fileId, quint8 *data, quint32 length);
156  UAVObject *updateObject(quint32 objId, quint16 instId, quint8 *data);
157  bool transmitNack(quint32 objId);
158  bool transmitObject(UAVObject *obj, quint8 type, bool allInstances);
159  bool transmitSingleObject(UAVObject *obj, quint8 type, bool allInstances);
160  quint8 updateCRC(quint8 crc, const quint8 *data, qint32 length);
161  bool transmitFrame(quint32 length, bool incrTxObj = true);
162 };
163 
164 #endif // UAVTALK_H
bool canBlock
Definition: uavtalk.h:137
quint32 txObjectBytes
Definition: uavtalk.h:55
global crc_table
quint32 txBytes
Definition: uavtalk.h:53
quint32 rxErrors
Definition: uavtalk.h:60
ComStats stats
Definition: uavtalk.h:149
DataFields data
quint32 filledBytes
Definition: uavtalk.h:147
quint32 txObjects
Definition: uavtalk.h:58
quint32 startOffset
Definition: uavtalk.h:146
quint32 rxBytes
Definition: uavtalk.h:54
quint32 rxObjectBytes
Definition: uavtalk.h:56
QPointer< QIODevice > io
Definition: uavtalk.h:135
quint32 rxObjects
Definition: uavtalk.h:57
UAVObjectManager * objMngr
Definition: uavtalk.h:136
function crc
quint32 txErrors
Definition: uavtalk.h:59