# examples/SysLibs/TaskSchedulerExample/taskexecutor.cpp

Go to the documentation of this file.

    00001 // Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
    00002 // All rights reserved.
    00003 // This component and the accompanying materials are made available
    00004 // under the terms of "Eclipse Public License v1.0"
    00005 // which accompanies this distribution, and is available
    00006 // at the URL "http://www.eclipse.org/legal/epl-v10.html".
    00007 //
    00008 // Initial Contributors:
    00009 // Nokia Corporation - initial contribution.
    00010 //
    00011 // Contributors:
    00012 //
    00013 // Description:
    00014 // This is simple code that executes the task scheduled by the task scheduler.
    00015 // The code also checks if the task requester has the required capability.
    00016 // This window belongs to the task scheduled to execute by the main program.
    00017 // It shows the details of the task information passed by the Task Scheduler
    00018 // to the executed program.
    00019 //
    00020
    00021
    00022
    00026 #include <schtask.h>
    00027 #include <e32cons.h>
    00028
    00029 _LIT(KTaskConsoleName, "TaskExecutor");
    00030 _LIT(KContent,"\nContents of task file:\n");
    00031 _LIT(KTask,"\nRunning task: %S");
    00032 _LIT(KTaskData,"\nThis is the task data for a");
    00033 _LIT(KTaskId,"\nThe task Id is: %d\n");
    00034 _LIT(KValidity,"Task is valid until %S\n");
    00035 _LIT(KDateString,"%H%:1%T %*E%*D%X%*N%Y %1 %2 %3");
    00036 _LIT(KPressAnyKey,"Press any key to continue \n");
    00037 _LIT(KAbort,"\nCapabilities of the task scheduler and the executor do not match. Task aborted\n");
    00038 _LIT(KSidMismatch,"SID of the task executor is not same as that of the scheduler. Task aborted\n");
    00039 // Uid of the task requester
    00040 const TUint32 KTaskSchedulerSid = 0xE80000B5;
    00041 const TCapability KTaskCapability = ECapabilityWriteDeviceData;
    00042
    00049 void TaskExecuteL(RFile& aTaskFile)
    00050         {
    00051         // Construct console
    00052         CConsoleBase* console = Console::NewL(KTaskConsoleName, TSize(KConsFullScreen, KConsFullScreen));
    00053         CleanupStack::PushL(console);
    00054         console->Printf(KContent);
    00055
    00056         // Open the filestore
    00057         CFileStore*     store = CDirectFileStore::FromLC(aTaskFile);//pushes store onto CleanupStack
    00058         RStoreReadStream instream;
    00059
    00060         // Open the file containing the store
    00061         instream.OpenLC(*store,store->Root());//pushes instream onto CleanupStack
    00062
    00063         // Get task count
    00064         TInt count = instream.ReadInt32L();
    00065         for (TInt i=0;i<count;i++)
    00066                 {
    00067                 CScheduledTask* task = CScheduledTask::NewLC(instream); //pushes task onto CleanupStack
    00068
    00069                 // Check if this is the SID of task requester
    00070                 if(task->SecurityInfo().iSecureId == KTaskSchedulerSid)
    00071                         {
    00072
    00073                         // Check if the requester has the necessary capability to run
    00074                         // the scheduled tasks.
    00075                         if(task->SecurityInfo().iCaps.HasCapability(KTaskCapability))
    00076                                 {
    00077                                 TBuf<50> buf;
    00078                                 buf.Format(KTask, &task->Info().iName);
    00079                                 console->Printf(buf);
    00080
    00081                                 HBufC* data = const_cast<HBufC*>(&(task->Data()));
    00082
    00083                                 console->Printf(KTaskData);
    00084                                 console->Printf(*data);
    00085
    00086                                 console->Printf(KTaskId,task->Info().iTaskId);
    00087
    00088                                 // Get the time when the task stops being valid
    00089                                 TTsTime tstime = task->ValidUntil();
    00090                                 const TTime time = tstime.GetLocalTime();
    00091                                 TBuf<30> dateString;
    00092                                 time.FormatL(dateString,(KDateString));
    00093                                 console->Printf(KValidity, &dateString);
    00094                                 }
    00095                         else
    00096                                 {
    00097                                 console->Printf(KAbort);
    00098                                 }
    00099                         }
    00100                 else
    00101                         {
    00102                         console->Printf(KSidMismatch);
    00103                         }
    00104                 console->Printf(KPressAnyKey);
    00105                 console->Getch();
    00106
    00107                 CleanupStack::PopAndDestroy(task);
    00108
    00109                 }
    00110
    00111         CleanupStack::PopAndDestroy(3); // instream, store, console
    00112         }
    00113
    00114
    00115 TInt Execute()
    00116         {
    00117         TInt err = KErrNoMemory;
    00118         CTrapCleanup* cleanup = CTrapCleanup::New();
    00119         if (cleanup)
    00120                 {
    00121                 RFile file;
    00122
    00123                 // Needs to be called early-on to allow the task scheduler server
    00124                 // to adopt the already open task file from the task scheduler
    00125                 err = file.AdoptFromCreator(TScheduledTaskFile::FsHandleIndex(),
    00126                                                                         TScheduledTaskFile::FileHandleIndex());
    00127                 if (err != KErrNone)
    00128                         {
    00129                         return err;
    00130                         }
    00131
    00132                 // Execute
    00133                 TRAPD(err,TaskExecuteL(file));
    00134                 if(err != KErrNone)
    00135                         {
    00136                         User::Panic(_L("Failed to complete"),err);
    00137                         }
    00138
    00139                 // Close the file
    00140                 file.Close();
    00141                 delete cleanup;
    00142                 }
    00143         return err;
    00144         }
    00145
    00146
    00147 GLDEF_C TInt E32Main()
    00148         {
    00149         return Execute();
    00150         }