Fixed a bug that caused wlan connections to not being found in daemon.
authoreshe <jessehakanen@gmail.com>
Tue, 15 Jun 2010 18:16:09 +0000 (19:16 +0100)
committereshe <jessehakanen@gmail.com>
Tue, 15 Jun 2010 18:16:09 +0000 (19:16 +0100)
src/common/connectionmanager.cpp
src/common/connectionmanager.h
src/daemon/calllistener.cpp
src/daemon/calllistener.h
src/daemon/main.cpp

index dfee7d6..4f0d99e 100644 (file)
@@ -29,9 +29,9 @@
 bool ConnectionManager::connected_ = false;
 
 ConnectionManager::ConnectionManager(QObject* parent): QObject(parent),
-blocking_(true), stateReady_(false), connectionReady_(false), scanReady_(false),
+stateReady_(false), connectionReady_(false), scanReady_(false),
 timeout_(false), numberOfConnections_(0),
-scannedConnections_(0), timer_(0), error_(NO_ERROR), connections_(0)
+scannedConnections_(0), timer_(0), searchType_(NO_TYPE), error_(NO_ERROR), connections_(0)
 {
     QDBusConnection systemBus = QDBusConnection::systemBus();
 
@@ -70,24 +70,14 @@ ConnectionManager::~ConnectionManager()
                          this, SLOT(scanResult(const QDBusMessage&)));
 }
 
-void ConnectionManager::setBlocking(bool value)
-{
-    blocking_ = value;
-}
-
 bool ConnectionManager::connect()
 {
     connectionReady_ = false;
     unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
     icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
 
-    if(blocking_)
-    {
-        waitSignal(&connectionReady_);
-        return connected_;
-    }
-
-    return true;
+    waitSignal(&connectionReady_);
+    return connected_;
 }
 
 bool ConnectionManager::connect(ConnectionManager::Connection const& connection)
@@ -136,10 +126,6 @@ bool ConnectionManager::connect(QString const& id)
 
 bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType type)
 {
-    bool blockingValue = blocking_;
-
-    blocking_ = true;
-
     QList<Connection> connections;
 
     if(!scanConnections(connections))
@@ -148,8 +134,6 @@ bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType
         return false;
     }
 
-    blocking_ = blockingValue;
-
     if(connections.size() == 0)
     {
         error_ = NO_AVAILABLE_CONNECTIONS;
@@ -163,27 +147,27 @@ bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType
 
     for(int i = 0; i < connections.size(); i++)
     {
-       switch(connections.at(i).type)
-       {
-       case WLAN:
-           if(type != GPRS && connections.at(i).strength > biggestWlan)
-           {
-               biggestWlan = connections.at(i).strength;
-               bestWlan = i;
-           }
-           break;
-
-       case GPRS:
-           if(type != WLAN && connections.at(i).strength > biggestGprs)
-           {
-               biggestGprs = connections.at(i).strength;
-               bestGprs = i;
-           }
-           break;
-
-       default:
-           qDebug() << "Unknown connection type";
-       }
+        switch(connections.at(i).type)
+        {
+        case WLAN:
+            if(type != GPRS && connections.at(i).strength > biggestWlan)
+            {
+                biggestWlan = connections.at(i).strength;
+                bestWlan = i;
+            }
+            break;
+
+        case GPRS:
+            if(type != WLAN && connections.at(i).strength > biggestGprs)
+            {
+                biggestGprs = connections.at(i).strength;
+                bestGprs = i;
+            }
+            break;
+
+        default:
+            qDebug() << "Unknown connection type";
+        }
     }
 
     if(bestWlan >= 0)
@@ -253,17 +237,11 @@ bool ConnectionManager::isConnected()
 
     if(numOfReplies == 0)
     {
-        emit isConnectedReply(false);
         return false;
     }
 
-    if(blocking_)
-    {
-        waitSignal(&stateReady_);
-        return connected_;
-    }
-
-    return true;
+    waitSignal(&stateReady_);
+    return connected_;
 }
 
 bool ConnectionManager::scanConnections(QList<ConnectionManager::Connection>& connections,
@@ -273,42 +251,24 @@ bool ConnectionManager::scanConnections(QList<ConnectionManager::Connection>& co
     scanReady_ = false;
     scannedConnections_ = 0;
     connections_ = &connections;
-
-    QStringList networks;
-
-    switch(type)
-    {
-    case WLAN:
-        networks << "WLAN_INFRA" << "WLAN_ADHOC";
-        break;
-    case GPRS:
-        networks << "GPRS";
-        break;
-    default:
-        break;
-    }
+    searchType_ = type;
 
     QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_SCAN_REQ,
-                                            QVariant(flags),
-                                            QVariant(networks));
+                                            QVariant(flags));
 
     numberOfConnections_ = rep.arguments().value(0).toList().size();
 
