1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/os/kernelhwsrv/kerneltest/e32test/active/t_dtim.cpp Fri Jun 15 03:10:57 2012 +0200
1.3 @@ -0,0 +1,227 @@
1.4 +// Copyright (c) 1996-2009 Nokia Corporation and/or its subsidiary(-ies).
1.5 +// All rights reserved.
1.6 +// This component and the accompanying materials are made available
1.7 +// under the terms of the License "Eclipse Public License v1.0"
1.8 +// which accompanies this distribution, and is available
1.9 +// at the URL "http://www.eclipse.org/legal/epl-v10.html".
1.10 +//
1.11 +// Initial Contributors:
1.12 +// Nokia Corporation - initial contribution.
1.13 +//
1.14 +// Contributors:
1.15 +//
1.16 +// Description:
1.17 +// e32test\active\t_dtim.cpp
1.18 +// Overview:
1.19 +// Test delta timers.
1.20 +// API Information:
1.21 +// CDeltaTimer.
1.22 +// Details:
1.23 +// - Create a delta timer and queue a number of timed events with different time intervals.
1.24 +// The callback functions of the timed events perform various actions:
1.25 +// - Requeuing itself
1.26 +// - Cancelling the previous or next timer
1.27 +// - Doing nothing
1.28 +// - Stopping the active scheduler
1.29 +// - Verifies the timed events are run correctly and orderly
1.30 +// - The callback for each timed event increments a counter each time they are run
1.31 +// - The counters are checked to check how many times each callback has been called
1.32 +// Platforms/Drives/Compatibility:
1.33 +// All.
1.34 +// Assumptions/Requirement/Pre-requisites:
1.35 +// Failures and causes:
1.36 +// Base Port information:
1.37 +//
1.38 +//
1.39 +
1.40 +#include <e32std.h>
1.41 +#include <e32std_private.h>
1.42 +#include <e32base.h>
1.43 +#include <e32base_private.h>
1.44 +#include <e32test.h>
1.45 +#include <e32hal.h>
1.46 +#include <e32def.h>
1.47 +#include <e32def_private.h>
1.48 +
1.49 +RTest test(_L("T_DTIM"));
1.50 +
1.51 +
1.52 +TInt theResults[10];
1.53 +TCallBack* theCallBacks;
1.54 +TDeltaTimerEntry TheTimers[10];
1.55 +CDeltaTimer* theTimer;
1.56 +
1.57 +class CMyActiveScheduler : public CActiveScheduler
1.58 + {
1.59 +public:
1.60 + void Error(TInt anError)const;
1.61 + static CMyActiveScheduler* NewL();
1.62 + };
1.63 +
1.64 +void CMyActiveScheduler::Error(TInt /*anError*/) const
1.65 +//
1.66 +//
1.67 +//
1.68 + {
1.69 + User::Panic(_L("MYActiveScheduler"),0);
1.70 + }
1.71 +
1.72 +CMyActiveScheduler* CMyActiveScheduler::NewL()
1.73 + {
1.74 + return new(ELeave) CMyActiveScheduler;
1.75 + }
1.76 +
1.77 +struct TDeltaTimerCallData
1.78 + {
1.79 + CDeltaTimer* iTimer;
1.80 + TDeltaTimerEntry *iTimers;
1.81 + TInt iIndex;
1.82 + };
1.83 +
1.84 +TInt print(TAny* anArg)
1.85 +//
1.86 +// Null entry. Just prints and increments it's counter.
1.87 +//
1.88 + {
1.89 + TInt index=(TInt)anArg;
1.90 + theResults[index]++;
1.91 + test.Printf(_L("Callback %d\r\n"),index);
1.92 + return 0;
1.93 + }
1.94 +
1.95 +TInt cancelNext(TAny * anArg)
1.96 +//
1.97 +// Cancels the next entry - assumes it is valid.
1.98 +//
1.99 + {
1.100 + TInt index=(TInt)anArg;
1.101 + theTimer->Remove(TheTimers[index+1]);
1.102 + return print(anArg);
1.103 + }
1.104 +
1.105 +TInt requeue(TAny * anArg)
1.106 +//
1.107 +// Note that a requeue will run once every 200 ms until stop is called.
1.108 +//
1.109 + {
1.110 + TTimeIntervalMicroSeconds32 tickPeriod;
1.111 + UserHal::TickPeriod(tickPeriod);
1.112 + const TInt K13Ticks = 13 * tickPeriod.Int();
1.113 +
1.114 + TInt index=(TInt)anArg;
1.115 + theTimer->Queue(K13Ticks*2/3,TheTimers[index]);
1.116 + return print(anArg);
1.117 + }
1.118 +
1.119 +TInt requeuePrevious(TAny * anArg)
1.120 +//
1.121 +// Requeue the previous entry (assumes previous entry is valid handle)
1.122 +//
1.123 + {
1.124 + TTimeIntervalMicroSeconds32 tickPeriod;
1.125 + UserHal::TickPeriod(tickPeriod);
1.126 + const TInt K13Ticks = 13 * tickPeriod.Int();
1.127 +
1.128 + TInt index=(TInt)anArg - 1;
1.129 + theTimer->Queue(K13Ticks,TheTimers[index]);
1.130 + return print(anArg);
1.131 + }
1.132 +
1.133 +TInt cancel2ndFollowing(TAny * anArg)
1.134 +//
1.135 +// Assumes that 2nd following timer handle is valid.
1.136 +//
1.137 + {
1.138 + TInt index=(TInt)anArg;
1.139 + theTimer->Remove(TheTimers[index+2]);
1.140 + return print(anArg);
1.141 + }
1.142 +
1.143 +TInt stop(TAny * anArg)
1.144 +//
1.145 +// Stops the active schduler
1.146 +//
1.147 + {
1.148 + TInt index=(TInt)anArg;
1.149 + theResults[index]++;
1.150 + test.Printf(_L("Callback %d, stopping\r\n"),index);
1.151 + CMyActiveScheduler::Stop();
1.152 +
1.153 + return 0;
1.154 + }
1.155 +
1.156 +TInt E32Main()
1.157 +//
1.158 +//
1.159 +//
1.160 + {
1.161 +
1.162 + CMyActiveScheduler* s=NULL;
1.163 + TRAPD(ret,s=CMyActiveScheduler::NewL())
1.164 + test(ret==KErrNone);
1.165 +
1.166 + CActiveScheduler::Install(s);
1.167 + test.Title();
1.168 + test.Start(_L("Timer"));
1.169 +
1.170 + __KHEAP_MARK;
1.171 +
1.172 + TTimeIntervalMicroSeconds32 tickPeriod;
1.173 + UserHal::TickPeriod(tickPeriod);
1.174 +
1.175 + TRAP(ret,theTimer=CDeltaTimer::NewL(100, tickPeriod.Int()));
1.176 + test(ret==KErrNone);
1.177 +
1.178 + Mem::FillZ(theResults,10*sizeof(TInt));
1.179 +
1.180 + TCallBack callBacks[10]=
1.181 + {
1.182 + /* 0 */ TCallBack(print,(TAny*)0),
1.183 + /* 1 */ TCallBack(cancelNext,(TAny*)1),
1.184 + /* 2 */ TCallBack(print,(TAny*)2), // Gets cancelled
1.185 + /* 3 */ TCallBack(print,(TAny*)3), // Runs twice
1.186 + /* 4 */ TCallBack(requeuePrevious,(TAny*)4),
1.187 + /* 5 */ TCallBack(cancel2ndFollowing,(TAny*)5),
1.188 + /* 6 */ TCallBack(print,(TAny*)6),
1.189 + /* 7 */ TCallBack(cancelNext,(TAny*)7), // Gets cancelled
1.190 + /* 8 */ TCallBack(requeue,(TAny*)8), // Runs twice, once on the same RunL as the stop
1.191 + /* 9 */ TCallBack(stop,(TAny*)9),
1.192 + };
1.193 +
1.194 + theCallBacks=callBacks;
1.195 + for (TInt i=0;i<10;i++)
1.196 + TheTimers[i].Set(theCallBacks[i]);
1.197 +
1.198 + const TInt K13Ticks = 13 * tickPeriod.Int();
1.199 +
1.200 + theTimer->Queue(K13Ticks,TheTimers[0]);
1.201 + theTimer->Queue(2*K13Ticks,TheTimers[1]);
1.202 + theTimer->Queue(3*K13Ticks,TheTimers[2]);
1.203 + theTimer->Queue(4*K13Ticks,TheTimers[3]);
1.204 + theTimer->Queue(5*K13Ticks,TheTimers[4]);
1.205 + theTimer->Queue(6*K13Ticks,TheTimers[5]);
1.206 + theTimer->Queue(7*K13Ticks,TheTimers[6]);
1.207 + theTimer->Queue(8*K13Ticks,TheTimers[7]);
1.208 + theTimer->Queue(9*K13Ticks,TheTimers[8]);
1.209 + theTimer->Queue(10*K13Ticks,TheTimers[9]);
1.210 +
1.211 + CActiveScheduler::Start();
1.212 +
1.213 + test(theResults[0]==1);
1.214 + test(theResults[1]==1);
1.215 + test(theResults[2]==0);
1.216 + test(theResults[3]==2);
1.217 + test(theResults[4]==1);
1.218 + test(theResults[5]==1);
1.219 + test(theResults[6]==1);
1.220 + test(theResults[7]==0);
1.221 + test(theResults[8]==2);
1.222 + test(theResults[9]==1);
1.223 +
1.224 + delete theTimer;
1.225 +
1.226 + __KHEAP_MARKEND;
1.227 +
1.228 + test.End();
1.229 + return 0;
1.230 + }