From 88952215266ee1a2ffb96855ba5aec10e7280b14 Mon Sep 17 00:00:00 2001 From: Michael Zanetti Date: Tue, 16 Oct 2018 22:03:20 +0200 Subject: [PATCH] add a config file in /etc/nymea/nymea-networkmanager.conf options in config file can still be overridden with the command line arguments that existed all along. --- .gitignore | 35 ------ README.md | 12 ++ debian/changelog | 11 +- debian/nymea-networkmanager.install.in | 1 + nymea-networkmanager.conf | 5 + nymea-networkmanager.service | 2 +- .../bluetooth/networkservice.cpp | 5 +- nymea-networkmanager/core.cpp | 10 -- nymea-networkmanager/core.h | 5 - nymea-networkmanager/main.cpp | 108 +++++++++++++----- 10 files changed, 108 insertions(+), 86 deletions(-) create mode 100644 nymea-networkmanager.conf diff --git a/.gitignore b/.gitignore index 4639a49..da762a5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,38 +1,3 @@ -# C++ objects and libs - -*.slo -*.lo -*.o -*.a -*.la -*.lai -*.so -*.dll -*.dylib - -# Qt-es - -/.qmake.cache -/.qmake.stash *.pro.user -*.pro.user.* -*.qbs.user -*.qbs.user.* -*.moc -moc_*.cpp -moc_*.h -qrc_*.cpp -ui_*.h -Makefile* -*build-* - -# QtCreator - *.autosave -# QtCtreator Qml -*.qmlproject.user -*.qmlproject.user.* - -# QtCtreator CMake -CMakeLists.txt.user* diff --git a/README.md b/README.md index a113944..501cf32 100644 --- a/README.md +++ b/README.md @@ -3,6 +3,18 @@ This daemon allows to set up the wireless network using a bluetooth LE connection. The daemon will automatically start a bluetooth low energy server if the system is currently not connected to any network. Once the system is connected, the daemon will shutdown the bluetooth server. +# Config file + +nymea-networkmanager will search for a config file in the following locations (in this order): + +~/.config/nymea/nymea-networkmanager.conf +/etc/nymea/nymea-networkmanager.conf + +If such a config file is found, it reads values from there. There is a example config in +this repository and it will be installed to /etc/nymea-networkmanager.conf with the dpkg package. + +Note: Command line parameters will have higher priority than entries in the configuration file. + # Command line parameters $ nymea-network-manager --help diff --git a/debian/changelog b/debian/changelog index 2fa7fd7..ae7bf05 100644 --- a/debian/changelog +++ b/debian/changelog @@ -1,5 +1,12 @@ -nymea-networkmanager (0.0.1) UNRELEASED; urgency=medium +nymea-networkmanager (0.2.0) UNRELEASED; urgency=medium + * Add support for a config file + + -- Michael Zanetti Tue, 16 Oct 2018 22:10:51 +0200 + +nymea-networkmanager (0.0.1) bionic; urgency=medium + + [ Simon Stürz ] * Initial release. - -- Simon Stürz Mon, 05 Mar 2018 15:05:40 +0100 + -- Michael Zanetti Tue, 16 Oct 2018 22:10:45 +0200 diff --git a/debian/nymea-networkmanager.install.in b/debian/nymea-networkmanager.install.in index 176cca0..ed08cbe 100644 --- a/debian/nymea-networkmanager.install.in +++ b/debian/nymea-networkmanager.install.in @@ -1,2 +1,3 @@ usr/bin/nymea-networkmanager nymea-networkmanager.service /lib/systemd/system/ +nymea-networkmanager.conf /etc/nymea/ diff --git a/nymea-networkmanager.conf b/nymea-networkmanager.conf new file mode 100644 index 0000000..7a81a10 --- /dev/null +++ b/nymea-networkmanager.conf @@ -0,0 +1,5 @@ +[General] +Mode=offline +Timeout=60 +AdvertiseName=BT WLAN setup +PlatformName=nymea-box diff --git a/nymea-networkmanager.service b/nymea-networkmanager.service index 0bcbe24..73ccc7d 100644 --- a/nymea-networkmanager.service +++ b/nymea-networkmanager.service @@ -4,7 +4,7 @@ Documentation=https://github.com/guh/nymea-networkmanager After=network.target [Service] -ExecStart=/usr/bin/nymea-networkmanager -d -a nymea -p nymea-box +ExecStart=/usr/bin/nymea-networkmanager -d StandardOutput=journal StandardError=journal Restart=on-failure diff --git a/nymea-networkmanager/bluetooth/networkservice.cpp b/nymea-networkmanager/bluetooth/networkservice.cpp index b7a8d33..efd6da9 100644 --- a/nymea-networkmanager/bluetooth/networkservice.cpp +++ b/nymea-networkmanager/bluetooth/networkservice.cpp @@ -257,7 +257,7 @@ NetworkService::NetworkServiceCommand NetworkService::verifyCommand(const QByteA if (commandData.length() != 1) return NetworkServiceCommandInvalid; - uint commandInteger = commandData.toHex().toUInt(0, 16); + uint commandInteger = commandData.toHex().toUInt(nullptr, 16); switch (commandInteger) { case NetworkServiceCommandEnableNetworking: return NetworkServiceCommandEnableNetworking; @@ -345,9 +345,6 @@ void NetworkService::serviceError(const QLowEnergyService::ServiceError &error) case QLowEnergyService::UnknownError: errorString = "Unknown error"; break; - default: - errorString = "Unhandled error"; - break; } qCWarning(dcBluetoothServer()) << "NetworkService: Error:" << errorString; diff --git a/nymea-networkmanager/core.cpp b/nymea-networkmanager/core.cpp index e51ad61..a339be9 100644 --- a/nymea-networkmanager/core.cpp +++ b/nymea-networkmanager/core.cpp @@ -97,16 +97,6 @@ void Core::setAdvertisingTimeout(const int advertisingTimeout) m_advertisingTimeout = advertisingTimeout; } -bool Core::testingEnabled() const -{ - return m_testing; -} - -void Core::setTestingEnabled(bool testing) -{ - m_testing = testing; -} - void Core::run() { // Start the networkmanager diff --git a/nymea-networkmanager/core.h b/nymea-networkmanager/core.h index a9c330e..c44a6ae 100644 --- a/nymea-networkmanager/core.h +++ b/nymea-networkmanager/core.h @@ -59,9 +59,6 @@ public: int advertisingTimeout() const; void setAdvertisingTimeout(const int advertisingTimeout); - bool testingEnabled() const; - void setTestingEnabled(bool testing); - void run(); private: @@ -82,8 +79,6 @@ private: QString m_platformName; int m_advertisingTimeout = 60; - bool m_testing = false; - void evaluateNetworkManagerState(const NetworkManager::NetworkManagerState &state); void startService(); diff --git a/nymea-networkmanager/main.cpp b/nymea-networkmanager/main.cpp index 4ee4ca9..2d43d34 100644 --- a/nymea-networkmanager/main.cpp +++ b/nymea-networkmanager/main.cpp @@ -22,6 +22,10 @@ #include #include #include +#include +#include +#include +#include #include "core.h" #include "application.h" @@ -77,10 +81,17 @@ int main(int argc, char *argv[]) { qInstallMessageHandler(consoleLogHandler); + // Default configuration: + Core::Mode mode = Core::ModeOffline; + int timeout = 60; + QString advertiseName = "BT WLAN setup"; + QString platformName = "nymea-box"; + + Application application(argc, argv); application.setApplicationName("nymea-networkmanager"); application.setOrganizationName("nymea"); - application.setApplicationVersion("0.1.0"); + application.setApplicationVersion("0.2.0"); // Command line parser QCommandLineParser parser; @@ -92,16 +103,16 @@ int main(int argc, char *argv[]) QCommandLineOption debugOption(QStringList() << "d" << "debug", "Enable more debug output."); parser.addOption(debugOption); - QCommandLineOption advertiseNameOption(QStringList() << "a" << "advertise-name", "The name of the bluetooth server. Default \"nymea\".", "NAME"); - advertiseNameOption.setDefaultValue("nymea"); + QCommandLineOption advertiseNameOption(QStringList() << "a" << "advertise-name", QString("The name of the bluetooth server. Default \"%1\".").arg(advertiseName), "NAME"); + advertiseNameOption.setDefaultValue(advertiseName); parser.addOption(advertiseNameOption); - QCommandLineOption platformNameOption(QStringList() << "p" << "platform-name", "The name of the platform this daemon is running. Default \"nymea-box\".", "NAME"); - platformNameOption.setDefaultValue("nymea-box"); + QCommandLineOption platformNameOption(QStringList() << "p" << "platform-name", QString("The name of the platform this daemon is running. Default \"%1\".").arg(platformName), "NAME"); + platformNameOption.setDefaultValue(platformName); parser.addOption(platformNameOption); - QCommandLineOption timeoutOption(QStringList() << "t" << "timeout", "The timeout of the bluetooth server. Minimum value is 10. Default \"60\".", "SECONDS"); - timeoutOption.setDefaultValue("60"); + QCommandLineOption timeoutOption(QStringList() << "t" << "timeout", QString("The timeout of the bluetooth server. Minimum value is 10. Default \"%1\".").arg(timeout), "SECONDS"); + timeoutOption.setDefaultValue(QString::number(timeout)); parser.addOption(timeoutOption); QCommandLineOption modeOption(QStringList() << "m" << "mode", "Run the daemon in a specific mode. Default \"offline\".\n\n" \ @@ -110,9 +121,6 @@ int main(int argc, char *argv[]) "- start: this mode starts the bluetooth server for 3 minutes on start and shuts down after a connection.\n\n", "offline | always | start"); parser.addOption(modeOption); - QCommandLineOption testingOption(QStringList() << "t" << "testing", "Advertise the bluetoothserver alyways for testing."); - parser.addOption(testingOption); - parser.process(application); // Enable debug categories @@ -123,21 +131,45 @@ int main(int argc, char *argv[]) QLoggingCategory::installFilter(loggingCategoryFilter); - bool timeoutValueOk = false; - int timeout = parser.value(timeoutOption).toInt(&timeoutValueOk); + bool timeoutValueOk = true; - if (!timeoutValueOk) { - qCCritical(dcApplication()) << QString("Invalid timeout value passed: \"%1\". Please pass an integer >= 10").arg(parser.value(timeoutOption)); - parser.showHelp(1); + // Now read the cofig file, overriding defaults + QStringList configLocations; + configLocations << QStandardPaths::standardLocations(QStandardPaths::ConfigLocation); + configLocations << "/etc"; + QString fileName = "/nymea/nymea-networkmanager.conf"; + foreach (const QString &configLocation, configLocations) { + if (QFileInfo::exists(configLocation + fileName)) { + qCDebug(dcApplication) << "Using configuration file from:" << configLocation + fileName; + QSettings settings(configLocation + fileName, QSettings::IniFormat); + qCDebug(dcApplication()) << "Fooo" << settings.allKeys() << settings.childGroups(); + + + if (settings.contains("Mode")) { + if (settings.value("Mode").toString().toLower() == "offline") { + mode = Core::ModeOffline; + } else if (settings.value("Mode").toString().toLower() == "always") { + mode = Core::ModeAlways; + } else if (settings.value("Mode").toString().toLower() == "start") { + mode = Core::ModeStart; + } else { + qCWarning(dcApplication()).noquote() << QString("The config file's mode \"%1\" does not match the allowed modes.").arg(settings.value("Mode").toString()); + } + } + if (settings.contains("Timeout")) { + timeout = settings.value("Timeout").toInt(&timeoutValueOk); + } + if (settings.contains("AdvertiseName")) { + advertiseName = settings.value("AdvertiseName").toString(); + } + if (settings.contains("PlatformName")) { + platformName = settings.value("PlatformName").toString(); + } + break; + } } - if (timeout < 10) { - qCCritical(dcApplication()) << QString("Invalid timeout value passed: \"%1\". The minimal timeout is 10 [s].").arg(parser.value(timeoutOption)); - parser.showHelp(1); - } - - Core::Mode mode = Core::ModeOffline; - + // Now parse command line if (parser.isSet(modeOption)) { if (parser.value(modeOption).toLower() == "offline") { mode = Core::ModeOffline; @@ -150,21 +182,39 @@ int main(int argc, char *argv[]) parser.showHelp(1); } } + if (parser.isSet(advertiseNameOption)) { + advertiseName = parser.value(advertiseNameOption); + } + if (parser.isSet(platformNameOption)) { + platformName = parser.value(platformNameOption); + } + if (parser.isSet(timeoutOption)) { + timeout = parser.value(timeoutOption).toInt(&timeoutValueOk); + } + + // All parsed. Validate input: + if (!timeoutValueOk) { + qCCritical(dcApplication()) << QString("Invalid timeout value passed: \"%1\". Please pass an integer >= 10").arg(parser.value(timeoutOption)); + parser.showHelp(1); + } + if (timeout < 10) { + qCCritical(dcApplication()) << QString("Invalid timeout value passed: \"%1\". The minimal timeout is 10 [s].").arg(parser.value(timeoutOption)); + parser.showHelp(1); + } qCDebug(dcApplication()) << "====================================="; qCDebug(dcApplication()) << "Starting nymea-networkmanager" << application.applicationVersion(); qCDebug(dcApplication()) << "====================================="; - qCDebug(dcApplication()) << "Advertising name:" << parser.value(advertiseNameOption); - qCDebug(dcApplication()) << "Platform name:" << parser.value(platformNameOption); - qCDebug(dcApplication()) << "Mode:" << parser.value(modeOption); - qCDebug(dcApplication()) << "Timeout:" << parser.value(timeoutOption); + qCDebug(dcApplication()) << "Advertising name:" << advertiseName; + qCDebug(dcApplication()) << "Platform name:" << platformName; + qCDebug(dcApplication()) << "Mode:" << mode; + qCDebug(dcApplication()) << "Timeout:" << timeout; // Start core Core::instance()->setMode(mode); Core::instance()->setAdvertisingTimeout(timeout); - Core::instance()->setAdvertiseName(parser.value(advertiseNameOption)); - Core::instance()->setPlatformName(parser.value(platformNameOption)); - Core::instance()->setTestingEnabled(parser.isSet(testingOption)); + Core::instance()->setAdvertiseName(advertiseName); + Core::instance()->setPlatformName(platformName); Core::instance()->run();