qtmobility/src/bearer/qgenericengine.cpp
changeset 0 cfcbf08528c4
child 3 87be51aa5b5b
equal deleted inserted replaced
-1:000000000000 0:cfcbf08528c4
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2009 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 #endif
       
    62 
       
    63 QTM_BEGIN_NAMESPACE
       
    64 
       
    65 Q_GLOBAL_STATIC(QGenericEngine, genericEngine)
       
    66 
       
    67 static QString qGetInterfaceType(const QString &interface)
       
    68 {
       
    69 #ifdef Q_OS_WIN32
       
    70     unsigned long oid;
       
    71     DWORD bytesWritten;
       
    72 
       
    73     NDIS_MEDIUM medium;
       
    74     NDIS_PHYSICAL_MEDIUM physicalMedium;
       
    75 
       
    76     HANDLE handle = CreateFile((TCHAR *)QString("\\\\.\\%1").arg(interface).utf16(), 0,
       
    77                                FILE_SHARE_READ, 0, OPEN_EXISTING, 0, 0);
       
    78     if (handle == INVALID_HANDLE_VALUE)
       
    79         return QLatin1String("Unknown");
       
    80 
       
    81     oid = OID_GEN_MEDIA_SUPPORTED;
       
    82     bytesWritten = 0;
       
    83     bool result = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, sizeof(oid),
       
    84                                   &medium, sizeof(medium), &bytesWritten, 0);
       
    85     if (!result) {
       
    86         CloseHandle(handle);
       
    87         return QLatin1String("Unknown");
       
    88     }
       
    89 
       
    90     oid = OID_GEN_PHYSICAL_MEDIUM;
       
    91     bytesWritten = 0;
       
    92     result = DeviceIoControl(handle, IOCTL_NDIS_QUERY_GLOBAL_STATS, &oid, sizeof(oid),
       
    93                              &physicalMedium, sizeof(physicalMedium), &bytesWritten, 0);
       
    94     if (!result) {
       
    95         CloseHandle(handle);
       
    96 
       
    97         if (medium == NdisMedium802_3)
       
    98             return QLatin1String("Ethernet");
       
    99         else
       
   100             return QLatin1String("Unknown");
       
   101     }
       
   102 
       
   103     CloseHandle(handle);
       
   104 
       
   105     if (medium == NdisMedium802_3) {
       
   106         switch (physicalMedium) {
       
   107         case NdisPhysicalMediumWirelessLan:
       
   108             return QLatin1String("WLAN");
       
   109         case NdisPhysicalMediumBluetooth:
       
   110             return QLatin1String("Bluetooth");
       
   111         case NdisPhysicalMediumWiMax:
       
   112             return QLatin1String("WiMAX");
       
   113         default:
       
   114 #ifdef BEARER_MANAGEMENT_DEBUG
       
   115             qDebug() << "Physical Medium" << physicalMedium;
       
   116 #endif
       
   117             return QLatin1String("Ethernet");
       
   118         }
       
   119     }
       
   120 
       
   121 #ifdef BEARER_MANAGEMENT_DEBUG
       
   122     qDebug() << medium << physicalMedium;
       
   123 #endif
       
   124 #elif defined(Q_OS_LINUX)
       
   125     int sock = socket(AF_INET, SOCK_DGRAM, 0);
       
   126 
       
   127     ifreq request;
       
   128     strncpy(request.ifr_name, interface.toLocal8Bit().data(), sizeof(request.ifr_name));
       
   129     int result = ioctl(sock, SIOCGIFHWADDR, &request);
       
   130     close(sock);
       
   131 
       
   132     if (result >= 0 && request.ifr_hwaddr.sa_family == ARPHRD_ETHER)
       
   133         return QLatin1String("Ethernet");
       
   134 #else
       
   135     Q_UNUSED(interface);
       
   136 #endif
       
   137 
       
   138     return QLatin1String("Unknown");
       
   139 }
       
   140 
       
   141 QGenericEngine::QGenericEngine(QObject *parent)
       
   142 :   QNetworkSessionEngine(parent)
       
   143 {
       
   144     connect(&pollTimer, SIGNAL(timeout()), this, SIGNAL(configurationsChanged()));
       
   145     pollTimer.setInterval(10000);
       
   146 }
       
   147 
       
   148 QGenericEngine::~QGenericEngine()
       
   149 {
       
   150 }
       
   151 
       
   152 QList<QNetworkConfigurationPrivate *> QGenericEngine::getConfigurations(bool *ok)
       
   153 {
       
   154     if (ok)
       
   155         *ok = true;
       
   156 
       
   157     QList<QNetworkConfigurationPrivate *> foundConfigurations;
       
   158 
       
   159     // Immediately after connecting with a wireless access point
       
   160     // QNetworkInterface::allInterfaces() will sometimes return an empty list. Calling it again a
       
   161     // second time results in a non-empty list. If we loose interfaces we will end up removing
       
   162     // network configurations which will break current sessions.
       
   163     QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();
       
   164     if (interfaces.isEmpty())
       
   165         interfaces = QNetworkInterface::allInterfaces();
       
   166 
       
   167     // create configuration for each interface
       
   168     while (!interfaces.isEmpty()) {
       
   169         QNetworkInterface interface = interfaces.takeFirst();
       
   170 
       
   171         if (!interface.isValid())
       
   172             continue;
       
   173 
       
   174         // ignore loopback interface
       
   175         if (interface.flags() & QNetworkInterface::IsLoopBack)
       
   176             continue;
       
   177 
       
   178         // ignore WLAN interface handled in seperate engine
       
   179         if (qGetInterfaceType(interface.name()) == "WLAN")
       
   180             continue;
       
   181 
       
   182         QNetworkConfigurationPrivate *cpPriv = new QNetworkConfigurationPrivate;
       
   183         const QString humanReadableName = interface.humanReadableName();
       
   184         cpPriv->name = humanReadableName.isEmpty() ? interface.name() : humanReadableName;
       
   185         cpPriv->isValid = true;
       
   186 
       
   187         uint identifier;
       
   188         if (interface.index())
       
   189             identifier = qHash(QLatin1String("NLA:") + QString::number(interface.index()));
       
   190         else
       
   191             identifier = qHash(QLatin1String("NLA:") + interface.hardwareAddress());
       
   192 
       
   193         cpPriv->id = QString::number(identifier);
       
   194         cpPriv->state = QNetworkConfiguration::Discovered;
       
   195         cpPriv->type = QNetworkConfiguration::InternetAccessPoint;
       
   196         if (interface.name().isEmpty())
       
   197             cpPriv->bearer = QLatin1String("Unknown");
       
   198         else
       
   199             cpPriv->bearer = qGetInterfaceType(interface.name());
       
   200 
       
   201         if (interface.flags() & QNetworkInterface::IsUp)
       
   202             cpPriv->state |= QNetworkConfiguration::Active;
       
   203 
       
   204         configurationInterface[identifier] = interface.name();
       
   205 
       
   206         foundConfigurations.append(cpPriv);
       
   207     }
       
   208 
       
   209     pollTimer.start();
       
   210 
       
   211     return foundConfigurations;
       
   212 }
       
   213 
       
   214 QString QGenericEngine::getInterfaceFromId(const QString &id)
       
   215 {
       
   216     return configurationInterface.value(id.toUInt());
       
   217 }
       
   218 
       
   219 bool QGenericEngine::hasIdentifier(const QString &id)
       
   220 {
       
   221     return configurationInterface.contains(id.toUInt());
       
   222 }
       
   223 
       
   224 /*QString QGenericEngine::bearerName(const QString &id)
       
   225 {
       
   226     QString interface = getInterfaceFromId(id);
       
   227 
       
   228     if (interface.isEmpty())
       
   229         return QLatin1String("Unknown");
       
   230 
       
   231     return qGetInterfaceType(interface);
       
   232 }*/
       
   233 
       
   234 void QGenericEngine::connectToId(const QString &id)
       
   235 {
       
   236     emit connectionError(id, OperationNotSupported);
       
   237 }
       
   238 
       
   239 void QGenericEngine::disconnectFromId(const QString &id)
       
   240 {
       
   241     emit connectionError(id, OperationNotSupported);
       
   242 }
       
   243 
       
   244 void QGenericEngine::requestUpdate()
       
   245 {
       
   246     emit configurationsChanged();
       
   247 }
       
   248 
       
   249 QGenericEngine *QGenericEngine::instance()
       
   250 {
       
   251     return genericEngine();
       
   252 }
       
   253 
       
   254 #include "moc_qgenericengine_p.cpp"
       
   255 QTM_END_NAMESPACE
       
   256