omaprovisioning/provisioning/ProvisioningSC/Src/CWPProvisioningSC.cpp
changeset 0 b497e44ab2fc
child 27 5cc2995847ea
equal deleted inserted replaced
-1:000000000000 0:b497e44ab2fc
       
     1 /*
       
     2 * Copyright (c) 2002-2006 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:  Reads a provisioning document from a SIM/WIM card
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include	"CWPProvisioningSC.h"
       
    21 
       
    22 #include	<eikdll.h>
       
    23 #include	<e32std.h>
       
    24 #include	<e32svr.h>
       
    25 #include	<e32base.h>
       
    26 #include	<CWPEngine.h>
       
    27 #include	<AknGlobalNote.h> 
       
    28 #include	<ProvisioningSC.rsg>
       
    29 #include	<WPAdapterUtil.h>
       
    30 #include	"MWPPhone.h"
       
    31 #include	"CWPPushMessage.h"
       
    32 #include	"CWPBootstrap.h"
       
    33 #include	"CWPImsiDbHandler.h"
       
    34 #include	"CWPWimHandler.h"
       
    35 #include	"CWPBioMsgHandler.h"
       
    36 #include	"WPPhoneFactory.h"
       
    37 #include	"ProvisioningDebug.h"
       
    38 #include	"WPProvScPaths.h"
       
    39 #include	<ProvSC.h>
       
    40 
       
    41 const TInt KPhoneRetryCount = 7;
       
    42 const TInt KPhoneRetryTimeout = 100000; 
       
    43 
       
    44 _LIT( KProvisioningSCThread, "ProvisioningSC" );
       
    45 
       
    46 // LOCAL FUNCTION PROTOTYPES
       
    47 
       
    48 // Initializes the server.
       
    49 LOCAL_C void InitProvScL();
       
    50 
       
    51 
       
    52 
       
    53 
       
    54 // ==================== LOCAL FUNCTIONS ====================
       
    55 
       
    56 LOCAL_C void InitProvScL()
       
    57 	{
       
    58 	FLOG( _L( "[ProvisioningSC] InitProvScL" ) );
       
    59 
       
    60 __UHEAP_MARK;
       
    61 
       
    62 	TBool acInstalled( EFalse );
       
    63     // Scheduler exists before this point only in test code:
       
    64 	if( CActiveScheduler::Current()==NULL )
       
    65 		{
       
    66 		CActiveScheduler* ac = new (ELeave) CActiveScheduler;
       
    67 		CleanupStack::PushL( ac ) ;
       
    68 		CActiveScheduler::Install( ac );
       
    69 		acInstalled = ETrue;
       
    70 		}
       
    71 	
       
    72 	CWPProvisioningSC* reader = CWPProvisioningSC::NewLC();
       
    73       RProcess::Rendezvous(KErrNone);
       
    74 	if ( reader->ReadFile() )  
       
    75 		{
       
    76         FLOG( _L( "[ProvisioningSC] InitProvScL starting sched" ) );
       
    77 		CActiveScheduler::Start();  // start only if request issued
       
    78 		FLOG( _L( "[ProvisioningSC] InitProvScL, after shed start, continuing" ) );
       
    79 		}
       
    80 
       
    81 	CleanupStack::PopAndDestroy(  ); // reader
       
    82 	if( acInstalled )
       
    83 		{
       
    84 		FLOG( _L( "[ProvisioningSC] InitProvScL acinstalled - uninstalling." ) );
       
    85 		CActiveScheduler::Install( NULL ); // uninstall
       
    86 		CleanupStack::PopAndDestroy(); // ac
       
    87 		}
       
    88 
       
    89 __UHEAP_MARKEND;
       
    90 	}
       
    91 
       
    92 
       
    93 // ============================ MEMBER FUNCTIONS ===============================
       
    94 
       
    95 // -----------------------------------------------------------------------------
       
    96 // CWPProvisioningSC::CWPProvisioningSC
       
    97 // C++ default constructor can NOT contain any code, that
       
    98 // might leave.
       
    99 // -----------------------------------------------------------------------------
       
   100 //
       
   101 CWPProvisioningSC::CWPProvisioningSC()
       
   102     {
       
   103     }
       
   104 
       
   105 // -----------------------------------------------------------------------------
       
   106 // CWPProvisioningSC::ConstructL
       
   107 // Symbian 2nd phase constructor can leave.
       
   108 // -----------------------------------------------------------------------------
       
   109 //
       
   110 void CWPProvisioningSC::ConstructL()
       
   111     {
       
   112     User::RenameThread( KProvisioningSCThread );
       
   113 	iImsiHandler = CWPImsiDbHandler::NewL(); // 1
       
   114 	TBool isOld = ProcessImsiL( iPhoneImsi ); 
       
   115 	if( !isOld )
       
   116 		{
       
   117 		FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ConstructL new imsi" ) );
       
   118 		iWimHandler = CWPWimHandler::NewL( *this );
       
   119 		FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ConstructL StoreImsi" ) );
       
   120 		iImsiHandler->StoreImsiL( iPhoneImsi );
       
   121 		}
       
   122     }
       
   123 
       
   124 // -----------------------------------------------------------------------------
       
   125 // CWPProvisioningSC::NewL
       
   126 // Two-phased constructor.
       
   127 // -----------------------------------------------------------------------------
       
   128 //
       
   129 CWPProvisioningSC* CWPProvisioningSC::NewL()
       
   130     {
       
   131 	CWPProvisioningSC* self = CWPProvisioningSC::NewLC();
       
   132 	CleanupStack::Pop();
       
   133     return self;
       
   134     }
       
   135 
       
   136 // -----------------------------------------------------------------------------
       
   137 // CWPProvisioningSC::NewL
       
   138 // Two-phased constructor.
       
   139 // -----------------------------------------------------------------------------
       
   140 //
       
   141 CWPProvisioningSC* CWPProvisioningSC::NewLC()
       
   142     {
       
   143     CWPProvisioningSC* self = new( ELeave ) CWPProvisioningSC;
       
   144     CleanupStack::PushL( self );
       
   145     self->ConstructL();
       
   146     return self;
       
   147     }
       
   148 
       
   149 // -----------------------------------------------------------------------------
       
   150 // CWPProvisioningSC::Destructor
       
   151 // 
       
   152 // -----------------------------------------------------------------------------
       
   153 //
       
   154 CWPProvisioningSC::~CWPProvisioningSC()
       
   155     {
       
   156 	delete iProvisioningDoc;
       
   157 	delete iImsiNumbers;
       
   158 	delete iWimHandler;
       
   159 	delete iImsiHandler;
       
   160     }
       
   161 
       
   162 // -----------------------------------------------------------------------------
       
   163 // CWPProvisioningSC::ReadCompletedL
       
   164 // -----------------------------------------------------------------------------
       
   165 //
       
   166 void CWPProvisioningSC::ReadCompletedL()
       
   167 	{
       
   168 	FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ReadCompletedL" ) );
       
   169 	iProvisioningDoc = iWimHandler->DocL( );
       
   170 
       
   171 	TRAPD( err, StoreDocL( *iProvisioningDoc ));
       
   172 	if( err == KErrDiskFull || err == KErrNoMemory  )
       
   173 		{
       
   174 		ShowMemoryLowNoteL();
       
   175 		}
       
   176 	FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ReadCompletedL stopping sched" ) );
       
   177 	CActiveScheduler::Stop();
       
   178 	FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ReadCompletedL sched stopped" ) );
       
   179 	}	
       
   180 
       
   181 // -----------------------------------------------------------------------------
       
   182 // CWPProvisioningSC::ReadCancelledL
       
   183 // -----------------------------------------------------------------------------
       
   184 //
       
   185 void CWPProvisioningSC::ReadCancelledL()
       
   186 	{
       
   187 	if( iWimHandler )
       
   188 		{
       
   189 		CActiveScheduler::Stop();
       
   190 		}
       
   191 	}
       
   192 
       
   193 // -----------------------------------------------------------------------------
       
   194 // CWPProvisioningSC::ReadErrorL
       
   195 // -----------------------------------------------------------------------------
       
   196 //
       
   197 void CWPProvisioningSC::ReadErrorL()
       
   198 	{
       
   199 	if( iWimHandler )
       
   200 		{
       
   201 		CActiveScheduler::Stop();
       
   202 		}
       
   203 	}
       
   204 
       
   205 // -----------------------------------------------------------------------------
       
   206 // CWPProvisioningSC::ProcessImsiL
       
   207 //
       
   208 // -----------------------------------------------------------------------------
       
   209 //
       
   210 TBool CWPProvisioningSC::ProcessImsiL( TDes& aPhoneImsi )
       
   211 	{
       
   212 	TBool isOldImsi( ETrue );
       
   213 	if( iImsiHandler )
       
   214 		{
       
   215 		FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ProcessImsiL" ) );
       
   216 		TInt err( KErrNotFound );
       
   217 		MWPPhone* phone = NULL; 
       
   218 		for ( TInt j = 0 ; j < KPhoneRetryCount ; j++ )
       
   219 			{
       
   220 			TRAP( err,  phone = WPPhoneFactory::CreateL() ); 
       
   221 			// note: phone not in cleanupstack
       
   222 			if ( err == KErrNone )
       
   223 				{
       
   224 				aPhoneImsi.Copy( phone->SubscriberId() ); // store imsi
       
   225 				delete phone; 
       
   226 				phone = NULL;
       
   227 				break;
       
   228 				}
       
   229 			User::After( KPhoneRetryTimeout );
       
   230 			}
       
   231 		User::LeaveIfError( err );
       
   232 		FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ProcessImsiL imsiexistsL?" ) );
       
   233 		isOldImsi = iImsiHandler->ImsiExistsL( aPhoneImsi );
       
   234 		FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ProcessImsiL imsiexistsL ok" ) );
       
   235 		}
       
   236 	return isOldImsi;
       
   237 	}
       
   238 
       
   239 // -----------------------------------------------------------------------------
       
   240 // CWPProvisioningSC::ReadFileL
       
   241 //
       
   242 // -----------------------------------------------------------------------------
       
   243 //
       
   244 TBool CWPProvisioningSC::ReadFile()
       
   245 	{
       
   246     FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ReadFile" ) );	
       
   247 	if( iWimHandler )
       
   248 		{
       
   249 		FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ReadFile wimhandler" ) );
       
   250 		iWimHandler->ReadProvFile( EBootStrap );
       
   251 		return ETrue;
       
   252 		}
       
   253 	else
       
   254 		{
       
   255 		FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::ReadFile wimhandler null" ) );
       
   256 		return EFalse;
       
   257 		}
       
   258 	}
       
   259 
       
   260 // -----------------------------------------------------------------------------
       
   261 // CWPProvisioningSC::ShowMemoryLowNoteL
       
   262 //
       
   263 // -----------------------------------------------------------------------------
       
   264 //
       
   265 void CWPProvisioningSC::ShowMemoryLowNoteL()
       
   266 	{
       
   267 
       
   268 	TFileName fileName;
       
   269 	#ifndef __WINS__
       
   270 	fileName = RProcess().FileName();
       
   271 	#else
       
   272 	Dll::FileName( fileName );
       
   273 	#endif
       
   274 
       
   275 	HBufC* memLowText = WPAdapterUtil::ReadHBufCL( fileName,
       
   276 												KWPProvSCName,
       
   277 												R_TEXT_MEM_SUCS );
       
   278     CleanupStack::PushL( memLowText );  // 1
       
   279 	CAknGlobalNote* note = CAknGlobalNote::NewLC();  // 2
       
   280 	note->ShowNoteL( EAknGlobalInformationNote, *memLowText );
       
   281 	CleanupStack::PopAndDestroy( 2 ); // note, memLowText
       
   282 	}
       
   283 
       
   284 // -----------------------------------------------------------------------------
       
   285 // CWPProvisioningSC::StoreDocL
       
   286 //
       
   287 // -----------------------------------------------------------------------------
       
   288 //
       
   289 
       
   290 void CWPProvisioningSC::StoreDocL( TDesC8& aProvDoc )
       
   291 	{
       
   292     FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL" ) );
       
   293     
       
   294     HBufC8* convert = aProvDoc.Alloc();
       
   295        
       
   296     TPtr8 ptrBuffer( convert->Des() );
       
   297 
       
   298 	while ( ( ptrBuffer[ptrBuffer.Length()-1 ] == 0xFF ) && (ptrBuffer.Length() > 1 ) )
       
   299 	    {
       
   300 		FTRACE(RDebug::Print(_L("[ProvisioningSC] CWPProvisioningSC::StoreDocL ... deleting (%d)"), (ptrBuffer.Length()-1)));
       
   301 		ptrBuffer.Delete( ( ptrBuffer.Length()-1 ) , 1 );
       
   302 	    }
       
   303 	    
       
   304 	FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL cleanup done" ) );
       
   305         
       
   306 	aProvDoc = *convert;
       
   307     delete convert;
       
   308     
       
   309     #ifdef _DEBUG
       
   310     FHex(aProvDoc);
       
   311     #endif
       
   312     
       
   313 	CWPBioMsgHandler* msgHandler = CWPBioMsgHandler::NewLC(); // 1
       
   314 	CWPPushMessage* msg = msgHandler->CreateMessageLC( aProvDoc ); // 2
       
   315 	msg->SetAuthenticated( ETrue );
       
   316 
       
   317     // 0. read the message into the engine
       
   318     CWPEngine* engine = CWPEngine::NewLC();  // 3
       
   319 	FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL2" ) );
       
   320 	#ifdef _DEBUG
       
   321 	FHex(msg->Body() );
       
   322 	#endif
       
   323     engine->ImportDocumentL( msg->Body() );
       
   324 	FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL3" ) );
       
   325 	engine->PopulateL();
       
   326 
       
   327 	// 1. process bootstrap
       
   328     CWPBootstrap* bootstrap = CWPBootstrap::NewL( iPhoneImsi );
       
   329     CleanupStack::PushL( bootstrap );// 4
       
   330 	FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL4" ) );
       
   331     CWPBootstrap::TBootstrapResult bootstrapResult( 
       
   332         bootstrap->BootstrapL( *msg, *engine, KNullDesC ) );
       
   333     CleanupStack::PopAndDestroy(); // bootstrap, 4 
       
   334 	TBool haveSettings( engine->ItemCount() > 0 );
       
   335 
       
   336 	// 2.A Does the message contain bootstrap?
       
   337 	switch( bootstrapResult )
       
   338 		{
       
   339 	case CWPBootstrap::ESucceeded:
       
   340 			// fallthrough
       
   341 		case  CWPBootstrap::EBootstrapExists:
       
   342 		{
       
   343 		if( haveSettings ) 
       
   344 			{
       
   345 			FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL5" ) );
       
   346 			msgHandler->StoreMessageL( *msg );
       
   347 			FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL6" ) );
       
   348 			}
       
   349 		else
       
   350 			{
       
   351 			// no other settings than the TPS, store a note into inbox
       
   352 			// but first check that the TPS is not already defined
       
   353 			if(!( bootstrapResult == CWPBootstrap::EBootstrapExists ) )
       
   354 				{
       
   355 				FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL8" ) );
       
   356 				msgHandler->StoreMessageL( R_TEXT_PROV_SUCS, *msg );
       
   357 				FLOG( _L( "[ProvisioningSC] CWPProvisioningSC::StoreDocL9" ) );;
       
   358 				}
       
   359 			}
       
   360 		break;
       
   361 		}
       
   362 		// 2.B No bootstrap
       
   363 	case CWPBootstrap::ENoBootstrap:
       
   364 		{	
       
   365 		if( haveSettings )
       
   366 			{
       
   367 			// 2.B.I store settings into inbox
       
   368 			msgHandler->StoreMessageL( *msg );
       
   369 			}
       
   370 			// 2.B.II, no boostrap, no settings -> just ignore message.
       
   371 		break;
       
   372 		}	
       
   373 	default:
       
   374 		{
       
   375 		break;
       
   376 		}
       
   377 	} // switch
       
   378 	CleanupStack::PopAndDestroy( 3 ); // msg, handler, engine
       
   379 	}
       
   380 
       
   381 // ================= OTHER EXPORTED FUNCTIONS ==============
       
   382 
       
   383 // ---------------------------------------------------------
       
   384 // E32Main implements the executable entry function.
       
   385 // Note that because the target type of the ProvSC module
       
   386 // is EXEDLL, the entry point has different signature depending
       
   387 // on the build platform.
       
   388 // Creates a cleanup stack and runs the server.
       
   389 // Returns: Zero.
       
   390 
       
   391 #if defined (__WINS__)
       
   392 IMPORT_C TInt WinsMain();
       
   393 EXPORT_C TInt WinsMain()
       
   394 
       
   395 #else  //if defined(__EPOC32__)
       
   396 
       
   397 GLDEF_C TInt E32Main()
       
   398 
       
   399 #endif
       
   400     {
       
   401 	CTrapCleanup* cleanup = CTrapCleanup::New();
       
   402     TRAPD( error, InitProvScL() );
       
   403     if ( error ) error = 0; // prevent compiler warning	
       
   404 	delete cleanup;
       
   405 	return KErrNone; 
       
   406     }
       
   407 
       
   408 //  End of File