ginebra2/sym_iap_util.h
changeset 10 232fbd5a2dcb
parent 6 1c3b8676e58c
child 13 491a1d15372f
equal deleted inserted replaced
6:1c3b8676e58c 10:232fbd5a2dcb
     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 examples of the Qt Toolkit.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:LGPL$
       
    10 ** Commercial Usage
       
    11 ** Licensees holding valid Qt Commercial licenses may use this file in
       
    12 ** accordance with the Qt Commercial License Agreement provided with the
       
    13 ** Software or, alternatively, in accordance with the terms contained in
       
    14 ** a written agreement between you and Nokia.
       
    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 ** GNU General Public License Usage
       
    29 ** Alternatively, this file may be used under the terms of the GNU
       
    30 ** General Public License version 3.0 as published by the Free Software
       
    31 ** Foundation and appearing in the file LICENSE.GPL included in the
       
    32 ** packaging of this file.  Please review the following information to
       
    33 ** ensure the GNU General Public License version 3.0 requirements will be
       
    34 ** met: http://www.gnu.org/copyleft/gpl.html.
       
    35 **
       
    36 ** If you have questions regarding the use of this file, please contact
       
    37 ** Nokia at qt-info@nokia.com.
       
    38 ** $QT_END_LICENSE$
       
    39 **
       
    40 ****************************************************************************/
       
    41 #ifndef QSYM_IAP_UTIL_H
       
    42 #define QSYM_IAP_UTIL_H
       
    43 
       
    44 // Symbian
       
    45 #include <utf.h>
       
    46 #include <es_sock.h>
       
    47 #include <in_sock.h>
       
    48 #include <es_enum.h>
       
    49 #include <in_iface.h>
       
    50 #include <commdbconnpref.h>
       
    51 #include <e32cmn.h>
       
    52 
       
    53 // OpenC
       
    54 #include <sys/socket.h>
       
    55 #include <net/if.h>
       
    56 
       
    57 //Qt
       
    58 #include <QSettings>
       
    59 #include <QStringList>
       
    60 //#include <QTextCodec>
       
    61 
       
    62 _LIT(KIapNameSetting, "IAP\\Name");             // text - mandatory
       
    63 _LIT(KIapDialogPref, "IAP\\DialogPref");        // TUnit32 - optional
       
    64 _LIT(KIapService, "IAP\\IAPService");           // TUnit32 - mandatory
       
    65 _LIT(KIapServiceType, "IAP\\IAPServiceType");   // text - mandatory
       
    66 _LIT(KIapBearer, "IAP\\IAPBearer");             // TUint32 - optional
       
    67 _LIT(KIapBearerType, "IAP\\IAPBearerType");     // text - optional
       
    68 _LIT(KIapNetwork, "IAP\\IAPNetwork");           // TUint32 - optional
       
    69 
       
    70 const QLatin1String qtOrganizationTag("Trolltech");
       
    71 const QLatin1String qtNetworkModuleTag("QtNetwork");
       
    72 const QLatin1String iapGroupTag("IAP");
       
    73 const QLatin1String iapNamesArrayTag("Names");
       
    74 const QLatin1String iapNameItemTag("Name");
       
    75 
       
    76 static QTextCodec *utf16LETextCodec = 0;
       
    77 
       
    78 void clearIapNamesSettings(QSettings &settings) {
       
    79     settings.beginGroup(qtNetworkModuleTag);
       
    80         settings.beginGroup(iapGroupTag);
       
    81            settings.remove(iapNamesArrayTag);
       
    82         settings.endGroup();
       
    83     settings.endGroup();
       
    84 }
       
    85 
       
    86 void writeIapNamesSettings(QSettings &settings, const QStringList& iapNames) {
       
    87     clearIapNamesSettings(settings);
       
    88     settings.beginGroup(qtNetworkModuleTag);
       
    89         settings.beginGroup(iapGroupTag);
       
    90             settings.beginWriteArray(iapNamesArrayTag);
       
    91             for (int index = 0; index < iapNames.size(); ++index) {
       
    92                 settings.setArrayIndex(index);
       
    93                 settings.setValue(iapNameItemTag, iapNames.at(index));
       
    94             }
       
    95             settings.endArray();
       
    96         settings.endGroup();
       
    97     settings.endGroup();
       
    98 }
       
    99 
       
   100 void readIapNamesSettings(QSettings &settings, QStringList& iapNames) {
       
   101     settings.beginGroup(qtNetworkModuleTag);
       
   102         settings.beginGroup(iapGroupTag);
       
   103             int last = settings.beginReadArray(iapNamesArrayTag);
       
   104             for (int index = 0; index < last; ++index) {
       
   105                 settings.setArrayIndex(index);
       
   106                 iapNames.append(settings.value(iapNameItemTag).toString());
       
   107             }
       
   108             settings.endArray();
       
   109         settings.endGroup();
       
   110     settings.endGroup();
       
   111 }
       
   112 
       
   113 static QString qt_TNameToQString(TName data) {
       
   114     if (utf16LETextCodec == 0)
       
   115         utf16LETextCodec = QTextCodec::codecForName("UTF-16LE");
       
   116 
       
   117     QByteArray tmpByteArray = QByteArray::fromRawData((char*)(data.PtrZ()), data.Length() * 2);
       
   118     return utf16LETextCodec->toUnicode(tmpByteArray);
       
   119 }
       
   120 
       
   121 static QString qt_InterfaceInfoL()
       
   122 {
       
   123     QString output;
       
   124 
       
   125     TBuf8<512> buffer;
       
   126     TBuf<128> t;
       
   127     TAutoClose<RSocketServ> ss;
       
   128     User::LeaveIfError(ss.iObj.Connect());
       
   129     ss.PushL();
       
   130 
       
   131     TAutoClose<RSocket> sock;
       
   132     User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
       
   133     sock.PushL();
       
   134 
       
   135     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumInterfaces, KSolInetIfCtrl));
       
   136 
       
   137     TProtocolDesc in;
       
   138     User::LeaveIfError(sock.iObj.Info(in));
       
   139     printf("EPOC32 IP Configuration TCPIP Version %d.%d.%d\n", in.iVersion.iMajor, in.iVersion.iMinor, in.iVersion.iBuild);
       
   140 
       
   141     TPckgBuf<TSoInetInterfaceInfo> info, next;
       
   142 
       
   143     TInt res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, info);
       
   144     if (res!=KErrNone)
       
   145         User::Leave(res);
       
   146     TInt count = 0;
       
   147     while (res==KErrNone) {
       
   148         res=sock.iObj.GetOpt(KSoInetNextInterface, KSolInetIfCtrl, next);
       
   149 
       
   150         if (info().iName != _L("") && info().iName != _L("loop6") && info().iName != _L("loop4")) {
       
   151             printf("Interface %d\n", count++);
       
   152 
       
   153             printf("Name \"%s\"\n", qt_TNameToQString(info().iName).toLatin1().data());
       
   154             printf("NIF tag \"%s\"\n", qt_TNameToQString(info().iTag).toLatin1().data());
       
   155 
       
   156             printf("State ");
       
   157             switch (info().iState)
       
   158             {
       
   159                 case EIfPending:
       
   160                     printf("pending\n");
       
   161                     break;
       
   162                 case EIfUp:
       
   163                     printf("up\n");
       
   164                     break;
       
   165                 case EIfBusy:
       
   166                     printf("busy\n");
       
   167                     break;
       
   168                 default:
       
   169                     printf("down\n");
       
   170                     break;
       
   171             }
       
   172 
       
   173             printf("Mtu %d\n", info().iMtu);
       
   174             printf("Speed Metric %d\n", info().iSpeedMetric);
       
   175 
       
   176             printf("Features:");
       
   177             info().iFeatures & KIfIsLoopback         ? printf(" loopback") : printf("");
       
   178             info().iFeatures & KIfIsDialup           ? printf(" dialup") : printf("");
       
   179             info().iFeatures & KIfIsPointToPoint     ? printf(" pointtopoint") : printf("");
       
   180             info().iFeatures & KIfCanBroadcast       ? printf(" canbroadcast") : printf("");
       
   181             info().iFeatures & KIfCanMulticast       ? printf(" canmulticast") : printf("");
       
   182             info().iFeatures & KIfCanSetMTU          ? printf(" cansetmtu") : printf("");
       
   183             info().iFeatures & KIfHasHardwareAddr    ? printf(" hardwareaddr") : printf("");
       
   184             info().iFeatures & KIfCanSetHardwareAddr ? printf(" cansethardwareaddr") : printf("");
       
   185             printf("\n");
       
   186 
       
   187             TName address;
       
   188             info().iAddress.Output(address);
       
   189             printf("Addr: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   190 
       
   191             if (info().iAddress.IsLinkLocal()) {
       
   192                 printf("  -link local\n");
       
   193             } else if (info().iAddress.IsSiteLocal()) {
       
   194                 printf("  -site local\n");
       
   195             } else {
       
   196                 printf("  -global\n");
       
   197             }
       
   198 
       
   199             info().iNetMask.Output(address);
       
   200             printf("Netmask %s\n", qt_TNameToQString(address).toLatin1().data());
       
   201 
       
   202             info().iBrdAddr.Output(address);
       
   203             printf("Broadcast address %s\n", qt_TNameToQString(address).toLatin1().data());
       
   204 
       
   205             info().iDefGate.Output(address);
       
   206             printf("Gatew: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   207 
       
   208             info().iNameSer1.Output(address);
       
   209             printf("DNS 1: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   210 
       
   211             info().iNameSer2.Output(address);
       
   212             printf("DNS 2: %s\n", qt_TNameToQString(address).toLatin1().data());
       
   213 
       
   214             if (info().iHwAddr.Family() != KAFUnspec) {
       
   215                 printf("Hardware address ");
       
   216                 TUint j;
       
   217                 for (j = sizeof(SSockAddr) ; j < sizeof(SSockAddr) + 6 ; ++j) {
       
   218                     if (j < (TUint)info().iHwAddr.Length()) {
       
   219                         printf("%02X", info().iHwAddr[j]);
       
   220                     } else {
       
   221                         printf("??");
       
   222                     }
       
   223                     if (j < sizeof(SSockAddr) + 5)
       
   224                         printf("-");
       
   225                     else
       
   226                         printf("\n");
       
   227                     }
       
   228                 }
       
   229             }
       
   230         if (res == KErrNone) {
       
   231             info = next;
       
   232             printf("\n");
       
   233         } else {
       
   234             printf("\n");
       
   235         }
       
   236     }
       
   237 
       
   238     sock.Pop();
       
   239     ss.Pop();
       
   240 
       
   241     return output;
       
   242 }
       
   243 
       
   244 static QString qt_RouteInfoL() {
       
   245     QString output;
       
   246     TAutoClose<RSocketServ> ss;
       
   247     User::LeaveIfError(ss.iObj.Connect());
       
   248     ss.PushL();
       
   249 
       
   250     TAutoClose<RSocket> sock;
       
   251     User::LeaveIfError(sock.iObj.Open(ss.iObj, _L("udp")));
       
   252     sock.PushL();
       
   253 
       
   254     TSoInetRouteInfo routeInfo;
       
   255     TPckg<TSoInetRouteInfo> routeInfoPkg(routeInfo);
       
   256 
       
   257     TName destAddr;
       
   258     TName netMask;
       
   259     TName gateway;
       
   260     TName ifAddr;
       
   261 
       
   262     // Begins enumeration of routes by setting this option
       
   263     User::LeaveIfError(sock.iObj.SetOpt(KSoInetEnumRoutes, KSolInetRtCtrl));
       
   264 
       
   265     // The TSoInetRouteInfo contains information for a new route each time GetOpt returns KErrNone
       
   266     for (TInt i = 0; sock.iObj.GetOpt(KSoInetNextRoute, KSolInetRtCtrl, routeInfoPkg) == KErrNone ; i++)
       
   267     {
       
   268       // Extract the destination and netmask
       
   269       routeInfo.iDstAddr.Output(destAddr);
       
   270       routeInfo.iNetMask.Output(netMask);
       
   271       routeInfo.iGateway.Output(gateway);
       
   272       routeInfo.iIfAddr.Output(ifAddr);
       
   273 /*
       
   274       if (destAddr.Length() <= 2)
       
   275           continue;
       
   276 
       
   277       if (netMask.Find(_L("255.255.255.255")) != KErrNotFound
       
   278               || netMask.Find(_L("0.0.0.0")) != KErrNotFound
       
   279               || netMask.Find(_L("ffff:ffff:ffff:ffff")) != KErrNotFound)
       
   280           continue;
       
   281 */
       
   282       printf("Route Info #[%i]\n", i);
       
   283       printf("DstAddr %s\n", qt_TNameToQString(destAddr).toLatin1().data());
       
   284       printf("NetMask %s\n", qt_TNameToQString(netMask).toLatin1().data());
       
   285       printf("Gateway %s\n", qt_TNameToQString(gateway).toLatin1().data());
       
   286       printf("IfAddr %s\n", qt_TNameToQString(ifAddr).toLatin1().data());
       
   287       printf("\n");
       
   288     }
       
   289 
       
   290     sock.Pop();
       
   291     ss.Pop();
       
   292 
       
   293     return output;
       
   294 }
       
   295 
       
   296 QString qt_TDesC2QStringL(const TDesC& aDescriptor)
       
   297 {
       
   298 #ifdef QT_NO_UNICODE
       
   299     return QString::fromLocal8Bit(aDescriptor.Ptr(), aDescriptor.Length());
       
   300 #else
       
   301     return QString::fromUtf16(aDescriptor.Ptr(), aDescriptor.Length());
       
   302 #endif
       
   303 }
       
   304 
       
   305 static bool qt_SetDefaultIapName(const QString &iapName, int &error) {
       
   306     struct ifreq ifReq;
       
   307     // clear structure
       
   308     memset(&ifReq, 0, sizeof(struct ifreq));
       
   309     // set IAP name value
       
   310     // make sure it is in UTF8
       
   311     strcpy(ifReq.ifr_name, iapName.toUtf8().data());
       
   312 
       
   313     if (setdefaultif(&ifReq) == 0) {
       
   314         // OK
       
   315         error = 0;
       
   316         return true;
       
   317     } else {
       
   318         error = errno;
       
   319         return false;
       
   320     }
       
   321 
       
   322 }
       
   323 static bool qt_SetDefaultSnapId(const int snapId, int &error) {
       
   324     struct ifreq ifReq;
       
   325     // clear structure
       
   326     memset(&ifReq, 0, sizeof(struct ifreq));
       
   327     // set SNAP ID value
       
   328     ifReq.ifr_ifru.snap_id = snapId;
       
   329 
       
   330     if (setdefaultif(&ifReq) == 0) {
       
   331         // OK
       
   332         error = 0;
       
   333         return true;
       
   334     } else {
       
   335         error = errno;
       
   336         return false;
       
   337     }
       
   338 
       
   339 }
       
   340 
       
   341 static void qt_SaveIapName(QSettings& settings, QStringList& iapNames, QString& iapNameValue) {
       
   342     if (iapNames.contains(iapNameValue) && iapNames.first() == iapNameValue) {
       
   343         // no need to update
       
   344     } else {
       
   345         if (iapNameValue != QString("Easy WLAN")) {
       
   346             // new selection alway on top
       
   347             iapNames.removeAll(iapNameValue);
       
   348             iapNames.prepend(iapNameValue);
       
   349             writeIapNamesSettings(settings, iapNames);
       
   350         } else {
       
   351             // Unbeliveable ... if IAP dodn't exist before
       
   352             // no matter what you choose from IAP selection list
       
   353             // you will get "Easy WLAN" as IAP name value
       
   354 
       
   355             // somehow commsdb is not in sync
       
   356         }
       
   357     }
       
   358 }
       
   359 
       
   360 static QString qt_OfferIapDialog() {
       
   361     TBuf8<256> iapName;
       
   362 
       
   363     RSocketServ socketServ;
       
   364     CleanupClosePushL(socketServ);
       
   365 
       
   366     RConnection connection;
       
   367     CleanupClosePushL(connection);
       
   368 
       
   369     socketServ.Connect();
       
   370     connection.Open(socketServ);
       
   371     connection.Start();
       
   372 
       
   373     connection.GetDesSetting(TPtrC(KIapNameSetting), iapName);
       
   374 
       
   375     //connection.Stop();
       
   376 
       
   377     iapName.ZeroTerminate();
       
   378     QString strIapName((char*)iapName.Ptr());
       
   379 
       
   380     int error = 0;
       
   381     if (!qt_SetDefaultIapName(strIapName, error)) {
       
   382         //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   383         strIapName = QString("");
       
   384     }
       
   385 
       
   386     CleanupStack::PopAndDestroy(&connection);
       
   387     CleanupStack::PopAndDestroy(&socketServ);
       
   388 
       
   389     return strIapName;
       
   390 }
       
   391 
       
   392 static QString qt_CheckForActiveConnection() {
       
   393     TUint count;
       
   394 
       
   395     RSocketServ serv;
       
   396     CleanupClosePushL(serv);
       
   397 
       
   398     RConnection conn;
       
   399     CleanupClosePushL(conn);
       
   400 
       
   401     serv.Connect();
       
   402     conn.Open(serv);
       
   403 
       
   404     TConnectionInfoBuf connInfo;
       
   405 
       
   406     TBuf8<256> iapName;
       
   407     TBuf8<256> iapServiceType;
       
   408 
       
   409     QString strIapName;
       
   410 
       
   411     if (conn.EnumerateConnections(count) == KErrNone) {
       
   412         if (count > 0) {
       
   413             for (TUint i = 1; i <= count; i++) {
       
   414                 if (conn.GetConnectionInfo(i, connInfo) == KErrNone) {
       
   415                     RConnection tempConn;
       
   416                     CleanupClosePushL(tempConn);
       
   417                     tempConn.Open(serv);
       
   418                     if (tempConn.Attach(connInfo, RConnection::EAttachTypeNormal) == KErrNone) {
       
   419                        tempConn.GetDesSetting(TPtrC(KIapNameSetting), iapName);
       
   420                        tempConn.GetDesSetting(TPtrC(KIapServiceType), iapServiceType);
       
   421                        //tempConn.Stop();
       
   422                        iapName.ZeroTerminate();
       
   423                        iapServiceType.ZeroTerminate();
       
   424 
       
   425 //                        if (iapServiceType.Find(_L8("LANService")) != KErrNotFound) {
       
   426 //                            activeLanConnectionFound = ETrue;
       
   427 //                            break;
       
   428 //                        }
       
   429                         strIapName = QString((char*)iapName.Ptr());
       
   430                         int error = 0;
       
   431                         if (!qt_SetDefaultIapName(strIapName, error)) {
       
   432                             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   433                             strIapName = QString("");
       
   434                         }
       
   435 
       
   436                         CleanupStack::PopAndDestroy(&tempConn);
       
   437                         break;
       
   438                     }
       
   439                 }
       
   440             }
       
   441         }
       
   442     }
       
   443 
       
   444     //conn.Stop();
       
   445 
       
   446     CleanupStack::PopAndDestroy(&conn);
       
   447     CleanupStack::PopAndDestroy(&serv);
       
   448 
       
   449     return strIapName;
       
   450 }
       
   451 
       
   452 static QString qt_CheckSettingsForConnection(QStringList& iapNames) {
       
   453     QString strIapName;
       
   454     for (int index = 0; index < iapNames.size(); ++index) {
       
   455         strIapName = iapNames.at(index);
       
   456         int error = 0;
       
   457         if (!qt_SetDefaultIapName(strIapName, error)) {
       
   458             //printf("failed setdefaultif @ %i with %s and errno = %d \n", __LINE__, strIapName.toUtf8().data(), error);
       
   459             strIapName = QString("");
       
   460         } else {
       
   461             return strIapName;
       
   462         }
       
   463     }
       
   464     return strIapName;
       
   465 }
       
   466 
       
   467 static void qt_SetDefaultIapL()
       
   468 {
       
   469     // settings @ /c/data/.config/Trolltech.com
       
   470     QSettings settings(QSettings::UserScope, qtOrganizationTag);
       
   471     // populate iap name list
       
   472     QStringList iapNames;
       
   473     readIapNamesSettings(settings, iapNames);
       
   474 
       
   475     QString iapNameValue;
       
   476 
       
   477     iapNameValue = qt_CheckForActiveConnection();
       
   478 
       
   479     if (!iapNameValue.isEmpty()) {
       
   480         qt_SaveIapName(settings, iapNames, iapNameValue);
       
   481         return;
       
   482     }
       
   483 
       
   484     iapNameValue = qt_CheckSettingsForConnection(iapNames);
       
   485 
       
   486     if (!iapNameValue.isEmpty()) {
       
   487         qt_SaveIapName(settings, iapNames, iapNameValue);
       
   488         return;
       
   489     }
       
   490 
       
   491     /*
       
   492      * no active LAN connections yet
       
   493      * no IAP in settings
       
   494      * offer IAP dialog to user
       
   495      */
       
   496     iapNameValue = qt_OfferIapDialog();
       
   497     qt_SaveIapName(settings, iapNames, iapNameValue);
       
   498     return;
       
   499 
       
   500 }
       
   501 
       
   502 static int qt_SetDefaultIap()
       
   503 {
       
   504 //#ifndef __WINS__
       
   505     TRAPD(err1, qt_SetDefaultIapL());
       
   506 //    TRAPD(err2, qt_InterfaceInfoL());
       
   507 //    TRAPD(err3, qt_RouteInfoL());
       
   508     return err1;
       
   509 //#else
       
   510     return 0; // IAP dialog not required for emulator
       
   511 //#endif
       
   512 }
       
   513 
       
   514 #endif // QSYM_IAP_UTIL_H