bearermanagement/S60MCPR/src/s60mcprsimpleselectactivity.cpp
changeset 0 5a93021fdf25
child 8 2e6c4614c58e
child 18 fcbbe021d614
equal deleted inserted replaced
-1:000000000000 0:5a93021fdf25
       
     1 /*
       
     2 * Copyright (c) 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: S60 MCPR simple select activity implementation
       
    15 *
       
    16 */
       
    17 
       
    18 /**
       
    19 @file s60mcprsimpleselectactivity.cpp
       
    20 S60 MCPR simple select activity implementation
       
    21 */
       
    22 
       
    23 #include <comms-infras/ss_api_ext.h>
       
    24 #include <comms-infras/esock_params_internal.h>
       
    25 #include <comms-infras/esock_params.h>
       
    26 #include <commdbconnpref.h>
       
    27 
       
    28 #include <extendedconnpref.h>
       
    29 
       
    30 #include "s60mcpractivities.h"
       
    31 #include "s60mcprstates.h"
       
    32 
       
    33 using namespace Messages;
       
    34 using namespace MeshMachine;
       
    35 using namespace ESock;
       
    36 using namespace MCprActivities;
       
    37 using namespace S60MCprActivities;
       
    38 
       
    39 // -----------------------------------------------------------------------------
       
    40 // S60MCprSelectActivity
       
    41 // -----------------------------------------------------------------------------
       
    42 //
       
    43 namespace S60MCprSelectActivity
       
    44     {
       
    45     // -----------------------------------------------------------------------------
       
    46     // TAwaitingSelectNextLayer
       
    47     // -----------------------------------------------------------------------------
       
    48     //
       
    49     DEFINE_SMELEMENT(TAwaitingSelectNextLayer, NetStateMachine::MState, TContext)
       
    50     TBool TAwaitingSelectNextLayer::Accept()
       
    51         {
       
    52 		if ( !iContext.iMessage.IsMessage<TCFSelector::TSelect>() )
       
    53             {
       
    54             return EFalse;
       
    55             }
       
    56 
       
    57         __ASSERT_DEBUG( iContext.iPeer || iContext.iSender == iContext.NodeId(),
       
    58                         User::Panic(KS60MCprPanic, KPanicPeerMessage) );
       
    59 
       
    60         // Get MCPR
       
    61         CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
    62 
       
    63         // Next find out if our selection needed update. 
       
    64         
       
    65         // Get's the active service provider.
       
    66         RMetaServiceProviderInterface* sp = (RMetaServiceProviderInterface*) node.ServiceProvider();
       
    67         // Initially NULL.
       
    68         if ( sp && sp->ProviderInfo().APId() != node.PolicyPrefs().IapId() )
       
    69             {
       
    70             // Active sp exists but it differs from the required.
       
    71             // Do not select complete yet.
       
    72             S60MCPRLOGSTRING1("S60MCPR<%x>::TAwaitingSelectNextLayer::Accept() Active ServiceProvider but not the correct one.",(TInt*)&iContext.Node())
       
    73             return ETrue;
       
    74             }
       
    75 
       
    76         // If this node has any service providers (has already received some SelectComplete messages)
       
    77         // we forward these to the requesting client. The service providers forwarded here may include
       
    78         // all (selection has completed) or just a subset (selection still in progress) of this node's
       
    79         // possible service providers.
       
    80         TClientIter<TDefaultClientMatchPolicy> iter = 
       
    81             iContext.Node().GetClientIter<TDefaultClientMatchPolicy>( TClientType(TCFClientType::EServProvider) );
       
    82         RMetaServiceProviderInterface* serviceProvider = static_cast<RMetaServiceProviderInterface*>( iter++ );
       
    83         TCFSelector::TSelectComplete msg( TNodeId::NullId(), TProviderInfo() );
       
    84         while ( serviceProvider )
       
    85             {
       
    86             msg.iNodeId = serviceProvider->RecipientId();
       
    87             msg.iProviderInfo = serviceProvider->ProviderInfo();
       
    88             iContext.PostToSender( msg ); //We do not expect any answer so do not remember the peers TNodeId (SetPostedTo()).
       
    89             serviceProvider = static_cast<RMetaServiceProviderInterface*>( iter++ );
       
    90             }
       
    91 
       
    92         //If this node has already completed selection, we send a final SelectComplete(NULL).
       
    93         //if ( iContext.Node().IsSelectionCompleted() ) // private
       
    94         if ( sp )  // If active sp exists, the selection is completed
       
    95             {
       
    96             iContext.PostToSender( TCFSelector::TSelectComplete( TNodeId::NullId(),
       
    97                                                                  TProviderInfo()).CRef() );
       
    98             //We do not expect any answer so do not remember the peers TNodeId (SetPostedTo()).
       
    99             iContext.iMessage.ClearMessageId();
       
   100             return EFalse;
       
   101             }
       
   102 
       
   103         //Otherwise we perform the selection (start the activity or add the originator to the list).
       
   104         S60MCPRLOGSTRING1("S60MCPR<%x>::TAwaitingSelectNextLayer::Accept()",(TInt*)&iContext.Node())
       
   105         return ETrue;
       
   106         }    
       
   107     
       
   108     // -----------------------------------------------------------------------------
       
   109     // Post ChooseBestIAP to MPM.
       
   110     // -----------------------------------------------------------------------------
       
   111     //
       
   112     DEFINE_SMELEMENT(TSimpleSelect, NetStateMachine::MStateTransition, TContext)
       
   113     void TSimpleSelect::DoL()
       
   114         {
       
   115         S60MCPRLOGSTRING1("S60MCPR<%x>::TSimpleSelect::DoL()",(TInt*)&iContext.Node())
       
   116         
       
   117         // Send simple select to self
       
   118         iContext.iNodeActivity->PostRequestTo( iContext.NodeId(),
       
   119                                                TCFSelector::TSimpleSelect().CRef() );
       
   120         }
       
   121 
       
   122     // -----------------------------------------------------------------------------
       
   123     // Accepts state completion.
       
   124     // -----------------------------------------------------------------------------
       
   125     //
       
   126     DEFINE_SMELEMENT(TAwaitingSimpleSelectComplete, NetStateMachine::MState, TContext)
       
   127     TBool TAwaitingSimpleSelectComplete::Accept()
       
   128         {
       
   129         return EFalse;
       
   130         }
       
   131 
       
   132     // -----------------------------------------------------------------------------
       
   133     // S60 Selection logic in MCPR instead of TM!
       
   134     // -----------------------------------------------------------------------------
       
   135     //
       
   136     DEFINE_SMELEMENT(TSelectNextLayer, NetStateMachine::MStateTransition, TContext)
       
   137     void TSelectNextLayer::DoL()
       
   138         {
       
   139         __ASSERT_DEBUG(iContext.iNodeActivity, User::Panic(KS60MCprPanic, KPanicNoActivity));
       
   140         CSelectNextLayerActivity& ac = 
       
   141             static_cast<CSelectNextLayerActivity&>( *iContext.iNodeActivity );
       
   142 
       
   143         // Get MCPR
       
   144         CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
   145 
       
   146         // SNAP Attach
       
   147 #ifdef _DEBUG    
       
   148         if ( node.SelectionPrefs().Prefs().ExtensionId() == TConnPref::EConnPrefProviderInfo &&
       
   149              node.SelectionPrefs().Scope()&TSelectionPrefs::ESelectFromExisting )
       
   150             {
       
   151             S60MCPRLOGSTRING1("S60MCPR<%x>::TSelectNextLayer::DoL() SNAP Attach problem!!!!",(TInt*)&iContext.Node())
       
   152             __ASSERT_DEBUG(EFalse, User::Panic(KS60MCprPanic, KPanicUnsupportedMCPRState));
       
   153             }
       
   154 #endif //_DEBUG
       
   155 
       
   156 		TSelectionPrefs prefs;
       
   157 
       
   158 		ESock::RConnPrefList::TIter<TConnCSRPref> iterCSR = node.ConnPrefList().getIter<TConnCSRPref>();
       
   159 		//There should be one and only one CSR pref
       
   160 		ASSERT(iterCSR[0] != NULL && iterCSR[1] == NULL);
       
   161 		TConnCSRPref* csrprefs = iterCSR[0];
       
   162 
       
   163 		prefs.SetSubSessionUniqueId(csrprefs->SubSessionUniqueId());
       
   164 		prefs.SetFlags(csrprefs->Flags());
       
   165 		prefs.SetScope(csrprefs->Scope());
       
   166 
       
   167 		/*** HACK ALERT!
       
   168 
       
   169 		 */
       
   170 		TConnAPPref* nextAP = TConnAPPref::NewL(node.PolicyPrefs().IapId());
       
   171 		CleanupStack::PushL(nextAP);
       
   172 		node.ConnPrefList().AppendL(nextAP);
       
   173 		CleanupStack::Pop();
       
   174 		/* END OF HACK */
       
   175 
       
   176 		
       
   177         // Attach. 399 attach shouldn't be visible here.
       
   178         if ( prefs.Scope()&TSelectionPrefs::ESelectFromExisting )
       
   179             {
       
   180             S60MCPRLOGSTRING1("S60MCPR<%x>::TSelectNextLayer::DoL() Attach ",(TInt*)&iContext.Node())
       
   181             TCFSelector::TSimpleSelect msg( prefs );
       
   182             iContext.iNodeActivity->PostRequestTo( ac.iTierManager, msg );
       
   183             }
       
   184         // Start
       
   185         else
       
   186             {
       
   187             S60MCPRLOGSTRING1("S60MCPR<%x>::TSelectNextLayer::DoL() Start",(TInt*)&iContext.Node())
       
   188             TCommDbConnPref ippprefs;
       
   189             ippprefs.SetIapId( node.PolicyPrefs().IapId() );
       
   190             ippprefs.SetNetId( node.PolicyPrefs().NetId() );
       
   191             //node.MapPolicyPrefsToConnPrefL( node.PolicyPrefs(), tmppref );
       
   192             // don't use reference we want to preserve the original TSelectionPrefs.
       
   193             TSelectionPrefs newPrefs;
       
   194             newPrefs = prefs;
       
   195             newPrefs.SetPrefs( ippprefs );
       
   196             TCFSelector::TSimpleSelect msg(newPrefs);
       
   197             iContext.iNodeActivity->PostRequestTo(ac.iTierManager, msg);
       
   198 
       
   199             //TODO Clarify, if the SelectionChoices is actually needed...
       
   200             //if (ac.SelectionChoices().Count() == 0)
       
   201             //ac.SelectionChoices().Append(node.iIAPList[0]);
       
   202             }
       
   203         }
       
   204     }
       
   205 
       
   206 // -----------------------------------------------------------------------------
       
   207 // S60MCprSimpleSelectActivity
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 namespace S60MCprSimpleSelectActivity
       
   211     {
       
   212     // -----------------------------------------------------------------------------
       
   213     // TAwaitingSelectNextLayer
       
   214     // -----------------------------------------------------------------------------
       
   215     //
       
   216     DEFINE_SMELEMENT(TAwaitingSelectNextLayer, NetStateMachine::MState, TContext)
       
   217     TBool TAwaitingSelectNextLayer::Accept()
       
   218         {
       
   219         if ( !iContext.iMessage.IsMessage<TCFSelector::TSimpleSelect>() )
       
   220             {
       
   221             return EFalse;
       
   222             }
       
   223 
       
   224         __ASSERT_DEBUG( iContext.iPeer || iContext.iSender == iContext.NodeId(),
       
   225                         User::Panic(KS60MCprPanic, KPanicPeerMessage) );
       
   226 
       
   227         // Get MCPR
       
   228         CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
   229 
       
   230         // Next find out if our selection needed update. 
       
   231         
       
   232         // Get's the active service provider.
       
   233         RMetaServiceProviderInterface* sp = (RMetaServiceProviderInterface*) node.ServiceProvider();
       
   234         // Initially NULL.
       
   235         if ( sp && sp->ProviderInfo().APId() != node.PolicyPrefs().IapId() )
       
   236             {
       
   237             // Active sp exists but it differs from the required.
       
   238             // Do not select complete yet.
       
   239             S60MCPRLOGSTRING1("S60MCPR<%x>::TAwaitingSelectNextLayer::Accept() Active ServiceProvider but not the correct one.",(TInt*)&iContext.Node())
       
   240             return ETrue;
       
   241             }
       
   242 
       
   243         // If this node has any service providers (has already received some SelectComplete messages)
       
   244         // we forward these to the requesting client. The service providers forwarded here may include
       
   245         // all (selection has completed) or just a subset (selection still in progress) of this node's
       
   246         // possible service providers.
       
   247         TClientIter<TDefaultClientMatchPolicy> iter = 
       
   248             iContext.Node().GetClientIter<TDefaultClientMatchPolicy>( TClientType(TCFClientType::EServProvider) );
       
   249         RMetaServiceProviderInterface* serviceProvider = static_cast<RMetaServiceProviderInterface*>( iter++ );
       
   250         TCFSelector::TSelectComplete msg( TNodeId::NullId(), TProviderInfo() );
       
   251         while ( serviceProvider )
       
   252             {
       
   253             msg.iNodeId = serviceProvider->RecipientId();
       
   254             msg.iProviderInfo = serviceProvider->ProviderInfo();
       
   255             iContext.PostToSender( msg ); //We do not expect any answer so do not remember the peers TNodeId (SetPostedTo()).
       
   256             serviceProvider = static_cast<RMetaServiceProviderInterface*>( iter++ );
       
   257             }
       
   258 
       
   259         //If this node has already completed selection, we send a final SelectComplete(NULL).
       
   260         //if ( iContext.Node().IsSelectionCompleted() ) // private
       
   261         if ( sp )  // If active sp exists, the selection is completed
       
   262             {
       
   263             iContext.PostToSender( TCFSelector::TSelectComplete( TNodeId::NullId(),
       
   264                                                                  TProviderInfo()).CRef() );
       
   265             //We do not expect any answer so do not remember the peers TNodeId (SetPostedTo()).
       
   266             iContext.iMessage.ClearMessageId();
       
   267             return EFalse;
       
   268             }
       
   269 
       
   270         //Otherwise we perform the selection (start the activity or add the originator to the list).
       
   271         S60MCPRLOGSTRING1("S60MCPR<%x>::TAwaitingSelectNextLayer::Accept()",(TInt*)&iContext.Node())
       
   272         return ETrue;
       
   273         }
       
   274 
       
   275     // -----------------------------------------------------------------------------
       
   276     // S60 Selection logic in MCPR instead of TM!
       
   277     // -----------------------------------------------------------------------------
       
   278     //
       
   279     DEFINE_SMELEMENT(TSelectNextLayer, NetStateMachine::MStateTransition, TContext)
       
   280     void TSelectNextLayer::DoL()
       
   281         {
       
   282         __ASSERT_DEBUG(iContext.iNodeActivity, User::Panic(KS60MCprPanic, KPanicNoActivity));
       
   283         CSelectNextLayerActivity& ac = 
       
   284             static_cast<CSelectNextLayerActivity&>( *iContext.iNodeActivity );
       
   285 
       
   286         // Get MCPR
       
   287         CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
   288 
       
   289         // SNAP Attach
       
   290 #ifdef _DEBUG    
       
   291         if ( node.SelectionPrefs().Prefs().ExtensionId() == TConnPref::EConnPrefProviderInfo &&
       
   292              node.SelectionPrefs().Scope()&TSelectionPrefs::ESelectFromExisting )
       
   293             {
       
   294             S60MCPRLOGSTRING1("S60MCPR<%x>::TSelectNextLayer::DoL() SNAP Attach problem!!!!",(TInt*)&iContext.Node())
       
   295             __ASSERT_DEBUG(EFalse, User::Panic(KS60MCprPanic, KPanicUnsupportedMCPRState));
       
   296             }
       
   297 #endif //_DEBUG
       
   298 
       
   299         // Attach. 399 attach shouldn't be visible here.
       
   300         if ( node.SelectionPrefs().Scope()&TSelectionPrefs::ESelectFromExisting )
       
   301             {
       
   302             S60MCPRLOGSTRING1("S60MCPR<%x>::TSelectNextLayer::DoL() Attach ",(TInt*)&iContext.Node())
       
   303             TCFSelector::TSimpleSelect msg( node.SelectionPrefs() );
       
   304             iContext.iNodeActivity->PostRequestTo( ac.iTierManager, msg );
       
   305             }
       
   306         // Start
       
   307         else
       
   308             {
       
   309             S60MCPRLOGSTRING1("S60MCPR<%x>::TSelectNextLayer::DoL() Start",(TInt*)&iContext.Node())
       
   310             TCommDbConnPref ippprefs;
       
   311             ippprefs.SetIapId( node.PolicyPrefs().IapId() );
       
   312             ippprefs.SetNetId( node.PolicyPrefs().NetId() );
       
   313             //node.MapPolicyPrefsToConnPrefL( node.PolicyPrefs(), tmppref );
       
   314             // don't use reference we want to preserve the original TSelectionPrefs.
       
   315             TSelectionPrefs newPrefs;
       
   316             newPrefs = node.SelectionPrefs();
       
   317             newPrefs.SetPrefs( ippprefs );
       
   318             TCFSelector::TSimpleSelect msg(newPrefs);
       
   319             iContext.iNodeActivity->PostRequestTo(ac.iTierManager, msg);
       
   320 
       
   321             //TODO Clarify, if the SelectionChoices is actually needed...
       
   322             //if (ac.SelectionChoices().Count() == 0)
       
   323             //ac.SelectionChoices().Append(node.iIAPList[0]);
       
   324             }
       
   325         }
       
   326 
       
   327     // -----------------------------------------------------------------------------
       
   328     // Post ChooseBestIAP to MPM.
       
   329     // -----------------------------------------------------------------------------
       
   330     //
       
   331     DEFINE_SMELEMENT(TChooseBestIAP, NetStateMachine::MStateTransition, TContext)
       
   332     void TChooseBestIAP::DoL()
       
   333         {
       
   334         S60MCPRLOGSTRING1("S60MCPR<%x>::TChooseBestIAP::DoL()",(TInt*)&iContext.Node())
       
   335         
       
   336         // Get MCPR
       
   337         CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
   338 
       
   339         // Build up the PolicyRequest.
       
   340         TConnPref* pref = new ( ELeave ) TMpmConnPref();
       
   341         CleanupStack::PushL( pref );
       
   342         TConnPref* polpref = (TConnPref*)new ( ELeave ) TPolicyConnPref();
       
   343         CleanupStack::PushL( polpref );
       
   344 
       
   345         // Is it Ok to store references of both node and activity?
       
   346         // It should be ok since this is the activity we want to response for.
       
   347         CChooseBestIAPCb* cb = new( ELeave ) CChooseBestIAPCb( node, iContext.iNodeActivity );
       
   348 
       
   349         CleanupStack::PushL( cb );
       
   350 
       
   351         // Resolve application uid.
       
   352         TSubSessionUniqueId subSessionUniqueId = node.SelectionPrefs().SubSessionUniqueId();
       
   353 
       
   354 		if ( !node.ConnPrefList().Count() )
       
   355 			{
       
   356             subSessionUniqueId = node.SelectionPrefs().SubSessionUniqueId();
       
   357             }
       
   358         else
       
   359             {
       
   360             subSessionUniqueId = node.SubSessionUniqueId();
       
   361             }
       
   362 
       
   363         TSecureId secureId( 0 );
       
   364         
       
   365         // Note that we use Symbian internal API to resolve the uid. 
       
   366         AIPCClientInfo clientinfo;
       
   367         TInt ret = clientinfo.ClientSid( subSessionUniqueId, secureId );
       
   368         if ( ret != KErrNone )
       
   369             {
       
   370             S60MCPRLOGSTRING2("S60MCPR<%x>::TChooseBestIAP::DoL() Failed to resolve Application UID %d",(TInt*)&iContext.Node(),ret)
       
   371             // Application UID may not exist. Use null id.
       
   372             secureId = KNullUid;
       
   373             }
       
   374         
       
   375         TUid id = TUid::Uid( secureId );
       
   376         
       
   377         S60MCPRLOGSTRING2("S60MCPR<%x>::TChooseBestIAP::DoL() Application UID=%x",(TInt*)&iContext.Node(),id);        
       
   378 
       
   379         // Copy original TConnPref.
       
   380         if (!node.ConnPrefList().Count())
       
   381             {
       
   382             *pref = node.SelectionPrefs().Prefs();
       
   383             }
       
   384         else
       
   385             {
       
   386             ESock::RConnPrefList::TIter<TExtendedConnPref> iterAP = node.ConnPrefList().getIter<TExtendedConnPref>();
       
   387             TExtendedConnPref *ptr = &(*iterAP);
       
   388             static_cast<TMpmConnPref*>(pref)->SetSnapPurpose ( ptr->SnapPurpose() );
       
   389             static_cast<TMpmConnPref*>(pref)->SetSnapId ( ptr->SnapId() );
       
   390             static_cast<TMpmConnPref*>(pref)->SetIapId ( ptr->IapId() );
       
   391             static_cast<TMpmConnPref*>(pref)->SetBearerSet ( ptr->BearerSet() );
       
   392             static_cast<TMpmConnPref*>(pref)->SetNoteBehaviour ( ptr->NoteBehaviour() );
       
   393             static_cast<TMpmConnPref*>(pref)->SetDisconnectDialog ( ptr->DisconnectDialog() );
       
   394             static_cast<TMpmConnPref*>(pref)->SetConnSelectionDialog ( ptr->ConnSelectionDialog() );
       
   395             static_cast<TMpmConnPref*>(pref)->SetForcedRoaming ( ptr->ForcedRoaming() );
       
   396             }
       
   397 
       
   398         // Send request
       
   399         node.Policy()->ChooseBestIAPL( pref, id, polpref, cb );
       
   400 
       
   401         // Pop three objects.
       
   402         CleanupStack::Pop( cb );
       
   403         CleanupStack::Pop( polpref );
       
   404         CleanupStack::Pop( pref );
       
   405         }
       
   406 
       
   407     // -----------------------------------------------------------------------------
       
   408     // Accepts state completion.
       
   409     // -----------------------------------------------------------------------------
       
   410     //
       
   411     DEFINE_SMELEMENT(TAwaitingChooseBestIAP, NetStateMachine::MState, TContext)
       
   412     TBool TAwaitingChooseBestIAP::Accept()
       
   413         {
       
   414         if ( iContext.iMessage.IsMessage<TCFS60MCPRMessage::TMPMChooseBestIAPCompletedMsg>() )
       
   415             {
       
   416             S60MCPRLOGSTRING1("S60MCPR<%x>::TAwaitingChooseBestIAP::Accept() TMPMChooseBestIAPCompletedMsg Accepted", (TInt*)&iContext.Node())
       
   417 
       
   418             // MPM ChooseBestIAP completed.
       
   419             return ETrue;
       
   420             }
       
   421         else if ( iContext.iMessage.IsMessage<TEBase::TError>() )
       
   422             {
       
   423             S60MCPRLOGSTRING1("S60MCPR<%x>::TAwaitingChooseBestIAP::Accept() NOT Accept TError", (TInt*)&iContext.Node())
       
   424             // Some error possibly sent by the MPM. We can let the core handle this.
       
   425             return EFalse;
       
   426             }
       
   427         else if ( iContext.iMessage.IsMessage<TEBase::TCancel>() )
       
   428             {
       
   429             // Cancellation from the RConnection::Start(). 
       
   430             // Cancel MPM asynch.
       
   431             S60MCPRLOGSTRING1("S60MCPR<%x>::TAwaitingChooseBestIAP::Accept() NOT Accept TCancel, Cancelling ChooseBestIAPL()", (TInt*)&iContext.Node())
       
   432 
       
   433             // Get the MCPR
       
   434             CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
   435 
       
   436             node.Policy()->CancelChooseBestIAPL();
       
   437 
       
   438             return EFalse;
       
   439             }
       
   440         else
       
   441             {
       
   442             // Else
       
   443             return EFalse;
       
   444             }
       
   445         }
       
   446 
       
   447     // -----------------------------------------------------------------------------
       
   448     // Decides if ChooseBestIAP is required.
       
   449     // -----------------------------------------------------------------------------
       
   450     //
       
   451     DEFINE_SMELEMENT(TChooseBestIAPRequiredOrNoTagOrSetChosenApActive, NetStateMachine::MStateFork, TContext)
       
   452     TInt TChooseBestIAPRequiredOrNoTagOrSetChosenApActive::TransitionTag()
       
   453         {
       
   454         __ASSERT_DEBUG(iContext.iNodeActivity, User::Panic(KS60MCprPanic, KPanicNoActivity));
       
   455 
       
   456         // Initialize
       
   457 		CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
   458         TInt iapid = node.PolicyPrefs().IapId();
       
   459         TInt result = MeshMachine::KNoTag;
       
   460         RMetaServiceProviderInterface* sp = NULL;
       
   461         TBool boundButNotActiveMCPRfound = EFalse;
       
   462 
       
   463         // Search desired&bound MCPRs and check that EActive flag is not set.
       
   464         TClientIter<TDefaultClientMatchPolicy> iter = 
       
   465             node.GetClientIter<TDefaultClientMatchPolicy>(TCFClientType::EServProvider);
       
   466         for ( TInt i = 0; iter[i]; i++ )
       
   467             {
       
   468             sp = static_cast<RMetaServiceProviderInterface*>( iter[i] );
       
   469             // Make sure sp is not active and it exists
       
   470             if ( !( sp->Flags() & TCFClientType::EActive ) && 
       
   471                  sp->ProviderInfo().APId() == iapid )
       
   472                 {
       
   473                 boundButNotActiveMCPRfound = ETrue;
       
   474                 break;
       
   475                 }
       
   476             }
       
   477         
       
   478         // Set service provider to point to active service provider.
       
   479         sp = (RMetaServiceProviderInterface*) node.ServiceProvider();
       
   480         
       
   481         // If we are attaching to existing, we can use the passthrough preferences
       
   482         //
       
   483         if ( node.SelectionPrefs().Scope()&TSelectionPrefs::ESelectFromExisting )
       
   484             {
       
   485             S60MCPRLOGSTRING2("S60MCPR<%x>::TChooseBestIAPRequiredOrNoTagOrSetChosenApActive<%x>::TransitionTag() Selecting from existing",
       
   486                       (TInt*)&iContext.Node(),(TInt*)this);
       
   487             result = MeshMachine::KNoTag;
       
   488             }
       
   489         // If our policy selection doesn't match with the existing selection,
       
   490         // but we have one ( i.e. we're roaming or recovering from error ) 
       
   491         // We just need to select our IP Proto MCPRs.
       
   492         // Preferences should be in place and choose best iap is not required.
       
   493         //
       
   494         else if ( sp && sp->ProviderInfo().APId() != iapid )
       
   495             {
       
   496             S60MCPRLOGSTRING2("S60MCPR<%x>::TChooseBestIAPRequiredOrNoTagOrSetChosenApActive<%x>::TransitionTag() Selecting with existing preferences",(TInt*)&iContext.Node(),(TInt*)this);
       
   497             result = MeshMachine::KNoTag;
       
   498             }
       
   499         // If MCPR can be found but it's not active we can
       
   500         // just jump into setting it active.
       
   501         //
       
   502         else if ( boundButNotActiveMCPRfound )
       
   503             {
       
   504             S60MCPRLOGSTRING2("S60MCPR<%x>::TChooseBestIAPRequiredOrNoTagOrSetChosenApActive::TransitionTag() IP Proto MCPR of AP %d found but not Active",(TInt*)&iContext.Node(),iapid)
       
   505             result = S60MCprStates::KSetChosenApActive;
       
   506             }
       
   507         // We should execute the ChooseBestIAP query.
       
   508         //
       
   509         else
       
   510             {
       
   511             S60MCPRLOGSTRING1("S60MCPR<%x>::TChooseBestIAPRequiredOrNoTagOrSetChosenApActive::TransitionTag() Selection from MPM required",(TInt*)&iContext.Node())
       
   512             result = S60MCprStates::KChooseBestIAPRequired;
       
   513             }
       
   514         
       
   515 //  In other words:
       
   516 //         return ( ( passThrough || justSelect ) ? 
       
   517 //                    MeshMachine::KNoTag 
       
   518 //                  : justSetActive ? 
       
   519 //                          S60MCprStates::KSetChosenApActive
       
   520 //                        : S60MCprStates::KChooseBestIAPRequired ) 
       
   521 //               | NetStateMachine::EForward;
       
   522 
       
   523         return result | NetStateMachine::EForward;
       
   524         }
       
   525 
       
   526     // -----------------------------------------------------------------------------
       
   527     // TSetChosenApActiveOrSelectedProviderBackward
       
   528     // -----------------------------------------------------------------------------
       
   529     //
       
   530     DEFINE_SMELEMENT(TSetChosenApActiveOrSelectedProviderBackward, NetStateMachine::MStateFork, TContext)
       
   531     TInt TSetChosenApActiveOrSelectedProviderBackward::TransitionTag()
       
   532         {
       
   533         __ASSERT_DEBUG(iContext.iNodeActivity, User::Panic(KS60MCprPanic, KPanicNoActivity));
       
   534         CSelectNextLayerActivity& ac = static_cast<CSelectNextLayerActivity&>( *iContext.iNodeActivity );
       
   535         if ( ac.SelectionChoices().Count() > ( ac.iCurrentIndex + 1 ) )
       
   536             {
       
   537             S60MCPRLOGSTRING1("S60MCPR<%x>::TSetChosenApActiveOrSelectedProviderBackward::TransitionTag() KSelectedProvider",(TInt*)&iContext.Node())
       
   538             return MCprStates::KSelectedProvider | NetStateMachine::EBackward;
       
   539             }
       
   540         S60MCPRLOGSTRING1("S60MCPR<%x>::TSetChosenApActiveOrSelectedProviderBackward::TransitionTag() KSetChosenApActive",(TInt*)&iContext.Node())
       
   541         return S60MCprStates::KSetChosenApActive | NetStateMachine::EForward;
       
   542         }
       
   543 
       
   544     // -----------------------------------------------------------------------------
       
   545     // Sets the chosen AP as active.
       
   546     // -----------------------------------------------------------------------------
       
   547     //
       
   548     DEFINE_SMELEMENT(TSetChosenApActive, NetStateMachine::MStateTransition, TContext)
       
   549     void TSetChosenApActive::DoL()
       
   550         {
       
   551         S60MCPRLOGSTRING1("S60MCPR<%x>::TSetChosenApActive::DoL()",(TInt*)&iContext.Node())
       
   552 
       
   553         // Get the MCPR
       
   554         CS60MetaConnectionProvider& node = (CS60MetaConnectionProvider&)iContext.Node();
       
   555 
       
   556         // Get the policy server's selection result 
       
   557         TInt iapid = node.PolicyPrefs().IapId();
       
   558         // Get iterator from the base class.
       
   559         TClientIter<TDefaultClientMatchPolicy> iter = node.GetClientIter<TDefaultClientMatchPolicy>(TCFClientType::EServProvider);
       
   560         // Introduce handle to IPProtoMCPR
       
   561         RMetaServiceProviderInterface* sp = NULL;
       
   562         // Iterate and set the EActive flag when needed
       
   563         for ( TInt i = 0; iter[i]; i++ )
       
   564             {
       
   565             sp = static_cast<RMetaServiceProviderInterface*>(iter[i]);
       
   566             if ( sp->ProviderInfo().APId() == iapid ) // In IPProto the runtime APId is the IapId.
       
   567                 {
       
   568                 S60MCPRLOGSTRING2("S60MCPR<%x>::TSetChosenApActive::DoL() IP Proto MCPR of AP %d set Active",(TInt*)&iContext.Node(),iapid)
       
   569                 sp->SetFlags( TCFClientType::EActive );
       
   570                 break; // Leave sp selected
       
   571                 }
       
   572             else if ( node.SelectionPrefs().Scope()&TSelectionPrefs::ESelectFromExisting &&
       
   573                       iapid == 0 ) // Not selected by MPM. Legacy Attach.
       
   574                 {
       
   575                 // Set IapId.
       
   576                 node.PolicyPrefs().SetIapId( sp->ProviderInfo().APId() );
       
   577                 // Inform MPM of used AP.
       
   578                 node.Policy()->ApplicationJoinsConnectionL( node.PolicyPrefs().IapId() );
       
   579                 break;
       
   580                 }
       
   581 #ifdef _DEBUG // allow urel use any in case of error. The missing TSetChosenApActive -log above indicates a problem.
       
   582             else
       
   583                 {
       
   584                 sp = NULL;
       
   585                 }
       
   586 #endif
       
   587             }
       
   588         // At this point we're not doing reconnect or anything like that. 
       
   589         // Therefore the Selected provider must be the same as the one picked by MPM and must be found.
       
   590         __ASSERT_DEBUG(sp, User::Panic(KS60MCprPanic, KPanicInConsistentMCPRState));
       
   591         }
       
   592 
       
   593     // -----------------------------------------------------------------------------
       
   594     // CChooseBestIAPCb::PolicyResponse
       
   595     // -----------------------------------------------------------------------------
       
   596     //
       
   597     void CChooseBestIAPCb::PolicyResponse( PolicyRequest& aCompletedRequest )
       
   598         {
       
   599         S60MCPRLOGSTRING1("S60MCPR<%x>::CChooseBestIAPCb::PolicyResponse()",(TInt*)&iNode)
       
   600 
       
   601         // Request to MPM failed for some reason
       
   602         if( aCompletedRequest.iStatus != KErrNone )
       
   603             {
       
   604             S60MCPRLOGSTRING2("S60MCPR<%x>::CChooseBestIAPCb::PolicyResponse() MPM Error %d",(TInt*)&iNode,aCompletedRequest.iStatus)
       
   605 
       
   606             TEBase::TError msg( TCFS60MCPRMessage::TMPMChooseBestIAPCompletedMsg::Id(), 
       
   607                                 aCompletedRequest.iStatus );
       
   608             if ( iLastRequestOriginator.IsOpen() )
       
   609                 {
       
   610                 iLastRequestOriginator.ReplyTo( iNode.Id(), msg );
       
   611                 iLastRequestOriginator.Close();
       
   612                 }
       
   613             return;
       
   614             }
       
   615 
       
   616         // We only accept TPolicyConnPref from MPM.
       
   617         ASSERT( aCompletedRequest.iPolicyPref->ExtensionId() == TPolicyConnPref::EConnS60PolicyPref );
       
   618 
       
   619         TPolicyConnPref* policypref = (TPolicyConnPref*)aCompletedRequest.iPolicyPref;
       
   620 
       
   621         iNode.SetPolicyPrefs( *policypref );
       
   622 
       
   623         S60MCPRLOGSTRING3("S60MCPR<%x>::CChooseBestIAPCb::PolicyResponse() IAP %d NET %d",
       
   624                   (TInt*)&iNode,policypref->IapId(),policypref->NetId())
       
   625 
       
   626         TCFS60MCPRMessage::TMPMChooseBestIAPCompletedMsg req;
       
   627         if ( iLastRequestOriginator.IsOpen() )
       
   628             {
       
   629             iLastRequestOriginator.ReplyTo( iNode.Id(), req );
       
   630             iLastRequestOriginator.Close();
       
   631             }
       
   632         }
       
   633     } // namespace S60MCprSimpleSelectActivity
       
   634 
       
   635 //  End of File