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