psmservices/psmserver/src/server/psmsrvsession.cpp
branchRCL_3
changeset 22 8cb079868133
parent 21 ccb4f6b3db21
equal deleted inserted replaced
21:ccb4f6b3db21 22:8cb079868133
    12 * Contributors:
    12 * Contributors:
    13 *
    13 *
    14 * Description:  PSM Server session
    14 * Description:  PSM Server session
    15 *
    15 *
    16 */
    16 */
    17 #include <centralrepository.h>
    17 
    18 #include <psmsrvdomaincrkeys.h>
       
    19 #include <e32property.h>
    18 #include <e32property.h>
    20 #include <connect/sbdefs.h>
    19 #include <connect/sbdefs.h>
    21 #include <psmsettingsprovider.h>
    20 #include <psmsettingsprovider.h>
    22 #include "psmmanager.h"
    21 #include "psmmanager.h"
    23 #include "psmsrvserver.h"
    22 #include "psmsrvserver.h"
   110     else
   109     else
   111         {
   110         {
   112         COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::~CPsmSession - No pending requests") ) );
   111         COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::~CPsmSession - No pending requests") ) );
   113         }
   112         }
   114 
   113 
       
   114     // Reset and close config array
       
   115     iConfigArray.Reset();
       
   116     iConfigArray.Close();
   115 
   117 
   116     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::~CPsmSession - return") ) );
   118     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::~CPsmSession - return") ) );
   117     }
   119     }
   118 
   120 
   119 // -----------------------------------------------------------------------------
   121 // -----------------------------------------------------------------------------
   121 // -----------------------------------------------------------------------------
   123 // -----------------------------------------------------------------------------
   122 //
   124 //
   123 void CPsmSession::ServiceL( const RMessage2& aMessage )
   125 void CPsmSession::ServiceL( const RMessage2& aMessage )
   124     {
   126     {
   125     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceL()" ) ) );
   127     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceL()" ) ) );
       
   128 
       
   129     TRAPD( error, DispatchL( aMessage ) );
       
   130 
       
   131     // Complete message with error code if there were errors
       
   132     if ( KErrNone != error )
       
   133         {
       
   134         COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceL() - Error: %i" ), error ) );
       
   135         aMessage.Complete( error );
       
   136         }
       
   137 
       
   138     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceL - return" ) ) );
       
   139     }
       
   140 
       
   141 // -----------------------------------------------------------------------------
       
   142 // CPsmSession::DispatchL
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 void CPsmSession::DispatchL( const RMessage2& aMessage )
       
   146     {
       
   147     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::DispatchL()" ) ) );
       
   148 
   126     switch ( aMessage.Function() )
   149     switch ( aMessage.Function() )
   127         {
   150         {
   128         case EPsmServerNotifyModeChange: // For power save mode change
   151         case EPsmServerNotifyModeChange: // For power save mode change
   129             {
   152             {
   130             TInt mode = aMessage.Int0();
   153             TInt mode = aMessage.Int0();
   131 			if ( !iNotifyModeMessage )
   154             if ( !iNotifyModeMessage )
   132 				{
   155                 {
   133 				// Create message wrapper if not yet created
   156                 // Create message wrapper if not yet created
   134 				CPsmMessage* notifyModeMessage = CPsmMessage::NewL( iPsmManager );
   157                 iNotifyModeMessage = CPsmMessage::NewL( iPsmManager );
   135 				CleanupStack::PushL(notifyModeMessage);
   158                 iPsmManager.RegisterObserver( iNotifyModeMessage );
   136 				iPsmManager.RegisterObserverL( notifyModeMessage );
   159                 }
   137 				CleanupStack::Pop(notifyModeMessage);
   160 
   138 				iNotifyModeMessage = notifyModeMessage;
   161             // Check that mode to be set is valid, otherwise error code.
   139 				}
   162             if( mode == iPsmManager.SettingsProvider().Mode() )
   140 				
       
   141 			//Check whether the mode is already set .
       
   142 			TInt cenrepMode( 0 );
       
   143 			CRepository* cenrep = CRepository::NewLC( KCRUidPowerSaveMode );
       
   144 			User::LeaveIfError(cenrep->Get( KPsmCurrentMode, cenrepMode ));
       
   145 			CleanupStack::PopAndDestroy( cenrep );
       
   146 			// Check that mode to be set is valid, otherwise error code.
       
   147             if( mode == cenrepMode )
       
   148                 {
   163                 {
   149                 User::Leave( KErrAlreadyExists );
   164                 User::Leave( KErrAlreadyExists );
   150                 }
   165                 }
   151 
   166 
   152             if( ( mode > EPsmsrvPartialMode ) || ( mode < EPsmsrvModeNormal ) )
   167             if( ( mode > EPsmsrvPartialMode ) || ( mode < EPsmsrvModeNormal ) )
   171             }
   186             }
   172         case EPsmServerChangeSettings: // For PSM settings
   187         case EPsmServerChangeSettings: // For PSM settings
   173         case EPsmServerGetSettings: // For PSM settings
   188         case EPsmServerGetSettings: // For PSM settings
   174         case EPsmServerBackupSettings: // For PSM settings
   189         case EPsmServerBackupSettings: // For PSM settings
   175             {
   190             {
       
   191             // Check previous config
       
   192             if ( iConfigArray.Count() > 0 )
       
   193                 {
       
   194                 iConfigArray.Reset();
       
   195                 }
       
   196 
   176             // Handle settings requests in different function
   197             // Handle settings requests in different function
   177             HandleSettingsRequestL( aMessage );
   198             HandleSettingsRequestL( aMessage );
   178             break;
   199             break;
   179             }
   200             }
   180 
   201 
   201         case EPsmServerModeChangeNotificationRequest: // PSM Change notification request
   222         case EPsmServerModeChangeNotificationRequest: // PSM Change notification request
   202             {
   223             {
   203             if ( !iNotifyModeMessage )
   224             if ( !iNotifyModeMessage )
   204                 {
   225                 {
   205                 // Create message wrapper if not yet created
   226                 // Create message wrapper if not yet created
   206                 CPsmMessage* notifyModeMessage = CPsmMessage::NewL( iPsmManager );
   227                 iNotifyModeMessage = CPsmMessage::NewL( iPsmManager );
   207 				CleanupStack::PushL(notifyModeMessage);
   228                 iPsmManager.RegisterObserver( iNotifyModeMessage );
   208 				iPsmManager.RegisterObserverL( notifyModeMessage );
       
   209 				CleanupStack::Pop(notifyModeMessage);
       
   210 				iNotifyModeMessage = notifyModeMessage;
       
   211                 }
   229                 }
   212             iNotifyModeMessage->Initialize( aMessage );
   230             iNotifyModeMessage->Initialize( aMessage );
   213             break;
   231             break;
   214             }
   232             }
   215         case EPsmServerCancelModeChangeNotificationRequest: // For cancelling PSM change notification
   233         case EPsmServerCancelModeChangeNotificationRequest: // For cancelling PSM change notification
   227                 }
   245                 }
   228             // Complete message
   246             // Complete message
   229             aMessage.Complete( KErrNone );
   247             aMessage.Complete( KErrNone );
   230             break;
   248             break;
   231             }
   249             }
   232 #ifdef _DEBUG    
       
   233         case EDebugHeapMark:
       
   234             {        
       
   235             COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession - EDebugMarkHeap: Alloc Cells: %d."), User::CountAllocCells()));            
       
   236             __UHEAP_MARK;
       
   237             aMessage.Complete(KErrNone);
       
   238             break;
       
   239             }
       
   240             
       
   241         case EDebugHeapMarkEnd:
       
   242             {
       
   243             COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession - EDebugMarkHeapEnd: Alloc Cells: %d."), User::CountAllocCells()));
       
   244             if ( iNotifyModeMessage )
       
   245                 {
       
   246                 // Unregister observer from manager
       
   247                 iPsmManager.UnregisterObserver( iNotifyModeMessage );
       
   248                 // Compress the observer array
       
   249                 iPsmManager.CompressModeObserversArray();
       
   250                 // Finally delete message
       
   251                 delete iNotifyModeMessage;
       
   252                 iNotifyModeMessage = NULL;
       
   253                 }
       
   254             iPsmManager.ClosePluginLoader();
       
   255             __UHEAP_MARKEND;
       
   256             aMessage.Complete(KErrNone);
       
   257             break;
       
   258             }
       
   259             
       
   260         case EDebugSetHeapFailure:
       
   261             {
       
   262             COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession - EDebugSetHeapFailure: Value of Failure Rate: %d."), aMessage.Int0()));            
       
   263             __UHEAP_SETFAIL(RAllocator::EFailNext,aMessage.Int0());
       
   264             aMessage.Complete(KErrNone);
       
   265             break;
       
   266             }
       
   267             
       
   268         case EDebugHeapReset:
       
   269             {
       
   270             COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession - EDebugHeapReset ")));            
       
   271             __UHEAP_RESET;
       
   272             aMessage.Complete(KErrNone);
       
   273             break;
       
   274             }            
       
   275 #endif
       
   276         default:
   250         default:
   277             {
   251             {
   278             COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceL() - unknown request: %i - ERROR" ), aMessage.Function() ) );
   252             COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceL() - unknown request: %i - ERROR" ), aMessage.Function() ) );
   279             User::Leave( KErrUnknown );
   253             User::Leave( KErrUnknown );
   280             }
   254             }
   281         }
   255         }
   282     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceL - return" ) ) );
   256 
   283     }
   257     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::DispatchL - return" ) ) );
   284 
   258     }
   285 // -----------------------------------------------------------------------------
       
   286 // CPsmSession::ServiceError
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 void CPsmSession::ServiceError(const RMessage2 &aMessage, TInt aError)
       
   290     {
       
   291     // Complete message with error code if there were errors
       
   292     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ServiceError() - Error: %i" ), aError ) );
       
   293     if (iNotifyModeMessage && (aMessage.Handle() == iNotifyModeMessage->MessageHandle()) )
       
   294         {
       
   295         iNotifyModeMessage->Complete( aError );           
       
   296         }
       
   297     else
       
   298         {
       
   299         aMessage.Complete( aError );
       
   300         }
       
   301     }
       
   302 
       
   303 
   259 
   304 // -----------------------------------------------------------------------------
   260 // -----------------------------------------------------------------------------
   305 // CPsmSession::HandleSettingsRequestL
   261 // CPsmSession::HandleSettingsRequestL
   306 // -----------------------------------------------------------------------------
   262 // -----------------------------------------------------------------------------
   307 //
   263 //
   308 void CPsmSession::HandleSettingsRequestL( const RMessage2& aMessage )
   264 void CPsmSession::HandleSettingsRequestL( const RMessage2& aMessage )
   309     {
   265     {
   310     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL()" ) ) );
   266     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL()" ) ) );
   311 
   267 
   312     RConfigInfoArray configArray;
       
   313     CleanupClosePushL(configArray);
       
   314     // read config array from message
   268     // read config array from message
   315     ReadConfigArrayL( configArray, aMessage );
   269     ReadConfigArrayL( iConfigArray, aMessage );
   316     // Get storage UID
   270     // Get storage UID
   317     TUint32 storage = aMessage.Int2();
   271     TUint32 storage = aMessage.Int2();
   318 
   272 
   319     TInt err( KErrNone );
   273     TInt err( KErrNone );
   320 
   274 
   321     switch ( aMessage.Function() )
   275     if ( aMessage.Function() == EPsmServerBackupSettings )
   322         {
   276         {
   323         case EPsmServerBackupSettings: // For server backup settings
   277         iPsmManager.SettingsProvider().BackupSettingsL( iConfigArray, storage );
   324             {
   278         }
   325             iPsmManager.SettingsProvider().BackupSettingsL( configArray, storage );
   279     else 
   326             break;
   280         {
   327             }
   281         if ( aMessage.Function() == EPsmServerGetSettings )
   328         case EPsmServerGetSettings: // For getting server settingspower save mode change
   282             {
   329             {
   283             iPsmManager.SettingsProvider().GetSettingsL( iConfigArray, storage );
   330             iPsmManager.SettingsProvider().GetSettingsL( configArray, storage );
   284             }
   331             // check whether the configArray is empty before writing the value back to message.
   285         else
   332             if(configArray.Count() > 0)
   286             {
   333                 {
   287             // EPsmServerChangeSettings
   334                 // write changed values back to message
   288             iPsmManager.SettingsProvider().BackupAndGetSettingsL( iConfigArray, storage );
   335                 TInt arraySize( configArray.Count() * sizeof( TPsmsrvConfigInfo ) );
   289             }
   336                 TPtr8 arrayPtr( reinterpret_cast<TUint8*>(&configArray[0]), arraySize, arraySize );
   290 
   337                 err = aMessage.Write( 0, arrayPtr );
   291         // We have to write changed values back to message
   338                 COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL - New data wrote to message: %i" ), err ) );
   292         TInt arraySize( iConfigArray.Count() * sizeof( TPsmsrvConfigInfo ) );
   339                 }
   293         TPtr8 arrayPtr( reinterpret_cast<TUint8*>(&iConfigArray[0]), arraySize, arraySize );
   340             else
   294         err = aMessage.Write( 0, arrayPtr );
   341                 {
   295         ERROR_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL - New data wrote to message: %i" ), err ) );
   342                 ERROR_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL - Config Array is Empty" )) );
       
   343                 }
       
   344             break;
       
   345             }
       
   346         case EPsmServerChangeSettings: // For changing the settings
       
   347             {
       
   348             // We have to write changed values back to message
       
   349             iPsmManager.SettingsProvider().BackupAndGetSettingsL( configArray, storage );
       
   350             // We have to write changed values back to message
       
   351             TInt arraySize( configArray.Count() * sizeof( TPsmsrvConfigInfo ) );
       
   352             TPtr8 arrayPtr( reinterpret_cast<TUint8*>(&configArray[0]), arraySize, arraySize );
       
   353             err = aMessage.Write( 0, arrayPtr );
       
   354             ERROR_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL - New data wrote to message: %i" ), err ) );
       
   355             break;
       
   356             }
       
   357         default:
       
   358             {
       
   359             COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL() - unknown request: %i - ERROR" ), aMessage.Function() ) );
       
   360             User::Leave( KErrUnknown );
       
   361             }
       
   362         }
   296         }
   363     // Complete message before destroying local config array
   297     // Complete message before destroying local config array
   364     aMessage.Complete( err );
   298     aMessage.Complete( err );
   365 			
       
   366     CleanupStack::PopAndDestroy(&configArray);
       
   367 
       
   368     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL - return" ) ) );
   299     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::HandleSettingsRequestL - return" ) ) );
   369     }
   300     }
   370 
   301 
   371 // -----------------------------------------------------------------------------
   302 // -----------------------------------------------------------------------------
   372 // CPsmSession::ReadConfigArrayL
   303 // CPsmSession::ReadConfigArrayL
   377     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ReadConfigArrayL()" ) ) );
   308     COMPONENT_TRACE( ( _L( "PSM Server - CPsmSession::ReadConfigArrayL()" ) ) );
   378 
   309 
   379     const TInt configCount( aMessage.Int1() );
   310     const TInt configCount( aMessage.Int1() );
   380     TInt arrayLength( configCount * sizeof( TPsmsrvConfigInfo ) );
   311     TInt arrayLength( configCount * sizeof( TPsmsrvConfigInfo ) );
   381 
   312 
   382     aArray.ReserveL(configCount);    
   313     // There is no ResizeL() for RArray
   383     //Fill the array with dummy items
   314     // ReserveL() does not change iCount, which will
       
   315     // result array[0] failure (access beyond array index)
       
   316     // That is why we fill the array with dummy items
   384     for(TInt x = 0; x < configCount; ++x )
   317     for(TInt x = 0; x < configCount; ++x )
   385     	{
   318     	{
   386         // Append empty config infos to array
   319         // Append empty config infos to array
   387     	aArray.AppendL( TPsmsrvConfigInfo() );
   320     	aArray.AppendL( TPsmsrvConfigInfo() );
   388     	}
   321     	}