+    // For some reason, during call icd2 doesn't return any connections
+    // it is going to scan. However, it still scans them so use default value
+    // of 2.
     if(numberOfConnections_ == 0)
     {
-        connections_ = 0;
-        qDebug() << "No connections";
-        return false;
-    }
-
-    if(blocking_)
-    {
-        bool ret = waitSignal(&scanReady_);
-        connections_ = 0;
-        return ret;
+        numberOfConnections_ = 2;
     }
 
-    return true;
+    bool ret = waitSignal(&scanReady_);
+    connections_ = 0;
+    return ret;
 }
 
 ConnectionManager::Error ConnectionManager::error() const
@@ -353,11 +313,6 @@ void ConnectionManager::stateChange(const QDBusMessage& rep)
         break;
     }
 
-    if(stateReady_)
-    {
-        emit isConnectedReply(connected_);
-    }
-
 }
 
 void ConnectionManager::connectionChange(const QDBusMessage& rep)
@@ -383,10 +338,6 @@ void ConnectionManager::connectionChange(const QDBusMessage& rep)
         break;
     }
 
-    if(connectionReady_)
-    {
-        emit connectReply(connected_);
-    }
 }
 
 void ConnectionManager::scanResult(const QDBusMessage& rep)
@@ -409,7 +360,6 @@ void ConnectionManager::scanResult(const QDBusMessage& rep)
     {
         scanReady_ = true;
         connections_ = 0;
-        emit scanReady();
         return;
     }
 
@@ -419,18 +369,25 @@ void ConnectionManager::scanResult(const QDBusMessage& rep)
     }
 
     Connection connection;
-    connection.id = QString(args.value(10).toByteArray());
-    connection.name = args.value(8).toString();
-    connection.strength = args.value(11).toInt();
 
     QString type = args.value(7).toString();
 
     if(type == "GPRS")
     {
+        if(searchType_ == WLAN)
+        {
+            return;
+        }
+
         connection.type = GPRS;
     }
     else if(type == "WLAN_INFRA" || type == "WLAN_ADHOC")
     {
+        if(searchType_ == GPRS)
+        {
+            return;
+        }
+
         connection.type = WLAN;
     }
     else
@@ -439,7 +396,9 @@ void ConnectionManager::scanResult(const QDBusMessage& rep)
         return;
     }
 
-    emit newConnection(connection);
+    connection.id = QString(args.value(10).toByteArray());
+    connection.name = args.value(8).toString();
+    connection.strength = args.value(11).toInt();
 
     connections_->push_back(connection);
 }
index b92cca2..34ec24c 100644 (file)
@@ -46,7 +46,6 @@ public:
 
     ConnectionManager(QObject* parent = 0);
     ~ConnectionManager();
-    void setBlocking(bool value);
     bool connect();
     bool connect(Connection const& connection);
     bool connect(QString const& id);
@@ -55,13 +54,7 @@ public:
     bool isConnected();
     bool scanConnections(QList<Connection>& connections, ConnectionType type = NO_TYPE);
     Error error() const;
-    static unsigned int const TIMEOUT = 20000;
-
-signals:
-    void connectReply(bool connected);
-    void isConnectedReply(bool connected);
-    void newConnection(ConnectionManager::Connection const& connection);
-    void scanReady();
+    static unsigned int const TIMEOUT = 15000;
 
 protected:
     virtual void timerEvent(QTimerEvent* event);
@@ -73,7 +66,6 @@ private slots:
 
 private:
     bool waitSignal(bool* ready);
-    bool blocking_;
     bool stateReady_;
     bool connectionReady_;
     bool scanReady_;
@@ -82,6 +74,7 @@ private:
     int numberOfConnections_;
     int scannedConnections_;
     int timer_;
+    ConnectionType searchType_;
     Error error_;
     QList<Connection>* connections_;
     QDBusInterface* icd2interface_;
index 4a2f1c5..9110ff7 100644 (file)
@@ -77,8 +77,6 @@ bool CallListener::begin()
                        this,
                        SLOT(callTerminate()));
 
-    findGprsId();
-
     qDebug() << "Starting...";
 
     return true;
@@ -289,11 +287,6 @@ void CallListener::callTerminate()
     }
 
     searchClose();
-
-    if(gprsId_.isEmpty())
-    {
-        findGprsId();
-    }
 }
 
 void CallListener::showDelayedResult(QString const& text, int delay)
@@ -411,9 +404,14 @@ bool CallListener::handleConnection()
     }
 
     int cretries = 0;
+    int scans = 0;
+    bool found = false;
+    int maxScans = GPRS_SCANS;
 
