mediator/src/Server/MediatorServer.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     1 /*
       
     2 * Copyright (c) 2005 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:  Main functions to start the server
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include    <e32base.h>
       
    20 #include    <barsc.h>       // RResourceFile
       
    21 #include    <barsread.h>    // TResourceReader
       
    22 
       
    23 #include    "MediatorServer.h"
       
    24 #include    "MediatorCommon.h"
       
    25 #include    "MediatorServerSession.h"
       
    26 #include    "MediatorServerEventHandler.h"
       
    27 #include    "MediatorServerCommandHandler.h"
       
    28 #include    "MediatorServerObjectHandler.h"
       
    29 #include    "MediatorServerPluginHandler.h"
       
    30 
       
    31 #include    <allowedsids.rsg>
       
    32 #include    "MediatorDebug.h"
       
    33 #include    "Debug.h"
       
    34 
       
    35 // CONSTANTS
       
    36 // Resource directory
       
    37 _LIT(KResourceEventDir, "z:\\private\\10207449\\events\\");
       
    38 
       
    39 // Resource file containing exceptionally handled SIDs
       
    40 _LIT(KAllowedSidsFile, "z:\\private\\10207449\\allowedsids.rsc");
       
    41 
       
    42 // Domain resource index ( = 1)
       
    43 const TInt KEventResourceIndex  = 1;
       
    44 
       
    45 // Version number count within TVersion structure
       
    46 const TInt KVersionNumberCount  = 3;
       
    47 
       
    48 // NAMESPACE
       
    49 using namespace MediatorService;
       
    50 
       
    51 // ============================ MEMBER FUNCTIONS ===============================
       
    52 
       
    53 // -----------------------------------------------------------------------------
       
    54 // CMediatorServer::CMediatorServer
       
    55 // -----------------------------------------------------------------------------
       
    56 //
       
    57 CMediatorServer::CMediatorServer()
       
    58 	: CPolicyServer( CActive::EPriorityHigh, KMediatorServerPolicy )
       
    59 	{
       
    60 	LOG(_L("[Mediator Server]\t CMediatorServer::CMediatorServer"));
       
    61 	}
       
    62 
       
    63 // -----------------------------------------------------------------------------
       
    64 // CMediatorServer::ConstructL
       
    65 // -----------------------------------------------------------------------------
       
    66 //
       
    67 void CMediatorServer::ConstructL()
       
    68     {
       
    69     LOG(_L("[Mediator Server]\t CMediatorServer::ConstructL"));
       
    70     StartL( KMediatorServerName );
       
    71     
       
    72     // Create object, event and command handlers
       
    73     iObjectHandler = CMediatorServerObjectHandler::NewL();
       
    74     iEventHandler = CMediatorServerEventHandler::NewL( *iObjectHandler );
       
    75     iCommandHandler = CMediatorServerCommandHandler::NewL( *iObjectHandler );
       
    76     iPluginHandler = CMediatorServerPluginHandler::NewL();
       
    77     
       
    78     RFs fsSession;
       
    79     User::LeaveIfError( fsSession.Connect() );
       
    80     CleanupClosePushL( fsSession );
       
    81     
       
    82     // Do static event registrations
       
    83     ProcessResourceEventsL( fsSession );
       
    84     
       
    85     // read SIDs
       
    86     ReadAllowedSidsFileL( fsSession );
       
    87     
       
    88     CleanupStack::PopAndDestroy( &fsSession );
       
    89     
       
    90     RMediatorDebug::Initialize( this );
       
    91     
       
    92     LOG(_L("[Mediator Server]\t CMediatorServer::ConstructL end"));
       
    93     }
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CMediatorServer::NewLC
       
    97 // -----------------------------------------------------------------------------
       
    98 //
       
    99 CMediatorServer* CMediatorServer::NewLC()
       
   100 	{
       
   101 	LOG(_L("[Mediator Server]\t CMediatorServer::NewLC"));
       
   102 	CMediatorServer *self = new (ELeave) CMediatorServer();
       
   103 	CleanupStack::PushL( self );
       
   104 	self->ConstructL();
       
   105 	return self;
       
   106 	}
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CMediatorServer::~CMediatorServer
       
   110 // -----------------------------------------------------------------------------
       
   111 //    
       
   112 CMediatorServer::~CMediatorServer()
       
   113 	{
       
   114 	RMediatorDebug::Uninitialize();
       
   115 	
       
   116 	LOG(_L("[Mediator Server]\t CMediatorServer::~CMediatorServer"));
       
   117 	delete iEventHandler;
       
   118 	delete iCommandHandler;
       
   119 	delete iObjectHandler;
       
   120 	delete iPluginHandler;
       
   121 	iAllowedSids.Close();
       
   122 	}
       
   123 
       
   124 
       
   125 // -----------------------------------------------------------------------------
       
   126 // CMediatorServer::NewSessionL
       
   127 // Creates a new session to the server if version information is correct.
       
   128 // (other items were commented in a header).
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 CSession2 *CMediatorServer::NewSessionL( const TVersion &aVersion, 
       
   132 										 const RMessage2 &/*aMessage*/) const
       
   133 	{
       
   134 	LOG(_L("[Mediator Server]\t CMediatorServer::NewSessionL"));
       
   135 	// check we're the right version
       
   136 	TVersion version( KMediatorServerMajor, 
       
   137 					  KMediatorServerMinor, 
       
   138 					  KMediatorServerBuild);
       
   139 	if( !User::QueryVersionSupported( version, aVersion ) )
       
   140 	    {
       
   141 	    ERROR_LOG(_L("[Mediator] CMediatorServer::NewSessionL: Version conflict!\n") );
       
   142 	    ERROR_TRACE(Print(_L("[Mediator] Current version:%d.%d.%d\n"), version.iMajor, version.iMinor, version.iBuild ) );
       
   143 	    ERROR_TRACE(Print(_L("[Mediator] Connected version:%d.%d.%d\n"), aVersion.iMajor, aVersion.iMinor, aVersion.iBuild ) );
       
   144 	    User::Leave( KErrNotSupported );
       
   145 	    }
       
   146 		
       
   147 	// make new session
       
   148 	return CMediatorServerSession::NewL();
       
   149 	}
       
   150 
       
   151 // -----------------------------------------------------------------------------
       
   152 // CMediatorServer::EventHandler
       
   153 // Returns a reference to event handler
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 CMediatorServerEventHandler& CMediatorServer::EventHandler()
       
   157     {
       
   158     __ASSERT_DEBUG( iEventHandler, PanicServer( EMediatorServerNoEventHandler ));
       
   159     return *iEventHandler;
       
   160     }
       
   161         
       
   162 // -----------------------------------------------------------------------------
       
   163 // CMediatorServer::EventHandler
       
   164 // Returns a reference to command handler
       
   165 // -----------------------------------------------------------------------------
       
   166 //
       
   167 CMediatorServerCommandHandler& CMediatorServer::CommandHandler()
       
   168     {
       
   169     __ASSERT_DEBUG( iCommandHandler, PanicServer( EMediatorServerNoCommandHandler ));
       
   170     return *iCommandHandler;
       
   171     }
       
   172 
       
   173 // -----------------------------------------------------------------------------
       
   174 // CMediatorServer::ObjectHandler
       
   175 // Returns a reference to object handler
       
   176 // -----------------------------------------------------------------------------
       
   177 //
       
   178 CMediatorServerObjectHandler& CMediatorServer::ObjectHandler()
       
   179     {
       
   180     __ASSERT_DEBUG( iObjectHandler, PanicServer( EMediatorServerNoObjectHandler ));
       
   181     return *iObjectHandler;
       
   182     }
       
   183 
       
   184 // -----------------------------------------------------------------------------
       
   185 // CMediatorServer::PluginHandler
       
   186 // Returns a reference to plugin handler
       
   187 // -----------------------------------------------------------------------------
       
   188 //
       
   189 CMediatorServerPluginHandler& CMediatorServer::PluginHandler()
       
   190     {
       
   191     __ASSERT_DEBUG( iPluginHandler, PanicServer( EMediatorServerNoPluginHandler ));
       
   192     return *iPluginHandler;
       
   193     }
       
   194 
       
   195 // -----------------------------------------------------------------------------
       
   196 // CMediatorServer::CustomSecurityCheckL
       
   197 // Performs a custom security check.
       
   198 // -----------------------------------------------------------------------------
       
   199 //
       
   200 
       
   201 CPolicyServer::TCustomResult CMediatorServer::CustomSecurityCheckL( const RMessage2& aMsg, 
       
   202                                                                     TInt& /*aAction*/,
       
   203                                                                     TSecurityInfo& /*aMissing*/ )
       
   204     {
       
   205     LOG(_L("[Mediator Server]\t CMediatorServer::CustomSecurityCheckL"));
       
   206     // firstly check if this SwEvent capability, which is OK
       
   207     if ( aMsg.HasCapability( ECapabilitySwEvent ) )
       
   208         {
       
   209         return EPass;
       
   210         }
       
   211     
       
   212     // secondly check if special treatment for this SID is allowed
       
   213     if ( AllowCapabilityException( static_cast<TUid>( aMsg.SecureId() ) ) )
       
   214         {
       
   215         return EPass;
       
   216         }
       
   217     
       
   218     ERROR_TRACE(Print(_L("[Mediator] CMediatorServer::CustomSecurityCheckL: security check fail for client 0x%08X\n"), aMsg.SecureId().iId));
       
   219 
       
   220     // client has no right to send this message
       
   221     return EFail;
       
   222     }
       
   223 
       
   224 // -----------------------------------------------------------------------------
       
   225 // CMediatorServer::ProcessResourceEventsL
       
   226 // Does static event resource registration processing
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 void CMediatorServer::ProcessResourceEventsL( RFs& aFsSession )
       
   230     {
       
   231     LOG(_L("[Mediator Server]\t CMediatorServer::ProcessResourceEventsL"));
       
   232     
       
   233     // Gather all files from registration directory
       
   234     CDir* resourceList;
       
   235 	
       
   236     TInt err = aFsSession.GetDir( KResourceEventDir,
       
   237 		                         KEntryAttNormal,
       
   238 		                         ESortByName, 
       
   239 		                         resourceList );
       
   240     
       
   241     // If events-directory does not exist, it means that there are no static events registered
       
   242     if ( err != KErrNone && err != KErrPathNotFound )
       
   243         {
       
   244         ERROR_TRACE(Print(_L("[Mediator] CMediatorServer::ProcessResourceEventsL: err=%d\n"), err ) );
       
   245         User::Leave( err );
       
   246         }
       
   247     
       
   248     if ( err != KErrPathNotFound )    		                                  
       
   249         {
       
   250         CleanupStack::PushL( resourceList );
       
   251         TRACE(Print(_L("[Mediator Server]\t ProcessResourceEventsL file count %d\n"), resourceList->Count() ));
       
   252     
       
   253         // Loop through the resource files and process the resources
       
   254         for ( TInt index = 0; index < resourceList->Count(); index++ )
       
   255             {
       
   256             TEntry resourceFile = (*resourceList)[ index ];
       
   257             TBufC<KMaxFileName> fileName = resourceFile.iName;
       
   258             // TRAP error, but continue
       
   259             TRAPD( error, ProcessResourceFileL( aFsSession, fileName ) );
       
   260             
       
   261             if ( error != KErrNone )
       
   262                 {
       
   263                 ERROR_TRACE(Print(_L("[Mediator] CMediatorServer::ProcessResourceEventsL: error=%d\n"), error ) );
       
   264                 }
       
   265                 
       
   266             TRACE(Print(_L("[Mediator Server]\t ProcessResourceEventsL file %d process status %d\n"), index, error )); 
       
   267             }
       
   268         CleanupStack::PopAndDestroy( resourceList );
       
   269         }
       
   270     }
       
   271 
       
   272 // -----------------------------------------------------------------------------
       
   273 // CMediatorServer::ProcessResourceFileL
       
   274 // Processes one resource file
       
   275 // -----------------------------------------------------------------------------
       
   276 //
       
   277 void CMediatorServer::ProcessResourceFileL( RFs& aFsSession, const TDesC& aFileName )
       
   278     {
       
   279     LOG(_L("[Mediator Server]\t CMediatorServer::ProcessResourceFileL"));
       
   280     
       
   281     // Parse full file name
       
   282     TParse parse;
       
   283     parse.Set( aFileName, &KResourceEventDir, NULL );
       
   284     TFileName fullName = parse.FullName();
       
   285     
       
   286     // Get this thread's secure id
       
   287     RThread thread = RThread();
       
   288     TSecureId secureId = thread.SecureId();
       
   289     
       
   290     // Open resource file
       
   291     RResourceFile resourceFile;
       
   292     resourceFile.OpenL( aFsSession, fullName );
       
   293 	CleanupClosePushL( resourceFile );
       
   294 		
       
   295     // Read resource information		
       
   296 	HBufC8* domainResource = resourceFile.AllocReadLC( KEventResourceIndex );
       
   297 	TResourceReader reader;
       
   298 	reader.SetBuffer( domainResource );
       
   299 	
       
   300 	// Read domain from resource
       
   301 	TUid domainUid = TUid::Uid( reader.ReadInt32() );
       
   302 	// Process categories
       
   303 	TInt categoryCount = reader.ReadInt16();
       
   304 	for ( TInt index = 0; index < categoryCount; index++ )
       
   305 	    {
       
   306 	    TUid categoryUid = TUid::Uid( reader.ReadInt32() );
       
   307 	    // Process events within category
       
   308 	    TInt eventCount = reader.ReadInt16();
       
   309 	    REventList eventList;
       
   310 	    for ( TInt index2 = 0; index2 < eventCount; index2++ )
       
   311 	        {
       
   312 	        MediatorService::TEvent newEvent = ReadEvent( reader );
       
   313 	        eventList.AppendL( newEvent );
       
   314 	        }
       
   315 	    // Register events to event handler, if there's something to register
       
   316 	    if ( eventList.Count() > 0 )
       
   317 	        {
       
   318 	        TMediatorCategory newCategory;
       
   319     	    newCategory.iDomain = domainUid;
       
   320     	    newCategory.iCategory = categoryUid;
       
   321     	    // TRAP error, but continue anyway
       
   322     	    TRAPD( error, 
       
   323     	           EventHandler().RegisterEventListL( newCategory, 
       
   324     	                                              eventList, 
       
   325     	                                              secureId ) );
       
   326             
       
   327             if ( error != KErrNone )
       
   328                 {
       
   329                 ERROR_TRACE(Print(_L("[Mediator] CMediatorServer::ProcessResourceFileL: error=%d\n"), error ) );
       
   330                 }
       
   331             
       
   332     	                                              
       
   333     	    TRACE(Print(_L("[Mediator Server]\t ProcessResourceFileL events registered with status %d\n"), error ));                                          
       
   334 	        }  
       
   335 	    eventList.Close();
       
   336 	    }
       
   337 		
       
   338 	CleanupStack::PopAndDestroy( domainResource);
       
   339 	CleanupStack::PopAndDestroy( &resourceFile );
       
   340     }
       
   341 
       
   342 
       
   343 // -----------------------------------------------------------------------------
       
   344 // CMediatorServer::ReadEvent
       
   345 // Reads one event from resource
       
   346 // -----------------------------------------------------------------------------
       
   347 //
       
   348 MediatorService::TEvent CMediatorServer::ReadEvent( TResourceReader& aReader )
       
   349     {
       
   350     MediatorService::TEvent newEvent;
       
   351     
       
   352     // Read Event id
       
   353     newEvent.iEventId =  aReader.ReadInt16();
       
   354     
       
   355     TRACE(Print(_L("[Mediator Server]\t ProcessResourceFileL new event id %d\n"), newEvent.iEventId )); 
       
   356     
       
   357     // Read required capabilities
       
   358     TCapabilitySet capabilitySet;
       
   359     capabilitySet.SetEmpty();
       
   360     TInt capabilityCount = aReader.ReadInt16();
       
   361     for ( TInt index = 0; index < capabilityCount; index++ )
       
   362         {
       
   363         TCapability newCapability = (TCapability) aReader.ReadInt16();
       
   364         capabilitySet.AddCapability( newCapability );  
       
   365         }
       
   366     newEvent.iCaps = capabilitySet;
       
   367     
       
   368     // Read version information
       
   369     TInt versionCount = aReader.ReadInt16();
       
   370     if ( versionCount == KVersionNumberCount ) // Should have 3 values in version structure
       
   371         {
       
   372         TInt major = aReader.ReadInt16();
       
   373         TInt minor = aReader.ReadInt16();
       
   374         TInt build = aReader.ReadInt16();
       
   375         newEvent.iVersion = TVersion( major, minor, build );
       
   376         }
       
   377     else
       
   378         {
       
   379         ERROR_TRACE(Print(_L("[Mediator] CMediatorServer::ReadEvent: Event %d has invalid data, versionCount=%d\n"), newEvent.iEventId, versionCount ) );
       
   380         // Something wrong with the resource --> use zeros as version info
       
   381         newEvent.iVersion = TVersion( 0, 0, 0 );
       
   382         }    
       
   383     // Return event 
       
   384     return newEvent;
       
   385     }
       
   386 
       
   387 // -----------------------------------------------------------------------------
       
   388 // CMediatorServer::ReadAllowedSidsFileL
       
   389 // Read resource file containing exeptionally handled SIDs
       
   390 // -----------------------------------------------------------------------------
       
   391 //
       
   392 void CMediatorServer::ReadAllowedSidsFileL( RFs& aFsSession )
       
   393     {
       
   394     LOG(_L("[Mediator Server]\t CMediatorServer::ReadAllowedSidsFileL"));
       
   395     
       
   396     // read settings from resource files
       
   397     TFileName resourceFileName (KAllowedSidsFile);
       
   398     
       
   399     RResourceFile resourceFile;
       
   400     resourceFile.OpenL(aFsSession, resourceFileName);
       
   401     
       
   402     CleanupClosePushL(resourceFile);
       
   403     
       
   404     // Read the SID structure
       
   405     HBufC8* res = resourceFile.AllocReadLC(SID_INFO);
       
   406     TResourceReader theReader;
       
   407     theReader.SetBuffer(res);
       
   408 
       
   409     TInt sidCount = theReader.ReadUint16();
       
   410     
       
   411     // read SIDs
       
   412     TUid sid;
       
   413         
       
   414     for (TInt i = 0; i < sidCount; i++)
       
   415         {
       
   416         sid.iUid = theReader.ReadUint32();
       
   417         TRACE(Print(_L("[Mediator Server]\t Allowed SID: 0x%08X\n"), sid.iUid)); 
       
   418         User::LeaveIfError( iAllowedSids.Append( sid ) );
       
   419         }    
       
   420     
       
   421     CleanupStack::PopAndDestroy( res );
       
   422     CleanupStack::PopAndDestroy( &resourceFile );
       
   423     }
       
   424 
       
   425 // -----------------------------------------------------------------------------
       
   426 // CMediatorServer::AllowCapabilityException
       
   427 // Returns whether capability requirements can be ignored for the specified client.
       
   428 // -----------------------------------------------------------------------------
       
   429 //
       
   430 TBool CMediatorServer::AllowCapabilityException( const TUid& aSid )
       
   431     {
       
   432     return ( ( iAllowedSids.Find ( aSid ) != KErrNotFound ) ? ETrue : EFalse );
       
   433     }
       
   434 
       
   435     
       
   436 // -----------------------------------------------------------------------------
       
   437 // PanicServer
       
   438 // Server panic handler
       
   439 // Panic our own thread
       
   440 // -----------------------------------------------------------------------------
       
   441 //
       
   442 void CMediatorServer::PanicServer(TInt aPanic)
       
   443     {
       
   444     ERROR_TRACE(Print(_L("[Mediator] CMediatorServer::PanicServer: Reason = %d\n"), aPanic));
       
   445     User::Panic( KMediatorServerPanic, aPanic );
       
   446     }
       
   447 
       
   448 // -----------------------------------------------------------------------------
       
   449 // PanicClient
       
   450 // Client panic handler
       
   451 // RMessage2::Panic() also completes the message. This is:
       
   452 // (a) important for efficient cleanup within the kernel
       
   453 // (b) a problem if the message is completed a second time
       
   454 // (other items were commented in a header).
       
   455 // -----------------------------------------------------------------------------
       
   456 //
       
   457 void CMediatorServer::PanicClient( const RMessage2& aMessage, TInt aPanic )
       
   458     {
       
   459     ERROR_TRACE(Print(_L("[Mediator] CMediatorServer::PanicClient: Reason = %d\n"), aPanic));
       
   460     aMessage.Panic( KMediatorServerPanic, aPanic );
       
   461     }
       
   462 
       
   463 // End of File