Reenable bluetooth on nymea on shutdown

This commit is contained in:
Simon Stürz 2019-10-08 16:21:54 +02:00
parent 163f4ce77f
commit 6a51b0572e
4 changed files with 21 additions and 14 deletions

View File

@ -106,7 +106,7 @@ void Core::run()
} }
// Note: give network-manager more time to start and get online status // Note: give network-manager more time to start and get online status
QTimer::singleShot(5000, this, &Core::postRun); QTimer::singleShot(3000, this, &Core::postRun);
} }
Core::Core(QObject *parent) : Core::Core(QObject *parent) :
@ -117,6 +117,7 @@ Core::Core(QObject *parent) :
connect(m_networkManager, &NetworkManager::stateChanged, this, &Core::onNetworkManagerStateChanged); connect(m_networkManager, &NetworkManager::stateChanged, this, &Core::onNetworkManagerStateChanged);
m_bluetoothServer = new BluetoothServer(m_networkManager); m_bluetoothServer = new BluetoothServer(m_networkManager);
connect(m_bluetoothServer, &BluetoothServer::runningChanged, this, &Core::onBluetoothServerRunningChanged); connect(m_bluetoothServer, &BluetoothServer::runningChanged, this, &Core::onBluetoothServerRunningChanged);
connect(m_bluetoothServer, &BluetoothServer::connectedChanged, this, &Core::onBluetoothServerConnectedChanged); connect(m_bluetoothServer, &BluetoothServer::connectedChanged, this, &Core::onBluetoothServerConnectedChanged);
@ -179,13 +180,14 @@ void Core::evaluateNetworkManagerState(NetworkManager::NetworkManagerState state
startService(); startService();
break; break;
default: default:
qCDebug(dcApplication()) << "Ignoring networkmanager state" << state; qCDebug(dcApplication()) << "Ignoring" << state;
break; break;
} }
} }
void Core::startService() void Core::startService()
{ {
qCDebug(dcApplication()) << "Start the service...";
if (!m_networkManager->available()) { if (!m_networkManager->available()) {
qCWarning(dcApplication()) << "Could not start services. There is no network manager available."; qCWarning(dcApplication()) << "Could not start services. There is no network manager available.";
return; return;
@ -197,18 +199,14 @@ void Core::startService()
return; return;
} }
qCDebug(dcApplication()) << "Start service";
// Disable bluetooth on nymea in order to not crash with client connections // Disable bluetooth on nymea in order to not crash with client connections
m_nymeaService->enableBluetooth(false); m_nymeaService->enableBluetooth(false);
// Start the bluetooth server for this wireless device // Start the bluetooth server for this wireless device
qCDebug(dcApplication()) << "Start bluetooth service";
m_bluetoothServer->setAdvertiseName(m_advertiseName); m_bluetoothServer->setAdvertiseName(m_advertiseName);
m_bluetoothServer->setModelName(m_platformName); m_bluetoothServer->setModelName(m_platformName);
m_bluetoothServer->setSoftwareVersion(VERSION_STRING); m_bluetoothServer->setSoftwareVersion(VERSION_STRING);
m_bluetoothServer->start();
QTimer::singleShot(5000, m_bluetoothServer, &BluetoothServer::start);
} }
void Core::stopService() void Core::stopService()
@ -262,28 +260,31 @@ void Core::onBluetoothServerRunningChanged(bool running)
qCDebug(dcApplication()) << "Bluetooth server" << (running ? "started" : "stopped"); qCDebug(dcApplication()) << "Bluetooth server" << (running ? "started" : "stopped");
if (!running) { if (!running) {
// Enable bluetooth on nymea
m_advertisingTimer->stop(); m_advertisingTimer->stop();
switch (m_mode) { switch (m_mode) {
case ModeAlways: case ModeAlways:
qCDebug(dcApplication()) << "Restart the bluetooth service because of \"always\" mode."; qCDebug(dcApplication()) << "Restart the bluetooth service because of \"always\" mode.";
// Give some grace periode for bluez to clean up // Give some grace periode for bluez to clean up and restart the service again
QTimer::singleShot(2000, this, &Core::startService); QTimer::singleShot(3000, this, &Core::startService);
break; break;
case ModeStart: case ModeStart:
// Enable bluetooth on nymea
m_nymeaService->enableBluetooth(true); m_nymeaService->enableBluetooth(true);
// We are done here. The bluetooth server was already running
break; break;
case ModeOffline: case ModeOffline:
// Enable bluetooth on nymea
m_nymeaService->enableBluetooth(true); m_nymeaService->enableBluetooth(true);
evaluateNetworkManagerState(m_networkManager->state()); evaluateNetworkManagerState(m_networkManager->state());
break; break;
case ModeOnce: case ModeOnce:
m_nymeaService->enableBluetooth(true);
if (m_networkManager->networkSettings()->connections().isEmpty()) { if (m_networkManager->networkSettings()->connections().isEmpty()) {
qCDebug(dcApplication()) << "Start the bluetooth service because of \"once\" mode and there is currenlty no network configured yet."; qCDebug(dcApplication()) << "Start the bluetooth service because of \"once\" mode and there is currenlty no network configured yet.";
startService(); startService();
} else { } else {
// Enable bluetooth on nymea
m_nymeaService->enableBluetooth(true);
qCDebug(dcApplication()) << "Not starting the bluetooth service because of \"once\" mode. There are" << m_networkManager->networkSettings()->connections().count() << "network configurations."; qCDebug(dcApplication()) << "Not starting the bluetooth service because of \"once\" mode. There are" << m_networkManager->networkSettings()->connections().count() << "network configurations.";
} }
break; break;
@ -310,7 +311,7 @@ void Core::onNetworkManagerAvailableChanged(bool available)
} }
if (m_initRunning) { if (m_initRunning) {
qCDebug(dcApplication()) << "Init is running"; qCDebug(dcApplication()) << "Init is running...";
return; return;
} }

View File

@ -90,7 +90,7 @@ int main(int argc, char *argv[])
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.3.2"); application.setApplicationVersion(VERSION_STRING);
// Command line parser // Command line parser
QCommandLineParser parser; QCommandLineParser parser;

View File

@ -24,6 +24,12 @@ NymeadService::NymeadService(bool pushbuttonEnabled, QObject *parent) :
} }
} }
NymeadService::~NymeadService()
{
// Note: re-enable bluetooth hardware resource on nymea
enableBluetooth(true);
}
bool NymeadService::available() const bool NymeadService::available() const
{ {
return m_available; return m_available;

View File

@ -13,7 +13,7 @@ class NymeadService : public QObject
Q_OBJECT Q_OBJECT
public: public:
explicit NymeadService(bool pushbuttonEnabled, QObject *parent = nullptr); explicit NymeadService(bool pushbuttonEnabled, QObject *parent = nullptr);
~NymeadService();
bool available() const; bool available() const;
void enableBluetooth(const bool &enable); void enableBluetooth(const bool &enable);