hwrmhaptics/hapticspluginmanager/src/hwrmhapticspolicy.cpp
changeset 0 4e1aa6a622a0
equal deleted inserted replaced
-1:000000000000 0:4e1aa6a622a0
       
     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 "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:  Haptics policy implementation.
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 #include <s32file.h>
       
    20 #include <f32file.h>
       
    21 #include <hwrmhaptics.h> // min and max device priority values
       
    22 #include <pathinfo.h>
       
    23 
       
    24 #include "hwrmhapticspolicy.h"
       
    25 #include "hwrmhapticstrace.h"
       
    26 
       
    27 // Internal policy filename prefix
       
    28 _LIT( KInternalPolicyPrefix, "internal" );
       
    29 
       
    30 //drive char and colon string length, e.g. "c:"
       
    31 const TInt KDriveAndColon = 2; 
       
    32 
       
    33 // Maximum policy file line length
       
    34 const TInt KMaxLineLength( 100 );
       
    35 
       
    36 // New line character used to terminate each line in policy file
       
    37 const TInt KNewLine( '\n' );
       
    38 
       
    39 // format specifier used to check for feedback clients
       
    40 _LIT8( KFeedback, "FEEDBACK" );
       
    41 
       
    42 // format specifier used to match valid UIDs or SIDs
       
    43 _LIT8( KMatchUid, "0x????????" );
       
    44 
       
    45 // ---------------------------------------------------------------------------
       
    46 // Two-phased constructor.
       
    47 // ---------------------------------------------------------------------------
       
    48 //
       
    49 CHWRMHapticsPolicy* CHWRMHapticsPolicy::NewL( const TDesC& aFilename )
       
    50     {
       
    51     CHWRMHapticsPolicy* self = NewLC( aFilename );
       
    52     CleanupStack::Pop( self );
       
    53     
       
    54     return self;
       
    55     }
       
    56 
       
    57 // ---------------------------------------------------------------------------
       
    58 // Two-phased constructor. Leaves instance on the cleanup stack.
       
    59 // ---------------------------------------------------------------------------
       
    60 //
       
    61 CHWRMHapticsPolicy* CHWRMHapticsPolicy::NewLC( const TDesC& aFilename )
       
    62     {
       
    63     CHWRMHapticsPolicy* self = new( ELeave ) CHWRMHapticsPolicy();
       
    64     
       
    65     CleanupStack::PushL( self );
       
    66     self->ConstructL( aFilename );
       
    67     
       
    68     return self;
       
    69     }
       
    70 
       
    71 // ---------------------------------------------------------------------------
       
    72 // Destructor
       
    73 // ---------------------------------------------------------------------------
       
    74 //
       
    75 CHWRMHapticsPolicy::~CHWRMHapticsPolicy()
       
    76     {
       
    77     // Delete all the array objects
       
    78     iClientArray.ResetAndDestroy();
       
    79     iClientArray.Close();
       
    80     }
       
    81 
       
    82 // ---------------------------------------------------------------------------
       
    83 // Method to retrieve the priority of a client with the given ID and if
       
    84 // it is trusted or not.
       
    85 // ---------------------------------------------------------------------------
       
    86 //
       
    87 void CHWRMHapticsPolicy::GetPriority( const TSecureId aSid, 
       
    88                                       TBool& aTrusted, 
       
    89                                       TInt& aPriority ) const
       
    90     {
       
    91     TInt ret( KErrNotFound );
       
    92     
       
    93     // by default not found, i.e. not trusted
       
    94     aTrusted = EFalse;
       
    95     
       
    96     // find the client
       
    97     TInt pos = FindClient( aSid );
       
    98     
       
    99     if( pos != KErrNotFound )
       
   100         {
       
   101         // if client exists return the priority
       
   102         ret = iClientArray[pos]->Priority();
       
   103         aTrusted = ETrue;
       
   104         }
       
   105 
       
   106     aPriority = ret;
       
   107     }
       
   108 
       
   109 // ---------------------------------------------------------------------------
       
   110 // C++ constructor
       
   111 // ---------------------------------------------------------------------------
       
   112 //
       
   113 CHWRMHapticsPolicy::CHWRMHapticsPolicy()
       
   114     {
       
   115     }
       
   116 
       
   117 // ---------------------------------------------------------------------------
       
   118 // By default Symbian 2nd phase constructor is private.
       
   119 // ---------------------------------------------------------------------------
       
   120 //
       
   121 void CHWRMHapticsPolicy::ConstructL( const TDesC& aFilename )
       
   122     {
       
   123     RFs fs;
       
   124     User::LeaveIfError( fs.Connect() );
       
   125     CleanupClosePushL( fs );
       
   126     
       
   127     // Buffer to store the full path and filename
       
   128     TFileName filename;
       
   129     TFileName internalFilename;
       
   130     
       
   131     // Get private file path
       
   132     User::LeaveIfError( fs.PrivatePath( filename ) );
       
   133 
       
   134     // add the filename to path
       
   135     internalFilename.Append( PathInfo::RomRootPath().Left( KDriveAndColon ) );
       
   136     internalFilename.Append( filename ); // private-folder of haptics server
       
   137     internalFilename.Append( KInternalPolicyPrefix ); // "Internal" -prefix
       
   138     internalFilename.Append( aFilename ); // actual filename
       
   139     filename.Insert( 0, PathInfo::RomRootPath().Left( KDriveAndColon ) );
       
   140     filename.Append( aFilename );
       
   141     
       
   142     // Parse the file and construct the array of policy clients
       
   143     // Add product specific file first if they need to change priorieties of
       
   144     // S60 defined processes.
       
   145     ParsePriorityFileL( fs, filename );
       
   146     ParsePriorityFileL( fs, internalFilename );
       
   147 
       
   148     CleanupStack::PopAndDestroy( &fs );
       
   149     }
       
   150 
       
   151 // ---------------------------------------------------------------------------
       
   152 // Method constructs the array of policy clients from the 
       
   153 // given file 
       
   154 // A valid line in the file contains priority, id and app name
       
   155 // priority must be a valid integer from KLowestPriority to KHighestPriority.
       
   156 // id must be a valid SID or ALL indicating the priority for all clients 
       
   157 //      not specified in the file.
       
   158 // app name can be any string indicating the name of the client.
       
   159 // ---------------------------------------------------------------------------
       
   160 //
       
   161 void CHWRMHapticsPolicy::ParsePriorityFileL( RFs& aFs, 
       
   162                                              const TDesC& aFilename )
       
   163     {
       
   164     TLex8 lex;
       
   165     TInt priority;
       
   166     TPtrC8 id;
       
   167     TSecureId sid;
       
   168     TPtrC8 feedback;
       
   169     TBool feedbackClient;
       
   170     TPtrC8 appname( KNullDesC8 );
       
   171     TInt err( KErrNone );
       
   172     
       
   173     // Buffer to read each line of the file into
       
   174     TBuf8<KMaxLineLength> fileline;
       
   175     TChar newLine( KNewLine );  
       
   176     RFileReadStream stream;    
       
   177     
       
   178     COMPONENT_TRACE( ( _L( "CHWRMHapticsPolicy::ParsePriorityFileL - Opening policy file: %S" ), &aFilename ) );
       
   179     
       
   180     // Open the file and attach to stream 
       
   181     err = stream.Open( aFs, aFilename, EFileRead );
       
   182     
       
   183     // Return without error if file is not found
       
   184     if ( err != KErrNone )
       
   185         {
       
   186         COMPONENT_TRACE( ( _L( "CHWRMHapticsPolicy::ParsePriorityFileL - Policy file open failed: %S, error: %d" ), &aFilename, err ) );
       
   187         }
       
   188     else
       
   189         {
       
   190         CleanupClosePushL( stream );
       
   191        
       
   192         while( err != KErrEof )
       
   193             {
       
   194             // read from the file upto the newline character
       
   195             TRAP( err, stream.ReadL( fileline, newLine ) );
       
   196 
       
   197             lex.Assign( fileline );
       
   198             if( lex.Val( priority ) == KErrNone )
       
   199                 {
       
   200                 // got the priority now validate it
       
   201                 if( priority >= KHWRMHapticsMinDevicePriority &&
       
   202                     priority <= KHWRMHapticsMaxDevicePriority )
       
   203                     {
       
   204                     // get the id
       
   205                     id.Set( lex.NextToken() );
       
   206 
       
   207                     // convert it
       
   208                     if( ConvertId( id, sid ) != KErrCorrupt )
       
   209                         {
       
   210                         feedbackClient = EFalse;
       
   211                         
       
   212                         // check whether feedback client (may be empty)
       
   213                         feedback.Set( lex.NextToken() );
       
   214                         if( feedback.MatchF( KFeedback ) == 0 )
       
   215                             {
       
   216                             feedbackClient = ETrue;
       
   217                             }
       
   218                         
       
   219                         // now get the appname (may be empty)
       
   220                         appname.Set( lex.NextToken() );
       
   221                         
       
   222                         // if last token was empty and previous not, but it was
       
   223                         // not feedback token, then it must be appname
       
   224                         if( appname.Size() == 0 && 
       
   225                             ( feedback.Size() != 0 && !feedbackClient ) )
       
   226                             {
       
   227                             appname.Set( feedback );
       
   228                             }
       
   229                         
       
   230                         // Create the policy
       
   231                         AddPolicyClientL( priority, sid, 
       
   232                                           feedbackClient, appname );
       
   233                         }
       
   234                     }
       
   235                 }
       
   236             }
       
   237 
       
   238         // Close stream
       
   239         CleanupStack::PopAndDestroy( &stream );
       
   240         }
       
   241     }
       
   242 
       
   243 // ---------------------------------------------------------------------------
       
   244 // Helper method to convert and validate SID from a descriptor
       
   245 // ---------------------------------------------------------------------------
       
   246 //
       
   247 TInt CHWRMHapticsPolicy::ConvertId( const TDesC8& aSidDes, 
       
   248                                     TSecureId& aSid ) const
       
   249     {
       
   250     TInt ret( KErrNone );
       
   251     TUint32 id;
       
   252 
       
   253     if( aSidDes.Match( KMatchUid ) == 0 )
       
   254         {
       
   255         // this is a matching id
       
   256         TLex8 lex( aSidDes.Right( 8 ) );
       
   257         
       
   258         if( lex.Val( id, EHex ) != KErrNone )
       
   259             {
       
   260             // Failed to convert to int
       
   261             ret = KErrCorrupt;
       
   262             }
       
   263         else
       
   264             {
       
   265             // conversion ok so set the sid
       
   266             aSid.iId = id;
       
   267             }
       
   268         }
       
   269     else
       
   270         {
       
   271         ret = KErrCorrupt;
       
   272         }
       
   273 
       
   274     return ret;
       
   275     }
       
   276 
       
   277 // ---------------------------------------------------------------------------
       
   278 // Adds a policy client to the array, first checks that no other 
       
   279 // client has been registered with the identical sid.
       
   280 // If one already exists KErrAlreadyExists is returned.
       
   281 // ---------------------------------------------------------------------------
       
   282 //
       
   283 TInt CHWRMHapticsPolicy::AddPolicyClientL( const TInt aPriority, 
       
   284                                            const TSecureId& aSid,
       
   285                                            const TBool aFeedbackClient,
       
   286                                            const TDesC8& aAppName )
       
   287     {
       
   288     TInt ret( KErrNone );
       
   289     
       
   290     // search for existing client
       
   291     if( FindClient( aSid ) == KErrNotFound )
       
   292         {
       
   293         // does not already exist so create and add to the array
       
   294         COMPONENT_TRACE( ( _L( "CHWRMHapticsPolicy::AddPolicyClientL - adding client with sid=0x%x" ), aSid.iId ) );
       
   295 
       
   296         // create new client to be added to the array and put on cleanup stack
       
   297         CPolicyClient* ptrClient = CPolicyClient::NewLC( aPriority, aSid,
       
   298                                                          aFeedbackClient,
       
   299                                                          aAppName );
       
   300         
       
   301         // append the new client to the array
       
   302         iClientArray.AppendL( ptrClient );
       
   303 
       
   304         // array now owns the new client so pop it off the cleanup stack
       
   305         CleanupStack::Pop( ptrClient );
       
   306         }
       
   307     else
       
   308         {
       
   309         // client already exists so return KErrAlreadyExists
       
   310         COMPONENT_TRACE( ( _L( "CHWRMHapticsPolicy::AddPolicyClientL - client already exists with sid=0x%x" ), aSid.iId ) );
       
   311         ret = KErrAlreadyExists;
       
   312         }
       
   313     
       
   314     return ret;
       
   315     }
       
   316 
       
   317 // ---------------------------------------------------------------------------
       
   318 // Searches for client with the given sid.
       
   319 // ---------------------------------------------------------------------------
       
   320 //
       
   321 TInt CHWRMHapticsPolicy::FindClient( const TSecureId& aSid ) const
       
   322     {
       
   323     TInt index = KErrNotFound;
       
   324     
       
   325     for( TInt i = 0; i < iClientArray.Count() && index == KErrNotFound; i++ )
       
   326         {
       
   327         if( aSid == iClientArray[i]->Sid() )
       
   328             {
       
   329             COMPONENT_TRACE( ( _L( "CHWRMHapticsPolicy::FindClient - client found with sid=0x%x" ), aSid.iId ) );
       
   330             index = i;
       
   331             }
       
   332         }
       
   333     
       
   334     return index;
       
   335     }
       
   336 
       
   337 // ====================== Embedded CPolicyClient class =======================
       
   338 
       
   339 // ---------------------------------------------------------------------------
       
   340 // Two-phased constructor.
       
   341 // ---------------------------------------------------------------------------
       
   342 //
       
   343 CHWRMHapticsPolicy::CPolicyClient* 
       
   344 CHWRMHapticsPolicy::CPolicyClient::NewL( const TInt aPriority,
       
   345                                          const TSecureId& aSid,
       
   346                                          const TBool aFeedbackClient,
       
   347                                          const TDesC8& aAppName )
       
   348     {
       
   349     CHWRMHapticsPolicy::CPolicyClient* self = NewLC( aPriority, aSid, 
       
   350                                                      aFeedbackClient,
       
   351                                                      aAppName );
       
   352     CleanupStack::Pop( self );
       
   353     return self;
       
   354     }
       
   355 
       
   356 // ---------------------------------------------------------------------------
       
   357 // Two-phased constructor. Leaves instance on the cleanup stack.
       
   358 // ---------------------------------------------------------------------------
       
   359 //
       
   360 CHWRMHapticsPolicy::CPolicyClient*
       
   361 CHWRMHapticsPolicy::CPolicyClient::NewLC( const TInt aPriority, 
       
   362                                           const TSecureId& aSid,
       
   363                                           const TBool aFeedbackClient, 
       
   364                                           const TDesC8& aAppName)
       
   365     {
       
   366     CHWRMHapticsPolicy::CPolicyClient* self =
       
   367         new( ELeave ) CHWRMHapticsPolicy::CPolicyClient( aPriority, aSid, 
       
   368                                                          aFeedbackClient );
       
   369     
       
   370     CleanupStack::PushL( self );
       
   371     self->ConstructL( aAppName );
       
   372     
       
   373     return self;
       
   374     }
       
   375 
       
   376 // ---------------------------------------------------------------------------
       
   377 // Destructor.
       
   378 // ---------------------------------------------------------------------------
       
   379 //
       
   380 CHWRMHapticsPolicy::CPolicyClient::~CPolicyClient()
       
   381     {
       
   382     delete iAppName;
       
   383     }
       
   384 
       
   385 // ---------------------------------------------------------------------------
       
   386 // Returns the clients priority.
       
   387 // ---------------------------------------------------------------------------
       
   388 //
       
   389 TInt CHWRMHapticsPolicy::CPolicyClient::Priority() const
       
   390     {
       
   391     return iPriority;
       
   392     }
       
   393 
       
   394 // ---------------------------------------------------------------------------
       
   395 // Returns the clients ID.
       
   396 // ---------------------------------------------------------------------------
       
   397 //
       
   398 TSecureId CHWRMHapticsPolicy::CPolicyClient::Sid() const
       
   399     {
       
   400     return iSid;
       
   401     }
       
   402 
       
   403 // ---------------------------------------------------------------------------
       
   404 // Returns ETrue if feedback client.
       
   405 // ---------------------------------------------------------------------------
       
   406 //
       
   407 TBool CHWRMHapticsPolicy::CPolicyClient::FeedbackClient() const
       
   408     {
       
   409     return iFeedbackClient;
       
   410     }
       
   411 
       
   412 // ---------------------------------------------------------------------------
       
   413 // Returns the client application name.
       
   414 // ---------------------------------------------------------------------------
       
   415 //
       
   416 const TDesC& CHWRMHapticsPolicy::CPolicyClient::AppName() const
       
   417     {
       
   418     return *iAppName;
       
   419     }
       
   420 
       
   421 // ---------------------------------------------------------------------------
       
   422 // C++ constructor.
       
   423 // ---------------------------------------------------------------------------
       
   424 //
       
   425 CHWRMHapticsPolicy::CPolicyClient::CPolicyClient( TInt aPriority, 
       
   426                                                   const TSecureId& aSid,
       
   427                                                   const TBool aFeedbackClient)
       
   428     : iPriority( aPriority ), iSid( aSid ), iFeedbackClient( aFeedbackClient )
       
   429     {
       
   430     }
       
   431 
       
   432 // ---------------------------------------------------------------------------
       
   433 // By default Symbian 2nd phase constructor is private.
       
   434 // ---------------------------------------------------------------------------
       
   435 //
       
   436 void CHWRMHapticsPolicy::CPolicyClient::ConstructL( const TDesC8& aAppName )
       
   437     {
       
   438     // create the descriptor for the appname
       
   439     iAppName = HBufC::NewL( aAppName.Length() );
       
   440     
       
   441     // and copy the 8 bit descriptor into it
       
   442     iAppName->Des().Copy( aAppName );
       
   443     }
       
   444 
       
   445 // End of File