localconnectivityservice/dun/utils/src/DunTransporter.cpp
branchRCL_3
changeset 20 4a793f564d72
parent 19 0aa8cc770c8a
child 21 74aa6861c87d
equal deleted inserted replaced
19:0aa8cc770c8a 20:4a793f564d72
     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:  Managing abstracted "channels" of network side communication
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include "DunTransUtils.h"
       
    20 #include "DunTransporter.h"
       
    21 #include "DunDataWaiter.h"
       
    22 #include "DunUpstream.h"
       
    23 #include "DunDownstream.h"
       
    24 #include "DunSignalCopy.h"
       
    25 #include "DunNoteHandler.h"
       
    26 #include "DunUtils.h"
       
    27 #include "DunDebug.h"
       
    28 #include "DunPlugin.h"
       
    29 
       
    30 // ---------------------------------------------------------------------------
       
    31 // Two-phased constructor.
       
    32 // ---------------------------------------------------------------------------
       
    33 //
       
    34 EXPORT_C CDunTransporter* CDunTransporter::NewL(
       
    35     MDunPluginManager* aPluginManager,
       
    36     TInt aNumOfMaxChannels )
       
    37     {
       
    38     CDunTransporter* self = new (ELeave) CDunTransporter( aPluginManager,
       
    39                                                           aNumOfMaxChannels );
       
    40     CleanupStack::PushL( self );
       
    41     self->ConstructL();
       
    42     CleanupStack::Pop( self );
       
    43     return self;
       
    44     }
       
    45 
       
    46 // ---------------------------------------------------------------------------
       
    47 // Destructor.
       
    48 // ---------------------------------------------------------------------------
       
    49 //
       
    50 CDunTransporter::~CDunTransporter()
       
    51     {
       
    52     FTRACE(FPrint( _L("CDunTransporter::~CDunTransporter()" )));
       
    53     UnInitialize();
       
    54     FTRACE(FPrint( _L("CDunTransporter::~CDunTransporter() complete" )));
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Number of allocated channels, is the same number as allocated and active
       
    59 // (non-waiting) channels
       
    60 // ---------------------------------------------------------------------------
       
    61 //
       
    62 EXPORT_C TInt CDunTransporter::NumberOfAllocatedChannels()
       
    63     {
       
    64     FTRACE(FPrint( _L("CDunTransporter::NumberOfAllocatedChannels()" )));
       
    65     TInt i;
       
    66     TInt allocChannels = 0;
       
    67     TInt count = iChannelData.Count();
       
    68     for ( i=0; i<count; i++ )
       
    69         {
       
    70         TDunChannelData& channelData = iChannelData[i];
       
    71         if ( channelData.iChannelInUse )
       
    72             {
       
    73             allocChannels++;
       
    74             }
       
    75         }
       
    76     FTRACE(FPrint( _L("CDunTransporter::NumberOfAllocatedChannels() complete" )));
       
    77     return allocChannels;
       
    78     }
       
    79 
       
    80 // ---------------------------------------------------------------------------
       
    81 // Number of waiting channels, is the same number as allocated and inactive
       
    82 // (waiting) channels
       
    83 // ---------------------------------------------------------------------------
       
    84 //
       
    85 EXPORT_C TInt CDunTransporter::NumberOfWaitingChannels()
       
    86     {
       
    87     FTRACE(FPrint( _L("CDunTransporter::NumberWaitingChannels()" )));
       
    88     if ( !iInitialized )
       
    89         {
       
    90         FTRACE(FPrint( _L("CDunTransporter::NumberWaitingChannels() complete" )));
       
    91         return 0;
       
    92         }
       
    93     TInt waiters = iChanMan->NumberOfWaiters();
       
    94     FTRACE(FPrint( _L("CDunTransporter::NumberWaitingChannels() complete" )));
       
    95     return waiters;
       
    96     }
       
    97 
       
    98 // ---------------------------------------------------------------------------
       
    99 // Gets the number of allocated channels by owner UID, is the same number as
       
   100 // allocated and active (non-waiting) channels
       
   101 // ---------------------------------------------------------------------------
       
   102 //
       
   103 EXPORT_C TInt CDunTransporter::GetNumberOfAllocatedChannelsByUid(
       
   104     TUid aOwnerUid )
       
   105     {
       
   106     FTRACE(FPrint( _L("CDunTransporter::GetNumberOfAllocatedChannelsByUid()" )));
       
   107     TInt i;
       
   108     TInt allocChannels = 0;
       
   109     TInt count = iChannelData.Count();
       
   110     for ( i=0; i<count; i++ )
       
   111         {
       
   112         TDunChannelData& channelData = iChannelData[i];
       
   113         if ( channelData.iOwnerUid==aOwnerUid && channelData.iChannelInUse )
       
   114             {
       
   115             allocChannels++;
       
   116             }
       
   117         }
       
   118     FTRACE(FPrint( _L("CDunTransporter::GetNumberOfAllocatedChannelsByUid() complete" )));
       
   119     return allocChannels;
       
   120     }
       
   121 
       
   122 // ---------------------------------------------------------------------------
       
   123 // Gets the number of waiting channels by owner UID, is the same number as
       
   124 // allocated and inactive (waiting) channels
       
   125 // ---------------------------------------------------------------------------
       
   126 //
       
   127 EXPORT_C TInt CDunTransporter::GetNumberOfWaitingChannelsByUid(
       
   128     TUid aOwnerUid )
       
   129     {
       
   130     FTRACE(FPrint( _L("CDunTransporter::GetNumberWaitingChannelsByUid()" )));
       
   131     if ( !iInitialized )
       
   132         {
       
   133         FTRACE(FPrint( _L("CDunTransporter::GetNumberWaitingChannelsByUid() complete" )));
       
   134         return 0;
       
   135         }
       
   136     TInt waiters = iChanMan->GetNumberOfWaitersByUid( aOwnerUid );
       
   137     FTRACE(FPrint( _L("CDunTransporter::GetNumberWaitingChannelsByUid() complete" )));
       
   138     return waiters;
       
   139     }
       
   140 
       
   141 // ---------------------------------------------------------------------------
       
   142 // Transporter's service advertisement status
       
   143 // ---------------------------------------------------------------------------
       
   144 //
       
   145 EXPORT_C TBool CDunTransporter::AdvertisementStatus()
       
   146     {
       
   147     FTRACE(FPrint( _L("CDunTransporter::AdvertisementStatus()" )));
       
   148     FTRACE(FPrint( _L("CDunTransporter::AdvertisementStatus() complete" )));
       
   149     return iAdvertise;
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // Creates a channel of communication between local media (aComm) and network
       
   154 // Local media object pointer also works as a connection ID for the
       
   155 // allocated channel
       
   156 // ---------------------------------------------------------------------------
       
   157 //
       
   158 EXPORT_C void CDunTransporter::AllocateChannelL(
       
   159     RComm* aComm,
       
   160     TUid aOwnerUid,
       
   161     const TDesC8& aName,
       
   162     TBool aEnqueuedFail,
       
   163     MDunBufferCorrection* aCorrection )
       
   164     {
       
   165     FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RComm)" )));
       
   166 
       
   167     if ( !aComm )
       
   168         {
       
   169         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (aComm not initialized!) complete" )));
       
   170         User::Leave( KErrGeneral );
       
   171         }
       
   172 
       
   173     if ( !aComm->SubSessionHandle() )
       
   174         {
       
   175         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RComm) (bad handle) complete" ) ));
       
   176         User::Leave( KErrBadHandle );
       
   177         }
       
   178 
       
   179     TInt retTemp = InitializeOnDemand();
       
   180     if ( retTemp != KErrNone )
       
   181         {
       
   182         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RComm) (ERROR) complete" ) ));
       
   183         User::Leave( retTemp );
       
   184         }
       
   185 
       
   186     iChanMan->AddConnWaiterL( aComm,
       
   187                               aOwnerUid,
       
   188                               aName,
       
   189                               aEnqueuedFail,
       
   190                               aCorrection );
       
   191 
       
   192     FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RComm) complete" )));
       
   193     }
       
   194 
       
   195 // ---------------------------------------------------------------------------
       
   196 // Creates a channel of communication between local media (aSocket) and
       
   197 // network
       
   198 // Local media object pointer also works as a connection ID for the
       
   199 // allocated channel
       
   200 // ---------------------------------------------------------------------------
       
   201 //
       
   202 EXPORT_C void CDunTransporter::AllocateChannelL(
       
   203     RSocket* aSocket,
       
   204     TUid aOwnerUid,
       
   205     const TDesC8& aName,
       
   206     TBool aEnqueuedFail,
       
   207     TBool& aNoFreeChans )
       
   208     {
       
   209     FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket)" )));
       
   210 
       
   211     aNoFreeChans = EFalse;  // Initialize now if plugin didn't do it already
       
   212 
       
   213     if ( !aSocket )
       
   214         {
       
   215         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (aSocket not initialized!) complete" )));
       
   216         User::Leave( KErrGeneral );
       
   217         }
       
   218     if ( !aSocket->SubSessionHandle() )
       
   219         {
       
   220         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) (bad handle) complete" ) ));
       
   221         User::Leave( KErrBadHandle );
       
   222         }
       
   223     TInt retTemp = InitializeOnDemand();
       
   224     if ( retTemp != KErrNone )
       
   225         {
       
   226         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) (ERROR) complete" )));
       
   227         User::Leave( retTemp );
       
   228         }
       
   229     TInt firstFree = iUtility->InitializeFirstFreeChannel( aSocket );
       
   230     if ( firstFree < 0 )
       
   231         {
       
   232         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) (firstfree failed!) complete" ) ));
       
   233         User::Leave( firstFree );
       
   234         }
       
   235     if ( firstFree >= iChannelData.Count() )
       
   236         {
       
   237         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) (firstfree failed!) complete" ) ));
       
   238         User::Leave( KErrGeneral );
       
   239         }
       
   240     TInt bufferLength = KErrNotFound;
       
   241     // bufferLength will be omitted (not needed to set to RSocket)
       
   242     TRAPD( retTrap,
       
   243         iUtility->DoAllocateChannelL(NULL, bufferLength, firstFree, NULL) );
       
   244     if ( retTrap != KErrNone )
       
   245         {
       
   246         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) trapped!" ) ));
       
   247         UnInitializeOnDemand();  // remove unused initialized channel
       
   248         if ( retTrap == KErrTooBig )
       
   249             {
       
   250             if ( aEnqueuedFail )
       
   251                 {
       
   252                 // Inform plugin enqueue request
       
   253                 iPluginManager->NotifyPluginEnqueueRequest( aOwnerUid );
       
   254                 }
       
   255             aNoFreeChans = ETrue;  // Inform plugin about no free channels
       
   256             FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) complete" )));
       
   257             User::Leave( KErrTooBig );
       
   258             }
       
   259         FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) complete" )));
       
   260         User::Leave( retTrap );
       
   261         }
       
   262     TDunChannelData& channelData = iChannelData[firstFree];
       
   263     channelData.iSocket = aSocket;
       
   264     channelData.iChannelName = HBufC8::NewMaxL( aName.Length() );
       
   265     TPtr8 chanNamePtr = channelData.iChannelName->Des();
       
   266     chanNamePtr.Copy( aName );
       
   267     channelData.iUpstreamRW->SetMedia( aSocket, EDunMediaContextLocal );
       
   268     channelData.iDownstreamRW->SetMedia( aSocket, EDunMediaContextLocal );
       
   269     channelData.iOwnerUid = aOwnerUid;
       
   270     // Channel now occupied
       
   271     channelData.iChannelInUse = ETrue;
       
   272 
       
   273     // Clear the queue, just to be sure
       
   274     iPluginManager->NotifyPluginDequeueRequest( aOwnerUid );
       
   275 
       
   276     FTRACE(FPrint( _L("CDunTransporter::AllocateChannel() (RSocket) complete" )));
       
   277     }
       
   278 
       
   279 // ---------------------------------------------------------------------------
       
   280 // Frees an allocated channel by local media (aComm) connection ID
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 EXPORT_C TInt CDunTransporter::FreeChannel( RComm* aComm )
       
   284     {
       
   285     FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RComm)" )));
       
   286 
       
   287     TInt retTemp = CheckInitAndHandle( aComm );
       
   288     if ( retTemp != KErrNone )
       
   289         {
       
   290         FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RComm) (ERROR) complete" ) ));
       
   291         return retTemp;
       
   292         }
       
   293 
       
   294     retTemp = iChanMan->RemoveConnWaiter( aComm );
       
   295     if ( retTemp == KErrNone )
       
   296         {
       
   297         FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RComm) complete" )));
       
   298         return KErrNone;
       
   299         }
       
   300 
       
   301     // No waiter found, so try to find from channels
       
   302     TInt mediaIndex = GetMediaIndex( aComm );
       
   303     if ( mediaIndex < 0 )
       
   304         {
       
   305         FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RComm) (ERROR) complete" ) ));
       
   306         return mediaIndex;
       
   307         }
       
   308 
       
   309     retTemp = iUtility->DoFreeChannel( mediaIndex );
       
   310     if ( retTemp != KErrNone )
       
   311         {
       
   312         FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RComm) (ERROR) complete" )));
       
   313         return retTemp;
       
   314         }
       
   315     UnInitializeOnDemand();
       
   316 
       
   317     FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RComm) complete" )));
       
   318     return KErrNone;
       
   319     }
       
   320 
       
   321 // ---------------------------------------------------------------------------
       
   322 // Frees an allocated channel by local media (aSocket) connection ID
       
   323 // ---------------------------------------------------------------------------
       
   324 //
       
   325 EXPORT_C TInt CDunTransporter::FreeChannel( RSocket* aSocket )
       
   326     {
       
   327     FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RSocket)" )));
       
   328 
       
   329     TInt retTemp = CheckInitAndHandle( aSocket );
       
   330     if ( retTemp != KErrNone )
       
   331         {
       
   332         FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RSocket) (ERROR) complete" ) ));
       
   333         return retTemp;
       
   334         }
       
   335 
       
   336     TInt mediaIndex = GetMediaIndex( aSocket );
       
   337     if ( mediaIndex < 0 )
       
   338         {
       
   339         return mediaIndex;
       
   340         }
       
   341 
       
   342     retTemp = iUtility->DoFreeChannel( mediaIndex );
       
   343     if ( retTemp != KErrNone )
       
   344         {
       
   345         FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RSocket) (ERROR) complete" ) ));
       
   346         return retTemp;
       
   347         }
       
   348     UnInitializeOnDemand();
       
   349 
       
   350     FTRACE(FPrint( _L("CDunTransporter::FreeChannel() (RSocket) complete" )));
       
   351     return KErrNone;
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // Issues transfer requests for all transfer objects by local media
       
   356 // (aComm) connection ID
       
   357 // This will cause the Transporter by be ready for transferring data
       
   358 // ---------------------------------------------------------------------------
       
   359 //
       
   360 EXPORT_C void CDunTransporter::IssueTransferRequestsL( RComm* aComm )
       
   361     {
       
   362     FTRACE(FPrint( _L("CDunTransporter::IssueTransferRequests() (RComm)" )));
       
   363 
       
   364     User::LeaveIfError( CheckInitAndHandle( aComm ) );
       
   365     TInt retTemp = iChanMan->IssueConnWaiterRequest( aComm );
       
   366     if ( retTemp == KErrNone )
       
   367         {
       
   368         FTRACE(FPrint( _L("CDunTransporter::IssueTransferRequests() (RComm) complete" )));
       
   369         return;
       
   370         }
       
   371 
       
   372     // No waiter found, so try to find from channels
       
   373     TInt mediaIndex = GetMediaIndexL( aComm );
       
   374     User::LeaveIfError( iUtility->DoIssueTransferRequests( mediaIndex ) );
       
   375 
       
   376     FTRACE(FPrint( _L("CDunTransporter::IssueTransferRequests() (RComm) complete" )));
       
   377     }
       
   378 
       
   379 // ---------------------------------------------------------------------------
       
   380 // Issues transfer requests for all transfer objects by local media
       
   381 // (aSocket) connection ID
       
   382 // This will cause the Transporter by be ready for transferring data
       
   383 // ---------------------------------------------------------------------------
       
   384 //
       
   385 EXPORT_C void CDunTransporter::IssueTransferRequestsL( RSocket* aSocket )
       
   386     {
       
   387     FTRACE(FPrint( _L("CDunTransporter::IssueTransferRequests() (RSocket)" )));
       
   388 
       
   389     User::LeaveIfError( CheckInitAndHandle( aSocket ) );
       
   390     TInt mediaIndex = GetMediaIndexL( aSocket );
       
   391     User::LeaveIfError( iUtility->DoIssueTransferRequests(mediaIndex) );
       
   392 
       
   393     FTRACE(FPrint( _L("CDunTransporter::IssueTransferRequests() (RSocket) complete" )));
       
   394     }
       
   395 
       
   396 // ---------------------------------------------------------------------------
       
   397 // Stops transfers for all transfer objects by local media (aComm)
       
   398 // connection ID
       
   399 // ---------------------------------------------------------------------------
       
   400 //
       
   401 EXPORT_C TInt CDunTransporter::StopTransfers( RComm* aComm )
       
   402     {
       
   403     FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RComm)" )));
       
   404 
       
   405     TInt retTemp = CheckInitAndHandle( aComm );
       
   406     if ( retTemp != KErrNone )
       
   407         {
       
   408         FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RComm) (ERROR) complete" ) ));
       
   409         return retTemp;
       
   410         }
       
   411 
       
   412     retTemp = iChanMan->StopConnWaiter( aComm );
       
   413     if ( retTemp == KErrNone )
       
   414         {
       
   415         FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RComm) complete" )));
       
   416         return KErrNone;
       
   417         }
       
   418 
       
   419     // No waiter found, so try to find from channels
       
   420     TInt mediaIndex = GetMediaIndex( aComm );
       
   421     if ( mediaIndex < 0 )
       
   422         {
       
   423         FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RComm) (ERROR) complete" ) ));
       
   424         return mediaIndex;
       
   425         }
       
   426 
       
   427     retTemp = iUtility->DoStopTransfers( mediaIndex );
       
   428     if ( retTemp != KErrNone )
       
   429         {
       
   430         FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RComm) (ERROR) complete" )));
       
   431         return retTemp;
       
   432         }
       
   433 
       
   434     FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RComm) complete" )));
       
   435     return KErrNone;
       
   436     }
       
   437 
       
   438 // ---------------------------------------------------------------------------
       
   439 // Stops transfers for all transfer objects by local media (aSocket)
       
   440 // connection ID
       
   441 // ---------------------------------------------------------------------------
       
   442 //
       
   443 EXPORT_C TInt CDunTransporter::StopTransfers( RSocket* aSocket )
       
   444     {
       
   445     FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RSocket)" )));
       
   446 
       
   447     TInt retTemp = CheckInitAndHandle( aSocket );
       
   448     if ( retTemp != KErrNone )
       
   449         {
       
   450         FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RSocket) (ERROR) complete" ) ));
       
   451         return retTemp;
       
   452         }
       
   453 
       
   454     TInt mediaIndex = GetMediaIndex( aSocket );
       
   455     if ( mediaIndex < 0 )
       
   456         {
       
   457         FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RSocket) (ERROR) complete" ) ));
       
   458         return mediaIndex;
       
   459         }
       
   460 
       
   461     retTemp = iUtility->DoStopTransfers( mediaIndex );
       
   462     if ( retTemp != KErrNone )
       
   463         {
       
   464         FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RSocket) (ERROR) complete" )));
       
   465         return retTemp;
       
   466         }
       
   467 
       
   468     FTRACE(FPrint( _L("CDunTransporter::StopTransfers() (RSocket) complete" )));
       
   469     return KErrNone;
       
   470     }
       
   471 
       
   472 // ---------------------------------------------------------------------------
       
   473 // Adds connection monitor callback for either local media or network side
       
   474 // by connection ID
       
   475 // Callbacks will be called read/write error is detected during endpoint
       
   476 // operation
       
   477 // ---------------------------------------------------------------------------
       
   478 //
       
   479 EXPORT_C void CDunTransporter::AddConnMonCallbackL( RComm* aComm,
       
   480                                                     MDunConnMon* aCallback,
       
   481                                                     TDunDirection aDirection,
       
   482                                                     TBool /*aSignal*/ )
       
   483     {
       
   484     FTRACE(FPrint( _L("CDunTransporter::AddConnMonCallbackL() (RComm)" )));
       
   485 
       
   486     User::LeaveIfError( CheckInitAndHandle( aComm ) );
       
   487     TInt retTemp = iChanMan->SaveWaiterConnMonCallbackL( aComm,
       
   488                                                          aCallback,
       
   489                                                          aDirection );
       
   490     if ( retTemp == KErrNone )
       
   491         {
       
   492         FTRACE(FPrint( _L("CDunTransporter::AddConnMonCallbackL() (RComm) complete" )));
       
   493         return;
       
   494         }
       
   495 
       
   496     TInt mediaIndex = GetMediaIndexL( aComm );
       
   497     User::LeaveIfError( iUtility->DoAddConnMonCallback( mediaIndex,
       
   498                                                         aCallback,
       
   499                                                         aDirection,
       
   500                                                         NULL ));
       
   501 
       
   502     FTRACE(FPrint( _L("CDunTransporter::AddConnMonCallbackL() (RComm) complete" )));
       
   503     }
       
   504 
       
   505 // ---------------------------------------------------------------------------
       
   506 // Adds connection monitor callback for either local media or network side
       
   507 // by connection ID
       
   508 // Callbacks will be called when line status switches to high or low
       
   509 // ---------------------------------------------------------------------------
       
   510 //
       
   511 EXPORT_C void CDunTransporter::AddConnMonCallbackL( RSocket* aSocket,
       
   512                                                     MDunConnMon* aCallback,
       
   513                                                     TDunDirection aDirection,
       
   514                                                     TBool aSignal )
       
   515     {
       
   516     FTRACE(FPrint( _L("CDunTransporter::AddConnMonCallbackL() (RSocket)" )));
       
   517 
       
   518     User::LeaveIfError( CheckInitAndHandle( aSocket ) );
       
   519     TInt mediaIndex = GetMediaIndexL( aSocket );
       
   520     User::LeaveIfError( iUtility->DoAddConnMonCallback( mediaIndex,
       
   521                                                         aCallback,
       
   522                                                         aDirection,
       
   523                                                         aSignal ));
       
   524 
       
   525     FTRACE(FPrint( _L("CDunTransporter::AddConnMonCallbackL() (RSocket) complete" )));
       
   526     }
       
   527 
       
   528 // ---------------------------------------------------------------------------
       
   529 // Adds error to consider as no error condition when doing any of the four
       
   530 // endpoint's read/writer operation
       
   531 // ---------------------------------------------------------------------------
       
   532 //
       
   533 EXPORT_C void CDunTransporter::AddSkippedErrorL( TInt aError,
       
   534                                                  RComm* aComm,
       
   535                                                  TDunDirection aDirection )
       
   536     {
       
   537     FTRACE(FPrint( _L("CDunTransporter::AddSkippedErrorL() (RComm)" )));
       
   538 
       
   539     User::LeaveIfError( CheckInitAndHandle( aComm ) );
       
   540     TInt retTemp = iChanMan->SaveWaiterSkippedErrorL( aError,
       
   541                                                       aComm,
       
   542                                                       aDirection );
       
   543     if ( retTemp == KErrNone )
       
   544         {
       
   545         FTRACE(FPrint( _L("CDunTransporter::AddSkippedErrorL() (RComm) complete" )));
       
   546         return;
       
   547         }
       
   548 
       
   549     TInt mediaIndex = GetMediaIndexL( aComm );
       
   550     User::LeaveIfError( iUtility->DoAddSkippedError( mediaIndex,
       
   551                                                      aError,
       
   552                                                      aDirection ));
       
   553 
       
   554     FTRACE(FPrint( _L("CDunTransporter::AddSkippedErrorL() (RComm) complete" )));
       
   555     }
       
   556 
       
   557 // ---------------------------------------------------------------------------
       
   558 // Adds error to consider as no error condition when doing any of the four
       
   559 // endpoint's read/writer operation
       
   560 // ---------------------------------------------------------------------------
       
   561 //
       
   562 EXPORT_C void CDunTransporter::AddSkippedErrorL( TInt aError,
       
   563                                                  RSocket* aSocket,
       
   564                                                  TDunDirection aDirection )
       
   565     {
       
   566     FTRACE(FPrint( _L("CDunTransporter::AddSkippedErrorL() (RSocket)" )));
       
   567 
       
   568     User::LeaveIfError( CheckInitAndHandle( aSocket ) );
       
   569     TInt mediaIndex = GetMediaIndexL( aSocket );
       
   570     User::LeaveIfError( iUtility->DoAddSkippedError( mediaIndex,
       
   571                                                      aError,
       
   572                                                      aDirection ));
       
   573 
       
   574     FTRACE(FPrint( _L("CDunTransporter::AddSkippedErrorL() (RSocket) complete" )));
       
   575     }
       
   576 
       
   577 // ---------------------------------------------------------------------------
       
   578 // Sets service advertisement monitor callback by owner UID
       
   579 // Callbacks will be called when advertisement status changes.
       
   580 // The callbacks are updated with every successfully completed
       
   581 // channel allocation/free (and allocation failure) so it is recommended
       
   582 // to call this method after AllocateChannelL().
       
   583 // ---------------------------------------------------------------------------
       
   584 //
       
   585 EXPORT_C void CDunTransporter::SetAdvertisementMonitorL(
       
   586     TUid aOwnerUid,
       
   587     MDunServAdvMon* aCallback )
       
   588     {
       
   589     FTRACE(FPrint( _L("CDunTransporter::SetAdvertisementMonitorL()" )));
       
   590     TInt i;
       
   591     TInt count;
       
   592     if ( !aCallback )
       
   593         {
       
   594         FTRACE(FPrint( _L("CDunTransporter::SetAdvertisementMonitorL() (aCallback) not initialized!" )));
       
   595         User::Leave( KErrGeneral );
       
   596         }
       
   597     count = iServAdvData.Count();
       
   598     for ( i=0; i<count; i++ )
       
   599         {
       
   600         TDunServAdvData& servAdvData = iServAdvData[i];
       
   601         if ( servAdvData.iOwnerUid==aOwnerUid &&
       
   602              servAdvData.iServAdvMon==aCallback )
       
   603             {
       
   604             FTRACE(FPrint( _L("CDunTransporter::SetAdvertisementMonitorL() (already exist) complete" )));
       
   605             User::Leave( KErrAlreadyExists );
       
   606             }
       
   607         }
       
   608     TDunServAdvData servAdvData;
       
   609     servAdvData.iOwnerUid = aOwnerUid;
       
   610     servAdvData.iServAdvMon = aCallback;
       
   611     iServAdvData.AppendL( servAdvData );
       
   612     FTRACE(FPrint( _L("CDunTransporter::SetAdvertisementMonitorL() complete" )));
       
   613     }
       
   614 
       
   615 // ---------------------------------------------------------------------------
       
   616 // Frees service advertisement monitor callback by plugin UID
       
   617 // ---------------------------------------------------------------------------
       
   618 //
       
   619 EXPORT_C TInt CDunTransporter::FreeAdvertisementMonitor(
       
   620     TUid aOwnerUid,
       
   621     MDunServAdvMon* aCallback )
       
   622     {
       
   623     FTRACE(FPrint( _L("CDunTransporter::FreeAdvertisementMonitor()" )));
       
   624     TInt i;
       
   625     TInt count = iServAdvData.Count();
       
   626     for ( i=0; i<count; i++ )
       
   627         {
       
   628         TDunServAdvData& servAdvData = iServAdvData[i];
       
   629         if ( servAdvData.iOwnerUid==aOwnerUid &&
       
   630              servAdvData.iServAdvMon==aCallback )
       
   631             {
       
   632             iServAdvData.Remove( i );
       
   633             FTRACE(FPrint( _L("CDunTransporter::FreeAdvertisementMonitor() complete" )));
       
   634             return KErrNone;
       
   635             }
       
   636         }
       
   637     FTRACE(FPrint( _L("CDunTransporter::FreeAdvertisementMonitor() (not found) complete" )));
       
   638     return KErrNotFound;
       
   639     }
       
   640 
       
   641 // ---------------------------------------------------------------------------
       
   642 // CDunTransporter::CDunTransporter
       
   643 // ---------------------------------------------------------------------------
       
   644 //
       
   645 CDunTransporter::CDunTransporter( MDunPluginManager* aPluginManager,
       
   646                                   TInt aNumOfMaxChannels ) :
       
   647     iUtility( NULL ),
       
   648     iPluginManager( aPluginManager ),
       
   649     iActiveChannels( 0 ),
       
   650     iNumOfMaxChannels( aNumOfMaxChannels ),
       
   651     iInitialized( EFalse ),
       
   652     iAdvertise( ETrue ),
       
   653     iNetwork( NULL )
       
   654     {
       
   655     }
       
   656 
       
   657 // ---------------------------------------------------------------------------
       
   658 // CDunTransporter::ConstructL
       
   659 // ---------------------------------------------------------------------------
       
   660 //
       
   661 void CDunTransporter::ConstructL()
       
   662     {
       
   663     FTRACE(FPrint( _L("CDunTransporter::ConstructL()" )));
       
   664     if ( !iPluginManager || iNumOfMaxChannels<0 )
       
   665         {
       
   666         User::Leave( KErrGeneral );
       
   667         }
       
   668     FTRACE(FPrint( _L("CDunTransporter::ConstructL() complete" )));
       
   669     }
       
   670 
       
   671 // ---------------------------------------------------------------------------
       
   672 // Initializes the transporter, must be called as the first operation
       
   673 // ---------------------------------------------------------------------------
       
   674 //
       
   675 EXPORT_C TInt CDunTransporter::InitializeL()
       
   676     {
       
   677     FTRACE(FPrint( _L("CDunTransporter::InitializeL()" )));
       
   678 
       
   679     if ( iInitialized )
       
   680         {
       
   681         FTRACE(FPrint( _L("CDunTransporter::InitializeL() (already exists) complete" )));
       
   682         return KErrAlreadyExists;
       
   683         }
       
   684     CDunTransUtils* utility = CDunTransUtils::NewL( *this, iPluginManager );
       
   685     iUtility = static_cast<MDunTransporterUtility*>( utility );
       
   686     MDunTransporterUtilityAux* utilityAux = static_cast<MDunTransporterUtilityAux*>( utility );
       
   687     iChanMan = CDunChanMan::NewL( *this, iUtility, utilityAux, iPluginManager );
       
   688     iNetwork = CDunNetDataport::NewL( iNumOfMaxChannels );
       
   689     iNetwork->InitializeL();
       
   690     iNoteHandler = CDunNoteHandler::NewL();
       
   691     iInitialized = ETrue;
       
   692 
       
   693     FTRACE(FPrint( _L("CDunTransporter::InitializeL() complete" )));
       
   694     return KErrNone;
       
   695     }
       
   696 
       
   697 // ---------------------------------------------------------------------------
       
   698 // UnInitializes the transporter, can be called as the last operation
       
   699 // ---------------------------------------------------------------------------
       
   700 //
       
   701 EXPORT_C void CDunTransporter::UnInitialize()
       
   702     {
       
   703     FTRACE(FPrint( _L("CDunTransporter::UnInitialize()" )));
       
   704     // first stop channel waiters before deletion
       
   705     if ( iChanMan )
       
   706         {
       
   707         iChanMan->ResetData();
       
   708         }
       
   709     // now ready to remove channel data as no existing waiters
       
   710     TInt i;
       
   711     TInt count = iChannelData.Count();
       
   712     for ( i=0; i<count; i++ )
       
   713         {
       
   714         if ( iChannelData[i].iChannelInUse )
       
   715             {
       
   716             iUtility->DoFreeChannel( i );
       
   717             }
       
   718         }
       
   719     iChannelData.Close();
       
   720     iServAdvData.Close();
       
   721     DeleteTransporter();
       
   722     iInitialized = EFalse;
       
   723     FTRACE(FPrint( _L("CDunTransporter::UnInitialize() complete" )));
       
   724     }
       
   725 
       
   726 // ---------------------------------------------------------------------------
       
   727 // Initialize the transporter
       
   728 // ---------------------------------------------------------------------------
       
   729 //
       
   730 TInt CDunTransporter::InitializeOnDemand()
       
   731     {
       
   732     FTRACE(FPrint( _L("CDunTransporter::InitializeOnDemand()" ) ));
       
   733     if ( !iInitialized )
       
   734         {
       
   735         TRAPD( retTrap, InitializeL() );
       
   736         if ( retTrap != KErrNone )
       
   737             {
       
   738             FTRACE(FPrint( _L("CDunTransporter::InitializeOnDemand() initialize failed!" ) ));
       
   739             return retTrap;
       
   740             }
       
   741         }
       
   742     FTRACE(FPrint( _L("CDunTransporter::InitializeOnDemand() complete" ) ));
       
   743     return KErrNone;
       
   744     }
       
   745 
       
   746 // ---------------------------------------------------------------------------
       
   747 // UnInitialize the transporter
       
   748 // ---------------------------------------------------------------------------
       
   749 //
       
   750 TInt CDunTransporter::UnInitializeOnDemand()
       
   751     {
       
   752     FTRACE(FPrint( _L("CDunTransporter::UnInitializeOnDemand()" ) ));
       
   753     if ( !iInitialized )
       
   754         {
       
   755         FTRACE(FPrint( _L("CDunTransporter::UnInitializeOnDemand() (not ready) complete" ) ));
       
   756         return KErrNotReady;
       
   757         }
       
   758     // Check if non-free channel exists, also remove empty channel(s)
       
   759     TInt i;
       
   760     TBool allFree = ETrue;
       
   761     for ( i=iChannelData.Count()-1; i>=0; i-- )
       
   762         {
       
   763         TDunChannelData& channelData = iChannelData[i];
       
   764         if ( !channelData.iChannelInUse )
       
   765             {
       
   766             if ( !channelData.iNetwork )
       
   767                 {
       
   768                 // iChannelData must not contain data here
       
   769                 iChannelData.Remove( i );
       
   770                 }
       
   771             }
       
   772         else  // channel not free
       
   773             {
       
   774             allFree = EFalse;
       
   775             FTRACE(FPrint( _L("CDunTransporter::UnInitializeOnDemand() channel found" ) ));
       
   776             }
       
   777         }
       
   778     if ( iChanMan->NumberOfWaiters() > 0 )
       
   779         {
       
   780         allFree = EFalse;
       
   781         FTRACE(FPrint( _L("CDunTransporter::UnInitializeOnDemand() waiter found" ) ));
       
   782         }
       
   783     if ( allFree )
       
   784         {
       
   785         // All channels were free -> uninitialize
       
   786         UnInitialize();
       
   787         FTRACE(FPrint( _L("CDunTransporter::UnInitializeOnDemand() complete" ) ));
       
   788         return KErrNone;
       
   789         }
       
   790     FTRACE(FPrint( _L("CDunTransporter::UnInitializeOnDemand() (not ready) complete" ) ));
       
   791     return KErrNotReady;
       
   792     }
       
   793 
       
   794 // ---------------------------------------------------------------------------
       
   795 // Returns index of media for connection ID
       
   796 // ---------------------------------------------------------------------------
       
   797 //
       
   798 TInt CDunTransporter::GetMediaIndex( TConnId aConnId,
       
   799                                      TDunMediaContext aMediaContext )
       
   800     {
       
   801     FTRACE(FPrint( _L("CDunTransporter::GetMediaIndex()" )));
       
   802     if ( aMediaContext != EDunMediaContextNetwork &&
       
   803          aMediaContext != EDunMediaContextLocal )
       
   804         {
       
   805         FTRACE(FPrint( _L("CDunTransporter::GetMediaIndex() (not supported) complete" )));
       
   806         return KErrNotSupported;
       
   807         }
       
   808     TInt i;
       
   809     TInt count = iChannelData.Count();
       
   810     for ( i=0; i<count; i++ )
       
   811         {
       
   812         TDunChannelData& channelData = iChannelData[i];
       
   813         if ( (aMediaContext==EDunMediaContextNetwork && channelData.iNetwork==aConnId) ||
       
   814              (aMediaContext==EDunMediaContextLocal   && channelData.iComm==aConnId)    ||
       
   815              (aMediaContext==EDunMediaContextLocal   && channelData.iSocket==aConnId) )
       
   816             {
       
   817             if ( !channelData.iChannelInUse )
       
   818                 {
       
   819                 FTRACE(FPrint( _L("CDunTransporter::GetMediaIndex() (channel free!) complete" ) ));
       
   820                 return KErrGeneral;
       
   821                 }
       
   822             FTRACE(FPrint( _L("CDunTransporter::GetMediaIndex() complete (i=%d)" ), i));
       
   823             return i;
       
   824             }
       
   825         }
       
   826     FTRACE(FPrint( _L("CDunTransporter::GetMediaIndex() (not found) complete" )));
       
   827     return KErrNotFound;
       
   828     }
       
   829 
       
   830 // ---------------------------------------------------------------------------
       
   831 // Returns index of media for connection ID
       
   832 // ---------------------------------------------------------------------------
       
   833 //
       
   834 TInt CDunTransporter::GetMediaIndexL( TConnId aConnId,
       
   835                                       TDunMediaContext aMediaContext )
       
   836     {
       
   837     FTRACE(FPrint( _L("CDunTransporter::GetMediaIndexL()" )));
       
   838     TInt index = GetMediaIndex( aConnId, aMediaContext );
       
   839     if ( index < 0 )
       
   840         {
       
   841         FTRACE(FPrint( _L("CDunTransporter::GetMediaIndexL() (ERROR) complete" )));
       
   842         User::Leave( index );
       
   843         }
       
   844     FTRACE(FPrint( _L("CDunTransporter::GetMediaIndexL() complete" )));
       
   845     return index;
       
   846     }
       
   847 
       
   848 // ---------------------------------------------------------------------------
       
   849 // Checks initialization and RSubSessionBase() handle
       
   850 // ---------------------------------------------------------------------------
       
   851 //
       
   852 TInt CDunTransporter::CheckInitAndHandle( TConnId aConnId )
       
   853     {
       
   854     FTRACE(FPrint( _L("CDunTransporter::CheckInitAndHandle()" )));
       
   855     if ( !iInitialized )
       
   856         {
       
   857         FTRACE(FPrint( _L("CDunTransporter::CheckInitAndHandle() (not ready) complete" )));
       
   858         return KErrNotReady;
       
   859         }
       
   860     RSubSessionBase* subBase = static_cast<RSubSessionBase*>( aConnId );
       
   861     if ( !subBase->SubSessionHandle() )
       
   862         {
       
   863         FTRACE(FPrint( _L("CDunTransporter::CheckInitAndHandle() (bad handle) complete" )));
       
   864         return KErrBadHandle;
       
   865         }
       
   866     FTRACE(FPrint( _L("CDunTransporter::CheckInitAndHandle() complete" )));
       
   867     return KErrNone;
       
   868     }
       
   869 
       
   870 // ---------------------------------------------------------------------------
       
   871 // Deletes own internal data
       
   872 // ---------------------------------------------------------------------------
       
   873 //
       
   874 void CDunTransporter::DeleteTransporter()
       
   875     {
       
   876     FTRACE(FPrint( _L("CDunTransporter::DeleteTransporter()" )));
       
   877     // first, delete channel manager with waiters
       
   878     delete iChanMan;
       
   879     iChanMan = NULL;
       
   880     // second, delete the network object
       
   881     delete iNetwork;
       
   882     iNetwork = NULL;
       
   883     // as last step delete utility class
       
   884     CDunTransUtils* utility = static_cast<CDunTransUtils*>( iUtility );
       
   885     delete utility;
       
   886     iUtility = NULL;
       
   887     // delete note class
       
   888     delete iNoteHandler;
       
   889     iNoteHandler = NULL;
       
   890     FTRACE(FPrint( _L("CDunTransporter::DeleteTransporter() complete" )));
       
   891     }