35 #include "physical_constants.h"
41 #include <QRegularExpression>
42 #include <objectpersistence.h>
44 #include "firmwareiapobj.h"
45 #include "homelocation.h"
46 #include "gpsposition.h"
47 #include "actuatorsettings.h"
51 #ifdef UAVOBJECTUTIL_DEBUG
52 #define UAVOBJECTUTIL_QXTLOG_DEBUG(...) qDebug() << __VA_ARGS__
53 #else // UAVOBJECTUTIL_DEBUG
54 #define UAVOBJECTUTIL_QXTLOG_DEBUG(...)
55 #endif // UAVOBJECTUTIL_DEBUG
61 failureTimer.setSingleShot(
true);
62 failureTimer.setInterval(1000);
63 connect(&failureTimer, SIGNAL(timeout()),
this, SLOT(objectPersistenceOperationFailed()));
67 pm = ExtensionSystem::PluginManager::instance();
115 UAVOBJECTUTIL_QXTLOG_DEBUG(QString(
"Enqueue object:%0").arg(obj->
getName()));
119 if (queue.length() == 1)
128 void UAVObjectUtilManager::saveNextObject()
130 if (queue.isEmpty()) {
134 Q_ASSERT(saveState == IDLE);
139 UAVOBJECTUTIL_QXTLOG_DEBUG(QString(
"Send save object request to board %0").arg(obj->
getName()));
141 ObjectPersistence *objectPersistence = ObjectPersistence::GetInstance(
getObjectManager());
142 Q_ASSERT(objectPersistence);
151 connect(objectPersistence, SIGNAL(transactionCompleted(
UAVObject *,
bool)),
this,
152 SLOT(objectPersistenceTransactionCompleted(
UAVObject *,
bool)), Qt::UniqueConnection);
158 connect(objectPersistence, SIGNAL(objectUpdated(
UAVObject *)),
this,
159 SLOT(objectPersistenceUpdated(
UAVObject *)), Qt::UniqueConnection);
160 saveState = AWAITING_ACK;
161 UAVOBJECTUTIL_QXTLOG_DEBUG(QString(
"[saveObjectToFlash] Moving on to AWAITING_ACK"));
163 ObjectPersistence::DataFields
data;
164 data.Operation = ObjectPersistence::OPERATION_SAVE;
167 objectPersistence->setData(data);
168 objectPersistence->updated();
193 void UAVObjectUtilManager::objectPersistenceTransactionCompleted(
UAVObject *obj,
bool success)
196 Q_ASSERT(obj->
getName().compare(
"ObjectPersistence") == 0);
197 Q_ASSERT(saveState == AWAITING_ACK);
206 saveState = AWAITING_COMPLETED;
207 UAVOBJECTUTIL_QXTLOG_DEBUG(QString(
"[saveObjectToFlash] Moving on to AWAITING_COMPLETED"));
208 disconnect(obj, SIGNAL(transactionCompleted(
UAVObject *,
bool)),
this,
209 SLOT(objectPersistenceTransactionCompleted(
UAVObject *,
bool)));
210 failureTimer.start(2000);
213 UAVOBJECTUTIL_QXTLOG_DEBUG(QString(
"objectPersistenceTranscationCompleted (error))"));
214 ObjectPersistence *objectPersistence = ObjectPersistence::GetInstance(
getObjectManager());
215 Q_ASSERT(objectPersistence);
217 objectPersistence->disconnect(
this);
232 void UAVObjectUtilManager::objectPersistenceOperationFailed()
234 if (saveState == AWAITING_COMPLETED) {
236 ObjectPersistence *objectPersistence = ObjectPersistence::GetInstance(
getObjectManager());
237 Q_ASSERT(objectPersistence);
242 objectPersistence->disconnect(
this);
256 void UAVObjectUtilManager::objectPersistenceUpdated(
UAVObject *obj)
259 Q_ASSERT(obj->
getObjID() == ObjectPersistence::OBJID);
261 if (saveState != AWAITING_COMPLETED) {
266 ObjectPersistence::DataFields objectPersistence =
267 (
static_cast<ObjectPersistence *
>(obj))->getData();
269 if (objectPersistence.Operation == ObjectPersistence::OPERATION_ERROR) {
271 objectPersistenceOperationFailed();
272 }
else if (objectPersistence.Operation == ObjectPersistence::OPERATION_COMPLETED) {
276 if (objectPersistence.ObjectID != savingObj->
getObjID()) {
277 objectPersistenceOperationFailed();
281 obj->disconnect(
this);
284 UAVOBJECTUTIL_QXTLOG_DEBUG(QString(
"[saveObjectToFlash] Object save succeeded"));
305 QMap<QString, UAVObject::Metadata>
308 QMap<QString, UAVObject::Metadata> metaDataList;
313 foreach (QVector<UAVDataObject *> list, objList) {
315 bool updateMetadataFlag =
false;
316 switch (metadataReadType) {
318 updateMetadataFlag =
true;
322 updateMetadataFlag =
true;
327 updateMetadataFlag =
true;
332 if (updateMetadataFlag) {
348 QMap<QString, UAVObject::Metadata> metaDataList)
366 if (!metadataSendlist.isEmpty())
370 foreach (QString str, metaDataSetList.keys()) {
372 bool updateMetadataFlag =
false;
373 switch (metadataSetType) {
375 updateMetadataFlag =
true;
379 updateMetadataFlag =
true;
384 updateMetadataFlag =
true;
392 UAVOBJECTUTIL_QXTLOG_DEBUG(QString(
"Enqueued %0").arg(obj->
getName()));
393 metadataSendlist.insert(obj, metaDataSetList.value(str));
396 metadataSendSuccess =
true;
397 if (metadataSendlist.isEmpty()) {
405 SLOT(metadataTransactionCompleted(
UAVObject *,
bool)), Qt::UniqueConnection);
417 QMap<QString, UAVObject::Metadata> metaDataList;
422 foreach (QVector<UAVDataObject *> list, objList) {
430 bool ret =
setMetadata(metaDataList, metadataSetType);
441 void UAVObjectUtilManager::metadataTransactionCompleted(
UAVObject *uavoObject,
bool success)
447 if (metadataSendlist.contains(dobj)) {
449 UAVOBJECTUTIL_QXTLOG_DEBUG(
450 QString(
"Writing metadata succeded on %0").arg(uavoObject->
getName()));
453 metadataSendSuccess =
false;
454 UAVOBJECTUTIL_QXTLOG_DEBUG(
455 QString(
"metadata send failed").arg(metadataSendlist.keys().first()->getName()));
457 disconnect(mobj, SIGNAL(transactionCompleted(
UAVObject *,
bool)),
this,
458 SLOT(metadataTransactionCompleted(
UAVObject *,
bool)));
459 metadataSendlist.take(dobj);
460 if (!metadataSendlist.keys().isEmpty()) {
461 metadataSendlist.keys().first()->setMetadata(
462 metadataSendlist.value(metadataSendlist.keys().first()));
463 connect(metadataSendlist.keys().first()->getMetaObject(),
464 SIGNAL(transactionCompleted(
UAVObject *,
bool)),
this,
465 SLOT(metadataTransactionCompleted(
UAVObject *,
bool)), Qt::UniqueConnection);
478 FirmwareIAPObj::DataFields dummy = {};
480 FirmwareIAPObj *firmwareIap = FirmwareIAPObj::GetInstance(obm);
481 Q_ASSERT(firmwareIap);
485 return firmwareIap->getData();
498 int ret = firmwareIapData.BoardType << 8;
499 ret = ret + firmwareIapData.BoardRevision;
507 int ret = firmwareIapData.BoardRevision;
528 QByteArray cpuSerial;
531 for (
unsigned int i = 0;
i < FirmwareIAPObj::CPUSERIAL_NUMELEM;
i++)
532 cpuSerial.append(firmwareIapData.CPUSerial[
i]);
540 return firmwareIapData.crc;
551 for (
unsigned int i = 0;
i < FirmwareIAPObj::DESCRIPTION_NUMELEM;
i++)
552 ret.append(firmwareIapData.Description[
i]);
565 Q_ASSERT(retval >= 0);
573 HomeLocation *homeLocation = HomeLocation::GetInstance(obm);
574 Q_ASSERT(homeLocation != NULL);
576 HomeLocation::DataFields homeLocationData = homeLocation->getData();
577 homeLocationData.Latitude = LLA[0] * 1e7;
578 homeLocationData.Longitude = LLA[1] * 1e7;
579 homeLocationData.Altitude = LLA[2];
581 homeLocationData.Be[0] = Be[0];
582 homeLocationData.Be[1] = Be[1];
583 homeLocationData.Be[2] = Be[2];
585 homeLocationData.Set = HomeLocation::SET_TRUE;
587 homeLocation->setData(homeLocationData);
597 HomeLocation *homeLocation = HomeLocation::GetInstance(obm);
598 Q_ASSERT(homeLocation != NULL);
600 HomeLocation::DataFields homeLocationData = homeLocation->getData();
602 set = homeLocationData.Set;
604 LLA[0] = homeLocationData.Latitude * 1
e-7;
605 LLA[1] = homeLocationData.Longitude * 1
e-7;
606 LLA[2] = homeLocationData.Altitude;
608 if (LLA[0] != LLA[0])
610 else if (LLA[0] > 90)
612 else if (LLA[0] < -90)
615 if (LLA[1] != LLA[1])
617 else if (LLA[1] > 180)
619 else if (LLA[1] < -180)
622 if (LLA[2] != LLA[2])
633 GPSPosition *gpsPosition = GPSPosition::GetInstance(obm);
634 Q_ASSERT(gpsPosition != NULL);
636 GPSPosition::DataFields gpsPositionData = gpsPosition->getData();
638 LLA[0] = gpsPositionData.Latitude;
639 LLA[1] = gpsPositionData.Longitude;
640 LLA[2] = gpsPositionData.Altitude;
642 if (LLA[0] != LLA[0])
644 else if (LLA[0] > 90)
646 else if (LLA[0] < -90)
649 if (LLA[1] != LLA[1])
651 else if (LLA[1] > 180)
653 else if (LLA[1] < -180)
656 if (LLA[2] != LLA[2])
670 if (desc.startsWith(
"TlFw") || desc.startsWith(
"OpFw")) {
686 quint32 gitCommitHash = desc.at(7) & 0xFF;
687 for (
int i = 0;
i < 4;
i++) {
688 gitCommitHash = gitCommitHash << 8;
689 gitCommitHash += desc.at(7 -
i) & 0xFF;
691 struc.
gitHash = QString(
"%1").arg(gitCommitHash, 8, 16, QChar(
'0'));
693 quint64 gitAncestorHash = 0;
694 for (
int i = 0;
i < 8;
i++) {
695 gitAncestorHash = gitAncestorHash << 8;
696 gitAncestorHash += desc.at(87 -
i) & 0xFF;
698 struc.
nextAncestor = QString(
"%1").arg(gitAncestorHash, 16, 16, QChar(
'0'));
700 quint32 gitDate = desc.at(11) & 0xFF;
701 for (
int i = 1;
i < 4;
i++) {
702 gitDate = gitDate << 8;
703 gitDate += desc.at(11 -
i) & 0xFF;
705 struc.
gitDate = QDateTime::fromTime_t(gitDate).toUTC().toString(
"yyyyMMdd HH:mm");
707 QString gitTag = QString(desc.mid(14, 26));
711 QByteArray targetPlatform = desc.mid(12, 2);
712 struc.
boardType = (int)targetPlatform.at(0);
714 struc.
fwHash = desc.mid(40, 20);
719 QRegularExpression certifiedPattern(
"^Release-20[0-9]{6}(\\.[0-9])?$");
720 if (certifiedPattern.match(struc.
gitTag).hasMatch())
760 auto actuatorSettings = ActuatorSettings::GetInstance(obm);
761 if (!actuatorSettings)
764 if (!actuatorSettings->getIsPresentOnHardware())
767 auto minField = actuatorSettings->getField(
"ChannelMin");
768 auto maxField = actuatorSettings->getField(
"ChannelMax");
769 if (!minField || !maxField)
772 for (
auto i = 0;
i < maxField->getNumElements();
i++) {
773 if (maxField->getValue(
i).toInt() > 0)
777 for (
auto i = 0;
i < minField->getNumElements();
i++) {
778 if (minField->getValue(
i).toInt() > 0)
virtual Metadata getDefaultMetadata()=0
void completedMetadataWrite(bool)
QMap< QString, UAVObject::Metadata > readMetadata(metadataSetEnum metadataReadType)
UAVObjectUtilManager::readMetadata Get metadata for UAVOs.
void setMetadata(const Metadata &mdata)
QByteArray getBoardDescription()
UAVObject * getParentObject()
QString getFirmwareHash()
bool resetMetadataToDefaults()
UAVObjectUtilManager::resetMetadata Resets all metadata to defaults (from XML definitions) ...
int getBoardType()
Get the board type number.
QMap< QString, UAVObject::Metadata > readAllNonSettingsMetadata()
UAVObjectUtilManager::readAllNonSettingsMetadata Convenience function for calling readMetadata...
const char *const GCS_REVISION_SHORT_STR
UAVMetaObject * getMetaObject()
bool setAllNonSettingsMetadata(QMap< QString, UAVObject::Metadata >)
UAVObjectUtilManager::setAllNonSettingsMetadata Convenience function for calling setMetadata.
bool setMetadata(QMap< QString, UAVObject::Metadata >, metadataSetEnum metadataUpdateType)
UAVObjectUtilManager::setMetadata Sets the metadata for all metadata in QMap.
void saveObjectToFlash(UAVObject *obj)
UAVObjectUtilManager::saveObjectToSD Add a new object to save in the queue.
bool firmwareHashMatchesGcs()
Check if firmware version hash matches GCS version hash.
int getGPSPosition(double LLA[3])
UAVObjectManager * getObjectManager()
bool getIsPresentOnHardware() const
static bool descriptionToStructure(QByteArray desc, deviceDescriptorStruct &struc)
int setHomeLocation(double LLA[3], bool save_to_sdcard)
void saveCompleted(int objectID, bool status)
int getDetails(double LLA[3], double Be[3])
Get local magnetic field.
bool getBoardDescriptionStruct(deviceDescriptorStruct &device)
FirmwareIAPObj::DataFields getFirmwareIap()
UAVObject * getObject(const QString &name, quint32 instId=0)
QVector< QVector< UAVDataObject * > > getDataObjectsVector()
Core::IBoardType * getBoardType()
Get the IBoardType corresponding to the connected board.
QByteArray getBoardCPUSerial()
bool boardConfigured()
Check if the board has been configured for flight The heuristic is whether one or more actuators are ...
int getBoardRevision()
Get the connected board hardware revision.
int getHomeLocation(bool &set, double LLA[3])