Log connection state transitions.
[yandex-traffic] / connection.cpp
1 #include <QtDBus>
2
3 #include "connection.hpp"
4 #include "icd2_light.h"
5 #include "log.hpp"
6
7 static ConnectionChecker *_instance = NULL;
8
9
10 // --------------------------------------------------
11 // ConnectionChecker singleton
12 // --------------------------------------------------
13 ConnectionChecker *ConnectionChecker::instance ()
14 {
15     if (!_instance)
16         _instance = new ConnectionChecker;
17     return _instance;
18 }
19
20
21 ConnectionChecker::ConnectionChecker ()
22     : _bus (QDBusConnection::systemBus ())
23 {
24     _connected = true;
25
26     _itf = new QDBusInterface (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, _bus);
27     _bus.connect (ICD_DBUS_API_INTERFACE, ICD_DBUS_API_PATH, ICD_DBUS_API_INTERFACE, ICD_DBUS_API_STATE_SIG,
28                   this, SLOT (stateSignal (const QDBusMessage&)));
29     _conn_counter = 0;
30
31     requestState ();
32 }
33
34
35 void ConnectionChecker::requestState ()
36 {
37     QDBusMessage reply = _itf->call (ICD_DBUS_API_STATE_REQ);
38
39     // If there is no connection, we get no reply at all
40     if (!reply.arguments ().value (0).toUInt ())
41         updateState (false);
42 }
43
44
45 void ConnectionChecker::stateSignal (const QDBusMessage& msg)
46 {
47     if (msg.arguments ().count () != 8)
48         return;
49
50     unsigned int state = msg.arguments ().value (7).value<unsigned int>();
51     QString net = msg.arguments ().value (3).toString ();
52
53     if (state == ICD_STATE_CONNECTED)
54         _conn_counter++;
55     if (state == ICD_STATE_DISCONNECTED)
56         _conn_counter--;
57
58     Log::instance ()->add (QString ("stateSignal: state = %1, net = %2, counter = %3").arg (state).arg (net).arg (_conn_counter));
59
60     if (state == ICD_STATE_CONNECTED || !_conn_counter)
61         updateState (state == ICD_STATE_CONNECTED, net);
62 }
63
64
65 void ConnectionChecker::updateState (bool new_state, const QString& net_type)
66 {
67     network_type_t new_net = Net_None;
68
69     Log::instance ()->add (QString ("ConnectionChecker::updateState (%1, %2)").arg (new_state ? "connected" : "not connected").arg (net_type));
70
71     if (new_state != _connected) {
72         _connected = new_state;
73         emit connected (_connected);
74     }
75
76     if (_connected) {
77         if (net_type.startsWith ("WLAN"))
78             new_net = Net_WLAN;
79         else if (net_type.startsWith ("GPRS") || net_type.startsWith ("DUN_GSM"))
80             new_net = Net_GSM;
81     }
82
83     if (new_net != _net_type) {
84         _net_type = new_net;
85         type_changed (_net_type);
86     }
87 }
88
89
90 bool ConnectionChecker::checkConnection (bool allow_gsm, bool allow_wifi)
91 {
92     if (!_connected)
93         return false;
94
95     switch (_net_type) {
96         case Net_None:
97             Log::instance ()->add ("checkConnection: Net_None, allow");
98             return true;
99         case Net_WLAN:
100             Log::instance ()->add (QString ("checkConnection: Net_WLAN, allow = %1").arg (allow_wifi ? "true" : "false"));
101             return allow_wifi;
102         case Net_GSM:
103             Log::instance ()->add (QString ("checkConnection: Net_GSM, allow = %1").arg (allow_gsm ? "true" : "false"));
104             return allow_gsm;
105         default:
106             Log::instance ()->add ("checkConnection: unknown, allow");
107             return true;
108     }
109 }