-rw-r--r-- | noncore/securityplugins/blueping/bluepingplugin.cpp | 64 | ||||
-rw-r--r-- | noncore/securityplugins/blueping/bluepingplugin.h | 5 |
2 files changed, 46 insertions, 23 deletions
diff --git a/noncore/securityplugins/blueping/bluepingplugin.cpp b/noncore/securityplugins/blueping/bluepingplugin.cpp index f5d9ad6..1c514e5 100644 --- a/noncore/securityplugins/blueping/bluepingplugin.cpp +++ b/noncore/securityplugins/blueping/bluepingplugin.cpp @@ -18,13 +18,15 @@ using Opie::Security::MultiauthConfigWidget; /// standard c'tor -BluepingPlugin::BluepingPlugin() : MultiauthPluginObject(), m_ping(0), m_bluepingW(0) { - bluetoothAlreadyRestarted = false; +BluepingPlugin::BluepingPlugin() : MultiauthPluginObject(), m_ping(0), m_bluepingW(0), bluetoothWasOff(false) { } -/// cleans m_ping if we need to +/// cleans m_ping and m_bluepingW if we need to BluepingPlugin::~BluepingPlugin() { + odebug << "closing Blueping plugin..." << oendl; + if (m_ping != 0) delete m_ping; if (m_bluepingW != 0) delete m_bluepingW; + killBluetoothIfNecessary(); } @@ -40,11 +42,27 @@ MultiauthConfigWidget * BluepingPlugin::configWidget(QWidget * parent) { return m_bluepingW; } + QString BluepingPlugin::pixmapNameWidget() const { return "security/bluepingplugin"; } + QString BluepingPlugin::pixmapNameConfig() const { return "security/bluepingplugin"; } +/// If Bluetooth was off before the plugin ran, we switch it off again +void BluepingPlugin::killBluetoothIfNecessary() { + if (bluetoothWasOff) { + OProcess killB; + killB << "killall" << "hciattach"; + odebug << "killing Bluetooth... (since it was up only for Blueping)" << oendl; + if ( !killB.start(OProcess::Block) ) { + oerr << "could not kill bluetooth" << oendl; + } + } else { + odebug << "keeping Bluetooth on" << oendl; + } +} + /// Emit the MultiauthPluginObject::Success emitCode void BluepingPlugin::success() { @@ -90,5 +108,5 @@ void BluepingPlugin::ping() { else { - odebug << "Failed Bluetooth ping..." << oendl; + odebug << "Failed Bluetooth ping... (normalExit: " << ping->normalExit() << ", exitStatus: " << ping->exitStatus() << ")" << oendl; failure(); } @@ -137,15 +155,18 @@ int BluepingPlugin::authenticate() { QObject::connect(this, SIGNAL(emitCode(int)), &bluepingDialog, SLOT(done(int))); - // we can uncomment the following when testing - //bluetoothAlreadyRestarted = true; - if (!bluetoothAlreadyRestarted) + + + /* let's start Bluetooth if it's not running + */ + OProcess checkB; + checkB << "pidof" << "hciattach"; + odebug << "checking if Bluetooth is running..." << oendl; + // now we start bluetooth *only* if the previous command works, exits normally, and + // it returns a non-null exit code (which means hciattach is not running) + if ( checkB.start(OProcess::Block) && checkB.normalExit() && (checkB.exitStatus() != 0) ) { - // we have just started or resumed the device, so Bluetooth has to be (re)started - OProcess killB; - killB << "killall" << "hciattach"; - odebug << "killing Bluetooth..." << oendl; - if ( !killB.start(OProcess::Block) ) { - oerr << "could not kill bluetooth" << oendl; - } + // remember to switch off Bluetooth once we're finished... + bluetoothWasOff = true; + odebug << "Bluetooth is not running, we must start it now" << oendl; OProcess startB; @@ -165,13 +186,12 @@ int BluepingPlugin::authenticate() { if ( !startB.start(OProcess::Block) ) { - oerr << "could not (re)start bluetooth" << oendl; + oerr << "could not start Bluetooth" << oendl; return MultiauthPluginObject::Skip; } else { - if ( startB.normalExit() && (startB.exitStatus() == 0) ) + if ( (startB.normalExit()) && (startB.exitStatus() == 0) ) { - odebug << "hciattach exited normally."<< oendl; - bluetoothAlreadyRestarted = true; + odebug << "hciattach exited normally, Bluetooth is probably on now, let's wait 500 ms and ping" << oendl; // 500 ms timer, so l2ping won't try to find a route before bluetooth has \em really started QTimer::singleShot( 500, this, SLOT(ping()) ); @@ -179,6 +199,7 @@ int BluepingPlugin::authenticate() { else { - owarn << "hciattach exited anormally (error code: " << startB.exitStatus() << ")" << oendl; - } // end if startBluetooth exit status == 0 + owarn << "hciattach exited anormally (normalExit: " << startB.normalExit() << ", exit status: " << startB.exitStatus() << ")" << oendl; + return MultiauthPluginObject::Skip; + } // end if startB exited normaly } // end if startBluetooth started } @@ -186,6 +207,7 @@ int BluepingPlugin::authenticate() { { // we don't need to wait, since bluetooth has been started long enough ago + odebug << "Bluetooth is already running, we can try to ping now" << oendl; ping(); - } // end if bluetooth not restarted + } // end if Bluetooth was off diff --git a/noncore/securityplugins/blueping/bluepingplugin.h b/noncore/securityplugins/blueping/bluepingplugin.h index b8d6734..56ff22d 100644 --- a/noncore/securityplugins/blueping/bluepingplugin.h +++ b/noncore/securityplugins/blueping/bluepingplugin.h @@ -74,9 +74,10 @@ class BluepingPlugin : public QObject, public Opie::Security::MultiauthPluginObj private: + void killBluetoothIfNecessary(); Opie::Core::OProcess *m_ping; + Config * m_config; BluepingConfigWidget * m_bluepingW; - bool bluetoothAlreadyRestarted; + bool bluetoothWasOff; QString macToPing; - }; |