|
1 /* |
|
2 * Copyright (c) 2002-2005 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 the License "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: Factory class for timers |
|
15 * |
|
16 */ |
|
17 |
|
18 |
|
19 #include "wlanscanresultcache.h" |
|
20 #include "am_debug.h" |
|
21 |
|
22 // ============================ MEMBER FUNCTIONS =============================== |
|
23 |
|
24 // ----------------------------------------------------------------------------- |
|
25 // CWlanScanResultCache::NewL |
|
26 // ----------------------------------------------------------------------------- |
|
27 // |
|
28 CWlanScanResultCache* CWlanScanResultCache::NewL() |
|
29 { |
|
30 DEBUG( "CWlanScanResultCache::NewL()" ); |
|
31 CWlanScanResultCache* self = new(ELeave)CWlanScanResultCache(); |
|
32 CleanupStack::PushL( self ); |
|
33 self->ConstructL(); |
|
34 CleanupStack::Pop( self ); |
|
35 return self; |
|
36 } |
|
37 |
|
38 // ----------------------------------------------------------------------------- |
|
39 // CWlanScanResultCache::ConstructL |
|
40 // ----------------------------------------------------------------------------- |
|
41 // |
|
42 void CWlanScanResultCache::ConstructL() |
|
43 { |
|
44 DEBUG( "CWlanScanResultCache::ConstructL()" ); |
|
45 |
|
46 iScanList = new(ELeave) ScanList(); |
|
47 } |
|
48 |
|
49 // ----------------------------------------------------------------------------- |
|
50 // CWlanScanResultCache::CWlanScanResultCache |
|
51 // ----------------------------------------------------------------------------- |
|
52 // |
|
53 CWlanScanResultCache::CWlanScanResultCache() : |
|
54 iScanList( NULL ), |
|
55 iScanListTimeStamp( 0 ), |
|
56 iIapListTimeStamp( 0 ) |
|
57 { |
|
58 DEBUG( "CWlanScanResultCache::CWlanScanResultCache()" ); |
|
59 } |
|
60 |
|
61 // ----------------------------------------------------------------------------- |
|
62 // CWlanScanResultCache::~CWlanScanResultCache |
|
63 // ----------------------------------------------------------------------------- |
|
64 // |
|
65 CWlanScanResultCache::~CWlanScanResultCache() |
|
66 { |
|
67 DEBUG( "CWlanScanResultCache::~CWlanScanResultCache()" ); |
|
68 |
|
69 delete iScanList; |
|
70 iIapList.Close(); |
|
71 iAvailableIapList.Close(); |
|
72 iAvailableNetworkList.Close(); |
|
73 } |
|
74 |
|
75 // ----------------------------------------------------------------------------- |
|
76 // CWlanScanResultCache::UpdateScanList |
|
77 // ----------------------------------------------------------------------------- |
|
78 // |
|
79 void CWlanScanResultCache::UpdateScanList( |
|
80 ScanList* aScanList ) |
|
81 { |
|
82 DEBUG( "CWlanScanResultCache::UpdateScanList()" ); |
|
83 |
|
84 delete iScanList; |
|
85 iScanList = aScanList; // take ownership |
|
86 iScanListTimeStamp.HomeTime(); |
|
87 } |
|
88 |
|
89 // ----------------------------------------------------------------------------- |
|
90 // CWlanScanResultCache::GetScanList |
|
91 // ----------------------------------------------------------------------------- |
|
92 // |
|
93 ScanList* CWlanScanResultCache::GetScanList( |
|
94 TUint aCacheLifetime ) |
|
95 { |
|
96 DEBUG( "CWlanScanResultCache::GetScanList()" ); |
|
97 |
|
98 TTime currentTime(0); |
|
99 currentTime.HomeTime(); |
|
100 TTimeIntervalSeconds difference(0); |
|
101 |
|
102 TInt overflow = currentTime.SecondsFrom( iScanListTimeStamp, difference ); |
|
103 if( difference.Int() > aCacheLifetime || difference.Int() < 0 || overflow ) |
|
104 { |
|
105 DEBUG2( "CWlanScanResultCache::GetScanList() - results are too old, difference is %d, overflow is %d", |
|
106 difference.Int(), overflow ); |
|
107 return NULL; |
|
108 } |
|
109 else |
|
110 { |
|
111 DEBUG1( "CWlanScanResultCache::GetScanList() - old results are still valid, difference is %u", |
|
112 difference.Int() ); |
|
113 return iScanList; |
|
114 } |
|
115 } |
|
116 |
|
117 // ----------------------------------------------------------------------------- |
|
118 // CWlanScanResultCache::UpdateAvailableNetworksList |
|
119 // ----------------------------------------------------------------------------- |
|
120 // |
|
121 void CWlanScanResultCache::UpdateAvailableNetworksList( |
|
122 core_type_list_c<u32_t>& aIapIdList, |
|
123 RArray<TWlanAvailableNetwork>& aNetworkList, |
|
124 TBool& aNewIapsAvailable, |
|
125 TBool& aOldIapsLost ) |
|
126 { |
|
127 DEBUG( "CWlanScanResultCache::UpdateAvailableNetworksList()" ); |
|
128 |
|
129 DEBUG1( "CWlanScanResultCache::UpdateAvailableNetworksList() - %u IAP(s) available", |
|
130 aIapIdList.count() ); |
|
131 DEBUG1( "CWlanScanResultCache::UpdateAvailableNetworksList() - %u IAP(s) previously available", |
|
132 iAvailableIapList.Count() ); |
|
133 DEBUG1( "CWlanScanResultCache::UpdateAvailableNetworksList() - %u networks(s) available", |
|
134 aNetworkList.Count() ); |
|
135 DEBUG1( "CWlanScanResultCache::UpdateAvailableNetworksList() - %u networks(s) previously available", |
|
136 iAvailableNetworkList.Count() ); |
|
137 |
|
138 const TInt oldIapCount( iAvailableIapList.Count() ); |
|
139 const TInt newNetworkCount( aNetworkList.Count() ); |
|
140 const TInt oldNetworkCount( iAvailableNetworkList.Count() ); |
|
141 aNewIapsAvailable = EFalse; |
|
142 aOldIapsLost = EFalse; |
|
143 TIdentityRelation<TWlanAvailableNetwork> isEqual( CWlanScanResultCache::IsNetworkEqual ); |
|
144 |
|
145 // Iterate through previously available IAPs to find lost IAPs. |
|
146 TInt idx( 0 ); |
|
147 //while( idx < oldIapCount && !aOldIapsLost ) |
|
148 while( idx < oldIapCount ) |
|
149 { |
|
150 const TUint32* newId = aIapIdList.first(); |
|
151 |
|
152 while( newId ) |
|
153 { |
|
154 if( *newId == iAvailableIapList[idx] ) |
|
155 { |
|
156 break; |
|
157 } |
|
158 |
|
159 newId = aIapIdList.next(); |
|
160 } |
|
161 |
|
162 if( !newId ) |
|
163 { |
|
164 DEBUG1( "CWlanScanResultCache::UpdateAvailableNetworksList() - old IAP %u has been lost", |
|
165 iAvailableIapList[idx] ); |
|
166 aOldIapsLost = ETrue; |
|
167 } |
|
168 |
|
169 ++idx; |
|
170 } |
|
171 |
|
172 // Iterate through available IAPs to find new IAPs. |
|
173 const TUint32* newId = aIapIdList.first(); |
|
174 //while( newId && !aNewIapsAvailable ) |
|
175 while( newId ) |
|
176 { |
|
177 if ( iAvailableIapList.Find( *newId ) == KErrNotFound ) |
|
178 { |
|
179 DEBUG1( "CWlanScanResultCache::UpdateAvailableNetworksList() - new IAP %u has been detected", |
|
180 *newId ); |
|
181 aNewIapsAvailable = ETrue; |
|
182 } |
|
183 |
|
184 newId = aIapIdList.next(); |
|
185 } |
|
186 |
|
187 // Iterate through previously available networks to find lost networks. |
|
188 idx = 0; |
|
189 //while ( idx < oldNetworkCount && !aOldIapsLost ) |
|
190 while ( idx < oldNetworkCount ) |
|
191 { |
|
192 if ( aNetworkList.Find( iAvailableNetworkList[idx], isEqual ) == KErrNotFound ) |
|
193 { |
|
194 DEBUG1S( "CWlanScanResultCache::UpdateAvailableNetworksList() - old network has been lost, SSID ", |
|
195 iAvailableNetworkList[idx].ssid.Length(), iAvailableNetworkList[idx].ssid.Ptr() ); |
|
196 aOldIapsLost = ETrue; |
|
197 } |
|
198 ++idx; |
|
199 } |
|
200 |
|
201 // Iterate through available networks to find new networks. |
|
202 idx = 0; |
|
203 //while( idx < newNetworkCount && !aNewIapsAvailable ) |
|
204 while( idx < newNetworkCount ) |
|
205 { |
|
206 if ( iAvailableNetworkList.Find( aNetworkList[idx], isEqual ) == KErrNotFound ) |
|
207 { |
|
208 DEBUG1S( "CWlanScanResultCache::UpdateAvailableNetworksList() - new network has been detected, SSID ", |
|
209 aNetworkList[idx].ssid.Length(), aNetworkList[idx].ssid.Ptr() ); |
|
210 aNewIapsAvailable = ETrue; |
|
211 } |
|
212 ++idx; |
|
213 } |
|
214 |
|
215 // Update the list |
|
216 iAvailableIapList.Reset(); |
|
217 iAvailableNetworkList.Reset(); |
|
218 iIapListTimeStamp.HomeTime(); |
|
219 |
|
220 newId = aIapIdList.first(); |
|
221 while( newId ) |
|
222 { |
|
223 iAvailableIapList.Append( *newId ); |
|
224 newId = aIapIdList.next(); |
|
225 } |
|
226 |
|
227 idx = 0; |
|
228 while( idx < newNetworkCount ) |
|
229 { |
|
230 iAvailableNetworkList.Append( aNetworkList[idx] ); |
|
231 ++idx; |
|
232 } |
|
233 } |
|
234 |
|
235 // ----------------------------------------------------------------------------- |
|
236 // CWlanScanResultCache::AvailableIaps |
|
237 // ----------------------------------------------------------------------------- |
|
238 // |
|
239 RArray<TUint>* CWlanScanResultCache::AvailableIaps( |
|
240 RArray<TWlanLimitedIapData>& aIapList, |
|
241 TUint aCacheLifetime ) |
|
242 { |
|
243 DEBUG( "CWlanScanResultCache::AvailableIaps()" ); |
|
244 |
|
245 /** |
|
246 * Take a copy of the previous IAP list since GetIapDataList() will overwrite |
|
247 * the member variable. |
|
248 */ |
|
249 RArray<TWlanLimitedIapData> iapList; |
|
250 for( TInt idx( 0 ); idx < iIapList.Count(); ++idx ) |
|
251 { |
|
252 iapList.Append( iIapList[idx] ); |
|
253 } |
|
254 |
|
255 TInt ret = GetIapDataList( aIapList ); |
|
256 if ( ret != KErrNone ) |
|
257 { |
|
258 DEBUG( "CWlanScanResultCache::AvailableIaps() - unable to read list of IAPs, assuming no cache" ); |
|
259 iapList.Close(); |
|
260 |
|
261 return NULL; |
|
262 } |
|
263 |
|
264 TTime currentTime(0); |
|
265 currentTime.HomeTime(); |
|
266 TTimeIntervalSeconds difference(0); |
|
267 |
|
268 TInt overflow = currentTime.SecondsFrom( iIapListTimeStamp, difference ); |
|
269 if( difference.Int() > aCacheLifetime || difference.Int() < 0 || overflow ) |
|
270 { |
|
271 DEBUG2( "CWlanScanResultCache::AvailableIaps() - results are too old, difference is %d, overflow is %d", |
|
272 difference.Int(), overflow ); |
|
273 iapList.Close(); |
|
274 |
|
275 return NULL; |
|
276 } |
|
277 |
|
278 /** |
|
279 * Check whether the list of IAPs has change since last GetIapDataList(). |
|
280 */ |
|
281 if ( !IsIapListEqual( aIapList, iapList ) ) |
|
282 { |
|
283 DEBUG( "CWlanScanResultCache::AvailableIaps() - cache time is still valid but IAPs have changed" ); |
|
284 iapList.Close(); |
|
285 |
|
286 return NULL; |
|
287 } |
|
288 |
|
289 DEBUG1( "CWlanScanResultCache::AvailableIaps() - old results are still valid, difference is %u", |
|
290 difference.Int() ); |
|
291 iapList.Close(); |
|
292 |
|
293 return &iAvailableIapList; |
|
294 } |
|
295 |
|
296 // ----------------------------------------------------------------------------- |
|
297 // CWlanScanResultCache::GetIapDataList |
|
298 // ----------------------------------------------------------------------------- |
|
299 // |
|
300 TInt CWlanScanResultCache::GetIapDataList( |
|
301 RArray<TWlanLimitedIapData>& aIapList ) |
|
302 { |
|
303 DEBUG( "CWlanScanResultCache::GetIapDataList()" ); |
|
304 |
|
305 aIapList.Reset(); |
|
306 |
|
307 /** |
|
308 * Read all WLAN IAPs from commsdat. |
|
309 */ |
|
310 CWLanSettings wlanSettings; |
|
311 TInt ret = wlanSettings.Connect(); |
|
312 if ( ret != KErrNone ) |
|
313 { |
|
314 DEBUG1( "CWlanScanResultCache::GetIapDataList() - CWLanSettings::Connect() failed (%d)", |
|
315 ret ); |
|
316 |
|
317 return ret; |
|
318 } |
|
319 |
|
320 RArray<SWlanIAPId> wlanIapIds; |
|
321 TRAP( ret, wlanSettings.GetIAPWlanServicesL( wlanIapIds ) ); |
|
322 if( ret != KErrNone ) |
|
323 { |
|
324 DEBUG1( "CWlanScanResultCache::GetIapDataList() - CWLanSettings::GetIAPWlanServicesL() failed (%d)", |
|
325 ret ); |
|
326 wlanIapIds.Close(); |
|
327 wlanSettings.Disconnect(); |
|
328 |
|
329 return ret; |
|
330 } |
|
331 |
|
332 /** |
|
333 * Retrieve IAP data for each IAP id. |
|
334 */ |
|
335 DEBUG1( "CWlanScanResultCache::GetIapDataList() - reading %u WLAN IAPs", |
|
336 wlanIapIds.Count() ); |
|
337 |
|
338 for( TInt i( 0 ); i < wlanIapIds.Count(); i++ ) |
|
339 { |
|
340 TWlanLimitedIapData iap; |
|
341 iap.iapId = wlanIapIds[i].iIAPId; |
|
342 iap.serviceId = wlanIapIds[i].iWLANRecordId; |
|
343 |
|
344 DEBUG1( "CWlanScanResultCache::GetIapDataList() - reading IAP %u", |
|
345 wlanIapIds[i].iIAPId ); |
|
346 |
|
347 SWLANSettings iapData; |
|
348 ret = wlanSettings.GetWlanSettings( |
|
349 wlanIapIds[i].iWLANRecordId, |
|
350 iapData ); |
|
351 if( ret != KErrNone ) |
|
352 { |
|
353 DEBUG2( "CWlanScanResultCache::GetIapDataList() - reading of IAP %u failed (%d), ignoring", |
|
354 wlanIapIds[i].iIAPId, ret ); |
|
355 } |
|
356 else if( iapData.SSID.Length() ) // filter out EasyWlan IAP |
|
357 { |
|
358 iap.ssid.Copy( iapData.SSID ); |
|
359 iap.usedSsid.Copy( iapData.UsedSSID ); |
|
360 iap.networkType = static_cast<EConnectionMode>( iapData.ConnectionMode ); |
|
361 iap.securityMode = static_cast<EWlanSecurityMode>( iapData.SecurityMode ); |
|
362 iap.isPskEnabled = iapData.EnableWpaPsk; |
|
363 iap.isHidden = iapData.ScanSSID; |
|
364 |
|
365 aIapList.Append( iap ); |
|
366 } |
|
367 } |
|
368 |
|
369 UpdateIapList( aIapList ); |
|
370 wlanIapIds.Close(); |
|
371 wlanSettings.Disconnect(); |
|
372 |
|
373 return KErrNone; |
|
374 } |
|
375 |
|
376 // ----------------------------------------------------------------------------- |
|
377 // CWlanScanResultCache::IsNetworkEqual |
|
378 // ----------------------------------------------------------------------------- |
|
379 // |
|
380 TBool CWlanScanResultCache::IsNetworkEqual( |
|
381 const TWlanAvailableNetwork& aFirst, |
|
382 const TWlanAvailableNetwork& aSecond ) |
|
383 { |
|
384 if ( aFirst.ssid != aSecond.ssid || |
|
385 aFirst.networkType != aSecond.networkType || |
|
386 aFirst.securityMode != aSecond.securityMode ) |
|
387 { |
|
388 return EFalse; |
|
389 } |
|
390 |
|
391 return ETrue; |
|
392 } |
|
393 |
|
394 // ----------------------------------------------------------------------------- |
|
395 // CWlanScanResultCache::IsIapListEqual |
|
396 // ----------------------------------------------------------------------------- |
|
397 // |
|
398 TBool CWlanScanResultCache::IsIapListEqual( |
|
399 const RArray<TWlanLimitedIapData>& aFirst, |
|
400 const RArray<TWlanLimitedIapData>& aSecond ) |
|
401 { |
|
402 /** |
|
403 * If the amount of IAPs is different, it's an obvious fault. |
|
404 */ |
|
405 if ( aFirst.Count() != aSecond.Count() ) |
|
406 { |
|
407 DEBUG2( "CWlanScanResultCache::GetIapDataList() - false, amount of IAPs is different (%u vs %u)", |
|
408 aFirst.Count(), aSecond.Count() ); |
|
409 |
|
410 return EFalse; |
|
411 } |
|
412 |
|
413 /** |
|
414 * Some of the settings might have changed. |
|
415 */ |
|
416 for( TInt i( 0 ); i < aFirst.Count(); ++i ) |
|
417 { |
|
418 TBool isFound( EFalse ); |
|
419 for ( TInt j( 0 ); j < aSecond.Count() && !isFound; ++j ) |
|
420 { |
|
421 if ( aFirst[i].iapId == aSecond[j].iapId ) |
|
422 { |
|
423 isFound = ETrue; |
|
424 if ( aFirst[i].ssid != aSecond[j].ssid ) |
|
425 { |
|
426 DEBUG1( "CWlanScanResultCache::GetIapDataList() - false, IAP %u has mismatching SSID", |
|
427 aFirst[i].iapId ); |
|
428 |
|
429 return EFalse; |
|
430 } |
|
431 else if ( aFirst[i].networkType != aSecond[j].networkType ) |
|
432 { |
|
433 DEBUG1( "CWlanScanResultCache::GetIapDataList() - false, IAP %u has mismatching network mode", |
|
434 aFirst[i].iapId ); |
|
435 |
|
436 return EFalse; |
|
437 } |
|
438 else if ( aFirst[i].securityMode != aSecond[j].securityMode || |
|
439 aFirst[i].isPskEnabled != aSecond[j].isPskEnabled ) |
|
440 { |
|
441 DEBUG1( "CWlanScanResultCache::GetIapDataList() - false, IAP %u has mismatching security mode", |
|
442 aFirst[i].iapId ); |
|
443 |
|
444 return EFalse; |
|
445 } |
|
446 else if ( aFirst[i].isHidden != aSecond[j].isHidden ) |
|
447 { |
|
448 DEBUG1( "CWlanScanResultCache::GetIapDataList() - false, IAP %u has mismatching hidden setting", |
|
449 aFirst[i].iapId ); |
|
450 |
|
451 return EFalse; |
|
452 } |
|
453 } |
|
454 } |
|
455 if ( !isFound ) |
|
456 { |
|
457 DEBUG1( "CWlanScanResultCache::GetIapDataList() - false, IAP %u not found in previous list", |
|
458 aFirst[i].iapId ); |
|
459 |
|
460 return EFalse; |
|
461 } |
|
462 } |
|
463 |
|
464 DEBUG( "CWlanScanResultCache::GetIapDataList() - true, IAP lists are equal" ); |
|
465 |
|
466 return ETrue; |
|
467 } |
|
468 |
|
469 // ----------------------------------------------------------------------------- |
|
470 // CWlanScanResultCache::UpdateIapList |
|
471 // ----------------------------------------------------------------------------- |
|
472 // |
|
473 void CWlanScanResultCache::UpdateIapList( |
|
474 const RArray<TWlanLimitedIapData>& aIapDataList ) |
|
475 { |
|
476 iIapList.Reset(); |
|
477 TUint32 arraySize( 0 ); |
|
478 |
|
479 for( TInt idx( 0 ); idx < aIapDataList.Count(); ++idx ) |
|
480 { |
|
481 arraySize += sizeof( aIapDataList[idx] ); |
|
482 iIapList.Append( aIapDataList[idx] ); |
|
483 } |
|
484 |
|
485 DEBUG1( "CWlanScanResultCache::UpdateIapList() - total size of IAP list is %u bytes", |
|
486 arraySize ); |
|
487 } |
|
488 |
|
489 // ----------------------------------------------------------------------------- |
|
490 // CWlanScanResultCache::CachedIapDataList |
|
491 // ----------------------------------------------------------------------------- |
|
492 // |
|
493 const RArray<TWlanLimitedIapData>& CWlanScanResultCache::CachedIapDataList() const |
|
494 { |
|
495 return iIapList; |
|
496 } |
|
497 |
|
498 // ----------------------------------------------------------------------------- |
|
499 // CWlanScanResultCache::InvalidateAvailabilityCache |
|
500 // ----------------------------------------------------------------------------- |
|
501 // |
|
502 void CWlanScanResultCache::InvalidateAvailabilityCache() |
|
503 { |
|
504 iIapListTimeStamp = 0; |
|
505 } |