fotaapplication/fotaserver/fmserver/src/fmsserversession.cpp
branchRCL_3
changeset 25 b183ec05bd8c
parent 24 13d7c31c74e0
child 26 19bba8228ff0
equal deleted inserted replaced
24:13d7c31c74e0 25:b183ec05bd8c
     1 /*
       
     2 * Copyright (c) 2009 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: Implementation of fotaserver component
       
    15 * 	This is part of fotaapplication.
       
    16 *
       
    17 */
       
    18 
       
    19 #include <f32file.h>
       
    20 #include <cmconnectionmethoddef.h>
       
    21 #include <cmmanagerext.h>
       
    22 #include <cmpluginwlandef.h>
       
    23 #include <cmpluginpacketdatadef.h>
       
    24 #include <etel.h> //for telephone mode
       
    25 #include <etelmm.h> //for telephone mode
       
    26 #include <fotaConst.h> 
       
    27 #include "fmsserversession.h"
       
    28 #include "fmsclientserver.h"
       
    29 
       
    30 // ----------------------------------------------------------------------------------------
       
    31 // CFMSSession::CFMSSession
       
    32 // ----------------------------------------------------------------------------------------
       
    33 CFMSSession::CFMSSession()
       
    34 	{	
       
    35 	iStopServer  =  EFalse;
       
    36 	iWlanbearer  =  EFalse;
       
    37 	iWcdmaBearer =  EFalse;
       
    38 	}
       
    39 
       
    40 // ----------------------------------------------------------------------------------------
       
    41 // CFMSSession::~CFMSSession
       
    42 // ----------------------------------------------------------------------------------------
       
    43 CFMSSession::~CFMSSession()
       
    44 	{	
       
    45 	FLOG(_L("CFMSSession::~CFMSSession-- begin"));
       
    46 	if( iStopServer )
       
    47 		{
       
    48 		FLOG(_L("Stop fmserver CFMSSession::~CFMSSession"));	
       
    49 		Server().DropSession();
       
    50 		}
       
    51 	FLOG(_L("CFMSSession::~CFMSSession-- end"));
       
    52 	}
       
    53 
       
    54 // ----------------------------------------------------------------------------------------
       
    55 // CFMSSession::Server
       
    56 // ----------------------------------------------------------------------------------------
       
    57 CFMSServer& CFMSSession::Server()
       
    58 	{
       
    59 	return *static_cast<CFMSServer*>(const_cast<CServer2*>(CSession2::Server()));
       
    60 	}
       
    61 
       
    62 
       
    63 // ----------------------------------------------------------------------------------------
       
    64 // CFMSSession::CheckClientSecureIdL
       
    65 // ----------------------------------------------------------------------------------------
       
    66 TBool CFMSSession::CheckClientSecureIdL (
       
    67 		const RMessage2& aMessage)
       
    68 	{
       
    69 	TUid    fota            = TUid::Uid( KFotaServerUid );
       
    70 	TUid    starter         = TUid::Uid( KStarterUid );
       
    71 
       
    72 	// Fotaserver
       
    73 	if ( aMessage.SecureId() == fota.iUid ) 
       
    74 		{
       
    75 		FLOG(_L("[CFMSSession] CheckClientSecureIdL client is \
       
    76 		fotaserver!"));
       
    77 		return ETrue;
       
    78 		}
       
    79 
       
    80 	// Starter
       
    81 	if ( aMessage.SecureId() == starter.iUid )
       
    82 		{
       
    83 		return ETrue;
       
    84 		}
       
    85 
       
    86 	// test app
       
    87 	if ( aMessage.SecureId() == 0x01D278B9 )
       
    88 		{
       
    89 		return ETrue;
       
    90 		}
       
    91 
       
    92 	FLOG(_L("[CFMSSession] CheckClientSecureIdL client 0x%X is unknown \
       
    93 	(msg %d), bailing out"),aMessage.SecureId().iId, aMessage.Function());
       
    94 	iStopServer = ETrue;
       
    95 	User::Leave( KErrAccessDenied );    
       
    96 	return EFalse; // compiler warning otherwise
       
    97 	}
       
    98 
       
    99 // ----------------------------------------------------------------------------------------
       
   100 // CFMSSession::ServiceL
       
   101 // ----------------------------------------------------------------------------------------
       
   102 void CFMSSession::ServiceL(const RMessage2& aMessage)
       
   103 	{	
       
   104 	RThread clt; aMessage.ClientL(clt); TFullName cltnm = clt.FullName();
       
   105 	FLOG(_L( "CFMSSession::ServiceL      %d   serving for %S?" )
       
   106 			,aMessage.Function(), &cltnm );
       
   107 	if( CheckClientSecureIdL( aMessage ) )
       
   108 		{
       
   109 		//already one request pending
       
   110 		if( Server().RequestPending() && 
       
   111 				aMessage.Function() !=  ECancelOutstandingRequest )
       
   112 			{
       
   113 			aMessage.Complete(KErrCancel);
       
   114 			}
       
   115 		else //For no pending request case
       
   116 			{
       
   117 			TRAPD(err,DispatchMessageL(aMessage));
       
   118 			if(err != KErrNone ) 
       
   119 				{
       
   120 				iStopServer = ETrue;
       
   121 				}
       
   122 			aMessage.Complete(err);
       
   123 			}
       
   124 		}
       
   125 	else
       
   126 		{
       
   127 		aMessage.Complete(KErrAccessDenied);
       
   128 		}
       
   129 	}
       
   130 
       
   131 // ----------------------------------------------------------------------------------------
       
   132 // CFMSSession::ServiceError
       
   133 // ----------------------------------------------------------------------------------------
       
   134 void CFMSSession::ServiceError(const RMessage2& aMessage,TInt aError)
       
   135 	{		
       
   136 	CSession2::ServiceError(aMessage,aError);
       
   137 	}
       
   138 
       
   139 // ----------------------------------------------------------------------------------------
       
   140 // CFMSSession::DispatchMessageL
       
   141 // ----------------------------------------------------------------------------------------
       
   142 void CFMSSession::DispatchMessageL(const RMessage2& aMessage)
       
   143 	{	
       
   144 	FLOG(_L("CFMSSession::DispatchMessageL-begin"));
       
   145 
       
   146 	if( aMessage.Function() != EFotaStartUpPlugin && 
       
   147 			aMessage.Function() != ECancelOutstandingRequest && 
       
   148 			aMessage.Function() != EMonitorbattery &&
       
   149 			aMessage.Function() != EPhoneCallActive &&
       
   150 	        aMessage.Function() != EMonitorPhoneCallEnd )
       
   151 		{
       
   152 		 
       
   153 		 HandleBasedOnBearertypeL(aMessage);
       
   154 		
       
   155 			
       
   156 						
       
   157 		}
       
   158 	switch(aMessage.Function())
       
   159 	{
       
   160 	case EUserInterrupt:
       
   161 		{
       
   162 		if(Server().ChargerTobeMonitered())
       
   163 			{	
       
   164 			FLOG(_L("FMSSErver::EUserInterrupt"));			
       
   165 			Server().WriteToFile(EUserInterrupt,aMessage.Int0(),
       
   166 					(TDriveNumber)aMessage.Int1(),aMessage.Int2(),iWcdmaBearer);
       
   167 			Server().StartMonitoringL(EUserInterrupt);					    
       
   168 			}
       
   169 		else
       
   170 			{
       
   171 			WriteDummyFile();
       
   172 			iStopServer = ETrue;
       
   173 			}
       
   174 		}
       
   175 		break;
       
   176 	case ENetworkInterrupt:
       
   177 		{
       
   178 		if(Server().NetworkTobeMonitered())
       
   179 			{
       
   180 			FLOG(_L("FMSSErver::ENetworkInterrupt"));						
       
   181 			//If wlan = on & network interrupt, then stop the server and dont monitor
       
   182 			//mean other wlan n/w's present but this wlan gone off
       
   183 			if(iWlanbearer && Server().CheckWlanL())
       
   184 				{
       
   185 				WriteDummyFile();
       
   186 				Server().DeleteFile(EFalse);
       
   187 				iStopServer = ETrue;
       
   188 				break;
       
   189 				}			
       
   190 			Server().WriteToFile(ENetworkInterrupt,aMessage.Int0(),
       
   191 					(TDriveNumber)aMessage.Int1(),aMessage.Int2(),iWcdmaBearer);			
       
   192 			Server().AsyncSessionRequestL();
       
   193 			}
       
   194 		else
       
   195 			{
       
   196 			WriteDummyFile();
       
   197 			iStopServer = ETrue;
       
   198 			}
       
   199 		}
       
   200 		break;
       
   201 	case EMemoryInterrupt:
       
   202 		{			
       
   203 		if(Server().MemoryTobeMonitered())
       
   204 			{
       
   205 			FLOG(_L("FMSSErver::EMemoryInterrupt"));				
       
   206 			Server().MemoryToMonitorL(aMessage.Int2(),(TDriveNumber)aMessage.Int1()); 		
       
   207 			Server().WriteToFile(EMemoryInterrupt,aMessage.Int0(),
       
   208 					(TDriveNumber)aMessage.Int1(),aMessage.Int2(),iWcdmaBearer);
       
   209 			Server().StartMonitoringL(EMemoryInterrupt);		
       
   210 			}
       
   211 		else
       
   212 			{
       
   213 			WriteDummyFile();
       
   214 			iStopServer = ETrue;
       
   215 			}
       
   216 		}
       
   217 		break;
       
   218 	case EGeneralInterrupt:
       
   219 		{
       
   220 		if(Server().ChargerTobeMonitered())
       
   221 			{
       
   222 			FLOG(_L("FMSSErver::GeneralInterrupt"));	
       
   223 			Server().WriteToFile(EUserInterrupt,aMessage.Int0(),
       
   224 					(TDriveNumber)aMessage.Int1(),aMessage.Int2(),iWcdmaBearer);
       
   225 			Server().StartMonitoringL(EUserInterrupt);
       
   226 			}
       
   227 		else
       
   228 			{
       
   229 			WriteDummyFile();
       
   230 			iStopServer = ETrue;
       
   231 			}
       
   232 		}
       
   233 		break;
       
   234 	case EFotaStartUpPlugin:
       
   235 		{	
       
   236 		if(Server().MoniterAfterPhoneRestart())				
       
   237 			{
       
   238 			FLOG(_L("CFMSSession::EFotaStartUpPlugin"));	
       
   239 			TInt intrreason(-1),bearer(-1),size(0);
       
   240 			TInt drive(-1), wcdmabearer(-1);
       
   241 			//if loop for Hard reboot--no file or size is empty
       
   242 			if(Server().ReadFromFile(intrreason,bearer,drive,size,wcdmabearer) == EFalse )
       
   243 				{
       
   244 				FLOG(_L("CFMSSession::EFotaStartUpPlugin no file or size is empty"));
       
   245 				if(!Server().RequestPending())
       
   246 					{	
       
   247 					FLOG(_L("CFMSSession::EFotaStartUpPlugin-no request pending"));						  					  	
       
   248 					TRAPD(err,PhoneModeL());
       
   249 					if(err); // remove compiler warning
       
   250 					FLOG(_L("PhoneModeL() fotastartup returns err as %d"),err);
       
   251 					if(iWcdmaBearer)						 
       
   252 						Server().NetworkTypeL( (TInt)EBearerIdWCDMA );
       
   253 
       
   254 					else
       
   255 						Server().NetworkTypeL( (TInt)EBearerIdGPRS );	  					  					  
       
   256 					Server().LockSession();
       
   257 					Server().TriggerFotaL(); //checks n/w & trigger FOTA DL						
       
   258 					}
       
   259 				break;
       
   260 				}												
       
   261 			TUint32 BearerId = FindBearerIdL(bearer);
       
   262 			FLOG(_L("CFMSSession::EFotaStartUpPlugin wcdma bearer is %d"),wcdmabearer);
       
   263 			if( BearerId == KUidWlanBearerType )
       
   264 				{
       
   265 				FLOG(_L("CFMSSession::EFotaStartUpPlugin-wlan bearer"));	
       
   266 				Server().NetworkTypeL(EBearerIdWLAN);
       
   267 				if(intrreason == ENetworkInterrupt)
       
   268 					{
       
   269 					//stop the server , but dont delete file to support in second reboot
       
   270 					FLOG(_L("wlan bearer & n/w interrupt--stopping the server"));
       
   271 					Server().DeleteFile(EFalse);
       
   272 					iStopServer = ETrue;
       
   273 					break;
       
   274 					}
       
   275 				}
       
   276 			else if( BearerId == KUidPacketDataBearerType )
       
   277 				{
       
   278 				FLOG(_L("CFMSSession::EFotaStartUpPlugin-bearer is packet data"));	
       
   279 				if( wcdmabearer == 1 )
       
   280 					{
       
   281 					Server().NetworkTypeL(EBearerIdWCDMA);	
       
   282 					FLOG(_L("CFMSSession::EFotaStartUpPlugin-packet data and 3G"));
       
   283 					}
       
   284 				else
       
   285 					{
       
   286 					Server().NetworkTypeL(EBearerIdGPRS);
       
   287 					FLOG(_L("CFMSSession::EFotaStartUpPlugin-packet data and 2G"));
       
   288 					}
       
   289 				}
       
   290 			else
       
   291 				{
       
   292 				FLOG(_L("CFMSSession::EFotaStartUpPlugin-unknown bearer"));
       
   293 				Server().DeleteFile(EFalse);
       
   294 				iStopServer = ETrue;
       
   295 				break;
       
   296 				}
       
   297 			//when there is dummy file in the store
       
   298 			if( intrreason < EUserInterrupt || intrreason > EGeneralInterrupt )
       
   299 				{
       
   300 				FLOG(_L("CFMSSession::EFotaStartUpPlugin-Interrupt unknown"));	
       
   301 				Server().DeleteFile(EFalse); //used when variation support & reboot again
       
   302 				iStopServer = ETrue;
       
   303 				break;
       
   304 				}
       
   305 			if( intrreason == EUserInterrupt || intrreason == EGeneralInterrupt )
       
   306 				{
       
   307 				FLOG(_L("CFMSSession::EFotaStartUpPlugin-Check n/w & trigger fotaengine"));	
       
   308 				Server().CheckNetworkL(bearer,drive,size,wcdmabearer);//3 params used in monitoring if no n/w
       
   309 				break;
       
   310 				} 				
       
   311 			//for other interrupts it will monitor
       
   312 			Server().StartMonitoringL((TOmaDLInterruptReason)intrreason);
       
   313 			}
       
   314 		else
       
   315 			{
       
   316 			iStopServer = ETrue;
       
   317 			}
       
   318 		}
       
   319 		break;
       
   320 	case ECancelOutstandingRequest:
       
   321 		{					
       
   322 		FLOG(_L("CFMSSession::DispatchMessageL ECancelOutstandingRequest case:"));
       
   323 		iStopServer = ETrue; 
       
   324 		}
       
   325 		break;	
       
   326 
       
   327 	case EMonitorbattery:
       
   328 	   {
       
   329 	    FLOG(_L("CFMSSession::DispatchMessageL EMonitorbattery case:"));
       
   330 	 	Server().StartBatteryMonitoringL(EMonitorbattery, aMessage.Int0());
       
   331 	   }
       
   332 	 	break;
       
   333 	   
       
   334 	case EPhoneCallActive:
       
   335 	    {
       
   336 	    FLOG(_L("CFMSSession::DispatchMessageL EPhoneCallActiveSync case:"));
       
   337 	    Server().LockSession();
       
   338 	    TInt reason =EFalse;
       
   339 	    Server().CheckPhoneCallActiveL(reason);
       
   340 	    TPckgBuf<TInt> callactive(reason);
       
   341 	    aMessage.WriteL(0,callactive);
       
   342 	    Server().iSessionCount = 0;
       
   343 	    FLOG(_L("CFMSSession::DispatchMessageL EPhoneCallActiveSync case:%d"),reason);
       
   344 	    iStopServer = ETrue;
       
   345 	    }
       
   346 	    break;
       
   347 	    
       
   348 	case EMonitorPhoneCallEnd:
       
   349 	    {
       
   350 	    FLOG(_L("CFMSSession::DispatchMessageL EPhoneCallActiveAsync case:%d,%d"),aMessage.Int0(),aMessage.Int1());
       
   351 	    Server().LockSession();
       
   352 	    if(Server().MonitorPhoneCallEndL(aMessage.Int0(),aMessage.Int1()))
       
   353 	        {
       
   354 	        iStopServer = ETrue;
       
   355 	        Server().iSessionCount = 0;
       
   356 	        }
       
   357 	    else //let server monitor for active call end
       
   358 	        {
       
   359 	        iStopServer = EFalse;
       
   360 	        }
       
   361 	   }
       
   362 	 	break;
       
   363 	default:
       
   364 		{
       
   365 		FLOG(_L("CFMSSession::DispatchMessageL default case:and a leave happened"));
       
   366 		iStopServer = ETrue;
       
   367 		User::Leave(KErrArgument);			
       
   368 		}			
       
   369 	}
       
   370 	FLOG(_L("CFMSSession::DispatchMessageL:end"));
       
   371 	}
       
   372 
       
   373 // ----------------------------------------------------------------------------------------
       
   374 // CFMSSession::FindBearerId
       
   375 // ----------------------------------------------------------------------------------------
       
   376 TUint32 CFMSSession::FindBearerIdL(TInt aIapId)
       
   377 	{
       
   378 	FLOG(_L("CFMSSession::FindBearerId: % d"),aIapId);
       
   379 	TUint32 bearer = 0;
       
   380 	TInt err( KErrNone );
       
   381 	// Query CM Id
       
   382 	TInt cmId ( aIapId );			        
       
   383 	RCmManagerExt  CmManagerExt;
       
   384 	TRAP( err, CmManagerExt.OpenL() );
       
   385 	if( err == KErrNone )
       
   386 		{
       
   387 		RCmConnectionMethodExt cm;
       
   388 		TRAP( err, cm = CmManagerExt.ConnectionMethodL( cmId ) );			        	
       
   389 		if ( err == KErrNone )
       
   390 			{	        
       
   391 			CleanupClosePushL( cm );	  
       
   392 			bearer = cm.GetIntAttributeL( CMManager::ECmBearerType );	        			        
       
   393 			CleanupStack::PopAndDestroy();  // cm	        
       
   394 			}	    	        
       
   395 		CmManagerExt.Close();
       
   396 		}
       
   397 	return bearer;
       
   398 	}
       
   399 
       
   400 // ----------------------------------------------------------------------------------------
       
   401 // CFMSSession::WriteDummyFile
       
   402 // This method used when the variation wouldn't support and startup called
       
   403 // and also differentiates hard reeboot(with empty or no file)
       
   404 // ----------------------------------------------------------------------------------------
       
   405 void CFMSSession::WriteDummyFile()
       
   406 	{
       
   407 	FLOG(_L("CFMSSession::WriteDummyFile--begin"));
       
   408 	Server().WriteToFile(KErrNotFound,KErrNotFound,
       
   409 			(TDriveNumber)KErrNotFound,KErrNotFound,EFalse);
       
   410 	Server().DeleteFile(EFalse);
       
   411 	FLOG(_L("CFMSSession::WriteDummyFile--End"));
       
   412 	}
       
   413 
       
   414 // ----------------------------------------------------------------------------------------
       
   415 // CFMSSession::PhoneMode
       
   416 // This method used to know the current phone mode
       
   417 // If the phone is in dual mode it will check GPRS bearer only
       
   418 // ----------------------------------------------------------------------------------------
       
   419 void CFMSSession::PhoneModeL()
       
   420 	{
       
   421 	FLOG(_L("CFMSSession::PhoneModeL--begin"));	
       
   422 	// find out if we are in 2G or in 3G - it is needed for calculating bearer availability
       
   423 	__UHEAP_MARK; 
       
   424 	RTelServer telServer;
       
   425 	User::LeaveIfError( telServer.Connect());
       
   426 	FLOG(_L("CFMSSession::PhoneModeL--telServer Connected"));
       
   427 	RTelServer::TPhoneInfo teleinfo;
       
   428 	User::LeaveIfError( telServer.GetPhoneInfo( 0, teleinfo ) );
       
   429 	FLOG(_L("CFMSSession::PhoneModeL--telServer getting tel info"));
       
   430 	RMobilePhone phone;
       
   431 	User::LeaveIfError( phone.Open( telServer, teleinfo.iName ) );
       
   432 	FLOG(_L("CFMSSession::PhoneModeL--RMobilePhone opened"));
       
   433 	User::LeaveIfError(phone.Initialise());	
       
   434 	FLOG(_L("CFMSSession::PhoneModeL--phone.Initialise() success"));
       
   435 	RMobilePhone::TMobilePhoneNetworkMode mode;        	        	
       
   436 	TInt err = phone.GetCurrentMode( mode );
       
   437 	phone.Close();
       
   438 	telServer.Close();
       
   439 	__UHEAP_MARKEND;
       
   440 
       
   441 	if( KErrNone == err )
       
   442 		{
       
   443 		switch(mode)
       
   444 		{
       
   445 		case RMobilePhone::ENetworkModeGsm:
       
   446 		//case RMobilePhone::ENetworkModeUnknown:  // emulator default
       
   447 			{
       
   448 			FLOG(_L("CFMSSession::PhoneModeL-begin-GSM Mode"));	                		
       
   449 			iWcdmaBearer = EFalse;
       
   450 			Server().NetworkTypeL(EBearerIdGPRS);
       
   451 			break;		
       
   452 			}
       
   453 		case RMobilePhone::ENetworkModeWcdma:
       
   454 			{                		     		
       
   455 			FLOG(_L("CFMSSession::PhoneModeL-begin-3G Mode"));	
       
   456 			iWcdmaBearer = ETrue;
       
   457 			Server().SetWcdma();
       
   458 			Server().NetworkTypeL(EBearerIdWCDMA);
       
   459 			break;	
       
   460 			}	
       
   461 		default:        	
       
   462 		FLOG(_L("unknown"));
       
   463 		}        				
       
   464 		}
       
   465 	FLOG(_L("CFMSSession::PhoneModeL--end"));
       
   466 	}
       
   467 	
       
   468 //----------------------------------------------------------------------------------------
       
   469 // CFMSSession::HandleBasedOnBearertypeL
       
   470 // Handles based on bearer type
       
   471 //----------------------------------------------------------------------------------------	
       
   472 
       
   473 void  CFMSSession::HandleBasedOnBearertypeL(const RMessage2& aMessage)
       
   474 {
       
   475     TUint32 bearer = FindBearerIdL(aMessage.Int0());
       
   476 		if( bearer == KUidPacketDataBearerType )
       
   477 			{
       
   478 			FLOG(_L("CFMSSession::DispatchMessageL bearer is packet data"));
       
   479 			TRAPD(err,PhoneModeL());
       
   480 			if(err); // remove compiler warning
       
   481 			FLOG(_L("PhoneModeL() returns err as %d"),err);
       
   482 			}
       
   483 		else if	(	bearer == KUidWlanBearerType )
       
   484 			{
       
   485 			FLOG(_L("CFMSSession::DispatchMessageL bearer is WLAN"));
       
   486 			iWlanbearer = ETrue;
       
   487 			Server().NetworkTypeL(EBearerIdWLAN);
       
   488 			}
       
   489 		else	
       
   490 			{
       
   491 			FLOG(_L("CFMSSession::DispatchMessageL bearer is not known and a leave happened "));
       
   492 			iStopServer = ETrue;
       
   493 			User::Leave(KErrArgument);
       
   494 			}
       
   495 }				
       
   496 //End of file