localconnectivityservice/dun/plugins/src/bt/DunBtListen.cpp
changeset 0 c3e98f10fcf4
child 12 031b9cffe6e4
equal deleted inserted replaced
-1:000000000000 0:c3e98f10fcf4
       
     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:  DUN Bluetooth plugin's listener
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <btsdp.h>
       
    20 #include <e32std.h>
       
    21 #include <bt_sock.h>
       
    22 #include <btengdiscovery.h>
       
    23 #include "DunPlugin.h"
       
    24 #include "DunBtListen.h"
       
    25 #include "DunBtPlugin.h"
       
    26 #include "DunDebug.h"
       
    27 
       
    28 const TInt KListenQueSize   = 1;
       
    29 const TInt KDunFixedChannel = 22;  // Hack/kludge for Apple Bug ID 6527598
       
    30 
       
    31 // ======== MEMBER FUNCTIONS ========
       
    32 
       
    33 // ---------------------------------------------------------------------------
       
    34 // Two-phased constructor.
       
    35 // ---------------------------------------------------------------------------
       
    36 //
       
    37 CDunBtListen* CDunBtListen::NewL( MDunServerCallback* aServer,
       
    38                                   MDunListenCallback* aParent,
       
    39                                   CDunTransporter* aTransporter,
       
    40                                   TBtPortEntity& aEntity )
       
    41     {
       
    42     CDunBtListen* self = new (ELeave) CDunBtListen( aServer,
       
    43                                                     aParent,
       
    44                                                     aTransporter,
       
    45                                                     aEntity );
       
    46     CleanupStack::PushL( self );
       
    47     self->ConstructL();
       
    48     CleanupStack::Pop( self );
       
    49     return self;
       
    50     }
       
    51 
       
    52 // ---------------------------------------------------------------------------
       
    53 // Destructor.
       
    54 // ---------------------------------------------------------------------------
       
    55 //
       
    56 CDunBtListen::~CDunBtListen()
       
    57     {
       
    58     FTRACE(FPrint( _L("CDunBtListen::~CDunBtListen()") ));
       
    59     ResetData();
       
    60     FTRACE(FPrint( _L("CDunBtListen::~CDunBtListen() complete") ));
       
    61     }
       
    62 
       
    63 // ---------------------------------------------------------------------------
       
    64 // Resets data to initial values
       
    65 // ---------------------------------------------------------------------------
       
    66 //
       
    67 void CDunBtListen::ResetData()
       
    68     {
       
    69     // APIs affecting this:
       
    70     // IssueRequestL()
       
    71     Stop();
       
    72     StopServiceAdvertisement();
       
    73     // NewL()
       
    74     iTransporter->FreeAdvertisementMonitor( KDunBtPluginUid, this );
       
    75     delete iDiscovery;
       
    76     iDiscovery = NULL;
       
    77     if ( iSockServer.Handle() != KNullHandle )
       
    78         {
       
    79         iSockServer.Close();
       
    80         }
       
    81     // Internal
       
    82     Initialize();
       
    83     }
       
    84 
       
    85 // ---------------------------------------------------------------------------
       
    86 // Registers itself to SDP and BT manager, opens a socket
       
    87 // and starts to listen it.
       
    88 // ---------------------------------------------------------------------------
       
    89 //
       
    90 void CDunBtListen::IssueRequestL()
       
    91     {
       
    92     FTRACE(FPrint( _L( "CDunBtListen::IssueRequestL()" ) ));
       
    93 
       
    94     if ( iListenState == EBtListenStateListening )
       
    95         {
       
    96         FTRACE(FPrint( _L( "CDunBtListen::IssueRequestL() (already active) complete" ) ));
       
    97         User::Leave( KErrNotReady );
       
    98         }
       
    99 
       
   100     TBool advertise = iTransporter->AdvertisementStatus();
       
   101     if ( !advertise )
       
   102         {
       
   103         // Return silently here as CDunTransporter will notify later
       
   104         return;
       
   105         }
       
   106 
       
   107     TBool inUse = EFalse;
       
   108     TInt numOfChans = 0;
       
   109     TInt retTemp = StartServiceAdvertisement( inUse );
       
   110     if ( retTemp != KErrNone )
       
   111         {
       
   112         if ( inUse )
       
   113             {
       
   114             numOfChans = iTransporter->GetNumberOfAllocatedChannelsByUid(
       
   115                 KDunBtPluginUid );
       
   116             if ( numOfChans == 0)
       
   117                 {
       
   118                 // No channels so parent can't reissue requests of this object
       
   119                 // This is fatal case -> leave.
       
   120                 // NOTE: To add full support for this case a poller (timer) is
       
   121                 // needed that polls for free RFCOMM channel by given interval.
       
   122                 User::Leave( retTemp );
       
   123                 }
       
   124             // If in use and parent has channels then just fail silently.
       
   125             // Let this object to wait until parent finds new resources.
       
   126             FTRACE(FPrint( _L( "CDunBtListen::IssueRequestL() complete" ) ));
       
   127             return;
       
   128             }
       
   129         FTRACE(FPrint( _L( "CDunBtListen::IssueRequestL() (failed!) complete" ) ));
       
   130         User::Leave( retTemp );
       
   131         }
       
   132 
       
   133     // Not already active here so start listening
       
   134     // First open blank data socket
       
   135     retTemp = iEntity.iBTPort.Open( iSockServer );
       
   136     if ( retTemp != KErrNone )
       
   137         {
       
   138         FTRACE(FPrint( _L( "CDunBtListen::IssueRequestL() (ERROR) complete (%d)" ), retTemp));
       
   139         User::Leave( retTemp );
       
   140         }
       
   141     iStatus = KRequestPending;
       
   142     iListenSocket.Accept( iEntity.iBTPort, iStatus );
       
   143     SetActive();
       
   144     iListenState = EBtListenStateListening;
       
   145 
       
   146     FTRACE(FPrint( _L( "CDunBtListen::IssueRequestL() complete") ));
       
   147     }
       
   148 
       
   149 // ---------------------------------------------------------------------------
       
   150 // Stops listening
       
   151 // ---------------------------------------------------------------------------
       
   152 //
       
   153 TInt CDunBtListen::Stop()
       
   154     {
       
   155     FTRACE(FPrint( _L( "CDunBtListen::Stop()") ));
       
   156     if ( iListenState != EBtListenStateListening )
       
   157         {
       
   158         FTRACE(FPrint( _L("CDunBtListen::Stop() (not ready) complete" )));
       
   159         return KErrNotReady;
       
   160         }
       
   161     iListenSocket.CancelAccept();
       
   162     Cancel();
       
   163     iListenState = EBtListenStateIdle;
       
   164     FTRACE(FPrint( _L( "CDunBtListen::Stop() complete") ));
       
   165     return KErrNone;
       
   166     }
       
   167 
       
   168 // ---------------------------------------------------------------------------
       
   169 // CDunBtListen::CDunBtListen
       
   170 // ---------------------------------------------------------------------------
       
   171 //
       
   172 CDunBtListen::CDunBtListen( MDunServerCallback* aServer,
       
   173                             MDunListenCallback* aParent,
       
   174                             CDunTransporter* aTransporter,
       
   175                             TBtPortEntity& aEntity ) :
       
   176     CActive( EPriorityStandard ),
       
   177     iServer( aServer ),
       
   178     iParent( aParent ),
       
   179     iTransporter( aTransporter ),
       
   180     iEntity( aEntity )
       
   181     {
       
   182     Initialize();
       
   183     }
       
   184 
       
   185 // ---------------------------------------------------------------------------
       
   186 // CDunBtListen::ConstructL
       
   187 // ---------------------------------------------------------------------------
       
   188 //
       
   189 void CDunBtListen::ConstructL()
       
   190     {
       
   191     FTRACE(FPrint(_L("CDunBtListen::ConstructL()")));
       
   192     if ( !iServer || !iParent || !iTransporter )
       
   193         {
       
   194         User::Leave( KErrGeneral );
       
   195         }
       
   196 
       
   197     CBTEngDiscovery* discovery = CBTEngDiscovery::NewLC();
       
   198     FTRACE(FPrint(_L("CDunBtListen::ConstructL: iSockServer.Connect")));
       
   199     User::LeaveIfError( iSockServer.Connect() );
       
   200 
       
   201     // Set advertisement monitor
       
   202     iTransporter->SetAdvertisementMonitorL( KDunBtPluginUid, this );
       
   203 
       
   204     // Then we are ready to start listening and accepting incoming connection
       
   205     // requests.
       
   206     CleanupStack::Pop( discovery );
       
   207     iDiscovery = discovery;
       
   208     CActiveScheduler::Add( this );
       
   209     FTRACE(FPrint(_L("CDunBtListen::ConstructL() complete")));
       
   210     }
       
   211 
       
   212 // ---------------------------------------------------------------------------
       
   213 // Initializes this class
       
   214 // ---------------------------------------------------------------------------
       
   215 //
       
   216 void CDunBtListen::Initialize()
       
   217     {
       
   218     // Don't initialize iServer here (it is set through NewL)
       
   219     // Don't initialize iParent here (it is set through NewL)
       
   220     // Don't initialize iTransporter here (it is set through NewL)
       
   221     // Don't initialize iEntity here (it is set through NewL)
       
   222     iListenState = EBtListenStateIdle;
       
   223     iDiscovery = NULL;
       
   224     iChannelNum = 0;
       
   225     iSDPHandleDun = 0;
       
   226     }
       
   227 
       
   228 // ---------------------------------------------------------------------------
       
   229 // Starts dialup service advertisement
       
   230 // ---------------------------------------------------------------------------
       
   231 //
       
   232 TInt CDunBtListen::StartServiceAdvertisement( TBool& aInUse )
       
   233     {
       
   234     FTRACE(FPrint( _L( "CDunBtListen::StartServiceAdvertisement()" ) ));
       
   235 
       
   236     TInt retTemp = ReserveLocalChannel( iSockServer,
       
   237                                         iListenSocket,
       
   238                                         iChannelNum,
       
   239                                         aInUse );
       
   240     if ( retTemp != KErrNone )
       
   241         {
       
   242         FTRACE(FPrint( _L( "CDunBtListen::StartServiceAdvertisement() (ERROR) complete" ) ));
       
   243         return retTemp;
       
   244         }
       
   245 
       
   246     // Now RFCOMM channel number of the next data socket must be the same as
       
   247     // the current listener's RFCOMM channel number. Set that now.
       
   248     iEntity.iChannelNum = iChannelNum;
       
   249 
       
   250     // Register SDP record
       
   251     iSDPHandleDun = 0;
       
   252     retTemp = iDiscovery->RegisterSdpRecord( KDialUpNetworkingUUID,
       
   253                                              iChannelNum,
       
   254                                              iSDPHandleDun );
       
   255     if ( retTemp != KErrNone )
       
   256         {
       
   257         FTRACE(FPrint( _L( "CDunBtListen::StartServiceAdvertisement() (failed!) complete (%d)" ), retTemp));
       
   258         return retTemp;
       
   259         }
       
   260     FTRACE(FPrint( _L( "CDunBtListen::StartServiceAdvertisement() complete" ) ));
       
   261     return KErrNone;
       
   262     }
       
   263 
       
   264 // ---------------------------------------------------------------------------
       
   265 // Stops dialup service advertisement
       
   266 // ---------------------------------------------------------------------------
       
   267 //
       
   268 TInt CDunBtListen::StopServiceAdvertisement()
       
   269     {
       
   270     FTRACE(FPrint( _L( "CDunBtListen::StopServiceAdvertisement()" ) ));
       
   271     if ( !iDiscovery )
       
   272         {
       
   273         FTRACE(FPrint( _L( "CDunBtListen::StopServiceAdvertisement() (iDiscovery) not initialized!" ) ));
       
   274         return KErrGeneral;
       
   275         }
       
   276     if ( iSDPHandleDun != 0 )
       
   277         {
       
   278         TInt retTemp = iDiscovery->DeleteSdpRecord( iSDPHandleDun );
       
   279         FTRACE(FPrint( _L( "CDunBtListen::StopServiceAdvertisement() record closed (%d)" ), retTemp ));
       
   280         iSDPHandleDun = 0;
       
   281         }
       
   282     if ( iListenSocket.SubSessionHandle() )
       
   283         {
       
   284         iListenSocket.Close();
       
   285         }
       
   286     FTRACE(FPrint( _L( "CDunBtListen::StopServiceAdvertisement() complete" ) ));
       
   287     return KErrNone;
       
   288     }
       
   289 
       
   290 // ---------------------------------------------------------------------------
       
   291 // Method which reserves local RFCOMM channel (from possible channels 1-30)
       
   292 // and returns it to client.
       
   293 // ---------------------------------------------------------------------------
       
   294 //
       
   295 TInt CDunBtListen::ReserveLocalChannel( RSocketServ& aSocketServ,
       
   296                                         RSocket& aListenSocket,
       
   297                                         TUint& aChannelNum,
       
   298                                         TBool& aInUse )
       
   299     {
       
   300     FTRACE(FPrint(_L("CDunBtListen::ReserveLocalChannel()")));
       
   301     aInUse = EFalse;
       
   302     if ( aListenSocket.SubSessionHandle() )
       
   303         {
       
   304         FTRACE(FPrint(_L("CDunBtListen::ReserveLocalChannel() (open socket!) complete")));
       
   305         return KErrArgument;
       
   306         }
       
   307     TInt retTemp;
       
   308     TProtocolDesc pInfo;
       
   309     retTemp = aSocketServ.FindProtocol( TProtocolName(KRFCOMMDesC), pInfo );
       
   310     if ( retTemp != KErrNone )
       
   311         {
       
   312         FTRACE(FPrint(_L("CDunBtListen::ReserveLocalChannel() (FindProtocol failed) complete (%d)"), retTemp));
       
   313         return retTemp;
       
   314         }
       
   315     retTemp = aListenSocket.Open( aSocketServ,
       
   316                                   pInfo.iAddrFamily,
       
   317                                   pInfo.iSockType,
       
   318                                   pInfo.iProtocol );
       
   319     if ( retTemp != KErrNone )
       
   320         {
       
   321         FTRACE(FPrint(_L("CDunBtListen::ReserveLocalChannel() (Open failed) complete (%d)"), retTemp));
       
   322         return retTemp;
       
   323         }
       
   324     TRfcommSockAddr addr;
       
   325     TBTServiceSecurity sec;
       
   326     sec.SetAuthentication( ETrue );
       
   327     sec.SetAuthorisation( ETrue );
       
   328     sec.SetEncryption( ETrue );
       
   329     sec.SetPasskeyMinLength( 0 );
       
   330     addr.SetSecurity( sec );
       
   331     addr.SetPort( KRfcommPassiveAutoBind );
       
   332     // When fix from Apple, replace the following with
       
   333     // "retTemp = aListenSocket.Bind( addr );"
       
   334     retTemp = DoExtendedBind( aListenSocket, addr );
       
   335     if ( retTemp != KErrNone )
       
   336         {
       
   337         aListenSocket.Close();
       
   338         aInUse = ETrue;
       
   339         FTRACE(FPrint(_L("CDunBtListen::ReserveLocalChannel() Bind() complete (%d)"), retTemp));
       
   340         return KErrInUse;
       
   341         }
       
   342     aChannelNum = aListenSocket.LocalPort();
       
   343     retTemp = aListenSocket.Listen( KListenQueSize );
       
   344     if ( retTemp != KErrNone )
       
   345         {
       
   346         aListenSocket.Close();
       
   347         FTRACE(FPrint(_L("CDunBtListen::ReserveLocalChannel() Listen() complete (%d)"), retTemp));
       
   348         return retTemp;
       
   349         }
       
   350     FTRACE(FPrint(_L("CDunBtListen::ReserveLocalChannel() complete (%d)"), aChannelNum));
       
   351     return KErrNone;
       
   352     }
       
   353 
       
   354 // ---------------------------------------------------------------------------
       
   355 // Tries to bind to a fixed port and if that fails with KRfcommPassiveAutoBind.
       
   356 // This is for spec breaking solutions like the OSX Leopard.
       
   357 // ---------------------------------------------------------------------------
       
   358 //
       
   359 TInt CDunBtListen::DoExtendedBind( RSocket& aListenSocket,
       
   360                                    TRfcommSockAddr& aSockAddr )
       
   361     {
       
   362     FTRACE(FPrint(_L("CDunBtListen::DoExtendedBind()")));
       
   363     if ( !aListenSocket.SubSessionHandle() )
       
   364         {
       
   365         FTRACE(FPrint(_L("CDunBtListen::DoExtendedBind() (closed socket!) complete")));
       
   366         return KErrGeneral;
       
   367         }
       
   368     TRfcommSockAddr fixedAddr = aSockAddr;
       
   369     fixedAddr.SetPort( KDunFixedChannel );
       
   370     TInt retTemp = aListenSocket.Bind( fixedAddr );
       
   371     if ( retTemp == KErrNone )
       
   372         {
       
   373         FTRACE(FPrint(_L("CDunBtListen::DoExtendedBind() complete")));
       
   374         return KErrNone;
       
   375         }
       
   376     TInt retVal = aListenSocket.Bind( aSockAddr );
       
   377     FTRACE(FPrint(_L("CDunBtListen::DoExtendedBind() complete")));
       
   378     return retVal;
       
   379     }
       
   380 
       
   381 // ---------------------------------------------------------------------------
       
   382 // From class CActive.
       
   383 // Called when a service is requested via BT.
       
   384 // ---------------------------------------------------------------------------
       
   385 //
       
   386 void CDunBtListen::RunL()
       
   387     {
       
   388     FTRACE(FPrint( _L( "CDunBtListen::RunL()" ) ));
       
   389     iListenState = EBtListenStateIdle;
       
   390 
       
   391     StopServiceAdvertisement();
       
   392 
       
   393     TInt retTemp = iStatus.Int();
       
   394     if ( retTemp != KErrNone )
       
   395         {
       
   396         FTRACE(FPrint( _L( "CDunBtListen::RunL() (ERROR) complete (%d)" ), retTemp));
       
   397         iServer->NotifyPluginCloseRequest( KDunBtPluginUid, ETrue );
       
   398         return;
       
   399         }
       
   400     // Notify new connection
       
   401     TBool noFreeChans = EFalse;
       
   402     retTemp = iParent->NotifyChannelAllocate( noFreeChans );
       
   403     if ( retTemp != KErrNone )
       
   404         {
       
   405         FTRACE(FPrint( _L( "CDunBtListen::RunL() channel allocation failed! (%d)" ), retTemp));
       
   406         // Other error than no free channels, close plugin now
       
   407         if ( !noFreeChans )
       
   408             {
       
   409             iServer->NotifyPluginCloseRequest( KDunBtPluginUid, ETrue );
       
   410             }
       
   411         return;
       
   412         }
       
   413 
       
   414     // Don't restart listening here. Request is issued via
       
   415     // NotifyAdvertisementStart()
       
   416 
       
   417     FTRACE(FPrint( _L( "CDunBtListen::RunL() complete" ) ));
       
   418     }
       
   419 
       
   420 // ---------------------------------------------------------------------------
       
   421 // From class CActive.
       
   422 // Cancel current activity.
       
   423 // ---------------------------------------------------------------------------
       
   424 //
       
   425 void CDunBtListen::DoCancel()
       
   426     {
       
   427     }
       
   428 
       
   429 // ---------------------------------------------------------------------------
       
   430 // From class MDunServAdvMon.
       
   431 // Gets called when advertisement status changes to start.
       
   432 // ---------------------------------------------------------------------------
       
   433 //
       
   434 void CDunBtListen::NotifyAdvertisementStart( TBool aCreation )
       
   435     {
       
   436     FTRACE(FPrint( _L( "CDunBtListen::NotifyAdvertisementStart()" ) ));
       
   437     // Remove the "if" below when fix comes from Apple
       
   438     if ( !aCreation )
       
   439         {
       
   440         TRAP_IGNORE( IssueRequestL() );
       
   441         }
       
   442     FTRACE(FPrint( _L( "CDunBtListen::NotifyAdvertisementStart() complete" ) ));
       
   443     }
       
   444 
       
   445 // ---------------------------------------------------------------------------
       
   446 // From class MDunServAdvMon.
       
   447 // Gets called when advertisement status changes to end.
       
   448 // ---------------------------------------------------------------------------
       
   449 //
       
   450 void CDunBtListen::NotifyAdvertisementEnd()
       
   451     {
       
   452     FTRACE(FPrint( _L( "CDunBtListen::NotifyAdvertisementEnd()" ) ));
       
   453     Stop();
       
   454     StopServiceAdvertisement();
       
   455     FTRACE(FPrint( _L( "CDunBtListen::NotifyAdvertisementEnd() complete" ) ));
       
   456     }