|
1 // Copyright (c) 2005-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 "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". |
|
7 // |
|
8 // Initial Contributors: |
|
9 // Nokia Corporation - initial contribution. |
|
10 // |
|
11 // Contributors: |
|
12 // |
|
13 // Description: |
|
14 // |
|
15 |
|
16 |
|
17 |
|
18 // INCLUDE FILES |
|
19 #include <e32base.h> |
|
20 #include "EPos_CPosCallbackTimer.h" |
|
21 |
|
22 // ================= MEMBER FUNCTIONS ======================= |
|
23 |
|
24 /** |
|
25 * C++ default constructor. |
|
26 * @param aCallBack |
|
27 */ |
|
28 CPosCallbackTimer::CPosCallbackTimer(TCallBack& aCallBack) |
|
29 : CTimer(EPriorityStandard), |
|
30 iTimeoutCallBack(aCallBack) |
|
31 { |
|
32 CActiveScheduler::Add(this); |
|
33 } |
|
34 /** |
|
35 * Symbian constructor. |
|
36 */ |
|
37 void CPosCallbackTimer::ConstructL() |
|
38 { |
|
39 CTimer::ConstructL(); |
|
40 } |
|
41 |
|
42 /** |
|
43 * Two-phased constructor. |
|
44 * @param aCallBack |
|
45 */ |
|
46 CPosCallbackTimer* CPosCallbackTimer::NewL(TCallBack& aCallBack) |
|
47 { |
|
48 CPosCallbackTimer* self = new (ELeave) CPosCallbackTimer(aCallBack); |
|
49 CleanupStack::PushL(self); |
|
50 self->ConstructL(); |
|
51 CleanupStack::Pop(self); |
|
52 return self; |
|
53 } |
|
54 |
|
55 /** |
|
56 * Destructor. |
|
57 */ |
|
58 CPosCallbackTimer::~CPosCallbackTimer() |
|
59 { |
|
60 } |
|
61 |
|
62 /** |
|
63 * Start the callback timer. If there is already a timer running, it |
|
64 * will be restarted. |
|
65 * |
|
66 * @param aTimeout The timeout. |
|
67 */ |
|
68 void CPosCallbackTimer::StartTimer(const TTimeIntervalMicroSeconds& aTimeOut) |
|
69 { |
|
70 Cancel(); |
|
71 |
|
72 // MaxInt64 = MaxInt32 * MaxUint32 |
|
73 // up to MaxUint32 internal rounds by MaxInt32 are possible before final |
|
74 // timeout of up to MaxInt32 |
|
75 |
|
76 iNumInternalRounds = static_cast<TUint32>(aTimeOut.Int64() / KMaxTInt32); |
|
77 iTimeOut = static_cast<TInt32>(aTimeOut.Int64() % KMaxTInt32); |
|
78 // iTimeOut can be also zero. It is catered for in NextRound |
|
79 |
|
80 NextRound(); |
|
81 } |
|
82 |
|
83 /** |
|
84 * Activates timer for next period: full round (KMaxTInt32) or |
|
85 * small delay stored in iTimeOut |
|
86 */ |
|
87 TBool CPosCallbackTimer::NextRound() |
|
88 { |
|
89 if (iNumInternalRounds > 0) |
|
90 { |
|
91 After(KMaxTInt32); |
|
92 iNumInternalRounds--; |
|
93 return ETrue; // timer not yet completed |
|
94 } |
|
95 else if (iTimeOut.Int() > 0) |
|
96 { |
|
97 After(iTimeOut); |
|
98 iTimeOut = 0; // timer will complete on next round |
|
99 return ETrue; // timer not yet completed |
|
100 } |
|
101 else |
|
102 { |
|
103 return EFalse; // timer completed |
|
104 } |
|
105 } |
|
106 |
|
107 /** |
|
108 * From CActive |
|
109 */ |
|
110 void CPosCallbackTimer::RunL() |
|
111 { |
|
112 if (!NextRound()) // timer completed? |
|
113 { |
|
114 if (iStatus.Int() == KErrNone) |
|
115 { |
|
116 iStatus = KErrTimedOut; |
|
117 } |
|
118 iTimeoutCallBack.CallBack(); |
|
119 } |
|
120 } |
|
121 |
|
122 /** |
|
123 * From CActive |
|
124 */ |
|
125 TInt CPosCallbackTimer::RunError(TInt /*aError*/) |
|
126 { |
|
127 return KErrNone; |
|
128 } |
|
129 |
|
130 /** |
|
131 * Returns the request status of this callback timer. |
|
132 * |
|
133 * @return Returns KErrTimedOut if timer elapsed or KErrCancel |
|
134 * if timer was cancelled. Otherwise any of the system wide |
|
135 * error codes. |
|
136 */ |
|
137 TInt CPosCallbackTimer::Status() |
|
138 { |
|
139 return iStatus.Int(); |
|
140 } |
|
141 |
|
142 void CPosCallbackTimer::ExtendTimeout(const TTimeIntervalMicroSeconds& aAdditionalTime) |
|
143 { |
|
144 if(!IsActive()) |
|
145 { |
|
146 return; |
|
147 } |
|
148 //We do not need to cancel here. Just correct the iNumInternalRounds and iTimeOut |
|
149 |
|
150 TInt64 timeout = iNumInternalRounds * KMaxTInt32 + iTimeOut.Int() + aAdditionalTime.Int64(); |
|
151 iNumInternalRounds = static_cast<TUint32>(timeout / KMaxTInt32); |
|
152 iTimeOut = static_cast<TInt32>(timeout % KMaxTInt32); |
|
153 } |
|
154 |
|
155 // End of File |