wlanutilities/wlanqtutilities/base/src/wlanqtutilsiapsettings.cpp
changeset 31 e8f4211554fb
child 39 7b3e49e4608a
equal deleted inserted replaced
30:ab513c8439db 31:e8f4211554fb
       
     1 /*
       
     2 * Copyright (c) 2009-2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description: 
       
    15 * WLAN Qt Utilities WLAN IAP settings handling.
       
    16 */
       
    17 
       
    18 // System includes
       
    19 
       
    20 #include <QScopedPointer>
       
    21 #include <QSharedPointer>
       
    22 #include <QVariant>
       
    23 
       
    24 #include <cmmanager_shim.h>
       
    25 #include <cmdestination_shim.h>
       
    26 #include <cmconnectionmethod_shim.h>
       
    27 
       
    28 // User includes
       
    29 
       
    30 #include "wlanqtutils.h"
       
    31 #include "wlanqtutilsap.h"
       
    32 #include "wlanqtutilsiap.h"
       
    33 #include "wlanqtutilsiapsettings.h"
       
    34 
       
    35 #include "OstTraceDefinitions.h"
       
    36 #ifdef OST_TRACE_COMPILER_IN_USE
       
    37 #include "wlanqtutilsiapsettingsTraces.h"
       
    38 #endif
       
    39 
       
    40 /*!
       
    41     \class WlanQtUtilsIapSettings
       
    42     \brief WLAN IAP related settings handler.
       
    43 
       
    44     Provides functionality to manipulate WLAN IAPs via the CM Manager Shim
       
    45     interface.
       
    46     
       
    47     NOTE: Shim functions may throw exceptions that need to be catched in
       
    48     this class.
       
    49 */
       
    50 
       
    51 // External function prototypes
       
    52 
       
    53 // Local constants
       
    54 
       
    55 // WEP key lengths used to determine key format
       
    56 static const int WepHex64BitMaxLength = 10;
       
    57 static const int WepHex128BitMaxLength = 26;
       
    58 static const int WepAscii64BitMaxLength = 5;
       
    59 static const int WepAscii128BitMaxLength = 13;
       
    60 
       
    61 // ======== LOCAL FUNCTIONS ========
       
    62 
       
    63 // ======== MEMBER FUNCTIONS ========
       
    64 
       
    65 /*!
       
    66     Constructor.
       
    67     
       
    68     @param [in] parent Parent object.
       
    69 */
       
    70 
       
    71 WlanQtUtilsIapSettings::WlanQtUtilsIapSettings(QObject *parent) :
       
    72     QObject(parent),
       
    73     mCmManager(new CmManagerShim())
       
    74 {
       
    75     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_WLANQTUTILSIAPSETTINGS_ENTRY);
       
    76     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_WLANQTUTILSIAPSETTINGS_EXIT);
       
    77 }
       
    78 
       
    79 /*!
       
    80     Destructor.
       
    81 */
       
    82 
       
    83 WlanQtUtilsIapSettings::~WlanQtUtilsIapSettings()
       
    84 {
       
    85     OstTraceFunctionEntry0(DUP1_WLANQTUTILSIAPSETTINGS_WLANQTUTILSIAPSETTINGS_ENTRY);
       
    86     
       
    87     delete mCmManager;
       
    88     
       
    89     OstTraceFunctionExit0(DUP1_WLANQTUTILSIAPSETTINGS_WLANQTUTILSIAPSETTINGS_EXIT);
       
    90 }
       
    91 
       
    92 /*!
       
    93     Fetch all WLAN IAPs.
       
    94     
       
    95     @param [out] iapList List of WLAN IAPs.
       
    96 */
       
    97 
       
    98 void WlanQtUtilsIapSettings::fetchIaps(
       
    99     QList< QSharedPointer<WlanQtUtilsIap> > &iapList) const
       
   100 {
       
   101     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_FETCHIAPS_ENTRY);
       
   102     
       
   103     // Clear the list content first for safety
       
   104     iapList.clear();
       
   105     
       
   106     QList<uint> iapIds;
       
   107     try {
       
   108         mCmManager->connectionMethod(iapIds, false);
       
   109     } catch (const std::exception &ex) {
       
   110         int error = qt_symbian_exception2Error(ex);
       
   111         OstTrace1(
       
   112             TRACE_NORMAL,
       
   113             WLANQTUTILSIAPSETTINGS_FETCHIAPS_EXCEPTION,
       
   114             "WlanQtUtilsIapSettings::fetchIaps exception;error=%d",
       
   115             error);
       
   116     }
       
   117 
       
   118     foreach (int iapId, iapIds) {
       
   119         QSharedPointer<WlanQtUtilsIap> wlanIap = fetchIap(iapId);
       
   120         if (wlanIap) {
       
   121             iapList.append(wlanIap);
       
   122         }
       
   123     }
       
   124     
       
   125     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_FETCHIAPS_EXIT);
       
   126 }
       
   127 
       
   128 /*!
       
   129     Fetch WLAN IAP with the given ID
       
   130     
       
   131     @param [in] iapId ID of IAP to fetch.
       
   132     
       
   133     @return Found IAP, NULL if not found.
       
   134 */
       
   135 
       
   136 QSharedPointer<WlanQtUtilsIap> WlanQtUtilsIapSettings::fetchIap(int iapId) const
       
   137 {
       
   138     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_FETCHIAP_ENTRY);
       
   139     
       
   140     OstTrace1(
       
   141         TRACE_NORMAL,
       
   142         WLANQTUTILSIAPSETTINGS_FETCHIAP,
       
   143         "WlanQtUtilsIapSettings::fetchIap;iapId=%d",
       
   144         iapId);
       
   145     
       
   146     QSharedPointer<WlanQtUtilsIap> wlanIap;
       
   147     try {
       
   148         QScopedPointer<CmConnectionMethodShim> iap(
       
   149             mCmManager->connectionMethod(iapId));
       
   150         if (iap && iap->getIntAttribute(CMManagerShim::CmBearerType) ==
       
   151             KUidWlanBearerType) {
       
   152 
       
   153             // Get WLAN IAP parameters
       
   154             int netId = iap->getIntAttribute(CMManagerShim::CmNetworkId);
       
   155             QString name = iap->getStringAttribute(CMManagerShim::CmName);
       
   156             QString ssid = iap->getStringAttribute(CMManagerShim::WlanSSID);
       
   157             int secMode = iap->getIntAttribute(
       
   158                 CMManagerShim::WlanSecurityMode);
       
   159             bool wpaPskUse = iap->getBoolAttribute(
       
   160                 CMManagerShim::WlanEnableWpaPsk);
       
   161     
       
   162             // Create a WLAN Qt Utils IAP
       
   163             wlanIap = QSharedPointer<WlanQtUtilsIap>(new WlanQtUtilsIap());
       
   164             wlanIap->setValue(WlanQtUtilsIap::ConfIdIapId, iapId);
       
   165             wlanIap->setValue(WlanQtUtilsIap::ConfIdNetworkId, netId);
       
   166             wlanIap->setValue(WlanQtUtilsIap::ConfIdName, name);
       
   167             wlanIap->setValue(WlanQtUtilsAp::ConfIdSsid, ssid);
       
   168             wlanIap->setValue(WlanQtUtilsAp::ConfIdSecurityMode, secMode);
       
   169             wlanIap->setValue(WlanQtUtilsAp::ConfIdWpaPskUse, wpaPskUse);
       
   170         }
       
   171     } catch (const std::exception &ex) {
       
   172         int error = qt_symbian_exception2Error(ex);
       
   173         OstTrace1(
       
   174             TRACE_NORMAL,
       
   175             WLANQTUTILSIAPSETTINGS_FETCHIAP_EXCEPTION,
       
   176             "WlanQtUtilsIapSettings::fetchIap exception;error=%d",
       
   177             error);
       
   178     }
       
   179     
       
   180     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_FETCHIAP_EXIT);
       
   181     return wlanIap;
       
   182 }
       
   183 
       
   184 /*!
       
   185     Create a new WLAN IAP as an uncategorized IAP.
       
   186     
       
   187     @param [in] wlanAp Information about the WLAN AP.
       
   188     
       
   189     @return ID of the created IAP, IapIdNone in error cases.
       
   190 */
       
   191 
       
   192 int WlanQtUtilsIapSettings::createIap(const WlanQtUtilsAp *wlanAp)
       
   193 {
       
   194     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_CREATEIAP_ENTRY);
       
   195     
       
   196     int iapId = WlanQtUtils::IapIdNone;
       
   197     
       
   198     try {
       
   199         // Create the new IAP
       
   200         QScopedPointer<CmConnectionMethodShim> iap(
       
   201             mCmManager->createConnectionMethod(KUidWlanBearerType));
       
   202         storeSettings(wlanAp, iap.data());
       
   203         iapId = iap->getIntAttribute(CMManagerShim::CmIapId);
       
   204     } catch (const std::exception &ex) {
       
   205         // Trace error cause and return failure (default value) to caller.
       
   206         int error = qt_symbian_exception2Error(ex);
       
   207         OstTrace1(
       
   208             TRACE_NORMAL,
       
   209             WLANQTUTILSIAPSETTINGS_CREATEIAP_EXCEPTION,
       
   210             "WlanQtUtilsIapSettings::createIap exception;error=%d",
       
   211             error);
       
   212     }
       
   213     
       
   214     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_CREATEIAP_EXIT);
       
   215     return iapId;
       
   216 }
       
   217 
       
   218 /*!
       
   219     Update the WLAN IAP given as parameter. All settings are stored to
       
   220     database (again) without checking whether they have actually changed
       
   221     or not.
       
   222     
       
   223     @param [in] iapId ID of IAP to update.
       
   224     @param [in] wlanAp Information about the WLAN AP.
       
   225     
       
   226     @return Was update succesful or not?
       
   227 */
       
   228 
       
   229 bool WlanQtUtilsIapSettings::updateIap(
       
   230     int iapId,
       
   231     const WlanQtUtilsAp *wlanAp)
       
   232 {
       
   233     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_UPDATEIAP_ENTRY);
       
   234     
       
   235     bool success = false;
       
   236     
       
   237     try {
       
   238         QScopedPointer<CmConnectionMethodShim> iap(
       
   239             mCmManager->connectionMethod(iapId));
       
   240         storeSettings(wlanAp, iap.data());
       
   241         success = true;
       
   242     } catch (const std::exception &ex) {
       
   243         // Trace error cause and return failure (default value) to caller.
       
   244         int error = qt_symbian_exception2Error(ex);
       
   245         OstTrace1(
       
   246             TRACE_NORMAL,
       
   247             WLANQTUTILSIAPSETTINGS_UPDATEIAP_EXCEPTION,
       
   248             "WlanQtUtilsIapSettings::updateIap exception;error=%d",
       
   249             error);
       
   250     }
       
   251     
       
   252     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_UPDATEIAP_EXIT);
       
   253     return success;
       
   254 }
       
   255 
       
   256 /*!
       
   257     Delete the WLAN IAP given as parameter.
       
   258     
       
   259     @param [in] iapId ID of IAP to delete.
       
   260 */
       
   261 
       
   262 void WlanQtUtilsIapSettings::deleteIap(int iapId)
       
   263 {
       
   264     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_DELETEIAP_ENTRY);
       
   265     
       
   266     try {
       
   267         QScopedPointer<CmConnectionMethodShim> iap(
       
   268             mCmManager->connectionMethod(iapId));
       
   269         iap->deleteConnectionMethod();
       
   270     } catch (const std::exception &ex) {
       
   271         // Just trace error cause. It is not fatal, if we are not able to
       
   272         // delete the IAP. No need to retry, since errors should be very
       
   273         // rare and user can delete the IAP later from Control Panel, if
       
   274         // needed.
       
   275         int error = qt_symbian_exception2Error(ex);
       
   276         OstTrace1(
       
   277             TRACE_NORMAL,
       
   278             WLANQTUTILSIAPSETTINGS_DELETEIAP_EXCEPTION,
       
   279             "WlanQtUtilsIapSettings::deleteIap exception;error=%d",
       
   280             error);
       
   281     }
       
   282 
       
   283     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_DELETEIAP_EXIT);
       
   284 }
       
   285 
       
   286 /*!
       
   287     Move IAP to the Internet SNAP.
       
   288     
       
   289     @param [in] iapId ID of the IAP to move.
       
   290 */
       
   291 
       
   292 void WlanQtUtilsIapSettings::moveIapToInternetSnap(int iapId)
       
   293 {
       
   294     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_MOVEIAPTOINTERNETSNAP_ENTRY);
       
   295     
       
   296     // Read all destination (SNAP) IDs
       
   297     QList<uint> destinations;
       
   298     try {
       
   299         mCmManager->allDestinations(destinations);
       
   300         foreach (int destId, destinations) {
       
   301             QScopedPointer<CmDestinationShim> destination(
       
   302                 mCmManager->destination(destId));
       
   303         
       
   304             // Internet destination will always exist in the system. It has 
       
   305             // SnapPurposeInternet set in its metadata.
       
   306             if (destination->metadata(CMManagerShim::SnapMetadataPurpose)
       
   307                 == CMManagerShim::SnapPurposeInternet) {
       
   308                 QScopedPointer<CmConnectionMethodShim> iap(
       
   309                     mCmManager->connectionMethod(iapId));
       
   310                 destination->addConnectionMethod(iap.data());
       
   311                 destination->update();
       
   312                 break;
       
   313             }
       
   314         }
       
   315     } catch (const std::exception &ex) {
       
   316         int error = qt_symbian_exception2Error(ex);
       
   317         OstTrace1(
       
   318             TRACE_NORMAL,
       
   319             WLANQTUTILSIAPSETTINGS_MOVEIAPTOINTERNETSNAP_EXCEPTION,
       
   320             "WlanQtUtilsIapSettings::moveIapToInternetSnap exception;error=%d",
       
   321             error);
       
   322     }
       
   323 
       
   324     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_MOVEIAPTOINTERNETSNAP_EXIT);
       
   325 }
       
   326 
       
   327 /*!
       
   328     Stores the given Wlan AP settings to database using CM Manager Shim.
       
   329    
       
   330     @param [in] wlanAp WLAN AP settings to store.
       
   331     @param [in] iap WLAN IAP to store to.
       
   332 */
       
   333 
       
   334 void WlanQtUtilsIapSettings::storeSettings(
       
   335     const WlanQtUtilsAp *wlanAp,
       
   336     CmConnectionMethodShim *iap)
       
   337 {
       
   338     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_STORESETTINGS_ENTRY);
       
   339     
       
   340     int secMode = wlanAp->value(WlanQtUtilsAp::ConfIdSecurityMode).toInt();
       
   341     QString ssid = wlanAp->value(WlanQtUtilsAp::ConfIdSsid).toString();
       
   342     
       
   343     // Store general values
       
   344     iap->setStringAttribute(CMManagerShim::CmName, ssid);
       
   345     iap->setStringAttribute(CMManagerShim::WlanSSID, ssid);
       
   346     iap->setIntAttribute(CMManagerShim::WlanSecurityMode, secMode);
       
   347     iap->setIntAttribute(
       
   348         CMManagerShim::WlanConnectionMode, 
       
   349         wlanAp->value(WlanQtUtilsAp::ConfIdConnectionMode).toInt());
       
   350     
       
   351     switch (secMode) {
       
   352     case CMManagerShim::WlanSecModeWep:
       
   353         // Store the 4 WEP keys (function does nothing, if a key is not set)
       
   354         storeWepKey(
       
   355             wlanAp->value(WlanQtUtilsAp::ConfIdWepKey1).toString(),
       
   356             1,
       
   357             iap);
       
   358         storeWepKey(
       
   359             wlanAp->value(WlanQtUtilsAp::ConfIdWepKey2).toString(),
       
   360             2,
       
   361             iap);
       
   362         storeWepKey(
       
   363             wlanAp->value(WlanQtUtilsAp::ConfIdWepKey3).toString(),
       
   364             3,
       
   365             iap);
       
   366         storeWepKey(
       
   367             wlanAp->value(WlanQtUtilsAp::ConfIdWepKey4).toString(),
       
   368             4,
       
   369             iap);
       
   370         
       
   371         iap->setIntAttribute(
       
   372             CMManagerShim::WlanWepKeyIndex,
       
   373             wlanAp->value(WlanQtUtilsAp::ConfIdWepDefaultIndex).toInt());
       
   374         break;
       
   375 
       
   376     case CMManagerShim::WlanSecModeWpa:
       
   377     case CMManagerShim::WlanSecModeWpa2:
       
   378         // Store WPA PSK values
       
   379         bool usePsk = wlanAp->value(WlanQtUtilsAp::ConfIdWpaPskUse).toBool();
       
   380         iap->setBoolAttribute(CMManagerShim::WlanEnableWpaPsk, usePsk);
       
   381         if (usePsk) {
       
   382             QString wpaKey(wlanAp->value(WlanQtUtilsAp::ConfIdWpaPsk ).toString());
       
   383             iap->setString8Attribute(CMManagerShim::WlanWpaPreSharedKey, wpaKey);
       
   384         }
       
   385         break;
       
   386     }
       
   387     
       
   388     iap->update();
       
   389 
       
   390     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_STORESETTINGS_EXIT);
       
   391 }
       
   392 
       
   393 /*!
       
   394     Stores the given valid WEP key to database using CM Manager Shim. Ignores
       
   395     keys with zero length.
       
   396   
       
   397     @note This method MUST not be called for invalid WEP Keys. Wlanwizard
       
   398           validates keys, before accepting user input.
       
   399           
       
   400     @param [in] key Key to write.
       
   401     @param [in] index Key index. Valid range is [0,4].
       
   402     @param [in] iap WLAN IAP to store the key into.
       
   403 */
       
   404 
       
   405 void WlanQtUtilsIapSettings::storeWepKey(
       
   406     const QString &key,
       
   407     int index,
       
   408     CmConnectionMethodShim *iap)
       
   409 {
       
   410     OstTraceFunctionEntry0(WLANQTUTILSIAPSETTINGS_STOREWEPKEY_ENTRY);
       
   411     
       
   412     // Default value is set just to silence compiler, Q_ASSERTs take care
       
   413     // that valid attribute is set below
       
   414     CMManagerShim::ConnectionMethodAttribute attribute = 
       
   415         CMManagerShim::WlanWepKey1InHex;
       
   416     
       
   417     int length = key.length();
       
   418     if (length == WepHex64BitMaxLength || length == WepHex128BitMaxLength) {
       
   419         // HEX
       
   420         switch (index) {
       
   421         case 1:
       
   422             attribute = CMManagerShim::WlanWepKey1InHex;
       
   423             break;
       
   424             
       
   425         case 2:
       
   426             attribute = CMManagerShim::WlanWepKey2InHex;
       
   427             break;
       
   428             
       
   429         case 3:
       
   430             attribute = CMManagerShim::WlanWepKey3InHex;
       
   431             break;
       
   432             
       
   433         case 4:
       
   434             attribute = CMManagerShim::WlanWepKey4InHex;
       
   435             break;
       
   436             
       
   437 #ifndef QT_NO_DEBUG
       
   438         default:
       
   439             // Invalid key index detected
       
   440             Q_ASSERT(0);
       
   441             break;
       
   442 #endif            
       
   443         }
       
   444     } else if (length == WepAscii64BitMaxLength || length == WepAscii128BitMaxLength) {
       
   445         // ASCII
       
   446         switch (index) {
       
   447         case 1:
       
   448             attribute = CMManagerShim::WlanWepKey1InAscii;
       
   449             break;
       
   450             
       
   451         case 2:
       
   452             attribute = CMManagerShim::WlanWepKey2InAscii;
       
   453             break;
       
   454             
       
   455         case 3:
       
   456             attribute = CMManagerShim::WlanWepKey3InAscii;
       
   457             break;
       
   458 
       
   459         case 4:
       
   460             attribute = CMManagerShim::WlanWepKey4InAscii;
       
   461             break;
       
   462             
       
   463 #ifndef QT_NO_DEBUG
       
   464         default:
       
   465             // Invalid key index detected
       
   466             Q_ASSERT(0);
       
   467             break;
       
   468 #endif            
       
   469         }
       
   470     }
       
   471     
       
   472     if (length > 0) {
       
   473         iap->setString8Attribute(attribute, key);
       
   474     } // else: key is not set, ignore
       
   475 
       
   476     OstTraceFunctionExit0(WLANQTUTILSIAPSETTINGS_STOREWEPKEY_EXIT);
       
   477 }