examples/SysLibs/TaskSchedulerExample/taskexecutor.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2007-2010 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:
00028 This is simple code that executes the task scheduled by the task scheduler. 
00029 The code also checks if the task requester has the required capability.
00030 This window belongs to the task scheduled to execute by the main program. 
00031 It shows the details of the task information passed by the Task Scheduler 
00032 to the executed program.   
00033 */
00034 
00035 
00036 
00037 
00038 
00042 #include <schtask.h>
00043 #include <e32cons.h>
00044 
00045 _LIT(KTaskConsoleName, "TaskExecutor");
00046 _LIT(KContent,"\nContents of task file:\n");
00047 _LIT(KTask,"\nRunning task: %S");
00048 _LIT(KTaskData,"\nThis is the task data for a");
00049 _LIT(KTaskId,"\nThe task Id is: %d\n");
00050 _LIT(KValidity,"Task is valid until %S\n");
00051 _LIT(KDateString,"%H%:1%T %*E%*D%X%*N%Y %1 %2 %3");
00052 _LIT(KPressAnyKey,"Press any key to continue \n");
00053 _LIT(KAbort,"\nCapabilities of the task scheduler and the executor do not match. Task aborted\n");
00054 _LIT(KSidMismatch,"SID of the task executor is not same as that of the scheduler. Task aborted\n");
00055 // Uid of the task requester
00056 const TUint32 KTaskSchedulerSid = 0xE80000B5;
00057 const TCapability KTaskCapability = ECapabilityWriteDeviceData;
00058 
00065 void TaskExecuteL(RFile& aTaskFile)
00066         {
00067         // Construct console
00068         CConsoleBase* console = Console::NewL(KTaskConsoleName, TSize(KConsFullScreen, KConsFullScreen));
00069         CleanupStack::PushL(console);
00070         console->Printf(KContent);
00071         
00072         // Open the filestore
00073         CFileStore*     store = CDirectFileStore::FromLC(aTaskFile);//pushes store onto CleanupStack
00074         RStoreReadStream instream;
00075         
00076         // Open the file containing the store
00077         instream.OpenLC(*store,store->Root());//pushes instream onto CleanupStack
00078 
00079         // Get task count
00080         TInt count = instream.ReadInt32L();
00081         for (TInt i=0;i<count;i++)
00082                 {
00083                 CScheduledTask* task = CScheduledTask::NewLC(instream); //pushes task onto CleanupStack
00084                 
00085                 // Check if this is the SID of task requester
00086                 if(task->SecurityInfo().iSecureId == KTaskSchedulerSid)
00087                         {
00088                         
00089                         // Check if the requester has the necessary capability to run 
00090                         // the scheduled tasks.
00091                         if(task->SecurityInfo().iCaps.HasCapability(KTaskCapability))
00092                                 {
00093                                 TBuf<50> buf;
00094                                 buf.Format(KTask, &task->Info().iName);
00095                                 console->Printf(buf);
00096                         
00097                                 HBufC* data = const_cast<HBufC*>(&(task->Data()));
00098                 
00099                                 console->Printf(KTaskData);
00100                                 console->Printf(*data);
00101                                 
00102                                 console->Printf(KTaskId,task->Info().iTaskId);
00103                 
00104                                 // Get the time when the task stops being valid
00105                                 TTsTime tstime = task->ValidUntil();
00106                                 const TTime time = tstime.GetLocalTime();
00107                                 TBuf<30> dateString;
00108                                 time.FormatL(dateString,(KDateString));
00109                                 console->Printf(KValidity, &dateString);
00110                                 }
00111                         else
00112                                 {
00113                                 console->Printf(KAbort);        
00114                                 }
00115                         }
00116                 else
00117                         {
00118                         console->Printf(KSidMismatch);
00119                         }
00120                 console->Printf(KPressAnyKey);
00121                 console->Getch();
00122 
00123                 CleanupStack::PopAndDestroy(task);
00124                         
00125                 }
00126         
00127         CleanupStack::PopAndDestroy(3); // instream, store, console
00128         }
00129 
00130 
00131 TInt Execute()
00132         {
00133         TInt err = KErrNoMemory;
00134         CTrapCleanup* cleanup = CTrapCleanup::New();
00135         if (cleanup)
00136                 {
00137                 RFile file;
00138                 
00139                 // Needs to be called early-on to allow the task scheduler server
00140                 // to adopt the already open task file from the task scheduler
00141                 err = file.AdoptFromCreator(TScheduledTaskFile::FsHandleIndex(),
00142                                                                         TScheduledTaskFile::FileHandleIndex());
00143                 if (err != KErrNone)
00144                         {
00145                         return err;             
00146                         }
00147                         
00148                 // Execute 
00149                 TRAPD(err,TaskExecuteL(file));
00150                 if(err != KErrNone)
00151                         {
00152                         User::Panic(_L("Failed to complete"),err);
00153                         }
00154         
00155                 // Close the file
00156                 file.Close();           
00157                 delete cleanup;
00158                 }               
00159         return err;
00160         }
00161 
00162 
00163 GLDEF_C TInt E32Main()
00164         {
00165         return Execute();
00166         }

Generated by  doxygen 1.6.2