localconnectivityservice/dun/utils/src/DunNetDataport.cpp
branchRCL_3
changeset 42 0aa8cc770c8a
equal deleted inserted replaced
40:453dfc402455 42:0aa8cc770c8a
       
     1 /*
       
     2 * Copyright (c) 2006-2008 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:  Dataport specific network resource accessor implementation
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <mmtsy_names.h>
       
    20 #include "DunNetDataport.h"
       
    21 #include "DunUtils.h"
       
    22 #include "DunDebug.h"
       
    23 
       
    24 _LIT(DUN_GGP_DATAPORT_CSY_PORT, "::DUN");
       
    25 _LIT(DUN_GGP_DATAPORT_CSY, "DATAPORT");
       
    26 
       
    27 // ======== MEMBER FUNCTIONS ========
       
    28 
       
    29 // ---------------------------------------------------------------------------
       
    30 // Two-phased constructor.
       
    31 // ---------------------------------------------------------------------------
       
    32 //
       
    33 CDunNetDataport* CDunNetDataport::NewL( TInt aNumOfMaxChannels )
       
    34     {
       
    35     CDunNetDataport* self = new (ELeave) CDunNetDataport( aNumOfMaxChannels );
       
    36     CleanupStack::PushL( self );
       
    37     self->ConstructL();
       
    38     CleanupStack::Pop( self );
       
    39     return self;
       
    40     }
       
    41 
       
    42 // ---------------------------------------------------------------------------
       
    43 // Destructor.
       
    44 // ---------------------------------------------------------------------------
       
    45 //
       
    46 CDunNetDataport::~CDunNetDataport()
       
    47     {
       
    48     FTRACE(FPrint(_L( "CDunNetDataport::~CDunNetDataport()")));
       
    49     TInt i;
       
    50     TInt count = iEntities.Count();
       
    51     for ( i=0; i<count; i++ )
       
    52         {
       
    53         DeleteNetworkEntity( i, ETrue );
       
    54         }
       
    55     DeleteNetwork();
       
    56     FTRACE(FPrint(_L( "CDunNetDataport::~CDunNetDataport() complete")));
       
    57     }
       
    58 
       
    59 // ---------------------------------------------------------------------------
       
    60 // From class MDunNetwork (MDunNetDataport -> MDunNetwork).
       
    61 // Initializes network for Dataport
       
    62 // Must be called before any other operation
       
    63 // ---------------------------------------------------------------------------
       
    64 //
       
    65 void CDunNetDataport::InitializeL()
       
    66     {
       
    67     FTRACE(FPrint(_L( "CDunNetDataport::InitializeL()")));
       
    68     AllocatePhoneObjectsL();
       
    69     FTRACE(FPrint(_L( "CDunNetDataport::InitializeL() complete")));
       
    70     }
       
    71 
       
    72 // ---------------------------------------------------------------------------
       
    73 // From class MDunNetDataport.
       
    74 // Called when channel was created by transporter for Dataport
       
    75 // Initializes network for channel creation
       
    76 // ---------------------------------------------------------------------------
       
    77 //
       
    78 TInt CDunNetDataport::AllocateChannel( RComm*& aComm )
       
    79     {
       
    80     FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel()")));
       
    81     TInt firstFree = InitializeFirstFreeEntity();
       
    82     if ( firstFree < 0 )
       
    83         {
       
    84         FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() (not found) complete")));
       
    85         return firstFree;
       
    86         }
       
    87     if ( firstFree >= iEntities.Count() )
       
    88         {
       
    89         FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() (firstfree failed!) complete")));
       
    90         return KErrGeneral;
       
    91         }
       
    92     TInt retTemp = iEntities[firstFree].iMobileCall.OpenNewCall( iMobileLine );
       
    93     if ( retTemp != KErrNone )
       
    94         {
       
    95         RemoveEntity( firstFree );  // remove unused initialized channel
       
    96         FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() OpenNewCall FAILED %d" ), retTemp));
       
    97         return KErrGeneral;
       
    98         }
       
    99     retTemp = iEntities[firstFree].iMobileCall.Connect();
       
   100     if ( retTemp != KErrNone )
       
   101         {
       
   102         RemoveEntity( firstFree );  // remove unused initialized channel
       
   103         FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() iEntities[%d]->iMobileCall.Connect FAILED %d" ), firstFree, retTemp));
       
   104         return KErrGeneral;
       
   105         }
       
   106     RCall::TCommPort portName;
       
   107     portName.iPort.Copy( DUN_GGP_DATAPORT_CSY );
       
   108     portName.iPort.Append( DUN_GGP_DATAPORT_CSY_PORT );
       
   109     retTemp = iEntities[firstFree].iMobileCall.LoanDataPort( portName );
       
   110     if ( retTemp != KErrNone )
       
   111         {
       
   112         RemoveEntity( firstFree );  // remove unused initialized channel
       
   113         if ( retTemp == KErrEtelPortNotLoanedToClient )
       
   114             {
       
   115             FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() (too big) complete")));
       
   116             return KErrTooBig;
       
   117             }
       
   118         FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() iEntities[%d]->iMobileCall.LoanDataPort FAILED %d" ), firstFree, retTemp));
       
   119         return KErrGeneral;
       
   120         }
       
   121     FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() Created call object at index %d" ), firstFree));
       
   122     retTemp = iEntities[firstFree].iDataport.Open( iCommServer,
       
   123                                                    portName.iPort,
       
   124                                                    ECommExclusive,
       
   125                                                    ECommRoleDTE );
       
   126     if ( retTemp != KErrNone )
       
   127         {
       
   128         RemoveEntity( firstFree );  // remove unused initialized channel
       
   129         FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() FAILED to open dataport %d"), retTemp));
       
   130         return KErrGeneral;
       
   131         }
       
   132     iEntities[firstFree].iDataport.ResetBuffers();
       
   133     iEntities[firstFree].iEntityInUse = ETrue;
       
   134     aComm = &iEntities[firstFree].iDataport;
       
   135     FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() (iEntities[%d]->iDataport) opened"), firstFree));
       
   136     FTRACE(FPrint(_L( "CDunNetDataport::AllocateChannel() complete")));
       
   137     return KErrNone;
       
   138     }
       
   139 
       
   140 // ---------------------------------------------------------------------------
       
   141 // From class MDunNetDataport.
       
   142 // Called when channel was deleted/closed by transporter for Dataport
       
   143 // Uninitializes network for channel deletion/close
       
   144 // ---------------------------------------------------------------------------
       
   145 //
       
   146 TInt CDunNetDataport::FreeChannel( RComm* aComm )
       
   147     {
       
   148     FTRACE(FPrint(_L( "CDunNetDataport::FreeChannel()")));
       
   149     TInt i;
       
   150     TInt count = iEntities.Count();
       
   151     for ( i=0; i<count; i++ )
       
   152         {
       
   153         if ( &iEntities[i].iDataport == aComm )
       
   154             {
       
   155             if ( iEntities[i].iEntityInUse )
       
   156                 {
       
   157                 break;
       
   158                 }
       
   159             }
       
   160         }
       
   161     if ( i >= count )
       
   162         {
       
   163         FTRACE(FPrint(_L( "CDunNetDataport::FreeChannel() (not found) complete")));
       
   164         return KErrNotFound;
       
   165         }
       
   166     DeleteNetworkEntity( i, ETrue );
       
   167     FTRACE(FPrint(_L( "CDunNetDataport::FreeChannel() (iEntities[%d]->iDataport) freed"), i));
       
   168     FTRACE(FPrint(_L( "CDunNetDataport::FreeChannel() complete")));
       
   169     return KErrNone;
       
   170     }
       
   171 
       
   172 // ---------------------------------------------------------------------------
       
   173 // From class MDunNetDataport.
       
   174 // Gets index by network ID for Dataport
       
   175 // ---------------------------------------------------------------------------
       
   176 //
       
   177 TInt CDunNetDataport::GetIndexById( RComm* aComm )
       
   178     {
       
   179     FTRACE(FPrint(_L( "CDunNetDataport::GetIndexById()")));
       
   180     TInt i;
       
   181     TInt count = iEntities.Count();
       
   182     for ( i=0; i<count; i++ )
       
   183         {
       
   184         TDunDataportEntity& entity = iEntities[i];
       
   185         if ( entity.iEntityInUse )
       
   186             {
       
   187             if ( &entity.iDataport == aComm )
       
   188                 {
       
   189                 return i;
       
   190                 }
       
   191             }
       
   192         }
       
   193     FTRACE(FPrint(_L( "CDunNetDataport::GetIndexById() (not found) complete")));
       
   194     return KErrNotFound;
       
   195     }
       
   196 
       
   197 // ---------------------------------------------------------------------------
       
   198 // CDunNetDataport::CDunNetDataport
       
   199 // ---------------------------------------------------------------------------
       
   200 //
       
   201 CDunNetDataport::CDunNetDataport( TInt aNumOfMaxChannels ) :
       
   202     iNumOfMaxChannels( aNumOfMaxChannels )
       
   203     {
       
   204     }
       
   205 
       
   206 // ---------------------------------------------------------------------------
       
   207 // CDunNetDataport::ConstructL
       
   208 // ---------------------------------------------------------------------------
       
   209 //
       
   210 void CDunNetDataport::ConstructL()
       
   211     {
       
   212     FTRACE(FPrint(_L( "CDunNetDataport::ConstructL()")));
       
   213     if ( iNumOfMaxChannels < 0 )
       
   214         {
       
   215         User::Leave( KErrGeneral );
       
   216         }
       
   217     TInt retTemp = CDunUtils::ConnectCommsServer( iCommServer );
       
   218     if ( retTemp != KErrNone )
       
   219         {
       
   220         FTRACE(FPrint(_L( "CDunNetDataport::ConstructL() FAILED to connect comms server")));
       
   221         User::Leave( retTemp );
       
   222         }
       
   223     retTemp = iCommServer.LoadCommModule( DUN_GGP_DATAPORT_CSY );
       
   224     if ( retTemp != KErrNone )
       
   225         {
       
   226         iCommServer.Close();
       
   227         FTRACE(FPrint(_L( "CDunNetDataport::ConstructL() FAILED to load comm module")));
       
   228         User::Leave( retTemp );
       
   229         }
       
   230     FTRACE(FPrint(_L( "CDunNetDataport::ConstructL() complete")));
       
   231     }
       
   232 
       
   233 // ---------------------------------------------------------------------------
       
   234 // Allocates phone objects for use
       
   235 // ---------------------------------------------------------------------------
       
   236 //
       
   237 void CDunNetDataport::AllocatePhoneObjectsL()
       
   238     {
       
   239     FTRACE(FPrint(_L( "CDunNetDataport::AllocatePhoneObjectsL()" ) ));
       
   240     TInt retTemp;
       
   241     retTemp = iTelServer.Connect();
       
   242     if ( retTemp != KErrNone )
       
   243         {
       
   244         FTRACE(FPrint(_L( "CDunNetDataport::AllocatePhoneObjectsL() iTelServer Connect FAILED %d" ), retTemp ));
       
   245         User::Leave( retTemp );
       
   246         }
       
   247     retTemp = iTelServer.LoadPhoneModule( KMmTsyModuleName );
       
   248     if ( retTemp!=KErrNone && retTemp!=KErrAlreadyExists )
       
   249         {
       
   250         FTRACE(FPrint(_L( "CDunNetDataport::AllocatePhoneObjectsL() LoadPhoneModule FAILED %d" ), retTemp ));
       
   251         User::Leave( retTemp );
       
   252         }
       
   253     retTemp = iMobilePhone.Open( iTelServer, KMmTsyPhoneName );
       
   254     if ( retTemp != KErrNone )
       
   255         {
       
   256         FTRACE(FPrint(_L( "CDunNetDataport::AllocatePhoneObjectsL() MobilePhone Open FAILED %d" ), retTemp ));
       
   257         User::Leave( retTemp );
       
   258         }
       
   259     retTemp = iMobileLine.Open( iMobilePhone, KMmTsyDataLineName );
       
   260     if ( retTemp != KErrNone )
       
   261         {
       
   262         FTRACE(FPrint(_L( "CDunNetDataport::AllocatePhoneObjectsL() MobileLine Open FAILED %d" ), retTemp ));
       
   263         User::Leave( retTemp );
       
   264         }
       
   265     FTRACE(FPrint(_L( "CDunNetDataport::AllocatePhoneObjectsL() complete" ) ));
       
   266     }
       
   267 
       
   268 // ---------------------------------------------------------------------------
       
   269 // Initializes first free entity
       
   270 // ---------------------------------------------------------------------------
       
   271 //
       
   272 TInt CDunNetDataport::InitializeFirstFreeEntity()
       
   273     {
       
   274     FTRACE(FPrint(_L( "CDunNetDataport::InitializeFirstFreeEntity()")));
       
   275     TInt i;
       
   276     TInt retTemp;
       
   277     TInt count = iEntities.Count();
       
   278     for ( i=0; i<count; i++ )
       
   279         {
       
   280         if ( !iEntities[i].iEntityInUse )
       
   281             {
       
   282             FTRACE(FPrint( _L("CDunNetDataport::InitializeFirstFreeEntity() complete" )));
       
   283             return i;
       
   284             }
       
   285         }
       
   286     // Free channel not found, now create new if possible
       
   287     if ( iNumOfMaxChannels!=0 && iEntities.Count()>=iNumOfMaxChannels )
       
   288         {
       
   289         FTRACE(FPrint( _L("CDunNetDataport::InitializeFirstFreeEntity() (too big) complete" )));
       
   290         return KErrTooBig;
       
   291         }
       
   292     TDunDataportEntity emptyEntity;
       
   293     emptyEntity.iEntityInUse = EFalse;
       
   294     retTemp = iEntities.Append( emptyEntity );
       
   295     if ( retTemp != KErrNone )
       
   296         {
       
   297         FTRACE(FPrint( _L("CDunNetDataport::InitializeFirstFreeEntity() (append failed!) complete" )));
       
   298         return retTemp;
       
   299         }
       
   300     FTRACE(FPrint(_L( "CDunNetDataport::InitializeFirstFreeEntity() complete")));
       
   301     return i;
       
   302     }
       
   303 
       
   304 // ---------------------------------------------------------------------------
       
   305 // Remove network entity by index
       
   306 // ---------------------------------------------------------------------------
       
   307 //
       
   308 TInt CDunNetDataport::RemoveEntity( TInt aIndex )
       
   309     {
       
   310     FTRACE(FPrint(_L( "CDunNetDataport::RemoveEntity()")));
       
   311     if ( aIndex < 0 ||
       
   312          aIndex >= iEntities.Count() )
       
   313         {
       
   314         FTRACE(FPrint(_L( "CDunNetDataport::RemoveEntity() (not found) complete")));
       
   315         return KErrNotFound;
       
   316         }
       
   317     DeleteNetworkEntity( aIndex, EFalse );
       
   318     FTRACE(FPrint(_L( "CDunNetDataport::RemoveEntity() complete")));
       
   319     return KErrNone;
       
   320     }
       
   321 
       
   322 // ---------------------------------------------------------------------------
       
   323 // Deletes own internal data
       
   324 // ---------------------------------------------------------------------------
       
   325 //
       
   326 void CDunNetDataport::DeleteNetwork()
       
   327     {
       
   328     FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork()")));
       
   329     if ( iMobileLine.SubSessionHandle() )
       
   330         {
       
   331         iMobileLine.Close();
       
   332         FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork() mobile line closed")));
       
   333         }
       
   334     if ( iMobilePhone.SubSessionHandle() )
       
   335         {
       
   336         iMobilePhone.Close();
       
   337         FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork() mobile phone closed")));
       
   338         }
       
   339     if ( iTelServer.Handle() )
       
   340         {
       
   341         iTelServer.UnloadPhoneModule( KMmTsyModuleName );
       
   342         FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork() phone module unloaded")));
       
   343         iTelServer.Close();
       
   344         FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork() phone module closed")));
       
   345         }
       
   346     if ( iCommServer.Handle() )
       
   347         {
       
   348         iCommServer.UnloadCommModule( DUN_GGP_DATAPORT_CSY );
       
   349         FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork() comm module unloaded")));
       
   350         iCommServer.Close();
       
   351         FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork() comm module closed")));
       
   352         }
       
   353     iEntities.Close();
       
   354     FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetwork() complete")));
       
   355     }
       
   356 
       
   357 // ---------------------------------------------------------------------------
       
   358 // Deletes one network entity at index aIndex for Dataport
       
   359 // ---------------------------------------------------------------------------
       
   360 //
       
   361 TInt CDunNetDataport::DeleteNetworkEntity( TInt aIndex, TBool aCheckFree )
       
   362     {
       
   363     FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetworkEntity()")));
       
   364     if ( aIndex < 0 ||
       
   365          aIndex >= iEntities.Count() )
       
   366         {
       
   367         FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetworkEntity() (not found) complete"), aIndex));
       
   368         return KErrGeneral;
       
   369         }
       
   370     TDunDataportEntity& entity = iEntities[aIndex];
       
   371     if ( (aCheckFree&&entity.iEntityInUse) || !aCheckFree )
       
   372         {
       
   373         if ( entity.iDataport.SubSessionHandle() )
       
   374             {
       
   375             // The next will set KSignalDTEOutputs down twice for RComm
       
   376             // local media case because CDunSignalCopy clears them also which
       
   377             // in turn causes plugin to free channel. But this probably won't
       
   378             // cause any harm.
       
   379             entity.iDataport.SetSignals( 0, KSignalDTEOutputs );
       
   380             FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetworkEntity() RComm signals set")));
       
   381             entity.iDataport.Close();
       
   382             FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetworkEntity() RComm closed")));
       
   383             }
       
   384         if ( entity.iMobileCall.SubSessionHandle() )
       
   385             {
       
   386             entity.iMobileCall.RecoverDataPort();
       
   387             FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetworkEntity() Dataport recovered")));
       
   388             entity.iMobileCall.Close();
       
   389             FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetworkEntity() Dataport closed")));
       
   390             }
       
   391         entity.iEntityInUse = EFalse;
       
   392         }
       
   393     FTRACE(FPrint(_L( "CDunNetDataport::DeleteNetworkEntity() complete")));
       
   394     return KErrNone;
       
   395     }