|
1 /* |
|
2 * Copyright (c) 2007 Nokia Corporation and/or its subsidiary(-ies). |
|
3 * All rights reserved. |
|
4 * This component and the accompanying materials are made available |
|
5 * under the terms of "Eclipse Public License v1.0" |
|
6 * which accompanies this distribution, and is available |
|
7 * at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
8 * |
|
9 * Initial Contributors: |
|
10 * Nokia Corporation - initial contribution. |
|
11 * |
|
12 * Contributors: |
|
13 * |
|
14 * Description: This module implements linger functionality for not-always-on |
|
15 * connections. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #include "linger.h" |
|
21 #include "logger.h" |
|
22 #include "maosettings.h" |
|
23 |
|
24 const TInt KSecondsToMicro = 1000000; // one second |
|
25 const TInt KDataInactivityInterval = 30; // 30 seconds |
|
26 const TInt KMaxTimerInSeconds = 1800; // 30 minutes |
|
27 |
|
28 |
|
29 // ======== MEMBER FUNCTIONS ======== |
|
30 |
|
31 |
|
32 // --------------------------------------------------------------------------- |
|
33 // CLingerConnection::CLingerConnection |
|
34 // --------------------------------------------------------------------------- |
|
35 // |
|
36 CLingerConnection::CLingerConnection( TUint aConnectionId, |
|
37 TConnectionInfo aConnectionInfo, |
|
38 MAOSettings& aSettings, |
|
39 RSocketServ* aSocketServ) : |
|
40 iConnectionId( aConnectionId ), |
|
41 iConnectionInfo( aConnectionInfo ), |
|
42 iSettings( aSettings ), |
|
43 iSocketServ( aSocketServ ), |
|
44 iLingering( EFalse ), |
|
45 iAttached( EFalse ), |
|
46 iDlData( 0 ), |
|
47 iPckgDlData( iDlData ), |
|
48 iUlData( 0 ), |
|
49 iPckgUlData( iUlData ) |
|
50 { |
|
51 } |
|
52 |
|
53 |
|
54 // --------------------------------------------------------------------------- |
|
55 // CLingerConnection::ConstructL |
|
56 // --------------------------------------------------------------------------- |
|
57 // |
|
58 void CLingerConnection::ConstructL() |
|
59 { |
|
60 LOG_1( _L("CLingerConnection::ConstructL") ); |
|
61 |
|
62 // Create timer |
|
63 iTimer = CPeriodic::NewL( CActive::EPriorityStandard ); |
|
64 } |
|
65 |
|
66 |
|
67 // --------------------------------------------------------------------------- |
|
68 // CLingerConnection::NewL |
|
69 // --------------------------------------------------------------------------- |
|
70 // |
|
71 CLingerConnection* CLingerConnection::NewL( TUint aConnectionId, |
|
72 TConnectionInfo aConnectionInfo, |
|
73 MAOSettings& aSettings, |
|
74 RSocketServ* aSocketServ) |
|
75 { |
|
76 LOG_1( _L("CLingerConnection::NewL") ); |
|
77 |
|
78 CLingerConnection* self = CLingerConnection::NewLC( aConnectionId, |
|
79 aConnectionInfo, |
|
80 aSettings, |
|
81 aSocketServ ); |
|
82 CleanupStack::Pop( self ); |
|
83 return self; |
|
84 } |
|
85 |
|
86 |
|
87 // --------------------------------------------------------------------------- |
|
88 // CLingerConnection::NewLC |
|
89 // --------------------------------------------------------------------------- |
|
90 // |
|
91 CLingerConnection* CLingerConnection::NewLC( TUint aConnectionId, |
|
92 TConnectionInfo aConnectionInfo, |
|
93 MAOSettings& aSettings, |
|
94 RSocketServ* aSocketServ) |
|
95 { |
|
96 LOG_1( _L("CLingerConnection::NewLC") ); |
|
97 |
|
98 CLingerConnection* self = new( ELeave ) CLingerConnection ( aConnectionId, |
|
99 aConnectionInfo, |
|
100 aSettings, |
|
101 aSocketServ ); |
|
102 CleanupStack::PushL( self ); |
|
103 self->ConstructL(); |
|
104 return self; |
|
105 } |
|
106 |
|
107 |
|
108 // --------------------------------------------------------------------------- |
|
109 // ~CLingerConnection |
|
110 // --------------------------------------------------------------------------- |
|
111 // |
|
112 CLingerConnection::~CLingerConnection() |
|
113 { |
|
114 LOG_1( _L("CLingerConnection::~CLingerConnection") ); |
|
115 |
|
116 // Cancel timer |
|
117 CancelTimer(); |
|
118 |
|
119 // Delete timer |
|
120 delete iTimer; |
|
121 |
|
122 // Close RConnection object |
|
123 CloseConnection(); |
|
124 } |
|
125 |
|
126 // --------------------------------------------------------------------------- |
|
127 // CLingerConnection::StartLinger |
|
128 // --------------------------------------------------------------------------- |
|
129 // |
|
130 TInt CLingerConnection::StartLinger() |
|
131 { |
|
132 LOG_1( _L("CLingerConnection::StartLinger") ); |
|
133 |
|
134 if ( !iAttached ) |
|
135 { |
|
136 // Read settings |
|
137 ReadSettings(); |
|
138 |
|
139 if ( iLingerInterval != 0 ) |
|
140 { |
|
141 TInt err = OpenAndAttach(); |
|
142 |
|
143 if ( err != KErrNone ) |
|
144 { |
|
145 // Write to log |
|
146 LOG_2( _L("OpenAndAttach: err: %d"), err ); |
|
147 return( err ); |
|
148 } |
|
149 |
|
150 if ( iLingerInterval > 0 ) |
|
151 { |
|
152 // Start timer |
|
153 StartTimer( KDataInactivityInterval ); |
|
154 |
|
155 LOG_1( _L("Linger timer started OK") ); |
|
156 } |
|
157 |
|
158 } |
|
159 } |
|
160 |
|
161 return KErrNone; |
|
162 } |
|
163 |
|
164 // --------------------------------------------------------------------------- |
|
165 // CLingerConnection::StopLinger |
|
166 // --------------------------------------------------------------------------- |
|
167 // |
|
168 void CLingerConnection::StopLinger() |
|
169 { |
|
170 LOG_1( _L("CLingerConnection::StopLinger") ); |
|
171 |
|
172 if ( iAttached ) |
|
173 { |
|
174 CancelTimer(); |
|
175 CloseConnection(); |
|
176 iLingering = EFalse; |
|
177 |
|
178 LOG_1( _L("Linger timer stopped") ); |
|
179 } |
|
180 } |
|
181 |
|
182 // --------------------------------------------------------------------------- |
|
183 // CLingerConnection::StopConnection |
|
184 // --------------------------------------------------------------------------- |
|
185 // |
|
186 TInt CLingerConnection::StopConnection() |
|
187 { |
|
188 LOG_1( _L("CLingerConnection::StopConnection") ); |
|
189 |
|
190 if ( !iAttached ) |
|
191 { |
|
192 return KErrNotSupported; |
|
193 } |
|
194 |
|
195 // Must first stop connection |
|
196 TInt err = iConnection.Stop( RConnection::EStopAuthoritative ); |
|
197 |
|
198 // Stop linger & close handles |
|
199 if ( err == KErrNone ) |
|
200 { |
|
201 StopLinger(); |
|
202 } |
|
203 |
|
204 return err; |
|
205 } |
|
206 |
|
207 // --------------------------------------------------------------------------- |
|
208 // CLingerConnection::HandleSettingsChanged |
|
209 // --------------------------------------------------------------------------- |
|
210 // |
|
211 void CLingerConnection::HandleSettingsChanged() |
|
212 { |
|
213 LOG_1( _L("CLingerConnection::HandleSettingsChangedL") ); |
|
214 |
|
215 TInt oldLingerInterval( iLingerInterval ); |
|
216 |
|
217 ReadSettings(); |
|
218 |
|
219 if ( iLingerInterval != oldLingerInterval ) |
|
220 { |
|
221 if ( iLingerInterval == 0 ) |
|
222 { |
|
223 // Linger was turned off |
|
224 StopLinger(); |
|
225 return; |
|
226 } |
|
227 |
|
228 if ( iLingering ) |
|
229 { |
|
230 if ( iLingerInterval > 0 ) |
|
231 { |
|
232 // Linger timer has a new positive value |
|
233 CancelTimer(); |
|
234 StartTimer( iLingerInterval ); |
|
235 } |
|
236 else |
|
237 { |
|
238 // Linger timer has a new negative value |
|
239 // -> linger forever |
|
240 CancelTimer(); |
|
241 iLingering = EFalse; |
|
242 } |
|
243 } |
|
244 else if ( iAttached ) |
|
245 { |
|
246 if ( ( oldLingerInterval > 0 ) && ( iLingerInterval < 0 ) ) |
|
247 { |
|
248 // Linger timer has a new negative value |
|
249 // -> linger forever |
|
250 CancelTimer(); |
|
251 } |
|
252 else if ( ( oldLingerInterval < 0 ) && ( iLingerInterval > 0 ) ) |
|
253 { |
|
254 // Linger timer has a new positive value |
|
255 CancelTimer(); |
|
256 StartTimer( KDataInactivityInterval ); |
|
257 } |
|
258 } |
|
259 else // not attached, not lingering |
|
260 { |
|
261 StartLinger(); |
|
262 } |
|
263 } |
|
264 } |
|
265 |
|
266 // --------------------------------------------------------------------------- |
|
267 // CLingerConnection::Status |
|
268 // --------------------------------------------------------------------------- |
|
269 // |
|
270 TInt CLingerConnection::Status() |
|
271 { |
|
272 if ( iLingering ) |
|
273 { |
|
274 return ELingerRunning; |
|
275 } |
|
276 else if ( iAttached ) |
|
277 { |
|
278 return ELingerAttached; |
|
279 } |
|
280 else |
|
281 { |
|
282 return ELingerStopped; |
|
283 } |
|
284 } |
|
285 |
|
286 // --------------------------------------------------------------------------- |
|
287 // CLingerConnection::OpenAndAttach |
|
288 // --------------------------------------------------------------------------- |
|
289 // |
|
290 TInt CLingerConnection::OpenAndAttach() |
|
291 { |
|
292 LOG_1( _L("CLingerConnection::OpenAndAttach") ); |
|
293 |
|
294 if ( !iAttached ) |
|
295 { |
|
296 // Open |
|
297 TInt err = iConnection.Open( *iSocketServ, KAfInet ); |
|
298 |
|
299 if ( err != KErrNone ) |
|
300 { |
|
301 return ( err ); |
|
302 } |
|
303 |
|
304 // Attach to keep the connection open |
|
305 err = iConnection.Attach( TPckg< TConnectionInfo >( iConnectionInfo ), |
|
306 RConnection::EAttachTypeNormal ); |
|
307 |
|
308 if ( err != KErrNone ) |
|
309 { |
|
310 iConnection.Close(); |
|
311 return ( err ); |
|
312 } |
|
313 |
|
314 iAttached = ETrue; |
|
315 } |
|
316 |
|
317 return KErrNone; |
|
318 } |
|
319 |
|
320 // --------------------------------------------------------------------------- |
|
321 // CLingerConnection::CloseConnection |
|
322 // --------------------------------------------------------------------------- |
|
323 // |
|
324 void CLingerConnection::CloseConnection() |
|
325 { |
|
326 LOG_1( _L("CLingerConnection::CloseConnection") ); |
|
327 |
|
328 // Close RConnection object |
|
329 if ( iAttached ) |
|
330 { |
|
331 iConnection.Close(); |
|
332 |
|
333 // To really close: re-open & re-close |
|
334 TInt err = iConnection.Open( *iSocketServ, KAfInet ); |
|
335 |
|
336 if ( err == KErrNone ) |
|
337 { |
|
338 iConnection.Close(); |
|
339 } |
|
340 |
|
341 iAttached = EFalse; |
|
342 } |
|
343 } |
|
344 |
|
345 // --------------------------------------------------------------------------- |
|
346 // CLingerConnection::IsConnectionIdle |
|
347 // --------------------------------------------------------------------------- |
|
348 // |
|
349 TBool CLingerConnection::IsConnectionIdle() |
|
350 { |
|
351 LOG_1( _L("CLingerConnection::IsConnectionIdle") ); |
|
352 |
|
353 TUint currentDlData( iDlData ); |
|
354 TUint currentUlData( iUlData ); |
|
355 |
|
356 if ( !iAttached ) |
|
357 { |
|
358 return ETrue; |
|
359 } |
|
360 |
|
361 LOG_2( _L("old Uplink data volume: %d"), currentUlData ); |
|
362 LOG_2( _L("old Downlink data volume: %d"), currentDlData ); |
|
363 |
|
364 // get new data volumes from ESock |
|
365 iDlData = 0; |
|
366 iUlData = 0; |
|
367 |
|
368 TRequestStatus status( KErrNone ); |
|
369 |
|
370 iConnection.DataTransferredRequest( iPckgUlData, iPckgDlData, status ); |
|
371 |
|
372 User::WaitForRequest( status ); |
|
373 |
|
374 LOG_2( _L("new Uplink data volume: %d"), iUlData ); |
|
375 LOG_2( _L("new Downlink data volume: %d"), iDlData ); |
|
376 |
|
377 if ( status == KErrNone ) |
|
378 { |
|
379 if ( ( iUlData == currentUlData ) && ( iDlData == currentDlData ) ) |
|
380 { |
|
381 return ETrue; |
|
382 } |
|
383 else |
|
384 { |
|
385 return EFalse; |
|
386 } |
|
387 } |
|
388 else |
|
389 { |
|
390 return EFalse; |
|
391 } |
|
392 } |
|
393 |
|
394 // --------------------------------------------------------------------------- |
|
395 // CLingerConnection::ReadSettings |
|
396 // --------------------------------------------------------------------------- |
|
397 // |
|
398 void CLingerConnection::ReadSettings() |
|
399 { |
|
400 LOG_1( _L("CLingerConnection::ReadSettings") ); |
|
401 |
|
402 iLingerInterval = iSettings.LingerTimerValue( iConnectionInfo.iIapId ); |
|
403 } |
|
404 |
|
405 |
|
406 // --------------------------------------------------------------------------- |
|
407 // CLingerConnection::StartTimer() |
|
408 // --------------------------------------------------------------------------- |
|
409 // |
|
410 void CLingerConnection::StartTimer( TInt aTimerInterval, TBool aReset ) |
|
411 { |
|
412 LOG_1( _L("CLingerConnection::StartTimer") ); |
|
413 |
|
414 if ( aReset ) |
|
415 { |
|
416 iLingerTimerCount = 0; |
|
417 } |
|
418 |
|
419 TCallBack cb( TimerCallBack, this ); |
|
420 |
|
421 // if linger timer is negative we should not start timer but linger forever... |
|
422 if ( iLingerInterval > 0 ) |
|
423 { |
|
424 LOG_2( _L("iTimer->Start: aTimerInterval: %d"), aTimerInterval ); |
|
425 |
|
426 if( aTimerInterval > KMaxTimerInSeconds ) |
|
427 { |
|
428 // Maximum allowed interval is 30 minutes. |
|
429 // This restriction comes from TTimeIntervalMicroSeconds32 |
|
430 iCurrentTimerInterval = KMaxTimerInSeconds; |
|
431 } |
|
432 else |
|
433 { |
|
434 // use current value |
|
435 iCurrentTimerInterval = aTimerInterval; |
|
436 } |
|
437 |
|
438 LOG_2( _L("iTimer->Start: iCurrentTimerInterval: %d"), iCurrentTimerInterval ); |
|
439 CancelTimer(); |
|
440 iTimer->Start( ( iCurrentTimerInterval * KSecondsToMicro ), |
|
441 ( iCurrentTimerInterval * KSecondsToMicro ), |
|
442 cb ); |
|
443 } |
|
444 } |
|
445 |
|
446 // --------------------------------------------------------------------------- |
|
447 // CLingerConnection::CancelTimer() |
|
448 // --------------------------------------------------------------------------- |
|
449 // |
|
450 void CLingerConnection::CancelTimer() |
|
451 { |
|
452 LOG_1( _L("CLingerConnection::CancelTimer") ); |
|
453 |
|
454 if( iTimer->IsActive() ) |
|
455 { |
|
456 iTimer->Cancel(); |
|
457 } |
|
458 } |
|
459 |
|
460 // --------------------------------------------------------------------------- |
|
461 // CLingerConnection::TimerCallBack |
|
462 // --------------------------------------------------------------------------- |
|
463 // |
|
464 TInt CLingerConnection::TimerCallBack( TAny* aSelf ) |
|
465 { |
|
466 LOG_1( _L("CLingerConnection::TimerCallBack") ); |
|
467 |
|
468 CLingerConnection* self = static_cast< CLingerConnection* >( aSelf ); |
|
469 |
|
470 if ( self->iLingering ) |
|
471 { |
|
472 // Maximum allowed timer interval is 30 min |
|
473 // This restriction comes from TTimeIntervalMicroSeconds32 |
|
474 self->iLingerTimerCount++; |
|
475 |
|
476 TInt currentlyRanInSec = |
|
477 self->iCurrentTimerInterval + ( ( self->iLingerTimerCount - 1 ) * KMaxTimerInSeconds ); |
|
478 |
|
479 if( self->iLingerInterval != currentlyRanInSec ) |
|
480 { |
|
481 // Get remaining time in seconds |
|
482 TInt timeRemainingInSec = self->iLingerInterval - currentlyRanInSec; |
|
483 |
|
484 // Check if remaining time is longer than 30 minutes |
|
485 if ( timeRemainingInSec > KMaxTimerInSeconds ) |
|
486 { |
|
487 // Current interval is 30 minutes |
|
488 self->iCurrentTimerInterval = KMaxTimerInSeconds; |
|
489 } |
|
490 else |
|
491 { |
|
492 // use time that is left |
|
493 self->iCurrentTimerInterval = timeRemainingInSec; |
|
494 } |
|
495 |
|
496 // Restart timer to reach iLingerInterval |
|
497 self->iTimer->Cancel(); |
|
498 self->StartTimer( self->iCurrentTimerInterval, EFalse ); |
|
499 |
|
500 return KErrNone; |
|
501 } |
|
502 } |
|
503 |
|
504 if ( self->IsConnectionIdle() ) |
|
505 { |
|
506 if ( self->iLingering ) |
|
507 { |
|
508 // Connection has been idle during lingering period e.g. 60 min. |
|
509 // Stop connection. |
|
510 TInt err = self->StopConnection(); |
|
511 |
|
512 if ( err == KErrNone ) |
|
513 { |
|
514 self->iTimer->Cancel(); |
|
515 self->iLingering = EFalse; |
|
516 } |
|
517 } |
|
518 else |
|
519 { |
|
520 // Connection has been idle during data inactivity period e.g 30 s. |
|
521 // Start lingering timer. |
|
522 self->iTimer->Cancel(); |
|
523 self->StartTimer( self->iLingerInterval ); |
|
524 self->iLingering = ETrue; |
|
525 } |
|
526 } |
|
527 else |
|
528 { |
|
529 // There was data trasfer through the connection |
|
530 if ( self->iLingering ) |
|
531 { |
|
532 // Start monitoring data transfer with KDataInactivityInterval |
|
533 // if connection was already in lingering mode. |
|
534 self->iTimer->Cancel(); |
|
535 self->StartTimer( KDataInactivityInterval ); |
|
536 self->iLingering = EFalse; |
|
537 } |
|
538 } |
|
539 |
|
540 return KErrNone; |
|
541 } |