dvrengine/CommonRecordingEngine/src/CCRServer.cpp
changeset 41 d88d70d98bbc
parent 34 814ba97beeb9
child 46 3bc36dbd63c2
equal deleted inserted replaced
34:814ba97beeb9 41:d88d70d98bbc
     1 /*
       
     2 * Copyright (c) 2007 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 the License "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 CR servers server class*
       
    15 */
       
    16 
       
    17 
       
    18 
       
    19 
       
    20 // INCLUDE FILES
       
    21 #include "CCRServer.h"
       
    22 #include "CCRSession.h"
       
    23 #include "CCREngine.h"
       
    24 #include <ipvideo/CRTypeDefs.h> // Constants exported from client library
       
    25 #include <e32svr.h>
       
    26 #include <e32math.h>
       
    27 #include <e32uid.h>
       
    28 #include "videoserviceutilsLogger.h"
       
    29 
       
    30 // CONSTANTS
       
    31 // Platform security. Custom check is applied to all IPCs.
       
    32 static const int KRangeCount( 1 );
       
    33 
       
    34 static const TInt SecurityRanges[KRangeCount] =
       
    35     {
       
    36     0, // Range is from 0 to KMaxTInt
       
    37     };
       
    38 
       
    39 static const TUint8 SecurityRangesPolicy[KRangeCount] =
       
    40     {
       
    41     CPolicyServer::ECustomCheck
       
    42     };
       
    43 
       
    44 static const CPolicyServer::TPolicy Policy =
       
    45     {
       
    46     CPolicyServer::EAlwaysPass,
       
    47     KRangeCount,
       
    48     SecurityRanges,
       
    49     SecurityRangesPolicy,
       
    50     NULL,
       
    51     };
       
    52 
       
    53 // ============================ MEMBER FUNCTIONS ===============================
       
    54 
       
    55 // -----------------------------------------------------------------------------
       
    56 // CCRServer::NewLC()
       
    57 // 
       
    58 // -----------------------------------------------------------------------------
       
    59 //
       
    60 CCRServer* CCRServer::NewLC(  )
       
    61     {
       
    62     CCRServer* self = new( ELeave ) CCRServer;
       
    63     CleanupStack::PushL( self );  
       
    64     self->ConstructL( );
       
    65     return self;
       
    66     }
       
    67 
       
    68 // -----------------------------------------------------------------------------
       
    69 // CCRServer::CCRServer()
       
    70 // C++ constructor 
       
    71 // -----------------------------------------------------------------------------
       
    72 //
       
    73 CCRServer::CCRServer()
       
    74   : CPolicyServer( EPriorityRealTimeServer, Policy, ESharableSessions ),
       
    75     iSessionCount( 0 )
       
    76     {
       
    77     __DECLARE_NAME( _S( "CCRServer" ) );
       
    78     }
       
    79 
       
    80 // -----------------------------------------------------------------------------
       
    81 // CCRServer::ConstructL()
       
    82 // second-phase constructor; create the object container index.
       
    83 // -----------------------------------------------------------------------------
       
    84 //
       
    85 void CCRServer::ConstructL(  )
       
    86     {
       
    87     LOG( "CCRServer::ConstructL()" );    
       
    88     
       
    89     iContainerIx = CObjectConIx::NewL();
       
    90     iObjectCon = iContainerIx->CreateL();
       
    91     
       
    92     StartL( KCRServerNameExe );
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CCRServer::~CCRServer()
       
    97 // Destructor.
       
    98 // -----------------------------------------------------------------------------
       
    99 //
       
   100 CCRServer::~CCRServer()
       
   101     {
       
   102     LOG( "CCRServer::~CCRServer()" );
       
   103 
       
   104     DeleteRtpEngine();
       
   105     }
       
   106 
       
   107 // -----------------------------------------------------------------------------
       
   108 // CCRServer::ThreadFunction()
       
   109 // The active scheduler is installed and started here.
       
   110 // -----------------------------------------------------------------------------
       
   111 //
       
   112 TInt CCRServer::ThreadFunction(  )
       
   113     {
       
   114     CTrapCleanup* cleanupStack = CTrapCleanup::New();    
       
   115     if ( !cleanupStack )
       
   116         {
       
   117         PanicServer( KErrNoMemory );
       
   118         }
       
   119 
       
   120     TRAPD( err, ThreadFunctionL( ) );
       
   121     if ( err )
       
   122         {
       
   123         PanicServer( KErrServerTerminated );
       
   124         }
       
   125 
       
   126     delete cleanupStack;
       
   127     cleanupStack = NULL;
       
   128     
       
   129     return KErrNone;    
       
   130     }
       
   131 
       
   132 // -----------------------------------------------------------------------------
       
   133 // CCRServer::ThreadFunctionL()
       
   134 //
       
   135 // -----------------------------------------------------------------------------
       
   136 //
       
   137 void CCRServer::ThreadFunctionL(  )
       
   138     {
       
   139     LOG( "CCRServer::ThreadFunctionL() - In" );
       
   140 
       
   141     // Construct active scheduler
       
   142     CActiveScheduler* activeScheduler = new ( ELeave ) CActiveScheduler;
       
   143     CleanupStack::PushL( activeScheduler ); // |-> 1
       
   144 
       
   145     // Install active scheduler. 
       
   146     // We don't need to check whether an active scheduler is already installed
       
   147     // as this is a new thread, so there won't be one
       
   148     CActiveScheduler::Install( activeScheduler );
       
   149 
       
   150     // Construct our server, pushed cleanup stack and leaved there
       
   151     CCRServer* server = CCRServer::NewLC( );  // |-> 2    
       
   152     
       
   153     // Signal server is up
       
   154     RProcess::Rendezvous( KErrNone );
       
   155 
       
   156     // Start handling requests
       
   157     CActiveScheduler::Start();
       
   158 
       
   159     CleanupStack::PopAndDestroy( server ); // 2<-|
       
   160     CleanupStack::PopAndDestroy( activeScheduler ); // 1<-|
       
   161     
       
   162     LOG( "CCRServer::ThreadFunctionL() - Out" );    
       
   163     }
       
   164 
       
   165 // -----------------------------------------------------------------------------
       
   166 // CCRServer::SignalClientL
       
   167 // Signal the client that server is running.
       
   168 // -----------------------------------------------------------------------------
       
   169 void CCRServer::SignalClientL()
       
   170     {
       
   171     RSemaphore semaphore;
       
   172     User::LeaveIfError( semaphore.OpenGlobal( KCRServerSemaphoreName ) );
       
   173     semaphore.Signal();
       
   174     semaphore.Close();
       
   175     }
       
   176 
       
   177 // -----------------------------------------------------------------------------
       
   178 // CCRServer::PanicServer
       
   179 // Utility - panic the server
       
   180 // -----------------------------------------------------------------------------
       
   181 void CCRServer::PanicServer( TInt aPanic )
       
   182     {
       
   183     LOG1( "CCRServer::PanicServer(), aPanic: %d", aPanic );
       
   184     
       
   185     User::Panic( KCRServerNameExe, aPanic );
       
   186     }
       
   187 
       
   188 // -----------------------------------------------------------------------------
       
   189 // CCRServer::GetEngineObjectL()
       
   190 //
       
   191 // -----------------------------------------------------------------------------
       
   192 //
       
   193 CCREngine* CCRServer::GetEngineObjectL()
       
   194     {
       
   195     if ( iObjectCon->Count() == 0 )
       
   196         {
       
   197         // Create CR engine
       
   198         iEngine = CCREngine::NewL( );
       
   199 
       
   200         // Add our engine to container
       
   201         iObjectCon->AddL( iEngine );
       
   202         }
       
   203     else
       
   204         {
       
   205         // default implementation return KErrNone.
       
   206         if ( KErrNone != iEngine->Open() )
       
   207             {
       
   208             User::Leave( KErrGeneral );
       
   209             }
       
   210         }
       
   211 
       
   212     // We have only one object in our container
       
   213     return iEngine;
       
   214     }
       
   215 
       
   216 // -----------------------------------------------------------------------------
       
   217 // CCRServer::Inc()
       
   218 // 
       
   219 // -----------------------------------------------------------------------------
       
   220 //
       
   221 void CCRServer::Inc()
       
   222     {
       
   223     iSessionCount++;
       
   224     LOG1( "CCRServer::Inc(), New iSessionCount: %d", iSessionCount );
       
   225     }
       
   226 
       
   227 // -----------------------------------------------------------------------------
       
   228 // CCRServer::Dec()
       
   229 // 
       
   230 // -----------------------------------------------------------------------------
       
   231 //
       
   232 void CCRServer::Dec()
       
   233     {
       
   234     iSessionCount--;
       
   235     LOG1( "CCRServer::Dec(), New iSessionCount: %d", iSessionCount );
       
   236 
       
   237     if ( iSessionCount <= 0 )
       
   238         {
       
   239         StopServer();
       
   240         }
       
   241     }
       
   242 
       
   243 // -----------------------------------------------------------------------------
       
   244 // CCRServer::NewSessionL()
       
   245 // 
       
   246 // -----------------------------------------------------------------------------
       
   247 //
       
   248 CSession2* CCRServer::NewSessionL(
       
   249     const TVersion& aVersion,
       
   250     const RMessage2& /*aMessage*/ ) const
       
   251     {
       
   252     // Check version is ok
       
   253     TVersion v( KCRServMajorVersionNumber,
       
   254                 KCRServMinorVersionNumber,
       
   255                 KCRServBuildVersionNumber );
       
   256     
       
   257     if ( !User::QueryVersionSupported( v, aVersion ) )
       
   258         {
       
   259         User::Leave( KErrNotSupported );
       
   260         }
       
   261         
       
   262     // Make new session
       
   263     return CCRSession::NewL( ( CCRServer* ) this );
       
   264     }
       
   265 
       
   266 // -----------------------------------------------------------------------------
       
   267 // CCRServer::StopServer
       
   268 // Stops the server thread if no sessions active.
       
   269 // -----------------------------------------------------------------------------
       
   270 //
       
   271 void CCRServer::StopServer()
       
   272     {
       
   273     LOG( "CCRServer::StopServer()" );    
       
   274     
       
   275     CActiveScheduler::Stop();
       
   276     }
       
   277     
       
   278 // -----------------------------------------------------------------------------
       
   279 // CCRServer::DeleteRtpEngine
       
   280 // Stops active scheduler and deletes object container and other objects.
       
   281 // -----------------------------------------------------------------------------
       
   282 //
       
   283 void CCRServer::DeleteRtpEngine()
       
   284     {
       
   285     LOG1( "CCRServer::DeleteRtpEngine(), iContainerIx: %d", iContainerIx );
       
   286     
       
   287     if ( iContainerIx )
       
   288         {
       
   289         iContainerIx->Remove( iObjectCon );
       
   290         delete iContainerIx; iContainerIx = NULL;
       
   291         }
       
   292     }
       
   293 
       
   294 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   295 
       
   296 // -----------------------------------------------------------------------------
       
   297 // StartThread()
       
   298 // Start the server thread. This is called from the client.
       
   299 // -----------------------------------------------------------------------------
       
   300 //
       
   301 TInt CCRServer::StartThread()
       
   302     {
       
   303 #ifdef _DEBUG
       
   304     __UHEAP_MARK;
       
   305 #endif
       
   306     
       
   307     LOG( "CCRServer::StartThread() - In" );
       
   308     // Check server not already started
       
   309     TFindServer findCountServer( KCRServerNameExe );
       
   310     TFullName name;
       
   311     if ( findCountServer.Next( name ) == KErrNone )
       
   312         { 
       
   313         // Found server already
       
   314         TRAP_IGNORE( CCRServer::SignalClientL() );
       
   315         return KErrAlreadyExists;
       
   316         }
       
   317     
       
   318     ThreadFunction( );
       
   319     
       
   320     LOG( "CCRServer::StartThread() - Out" );
       
   321 
       
   322 #ifdef _DEBUG
       
   323     __UHEAP_MARKEND;
       
   324 #endif
       
   325     
       
   326     return KErrNone;
       
   327     }
       
   328 
       
   329 // -----------------------------------------------------------------------------
       
   330 // E32Main()
       
   331 // Server startup
       
   332 // Returns: KErrNone
       
   333 // -----------------------------------------------------------------------------
       
   334 //
       
   335 GLDEF_C TInt E32Main()
       
   336     {
       
   337     return CCRServer::StartThread();
       
   338     }
       
   339     
       
   340 // --------------------------------------------------------------------------
       
   341 // CCRServer::CustomSecurityCheckL()
       
   342 // --------------------------------------------------------------------------
       
   343 //
       
   344 CPolicyServer::TCustomResult CCRServer::CustomSecurityCheckL(
       
   345     const RMessage2& aMsg,
       
   346     TInt& /*aAction*/,
       
   347     TSecurityInfo& /*aMissing*/ )
       
   348     {
       
   349     
       
   350     TCustomResult retVal ( EFail );
       
   351     
       
   352     // Check the messagge function range
       
   353     if ( ( aMsg.Function() > ECRServBase && 
       
   354            aMsg.Function() < ECRServLastEnum ) ||
       
   355          ( aMsg.Function() > ECRLiveTvBase && 
       
   356            aMsg.Function() < ECRLiveTvLastEnum ) )
       
   357         {
       
   358         // Check if the client has required capabilities                
       
   359         // From .mmp-file following are CAP_APPLICATION capabilities
       
   360         if( ! aMsg.HasCapability(ECapabilityNetworkServices ) ||
       
   361             ! aMsg.HasCapability(ECapabilityLocalServices ) ||
       
   362             ! aMsg.HasCapability(ECapabilityLocation ) ||
       
   363             ! aMsg.HasCapability(ECapabilityReadUserData ) ||
       
   364             ! aMsg.HasCapability(ECapabilityWriteUserData ) ||
       
   365             ! aMsg.HasCapability(ECapabilityReadDeviceData ) ||
       
   366             ! aMsg.HasCapability(ECapabilityWriteDeviceData ) ||
       
   367             ! aMsg.HasCapability(ECapabilitySwEvent ) )
       
   368             {
       
   369             LOG1(
       
   370                 "CCRServer::CustomSecurityCheckL() No capability for message %d!!!",
       
   371                 aMsg.Function() );                 
       
   372             }
       
   373         else
       
   374             {
       
   375             LOG1(
       
   376                 "CCRServer::CustomSecurityCheckL() Message %d inside range and capabilities ok",
       
   377                 aMsg.Function() );
       
   378             retVal = EPass;
       
   379             }        
       
   380         }
       
   381     else
       
   382         {
       
   383         LOG1(
       
   384             "CCRServer::CustomSecurityCheckL() Message %d outside known range!!!",
       
   385             aMsg.Function() );            
       
   386         }
       
   387        
       
   388     return retVal;
       
   389 
       
   390     }
       
   391 // End of File
       
   392