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