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/>.
20 #include <QApplication>
21 #include <QtCore/QTimerEvent>
22 #include <QtCore/QVariant>
23 #include <QtCore/QStringList>
24 #include <QtDBus/QDBusArgument>
25 #include <QtDBus/QDBusConnection>
26 #include <icd/dbus_api.h>
27 #include "connectionmanager.h"
30 ConnectionManager::ConnectionManager(QObject* parent): QObject(parent),
31 blocking_(true), stateReady_(false), connectionReady_(false), scanReady_(false),
32 connected_(false), timeout_(false), numberOfConnections_(0),
33 scannedConnections_(0), timer_(0), error_(NO_ERROR), connections_(0)
35 QDBusConnection systemBus = QDBusConnection::systemBus();
37 icd2interface_ = new QDBusInterface(ICD_DBUS_API_INTERFACE,
38 ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE,
41 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
42 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
43 this, SLOT(stateChange(const QDBusMessage&)));
45 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
46 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_CONNECT_SIG,
47 this, SLOT(connectionChange(const QDBusMessage&)));
49 systemBus.connect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
50 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_SCAN_SIG,
51 this, SLOT(scanResult(const QDBusMessage&)));
55 ConnectionManager::~ConnectionManager()
57 QDBusConnection systemBus = QDBusConnection::systemBus();
59 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
60 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
61 this, SLOT(stateChange(const QDBusMessage&)));
63 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
64 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_CONNECT_SIG,
65 this, SLOT(connectionChange(const QDBusMessage&)));
67 systemBus.disconnect(ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH,
68 ICD_DBUS_API_INTERFACE, ICD_DBUS_API_SCAN_SIG,
69 this, SLOT(scanResult(const QDBusMessage&)));
72 void ConnectionManager::setBlocking(bool value)
77 bool ConnectionManager::connect()
79 connectionReady_ = false;
80 unsigned int flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
81 icd2interface_->call(ICD_DBUS_API_CONNECT_REQ, QVariant(flags));
85 waitSignal(&connectionReady_);
92 bool ConnectionManager::connect(ConnectionManager::Connection const& connection)
94 return connect(connection.id);
97 bool ConnectionManager::connect(QString const& id)
99 QDBusMessage msg = QDBusMessage::createMethodCall("com.nokia.icd",
103 QList<QVariant> arguments;
105 arguments.append(QVariant(id));
107 unsigned int val = 0;
109 arguments.append(QVariant(val));
111 msg.setArguments(arguments);
113 QDBusMessage rep = QDBusConnection::systemBus().call(msg);
115 if(rep.type() == QDBusMessage::ErrorMessage)
117 if(rep.errorName() == "com.nokia.icd.error.invalid_iap")
119 error_ = INVALID_IAP;
123 error_ = UNKNOWN_ERROR;
132 bool ConnectionManager::getBestConnection(Connection& connection, ConnectionType type)
134 bool blockingValue = blocking_;
138 QList<Connection> connections;
140 if(!scanConnections(connections))
145 blocking_ = blockingValue;
147 if(connections.size() == 0)
149 error_ = NO_AVAILABLE_CONNECTIONS;
153 int biggestWlan = -1;
154 int biggestGprs = -1;
158 for(int i = 0; i < connections.size(); i++)
160 switch(connections.at(i).type)
163 if(type != GPRS && connections.at(i).strength > biggestWlan)
165 biggestWlan = connections.at(i).strength;
171 if(type != WLAN && connections.at(i).strength > biggestGprs)
173 biggestGprs = connections.at(i).strength;
179 qDebug() << "Unknown connection type";
185 connection = connections.at(bestWlan);
188 else if(bestGprs >= 0)
190 connection = connections.at(bestGprs);
195 error_ = NO_AVAILABLE_CONNECTIONS;
201 bool ConnectionManager::disconnect(bool force)
203 // Forced disconnect is not allowed if connection
204 // was not initialized by this class
205 if(!connected_ && force)
210 connectionReady_ = false;
215 flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_UI_EVENT);
219 flags = static_cast<unsigned int>(ICD_CONNECTION_FLAG_USER_EVENT);
222 icd2interface_->call(ICD_DBUS_API_DISCONNECT_REQ, QVariant(flags));
227 bool ConnectionManager::isConnected()
230 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_STATE_REQ);
232 unsigned int numOfReplies = rep.arguments().value(0).value<unsigned int>();
234 if(numOfReplies == 0)
236 emit isConnectedReply(false);
242 waitSignal(&stateReady_);
249 bool ConnectionManager::scanConnections(QList<ConnectionManager::Connection>& connections,
250 ConnectionManager::ConnectionType type)
252 unsigned int flags = static_cast<unsigned int>(ICD_SCAN_REQUEST_ACTIVE_SAVED);
254 scannedConnections_ = 0;
255 connections_ = &connections;
257 QStringList networks;
262 networks << "WLAN_INFRA" << "WLAN_ADHOC";
271 QDBusMessage rep = icd2interface_->call(ICD_DBUS_API_SCAN_REQ,
275 numberOfConnections_ = rep.arguments().value(0).toList().size();
277 if(numberOfConnections_ == 0)
280 qDebug() << "No connections";
286 bool ret = waitSignal(&scanReady_);
294 ConnectionManager::Error ConnectionManager::error() const
299 void ConnectionManager::stateChange(const QDBusMessage& rep)
301 unsigned int status = rep.arguments().value(7).value<unsigned int>();
305 case ICD_STATE_CONNECTING:
307 case ICD_STATE_CONNECTED:
311 case ICD_STATE_DISCONNECTING:
313 case ICD_STATE_DISCONNECTED:
317 case ICD_STATE_LIMITED_CONN_ENABLED:
321 case ICD_STATE_LIMITED_CONN_DISABLED:
325 case ICD_STATE_SEARCH_START:
327 case ICD_STATE_SEARCH_STOP:
329 case ICD_STATE_INTERNAL_ADDRESS_ACQUIRED:
332 qDebug() << "Unknown connection status";
338 emit isConnectedReply(connected_);
343 void ConnectionManager::connectionChange(const QDBusMessage& rep)
345 unsigned int status = rep.arguments().value(6).value<unsigned int>();
349 case ICD_CONNECTION_SUCCESSFUL:
351 connectionReady_ = true;
353 case ICD_CONNECTION_NOT_CONNECTED:
355 connectionReady_ = true;
357 case ICD_CONNECTION_DISCONNECTED:
359 connectionReady_ = true;
362 qDebug() << "Unknown connection status";
368 emit connectReply(connected_);
372 void ConnectionManager::scanResult(const QDBusMessage& rep)
379 QList<QVariant> args = rep.arguments();
381 unsigned int status = args.value(0).value<unsigned int>();
383 if(status == ICD_SCAN_COMPLETE)
385 scannedConnections_++;
388 if(scannedConnections_ >= numberOfConnections_)
396 if(status != ICD_SCAN_NEW)
401 Connection connection;
402 connection.id = QString(args.value(10).toByteArray());
403 connection.name = args.value(8).toString();
404 connection.strength = args.value(11).toInt();
406 QString type = args.value(7).toString();
410 connection.type = GPRS;
412 else if(type == "WLAN_INFRA" || type == "WLAN_ADHOC")
414 connection.type = WLAN;
418 qDebug() << "Unknown connection type: " << type;
422 emit newConnection(connection);
424 connections_->push_back(connection);
427 bool ConnectionManager::waitSignal(bool* ready)
430 timer_ = startTimer(TIMEOUT);
432 while(!*ready && !timeout_)
434 QCoreApplication::processEvents(QEventLoop::WaitForMoreEvents);
439 return *ready || !timeout_;
442 void ConnectionManager::timerEvent(QTimerEvent* event)
449 qDebug() << "Connection request timed out";