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.
This commit is contained in:
Michael Zanetti 2018-10-16 22:03:20 +02:00
parent c442887971
commit 8895221526
10 changed files with 108 additions and 86 deletions

35
.gitignore vendored
View File

@ -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
*.pro.user.*
*.qbs.user
*.qbs.user.*
*.moc
moc_*.cpp
moc_*.h
qrc_*.cpp
ui_*.h
Makefile*
*build-*
# QtCreator
*.autosave *.autosave
# QtCtreator Qml
*.qmlproject.user
*.qmlproject.user.*
# QtCtreator CMake
CMakeLists.txt.user*

View File

@ -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 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. 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 # Command line parameters
$ nymea-network-manager --help $ nymea-network-manager --help

11
debian/changelog vendored
View File

@ -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 <micha@MetalSonic> Tue, 16 Oct 2018 22:10:51 +0200
nymea-networkmanager (0.0.1) bionic; urgency=medium
[ Simon Stürz ]
* Initial release. * Initial release.
-- Simon Stürz <simon.stuerz@guh.io> Mon, 05 Mar 2018 15:05:40 +0100 -- Michael Zanetti <micha@MetalSonic> Tue, 16 Oct 2018 22:10:45 +0200

View File

@ -1,2 +1,3 @@
usr/bin/nymea-networkmanager usr/bin/nymea-networkmanager
nymea-networkmanager.service /lib/systemd/system/ nymea-networkmanager.service /lib/systemd/system/
nymea-networkmanager.conf /etc/nymea/

View File

@ -0,0 +1,5 @@
[General]
Mode=offline
Timeout=60
AdvertiseName=BT WLAN setup
PlatformName=nymea-box

View File

@ -4,7 +4,7 @@ Documentation=https://github.com/guh/nymea-networkmanager
After=network.target After=network.target
[Service] [Service]
ExecStart=/usr/bin/nymea-networkmanager -d -a nymea -p nymea-box ExecStart=/usr/bin/nymea-networkmanager -d
StandardOutput=journal StandardOutput=journal
StandardError=journal StandardError=journal
Restart=on-failure Restart=on-failure

View File

