2 * This file is part of Jenirok.
4 * Jenirok is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Jenirok is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Jenirok. If not, see <http://www.gnu.org/licenses/>.
19 #include <QtCore/QDebug>
20 #include <QtCore/QTimerEvent>
21 #include <QtCore/QVariant>
22 #include <QtCore/QStringList>
23 #include <QtGui/QApplication>
24 #include <QtDBus/QDBusArgument>
25 #include <QtDBus/QDBusConnection>
26 #include <icd/dbus_api.h>
27 #include "connectionmanager.h"
29 bool ConnectionManager::connected_ = false;
31 ConnectionManager::ConnectionManager(QObject* parent): QObject(parent),
32 stateReady_(false), connectionReady_(false), scanReady_(false),
33 timeout_(false), numberOfConnections_(0),
34 scannedConnections_(0), timer_(0), searchType_(NO_TYPE), error_(NO_ERROR), connections_(0)
36 QDBusConnection systemBus = QDBusConnection::systemBus();
38 icd2interface_ = new QDBusInterface(ICD_DBUS_API_INTERFACE,
39 ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
42 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
43 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
44 this, SLOT(stateChange(const QDBusMessage&)));
46 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
47 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_CONNECT_SIG,
48 this, SLOT(connectionChange(const QDBusMessage&)));
50 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
51 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_SCAN_SIG,
52 this, SLOT(scanResult(const QDBusMessage&)));
56 ConnectionManager::~ConnectionManager()
58 QDBusConnection systemBus = QDBusConnection::systemBus();
60 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
61 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
62 this, SLOT(stateChange(const QDBusMessage&)));
64 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
65 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_CONNECT_SIG,
66 this, SLOT(connectionChange(const QDBusMessage&)));
68 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
69 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_SCAN_SIG,
70 this, SLOT(scanResult(const QDBusMessage&)));
73 bool ConnectionManager::connect()
75 connectionReady_ = false;
76 unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
77 icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
79 waitSignal(&connectionReady_);
83 bool ConnectionManager::connect(ConnectionManager::Connection const& connection)
85 return connect(connection.id);
88 bool ConnectionManager::connect(QString const& id)
90 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.icd",
94 QList<QVariant> arguments;
96 arguments.append(QVariant(id));
100 arguments.append(QVariant(val));
102 msg.setArguments(arguments);
104 QDBusMessage rep = QDBusConnection::systemBus().call(msg);
106 if(rep.type() == QDBusMessage::ErrorMessage)
108 if(rep.errorName() == "com.nokia.icd.error.invalid_iap")
110 error_ = INVALID_IAP;
114 error_ = UNKNOWN_ERROR;
127 bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType type)
129 QList<Connection> connections;
131 if(!scanConnections(connections))
133 qDebug() << "Unable to scan connections";
137 if(connections.size() == 0)
139 error_ = NO_AVAILABLE_CONNECTIONS;
143 int biggestWlan = -1;
144 int biggestGprs = -1;
148 for(int i = 0; i < connections.size(); i++)
150 switch(connections.at(i).type)
153 if(type != GPRS && connections.at(i).strength > biggestWlan)
155 biggestWlan = connections.at(i).strength;
161 if(type != WLAN && connections.at(i).strength > biggestGprs)
163 biggestGprs = connections.at(i).strength;
169 qDebug() << "Unknown connection type";
175 connection = connections.at(bestWlan);
178 else if(bestGprs >= 0)
180 connection = connections.at(bestGprs);
185 error_ = NO_AVAILABLE_CONNECTIONS;
191 bool ConnectionManager::disconnect(bool force)
195 // Do not allow forced disconnection if connection was not started
202 QDBusMessage msg = QDBusMessage::createSignal("/com/nokia/icd_ui",
206 QList<QVariant> arguments;
208 arguments.append(QVariant(val));
209 msg.setArguments(arguments);
211 bool ret = QDBusConnection::systemBus().send(msg);
221 connectionReady_ = false;
224 flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
226 icd2interface_->call(ICD_DBUS_API_DISCONNECT_REQ, QVariant(flags));
231 bool ConnectionManager::isConnected()
234 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_STATE_REQ);
236 unsigned int numOfReplies = rep.arguments().value(0).value<unsigned int>();
238 if(numOfReplies == 0)
243 waitSignal(&stateReady_);
247 bool ConnectionManager::scanConnections(QList<ConnectionManager::Connection>& connections,
248 ConnectionManager::ConnectionType type)
250 unsigned int flags = static_cast<unsigned int>(ICD_SCAN_REQUEST_ACTIVE_SAVED);
252 scannedConnections_ = 0;
253 connections_ = &connections;
256 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_SCAN_REQ,
259 numberOfConnections_ = rep.arguments().value(0).toList().size();
261 // For some reason, during call icd2 doesn't return any connections
262 // it is going to scan. However, it still scans them so use default value
264 if(numberOfConnections_ == 0)
266 numberOfConnections_ = 2;
269 bool ret = waitSignal(&scanReady_);
274 ConnectionManager::Error ConnectionManager::error() const
279 void ConnectionManager::stateChange(const QDBusMessage& rep)
281 unsigned int status = rep.arguments().value(7).value<unsigned int>();
285 case ICD_STATE_CONNECTING:
287 case ICD_STATE_CONNECTED:
291 case ICD_STATE_DISCONNECTING:
293 case ICD_STATE_DISCONNECTED:
297 case ICD_STATE_LIMITED_CONN_ENABLED:
301 case ICD_STATE_LIMITED_CONN_DISABLED:
305 case ICD_STATE_SEARCH_START:
307 case ICD_STATE_SEARCH_STOP:
309 case ICD_STATE_INTERNAL_ADDRESS_ACQUIRED:
312 qDebug() << "Unknown connection status";
318 void ConnectionManager::connectionChange(const QDBusMessage& rep)
320 unsigned int status = rep.arguments().value(6).value<unsigned int>();
324 case ICD_CONNECTION_SUCCESSFUL:
326 connectionReady_ = true;
328 case ICD_CONNECTION_NOT_CONNECTED:
330 connectionReady_ = true;
332 case ICD_CONNECTION_DISCONNECTED:
334 connectionReady_ = true;
337 qDebug() << "Unknown connection status";
343 void ConnectionManager::scanResult(const QDBusMessage& rep)
350 QList<QVariant> args = rep.arguments();
352 unsigned int status = args.value(0).value<unsigned int>();
354 if(status == ICD_SCAN_COMPLETE)
356 scannedConnections_++;
359 if(scannedConnections_ >= numberOfConnections_)
366 if(status != ICD_SCAN_NEW && status != ICD_SCAN_NOTIFY)
371 Connection connection;
373 QString type = args.value(7).toString();
377 if(searchType_ == WLAN)
382 connection.type = GPRS;
384 else if(type == "WLAN_INFRA" || type == "WLAN_ADHOC")
386 if(searchType_ == GPRS)
391 connection.type = WLAN;
395 qDebug() << "Unknown connection type: " << type;
399 connection.id = QString(args.value(10).toByteArray());
400 connection.name = args.value(8).toString();
401 connection.strength = args.value(11).toInt();
403 for(int i = 0; i < connections_->size(); i++)
405 if(connections_->at(i).id == connection.id)
407 if(status == ICD_SCAN_NEW)
409 connections_->replace(i, connection);
416 connections_->push_back(connection);
419 bool ConnectionManager::waitSignal(bool* ready)
422 timer_ = startTimer(TIMEOUT);
424 while(!*ready && !timeout_)
426 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
431 return *ready || !timeout_;
434 void ConnectionManager::timerEvent(QTimerEvent* event)
441 qDebug() << "Connection request timed out";