bluetoothengine/btsap/src/BTSapPlugin.cpp
changeset 0 f63038272f30
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2004-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: 
       
    15 *     BT SAP Plugin header definition
       
    16 *
       
    17 *
       
    18 */
       
    19 
       
    20 
       
    21 // INCLUDE FILES
       
    22 #include <ecom/ecom.h>
       
    23 #include <ecom/implementationproxy.h>
       
    24 #include <btmanclient.h>
       
    25 #include <bt_sock.h>
       
    26 #include "BTSapPlugin.h"
       
    27 #include "BTSapServerState.h"
       
    28 #include "debug.h"
       
    29 
       
    30 // ================= MEMBER FUNCTIONS =======================
       
    31 
       
    32 // ---------------------------------------------------------
       
    33 // CBTSapStatusObserver::CBTSapStatusObserver()
       
    34 //----------------------------------------------------------
       
    35 //
       
    36 CBTSapAsyncHelper::CBTSapAsyncHelper(): CActive(CActive::EPriorityStandard)
       
    37     {                                
       
    38     CActiveScheduler::Add( this );
       
    39     }
       
    40 
       
    41 // ---------------------------------------------------------
       
    42 // CBTSapStatusObserver::NewL()
       
    43 //----------------------------------------------------------
       
    44 // 
       
    45 CBTSapAsyncHelper* CBTSapAsyncHelper::NewL(CBTSapServerState* aServerState, MSapAsyncObserver* aObserver) 
       
    46     {
       
    47     CBTSapAsyncHelper* self = new(ELeave) CBTSapAsyncHelper();
       
    48     CleanupStack::PushL( self );
       
    49     self->ConstructL(aServerState, aObserver);
       
    50     CleanupStack::Pop();
       
    51     return self;
       
    52     }
       
    53  
       
    54 // ---------------------------------------------------------
       
    55 // CBTSapAsyncHelper::ConstructL()
       
    56 //----------------------------------------------------------
       
    57 //   
       
    58 void CBTSapAsyncHelper::ConstructL(CBTSapServerState* aServerState, MSapAsyncObserver* aObserver)
       
    59     {
       
    60     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapAsyncHelper::ConstructL")));
       
    61     
       
    62     iServerState = aServerState;
       
    63     iObserver = aObserver;
       
    64     iOperation = MSapAsyncObserver::EConnect;
       
    65     iDiscType = EDisconnectImmediate;
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------
       
    69 // CBTSapAsyncHelper::~CBTSapAsyncHelper
       
    70 //----------------------------------------------------------
       
    71 //
       
    72 CBTSapAsyncHelper::~CBTSapAsyncHelper()
       
    73     {
       
    74     Cancel();
       
    75     }
       
    76     
       
    77 // ---------------------------------------------------------
       
    78 // CBTSapAsyncHelper::AsyncConnect()
       
    79 //----------------------------------------------------------
       
    80 //
       
    81 /*
       
    82 void CBTSapAsyncHelper::AsyncConnect(const TBTDevAddr& aAddr)
       
    83     {
       
    84     // Not supported at the moment
       
    85     }
       
    86 */
       
    87   
       
    88 // ---------------------------------------------------------
       
    89 // CBTSapAsyncHelper::AsyncCancelConnect()
       
    90 //----------------------------------------------------------
       
    91 //
       
    92 /*
       
    93 void CBTSapAsyncHelper::AsyncCancelConnect(const TBTDevAddr& aAddr)
       
    94     {
       
    95     // Not supported at the moment
       
    96     }
       
    97 */
       
    98 
       
    99 // ---------------------------------------------------------
       
   100 // CBTSapAsyncHelper::AsyncDissconnect()
       
   101 //----------------------------------------------------------
       
   102 //    
       
   103 void CBTSapAsyncHelper::AsyncDisconnect(const TBTDevAddr& /*aAddr*/, TBTSapDisconnectType aDiscType)
       
   104     {
       
   105     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapAsyncHelper::AsyncDisconnect")));
       
   106     
       
   107     if (IsActive())
       
   108         {
       
   109         return;
       
   110         }
       
   111     iOperation = MSapAsyncObserver::EDisconnect;
       
   112     iDiscType = aDiscType;
       
   113     TRequestStatus* ownStatus;
       
   114     ownStatus = &iStatus;
       
   115     *ownStatus = KRequestPending;
       
   116     SetActive();
       
   117     User::RequestComplete(ownStatus, KErrNone);;
       
   118     }
       
   119     
       
   120 
       
   121 // ---------------------------------------------------------
       
   122 // CBTSapAsyncHelper::DoCancel()
       
   123 // ---------------------------------------------------------
       
   124 //
       
   125 void CBTSapAsyncHelper::DoCancel()
       
   126     {
       
   127     }
       
   128 
       
   129 // ---------------------------------------------------------
       
   130 // CBTSapAsyncHelper::RunL()
       
   131 // ---------------------------------------------------------
       
   132 //
       
   133 void CBTSapAsyncHelper::RunL()
       
   134     {
       
   135     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapAsyncHelper::RunL")));
       
   136     
       
   137     TInt r = KErrNone;
       
   138     
       
   139     switch(iOperation)
       
   140         {
       
   141         case MSapAsyncObserver::EDisconnect:
       
   142             {
       
   143             r = iServerState->DisconnectSapConnection(iDiscType);
       
   144             return; // Disconnect callback will happen after a socket is closed, not yet here
       
   145             }
       
   146         default:
       
   147             {
       
   148             // Nothing
       
   149             }
       
   150         }
       
   151     iObserver->OperationCompletedL(iOperation, r);
       
   152     }
       
   153 
       
   154 // ---------------------------------------------------------
       
   155 // CBTSapPlugin::CBTSapPlugin()
       
   156 // ---------------------------------------------------------
       
   157 //
       
   158 CBTSapPlugin::CBTSapPlugin()
       
   159     : iServiceState(EServiceOff)
       
   160     {
       
   161     }
       
   162 
       
   163 // ---------------------------------------------------------
       
   164 // CBTSapPlugin::~CBTSapPlugin()
       
   165 // ---------------------------------------------------------
       
   166 //
       
   167 CBTSapPlugin::~CBTSapPlugin()
       
   168     {
       
   169     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  ~CBTSapPlugin")));
       
   170     
       
   171     if(iBTSapServerState)
       
   172         {
       
   173         iBTSapServerState->DisconnectSapConnection(EDisconnectImmediate);
       
   174         }
       
   175 
       
   176     if(iAsyncHelper)
       
   177         {
       
   178         iAsyncHelper->Cancel();
       
   179         delete iAsyncHelper;
       
   180         }
       
   181     
       
   182     delete iBTSapServerState;
       
   183     iBTSapServerState = NULL;
       
   184     }
       
   185 
       
   186 // ---------------------------------------------------------
       
   187 // CBTSapPlugin::NewL
       
   188 // ---------------------------------------------------------
       
   189 //
       
   190 CBTSapPlugin* CBTSapPlugin::NewL()
       
   191     {
       
   192     CBTSapPlugin* self=new(ELeave) CBTSapPlugin();
       
   193     CleanupStack::PushL(self);
       
   194     self->ConstructL();
       
   195     CleanupStack::Pop();
       
   196     return self;
       
   197     }
       
   198 
       
   199 // ---------------------------------------------------------
       
   200 // CBTSapPlugin::ConstructL
       
   201 // ---------------------------------------------------------
       
   202 //
       
   203 void CBTSapPlugin::ConstructL()
       
   204     {
       
   205     BTSAP_TRACE_OPT(KBTSAP_TRACE_FUNCTIONS, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::ConstructL")));
       
   206     iBTSapServerState = CBTSapServerState::NewL(*this);
       
   207     iAsyncHelper = CBTSapAsyncHelper::NewL(iBTSapServerState, this);
       
   208     StartBTSapServiceL(); // Creates iBTSapServerState
       
   209     }
       
   210 
       
   211 // ---------------------------------------------------------
       
   212 // CBTSapPlugin::SetObserver, from CBTEngPlugin
       
   213 // ---------------------------------------------------------
       
   214 //
       
   215 void CBTSapPlugin::SetObserver( MBTEngPluginObserver* aObserver )
       
   216     {
       
   217     iObserver = aObserver;
       
   218     }
       
   219 
       
   220 // ---------------------------------------------------------
       
   221 // CBTSapPlugin::GetSupportedProfiles, from CBTEngPlugin
       
   222 // ---------------------------------------------------------
       
   223 //
       
   224 void CBTSapPlugin::GetSupportedProfiles( RProfileArray& aProfiles )
       
   225     {
       
   226     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::GetSupportedProfiles")));
       
   227     
       
   228     aProfiles.Append(EBTProfileSAP);
       
   229     }
       
   230 
       
   231 // ---------------------------------------------------------
       
   232 // CBTSapPlugin::IsProfileSupported, from CBTEngPlugin
       
   233 // ---------------------------------------------------------
       
   234 //
       
   235 TBool CBTSapPlugin::IsProfileSupported( const TBTProfile aProfile ) const
       
   236     {
       
   237     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::IsProfileSupported")));
       
   238     
       
   239     return (aProfile == EBTProfileSAP) ? ETrue : EFalse;
       
   240     }
       
   241 
       
   242 // ---------------------------------------------------------
       
   243 // CBTSapPlugin::Connect, from CBTEngPlugin
       
   244 // ---------------------------------------------------------
       
   245 //
       
   246 TInt CBTSapPlugin::Connect( const TBTDevAddr& /*aAddr*/ )
       
   247     {
       
   248     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::Connect")));
       
   249     
       
   250     // A SAP connection can only be created by a remote device
       
   251     return KErrNotSupported;
       
   252     }
       
   253 
       
   254 // ---------------------------------------------------------
       
   255 // CBTSapPlugin::CancelConnect, from CBTEngPlugin
       
   256 // ---------------------------------------------------------
       
   257 //
       
   258 void CBTSapPlugin::CancelConnect( const TBTDevAddr& /*aAddr*/ )
       
   259     {
       
   260     // A SAP connection can only be created by a remote device
       
   261     }
       
   262 
       
   263 // ---------------------------------------------------------
       
   264 // CBTSapPlugin::Disconnect, from CBTEngPlugin
       
   265 // ---------------------------------------------------------
       
   266 //
       
   267 TInt CBTSapPlugin::Disconnect( const TBTDevAddr& aAddr, TBTDisconnectType aDiscType )
       
   268     {
       
   269     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::Disconnect")));
       
   270     
       
   271     TInt r = KErrNotSupported;
       
   272     TBTDevAddr btDevAddr;
       
   273     iAddr = aAddr;
       
   274 
       
   275     r = GetRemoteBTAddress(btDevAddr);
       
   276 
       
   277     if (!r)
       
   278         {
       
   279         if (btDevAddr != aAddr)
       
   280             {
       
   281             return KErrNone;
       
   282             }
       
   283         }
       
   284     else
       
   285         {
       
   286         return r;
       
   287         }
       
   288     
       
   289     if (aDiscType == EBTDiscImmediate)
       
   290         {
       
   291         r = DisconnectSapConnection(EDisconnectImmediate);
       
   292         }
       
   293     else if (aDiscType == EBTDiscGraceful)
       
   294         {
       
   295         r = DisconnectSapConnection(EDisconnectGraceful);
       
   296         }
       
   297     return r;
       
   298     }
       
   299 
       
   300 // ---------------------------------------------------------
       
   301 // CBTSapPlugin::GetConnections, from CBTEngPlugin
       
   302 // ---------------------------------------------------------
       
   303 //    
       
   304 void CBTSapPlugin::GetConnections( RBTDevAddrArray& aAddrArray, TBTProfile aConnectedProfile )
       
   305     {
       
   306     TBTDevAddr btDevAddr;
       
   307     
       
   308     if(aConnectedProfile == EBTProfileSAP)
       
   309         {
       
   310         if (GetRemoteBTAddress(btDevAddr) == KErrNone)
       
   311             {
       
   312             aAddrArray.Append(btDevAddr);
       
   313             }
       
   314         }
       
   315     }
       
   316     
       
   317 // ---------------------------------------------------------
       
   318 // CBTSapPlugin::IsConnected, from CBTEngPlugin
       
   319 // ---------------------------------------------------------
       
   320 //
       
   321 TBTEngConnectionStatus CBTSapPlugin::IsConnected( const TBTDevAddr& aAddr )
       
   322     {
       
   323     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::IsConnected")));
       
   324     
       
   325     TBTDevAddr addr;
       
   326     TInt r = KErrNotSupported;
       
   327     
       
   328     if(iServiceState)
       
   329         {
       
   330         // Only check connection state if the service is enabled
       
   331         if (iBTSapServerState->IsSapConnected())
       
   332             {
       
   333             r = GetRemoteBTAddress( addr );
       
   334             if( !r && addr == aAddr )
       
   335                 {
       
   336                 return EBTEngConnected;
       
   337                 }
       
   338             }
       
   339         }
       
   340         
       
   341     // Otherwise there is no connection, for sure
       
   342     return EBTEngNotConnected;
       
   343     }
       
   344 
       
   345 
       
   346 // ---------------------------------------------------------
       
   347 // CBTSapPlugin::StartBTSapServiceL
       
   348 // ---------------------------------------------------------
       
   349 //
       
   350 void CBTSapPlugin::StartBTSapServiceL()
       
   351     {
       
   352     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::StartBTSapServiceL")));
       
   353     
       
   354     if(iServiceState == EServiceOff)
       
   355         {
       
   356         iBTSapServerState->StartL();
       
   357         iServiceState = EServiceOn;
       
   358         }
       
   359     }
       
   360 
       
   361 // ---------------------------------------------------------
       
   362 // CBTSapPlugin::AcceptSapConnection
       
   363 // ---------------------------------------------------------
       
   364 //
       
   365 TInt CBTSapPlugin::AcceptSapConnection()
       
   366     {
       
   367     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::AcceptSapConnection")));
       
   368     
       
   369     if(iServiceState)
       
   370         {
       
   371         // Only accept connection if the service is enabled
       
   372         return iBTSapServerState->AcceptSapConnection();
       
   373         }
       
   374     else
       
   375         {
       
   376         // Otherwise the state-machine is not ready yet
       
   377         BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR,
       
   378                         BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::AcceptSapConnection: Can't accept when service is disabled")));
       
   379         return KErrNotReady;
       
   380         }
       
   381     }
       
   382 
       
   383 // ---------------------------------------------------------
       
   384 // CBTSapPlugin::RejectSapConnection
       
   385 // ---------------------------------------------------------
       
   386 //
       
   387 TInt CBTSapPlugin::RejectSapConnection(TBTSapRejectReason aReason)
       
   388     {
       
   389     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::RejectSapConnection")));
       
   390     
       
   391     if(iServiceState)
       
   392         {
       
   393         // Only reject connection if the service is enabled
       
   394         return iBTSapServerState->RejectSapConnection(aReason);
       
   395         }
       
   396     else
       
   397         {
       
   398         // Otherwise the state-machine is not ready yet
       
   399         BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR,
       
   400                         BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::RejectSapConnection: Can't reject when service is disabled")));
       
   401         return KErrNotReady;
       
   402         }
       
   403     }
       
   404 
       
   405 // ---------------------------------------------------------
       
   406 // CBTSapPlugin::DisconnectSapConnection
       
   407 // ---------------------------------------------------------
       
   408 //
       
   409 TInt CBTSapPlugin::DisconnectSapConnection(TBTSapDisconnectType aType)
       
   410     {
       
   411     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::DisconnectSapConnection")));
       
   412     
       
   413     if(iServiceState)
       
   414         {
       
   415         TBTDevAddr addr;
       
   416         // Only disconnect SAP if the service is enabled
       
   417         iAsyncHelper->AsyncDisconnect(addr, aType);
       
   418         return KErrNone;
       
   419         }
       
   420     else
       
   421         {
       
   422         BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR,
       
   423                         BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::DisconnectSapConnection: Can't disconnect when service is disabled")));
       
   424         // Otherwise the state-machine is not ready yet
       
   425         return KErrNotReady;
       
   426         }
       
   427     }
       
   428 
       
   429 // ---------------------------------------------------------
       
   430 // CBTSapPlugin::IsSapConnected
       
   431 // ---------------------------------------------------------
       
   432 //
       
   433 TBool CBTSapPlugin::IsSapConnected()
       
   434     {
       
   435     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::IsSapConnected")));
       
   436     
       
   437     if(iServiceState)
       
   438         {
       
   439         // Only check connection state if the service is enabled
       
   440         return iBTSapServerState->IsSapConnected();
       
   441         }
       
   442         
       
   443     // Otherwise there is no connection, for sure
       
   444     return EFalse;
       
   445     }
       
   446 
       
   447 // ---------------------------------------------------------
       
   448 // CBTSapPlugin::GetRemoteBTAddress
       
   449 // ---------------------------------------------------------
       
   450 //
       
   451 TInt CBTSapPlugin::GetRemoteBTAddress(TBTDevAddr& aBTDevAddr)
       
   452     {
       
   453     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::GetRemoteBTAddress")));
       
   454     
       
   455     if(iServiceState)
       
   456         {
       
   457         // Only get remote address if the service is enabled
       
   458         return iBTSapServerState->GetRemoteBTAddress(aBTDevAddr);
       
   459         }
       
   460     else
       
   461         {
       
   462         BTSAP_TRACE_OPT(KBTSAP_TRACE_ERROR,
       
   463                         BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::GetRemoteBTAddress: Can't get remote address when service is disabled")));
       
   464         // Otherwise the state-machine is not ready yet
       
   465         return KErrNotReady;
       
   466         }
       
   467     }
       
   468 
       
   469 // ---------------------------------------------------------
       
   470 // CBTSapPlugin::OperationCompletedL
       
   471 // ---------------------------------------------------------
       
   472 //    
       
   473 void CBTSapPlugin::OperationCompletedL(MSapAsyncObserver::TOperation aOperation, TInt aError)
       
   474     {
       
   475     BTSAP_TRACE_OPT(KBTSAP_TRACE_STM, BTSapPrintTrace(_L("[BTSap]  CBTSapPlugin::OperationCompletedL")));
       
   476     
       
   477     TBTProfile profile = EBTProfileSAP;
       
   478     
       
   479     switch (aOperation)
       
   480         {
       
   481         case MSapAsyncObserver::EDisconnect:
       
   482             {
       
   483             iObserver->DisconnectComplete(iAddr, profile, aError);
       
   484             }
       
   485         }
       
   486     }
       
   487     
       
   488 // ---------------------------------------------------------
       
   489 // CBTSapPlugin::ConnectComplete
       
   490 // ---------------------------------------------------------
       
   491 //    
       
   492 void CBTSapPlugin::ConnectComplete()
       
   493     {
       
   494     // Used just for incoming SAP connectios, outgoing SAP connections are not supported.
       
   495     // And used only when connection is created successully.
       
   496     if ( iObserver )
       
   497         {
       
   498         TBTDevAddr addr;
       
   499         if ( !GetRemoteBTAddress( addr ) )
       
   500             {
       
   501             iObserver->ConnectComplete( addr, EBTProfileSAP, KErrNone );
       
   502             }
       
   503         }
       
   504     }
       
   505     
       
   506 // -----------------------------------------------------------------------------
       
   507 // Implementation table is required by ECom. Allows alternative
       
   508 // New methods to be specified.
       
   509 // -----------------------------------------------------------------------------
       
   510 //
       
   511 const TImplementationProxy ImplementationTable[] =
       
   512 	{
       
   513     IMPLEMENTATION_PROXY_ENTRY(0x101FFE46,CBTSapPlugin::NewL)
       
   514 	}; 
       
   515     
       
   516 // -----------------------------------------------------------------------------
       
   517 // ImplementationGroupProxy: Lookup method required by ECom
       
   518 // Returns the ImplementationTable to the  ECom framework
       
   519 // -----------------------------------------------------------------------------
       
   520 //
       
   521 EXPORT_C const TImplementationProxy* ImplementationGroupProxy(TInt& aTableCount)
       
   522 	{
       
   523 	aTableCount = sizeof(ImplementationTable) / sizeof(TImplementationProxy);
       
   524 	return ImplementationTable;
       
   525 	}
       
   526 
       
   527 // End of file