os/ossrv/genericservices/taskscheduler/Test/MinimalTaskHandler/minimaltaskhandler.cpp
author sl@SLION-WIN7.fritz.box
Fri, 15 Jun 2012 03:10:57 +0200
changeset 0 bde4ae8d615e
permissions -rw-r--r--
First public contribution.
     1 // Copyright (c) 2000-2009 Nokia Corporation and/or its subsidiary(-ies).
     2 // All rights reserved.
     3 // This component and the accompanying materials are made available
     4 // under the terms of "Eclipse Public License v1.0"
     5 // which accompanies this distribution, and is available
     6 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
     7 //
     8 // Initial Contributors:
     9 // Nokia Corporation - initial contribution.
    10 //
    11 // Contributors:
    12 //
    13 // Description:
    14 //
    15 
    16 #include <schinfo.h>
    17 #include <schinfointernal.h>
    18 #include <schtask.h>
    19 #include <s32file.h>
    20 #include <e32math.h>
    21 #include <e32cons.h>
    22 
    23 // Constants
    24 _LIT(KMinimalTaskConsoleName, "MinimalTaskExecutor");
    25 
    26 static void SignalTestExe()
    27 	{
    28 	_LIT(KSchSemaphoreName, "SCHMinimalTaskHandler");
    29 	RSemaphore sem;
    30 	TInt ret = sem.OpenGlobal(KSchSemaphoreName);
    31 	if (ret == KErrNone)
    32 		{
    33 		sem.Signal();
    34 		sem.Close();
    35 		}
    36 	}
    37 
    38 //***********************************************************************************
    39 LOCAL_D TInt GetRandomNumber(const TInt aLow, const TInt aHigh, TInt64& aSeed)
    40 	{
    41 	TReal initialRand = (Math::FRand(aSeed) * (aHigh - aLow));
    42 	TInt32 rand;
    43 
    44 	// Round to 0 decimal places, ie. the nearest whole numer
    45 	Math::Round(initialRand, initialRand, 0);
    46 	Math::Int(rand, initialRand);
    47 
    48 	return (aLow + rand);
    49 	}
    50 
    51 //***********************************************************************************
    52 LOCAL_D void ConstructConsoleL(RFile& aTaskFile)
    53 	{
    54 	CConsoleBase* console=Console::NewL(KMinimalTaskConsoleName, TSize(KConsFullScreen, KConsFullScreen));
    55 	CleanupStack::PushL(console);
    56 	console->Printf(_L(" contents of task file\n"));
    57 	
    58 	CFileStore* store;
    59 	// Open the filestore
    60 	store = CDirectFileStore::FromLC(aTaskFile);//pushes store
    61 	RStoreReadStream instream;
    62 	instream.OpenLC(*store,store->Root());//pushes instream
    63 
    64 	// Get task count
    65 	TInt count = instream.ReadInt32L();
    66 	for (TInt i=0;i<count;i++)
    67 		{
    68 		CScheduledTask* task = CScheduledTask::NewLC(instream);
    69 		
    70 		TBuf<150> buf;
    71 		buf.Format(_L("Running task \"%S\""), &task->Info().iName);
    72 		User::LeaveIfError(User::InfoPrint(buf));
    73 		
    74 		console->Printf(task->Info().iName);
    75 		console->Printf(_L("\n"));
    76 		HBufC* data = const_cast<HBufC*>(&(task->Data()));
    77 		console->Printf(*data);
    78 		console->Printf(_L("\n"));
    79 		console->Printf(_L("%d \n"),task->Info().iTaskId);
    80 		TTsTime tstime = task->ValidUntil();
    81 		const TTime time = tstime.GetLocalTime();
    82 		TBuf<30> dateString;
    83 		time.FormatL(dateString,(_L("%H%:1%T%*E%*D%X%*N%Y %1 %2 %3")));
    84 		console->Printf(_L(":%S\n"), &dateString);
    85 		CleanupStack::PopAndDestroy(task);
    86 		}
    87 	console->Printf(_L("Pausing for a one second..."));
    88 	User::After(1000000);
    89 	CleanupStack::PopAndDestroy(3); //console, store, instream
    90 	}
    91 
    92 
    93 //***********************************************************************************
    94 LOCAL_D TInt Execute()
    95 	{
    96 	TInt err = KErrNoMemory;
    97 	CTrapCleanup* cleanup=CTrapCleanup::New();	//can fail
    98 	if (cleanup)
    99 		{
   100 		RFile file;
   101 		
   102 		// Adopt the task file from the Task Scheduler
   103 		err = file.AdoptFromCreator(TScheduledTaskFile::FsHandleIndex(),
   104 									TScheduledTaskFile::FileHandleIndex());
   105 		if (err != KErrNone)
   106 			return err;
   107 		
   108 		// The aParam is the name of a file where the relevant CTaskExCmdLine is
   109 		// do the executing 
   110 		TRAPD(err, ConstructConsoleL(file));
   111 		if(err == KErrNone)
   112 			{
   113 			// Sometimes we want to return a bogus error value, 
   114 			// sometimes we don't.
   115 			TTime now;
   116 			now.HomeTime();
   117 			TInt64 seed = now.Int64();
   118 			err = GetRandomNumber(-50, 200, seed); //20% chance of error being returned
   119 			}
   120 		
   121 		file.Close();// Close the file		
   122 		delete cleanup;
   123 		}
   124 	SignalTestExe();		
   125 	return err;
   126 	}
   127 
   128 
   129 //***********************************************************************************
   130 GLDEF_C TInt E32Main()
   131 	{
   132 	return Execute();
   133 	}