omadrm/drmengine/roap/src/RoapConnection.cpp
changeset 0 95b198f216e5
child 18 8a03a285ab14
equal deleted inserted replaced
-1:000000000000 0:95b198f216e5
       
     1 /*
       
     2 * Copyright (c) 2002-2009 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:  Connection manager for ROAP
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <cmconnectionmethod.h>
       
    21 #include <cmdestination.h>
       
    22 #include <cmconnectionmethoddef.h>
       
    23 #include <cmmanager.h>
       
    24 #include <centralrepository.h>
       
    25 #include <CommDbConnPref.h>
       
    26 #include <cdblen.h>
       
    27 #include <es_enum.h>
       
    28 #ifdef __SERIES60_NATIVE_BROWSER
       
    29 #include <BrowserUiSDKCRKeys.h>
       
    30 #endif
       
    31 #include "RoapConnection.h"
       
    32 #include "RoapDef.h"
       
    33 #include "RoapLog.h"
       
    34 
       
    35 
       
    36 
       
    37 #ifndef __SERIES60_NATIVE_BROWSER
       
    38     const TUid KCRUidBrowser   = {0x10008D39};
       
    39     const TUint32 KBrowserDefaultAccessPoint =  0x0000000E;
       
    40     const TUint32 KBrowserAccessPointSelectionMode = 0x0000001E;
       
    41     const TUint32 KBrowserNGDefaultSnapId = 0x00000053;
       
    42 #endif
       
    43 
       
    44 
       
    45 // ================= LOCAL FUNCTIONS =========================================
       
    46 // ---------------------------------------------------------------------------
       
    47 // IapIdOfDefaultSnapL
       
    48 // for trapping purposes only
       
    49 // ---------------------------------------------------------------------------
       
    50 //
       
    51 LOCAL_C TUint32 IapIdOfDefaultSnapL(
       
    52     RCmManager& aCmManager,
       
    53     const TUint32 aDefaultSnap )
       
    54     {
       
    55     RCmDestination dest( aCmManager.DestinationL( aDefaultSnap ) );
       
    56     CleanupClosePushL( dest );
       
    57     TUint32 iapIdOfDest( 0 );
       
    58 
       
    59     if ( dest.ConnectionMethodCount() <= 0 )
       
    60         {
       
    61         User::Leave( KErrNotFound );
       
    62         }
       
    63 
       
    64     RCmConnectionMethod cMeth( dest.ConnectionMethodL( 0 ) );
       
    65     CleanupClosePushL( cMeth );
       
    66 
       
    67     iapIdOfDest = cMeth.GetIntAttributeL( CMManager::ECmIapId );
       
    68     CleanupStack::PopAndDestroy( &cMeth );
       
    69     CleanupStack::PopAndDestroy( &dest );
       
    70     return iapIdOfDest;
       
    71     }
       
    72 
       
    73 // ================= MEMBER FUNCTIONS =======================
       
    74 
       
    75 // ---------------------------------------------------------------------------
       
    76 // Roap::CRoapConnection::NewL
       
    77 // ---------------------------------------------------------------------------
       
    78 //
       
    79 Roap::CRoapConnection* Roap::CRoapConnection::NewL()
       
    80     {
       
    81     CRoapConnection* conn = new (ELeave) CRoapConnection();
       
    82     CleanupStack::PushL( conn );
       
    83     conn->ConstructL();
       
    84     CleanupStack::Pop( conn );
       
    85     return conn;
       
    86     }
       
    87 
       
    88 // ---------------------------------------------------------------------------
       
    89 // Roap::CRoapConnection::~CRoapConnection
       
    90 // ---------------------------------------------------------------------------
       
    91 //
       
    92 Roap::CRoapConnection::~CRoapConnection()
       
    93     {
       
    94     Cancel();
       
    95     iConnection.Close();
       
    96     iSocketServ.Close();
       
    97     }
       
    98 
       
    99 // ---------------------------------------------------------------------------
       
   100 // Roap::CRoapConnection::ConnectL
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 void Roap::CRoapConnection::ConnectL
       
   104 ( TUint32 aIap, TRequestStatus* aStatus )
       
   105     {
       
   106     LOGLIT( "CRoapConnection::ConnectL" )
       
   107     const TInt KAlwaysAskSelectionMode( 1 );
       
   108     const TInt KDestinationSelectionMode( 2 );
       
   109 
       
   110     if ( iState == EInit )
       
   111         {
       
   112         // Not connected. Attach to existing connection, or create new one if
       
   113         // allowed.
       
   114         iStatus = KErrRoapGeneral;
       
   115 
       
   116         // Make this part atomic by pushing closes on the stack.
       
   117         User::LeaveIfError( iSocketServ.Connect() );
       
   118         CleanupClosePushL<RSocketServ>( iSocketServ );
       
   119         User::LeaveIfError( iConnection.Open( iSocketServ ) );
       
   120         CleanupClosePushL<RConnection>( iConnection );
       
   121 
       
   122         TConnectionInfoBuf connInfo;
       
   123         TInt ap = 0;
       
   124         TInt alwaysAsk = 0;
       
   125         TUint count;
       
   126         User::LeaveIfError( iConnection.EnumerateConnections( count ) );
       
   127         TUint i;
       
   128         if ( count )
       
   129             {
       
   130             // Select from existing connections. Try to make AP match.
       
   131             for ( i = count; i; i-- )
       
   132                 {
       
   133                 // Note: GetConnectionInfo expects 1-based index.
       
   134                 User::LeaveIfError( iConnection.GetConnectionInfo( i, connInfo ) );
       
   135                 if ( aIap == 0 || connInfo().iIapId == aIap )
       
   136                     {
       
   137                     // "Accept any" or AP match. Attach to this one.
       
   138                     break;
       
   139                     }
       
   140                 }
       
   141             if ( !i )
       
   142                 {
       
   143                 // No AP match, select AP with largest index.
       
   144                 User::LeaveIfError
       
   145                     ( iConnection.GetConnectionInfo( count, connInfo ) );
       
   146                 }
       
   147             User::LeaveIfError
       
   148                 ( iConnection.Attach( connInfo, RConnection::EAttachTypeNormal ) );
       
   149             iState = EConnected;
       
   150             iStatus = KErrNone;
       
   151             }
       
   152         else
       
   153             {
       
   154             // No existing connections, create new one.
       
   155 #ifdef __WINS__
       
   156             // WINS connection creation does not work if preferences are given.
       
   157             // Defaults are to be used always.
       
   158             iConnection.Start( iStatus );
       
   159 #else
       
   160             // Note: the TCommDbConnPref must NOT be stack variable.
       
   161             // It must persist until completion of RConnection::Start().
       
   162             iConnPref.SetDirection( ECommDbConnectionDirectionOutgoing );
       
   163             //iConnPref.SetDialogPreference( ECommDbDialogPrefWarn )
       
   164             iConnPref.SetBearerSet( ECommDbBearerCSD | ECommDbBearerWcdma );
       
   165             // New connection is always created with user-selected AP
       
   166             // so 0 is used instead of aIap.
       
   167 
       
   168             TInt defaultSnap( 0 );
       
   169             CRepository* repository( NULL );
       
   170             repository = CRepository::NewL( KCRUidBrowser );
       
   171             CleanupStack::PushL( repository );
       
   172 
       
   173             repository->Get( KBrowserDefaultAccessPoint, ap);
       
   174             repository->Get( KBrowserAccessPointSelectionMode, alwaysAsk );
       
   175             repository->Get( KBrowserNGDefaultSnapId, defaultSnap );
       
   176             CleanupStack::PopAndDestroy( repository );
       
   177             repository = NULL;
       
   178 
       
   179             TUint32 iapd32 = 0;
       
   180             TInt err = KErrNone;
       
   181 
       
   182             if ( ap <= KErrNotFound && defaultSnap <= KErrNotFound )
       
   183                 {
       
   184                 alwaysAsk = KAlwaysAskSelectionMode;
       
   185                 }
       
   186             else
       
   187                 {
       
   188                 RCmManager cmManager;
       
   189                 cmManager.OpenLC();
       
   190                 if ( !alwaysAsk )
       
   191                     {
       
   192                     TRAP( err, iapd32 = cmManager.GetConnectionMethodInfoIntL(
       
   193                             ap, CMManager::ECmIapId ) );
       
   194                     }
       
   195                 else if ( alwaysAsk == KDestinationSelectionMode )
       
   196                     {
       
   197                     TRAP( err, iapd32 = IapIdOfDefaultSnapL(
       
   198                             cmManager, defaultSnap ) );
       
   199                     }
       
   200                 CleanupStack::PopAndDestroy( &cmManager );
       
   201                 }
       
   202 
       
   203             if ( err || alwaysAsk == KAlwaysAskSelectionMode )
       
   204                 {
       
   205                 // Always ask
       
   206                 LOGLIT( "SetDialogPreference( ECommDbDialogPrefPrompt )" )
       
   207                 iConnPref.SetDialogPreference( ECommDbDialogPrefPrompt );
       
   208                 iapd32 = 0;
       
   209                 }
       
   210             else
       
   211                 {
       
   212                 // User defined
       
   213                 LOGLIT( "SetDialogPreference( ECommDbDialogPrefDoNotPrompt )" )
       
   214                 iConnPref.SetDialogPreference( ECommDbDialogPrefDoNotPrompt );
       
   215                 }
       
   216 
       
   217             iConnPref.SetIapId( iapd32 );
       
   218             iConnection.Start( iConnPref, iStatus );
       
   219 #endif
       
   220             iState = EConnecting;
       
   221             SetActive();    // The only path with a real async request.
       
   222             }
       
   223         CleanupStack::Pop( 2, &iSocketServ ); // closing iConn and iSockServ
       
   224         // End of atomic part.
       
   225         }
       
   226     else
       
   227         {
       
   228         // Not expecting this to be called in other states.
       
   229         }
       
   230 
       
   231     iParentStatus = aStatus;
       
   232     *iParentStatus = KRequestPending;
       
   233 
       
   234     if ( !IsActive() )
       
   235         {
       
   236         // Unless we have an outstanding connect request (iConn.Start),
       
   237         // we are done.
       
   238         User::RequestComplete( iParentStatus, iStatus.Int() );
       
   239         iParentStatus = NULL;
       
   240         }
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // Roap::CRoapConnection::Close
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 void Roap::CRoapConnection::Close()
       
   248     {
       
   249     LOGLIT( "CRoapConnection::Close" )
       
   250 
       
   251     Cancel();
       
   252     iConnection.Close();
       
   253     iSocketServ.Close();
       
   254     iState = EInit;
       
   255     }
       
   256 
       
   257 // ---------------------------------------------------------------------------
       
   258 // Roap::CRoapConnection::IsConnected
       
   259 // ---------------------------------------------------------------------------
       
   260 //
       
   261 TBool Roap::CRoapConnection::IsConnected( TUint32& aIap )
       
   262     {
       
   263     LOGLIT( "CRoapConnection::IsConnected" )
       
   264 
       
   265     TBool connected( EFalse );
       
   266     if( iState == EConnected )
       
   267         {
       
   268         TBuf<KCommsDbSvrMaxColumnNameLength * 2 + 1> iapId;
       
   269         _LIT( KFormatIapId, "%S\\%S" );
       
   270         TPtrC iap( IAP );
       
   271         TPtrC id( COMMDB_ID );
       
   272         iapId.Format( KFormatIapId, &iap, &id );
       
   273         TInt err = iConnection.GetIntSetting( iapId, aIap );
       
   274         connected = err ? EFalse : ETrue;
       
   275         }
       
   276     return connected;
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // Roap::CRoapConnection::CRoapConnection
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 Roap::CRoapConnection::CRoapConnection()
       
   284 : CActive( CActive::EPriorityStandard ),
       
   285   iState( EInit )
       
   286     {
       
   287     CActiveScheduler::Add( this );
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // Roap::CRoapConnection::ConstructL
       
   292 // ---------------------------------------------------------------------------
       
   293 //
       
   294 void Roap::CRoapConnection::ConstructL()
       
   295     {
       
   296     }
       
   297 
       
   298 // ---------------------------------------------------------------------------
       
   299 // Roap::CRoapConnection::DoCancel
       
   300 // ---------------------------------------------------------------------------
       
   301 //
       
   302 void Roap::CRoapConnection::DoCancel()
       
   303     {
       
   304     LOGLIT( "CRoapConnection::DoCancel" )
       
   305 
       
   306     iConnection.Close();
       
   307     iSocketServ.Close();
       
   308     User::RequestComplete( iParentStatus, KErrCancel );
       
   309     }
       
   310 
       
   311 // ---------------------------------------------------------------------------
       
   312 // Roap::CRoapConnection::RunL
       
   313 // ---------------------------------------------------------------------------
       
   314 //
       
   315 void Roap::CRoapConnection::RunL()
       
   316     {
       
   317     LOGLIT( "CCRoapConnection::RunL" )
       
   318 
       
   319     User::LeaveIfError( iStatus.Int() );    // Handle errors in RunError().
       
   320 
       
   321     iState = EConnected;
       
   322     User::RequestComplete( iParentStatus, iStatus.Int() );
       
   323     iParentStatus = NULL;
       
   324     }
       
   325 
       
   326 // ---------------------------------------------------------------------------
       
   327 // Roap::CRoapConnection::RunError
       
   328 // ---------------------------------------------------------------------------
       
   329 //
       
   330 TInt Roap::CRoapConnection::RunError( TInt  /* aError */ )
       
   331     {
       
   332     LOGLIT( "CRoapConnection::RunError" )
       
   333 
       
   334     iConnection.Close();
       
   335     iSocketServ.Close();
       
   336     iState = EInit;
       
   337     User::RequestComplete( iParentStatus, iStatus.Int() );
       
   338     iParentStatus = NULL;
       
   339     return KErrNone;
       
   340     }
       
   341 
       
   342 // ---------------------------------------------------------------------------
       
   343 // Roap::CRoapConnection::SocketServ
       
   344 // ---------------------------------------------------------------------------
       
   345 //
       
   346 RSocketServ& Roap::CRoapConnection::SocketServ()
       
   347     {
       
   348     return iSocketServ;
       
   349     }
       
   350 
       
   351 // ---------------------------------------------------------------------------
       
   352 // Roap::CRoapConnection::Conn
       
   353 // ---------------------------------------------------------------------------
       
   354 //
       
   355 RConnection& Roap::CRoapConnection::Conn()
       
   356     {
       
   357     return iConnection;
       
   358     }
       
   359