Merged in f5soh/librepilot/update_credits (pull request #529)
[librepilot.git] / ground / gcs / src / plugins / uavobjects / uavobject.h
bloba1e332b16958277b68b747640d7ac4c53ac66597
1 /**
2 ******************************************************************************
4 * @file uavobject.h
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @see The GNU Public License (GPL) Version 3
7 * @addtogroup GCSPlugins GCS Plugins
8 * @{
9 * @addtogroup UAVObjectsPlugin UAVObjects Plugin
10 * @{
11 * @brief The UAVUObjects GCS plugin
12 *****************************************************************************/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful, but
20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 * for more details.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #ifndef UAVOBJECT_H
29 #define UAVOBJECT_H
31 #include "uavobjects_global.h"
32 #include <QtGlobal>
33 #include <QObject>
34 #include <QMutex>
35 #include <QMutexLocker>
36 #include <QString>
37 #include <QList>
38 #include <QFile>
39 #include <stdint.h>
41 #include "uavobjectfield.h"
43 #define UAVOBJ_ACCESS_SHIFT 0
44 #define UAVOBJ_GCS_ACCESS_SHIFT 1
45 #define UAVOBJ_TELEMETRY_ACKED_SHIFT 2
46 #define UAVOBJ_GCS_TELEMETRY_ACKED_SHIFT 3
47 #define UAVOBJ_TELEMETRY_UPDATE_MODE_SHIFT 4
48 #define UAVOBJ_GCS_TELEMETRY_UPDATE_MODE_SHIFT 6
49 #define UAVOBJ_LOGGING_UPDATE_MODE_SHIFT 8
50 #define UAVOBJ_UPDATE_MODE_MASK 0x3
52 class UAVObjectField;
53 class QXmlStreamWriter;
54 class QXmlStreamReader;
55 class QJsonObject;
57 class UAVOBJECTS_EXPORT UAVObject : public QObject {
58 Q_OBJECT
60 public:
61 Q_PROPERTY(QString Name READ getName)
63 /**
64 * Object update mode
66 typedef enum {
67 UPDATEMODE_MANUAL = 0, /** Manually update object, by calling the updated() function */
68 UPDATEMODE_PERIODIC = 1, /** Automatically update object at periodic intervals */
69 UPDATEMODE_ONCHANGE = 2, /** Only update object when its data changes */
70 UPDATEMODE_THROTTLED = 3 /** Object is updated on change, but not more often than the interval time */
71 } UpdateMode;
73 /**
74 * Access mode
76 typedef enum {
77 ACCESS_READWRITE = 0,
78 ACCESS_READONLY = 1
79 } AccessMode;
81 /**
82 * Object metadata, each object has a meta object that holds its metadata. The metadata define
83 * properties for each object and can be used by multiple modules (e.g. telemetry and logger)
85 * The object metadata flags are packed into a single 16 bit integer.
86 * The bits in the flag field are defined as:
88 * Bit(s) Name Meaning
89 * ------ ---- -------
90 * 0 access Defines the access level for the local transactions (readonly=0 and readwrite=1)
91 * 1 gcsAccess Defines the access level for the local GCS transactions (readonly=0 and readwrite=1), not used in the flight s/w
92 * 2 telemetryAcked Defines if an ack is required for the transactions of this object (1:acked, 0:not acked)
93 * 3 gcsTelemetryAcked Defines if an ack is required for the transactions of this object (1:acked, 0:not acked)
94 * 4-5 telemetryUpdateMode Update mode used by the telemetry module (UAVObjUpdateMode)
95 * 6-7 gcsTelemetryUpdateMode Update mode used by the GCS (UAVObjUpdateMode)
96 * 8-9 loggingUpdateMode Update mode used by the logging module (UAVObjUpdateMode)
98 typedef struct {
99 quint16 flags; /** Defines flags for update and logging modes and whether an update should be ACK'd (bits defined above) */
100 quint16 flightTelemetryUpdatePeriod; /** Update period used by the telemetry module (only if telemetry mode is PERIODIC) */
101 quint16 gcsTelemetryUpdatePeriod; /** Update period used by the GCS (only if telemetry mode is PERIODIC) */
102 quint16 loggingUpdatePeriod; /** Update period used by the logging module (only if logging mode is PERIODIC) */
103 } __attribute__((packed)) Metadata;
106 UAVObject(quint32 objID, bool isSingleInst, const QString & name);
107 void initialize(quint32 instID);
108 quint32 getObjID();
109 quint32 getInstID();
110 bool isSingleInstance();
111 QString getName();
112 QString getCategory();
113 QString getDescription();
114 quint32 getNumBytes();
115 qint32 pack(quint8 *dataOut);
116 qint32 unpack(const quint8 *dataIn);
117 quint8 updateCRC(quint8 crc = 0);
118 bool save();
119 bool save(QFile & file);
120 bool load();
121 bool load(QFile & file);
122 virtual void setMetadata(const Metadata & mdata) = 0;
123 virtual Metadata getMetadata() = 0;
124 virtual Metadata getDefaultMetadata() = 0;
125 void lock();
126 void lock(int timeoutMs);
127 void unlock();
128 QMutex *getMutex();
129 qint32 getNumFields();
130 QList<UAVObjectField *> getFields();
131 UAVObjectField *getField(const QString & name);
132 QString toString();
133 QString toStringBrief();
134 QString toStringData();
136 void toXML(QXmlStreamWriter *xmlWriter);
137 void fromXML(QXmlStreamReader *xmlReader);
139 void toJson(QJsonObject &jsonObject);
140 void fromJson(const QJsonObject &jsonObject);
142 void emitTransactionCompleted(bool success);
143 void emitNewInstance(UAVObject *);
145 bool isKnown() const;
146 void setIsKnown(bool isKnown);
148 virtual bool isSettingsObject();
149 virtual bool isDataObject();
150 virtual bool isMetaDataObject();
152 // Metadata accessors
153 static void MetadataInitialize(Metadata & meta);
154 static AccessMode GetFlightAccess(const Metadata & meta);
155 static void SetFlightAccess(Metadata & meta, AccessMode mode);
156 static AccessMode GetGcsAccess(const Metadata & meta);
157 static void SetGcsAccess(Metadata & meta, AccessMode mode);
158 static quint8 GetFlightTelemetryAcked(const Metadata & meta);
159 static void SetFlightTelemetryAcked(Metadata & meta, quint8 val);
160 static quint8 GetGcsTelemetryAcked(const Metadata & meta);
161 static void SetGcsTelemetryAcked(Metadata & meta, quint8 val);
162 static UpdateMode GetFlightTelemetryUpdateMode(const Metadata & meta);
163 static void SetFlightTelemetryUpdateMode(Metadata & meta, UpdateMode val);
164 static UpdateMode GetGcsTelemetryUpdateMode(const Metadata & meta);
165 static void SetGcsTelemetryUpdateMode(Metadata & meta, UpdateMode val);
166 static UpdateMode GetLoggingUpdateMode(const Metadata & meta);
167 static void SetLoggingUpdateMode(Metadata & meta, UpdateMode val);
169 public slots:
170 void requestUpdate();
171 void requestUpdateAll();
172 void updated();
173 void updatedAll();
175 signals:
176 void objectUpdated(UAVObject *obj);
177 void objectUpdatedAuto(UAVObject *obj);
178 void objectUpdatedManual(UAVObject *obj, bool all = false);
179 void objectUpdatedPeriodic(UAVObject *obj);
180 void objectUnpacked(UAVObject *obj);
181 void updateRequested(UAVObject *obj, bool all = false);
182 void transactionCompleted(UAVObject *obj, bool success);
183 void newInstance(UAVObject *obj);
184 void isKnownChanged(UAVObject *obj);
186 protected:
187 quint32 objID;
188 quint32 instID;
189 bool isSingleInst;
190 QString name;
191 QString description;
192 QString category;
193 quint32 numBytes;
194 QMutex *mutex;
195 quint8 *data;
196 QList<UAVObjectField *> fields;
198 void initializeFields(QList<UAVObjectField *> & fields, quint8 *data, quint32 numBytes);
199 void setDescription(const QString & description);
200 void setCategory(const QString & category);
202 private:
203 bool m_isKnown;
205 private slots:
206 void fieldUpdated(UAVObjectField *field);
209 #endif // UAVOBJECT_H