|
1 /**************************************************************************** |
|
2 ** |
|
3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
|
4 ** All rights reserved. |
|
5 ** Contact: Nokia Corporation (qt-info@nokia.com) |
|
6 ** |
|
7 ** This file is part of the Qt Mobility Components. |
|
8 ** |
|
9 ** $QT_BEGIN_LICENSE:LGPL$ |
|
10 ** No Commercial Usage |
|
11 ** This file contains pre-release code and may not be distributed. |
|
12 ** You may use this file in accordance with the terms and conditions |
|
13 ** contained in the Technology Preview License Agreement accompanying |
|
14 ** this package. |
|
15 ** |
|
16 ** GNU Lesser General Public License Usage |
|
17 ** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 ** General Public License version 2.1 as published by the Free Software |
|
19 ** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 ** packaging of this file. Please review the following information to |
|
21 ** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 ** |
|
24 ** In addition, as a special exception, Nokia gives you certain additional |
|
25 ** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 ** |
|
28 ** If you have questions regarding the use of this file, please contact |
|
29 ** Nokia at qt-info@nokia.com. |
|
30 ** |
|
31 ** |
|
32 ** |
|
33 ** |
|
34 ** |
|
35 ** |
|
36 ** |
|
37 ** |
|
38 ** $QT_END_LICENSE$ |
|
39 ** |
|
40 ****************************************************************************/ |
|
41 |
|
42 #include "qgenericengine_p.h" |
|
43 #include "qnetworkconfiguration_p.h" |
|
44 |
|
45 #include <QtCore/qthread.h> |
|
46 #include <QtCore/qmutex.h> |
|
47 #include <QtCore/qcoreapplication.h> |
|
48 #include <QtCore/qstringlist.h> |
|
49 |
|
50 #include <QtCore/qdebug.h> |
|
51 |
|
52 #ifdef Q_OS_WIN |
|
53 #include "qnetworksessionengine_win_p.h" |
|
54 #endif |
|
55 |
|
56 #ifdef Q_OS_LINUX |
|
57 #include <sys/socket.h> |
|
58 #include <sys/ioctl.h> |
|
59 #include <net/if.h> |
|
60 #include <net/if_arp.h> |
|
61 #include <unistd.h> |
|
62 #endif |
|
63 |
|
64 QTM_BEGIN_NAMESPACE |
|
65 |
|
66 Q_GLOBAL_STATIC(QGenericEngine, genericEngine) |
|
67 |
|
68 static QString qGetInterfaceType(const QString &interface) |
|
69 { |
|
70 #ifdef Q_OS_WIN32 |
|
71 unsigned long oid; |
|
72 DWORD bytesWritten; |
|
73 |
|
74 NDIS_MEDIUM medium; |
|
75 NDIS_PHYSICAL_MEDIUM physicalMedium; |
|
76 |
|
77 HANDLE handle = CreateFile((TCHAR *)QString("\\\\.\\%1").arg(interface).utf16(), 0, |
|
78 FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0); |
|
79 if (handle == INVALID_HANDLE_VALUE) |
|
80 return QLatin1String("Unknown"); |
|
81 |
|
82 oid = OID_GEN_MEDIA_SUPPORTED; |
|
83 bytesWritten = 0; |
|
84 bool result = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, sizeof(oid), |
|
85 &medium, sizeof(medium), &bytesWritten, 0); |
|
86 if (!result) { |
|
87 CloseHandle(handle); |
|
88 return QLatin1String("Unknown"); |
|
89 } |
|
90 |
|
91 oid = OID_GEN_PHYSICAL_MEDIUM; |
|
92 bytesWritten = 0; |
|
93 result = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, sizeof(oid), |
|
94 &physicalMedium, sizeof(physicalMedium), &bytesWritten, 0); |
|
95 if (!result) { |
|
96 CloseHandle(handle); |
|
97 |
|
98 if (medium == NdisMedium802_3) |
|
99 return QLatin1String("Ethernet"); |
|
100 else |
|
101 return QLatin1String("Unknown"); |
|
102 } |
|
103 |
|
104 CloseHandle(handle); |
|
105 |
|
106 if (medium == NdisMedium802_3) { |
|
107 switch (physicalMedium) { |
|
108 case NdisPhysicalMediumWirelessLan: |
|
109 return QLatin1String("WLAN"); |
|
110 case NdisPhysicalMediumBluetooth: |
|
111 return QLatin1String("Bluetooth"); |
|
112 case NdisPhysicalMediumWiMax: |
|
113 return QLatin1String("WiMAX"); |
|
114 default: |
|
115 #ifdef BEARER_MANAGEMENT_DEBUG |
|
116 qDebug() << "Physical Medium" << physicalMedium; |
|
117 #endif |
|
118 return QLatin1String("Ethernet"); |
|
119 } |
|
120 } |
|
121 |
|
122 #ifdef BEARER_MANAGEMENT_DEBUG |
|
123 qDebug() << medium << physicalMedium; |
|
124 #endif |
|
125 #elif defined(Q_OS_LINUX) |
|
126 int sock = socket(AF_INET, SOCK_DGRAM, 0); |
|
127 |
|
128 ifreq request; |
|
129 strncpy(request.ifr_name, interface.toLocal8Bit().data(), sizeof(request.ifr_name)); |
|
130 int result = ioctl(sock, SIOCGIFHWADDR, &request); |
|
131 close(sock); |
|
132 |
|
133 if (result >= 0 && request.ifr_hwaddr.sa_family == ARPHRD_ETHER) |
|
134 return QLatin1String("Ethernet"); |
|
135 #else |
|
136 Q_UNUSED(interface); |
|
137 #endif |
|
138 |
|
139 return QLatin1String("Unknown"); |
|
140 } |
|
141 |
|
142 QGenericEngine::QGenericEngine(QObject *parent) |
|
143 : QNetworkSessionEngine(parent) |
|
144 { |
|
145 connect(&pollTimer, SIGNAL(timeout()), this, SIGNAL(configurationsChanged())); |
|
146 pollTimer.setInterval(10000); |
|
147 } |
|
148 |
|
149 QGenericEngine::~QGenericEngine() |
|
150 { |
|
151 } |
|
152 |
|
153 QList<QNetworkConfigurationPrivate *> QGenericEngine::getConfigurations(bool *ok) |
|
154 { |
|
155 if (ok) |
|
156 *ok = true; |
|
157 |
|
158 QList<QNetworkConfigurationPrivate *> foundConfigurations; |
|
159 |
|
160 // Immediately after connecting with a wireless access point |
|
161 // QNetworkInterface::allInterfaces() will sometimes return an empty list. Calling it again a |
|
162 // second time results in a non-empty list. If we loose interfaces we will end up removing |
|
163 // network configurations which will break current sessions. |
|
164 QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces(); |
|
165 if (interfaces.isEmpty()) |
|
166 interfaces = QNetworkInterface::allInterfaces(); |
|
167 |
|
168 // create configuration for each interface |
|
169 while (!interfaces.isEmpty()) { |
|
170 QNetworkInterface interface = interfaces.takeFirst(); |
|
171 |
|
172 if (!interface.isValid()) |
|
173 continue; |
|
174 |
|
175 // ignore loopback interface |
|
176 if (interface.flags() & QNetworkInterface::IsLoopBack) |
|
177 continue; |
|
178 |
|
179 // ignore WLAN interface handled in separate engine |
|
180 if (qGetInterfaceType(interface.name()) == "WLAN") |
|
181 continue; |
|
182 |
|
183 QNetworkConfigurationPrivate *cpPriv = new QNetworkConfigurationPrivate; |
|
184 const QString humanReadableName = interface.humanReadableName(); |
|
185 cpPriv->name = humanReadableName.isEmpty() ? interface.name() : humanReadableName; |
|
186 cpPriv->isValid = true; |
|
187 |
|
188 uint identifier; |
|
189 if (interface.index()) |
|
190 identifier = qHash(QLatin1String("NLA:") + QString::number(interface.index())); |
|
191 else |
|
192 identifier = qHash(QLatin1String("NLA:") + interface.hardwareAddress()); |
|
193 |
|
194 cpPriv->id = QString::number(identifier); |
|
195 cpPriv->state = QNetworkConfiguration::Discovered; |
|
196 cpPriv->type = QNetworkConfiguration::InternetAccessPoint; |
|
197 if (interface.name().isEmpty()) |
|
198 cpPriv->bearer = QLatin1String("Unknown"); |
|
199 else |
|
200 cpPriv->bearer = qGetInterfaceType(interface.name()); |
|
201 |
|
202 if((interface.flags() & QNetworkInterface::IsUp) && !interface.addressEntries().isEmpty()) |
|
203 cpPriv->state |= QNetworkConfiguration::Active; |
|
204 |
|
205 configurationInterface[identifier] = interface.name(); |
|
206 |
|
207 foundConfigurations.append(cpPriv); |
|
208 } |
|
209 |
|
210 pollTimer.start(); |
|
211 |
|
212 return foundConfigurations; |
|
213 } |
|
214 |
|
215 QString QGenericEngine::getInterfaceFromId(const QString &id) |
|
216 { |
|
217 return configurationInterface.value(id.toUInt()); |
|
218 } |
|
219 |
|
220 bool QGenericEngine::hasIdentifier(const QString &id) |
|
221 { |
|
222 return configurationInterface.contains(id.toUInt()); |
|
223 } |
|
224 |
|
225 /*QString QGenericEngine::bearerName(const QString &id) |
|
226 { |
|
227 QString interface = getInterfaceFromId(id); |
|
228 |
|
229 if (interface.isEmpty()) |
|
230 return QLatin1String("Unknown"); |
|
231 |
|
232 return qGetInterfaceType(interface); |
|
233 }*/ |
|
234 |
|
235 void QGenericEngine::connectToId(const QString &id) |
|
236 { |
|
237 emit connectionError(id, OperationNotSupported); |
|
238 } |
|
239 |
|
240 void QGenericEngine::disconnectFromId(const QString &id) |
|
241 { |
|
242 emit connectionError(id, OperationNotSupported); |
|
243 } |
|
244 |
|
245 void QGenericEngine::requestUpdate() |
|
246 { |
|
247 emit configurationsChanged(); |
|
248 } |
|
249 |
|
250 QGenericEngine *QGenericEngine::instance() |
|
251 { |
|
252 return genericEngine(); |
|
253 } |
|
254 |
|
255 #include "moc_qgenericengine_p.cpp" |
|
256 QTM_END_NAMESPACE |
|
257 |