From 78f7ec4e0908ac9b128e6ac1ed22f374b6914e9a Mon Sep 17 00:00:00 2001 From: Max Lapan Date: Wed, 10 Mar 2010 17:25:03 +0300 Subject: [PATCH] Class which monitors n900 connection state. Finished. There is still an issue with request of connection state on class creation, but broadcast messages are handled correctly. --- connection.cpp | 39 +++++++++++++++++++++++++++++++++++---- connection.hpp | 15 +++++++++++++-- icd2_light.h | 47 +++++++++++++++++++++++++++++++++++++++++++++++ yandex-traffic-core.pri | 2 ++ 4 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 icd2_light.h diff --git a/connection.cpp b/connection.cpp index 18a2ee0..03207d5 100644 --- a/connection.cpp +++ b/connection.cpp @@ -1,4 +1,8 @@ +#include + #include "connection.hpp" +#include "icd2_light.h" + static ConnectionChecker *_instance = NULL; @@ -15,13 +19,40 @@ ConnectionChecker *ConnectionChecker::instance () ConnectionChecker::ConnectionChecker () + : _bus (QDBusConnection::systemBus ()) +{ + _connected = true; + + _itf = new QDBusInterface (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, _bus); + _bus.connect (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG, + this, SLOT (stateSignal (const QDBusMessage&))); + + requestState (); +} + + +void ConnectionChecker::requestState () { - // start timer which will check connection - startTimer (15*1000); + QDBusMessage msg = QDBusMessage::createSignal (ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_REQ); + + _bus.send (msg); } -void ConnectionChecker::timerEvent (QTimerEvent *) +void ConnectionChecker::stateSignal (const QDBusMessage& msg) { - // check for connection + unsigned int status = msg.arguments ().value (7).value(); + + if (status == ICD_STATE_CONNECTED) { + if (!_connected) { + _connected = true; + emit connected (true); + } + } + else { + if (_connected) { + _connected = false; + emit connected (false); + } + } } diff --git a/connection.hpp b/connection.hpp index 1840ae8..5c0d85c 100644 --- a/connection.hpp +++ b/connection.hpp @@ -2,22 +2,33 @@ #define __CONNECTION_H__ #include +#include -// Singleton, which periodically checks for connection state and notifies when it changed. +// Singleton, which listens for ICD DBUS events about connection change class ConnectionChecker : public QObject { Q_OBJECT private: + bool _connected; + QDBusConnection _bus; + QDBusInterface *_itf; + ConnectionChecker (); protected: - void timerEvent (QTimerEvent *event); + void requestState (); + +protected slots: + void stateSignal (const QDBusMessage& msg); public: static ConnectionChecker *instance (); + bool isConnected () const + { return _connected; }; + signals: void connected (bool active); }; diff --git a/icd2_light.h b/icd2_light.h new file mode 100644 index 0000000..d0a8da5 --- /dev/null +++ b/icd2_light.h @@ -0,0 +1,47 @@ +#ifndef __ICD2_LIGHT_H__ +#define __ICD2_LIGHT_H__ + +/* + * This is truncated version of file /usr/include/icd/dbus_api.h + * I created it because of glib.h inclusion in the original file + */ +#ifdef __cplusplus +extern "C" { +#endif + +#define ICD_DBUS_API_INTERFACE "com.nokia.icd2" +#define ICD_DBUS_API_PATH "/com/nokia/icd2" +#define ICD_DBUS_API_STATE_SIG "state_sig" +#define ICD_DBUS_API_STATE_REQ "state_req" + + +/** Connection state */ +enum icd_connection_state { + /** Network is or became disconnected */ + ICD_STATE_DISCONNECTED = 0, + /** Establishing network connection */ + ICD_STATE_CONNECTING = 1, + /** Network is connected */ + ICD_STATE_CONNECTED = 2, + /** Network is being disconnected */ + ICD_STATE_DISCONNECTING = 3, + /** service provider module informs about enabled limited connectivity */ + ICD_STATE_LIMITED_CONN_ENABLED = 4, + /** service provider module informs about disabled limited connectivity */ + ICD_STATE_LIMITED_CONN_DISABLED = 5, + + /** Network searching started */ + ICD_STATE_SEARCH_START = 8, + /** Network searching stopped */ + ICD_STATE_SEARCH_STOP = 9, + + /** Internal network state, IP address(es) has/have been acquired */ + ICD_STATE_INTERNAL_ADDRESS_ACQUIRED = 15 +}; + + +#ifdef __cplusplus +} +#endif + +#endif /* __ICD2_LIGHT_H__ */ diff --git a/yandex-traffic-core.pri b/yandex-traffic-core.pri index 425c2b1..485b2c6 100644 --- a/yandex-traffic-core.pri +++ b/yandex-traffic-core.pri @@ -1,6 +1,8 @@ HEADERS += $$PWD/regions.hpp $$PWD/settings.hpp $$PWD/traffic.hpp $$PWD/http_fetcher.hpp $$PWD/connection.hpp SOURCES += $$PWD/regions.cpp $$PWD/settings.cpp $$PWD/traffic.cpp $$PWD/http_fetcher.cpp $$PWD/connection.cpp +HEADERS += $$PWD/icd2_light.h + QT += network xml dbus CONFIG += qdbus -- 1.7.9.5