Symbian3/Examples/guid-6013a680-57f9-415b-8851-c4fa63356636/taskexecutor_8cpp_source.html
changeset 6 43e37759235e
equal deleted inserted replaced
5:f345bda72bc4 6:43e37759235e
       
     1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
       
     2 <html xmlns="http://www.w3.org/1999/xhtml">
       
     3 <head>
       
     4 <meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
       
     5 <title>TB9.2 Example Applications: examples/SysLibs/TaskSchedulerExample/taskexecutor.cpp Source File</title>
       
     6 <link href="tabs.css" rel="stylesheet" type="text/css"/>
       
     7 <link href="doxygen.css" rel="stylesheet" type="text/css"/>
       
     8 </head>
       
     9 <body>
       
    10 <!-- Generated by Doxygen 1.6.2 -->
       
    11 <h1>examples/SysLibs/TaskSchedulerExample/taskexecutor.cpp</h1><a href="taskexecutor_8cpp.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="comment">// Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).</span>
       
    12 <a name="l00002"></a>00002 <span class="comment">// All rights reserved.</span>
       
    13 <a name="l00003"></a>00003 <span class="comment">// This component and the accompanying materials are made available</span>
       
    14 <a name="l00004"></a>00004 <span class="comment">// under the terms of &quot;Eclipse Public License v1.0&quot;</span>
       
    15 <a name="l00005"></a>00005 <span class="comment">// which accompanies this distribution, and is available</span>
       
    16 <a name="l00006"></a>00006 <span class="comment">// at the URL &quot;http://www.eclipse.org/legal/epl-v10.html&quot;.</span>
       
    17 <a name="l00007"></a>00007 <span class="comment">//</span>
       
    18 <a name="l00008"></a>00008 <span class="comment">// Initial Contributors:</span>
       
    19 <a name="l00009"></a>00009 <span class="comment">// Nokia Corporation - initial contribution.</span>
       
    20 <a name="l00010"></a>00010 <span class="comment">//</span>
       
    21 <a name="l00011"></a>00011 <span class="comment">// Contributors:</span>
       
    22 <a name="l00012"></a>00012 <span class="comment">//</span>
       
    23 <a name="l00013"></a>00013 <span class="comment">// Description:</span>
       
    24 <a name="l00014"></a>00014 <span class="comment">// This is simple code that executes the task scheduled by the task scheduler. </span>
       
    25 <a name="l00015"></a>00015 <span class="comment">// The code also checks if the task requester has the required capability.</span>
       
    26 <a name="l00016"></a>00016 <span class="comment">// This window belongs to the task scheduled to execute by the main program. </span>
       
    27 <a name="l00017"></a>00017 <span class="comment">// It shows the details of the task information passed by the Task Scheduler </span>
       
    28 <a name="l00018"></a>00018 <span class="comment">// to the executed program. </span>
       
    29 <a name="l00019"></a>00019 <span class="comment">//</span>
       
    30 <a name="l00020"></a>00020 
       
    31 <a name="l00021"></a>00021 
       
    32 <a name="l00022"></a>00022 
       
    33 <a name="l00026"></a>00026 <span class="preprocessor">#include &lt;schtask.h&gt;</span>
       
    34 <a name="l00027"></a>00027 <span class="preprocessor">#include &lt;e32cons.h&gt;</span>
       
    35 <a name="l00028"></a>00028 
       
    36 <a name="l00029"></a>00029 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTaskConsoleName, <span class="stringliteral">&quot;TaskExecutor&quot;</span>);
       
    37 <a name="l00030"></a>00030 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KContent,<span class="stringliteral">&quot;\nContents of task file:\n&quot;</span>);
       
    38 <a name="l00031"></a>00031 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTask,<span class="stringliteral">&quot;\nRunning task: %S&quot;</span>);
       
    39 <a name="l00032"></a>00032 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTaskData,<span class="stringliteral">&quot;\nThis is the task data for a&quot;</span>);
       
    40 <a name="l00033"></a>00033 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KTaskId,<span class="stringliteral">&quot;\nThe task Id is: %d\n&quot;</span>);
       
    41 <a name="l00034"></a>00034 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KValidity,<span class="stringliteral">&quot;Task is valid until %S\n&quot;</span>);
       
    42 <a name="l00035"></a>00035 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KDateString,<span class="stringliteral">&quot;%H%:1%T %*E%*D%X%*N%Y %1 %2 %3&quot;</span>);
       
    43 <a name="l00036"></a>00036 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KPressAnyKey,<span class="stringliteral">&quot;Press any key to continue \n&quot;</span>);
       
    44 <a name="l00037"></a>00037 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KAbort,<span class="stringliteral">&quot;\nCapabilities of the task scheduler and the executor do not match. Task aborted\n&quot;</span>);
       
    45 <a name="l00038"></a>00038 <a class="code" href="secureclientandserver_8h.html#a5eadd4429627d82193b37536d4faa03a">_LIT</a>(KSidMismatch,<span class="stringliteral">&quot;SID of the task executor is not same as that of the scheduler. Task aborted\n&quot;</span>);
       
    46 <a name="l00039"></a>00039 <span class="comment">// Uid of the task requester</span>
       
    47 <a name="l00040"></a>00040 <span class="keyword">const</span> TUint32 KTaskSchedulerSid = 0xE80000B5;
       
    48 <a name="l00041"></a>00041 <span class="keyword">const</span> TCapability KTaskCapability = ECapabilityWriteDeviceData;
       
    49 <a name="l00042"></a>00042 
       
    50 <a name="l00049"></a><a class="code" href="taskexecutor_8cpp.html#ad51792a4c3cfb76bbbd1f61050928332">00049</a> <span class="keywordtype">void</span> <a class="code" href="taskexecutor_8cpp.html#ad51792a4c3cfb76bbbd1f61050928332">TaskExecuteL</a>(RFile&amp; aTaskFile)
       
    51 <a name="l00050"></a>00050         {
       
    52 <a name="l00051"></a>00051         <span class="comment">// Construct console</span>
       
    53 <a name="l00052"></a>00052         CConsoleBase* console = Console::NewL(KTaskConsoleName, TSize(KConsFullScreen, KConsFullScreen));
       
    54 <a name="l00053"></a>00053         CleanupStack::PushL(console);
       
    55 <a name="l00054"></a>00054         console-&gt;Printf(KContent);
       
    56 <a name="l00055"></a>00055         
       
    57 <a name="l00056"></a>00056         <span class="comment">// Open the filestore</span>
       
    58 <a name="l00057"></a>00057         CFileStore*     store = CDirectFileStore::FromLC(aTaskFile);<span class="comment">//pushes store onto CleanupStack</span>
       
    59 <a name="l00058"></a>00058         RStoreReadStream instream;
       
    60 <a name="l00059"></a>00059         
       
    61 <a name="l00060"></a>00060         <span class="comment">// Open the file containing the store</span>
       
    62 <a name="l00061"></a>00061         instream.OpenLC(*store,store-&gt;Root());<span class="comment">//pushes instream onto CleanupStack</span>
       
    63 <a name="l00062"></a>00062 
       
    64 <a name="l00063"></a>00063         <span class="comment">// Get task count</span>
       
    65 <a name="l00064"></a>00064         TInt count = instream.ReadInt32L();
       
    66 <a name="l00065"></a>00065         <span class="keywordflow">for</span> (TInt i=0;i&lt;count;i++)
       
    67 <a name="l00066"></a>00066                 {
       
    68 <a name="l00067"></a>00067                 CScheduledTask* task = CScheduledTask::NewLC(instream); <span class="comment">//pushes task onto CleanupStack</span>
       
    69 <a name="l00068"></a>00068                 
       
    70 <a name="l00069"></a>00069                 <span class="comment">// Check if this is the SID of task requester</span>
       
    71 <a name="l00070"></a>00070                 <span class="keywordflow">if</span>(task-&gt;SecurityInfo().iSecureId == KTaskSchedulerSid)
       
    72 <a name="l00071"></a>00071                         {
       
    73 <a name="l00072"></a>00072                         
       
    74 <a name="l00073"></a>00073                         <span class="comment">// Check if the requester has the necessary capability to run </span>
       
    75 <a name="l00074"></a>00074                         <span class="comment">// the scheduled tasks.</span>
       
    76 <a name="l00075"></a>00075                         <span class="keywordflow">if</span>(task-&gt;SecurityInfo().iCaps.HasCapability(KTaskCapability))
       
    77 <a name="l00076"></a>00076                                 {
       
    78 <a name="l00077"></a>00077                                 TBuf&lt;50&gt; buf;
       
    79 <a name="l00078"></a>00078                                 buf.Format(KTask, &amp;task-&gt;Info().iName);
       
    80 <a name="l00079"></a>00079                                 console-&gt;Printf(buf);
       
    81 <a name="l00080"></a>00080                         
       
    82 <a name="l00081"></a>00081                                 HBufC* data = <span class="keyword">const_cast&lt;</span>HBufC*<span class="keyword">&gt;</span>(&amp;(task-&gt;Data()));
       
    83 <a name="l00082"></a>00082                 
       
    84 <a name="l00083"></a>00083                                 console-&gt;Printf(KTaskData);
       
    85 <a name="l00084"></a>00084                                 console-&gt;Printf(*data);
       
    86 <a name="l00085"></a>00085                                 
       
    87 <a name="l00086"></a>00086                                 console-&gt;Printf(KTaskId,task-&gt;Info().iTaskId);
       
    88 <a name="l00087"></a>00087                 
       
    89 <a name="l00088"></a>00088                                 <span class="comment">// Get the time when the task stops being valid</span>
       
    90 <a name="l00089"></a>00089                                 TTsTime tstime = task-&gt;ValidUntil();
       
    91 <a name="l00090"></a>00090                                 <span class="keyword">const</span> TTime time = tstime.GetLocalTime();
       
    92 <a name="l00091"></a>00091                                 TBuf&lt;30&gt; dateString;
       
    93 <a name="l00092"></a>00092                                 time.FormatL(dateString,(KDateString));
       
    94 <a name="l00093"></a>00093                                 console-&gt;Printf(KValidity, &amp;dateString);
       
    95 <a name="l00094"></a>00094                                 }
       
    96 <a name="l00095"></a>00095                         <span class="keywordflow">else</span>
       
    97 <a name="l00096"></a>00096                                 {
       
    98 <a name="l00097"></a>00097                                 console-&gt;Printf(KAbort);        
       
    99 <a name="l00098"></a>00098                                 }
       
   100 <a name="l00099"></a>00099                         }
       
   101 <a name="l00100"></a>00100                 <span class="keywordflow">else</span>
       
   102 <a name="l00101"></a>00101                         {
       
   103 <a name="l00102"></a>00102                         console-&gt;Printf(KSidMismatch);
       
   104 <a name="l00103"></a>00103                         }
       
   105 <a name="l00104"></a>00104                 console-&gt;Printf(KPressAnyKey);
       
   106 <a name="l00105"></a>00105                 console-&gt;Getch();
       
   107 <a name="l00106"></a>00106 
       
   108 <a name="l00107"></a>00107                 CleanupStack::PopAndDestroy(task);
       
   109 <a name="l00108"></a>00108                         
       
   110 <a name="l00109"></a>00109                 }
       
   111 <a name="l00110"></a>00110         
       
   112 <a name="l00111"></a>00111         CleanupStack::PopAndDestroy(3); <span class="comment">// instream, store, console</span>
       
   113 <a name="l00112"></a>00112         }
       
   114 <a name="l00113"></a>00113 
       
   115 <a name="l00114"></a>00114 
       
   116 <a name="l00115"></a>00115 TInt Execute()
       
   117 <a name="l00116"></a>00116         {
       
   118 <a name="l00117"></a>00117         TInt err = KErrNoMemory;
       
   119 <a name="l00118"></a>00118         CTrapCleanup* cleanup = CTrapCleanup::New();
       
   120 <a name="l00119"></a>00119         <span class="keywordflow">if</span> (cleanup)
       
   121 <a name="l00120"></a>00120                 {
       
   122 <a name="l00121"></a>00121                 RFile file;
       
   123 <a name="l00122"></a>00122                 
       
   124 <a name="l00123"></a>00123                 <span class="comment">// Needs to be called early-on to allow the task scheduler server</span>
       
   125 <a name="l00124"></a>00124                 <span class="comment">// to adopt the already open task file from the task scheduler</span>
       
   126 <a name="l00125"></a>00125                 err = file.AdoptFromCreator(TScheduledTaskFile::FsHandleIndex(),
       
   127 <a name="l00126"></a>00126                                                                         TScheduledTaskFile::FileHandleIndex());
       
   128 <a name="l00127"></a>00127                 <span class="keywordflow">if</span> (err != KErrNone)
       
   129 <a name="l00128"></a>00128                         {
       
   130 <a name="l00129"></a>00129                         <span class="keywordflow">return</span> err;             
       
   131 <a name="l00130"></a>00130                         }
       
   132 <a name="l00131"></a>00131                         
       
   133 <a name="l00132"></a>00132                 <span class="comment">// Execute </span>
       
   134 <a name="l00133"></a>00133                 TRAPD(err,<a class="code" href="taskexecutor_8cpp.html#ad51792a4c3cfb76bbbd1f61050928332">TaskExecuteL</a>(file));
       
   135 <a name="l00134"></a>00134                 <span class="keywordflow">if</span>(err != KErrNone)
       
   136 <a name="l00135"></a>00135                         {
       
   137 <a name="l00136"></a>00136                         User::Panic(_L(<span class="stringliteral">&quot;Failed to complete&quot;</span>),err);
       
   138 <a name="l00137"></a>00137                         }
       
   139 <a name="l00138"></a>00138         
       
   140 <a name="l00139"></a>00139                 <span class="comment">// Close the file</span>
       
   141 <a name="l00140"></a>00140                 file.Close();           
       
   142 <a name="l00141"></a>00141                 <span class="keyword">delete</span> cleanup;
       
   143 <a name="l00142"></a>00142                 }               
       
   144 <a name="l00143"></a>00143         <span class="keywordflow">return</span> err;
       
   145 <a name="l00144"></a>00144         }
       
   146 <a name="l00145"></a>00145 
       
   147 <a name="l00146"></a>00146 
       
   148 <a name="l00147"></a><a class="code" href="taskexecutor_8cpp.html#a0f358e9c4355138f629b8c4f37310295">00147</a> GLDEF_C TInt E32Main()
       
   149 <a name="l00148"></a>00148         {
       
   150 <a name="l00149"></a>00149         <span class="keywordflow">return</span> Execute();
       
   151 <a name="l00150"></a>00150         }
       
   152 </pre></div></div>
       
   153 <hr size="1"/><address style="text-align: right;"><small>Generated by&nbsp;
       
   154 <a href="http://www.doxygen.org/index.html">
       
   155 <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.6.2 </small></address>
       
   156 </body>
       
   157 </html>