voipplugins/sipconnectionprovider/src/scpservice.cpp
branchRCL_3
changeset 22 d38647835c2e
parent 0 a4daefaec16c
equal deleted inserted replaced
21:f742655b05bf 22:d38647835c2e
       
     1 /*
       
     2 * Copyright (c) 2005-2007 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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <cchserviceobserver.h>
       
    20 
       
    21 #include "scpservice.h"
       
    22 #include "scpsubservice.h"
       
    23 #include "scplogger.h"
       
    24 #include "scputility.h"
       
    25 #include "scpprofilehandler.h"
       
    26 
       
    27 // -----------------------------------------------------------------------------
       
    28 // CScpService::CScpService
       
    29 // -----------------------------------------------------------------------------
       
    30 //
       
    31 CScpService::CScpService( TInt aId, 
       
    32                           TInt aServiceId,
       
    33                           CScpProfileHandler& aProfileHandler, 
       
    34                           CScpServiceStorage& aServiceStorage,
       
    35                           MCchServiceObserver& aServiceObserver ) :
       
    36     iId( aId ),
       
    37     iServiceId( aServiceId ),
       
    38     iProfileHandler( aProfileHandler ), 
       
    39     iServiceStorage( aServiceStorage ),
       
    40     iServiceObserver( aServiceObserver )
       
    41     {
       
    42     }
       
    43 
       
    44 // -----------------------------------------------------------------------------
       
    45 // CScpService::~CScpService
       
    46 // -----------------------------------------------------------------------------
       
    47 //
       
    48 CScpService::~CScpService()
       
    49     {
       
    50     SCPLOGSTRING2( "CScpService[0x%x]::~CScpService", this );
       
    51 
       
    52     iSubServices.ResetAndDestroy();
       
    53     iSubServices.Close();
       
    54     }
       
    55 
       
    56 // -----------------------------------------------------------------------------
       
    57 // CScpService::NewL
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CScpService* CScpService::NewL( TInt aId, 
       
    61                                 TInt aServiceId,
       
    62                                 CScpProfileHandler& aProfileHandler,
       
    63                                 CScpServiceStorage& aServiceStorage, 
       
    64                                 MCchServiceObserver& aServiceObserver )
       
    65     {
       
    66     SCPLOGSTRING3( "CScpService::NewL id: %d service id: %d", aId, aServiceId );
       
    67 
       
    68     CScpService* self = new (ELeave) CScpService( aId, 
       
    69                                                   aServiceId,
       
    70                                                   aProfileHandler,
       
    71                                                   aServiceStorage,
       
    72                                                   aServiceObserver );
       
    73     return self;
       
    74     }
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CScpService::Id
       
    78 // -----------------------------------------------------------------------------
       
    79 //
       
    80 TInt CScpService::Id() const
       
    81     {
       
    82     SCPLOGSTRING2( "CScpService[0x%x]::Id", this );
       
    83 
       
    84     return iId;
       
    85     }
       
    86 
       
    87 // -----------------------------------------------------------------------------
       
    88 // CScpService::ProfileHandler
       
    89 // -----------------------------------------------------------------------------
       
    90 //
       
    91 CScpProfileHandler& CScpService::ProfileHandler() const
       
    92     {
       
    93     return iProfileHandler;
       
    94     }
       
    95 
       
    96 // -----------------------------------------------------------------------------
       
    97 // CScpService::ServiceStorage
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CScpServiceStorage& CScpService::ServiceStorage() const
       
   101     {
       
   102     return iServiceStorage;
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CScpService::ServiceObserver
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 MCchServiceObserver& CScpService::ServiceObserver() const
       
   110     {
       
   111     return iServiceObserver;
       
   112     }
       
   113 
       
   114 // -----------------------------------------------------------------------------
       
   115 // CScpService::AddSubServiceL
       
   116 // -----------------------------------------------------------------------------
       
   117 //
       
   118 CScpSubService& 
       
   119     CScpService::AddSubServiceL( TCCHSubserviceType aSubServiceType )
       
   120     {
       
   121     SCPLOGSTRING2( "CScpService[0x%x]::AddSubServiceL", this );
       
   122 
       
   123     TInt newId = GenerateNewSubServiceId();    
       
   124     CScpSubService* service = CScpSubService::NewL( newId, 
       
   125                                                     ServiceId(),
       
   126                                                     aSubServiceType,
       
   127                                                     *this );
       
   128     CleanupStack::PushL( service );
       
   129     iSubServices.AppendL( service );
       
   130     CleanupStack::Pop( service );
       
   131 
       
   132     return *iSubServices[ iSubServices.Count() - 1 ];
       
   133     }
       
   134 
       
   135 // -----------------------------------------------------------------------------
       
   136 // CScpService::SubServiceCount
       
   137 // -----------------------------------------------------------------------------
       
   138 //
       
   139 TInt CScpService::SubServiceCount() const
       
   140     {
       
   141     SCPLOGSTRING2( "CScpService[0x%x]::SubServiceCount", this );
       
   142 
       
   143     return iSubServices.Count();
       
   144     }
       
   145 
       
   146 // -----------------------------------------------------------------------------
       
   147 // CScpService::GenerateNewSubServiceId
       
   148 // -----------------------------------------------------------------------------
       
   149 //
       
   150 TInt CScpService::GenerateNewSubServiceId()
       
   151     {
       
   152     SCPLOGSTRING2( "CScpService[0x%x]::GenerateNewSubServiceId", this );
       
   153 
       
   154     iSubServiceIdCounter++;
       
   155     
       
   156     return iId + iSubServiceIdCounter;
       
   157     }
       
   158 
       
   159 // -----------------------------------------------------------------------------
       
   160 // CScpService::GetSubServiceIds
       
   161 // -----------------------------------------------------------------------------
       
   162 //
       
   163 void CScpService::GetSubServiceIds( RArray<TInt>& aIds ) const
       
   164     {
       
   165     SCPLOGSTRING2( "CScpService[0x%x]::GetSubServiceIds", this );
       
   166     __ASSERT_DEBUG( aIds.Count() == 0, User::Panic( KNullDesC, KErrGeneral ) );
       
   167 
       
   168     for( TInt i=0; i<iSubServices.Count(); i++)
       
   169         {
       
   170         const CScpSubService* subService = iSubServices[ i ];
       
   171         
       
   172         aIds.Append( subService->Id() );
       
   173         }    
       
   174     }
       
   175 
       
   176 // -----------------------------------------------------------------------------
       
   177 // CScpService::GetSubService
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 CScpSubService* CScpService::GetSubService( TInt aId ) const
       
   181     {
       
   182     SCPLOGSTRING3( "CScpService[0x%x]::GetSubService id: %d", this, aId );
       
   183 
       
   184     for( TInt i=0; i<iSubServices.Count(); i++)
       
   185         {
       
   186         CScpService* self = const_cast<CScpService*>(this);
       
   187         CScpSubService* subService = self->iSubServices[ i ];
       
   188         if( aId == subService->Id() )
       
   189             {
       
   190             return subService;
       
   191             }
       
   192         }
       
   193     
       
   194     return NULL;
       
   195     }
       
   196 
       
   197 // -----------------------------------------------------------------------------
       
   198 // CScpService::RemoveSubService
       
   199 // -----------------------------------------------------------------------------
       
   200 //
       
   201 TInt CScpService::RemoveSubService( TInt aId )
       
   202     {
       
   203     SCPLOGSTRING3( "CScpService[0x%x]::RemoveSubService id: %d", this, aId );
       
   204 
       
   205     for( TInt i=0; i<iSubServices.Count(); i++)
       
   206         {
       
   207         CScpSubService* subService = iSubServices[ i ];
       
   208         if( aId == subService->Id() )
       
   209             {
       
   210             delete subService;
       
   211             iSubServices.Remove( i );
       
   212             return KErrNone;
       
   213             }
       
   214         }
       
   215 
       
   216     return KErrNotFound;        
       
   217     }
       
   218 
       
   219 // -----------------------------------------------------------------------------
       
   220 // CScpService::ServiceId
       
   221 // -----------------------------------------------------------------------------
       
   222 //
       
   223 TInt CScpService::ServiceId() const
       
   224     {
       
   225     SCPLOGSTRING2( "CScpService[0x%x]::ServiceId", this );
       
   226 
       
   227     return iServiceId;
       
   228     }
       
   229 
       
   230 // -----------------------------------------------------------------------------
       
   231 // CScpService::SetServiceId
       
   232 // -----------------------------------------------------------------------------
       
   233 //
       
   234 void CScpService::SetServiceId( TInt aServiceId )
       
   235     {
       
   236     SCPLOGSTRING3( "CScpService[0x%x]::SetServiceId service id: %d", 
       
   237                    this, aServiceId );
       
   238 
       
   239     iServiceId = aServiceId;
       
   240     }
       
   241 
       
   242 // -----------------------------------------------------------------------------
       
   243 // CScpService::State
       
   244 // -----------------------------------------------------------------------------
       
   245 //
       
   246 TInt CScpService::State( TCCHSubserviceType aSubServiceType, 
       
   247                          TCCHSubserviceState& aState ) const
       
   248     {
       
   249     SCPLOGSTRING2( "CScpService[0x%x]::State", this );
       
   250 
       
   251     TInt result = KErrNone;
       
   252 
       
   253     if( ECCHUnknown == aSubServiceType )
       
   254         {
       
   255         TCCHSubserviceState state( ECCHUninitialized );
       
   256 
       
   257         // Return other than unknown state if all the subservices are in same state 
       
   258         for ( TInt i=0; i<iSubServices.Count(); i++ )
       
   259             {
       
   260             const CScpSubService* subService = iSubServices[i];
       
   261             
       
   262             if( state == ECCHUninitialized )
       
   263                 {
       
   264                 state = subService->State();
       
   265                 result = subService->LastReportedError();
       
   266                 } 
       
   267             else
       
   268                 {
       
   269                 if ( state != subService->State() ||
       
   270                      result != subService->LastReportedError() )
       
   271                     {   
       
   272                     // Sub services are in different states or have
       
   273                     // different error codes
       
   274                     SCPLOGSTRING( "Query failed: different states or error codes" );
       
   275 
       
   276                     state = ECCHUninitialized;
       
   277                     result = KErrUnknown;
       
   278                     break;
       
   279                     }
       
   280                 }
       
   281             }
       
   282 
       
   283         aState = state;
       
   284         }
       
   285     else
       
   286         {
       
   287         CScpSubService* subService = GetSubServiceByType( aSubServiceType );
       
   288 
       
   289         if( subService )
       
   290             {
       
   291             aState = subService->State();
       
   292             result = subService->LastReportedError();    
       
   293             }
       
   294         else
       
   295             {
       
   296             result = KErrNotFound;
       
   297             }
       
   298         }
       
   299 
       
   300     SCPLOGSTRING3( "State: %d error: %d", aState, result );
       
   301     return result;
       
   302     }
       
   303 
       
   304 // -----------------------------------------------------------------------------
       
   305 // CScpService::GetSubServiceByType
       
   306 // -----------------------------------------------------------------------------
       
   307 //
       
   308 CScpSubService* CScpService::GetSubServiceByType( TCCHSubserviceType aSubServiceType ) const
       
   309     {
       
   310     SCPLOGSTRING3( "CScpService[0x%x]::GetSubService type: %d",
       
   311                    this, aSubServiceType );
       
   312 
       
   313     for( TInt i=0; i<iSubServices.Count(); i++)
       
   314         {
       
   315         CScpSubService* subService = iSubServices[ i ];
       
   316         if( subService->SubServiceType() == aSubServiceType )
       
   317             {
       
   318             return subService;
       
   319             }
       
   320         }
       
   321 
       
   322     return NULL; 
       
   323     }
       
   324 
       
   325 // -----------------------------------------------------------------------------
       
   326 // CScpService::ContainsSubServiceType
       
   327 // -----------------------------------------------------------------------------
       
   328 //
       
   329 TBool CScpService::ContainsSubServiceType( TCCHSubserviceType aSubServiceType ) const
       
   330     {
       
   331     SCPLOGSTRING3( "CScpService[0x%x]::ContainsSubServiceType type: %d",
       
   332                    this, aSubServiceType );
       
   333 
       
   334     for( TInt i=0; i<iSubServices.Count(); i++)
       
   335         {
       
   336         CScpSubService* subService = iSubServices[ i ];
       
   337         if( subService->SubServiceType() == aSubServiceType )
       
   338             {
       
   339             return ETrue;
       
   340             }
       
   341         }
       
   342 
       
   343     return EFalse; 
       
   344     }
       
   345 
       
   346 
       
   347 // -----------------------------------------------------------------------------
       
   348 // CScpService::SubServicesContainSameSipProfile
       
   349 // -----------------------------------------------------------------------------
       
   350 //
       
   351 TBool CScpService::SubServicesContainSameSipProfile() const
       
   352     {
       
   353     SCPLOGSTRING2( "CScpService[0x%x]::SubServicesContainSameSipProfile", this );    
       
   354 
       
   355     TInt sipProfileId( 0 );
       
   356 
       
   357     for( TInt i=0; i<iSubServices.Count(); i++)
       
   358         {
       
   359         TInt oldSipProfileId = sipProfileId;
       
   360         
       
   361         const CScpSubService* subService = iSubServices[ i ];
       
   362         sipProfileId = subService->SipProfileId();
       
   363 
       
   364         if( oldSipProfileId != 0 &&
       
   365             sipProfileId != oldSipProfileId )
       
   366             {
       
   367             return EFalse;
       
   368             }
       
   369         }
       
   370 
       
   371     return ETrue; 
       
   372     }
       
   373 
       
   374 // -----------------------------------------------------------------------------
       
   375 // CScpService::SetReserved
       
   376 // -----------------------------------------------------------------------------
       
   377 //
       
   378 TInt CScpService::SetReserved( TBool aReserved, 
       
   379                                TCCHSubserviceType aSubServiceType )
       
   380     {
       
   381     SCPLOGSTRING4( "CScpService[0x%x]::SetReserved: %d type: %d", 
       
   382                    this, aSubServiceType, aReserved );    
       
   383 
       
   384     TInt result = KErrNone;
       
   385 
       
   386     if( aSubServiceType != ECCHUnknown )
       
   387         {
       
   388         CScpSubService* subService = GetSubServiceByType( aSubServiceType );
       
   389 
       
   390         if( subService )
       
   391             {
       
   392             // If reserving the sub service must be enabled.
       
   393             // It is always possible to free the service
       
   394             if( subService->State() == ECCHEnabled ||
       
   395                 aReserved == EFalse )
       
   396                 {
       
   397                 subService->SetReserved( aReserved );
       
   398                 }
       
   399             else
       
   400                 {
       
   401                 result = KErrNotSupported;
       
   402                 }
       
   403             }
       
   404         else
       
   405             {
       
   406             result = KErrNotFound;
       
   407             }
       
   408         }
       
   409     else
       
   410         {
       
   411         for( TInt i=0; i<iSubServices.Count(); i++)
       
   412             {
       
   413             CScpSubService* subService = iSubServices[ i ];
       
   414 
       
   415             // If reserving the sub service must be enabled.
       
   416             // It is always possible to free the service
       
   417             if( subService->State() == ECCHEnabled ||
       
   418                 aReserved == EFalse )
       
   419                 {
       
   420                 subService->SetReserved( aReserved );
       
   421                 }
       
   422             else
       
   423                 {
       
   424                 result = KErrNotSupported;
       
   425                 }
       
   426             }
       
   427         }
       
   428 
       
   429     return result;
       
   430     }
       
   431 
       
   432 // -----------------------------------------------------------------------------
       
   433 // CScpService::IsReserved
       
   434 // -----------------------------------------------------------------------------
       
   435 //
       
   436 TBool CScpService::IsReserved( TCCHSubserviceType aSubServiceType ) const
       
   437     {
       
   438     SCPLOGSTRING3( "CScpService[0x%x]::IsReserved: type: %d", 
       
   439                    this, aSubServiceType );
       
   440 
       
   441     TBool result = EFalse;
       
   442 
       
   443     if( aSubServiceType == ECCHUnknown )
       
   444         {
       
   445         for( TInt i=0; i<iSubServices.Count(); i++)
       
   446             {
       
   447             CScpSubService* subService = iSubServices[ i ];
       
   448 
       
   449             TBool reserved = subService->IsReserved();
       
   450 
       
   451             if( reserved )
       
   452                 {
       
   453                 result = ETrue;
       
   454                 break;
       
   455                 }
       
   456             }
       
   457         }
       
   458     else
       
   459         {
       
   460         CScpSubService* subService = GetSubServiceByType( aSubServiceType );
       
   461 
       
   462         if( subService )
       
   463             {
       
   464             result = subService->IsReserved();
       
   465             }
       
   466         }
       
   467 
       
   468     return result;
       
   469     }
       
   470 
       
   471 // -----------------------------------------------------------------------------
       
   472 // CScpService::SetAccessPointId
       
   473 // -----------------------------------------------------------------------------
       
   474 //
       
   475 TInt CScpService::SetAccessPointId( TCCHSubserviceType aSubServiceType, 
       
   476                                     TScpAccessPointType aAccessPointType, 
       
   477                                     TInt aAccessPointId )
       
   478     {
       
   479     SCPLOGSTRING4( "CScpService[0x%x]::SetAccessPointId: type: %d id: %d", 
       
   480                     this, aSubServiceType, aAccessPointId );
       
   481     __ASSERT_DEBUG( iSubServices.Count() > 0, User::Panic( KNullDesC, KErrGeneral ) );
       
   482 
       
   483     TInt result = KErrNotFound;
       
   484 
       
   485     if( IsAccessPointModifiable( aSubServiceType ) )
       
   486         {
       
   487         TBool sipConnectionCreated = EFalse;
       
   488         CScpSipConnection* sipConnection = GetSipConnection( aSubServiceType,
       
   489                                                              sipConnectionCreated );
       
   490         if( sipConnection )
       
   491             {      
       
   492             switch ( aAccessPointType )
       
   493                 {
       
   494                 case EScpIap:
       
   495                     {
       
   496                     result = sipConnection->SetIap( aAccessPointId );
       
   497                     }
       
   498                     break;
       
   499             
       
   500                 case EScpSnap:
       
   501                     {
       
   502                     result = sipConnection->SetSnap( aAccessPointId );
       
   503                     }
       
   504                     break;
       
   505 
       
   506                 default:
       
   507                     break;
       
   508 
       
   509                 }
       
   510 
       
   511             if( sipConnectionCreated )
       
   512                 {
       
   513                 delete sipConnection;
       
   514                 }
       
   515             }
       
   516         }
       
   517     else
       
   518         {
       
   519         result = KErrNotSupported;
       
   520         }
       
   521 
       
   522     return result;
       
   523     }
       
   524 
       
   525 // -----------------------------------------------------------------------------
       
   526 // CScpService::IsAccessPointModifiable
       
   527 // -----------------------------------------------------------------------------
       
   528 //
       
   529 TBool CScpService::IsAccessPointModifiable( TCCHSubserviceType aSubServiceType ) const
       
   530     {
       
   531     SCPLOGSTRING3( "CScpService[0x%x]::IsAccessPointModifiable: type: %d", 
       
   532                     this, aSubServiceType );
       
   533 
       
   534     TBool modifiable = EFalse;
       
   535 
       
   536     TCCHSubserviceState state = ECCHUninitialized;
       
   537 
       
   538     TInt error = State( aSubServiceType, state );
       
   539 
       
   540     if( state == ECCHDisabled || ( state == ECCHConnecting && error != KErrNone ) )
       
   541 
       
   542         {
       
   543         if( !IsReserved( aSubServiceType ) )
       
   544             {
       
   545             // We can't set any valid access point values if different
       
   546             // sip profiles are in use
       
   547             if( aSubServiceType != ECCHUnknown || ( aSubServiceType == ECCHUnknown &&
       
   548                                                     SubServicesContainSameSipProfile() ) )
       
   549                 {
       
   550                 modifiable = ETrue;
       
   551                 } 
       
   552             }
       
   553         }
       
   554 
       
   555     return modifiable;
       
   556     }
       
   557 
       
   558 // -----------------------------------------------------------------------------
       
   559 // CScpService::GetSipConnection
       
   560 // -----------------------------------------------------------------------------
       
   561 //
       
   562 CScpSipConnection* CScpService::GetSipConnection( TCCHSubserviceType aSubServiceType,
       
   563                                                   TBool& aSipConnectionCreated )
       
   564     {
       
   565     SCPLOGSTRING3( "CScpService[0x%x]::GetSipConnection: type: %d", 
       
   566                     this, aSubServiceType );
       
   567 
       
   568     CScpSipConnection* sipConnection = NULL;
       
   569     CScpSubService* subService = NULL;
       
   570 
       
   571     if( aSubServiceType == ECCHUnknown && iSubServices.Count() > 0 )
       
   572         {
       
   573         subService = iSubServices[0];
       
   574         }
       
   575     else
       
   576         {
       
   577         subService = GetSubServiceByType( aSubServiceType );
       
   578         }
       
   579 
       
   580     if( subService )
       
   581         {
       
   582         TInt sipProfileId = subService->SipProfileId();
       
   583 
       
   584         if( iProfileHandler.ProfileExists( sipProfileId ) )
       
   585             {
       
   586             if( iProfileHandler.SipConnectionExists( sipProfileId ) )
       
   587                 {
       
   588                 sipConnection = iProfileHandler.GetSipConnection( sipProfileId );
       
   589                 }
       
   590             else
       
   591                 {
       
   592                 aSipConnectionCreated = ETrue;
       
   593                 TRAP_IGNORE( sipConnection = 
       
   594                              iProfileHandler.CreateSipConnectionL( sipProfileId ) );
       
   595                 }
       
   596             }
       
   597         }
       
   598 
       
   599     return sipConnection;  
       
   600     }
       
   601 
       
   602 // -----------------------------------------------------------------------------
       
   603 // CScpService::IsAllSubservicesDisabled
       
   604 // -----------------------------------------------------------------------------
       
   605 //
       
   606 TBool CScpService::IsAllSubservicesDisabled() const
       
   607     {
       
   608     TBool response( ETrue );
       
   609        
       
   610     for ( TInt i( 0 ); i < iSubServices.Count() && response; i++ )
       
   611         {
       
   612         SCPLOGSTRING3( "CScpService[0x%x]::IsAllSubservicesDisabled: state: %d", 
       
   613                             this, iSubServices[ i ]->State() );
       
   614         if ( ECCHDisabled != iSubServices[ i ]->State() )
       
   615             {
       
   616             response = EFalse;
       
   617             }
       
   618         }
       
   619     
       
   620     return response;
       
   621     }
       
   622         
       
   623 // -----------------------------------------------------------------------------
       
   624 // CScpService::ChangeLastReportedErrors
       
   625 // -----------------------------------------------------------------------------
       
   626 //
       
   627 void CScpService::ChangeLastReportedErrors(
       
   628     const TInt aError )
       
   629     {
       
   630     for ( TInt i( 0 ); i < iSubServices.Count(); i++ )
       
   631         {
       
   632         iSubServices[ i ]->SetLastReportedError( aError );
       
   633         }   
       
   634     }
       
   635 
       
   636 // End of file