From b3fb5b44d76b79742dc42fb57c1a9ad216481522 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Simon=20St=C3=BCrz?= Date: Mon, 28 Jun 2021 13:41:12 +0200 Subject: [PATCH] Make use of system data location for searching the mac address database --- data/mac-database/README.md | 15 ++++++++++++--- debian/nymea-data.install | 2 +- libnymea/network/macaddressdatabase.cpp | 23 +++++++++++++++++++++++ 3 files changed, 36 insertions(+), 4 deletions(-) diff --git a/data/mac-database/README.md b/data/mac-database/README.md index 778bd849..dc2f77bd 100644 --- a/data/mac-database/README.md +++ b/data/mac-database/README.md @@ -1,12 +1,21 @@ # Building the MAC address database -The MAC address database can be created using the `build-database.py` script. The script will download the latest registered MAC address block information from [https://macaddress.io](https://macaddress.io) and creates a SQLITE database file. +The MAC address database can be created using the `build-database.py` script. +The script will download the latest registered MAC address block information +from [https://macaddress.io](https://macaddress.io) and creates a size and access optimized +SQLITE database file. -The generated database is read performance optimized and tried to keep as small as possible for searching MAC address OUIs (Organizationally Unique Identifiers) blocks and returning the registered company name. +The generated database is read performance optimized and tried to keep as small as possible for +searching MAC address OUIs (Organizationally Unique Identifiers) blocks and returning the registered company name. $ python3 build-database.py The final database will be named `mac-addresses.db`. -In nymea the `MacAddressDatabase` will search by default for this database file in `/usr/share/nymea/mac-addresses.db` and provides an asynch threaded mechanism to get the company name for a given MAC address. +In nymea the `MacAddressDatabase` class will provide access to this generated database and provides an asynch threaded mechanism to +get the company name for a given MAC address. + +The database will be searched in the system default data location `${XDG_DATA_DIRS}/nymead/`. + +On debian package based system the database file will be installed in `/usr/share/nymea/nymead/mac-addresses.db`. diff --git a/debian/nymea-data.install b/debian/nymea-data.install index 2257e466..df0d954c 100644 --- a/debian/nymea-data.install +++ b/debian/nymea-data.install @@ -1,2 +1,2 @@ -data/mac-database/mac-addresses.db usr/share/nymea/ +data/mac-database/mac-addresses.db usr/share/nymea/nymead/ diff --git a/libnymea/network/macaddressdatabase.cpp b/libnymea/network/macaddressdatabase.cpp index 15ae6652..b175573b 100644 --- a/libnymea/network/macaddressdatabase.cpp +++ b/libnymea/network/macaddressdatabase.cpp @@ -36,12 +36,34 @@ #include #include #include +#include #include NYMEA_LOGGING_CATEGORY(dcMacAddressDatabase, "MacAddressDatabase") MacAddressDatabase::MacAddressDatabase(QObject *parent) : QObject(parent) { + // Find database in system data locations + QString databaseFileName; + foreach (const QString &dataLocation, QStandardPaths::standardLocations(QStandardPaths::DataLocation)) { + QFileInfo databaseFileInfo(dataLocation + QDir::separator() + "mac-addresses.db"); + if (!databaseFileInfo.exists()) { + continue; + } + + databaseFileName = databaseFileInfo.absoluteFilePath(); + break; + } + + if (databaseFileName.isEmpty()) { + qCWarning(dcMacAddressDatabase()) << "Could not find the mac address database in any system data location paths" << QStandardPaths::standardLocations(QStandardPaths::DataLocation); + qCWarning(dcMacAddressDatabase()) << "The mac address database lookup feature will not be available."; + return; + } + + + m_databaseName = databaseFileName; + m_available = initDatabase(); if (m_available) { m_futureWatcher = new QFutureWatcher(this); @@ -90,6 +112,7 @@ MacAddressDatabaseReply *MacAddressDatabase::lookupMacAddress(const QString &mac bool MacAddressDatabase::initDatabase() { + qCDebug(dcMacAddressDatabase()) << "Starting to initialize the mac address database:" << m_databaseName; m_connectionName = QFileInfo(m_databaseName).baseName(); m_db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"), m_connectionName); m_db.setDatabaseName(m_databaseName);