From 21ea4532c8ea914fa4d554920d0b9ca6efb1baf1 Mon Sep 17 00:00:00 2001 From: Oliver Toth Date: Sun, 19 Feb 2023 21:14:05 +0100 Subject: [PATCH] [BLE] Remove empty service during integrity check Remove empty service during integrity check from #1127 + Remove empty parent if there is no renamed child for the given service. --- src/MPDevice.cpp | 43 +++++++++++++++++++++++++++++++++++++++++-- src/MPDevice.h | 1 + 2 files changed, 42 insertions(+), 2 deletions(-) diff --git a/src/MPDevice.cpp b/src/MPDevice.cpp index a6adcaa3..993591d2 100755 --- a/src/MPDevice.cpp +++ b/src/MPDevice.cpp @@ -2993,6 +2993,35 @@ bool MPDevice::removeEmptyParentFromDB(MPNode* parentNodePt, bool isDataParent, } } +bool MPDevice::removeEmptyServiceNode() +{ + bool return_bool = true; + MPNode* tempNextParentNodePt = nullptr; + QByteArray tempParentAddress = startNode[Common::CRED_ADDR_IDX]; + quint32 tempVirtualParentAddress = virtualStartNode[Common::CRED_ADDR_IDX]; + /* Loop through the parent nodes */ + while ((tempParentAddress != MPNode::EmptyAddress) || (tempParentAddress.isNull() && tempVirtualParentAddress != 0)) + { + tempNextParentNodePt = findNodeWithAddressInList(loginNodes, tempParentAddress, tempVirtualParentAddress); + if (!tempNextParentNodePt) + { + break; + } + if (MPNode::EmptyAddress == tempNextParentNodePt->getStartChildAddress()) + { + qWarning() << "Parent " << tempNextParentNodePt->getService() << " has no children, erasing."; + MPNode *toDelete = tempNextParentNodePt; + tempParentAddress = tempNextParentNodePt->getNextParentAddress(); + tempVirtualParentAddress = tempNextParentNodePt->getNextParentVirtualAddress(); + return_bool = !removeEmptyParentFromDB(toDelete, false); + continue; + } + tempParentAddress = tempNextParentNodePt->getNextParentAddress(); + tempVirtualParentAddress = tempNextParentNodePt->getNextParentVirtualAddress(); + } + return return_bool; +} + bool MPDevice::removeChildFromDB(MPNode* parentNodePt, MPNode* childNodePt, bool deleteEmptyParent, bool deleteFromList, Common::AddressType addrType /*= Common::CRED_ADDR_IDX*/) { if (deleteFromList) @@ -3161,6 +3190,10 @@ bool MPDevice::checkLoadedNodes(bool checkCredentials, bool checkData, bool repa /* Tag pointed nodes, also detects DB errors */ if (checkCredentials || checkData) { + if (checkCredentials && repairAllowed) + { + return_bool &= removeEmptyServiceNode(); + } return_bool &= tagPointedNodes(checkCredentials, checkData, repairAllowed); } @@ -8122,13 +8155,19 @@ void MPDevice::setMMCredentials(const QJsonArray &creds, bool noDelete, } } } - removeChildFromDB(nodeItem, curNode, false, true); - packet_send_needed = true; + if (renamedNode) { loginChildNodes.append(renamedNode); addChildToDB(nodeItem, renamedNode); + removeChildFromDB(nodeItem, curNode, false, true); + } + else + { + //If not renamed node delete empty parent + removeChildFromDB(nodeItem, curNode, true, true); } + packet_send_needed = true; } } } diff --git a/src/MPDevice.h b/src/MPDevice.h index c3632c70..94ff76ca 100644 --- a/src/MPDevice.h +++ b/src/MPDevice.h @@ -355,6 +355,7 @@ private slots: Common::DataAddressType dataAddrType = Common::DATA_ADDR_IDX); bool removeEmptyParentFromDB(MPNode* parentNodePt, bool isDataParent, Common::AddressType addrType = Common::CRED_ADDR_IDX, Common::DataAddressType dataAddrType = Common::DATA_ADDR_IDX); + bool removeEmptyServiceNode(); bool readExportFile(const QByteArray &fileData, QString &errorString); void readExportNodes(QJsonArray &&nodes, ExportPayloadData id, bool fromMiniToBle = false, bool isData = false); void readExportDataChildNodes(QJsonArray &&nodes, ExportPayloadData id, bool fromMiniToBle = false);