atext/server/src/atextcommonsession.cpp
changeset 0 29b1cd4cb562
child 16 9f17f914e828
equal deleted inserted replaced
-1:000000000000 0:29b1cd4cb562
       
     1 /*
       
     2 * Copyright (c) 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 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <badesca.h>
       
    20 #include <atextcommonbase.h>
       
    21 #include "atextsrvcommon.h"
       
    22 #include "atextclientsrvcommon.h"
       
    23 #include "atextcommonsession.h"
       
    24 #include "atext_interfaceuid.h"
       
    25 #include "utils.h"
       
    26 #include "debug.h"
       
    27 
       
    28 // ---------------------------------------------------------------------------
       
    29 // Two-phased constructor.
       
    30 // ---------------------------------------------------------------------------
       
    31 //
       
    32 CATExtCommonSession* CATExtCommonSession::NewL( CATExtSrvCommon& aServer,
       
    33                                                 const TVersion& aVersion )
       
    34     {
       
    35     return new (ELeave) CATExtCommonSession( aServer, aVersion );
       
    36     }
       
    37 
       
    38 // ---------------------------------------------------------------------------
       
    39 // Destructor.
       
    40 // ---------------------------------------------------------------------------
       
    41 //
       
    42 CATExtCommonSession::~CATExtCommonSession()
       
    43     {
       
    44     TRACE_FUNC_ENTRY
       
    45     Destruct( EFalse );
       
    46     TRACE_FUNC_EXIT
       
    47     }
       
    48 
       
    49 // ---------------------------------------------------------------------------
       
    50 // Handles the servicing of client requests passed to the server
       
    51 // ---------------------------------------------------------------------------
       
    52 //
       
    53 void CATExtCommonSession::ServiceL( const RMessage2& aMessage )
       
    54     {
       
    55     if ( iVersion.iMajor==KCommonServerMajorVersionNumber &&
       
    56          iVersion.iMinor==KCommonServerMinorVersionNumber &&
       
    57          iVersion.iBuild==KCommonServerBuildVersionNumber )
       
    58         {
       
    59         DoServiceCommonL( aMessage );
       
    60         }
       
    61     else
       
    62         {
       
    63         aMessage.Complete( KErrNotSupported );
       
    64         }
       
    65     TRACE_FUNC_ENTRY
       
    66     }
       
    67 
       
    68 // ---------------------------------------------------------------------------
       
    69 // Handles the servicing of client requests passed to the server
       
    70 // ---------------------------------------------------------------------------
       
    71 //
       
    72 void CATExtCommonSession::DoServiceCommonL( const RMessage2& aMessage )
       
    73     {
       
    74     TRACE_FUNC_ENTRY
       
    75     switch ( aMessage.Function() )
       
    76         {
       
    77         case EATExtSetCommonInterface:
       
    78             {
       
    79             TInt retTrap = KErrNone;
       
    80             TRAP( retTrap, IpcSetCommonInterfaceL(aMessage) );
       
    81             aMessage.Complete( retTrap );
       
    82             }
       
    83             break;
       
    84         case EATExtSynchronousClose:
       
    85             {
       
    86             IpcSynchronousClose( aMessage );
       
    87             }
       
    88             break;
       
    89         case EATExtGetMode:
       
    90             {
       
    91             IpcGetMode( aMessage );
       
    92             }
       
    93             break;
       
    94         case EATExtReceiveModeStatusChange:
       
    95             {
       
    96             IpcReceiveModeStatusChange( aMessage );
       
    97             }
       
    98             break;
       
    99         case EATExtCancelReceiveModeStatusChange:
       
   100             {
       
   101             IpcCancelReceiveModeStatusChange( aMessage );
       
   102             }
       
   103             break;
       
   104         case EATExtGetNvramStatus:
       
   105             {
       
   106             IpcGetNvramStatus( aMessage );
       
   107             }
       
   108             break;
       
   109         case EATExtReceiveNvramStatusChange:
       
   110             {
       
   111             IpcReceiveNvramStatusChange( aMessage );
       
   112             }
       
   113             break;
       
   114         case EATExtCancelReceiveNvramStatusChange:
       
   115             {
       
   116             IpcCancelReceiveNvramStatusChange( aMessage );
       
   117             }
       
   118             break;
       
   119         default:
       
   120             {
       
   121             aMessage.Complete( KErrNotSupported );
       
   122             break;
       
   123             }
       
   124         }
       
   125     TRACE_FUNC_EXIT
       
   126     }
       
   127 
       
   128 // ---------------------------------------------------------------------------
       
   129 // CATExtCommonSession::CATExtCommonSession
       
   130 // ---------------------------------------------------------------------------
       
   131 //
       
   132 CATExtCommonSession::CATExtCommonSession( CATExtSrvCommon& aServer,
       
   133                                           const TVersion& aVersion ) :
       
   134     iServer( aServer ),
       
   135     iVersion( aVersion )
       
   136     {
       
   137     }
       
   138 
       
   139 // ---------------------------------------------------------------------------
       
   140 // Handles the servicing of setting up interface
       
   141 // ---------------------------------------------------------------------------
       
   142 //
       
   143 void CATExtCommonSession::IpcSetCommonInterfaceL( const RMessage2& aMessage )
       
   144     {
       
   145     TRACE_FUNC_ENTRY
       
   146     if ( iCommonBase )
       
   147         {
       
   148         TRACE_FUNC_EXIT
       
   149         User::Leave( KErrGeneral );
       
   150         }
       
   151     RBuf8 connectionName;
       
   152     CleanupClosePushL( connectionName );
       
   153     TInt retTemp = ReadStringFromMessage( connectionName,
       
   154                                           EATExtConnectParamName,
       
   155                                           aMessage );
       
   156     if ( retTemp != KErrNone )
       
   157         {
       
   158         TRACE_FUNC_EXIT
       
   159         User::Leave( retTemp );
       
   160         }
       
   161     REComSession& ecomSession = REComSession::OpenL();
       
   162     CleanupClosePushL( ecomSession );
       
   163     RImplInfoPtrArray implementations;
       
   164     CleanupResetDestroyClosePushL( implementations );
       
   165     TUid ifUid = TUid::Uid( ATEXT_INTERFACE_COM_UID );
       
   166     iEComSession.ListImplementationsL( ifUid,
       
   167                                        implementations );
       
   168     if ( implementations.Count() != 1 )
       
   169         {
       
   170         TRACE_FUNC_EXIT
       
   171         User::Leave( KErrGeneral );
       
   172         }
       
   173     TUid pluginUid = implementations[0]->ImplementationUid();
       
   174     iCommonBase = CATExtCommonBase::NewL( pluginUid, *this, connectionName );
       
   175     CleanupStack::PopAndDestroy( &implementations );
       
   176     CleanupStack::Pop( &ecomSession );
       
   177     CleanupStack::PopAndDestroy( &connectionName );
       
   178     iEComSession = ecomSession;
       
   179     TRACE_FUNC_EXIT
       
   180     }
       
   181 
       
   182 // ---------------------------------------------------------------------------
       
   183 // Synchronously closes the session
       
   184 // Optional: client can do either Close() or SynchronousClose()
       
   185 // ---------------------------------------------------------------------------
       
   186 //
       
   187 TInt CATExtCommonSession::IpcSynchronousClose( const RMessage2& aMessage )
       
   188     {
       
   189     TRACE_FUNC_ENTRY
       
   190     Destruct( ETrue );
       
   191     aMessage.Complete( KErrNone );
       
   192     TRACE_FUNC_EXIT
       
   193     return KErrNone;
       
   194     }
       
   195 
       
   196 // ---------------------------------------------------------------------------
       
   197 // Handles the servicing of mode status reporting
       
   198 // ---------------------------------------------------------------------------
       
   199 //
       
   200 TInt CATExtCommonSession::IpcGetMode( const RMessage2& aMessage )
       
   201     {
       
   202     TRACE_FUNC_ENTRY
       
   203     if ( !iCommonBase )
       
   204         {
       
   205         aMessage.Complete( KErrGeneral );
       
   206         TRACE_FUNC_EXIT
       
   207         return KErrGeneral;
       
   208         }
       
   209     TUint currentMode = 0;
       
   210     TPckgBuf<TUint> maskPckg;
       
   211     TInt retTemp = ReadStructFromMessage( maskPckg,
       
   212                                           EATExtGetModeParamMask,
       
   213                                           aMessage );
       
   214     if ( retTemp != KErrNone )
       
   215         {
       
   216         aMessage.Complete( retTemp );
       
   217         TRACE_FUNC_EXIT
       
   218         return retTemp;
       
   219         }
       
   220     retTemp = iCommonBase->GetMode( maskPckg(), currentMode );
       
   221     if ( retTemp != KErrNone )
       
   222         {
       
   223         aMessage.Complete( retTemp );
       
   224         TRACE_FUNC_EXIT
       
   225         return retTemp;
       
   226         }
       
   227     TPckg<TUint> currentModePckg( currentMode );
       
   228     retTemp = aMessage.Write( EATExtGetModeParamMode, currentModePckg );
       
   229     TRACE_INFO(( _L("Write returned %d"), retTemp ));
       
   230     aMessage.Complete( KErrNone );
       
   231     TRACE_FUNC_EXIT
       
   232     return KErrNone;
       
   233     }
       
   234 
       
   235 // ---------------------------------------------------------------------------
       
   236 // Handles the servicing of mode status change receiving
       
   237 // ---------------------------------------------------------------------------
       
   238 //
       
   239 TInt CATExtCommonSession::IpcReceiveModeStatusChange( const RMessage2& aMessage )
       
   240     {
       
   241     TRACE_FUNC_ENTRY
       
   242     if ( !iCommonBase )
       
   243         {
       
   244         aMessage.Complete( KErrGeneral );
       
   245         TRACE_FUNC_EXIT
       
   246         return KErrGeneral;
       
   247         }
       
   248     if ( iModeStatusMessage.Handle() )
       
   249         {
       
   250         aMessage.Complete( KErrInUse );
       
   251         TRACE_FUNC_EXIT
       
   252         return KErrInUse;
       
   253         }
       
   254     TInt retTemp = iCommonBase->ReceiveModeStatusChange();
       
   255     if ( retTemp != KErrNone )
       
   256         {
       
   257         aMessage.Complete( retTemp );
       
   258         TRACE_FUNC_EXIT
       
   259         return retTemp;
       
   260         }
       
   261     iModeStatusMessage = aMessage;
       
   262     TRACE_FUNC_EXIT
       
   263     return KErrNone;
       
   264     }
       
   265 
       
   266 // ---------------------------------------------------------------------------
       
   267 // Handles the servicing of mode status change receive cancel
       
   268 // ---------------------------------------------------------------------------
       
   269 //
       
   270 TInt CATExtCommonSession::IpcCancelReceiveModeStatusChange(
       
   271     const RMessage2& aMessage )
       
   272     {
       
   273     TRACE_FUNC_ENTRY
       
   274     if ( !iCommonBase )
       
   275         {
       
   276         aMessage.Complete( KErrGeneral );
       
   277         TRACE_FUNC_EXIT
       
   278         return KErrGeneral;
       
   279         }
       
   280     if ( !iModeStatusMessage.Handle() )
       
   281         {
       
   282         aMessage.Complete( KErrBadHandle );
       
   283         TRACE_FUNC_EXIT
       
   284         return KErrBadHandle;
       
   285         }
       
   286     iCommonBase->CancelReceiveModeStatusChange();
       
   287     iModeStatusMessage.Complete( KErrCancel );
       
   288     aMessage.Complete( KErrNone );
       
   289     TRACE_FUNC_EXIT
       
   290     return KErrNone;
       
   291     }
       
   292 
       
   293 // ---------------------------------------------------------------------------
       
   294 // Handles the servicing of NVRAM status reporting
       
   295 // ---------------------------------------------------------------------------
       
   296 //
       
   297 TInt CATExtCommonSession::IpcGetNvramStatus( const RMessage2& aMessage )
       
   298     {
       
   299     TRACE_FUNC_ENTRY
       
   300     if ( !iCommonBase )
       
   301         {
       
   302         aMessage.Complete( KErrGeneral );
       
   303         TRACE_FUNC_EXIT
       
   304         return KErrGeneral;
       
   305         }
       
   306     RBuf8 nvramBuffer;
       
   307     TInt retVal = iCommonBase->GetNvramStatus( nvramBuffer );
       
   308     WriteReplyBufferToClient( nvramBuffer,
       
   309                               EATExtGetNvramStatusParamNvram,
       
   310                               aMessage );
       
   311     aMessage.Complete( retVal );
       
   312     nvramBuffer.Close();
       
   313     TRACE_FUNC_EXIT
       
   314     return retVal;
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // Handles the servicing of NVRAM status receiving
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 TInt CATExtCommonSession::IpcReceiveNvramStatusChange(
       
   322     const RMessage2& aMessage )
       
   323     {
       
   324     TRACE_FUNC_ENTRY
       
   325     if ( !iCommonBase )
       
   326         {
       
   327         aMessage.Complete( KErrGeneral );
       
   328         TRACE_FUNC_EXIT
       
   329         return KErrGeneral;
       
   330         }
       
   331     if ( iNvramStatusMessage.Handle() )
       
   332         {
       
   333         aMessage.Complete( KErrInUse );
       
   334         TRACE_FUNC_EXIT
       
   335         return KErrInUse;
       
   336         }
       
   337     if ( aMessage.GetDesMaxLength(EATExtGetNvramStatusParamNvram) !=
       
   338          KDefaultNvramBufLength )
       
   339         {
       
   340         aMessage.Complete( KErrBadDescriptor );
       
   341         TRACE_FUNC_EXIT
       
   342         return KErrBadDescriptor;
       
   343         }
       
   344     TInt retTemp = iCommonBase->ReceiveNvramStatusChange();
       
   345     if ( retTemp != KErrNone )
       
   346         {
       
   347         aMessage.Complete( retTemp );
       
   348         TRACE_FUNC_EXIT
       
   349         return retTemp;
       
   350         }
       
   351     iNvramStatusMessage = aMessage;
       
   352     TRACE_FUNC_EXIT
       
   353     return KErrNone;
       
   354     }
       
   355 
       
   356 // ---------------------------------------------------------------------------
       
   357 // Handles the servicing of pending NVRAM status receive cancel
       
   358 // ---------------------------------------------------------------------------
       
   359 //
       
   360 TInt CATExtCommonSession::IpcCancelReceiveNvramStatusChange(
       
   361     const RMessage2& aMessage )
       
   362     {
       
   363     TRACE_FUNC_ENTRY
       
   364     if ( !iCommonBase )
       
   365         {
       
   366         aMessage.Complete( KErrGeneral );
       
   367         TRACE_FUNC_EXIT
       
   368         return KErrGeneral;
       
   369         }
       
   370     if ( !iNvramStatusMessage.Handle() )
       
   371         {
       
   372         aMessage.Complete( KErrBadHandle );
       
   373         TRACE_FUNC_EXIT
       
   374         return KErrBadHandle;
       
   375         }
       
   376     iCommonBase->CancelReceiveNvramStatusChange();
       
   377     iNvramStatusMessage.Complete( KErrCancel );
       
   378     aMessage.Complete( KErrNone );
       
   379     TRACE_FUNC_EXIT
       
   380     return KErrNone;
       
   381     }
       
   382 
       
   383 // ---------------------------------------------------------------------------
       
   384 // Writes NVRAM status to client with WriteReplyBufferToClient() and
       
   385 // completes the client request message
       
   386 // ---------------------------------------------------------------------------
       
   387 //
       
   388 TInt CATExtCommonSession::WriteNvramStatusToClient( const TDesC8& aNvram,
       
   389                                                     TInt aNvramSlot )
       
   390     {
       
   391     TRACE_FUNC_ENTRY
       
   392     if ( !iNvramStatusMessage.Handle() )
       
   393         {
       
   394         TRACE_FUNC_EXIT
       
   395         return KErrBadHandle;
       
   396         }
       
   397     TInt retVal = WriteReplyBufferToClient( aNvram,
       
   398                                             aNvramSlot,
       
   399                                             iNvramStatusMessage );
       
   400     iNvramStatusMessage.Complete( retVal );
       
   401     TRACE_FUNC_EXIT
       
   402     return retVal;
       
   403     }
       
   404 
       
   405 // ---------------------------------------------------------------------------
       
   406 // Reads a string from a message
       
   407 // ---------------------------------------------------------------------------
       
   408 //
       
   409 TInt CATExtCommonSession::ReadStringFromMessage( RBuf8& aBuffer,
       
   410                                                  TInt aDataSlot,
       
   411                                                  const RMessage2& aMessage )
       
   412     {
       
   413     TRACE_FUNC_ENTRY
       
   414     TInt retVal = KErrNone;
       
   415     TInt desLength = aMessage.GetDesLength( aDataSlot );
       
   416     if ( desLength <= 0 )
       
   417         {
       
   418         TRACE_FUNC_EXIT
       
   419         return KErrArgument;
       
   420         }
       
   421     retVal = aBuffer.Create( desLength );
       
   422     if ( retVal == KErrNone )
       
   423         {
       
   424         retVal = aMessage.Read( aDataSlot, aBuffer );
       
   425         TRACE_INFO(( _L("Read returned %d"), retVal ));
       
   426         }
       
   427     TRACE_FUNC_EXIT
       
   428     return retVal;
       
   429     }
       
   430 
       
   431 // ---------------------------------------------------------------------------
       
   432 // Reads a struct from a message
       
   433 // ---------------------------------------------------------------------------
       
   434 //
       
   435 TInt CATExtCommonSession::ReadStructFromMessage( TDes8& aBuffer,
       
   436                                                  TInt aDataSlot,
       
   437                                                  const RMessage2& aMessage )
       
   438     {
       
   439     TRACE_FUNC_ENTRY
       
   440     TInt desLength = aMessage.GetDesLength( aDataSlot );
       
   441     if ( desLength <= 0 )
       
   442         {
       
   443         TRACE_FUNC_EXIT
       
   444         return KErrArgument;
       
   445         }
       
   446     TInt retVal = aMessage.Read( aDataSlot, aBuffer );
       
   447     TRACE_INFO(( _L("Read returned %d"), retVal ));
       
   448     TRACE_FUNC_EXIT
       
   449     return retVal;
       
   450     }
       
   451 
       
   452 // ---------------------------------------------------------------------------
       
   453 // Writes specified input reply buffer to a client request message at given
       
   454 // message slot number.
       
   455 // ---------------------------------------------------------------------------
       
   456 //
       
   457 TInt CATExtCommonSession::WriteReplyBufferToClient( const TDesC8& aBuffer,
       
   458                                                     TInt aDataSlot,
       
   459                                                     const RMessage2& aMessage )
       
   460     {
       
   461     TRACE_FUNC_ENTRY
       
   462     TInt maxLength = aMessage.GetDesMaxLength( aDataSlot );
       
   463     if ( aBuffer.Length() > maxLength )
       
   464         {
       
   465         TRACE_FUNC_EXIT
       
   466         return KErrTooBig;
       
   467         }
       
   468     TInt retVal = aMessage.Write( aDataSlot, aBuffer );
       
   469     TRACE_INFO(( _L("Write returned %d"), retVal ));
       
   470     TRACE_FUNC_EXIT
       
   471     return retVal;
       
   472     }
       
   473 
       
   474 // ---------------------------------------------------------------------------
       
   475 // Destructs objects/associations for the current session
       
   476 // ---------------------------------------------------------------------------
       
   477 //
       
   478 void CATExtCommonSession::Destruct( TBool aSyncClose )
       
   479     {
       
   480     TRACE_FUNC_ENTRY
       
   481     if ( iModeStatusMessage.Handle() )
       
   482         {
       
   483         iModeStatusMessage.Complete( KErrServerTerminated );
       
   484         }
       
   485     if ( iNvramStatusMessage.Handle() )
       
   486         {
       
   487         iNvramStatusMessage.Complete( KErrServerTerminated );
       
   488         }
       
   489     delete iCommonBase;
       
   490     iCommonBase = NULL;
       
   491     iEComSession.Close();
       
   492     if ( !aSyncClose )
       
   493         {
       
   494         REComSession::FinalClose();
       
   495         iServer.ClientClosed( *this );
       
   496         }
       
   497     TRACE_FUNC_EXIT
       
   498     }
       
   499 
       
   500 // ---------------------------------------------------------------------------
       
   501 // From MATExtCommonObserver.
       
   502 // Called by concrete extension Plugin to inform that mode status has changed.
       
   503 // ---------------------------------------------------------------------------
       
   504 //
       
   505 TInt CATExtCommonSession::SendModeStatusChange( TUint aMode )
       
   506     {
       
   507     TRACE_FUNC_ENTRY
       
   508     if ( !iModeStatusMessage.Handle() )
       
   509         {
       
   510         TRACE_FUNC_EXIT
       
   511         return KErrBadHandle;
       
   512         }
       
   513     TPckg<TUint> modePckg( aMode );
       
   514     TInt retTemp = iModeStatusMessage.Write( EATExtReceiveModeChangeParamMode,
       
   515                                              modePckg );
       
   516     TRACE_INFO(( _L("Write returned %d"), retTemp ));
       
   517     iModeStatusMessage.Complete( KErrNone );
       
   518     TRACE_FUNC_EXIT
       
   519     return KErrNone;
       
   520     }
       
   521 
       
   522 // ---------------------------------------------------------------------------
       
   523 // From MATExtCommonObserver.
       
   524 // Called by concrete extension plugin to inform that NVRAM status has
       
   525 // changed.
       
   526 // ---------------------------------------------------------------------------
       
   527 //
       
   528 TInt CATExtCommonSession::SendNvramStatusChange( const TDesC8& aNvram )
       
   529     {
       
   530     TRACE_FUNC_ENTRY
       
   531     WriteNvramStatusToClient( aNvram, EATExtReceiveNvramChangeParamNvram );
       
   532     TRACE_FUNC_EXIT
       
   533     return KErrNone;
       
   534     }
       
   535 
       
   536 // ---------------------------------------------------------------------------
       
   537 // From MATExtCommonObserver.
       
   538 // Called by the destructor of CATExtPluginBase.
       
   539 // A concrete service provider implementation should not touch this.
       
   540 // ---------------------------------------------------------------------------
       
   541 //
       
   542 TInt CATExtCommonSession::ATExtPluginClosed( CATExtCommonBase* /*aPlugin*/ )
       
   543     {
       
   544     TRACE_FUNC_ENTRY
       
   545     iCommonBase = NULL;
       
   546     TRACE_FUNC_EXIT
       
   547     return KErrNone;
       
   548     }