-    bool tryGprs = (!gprsId_.isEmpty() &&
-                   (configType == Settings::ANY || configType == Settings::GPRS));
+    if(lookupType == ConnectionManager::WLAN)
+    {
+        maxScans = WLAN_SCANS;
+    }
 
     while(cretries < CONNECTION_LOOKUP_RETRIES)
     {
@@ -422,26 +420,32 @@ bool CallListener::handleConnection()
             return false;
         }
 
-        if(cm.getBestConnection(best, lookupType))
+        if(scans < maxScans)
         {
-            break;
+            if(cm.getBestConnection(best, lookupType))
+            {
+                found = true;
+            }
+
+            scans++;
         }
 
-        // ICD doesn't always find gprs connection during call, so
-        // try to use gprs anyway.
-        if(tryGprs && is3g())
+        // If there is only gprs connection available,
+        // make sure that we are on 3g network
+        if(found && (best.type != ConnectionManager::GPRS || is3g()))
         {
-            best.id = gprsId_;
-            qDebug() << "Trying gprs";
             break;
         }
 
+        if(found)
+        {
+            sleep(WAIT_BETWEEN_RETRIES);
+        }
+
         qDebug() << "No connections found, retrying...";
 
         cretries++;
 
-        sleep(WAIT_BETWEEN_RETRIES);
-
     }
 
     if(cretries >= CONNECTION_LOOKUP_RETRIES)
@@ -459,6 +463,8 @@ bool CallListener::handleConnection()
             return false;
         }
 
+        qDebug() << "Connecting to " << best.name;
+
         if(cm.connect(best.id))
         {
             break;
@@ -488,6 +494,12 @@ bool CallListener::handleConnection()
 void CallListener::showError(QString const& msg, int timeout)
 {
     qDebug() << "Error: " << msg;
+
+    if(!initialized_ || !box_)
+    {
+        return;
+    }
+
     box_->setTimeout(ERROR_BANNER_TIMEOUT);
 
     if(timeout)
@@ -500,41 +512,6 @@ void CallListener::showError(QString const& msg, int timeout)
     }
 }
 
-void CallListener::timerEvent(QTimerEvent* event)
-{
-    Q_UNUSED(event);
-    killTimer(timer_);
-    timer_ = 0;
-}
-
-void CallListener::sleep(int ms)
-{
-    if(timer_)
-    {
-        killTimer(timer_);
-    }
-
-    timer_ = startTimer(ms);
-
-    while(timer_)
-    {
-        QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
-    }
-}
-
-void CallListener::findGprsId()
-{
-    ConnectionManager cm;
-
-    QList<ConnectionManager::Connection> connections;
-
-    if(cm.scanConnections(connections, ConnectionManager::GPRS) &&
-       connections.size() > 0)
-    {
-        gprsId_ = connections.at(0).id;
-    }
-}
-
 bool CallListener::is3g()
 {
     QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.phone.net",
@@ -564,3 +541,25 @@ bool CallListener::is3g()
     // Something else
     return false;
 }
+
+void CallListener::timerEvent(QTimerEvent* event)
+{
+    Q_UNUSED(event);
+    killTimer(timer_);
+    timer_ = 0;
+}
+
+void CallListener::sleep(int ms)
+{
+    if(timer_)
+    {
+        killTimer(timer_);
+    }
+
+    timer_ = startTimer(ms);
+
+    while(timer_)
+    {
+        QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
+    }
+}
index 2ec5b7f..cf47953 100644 (file)
@@ -40,8 +40,10 @@ public:
     static const int BANNER_DELAY = 350;
     static const int SEARCH_RETRIES = 2;
     static const int CONNECT_RETRIES = 3;
-    static const int CONNECTION_LOOKUP_RETRIES = 15;
-    static const int WAIT_BETWEEN_RETRIES = 1000;
+    static const int CONNECTION_LOOKUP_RETRIES = 10;
+    static const int GPRS_SCANS = 2;
+    static const int WLAN_SCANS = 5;
+    static const int WAIT_BETWEEN_RETRIES = 500;
     static const int ERROR_BANNER_TIMEOUT = 4500;
 
 protected:
@@ -63,9 +65,8 @@ private:
     void searchClose();
     bool handleConnection();
     void showError(QString const& msg, int delay = 0);
-    void sleep(int ms);
-    void findGprsId();
     bool is3g();
+    void sleep(int ms);
     QString createResult(QString const& name, QString const& street, QString const& city);
     QString timedMessage_;
     Source* source_;
@@ -79,7 +80,6 @@ private:
     int retries_;
     QString currentSearch_;
     int timer_;
-    QString gprsId_;
 };
 
 #endif // CALLLISTENER_H
index f011649..3d3b672 100644 (file)
@@ -20,6 +20,7 @@
 #include <QtGui/QApplication>
 #include "calllistener.h"
 #include "settings.h"
+#include "connectionmanager.h"
 
 int main(int argc, char *argv[])
 {