bluetoothengine/bteng/src/btengsrvsession.cpp
changeset 0 f63038272f30
child 10 0707dd69d236
equal deleted inserted replaced
-1:000000000000 0:f63038272f30
       
     1 /*
       
     2 * Copyright (c) 2006 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:  Implementation of BTEng server session handling
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 
       
    20 #include <e32base.h>
       
    21 #include "btengsrvsession.h"
       
    22 #include "btengserver.h"
       
    23 #include "btengsrvbbconnectionmgr.h"
       
    24 #include "btengpairman.h"
       
    25 #include "btengclientserver.h"
       
    26 #include "debug.h"
       
    27 
       
    28 
       
    29 /**  The message argument which holds the client event package. */
       
    30 const TInt KBTEngEventSlot = 2;
       
    31 
       
    32 // ======== MEMBER FUNCTIONS ========
       
    33 
       
    34 // ---------------------------------------------------------------------------
       
    35 // C++ default constructor
       
    36 // ---------------------------------------------------------------------------
       
    37 //
       
    38 CBTEngSrvSession::CBTEngSrvSession()
       
    39 :   CSession2()
       
    40     {
       
    41     }
       
    42 
       
    43 
       
    44 // ---------------------------------------------------------------------------
       
    45 // Symbian 2nd-phase constructor
       
    46 // ---------------------------------------------------------------------------
       
    47 //
       
    48 void CBTEngSrvSession::ConstructL()
       
    49     {
       
    50     TRACE_FUNC_ENTRY
       
    51     
       
    52     //create queue for storing multiple connection events
       
    53     iConnectionEventQueue = new (ELeave) CDesC8ArraySeg(1);
       
    54     }
       
    55 
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // NewL
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CBTEngSrvSession* CBTEngSrvSession::NewL()
       
    62     {
       
    63     CBTEngSrvSession* self = new( ELeave ) CBTEngSrvSession();
       
    64     CleanupStack::PushL( self );
       
    65     self->ConstructL();
       
    66     CleanupStack::Pop( self );
       
    67     return self;
       
    68     }
       
    69 
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Destructor
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CBTEngSrvSession::~CBTEngSrvSession()
       
    76     {
       
    77     TRACE_FUNC_ENTRY
       
    78     if( !iNotifyConnMessage.IsNull() )
       
    79         {
       
    80         iNotifyConnMessage.Complete( KErrCancel );
       
    81         }
       
    82     CancelPairRequest();
       
    83     if( Server() )
       
    84         {
       
    85         Server()->RemoveSession( iAutoSwitchOff );
       
    86         }
       
    87     
       
    88     delete iConnectionEventQueue;
       
    89     
       
    90     TRACE_FUNC_EXIT
       
    91     }
       
    92 
       
    93 
       
    94 // ---------------------------------------------------------------------------
       
    95 // Complete the construction of the session.
       
    96 // ---------------------------------------------------------------------------
       
    97 //
       
    98 void CBTEngSrvSession::CreateL()
       
    99     {
       
   100     TRACE_FUNC_ENTRY
       
   101     Server()->AddSession();
       
   102     }
       
   103 
       
   104 
       
   105 // ---------------------------------------------------------------------------
       
   106 // Notify the client of the connection event.
       
   107 // ---------------------------------------------------------------------------
       
   108 //
       
   109 void CBTEngSrvSession::NotifyConnectionEvent(const TBTDevAddr aAddr, 
       
   110     TBTEngConnectionStatus aConnStatus, RBTDevAddrArray* aArray, TInt aErr)
       
   111     {
       
   112     TRACE_FUNC_ENTRY
       
   113 
       
   114     TRACE_INFO((_L("[BTEng]\t Informing client %d"), iNotifyConnMessage.Handle()));
       
   115     
       
   116     //package up connection event to send to the client
       
   117     TBTEngEventPkg pkg;
       
   118     pkg().iAddr = aAddr;
       
   119     pkg().iConnEvent = aConnStatus;
       
   120     pkg().iConflictsBuf.Zero();
       
   121     if (aArray)
       
   122         {
       
   123         TBTDevAddrPckgBuf addr;
       
   124         
       
   125         for (TInt i = 0; i < aArray->Count(); i++)
       
   126             {
       
   127             addr = (*aArray)[i];
       
   128             pkg().iConflictsBuf.Append(addr);
       
   129             }
       
   130         }
       
   131     
       
   132     TInt err = KErrNotFound;
       
   133     
       
   134     //if a client message is outstanding
       
   135     if (!iNotifyConnMessage.IsNull())
       
   136         {
       
   137         //complete the message prioritising the main error
       
   138         err = iNotifyConnMessage.Write(KBTEngEventSlot, pkg);
       
   139         aErr = (aErr != KErrNone) ? aErr : err;
       
   140         iNotifyConnMessage.Complete(aErr);
       
   141         }
       
   142     
       
   143     //if there is no client message outstanding or the write failed
       
   144     if (err != KErrNone)
       
   145         {
       
   146         //if the append fails we can't do anything with the error so ignore
       
   147         //it and let the event get dropped
       
   148         TRAP_IGNORE(iConnectionEventQueue->AppendL(pkg));
       
   149         }
       
   150     }
       
   151 
       
   152 // ---------------------------------------------------------------------------
       
   153 // Comfirm the caller if pairing request is completed in this invoke. 
       
   154 // ---------------------------------------------------------------------------
       
   155 //
       
   156 TInt CBTEngSrvSession::CompletePairRequest( TInt aResult )
       
   157     {
       
   158     if ( !iPairMessage.IsNull())
       
   159         {
       
   160         iPairMessage.Complete( aResult );
       
   161         return KErrNone;
       
   162         }
       
   163     return KErrNotFound;
       
   164     }
       
   165 
       
   166 // ---------------------------------------------------------------------------
       
   167 // From class CSession2
       
   168 // Handles servicing of a client request that has been passed to the server.
       
   169 // ---------------------------------------------------------------------------
       
   170 //
       
   171 void CBTEngSrvSession::ServiceL( const RMessage2& aMessage )
       
   172     {
       
   173     TRAPD( err, DispatchMessageL( aMessage ) );
       
   174 
       
   175     if( !aMessage.IsNull() &&
       
   176         ( err || 
       
   177           ( aMessage.Function() != EBTEngNotifyConnectionEvents && 
       
   178             aMessage.Function() != EBTEngPairDevice ) ) )
       
   179         {
       
   180             // Return the error code to the client.
       
   181         aMessage.Complete( err );
       
   182         }
       
   183     }
       
   184 
       
   185 
       
   186 // ---------------------------------------------------------------------------
       
   187 // Returns a handle to CBTEngServer.
       
   188 // ---------------------------------------------------------------------------
       
   189 //
       
   190 CBTEngServer* CBTEngSrvSession::Server()
       
   191     {
       
   192     return (CBTEngServer*) CSession2::Server();
       
   193     }
       
   194 
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // Handles a client request that has been passed to the server.
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 void CBTEngSrvSession::DispatchMessageL( const RMessage2& aMessage )
       
   201     {
       
   202     TRACE_FUNC_ENTRY
       
   203     TInt opcode = aMessage.Function();
       
   204    
       
   205     switch( opcode )
       
   206         {
       
   207         case EBTEngSetPowerState:
       
   208             {
       
   209             iAutoSwitchOff = (TBool) aMessage.Int1();
       
   210             Server()->SetPowerStateL( (TBTPowerStateValue) aMessage.Int0(), 
       
   211                                        iAutoSwitchOff );
       
   212             }
       
   213             break;
       
   214         case EBTEngSetVisibilityMode:
       
   215             {
       
   216             Server()->SetVisibilityModeL( (TBTVisibilityMode) aMessage.Int0(),
       
   217                                            aMessage.Int1() );
       
   218             }
       
   219             break;
       
   220         case EBTEngNotifyConnectionEvents:
       
   221             {
       
   222             //ensure the message contains the correctly sized descriptor
       
   223             if (aMessage.GetDesMaxLengthL(KBTEngEventSlot) != sizeof(TBTEngEventMsg))
       
   224                 {
       
   225                 User::Leave(KErrBadDescriptor);
       
   226                 }
       
   227             
       
   228             if(!iNotifyConnMessage.IsNull())
       
   229                 {
       
   230                 User::Leave(KErrInUse);
       
   231                 }
       
   232             
       
   233             //save the client message
       
   234             iNotifyConnMessage = RMessage2(aMessage);
       
   235             
       
   236             //if there is an existing connection event that the client does
       
   237             //not yet know about
       
   238             if (iConnectionEventQueue->Count() > 0)
       
   239                 {
       
   240                 //complete straight away with package from queue
       
   241                 TInt err = iNotifyConnMessage.Write(KBTEngEventSlot, (*iConnectionEventQueue)[0]);
       
   242                 if (err == KErrNone)
       
   243                     {
       
   244                     iConnectionEventQueue->Delete(0);
       
   245                     }
       
   246                 iNotifyConnMessage.Complete(err);
       
   247                 }
       
   248             }
       
   249             break;
       
   250         case EBTEngCancelEventNotifier:
       
   251             {
       
   252             if( !iNotifyConnMessage.IsNull() )
       
   253                 {
       
   254                 iNotifyConnMessage.Complete( KErrCancel );
       
   255                 }
       
   256             }
       
   257             break;
       
   258         case EBTEngConnectDevice:
       
   259         case EBTEngCancelConnectDevice:
       
   260         case EBTEngDisconnectDevice:
       
   261         case EBTEngIsDeviceConnected:
       
   262         case EBTEngGetConnectedAddresses:
       
   263             {
       
   264             TBTPowerStateValue pwr = EBTPowerOff;
       
   265             Server()->GetHwPowerState( pwr );
       
   266             if( pwr )
       
   267                 {
       
   268                     // Simply forward it to the plug-in manager
       
   269                 Server()->DispatchPluginMessageL( aMessage );
       
   270                 }
       
   271             else
       
   272                 {
       
   273                 User::Leave( KErrNotReady );
       
   274                 }
       
   275             }
       
   276             break;    
       
   277         case EBTEngIsDeviceConnectable:
       
   278             {
       
   279             Server()->DispatchPluginMessageL( aMessage );
       
   280             }
       
   281             break;
       
   282         case EBTEngPrepareDiscovery:
       
   283             {
       
   284             aMessage.Complete( KErrNone );  // Client does not have to wait.
       
   285             Server()->iBBConnMgr->ManageTopology( ETrue );
       
   286             }
       
   287             break;
       
   288         case EBTEngSetPairingObserver:
       
   289         case EBTEngPairDevice:
       
   290             {
       
   291             TBTPowerStateValue pwr = EBTPowerOff;
       
   292             (void) Server()->GetHwPowerState( pwr );
       
   293             if( pwr )
       
   294                 {
       
   295                 // Simply forward it to the pairing manager
       
   296                 Server()->PairManager().ProcessCommandL( aMessage );
       
   297                 if ( opcode == EBTEngPairDevice )
       
   298                     {
       
   299                     iPairMessage = RMessage2( aMessage );
       
   300                     }
       
   301                 }
       
   302             else
       
   303                 {
       
   304                 User::Leave( KErrNotReady );
       
   305                 }
       
   306             break;
       
   307             }
       
   308         case EBTEngCancelPairDevice:
       
   309             {
       
   310             CancelPairRequest();
       
   311             break;
       
   312             }
       
   313         default:
       
   314             {
       
   315             TRACE_INFO( ( _L( "[BTENG]\t DispatchMessageL: bad request (%d)" ), 
       
   316                                 aMessage.Function() ) )
       
   317             User::Leave( KErrArgument );
       
   318             }
       
   319             break;
       
   320         }
       
   321     TRACE_FUNC_EXIT
       
   322     }
       
   323 
       
   324 // ---------------------------------------------------------------------------
       
   325 // Only the originator of pairing can cancel the pairing request.
       
   326 // ---------------------------------------------------------------------------
       
   327 //
       
   328 void CBTEngSrvSession::CancelPairRequest()
       
   329     {
       
   330     if ( !iPairMessage.IsNull() )
       
   331         {
       
   332         Server()->PairManager().CancelCommand( iPairMessage.Function() );
       
   333         iPairMessage.Complete( KErrCancel );
       
   334         }
       
   335     }