dRonin  adbada4
dRonin GCS
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Groups Pages
uavobject.cpp
Go to the documentation of this file.
1 
17 /*
18  * This program is free software; you can redistribute it and/or modify
19  * it under the terms of the GNU General Public License as published by
20  * the Free Software Foundation; either version 3 of the License, or
21  * (at your option) any later version.
22  *
23  * This program is distributed in the hope that it will be useful, but
24  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
25  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
26  * for more details.
27  *
28  * You should have received a copy of the GNU General Public License along
29  * with this program; if not, see <http://www.gnu.org/licenses/>
30  *
31  * Additional note on redistribution: The copyright and license notices above
32  * must be maintained in each individual source file that is a derivative work
33  * of this source file; otherwise redistribution is prohibited.
34  */
35 
36 #include "uavobject.h"
37 #include <QtEndian>
38 #include <QDebug>
39 #include <QJsonArray>
40 #include <QJsonValue>
41 
42 // Constants
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_UPDATE_MODE_MASK 0x3
50 
51 // Macros
52 #define SET_BITS(var, shift, value, mask) var = (var & ~(mask << shift)) | (value << shift);
53 
60 UAVObject::UAVObject(quint32 objID, bool isSingleInst, const QString &name)
61 {
62  this->objID = objID;
63  this->instID = 0;
64  this->isSingleInst = isSingleInst;
65  this->name = name;
66 }
67 
71 void UAVObject::initialize(quint32 instID)
72 {
73  this->instID = instID;
74 }
75 
82 void UAVObject::initializeFields(QList<UAVObjectField *> &fields, quint8 *data, quint32 numBytes)
83 {
84  this->numBytes = numBytes;
85  this->data = data;
86  this->fields = fields;
87  // Initialize fields
88  quint32 offset = 0;
89  for (int n = 0; n < fields.length(); ++n) {
90  fields[n]->initialize(data, offset, this);
91  offset += fields[n]->getNumBytes();
92  connect(fields[n], &UAVObjectField::fieldUpdated, this, &UAVObject::fieldUpdated);
93  }
94 }
95 
99 void UAVObject::fieldUpdated(UAVObjectField *field)
100 {
101  Q_UNUSED(field);
102 }
103 
108 {
109  return objID;
110 }
111 
116 {
117  return instID;
118 }
119 
124 {
125  return isSingleInst;
126 }
127 
132 {
133  return name;
134 }
135 
140 {
141  return description;
142 }
143 
147 void UAVObject::setDescription(const QString &description)
148 {
149  this->description = description;
150 }
151 
156 {
157  return numBytes;
158 }
159 
164 {
165  emit updateRequested(this);
166 }
167 
172 {
173  emit updateAllInstancesRequested(this);
174 }
175 
180 {
181  emit objectUpdatedManual(this);
182  emit objectUpdated(this);
183 }
184 
189 {
190  return fields.count();
191 }
192 
197 {
198  return fields;
199 }
200 
205 {
206 
207  QJsonObject obj, fieldMap;
208 
209  obj["id"] = static_cast<qint64>(getObjID());
210  obj["name"] = getName();
211 
212  foreach (UAVObjectField *field, fields) {
213  quint32 nelem = field->getNumElements();
214 
215  if (nelem > 1) {
216  QVariantList vals;
217  for (unsigned int n = 0; n < nelem; ++n) {
218  vals.append(field->getValue(n));
219  }
220 
221  fieldMap[field->getName()] = QJsonArray::fromVariantList(vals);
222  } else {
223  fieldMap[field->getName()] = QJsonValue::fromVariant(field->getValue(0));
224  }
225  }
226 
227  obj["fields"] = fieldMap;
228 
229  return obj;
230 }
231 
236 UAVObjectField *UAVObject::getField(const QString &name)
237 {
238  // Look for field
239  for (int n = 0; n < fields.length(); ++n) {
240  if (name.compare(fields[n]->getName()) == 0) {
241  return fields[n];
242  }
243  }
244  // If this point is reached then the field was not found
245  qWarning() << "UAVObject::getField Non existant field " << name
246  << " requested. This indicates a bug. Make sure you also have null checking for "
247  "non-debug code.";
248  return NULL;
249 }
250 
255 qint32 UAVObject::pack(quint8 *dataOut)
256 {
257  qint32 offset = 0;
258  for (QList<UAVObjectField *>::iterator iter = fields.begin(); iter != fields.end(); ++iter) {
259  UAVObjectField *field = *iter;
260  field->pack(&dataOut[offset]);
261  offset += field->getNumBytes();
262  }
263  return numBytes;
264 }
265 
270 qint32 UAVObject::unpack(const quint8 *dataIn)
271 {
272  qint32 offset = 0;
273  for (QList<UAVObjectField *>::iterator iter = fields.begin(); iter != fields.end(); ++iter) {
274  UAVObjectField *field = *iter;
275  field->unpack(&dataIn[offset]);
276  offset += field->getNumBytes();
277  }
278  emit objectUnpacked(this); // trigger object updated event
279  emit objectUpdated(this);
280 
281  return numBytes;
282 }
283 
288 {
289  QString sout;
290  sout.append(toStringBrief());
291  sout.append(toStringData());
292  return sout;
293 }
294 
299 {
300  QString sout;
301  sout.append(QString("%1 (ID: %2, InstID: %3, NumBytes: %4, SInst: %5)\n")
302  .arg(getName())
303  .arg(getObjID())
304  .arg(getInstID())
305  .arg(getNumBytes())
306  .arg(isSingleInstance()));
307  return sout;
308 }
309 
314 {
315  QString sout;
316  sout.append("Data:\n");
317  for (QList<UAVObjectField *>::iterator iter = fields.begin(); iter != fields.end(); ++iter) {
318  UAVObjectField *field = *iter;
319  sout.append(QString("\t%1").arg(field->toString()));
320  }
321  return sout;
322 }
323 
328 {
329  emit transactionCompleted(this, success);
330 }
331 
335 void UAVObject::emitTransactionCompleted(bool success, bool nacked)
336 {
337  emit transactionCompleted(this, success, nacked);
338 }
339 
344 {
345  emit newInstance(obj);
346 }
347 
352 {
353  emit instanceRemoved(obj);
354 }
355 
360 void UAVObject::MetadataInitialize(UAVObject::Metadata &metadata)
361 {
362  metadata.flags = ACCESS_READWRITE << UAVOBJ_ACCESS_SHIFT
363  | ACCESS_READWRITE << UAVOBJ_GCS_ACCESS_SHIFT | 1 << UAVOBJ_TELEMETRY_ACKED_SHIFT
364  | 1 << UAVOBJ_GCS_TELEMETRY_ACKED_SHIFT
365  | UPDATEMODE_ONCHANGE << UAVOBJ_TELEMETRY_UPDATE_MODE_SHIFT
366  | UPDATEMODE_ONCHANGE << UAVOBJ_GCS_TELEMETRY_UPDATE_MODE_SHIFT;
367  metadata.flightTelemetryUpdatePeriod = 0;
368  metadata.gcsTelemetryUpdatePeriod = 0;
369  metadata.loggingUpdatePeriod = 0;
370 }
371 
377 UAVObject::AccessMode UAVObject::GetFlightAccess(const UAVObject::Metadata &metadata)
378 {
379  return UAVObject::AccessMode((metadata.flags >> UAVOBJ_ACCESS_SHIFT) & 1);
380 }
381 
387 void UAVObject::SetFlightAccess(UAVObject::Metadata &metadata, UAVObject::AccessMode mode)
388 {
389  SET_BITS(metadata.flags, UAVOBJ_ACCESS_SHIFT, mode, 1);
390 }
391 
397 UAVObject::AccessMode UAVObject::GetGcsAccess(const UAVObject::Metadata &metadata)
398 {
399  return UAVObject::AccessMode((metadata.flags >> UAVOBJ_GCS_ACCESS_SHIFT) & 1);
400 }
401 
407 void UAVObject::SetGcsAccess(UAVObject::Metadata &metadata, UAVObject::AccessMode mode)
408 {
409  SET_BITS(metadata.flags, UAVOBJ_GCS_ACCESS_SHIFT, mode, 1);
410 }
411 
417 quint8 UAVObject::GetFlightTelemetryAcked(const UAVObject::Metadata &metadata)
418 {
419  return (metadata.flags >> UAVOBJ_TELEMETRY_ACKED_SHIFT) & 1;
420 }
421 
427 void UAVObject::SetFlightTelemetryAcked(UAVObject::Metadata &metadata, quint8 val)
428 {
429  SET_BITS(metadata.flags, UAVOBJ_TELEMETRY_ACKED_SHIFT, val, 1);
430 }
431 
437 quint8 UAVObject::GetGcsTelemetryAcked(const UAVObject::Metadata &metadata)
438 {
439  return (metadata.flags >> UAVOBJ_GCS_TELEMETRY_ACKED_SHIFT) & 1;
440 }
441 
447 void UAVObject::SetGcsTelemetryAcked(UAVObject::Metadata &metadata, quint8 val)
448 {
449  SET_BITS(metadata.flags, UAVOBJ_GCS_TELEMETRY_ACKED_SHIFT, val, 1);
450 }
451 
458 {
459  return UAVObject::UpdateMode((metadata.flags >> UAVOBJ_TELEMETRY_UPDATE_MODE_SHIFT)
460  & UAVOBJ_UPDATE_MODE_MASK);
461 }
462 
468 void UAVObject::SetFlightTelemetryUpdateMode(UAVObject::Metadata &metadata,
470 {
471  SET_BITS(metadata.flags, UAVOBJ_TELEMETRY_UPDATE_MODE_SHIFT, val, UAVOBJ_UPDATE_MODE_MASK);
472 }
473 
480 {
481  return UAVObject::UpdateMode((metadata.flags >> UAVOBJ_GCS_TELEMETRY_UPDATE_MODE_SHIFT)
482  & UAVOBJ_UPDATE_MODE_MASK);
483 }
484 
490 void UAVObject::SetGcsTelemetryUpdateMode(UAVObject::Metadata &metadata, UAVObject::UpdateMode val)
491 {
492  SET_BITS(metadata.flags, UAVOBJ_GCS_TELEMETRY_UPDATE_MODE_SHIFT, val, UAVOBJ_UPDATE_MODE_MASK);
493 }
void initializeFields(QList< UAVObjectField * > &fields, quint8 *data, quint32 numBytes)
Definition: uavobject.cpp:82
iter
Definition: OPPlots.m:115
void emitTransactionCompleted(bool success)
Definition: uavobject.cpp:327
qint32 unpack(const quint8 *dataIn)
qint32 unpack(const quint8 *dataIn)
Definition: uavobject.cpp:270
size_t getNumBytes() const
void requestUpdate()
Definition: uavobject.cpp:163
qint32 pack(quint8 *dataOut)
Definition: uavobject.cpp:255
static void SetFlightTelemetryAcked(Metadata &meta, quint8 val)
Definition: uavobject.cpp:427
quint8 * data
Definition: uavobject.h:252
static UpdateMode GetFlightTelemetryUpdateMode(const Metadata &meta)
Definition: uavobject.cpp:457
static void SetFlightAccess(Metadata &meta, AccessMode mode)
Definition: uavobject.cpp:387
QString description
Definition: uavobject.h:249
QVariant getValue(int index=0) const
QList< UAVObjectField * > fields
Definition: uavobject.h:253
static void SetGcsTelemetryAcked(Metadata &meta, quint8 val)
Definition: uavobject.cpp:447
void objectUnpacked(UAVObject *obj)
objectUnpacked: triggered whenever an object is unpacked (i.e. arrives from the telemetry link) ...
static UpdateMode GetGcsTelemetryUpdateMode(const Metadata &meta)
Definition: uavobject.cpp:479
static quint8 GetFlightTelemetryAcked(const Metadata &meta)
Definition: uavobject.cpp:417
int getNumElements() const
bool isSingleInst
Definition: uavobject.h:247
void emitInstanceRemoved(UAVObject *)
Definition: uavobject.cpp:351
QJsonObject getJsonRepresentation()
Definition: uavobject.cpp:204
void objectUpdated(UAVObject *obj)
Signal sent whenever any field of the object is updated.
QString name
Definition: uavobject.h:248
static void SetGcsAccess(Metadata &meta, AccessMode mode)
Definition: uavobject.cpp:407
quint32 getInstID()
Definition: uavobject.cpp:115
DataFields data
static void SetFlightTelemetryUpdateMode(Metadata &meta, UpdateMode val)
Definition: uavobject.cpp:468
QString getDescription()
Definition: uavobject.cpp:139
UAVObject(quint32 objID, bool isSingleInst, const QString &name)
Definition: uavobject.cpp:60
void transactionCompleted(UAVObject *obj, bool success)
transactionCompleted. Triggered by a call to emitTransactionCompleted - done in telemetry.cpp whenever a transaction finishes.
bool isSingleInstance()
Definition: uavobject.cpp:123
qint32 getNumFields()
Definition: uavobject.cpp:188
void updated()
Definition: uavobject.cpp:179
UAVObjectField * getField(const QString &name)
Definition: uavobject.cpp:236
QString toStringBrief()
Definition: uavobject.cpp:298
Eccentricity n
Definition: OPPlots.m:137
QString getName() const
quint32 getObjID()
Definition: uavobject.cpp:107
QList< UAVObjectField * > getFields()
Definition: uavobject.cpp:196
void updateRequested(UAVObject *obj)
updateRequested
void newInstance(UAVObject *obj)
newInstance
QString toString() const
void updateAllInstancesRequested(UAVObject *obj)
updateAllInstancesRequested
quint32 instID
Definition: uavobject.h:246
void emitNewInstance(UAVObject *)
Definition: uavobject.cpp:343
quint32 getNumBytes()
Definition: uavobject.cpp:155
quint32 objID
Definition: uavobject.h:245
QString toString()
Definition: uavobject.cpp:287
QString getName()
Definition: uavobject.cpp:131
void initialize(quint32 instID)
Definition: uavobject.cpp:71
static AccessMode GetFlightAccess(const Metadata &meta)
Definition: uavobject.cpp:377
void fieldUpdated(UAVObjectField *field)
qint32 pack(quint8 *dataOut)
static void SetGcsTelemetryUpdateMode(Metadata &meta, UpdateMode val)
Definition: uavobject.cpp:490
void setDescription(const QString &description)
Definition: uavobject.cpp:147
QString toStringData()
Definition: uavobject.cpp:313
quint32 numBytes
Definition: uavobject.h:251
void objectUpdatedManual(UAVObject *obj)
objectUpdatedManual: triggered only from the "updated" slot in uavobject The telemetry manager listen...
void requestUpdateAllInstances()
Definition: uavobject.cpp:171
static void MetadataInitialize(Metadata &meta)
Definition: uavobject.cpp:360
static quint8 GetGcsTelemetryAcked(const Metadata &meta)
Definition: uavobject.cpp:437
static AccessMode GetGcsAccess(const Metadata &meta)
Definition: uavobject.cpp:397
void instanceRemoved(UAVObject *obj)
instance removed from manager