@ -257,7 +257,7 @@ NetworkService::NetworkServiceCommand NetworkService::verifyCommand(const QByteA
if (commandData.length() != 1) if (commandData.length() != 1)
return NetworkServiceCommandInvalid; return NetworkServiceCommandInvalid;
uint commandInteger = commandData.toHex().toUInt(0, 16); uint commandInteger = commandData.toHex().toUInt(nullptr, 16);
switch (commandInteger) { switch (commandInteger) {
case NetworkServiceCommandEnableNetworking: case NetworkServiceCommandEnableNetworking:
return NetworkServiceCommandEnableNetworking; return NetworkServiceCommandEnableNetworking;
@ -345,9 +345,6 @@ void NetworkService::serviceError(const QLowEnergyService::ServiceError &error)
case QLowEnergyService::UnknownError: case QLowEnergyService::UnknownError:
errorString = "Unknown error"; errorString = "Unknown error";
break; break;
default:
errorString = "Unhandled error";
break;
} }
qCWarning(dcBluetoothServer()) << "NetworkService: Error:" << errorString; qCWarning(dcBluetoothServer()) << "NetworkService: Error:" << errorString;

View File

@ -97,16 +97,6 @@ void Core::setAdvertisingTimeout(const int advertisingTimeout)
m_advertisingTimeout = advertisingTimeout; m_advertisingTimeout = advertisingTimeout;
} }
bool Core::testingEnabled() const
{
return m_testing;
}
void Core::setTestingEnabled(bool testing)
{
m_testing = testing;
}
void Core::run() void Core::run()
{ {
// Start the networkmanager // Start the networkmanager

View File

@ -59,9 +59,6 @@ public:
int advertisingTimeout() const; int advertisingTimeout() const;
void setAdvertisingTimeout(const int advertisingTimeout); void setAdvertisingTimeout(const int advertisingTimeout);
bool testingEnabled() const;
void setTestingEnabled(bool testing);
void run(); void run();
private: private:
@ -82,8 +79,6 @@ private:
QString m_platformName; QString m_platformName;
int m_advertisingTimeout = 60; int m_advertisingTimeout = 60;
bool m_testing = false;
void evaluateNetworkManagerState(const NetworkManager::NetworkManagerState &state); void evaluateNetworkManagerState(const NetworkManager::NetworkManagerState &state);
void startService(); void startService();

View File

@ -22,6 +22,10 @@
#include <QCoreApplication> #include <QCoreApplication>
#include <QCommandLineParser> #include <QCommandLineParser>
#include <QCommandLineOption> #include <QCommandLineOption>
#include <QSettings>
#include <QStandardPaths>
#include <QFileInfo>
#include <QMetaEnum>
#include "core.h" #include "core.h"
#include "application.h" #include "application.h"
@ -77,10 +81,17 @@ int main(int argc, char *argv[])
{ {
qInstallMessageHandler(consoleLogHandler); 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 application(argc, argv);
application.setApplicationName("nymea-networkmanager"); application.setApplicationName("nymea-networkmanager");
application.setOrganizationName("nymea"); application.setOrganizationName("nymea");
application.setApplicationVersion("0.1.0"); application.setApplicationVersion("0.2.0");
// Command line parser // Command line parser
QCommandLineParser parser; QCommandLineParser parser;
@ -92,16 +103,16 @@ int main(int argc, char *argv[])
QCommandLineOption debugOption(QStringList() << "d" << "debug", "Enable more debug output."); QCommandLineOption debugOption(QStringList() << "d" << "debug", "Enable more debug output.");
parser.addOption(debugOption); parser.addOption(debugOption);
QCommandLineOption advertiseNameOption(QStringList() << "a" << "advertise-name", "The name of the bluetooth server. Default \"nymea\".", "NAME"); QCommandLineOption advertiseNameOption(QStringList() << "a" << "advertise-name", QString("The name of the bluetooth server. Default \"%1\".").arg(advertiseName), "NAME");
advertiseNameOption.setDefaultValue("nymea"); advertiseNameOption.setDefaultValue(advertiseName);
parser.addOption(advertiseNameOption); parser.addOption(advertiseNameOption);
QCommandLineOption platformNameOption(QStringList() << "p" << "platform-name", "The name of the platform this daemon is running. Default \"nymea-box\".", "NAME"); QCommandLineOption platformNameOption(QStringList() << "p" << "platform-name", QString("The name of the platform this daemon is running. Default \"%1\".").arg(platformName), "NAME");
platformNameOption.setDefaultValue("nymea-box"); platformNameOption.setDefaultValue(platformName);
parser.addOption(platformNameOption); parser.addOption(platformNameOption);
QCommandLineOption timeoutOption(QStringList() << "t" << "timeout", "The timeout of the bluetooth server. Minimum value is 10. Default \"60\".", "SECONDS"); QCommandLineOption timeoutOption(QStringList() << "t" << "timeout", QString("The timeout of the bluetooth server. Minimum value is 10. Default \"%1\".").arg(timeout), "SECONDS");
timeoutOption.setDefaultValue("60"); timeoutOption.setDefaultValue(QString::number(timeout));
parser.addOption(timeoutOption); parser.addOption(timeoutOption);
QCommandLineOption modeOption(QStringList() << "m" << "mode", "Run the daemon in a specific mode. Default \"offline\".\n\n" \ 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"); "- 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); parser.addOption(modeOption);
QCommandLineOption testingOption(QStringList() << "t" << "testing", "Advertise the bluetoothserver alyways for testing.");
parser.addOption(testingOption);
parser.process(application); parser.process(application);
// Enable debug categories // Enable debug categories
@ -123,21 +131,45 @@ int main(int argc, char *argv[])
QLoggingCategory::installFilter(loggingCategoryFilter); QLoggingCategory::installFilter(loggingCategoryFilter);
bool timeoutValueOk = false; bool timeoutValueOk = true;
int timeout = parser.value(timeoutOption).toInt(&timeoutValueOk);
if (!timeoutValueOk) { // Now read the cofig file, overriding defaults
qCCritical(dcApplication()) << QString("Invalid timeout value passed: \"%1\". Please pass an integer >= 10").arg(parser.value(timeoutOption)); QStringList configLocations;
parser.showHelp(1); 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) { // Now parse command line
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;
if (parser.isSet(modeOption)) { if (parser.isSet(modeOption)) {
if (parser.value(modeOption).toLower() == "offline") { if (parser.value(modeOption).toLower() == "offline") {
mode = Core::ModeOffline; mode = Core::ModeOffline;
@ -150,21 +182,39 @@ int main(int argc, char *argv[])
parser.showHelp(1); 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()) << "=====================================";
qCDebug(dcApplication()) << "Starting nymea-networkmanager" << application.applicationVersion(); qCDebug(dcApplication()) << "Starting nymea-networkmanager" << application.applicationVersion();
qCDebug(dcApplication()) << "====================================="; qCDebug(dcApplication()) << "=====================================";
qCDebug(dcApplication()) << "Advertising name:" << parser.value(advertiseNameOption); qCDebug(dcApplication()) << "Advertising name:" << advertiseName;
qCDebug(dcApplication()) << "Platform name:" << parser.value(platformNameOption); qCDebug(dcApplication()) << "Platform name:" << platformName;
qCDebug(dcApplication()) << "Mode:" << parser.value(modeOption); qCDebug(dcApplication()) << "Mode:" << mode;
qCDebug(dcApplication()) << "Timeout:" << parser.value(timeoutOption); qCDebug(dcApplication()) << "Timeout:" << timeout;
// Start core // Start core
Core::instance()->setMode(mode); Core::instance()->setMode(mode);
Core::instance()->setAdvertisingTimeout(timeout); Core::instance()->setAdvertisingTimeout(timeout);
Core::instance()->setAdvertiseName(parser.value(advertiseNameOption)); Core::instance()->setAdvertiseName(advertiseName);
Core::instance()->setPlatformName(parser.value(platformNameOption)); Core::instance()->setPlatformName(platformName);
Core::instance()->setTestingEnabled(parser.isSet(testingOption));
Core::instance()->run(); Core::instance()->run();