Index: sources/wifi/WifiInterface.cpp =================================================================== diff -u -rc77365fa76422bc2150e58d483c446325b50f4b8 -rf35b360054e1e2fa81bf107427fe2d50d78e9e8a --- sources/wifi/WifiInterface.cpp (.../WifiInterface.cpp) (revision c77365fa76422bc2150e58d483c446325b50f4b8) +++ sources/wifi/WifiInterface.cpp (.../WifiInterface.cpp) (revision f35b360054e1e2fa81bf107427fe2d50d78e9e8a) @@ -12,33 +12,101 @@ using namespace Storage; -WifiInterface::WifiInterface(QObject *parent) : QObject(parent) -{ -} +WifiInterface::WifiInterface(QObject *parent) : QObject(parent) {} void WifiInterface::onInitConnections() { connect(this, SIGNAL(didError(const QString)), this, SLOT(onLogFailure(const QString))); + + connect(&_processScan, SIGNAL(finished(int, QProcess::ExitStatus)), + this, SLOT(onScanFinished(int,QProcess::ExitStatus))); + +// connect(&_scanWatcher, SIGNAL(finished()), +// this , SLOT(onFinishedScan())); + + connect(this, SIGNAL(didScan()), + this, SLOT(onScan())); + } /*! * \brief WifiInterface::init + * \details Initialized the Class by calling the init() method first + * And initializes the thread vThread by calling initThread + * on success init(). + * \param vThread - the thread + * \return returns the return value of the init() method + */ +bool WifiInterface::init(QThread &vThread) +{ + if (!init()) return false; + initThread(vThread); + return true; +} + + +/*! + * \brief ApplicationController::initThread + * \details Moves this object into the thread vThread. + * And checks that this method is called from main thread. + * Also connects quitThread to application aboutToQuit. + * \param vThread - the thread + */ +void WifiInterface::initThread(QThread &vThread) +{ + // runs in main thread + Q_ASSERT_X(QThread::currentThread() == qApp->thread() , __func__, "The Class initialization must be done in Main Thread" ); + _thread = &vThread; + _thread->setObjectName(QString("%1_Thread").arg(metaObject()->className())); + connect(qApp, SIGNAL(aboutToQuit()), this, SLOT(quit())); + _thread->start(); + moveToThread(_thread); +} + +/*! + * \brief MessageAcknowModel::quit + * \details quits the class + * Calls quitThread + */ +void WifiInterface::quit() +{ + // coco begin validated: Application termination is not correctly done in coco!!! + // it has been tested and works perfectly fine in normal run. + quitThread(); // validated +} + +/*! + * \brief MessageAcknowModel::quitThread + * \details Moves this object to main thread to be handled by QApplicaiton + * And to be destroyed there. + */ +void WifiInterface::quitThread() +{ + // coco begin validated: Application termination is not correctly done in coco!!! + // it has been tested and works perfectly fine in normal run. + + if (!_thread) return; + + // runs in thread + moveToThread(qApp->thread()); // validated +} +// coco end + + +/*! + * \brief WifiInterface::init * \details Initializes the class by setting the connections * \return true on first initialization, false if it has already been initialized */ -bool WifiInterface::doInit() +bool WifiInterface::init() { if (_init) return false; - _init = true; - onInitConnections(); - LOG_EVENT("UI," + tr("%1 Initialized").arg(metaObject()->className())); - return true; } @@ -60,7 +128,7 @@ */ void WifiInterface::onQuitThread() { - if (!_thread ) + if (!_thread) return; moveToThread(qApp->thread()); @@ -81,29 +149,43 @@ * Scans for Wifi Access Points */ void WifiInterface::doScan() +{ + qDebug() << __FUNCTION__ << QThread::currentThread()->objectName(); + emit didScan(); +} + +void WifiInterface::onScan() { - QtConcurrent::run([=]() { - if (_scanRunning) - { - LOG_DEBUG("Wifi network scan is already running."); - return; - } - LOG_DEBUG("Scanning for Wifi Access Points..."); - QProcess process; - process.setWorkingDirectory(Wifi_Scripts_Dir); - _scanRunning = true; - emit didScanStatusChanged(_scanRunning); - process.start(Wifi_Scan_For_Networks); - process.waitForFinished(_scanTimeout); - QString out = process.readAllStandardOutput(); - QString err = process.readAllStandardError(); - LOG_DEBUG(out); - LOG_DEBUG(err); - _scanRunning = false; - emit didScanStatusChanged(_scanRunning); - onParseWifiScan(out); - }); + qDebug() << __FUNCTION__ << QThread::currentThread()->objectName(); + if (_scanRunning) + { + LOG_DEBUG("Wifi network scan is already running."); + return; + } + LOG_DEBUG("Scanning for Wifi Access Points..."); + _processScan.setWorkingDirectory(Wifi_Scripts_Dir); + _scanRunning = true; + emit didScanStatusChanged(_scanRunning); + _processScan.start(Wifi_Scan_For_Networks); +} +/*! + * \brief WifiInterface::onScanFinished + * Called when the scan process has finished + * \param vExitCode - (int) the exit code + * \param vExitStatus - (QProcess::ExitStatus) the exit status + */ +void WifiInterface::onScanFinished(int vExitCode, QProcess::ExitStatus vExitStatus) +{ + Q_UNUSED(vExitCode) + Q_UNUSED(vExitStatus) +// QString out = _processScan.readAllStandardOutput(); +// QString err = _processScan.readAllStandardError(); +// LOG_DEBUG(out); +// LOG_DEBUG(err); +// _scanRunning = false; +// emit didScanStatusChanged(_scanRunning); +// onParseWifiScan(out); } /*! @@ -168,31 +250,29 @@ */ void WifiInterface::doJoinNetwork(const Network &vNetwork, const QString &vPassword) { - QtConcurrent::run([=]() { - LOG_DEBUG(QString("Joining Network %1").arg(vNetwork.ssid())); - if (!generateWPASupplicant(vNetwork, vPassword)) - { - emit didError("Could not configure network."); - return; - } + LOG_DEBUG(QString("Joining Network %1").arg(vNetwork.ssid())); + if (!generateWPASupplicant(vNetwork, vPassword)) + { + emit didError("Could not configure network."); + return; + } - if (!startWPASupplicant()) - { - emit didError("Could not configure network."); - return; - } + if (!startWPASupplicant()) + { + emit didError("Could not configure network."); + return; + } - // TODO: Add option to setup with static IP settings instead - if (!requestAutoAssignedIP()) - { - emit didError(QString("Could not connect to %1").arg(vNetwork.ssid())); - return; - } + // TODO: Add option to setup with static IP settings instead + if (!requestAutoAssignedIP()) + { + emit didError(QString("Could not connect to %1").arg(vNetwork.ssid())); + return; + } - Network network = vNetwork; - network.ipSettings(getIPSettings()); - emit didConnectToNetwork(network); - }); + Network network = vNetwork; + network.ipSettings(getIPSettings()); + emit didConnectToNetwork(network); } /*!