Update contrib.
1 // Copyright (c) 1996-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 the License "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".
8 // Initial Contributors:
9 // Nokia Corporation - initial contribution.
14 // e32test\active\t_dtim.cpp
20 // - Create a delta timer and queue a number of timed events with different time intervals.
21 // The callback functions of the timed events perform various actions:
23 // - Cancelling the previous or next timer
25 // - Stopping the active scheduler
26 // - Verifies the timed events are run correctly and orderly
27 // - The callback for each timed event increments a counter each time they are run
28 // - The counters are checked to check how many times each callback has been called
29 // Platforms/Drives/Compatibility:
31 // Assumptions/Requirement/Pre-requisites:
32 // Failures and causes:
33 // Base Port information:
38 #include <e32std_private.h>
40 #include <e32base_private.h>
44 #include <e32def_private.h>
46 RTest test(_L("T_DTIM"));
50 TCallBack* theCallBacks;
51 TDeltaTimerEntry TheTimers[10];
52 CDeltaTimer* theTimer;
54 class CMyActiveScheduler : public CActiveScheduler
57 void Error(TInt anError)const;
58 static CMyActiveScheduler* NewL();
61 void CMyActiveScheduler::Error(TInt /*anError*/) const
66 User::Panic(_L("MYActiveScheduler"),0);
69 CMyActiveScheduler* CMyActiveScheduler::NewL()
71 return new(ELeave) CMyActiveScheduler;
74 struct TDeltaTimerCallData
77 TDeltaTimerEntry *iTimers;
81 TInt print(TAny* anArg)
83 // Null entry. Just prints and increments it's counter.
86 TInt index=(TInt)anArg;
88 test.Printf(_L("Callback %d\r\n"),index);
92 TInt cancelNext(TAny * anArg)
94 // Cancels the next entry - assumes it is valid.
97 TInt index=(TInt)anArg;
98 theTimer->Remove(TheTimers[index+1]);
102 TInt requeue(TAny * anArg)
104 // Note that a requeue will run once every 200 ms until stop is called.
107 TTimeIntervalMicroSeconds32 tickPeriod;
108 UserHal::TickPeriod(tickPeriod);
109 const TInt K13Ticks = 13 * tickPeriod.Int();
111 TInt index=(TInt)anArg;
112 theTimer->Queue(K13Ticks*2/3,TheTimers[index]);
116 TInt requeuePrevious(TAny * anArg)
118 // Requeue the previous entry (assumes previous entry is valid handle)
121 TTimeIntervalMicroSeconds32 tickPeriod;
122 UserHal::TickPeriod(tickPeriod);
123 const TInt K13Ticks = 13 * tickPeriod.Int();
125 TInt index=(TInt)anArg - 1;
126 theTimer->Queue(K13Ticks,TheTimers[index]);
130 TInt cancel2ndFollowing(TAny * anArg)
132 // Assumes that 2nd following timer handle is valid.
135 TInt index=(TInt)anArg;
136 theTimer->Remove(TheTimers[index+2]);
140 TInt stop(TAny * anArg)
142 // Stops the active schduler
145 TInt index=(TInt)anArg;
147 test.Printf(_L("Callback %d, stopping\r\n"),index);
148 CMyActiveScheduler::Stop();
159 CMyActiveScheduler* s=NULL;
160 TRAPD(ret,s=CMyActiveScheduler::NewL())
163 CActiveScheduler::Install(s);
165 test.Start(_L("Timer"));
169 TTimeIntervalMicroSeconds32 tickPeriod;
170 UserHal::TickPeriod(tickPeriod);
172 TRAP(ret,theTimer=CDeltaTimer::NewL(100, tickPeriod.Int()));
175 Mem::FillZ(theResults,10*sizeof(TInt));
177 TCallBack callBacks[10]=
179 /* 0 */ TCallBack(print,(TAny*)0),
180 /* 1 */ TCallBack(cancelNext,(TAny*)1),
181 /* 2 */ TCallBack(print,(TAny*)2), // Gets cancelled
182 /* 3 */ TCallBack(print,(TAny*)3), // Runs twice
183 /* 4 */ TCallBack(requeuePrevious,(TAny*)4),
184 /* 5 */ TCallBack(cancel2ndFollowing,(TAny*)5),
185 /* 6 */ TCallBack(print,(TAny*)6),
186 /* 7 */ TCallBack(cancelNext,(TAny*)7), // Gets cancelled
187 /* 8 */ TCallBack(requeue,(TAny*)8), // Runs twice, once on the same RunL as the stop
188 /* 9 */ TCallBack(stop,(TAny*)9),
191 theCallBacks=callBacks;
192 for (TInt i=0;i<10;i++)
193 TheTimers[i].Set(theCallBacks[i]);
195 const TInt K13Ticks = 13 * tickPeriod.Int();
197 theTimer->Queue(K13Ticks,TheTimers[0]);
198 theTimer->Queue(2*K13Ticks,TheTimers[1]);
199 theTimer->Queue(3*K13Ticks,TheTimers[2]);
200 theTimer->Queue(4*K13Ticks,TheTimers[3]);
201 theTimer->Queue(5*K13Ticks,TheTimers[4]);
202 theTimer->Queue(6*K13Ticks,TheTimers[5]);
203 theTimer->Queue(7*K13Ticks,TheTimers[6]);
204 theTimer->Queue(8*K13Ticks,TheTimers[7]);
205 theTimer->Queue(9*K13Ticks,TheTimers[8]);
206 theTimer->Queue(10*K13Ticks,TheTimers[9]);
208 CActiveScheduler::Start();
210 test(theResults[0]==1);
211 test(theResults[1]==1);
212 test(theResults[2]==0);
213 test(theResults[3]==2);
214 test(theResults[4]==1);
215 test(theResults[5]==1);
216 test(theResults[6]==1);
217 test(theResults[7]==0);
218 test(theResults[8]==2);
219 test(theResults[9]==1);