|
1 /* |
|
2 * Copyright (c) 2009 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: This class implements BgScan AWS communication. |
|
15 * |
|
16 */ |
|
17 |
|
18 /* |
|
19 * %version: 2 % |
|
20 */ |
|
21 |
|
22 #include <e32base.h> |
|
23 #include <e32atomics.h> |
|
24 |
|
25 #include "awsinterface.h" |
|
26 #include "awsenginebase.h" |
|
27 #include "wlanbgscancommandlistener.h" |
|
28 #include "wlanbgscancommand.h" |
|
29 #include "wlanbgscanawscomms.h" |
|
30 #include "wlanbgscan.h" |
|
31 #include "am_debug.h" |
|
32 |
|
33 /** |
|
34 * Maximum heap size for AWS thread. |
|
35 */ |
|
36 const TInt KMaxHeapSize = 0x10000; |
|
37 |
|
38 /** |
|
39 * BgScan version communicated to AWS. |
|
40 */ |
|
41 const TUint KBgScanVersion = 1; |
|
42 |
|
43 /** |
|
44 * Index of the first item in an array. |
|
45 */ |
|
46 const TInt KFirstItemIndex = 0; |
|
47 |
|
48 // ======== MEMBER FUNCTIONS ======== |
|
49 |
|
50 // --------------------------------------------------------------------------- |
|
51 // CWlanBgScanAwsComms::CWlanBgScanAwsComms |
|
52 // --------------------------------------------------------------------------- |
|
53 // |
|
54 CWlanBgScanAwsComms::CWlanBgScanAwsComms( CWlanBgScan& aBgScan ) : |
|
55 CActive( CActive::EPriorityStandard ), |
|
56 iBgScan( aBgScan ), |
|
57 iAws( NULL ), |
|
58 iAwsImplInfo( NULL ), |
|
59 iCommandHandler( NULL ), |
|
60 iAwsVersion( 0 ), |
|
61 iPendingCommand( EAwsCommandMax ), |
|
62 iAwsOk( EFalse ) |
|
63 { |
|
64 DEBUG( "CWlanBgScanAwsComms::CWlanBgScanAwsComms()" ); |
|
65 } |
|
66 |
|
67 // --------------------------------------------------------------------------- |
|
68 // CWlanBgScanAwsComms::ConstructL |
|
69 // --------------------------------------------------------------------------- |
|
70 // |
|
71 void CWlanBgScanAwsComms::ConstructL() |
|
72 { |
|
73 DEBUG( "CWlanBgScanAwsComms::ConstructL()" ); |
|
74 |
|
75 // create handler for incoming messages from AWS |
|
76 iCommandHandler = CWlanBgScanCommand::NewL( *this ); |
|
77 |
|
78 CActiveScheduler::Add( this ); |
|
79 |
|
80 // leaves if no AWS present in system |
|
81 StartAwsThreadL(); |
|
82 |
|
83 DEBUG( "CWlanBgScanAwsComms::ConstructL() - done" ); |
|
84 } |
|
85 |
|
86 // --------------------------------------------------------------------------- |
|
87 // CWlanBgScanAwsComms::NewL |
|
88 // --------------------------------------------------------------------------- |
|
89 // |
|
90 CWlanBgScanAwsComms* CWlanBgScanAwsComms::NewL( CWlanBgScan& aBgScan ) |
|
91 { |
|
92 DEBUG( "CWlanBgScanAwsComms::NewL()" ); |
|
93 CWlanBgScanAwsComms* self = new ( ELeave ) CWlanBgScanAwsComms( aBgScan ); |
|
94 CleanupStack::PushL( self ); |
|
95 self->ConstructL(); |
|
96 CleanupStack::Pop( self ); |
|
97 return self; |
|
98 } |
|
99 |
|
100 // --------------------------------------------------------------------------- |
|
101 // CWlanBgScanAwsComms::~CWlanBgScanAwsComms() |
|
102 // --------------------------------------------------------------------------- |
|
103 // |
|
104 CWlanBgScanAwsComms::~CWlanBgScanAwsComms() |
|
105 { |
|
106 DEBUG( "CWlanBgScanAwsComms::~CWlanBgScanAwsComms()" ); |
|
107 |
|
108 delete iAws; |
|
109 iAws = NULL; |
|
110 |
|
111 delete iAwsImplInfo; |
|
112 iAwsImplInfo = NULL; |
|
113 |
|
114 delete iCommandHandler; |
|
115 iCommandHandler = NULL; |
|
116 |
|
117 iAwsMsgQueue.Close(); |
|
118 Cancel(); |
|
119 } |
|
120 |
|
121 // --------------------------------------------------------------------------- |
|
122 // CWlanBgScanAwsComms::CleanupEComArray |
|
123 // --------------------------------------------------------------------------- |
|
124 // |
|
125 void CWlanBgScanAwsComms::CleanupEComArray(TAny* aArray) |
|
126 { |
|
127 DEBUG( "CWlanBgScanAwsComms::CleanupEComArray()" ); |
|
128 |
|
129 ASSERT( aArray ); |
|
130 |
|
131 RImplInfoPtrArray *implInfoArray = static_cast<RImplInfoPtrArray*> ( aArray ); |
|
132 implInfoArray->ResetAndDestroy(); |
|
133 implInfoArray->Close(); |
|
134 } |
|
135 |
|
136 // --------------------------------------------------------------------------- |
|
137 // CWlanBgScanAwsComms::StartAwsThreadL |
|
138 // --------------------------------------------------------------------------- |
|
139 // |
|
140 void CWlanBgScanAwsComms::StartAwsThreadL() |
|
141 { |
|
142 DEBUG( "CWlanBgScanAwsComms::StartAwsThreadL()" ); |
|
143 |
|
144 RImplInfoPtrArray awsImplArray; |
|
145 TCleanupItem awsImplArrayCleanup( CleanupEComArray, &awsImplArray ); |
|
146 CleanupStack::PushL( awsImplArrayCleanup ); |
|
147 |
|
148 CAwsEngineBase::ListImplementationsL( awsImplArray ); |
|
149 |
|
150 if( awsImplArray.Count() == 0 ) |
|
151 { |
|
152 DEBUG( "CWlanBgScanAwsComms::StartAwsThreadL() - no AWS implementation found" ); |
|
153 User::Leave( KErrNotFound ); |
|
154 } |
|
155 |
|
156 // first found AWS implementation will be taken into use |
|
157 iAwsImplInfo = static_cast<CImplementationInformation*>( awsImplArray[KFirstItemIndex] ); |
|
158 awsImplArray.Remove( KFirstItemIndex ); |
|
159 |
|
160 CleanupStack::PopAndDestroy( &awsImplArray ); //this causes a call to CleanupEComArray |
|
161 |
|
162 DEBUG( "CWlanBgScanAwsComms::StartAwsThreadL() - creating AWS thread" ); |
|
163 RThread thread; |
|
164 TInt err = thread.Create( iAwsImplInfo->DisplayName(), |
|
165 AwsThreadEntryPoint, |
|
166 KDefaultStackSize, |
|
167 KMinHeapSize, |
|
168 KMaxHeapSize, |
|
169 reinterpret_cast<TAny*>( this ) ); |
|
170 if( err != KErrNone) |
|
171 { |
|
172 DEBUG1( "CWlanBgScanAwsComms::StartAwsThreadL() - error: thread creation failed with error %i", err ); |
|
173 delete iAwsImplInfo; |
|
174 iAwsImplInfo = NULL; |
|
175 User::Leave( err ); |
|
176 } |
|
177 |
|
178 DEBUG( "CWlanBgScanAwsComms::StartAwsThreadL() - Resuming AWS thread" ); |
|
179 thread.Resume(); |
|
180 thread.Close(); |
|
181 |
|
182 DEBUG( "CWlanBgScanAwsComms::StartAwsThreadL() - done" ); |
|
183 } |
|
184 |
|
185 // ----------------------------------------------------------------------------- |
|
186 // CWlanBgScanAwsComms::AwsThreadEntryPoint |
|
187 // |
|
188 // This method is executing in the context of the AWS thread. |
|
189 // ----------------------------------------------------------------------------- |
|
190 // |
|
191 TInt CWlanBgScanAwsComms::AwsThreadEntryPoint( TAny* aThisPtr ) |
|
192 { |
|
193 DEBUG("CWlanBgScanAwsComms::AwsThreadEntryPoint()"); |
|
194 |
|
195 CWlanBgScanAwsComms* self = static_cast<CWlanBgScanAwsComms*>( aThisPtr ); |
|
196 |
|
197 // create cleanup stack |
|
198 CTrapCleanup* cleanup = CTrapCleanup::New(); |
|
199 if ( cleanup == NULL ) |
|
200 { |
|
201 DEBUG("CWlanBgScanAwsComms::AwsThreadEntryPoint() - error: Cleanup stack creation failed"); |
|
202 User::Exit( KErrNoMemory ); |
|
203 } |
|
204 |
|
205 __UHEAP_MARK; |
|
206 |
|
207 TRAPD( err, self->InstantiateAwsPluginL() ); |
|
208 if ( err != KErrNone ) |
|
209 { |
|
210 DEBUG1("CWlanBgScanAwsComms::AwsThreadEntryPoint() - AWS instantiation leaved with code %i", err); |
|
211 } |
|
212 |
|
213 __UHEAP_MARKEND; |
|
214 |
|
215 delete cleanup; |
|
216 cleanup = NULL; |
|
217 return KErrNone; |
|
218 } |
|
219 |
|
220 // ----------------------------------------------------------------------------- |
|
221 // CWlanBgScanAwsComms::InstantiateAwsPluginL |
|
222 // |
|
223 // This method is executing in the context of the AWS thread. |
|
224 // ----------------------------------------------------------------------------- |
|
225 // |
|
226 void CWlanBgScanAwsComms::InstantiateAwsPluginL() |
|
227 { |
|
228 DEBUG("CWlanBgScanAwsComms::InstantiateAwsPluginL()"); |
|
229 |
|
230 ASSERT( iAwsImplInfo ); |
|
231 |
|
232 CActiveScheduler* scheduler = new (ELeave) CActiveScheduler(); |
|
233 CleanupStack::PushL( scheduler ); |
|
234 CActiveScheduler::Install( scheduler ); |
|
235 |
|
236 DEBUG( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - trying to instantiate AWS implementation:" ); |
|
237 DEBUG1( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - ImplementationUid: 0x%08X", iAwsImplInfo->ImplementationUid().iUid ); |
|
238 #ifdef _DEBUG |
|
239 TBuf8<KPrintLineLength> buf8; |
|
240 buf8.Copy( iAwsImplInfo->DisplayName() ); |
|
241 #endif |
|
242 DEBUG1S("CWlanBgScanAwsComms::InstantiateAwsPluginL() - DisplayName: ", buf8.Length(), buf8.Ptr() ); |
|
243 DEBUG1( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - Version: %i", iAwsImplInfo->Version() ); |
|
244 DEBUG1S("CWlanBgScanAwsComms::InstantiateAwsPluginL() - DataType: ", iAwsImplInfo->DataType().Length(), iAwsImplInfo->DataType().Ptr() ); |
|
245 DEBUG1S("CWlanBgScanAwsComms::InstantiateAwsPluginL() - OpaqueData: ", iAwsImplInfo->OpaqueData().Length(), iAwsImplInfo->OpaqueData().Ptr() ); |
|
246 DEBUG1( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - RomOnly: %i", iAwsImplInfo->RomOnly() ); |
|
247 DEBUG1( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - RomBased: %i", iAwsImplInfo->RomBased() ); |
|
248 DEBUG1( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - VendorId: 0x%08X", iAwsImplInfo->VendorId().iId ); |
|
249 |
|
250 CAwsEngineBase::TAwsEngineConstructionParameters params = { this, KBgScanVersion, iAwsVersion }; |
|
251 iAws = CAwsEngineBase::NewL( iAwsImplInfo->ImplementationUid().iUid, ¶ms ); |
|
252 |
|
253 DEBUG1( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - AWS instantiated OK, iAwsVersion %u", iAwsVersion ); |
|
254 iAwsOk = ETrue; |
|
255 |
|
256 __e32_memory_barrier(); |
|
257 DEBUG( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - data members synchronized" ); |
|
258 |
|
259 DEBUG( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - starting active scheduler - AWS is now in control of this thread" ); |
|
260 CActiveScheduler::Start(); |
|
261 |
|
262 // Thread execution will stay in CActiveScheduler::Start() until active scheduler is stopped |
|
263 DEBUG("CWlanBgScanAwsComms::InstantiateAwsPluginL() - active scheduler stopped" ); |
|
264 |
|
265 // clean up |
|
266 delete iAws; |
|
267 iAws = NULL; |
|
268 CleanupStack::PopAndDestroy( scheduler ); |
|
269 |
|
270 DEBUG( "CWlanBgScanAwsComms::InstantiateAwsPluginL() - exiting..." ); |
|
271 User::Exit( KErrNone ); |
|
272 } |
|
273 |
|
274 // --------------------------------------------------------------------------- |
|
275 // From class MAwsBgScanProvider. |
|
276 // CWlanBgScanAwsComms::SetInterval |
|
277 // |
|
278 // This method is executing in the context of the AWS thread. |
|
279 // --------------------------------------------------------------------------- |
|
280 // |
|
281 void CWlanBgScanAwsComms::SetInterval( TUint32 aNewInterval, TRequestStatus& aStatus ) |
|
282 { |
|
283 DEBUG1( "CWlanBgScanAwsComms::SetInterval() - new interval %u", aNewInterval ); |
|
284 |
|
285 iCommandHandler->CommandQueue( CWlanBgScanCommand::ESetInterval, aNewInterval, aStatus ); |
|
286 |
|
287 DEBUG( "CWlanBgScanAwsComms::SetInterval() - returning" ); |
|
288 } |
|
289 |
|
290 // --------------------------------------------------------------------------- |
|
291 // CWlanBgScanAwsComms::SendOrQueueAwsCommand |
|
292 // --------------------------------------------------------------------------- |
|
293 // |
|
294 void CWlanBgScanAwsComms::SendOrQueueAwsCommand( TAwsMessage& aMessage ) |
|
295 { |
|
296 DEBUG1( "CWlanBgScanAwsComms::SendOrQueueAwsCommand( command %u )", aMessage.iCmd ); |
|
297 |
|
298 // if a request is pending, queue the new command |
|
299 if( iStatus.Int() == KRequestPending ) |
|
300 { |
|
301 DEBUG( "CWlanBgScanAwsComms::SendOrQueueAwsCommand() - request pending -> queue new command" ); |
|
302 |
|
303 // store command to queue |
|
304 TInt err = iAwsMsgQueue.Append( aMessage ); |
|
305 if( KErrNone == err ) |
|
306 { |
|
307 DEBUG( "CWlanBgScanAwsComms::SendOrQueueAwsCommand() - command queued successfully" ); |
|
308 } |
|
309 else |
|
310 { |
|
311 DEBUG( "CWlanBgScanAwsComms::SendOrQueueAwsCommand() - command queueing failed" ); |
|
312 } |
|
313 return; |
|
314 } |
|
315 |
|
316 SendAwsCommand( aMessage ); |
|
317 |
|
318 DEBUG( "CWlanBgScanAwsComms::SendOrQueueAwsCommand() - done" ); |
|
319 } |
|
320 |
|
321 // --------------------------------------------------------------------------- |
|
322 // CWlanBgScanAwsComms::SendAwsCommand |
|
323 // --------------------------------------------------------------------------- |
|
324 // |
|
325 void CWlanBgScanAwsComms::SendAwsCommand( TAwsMessage& aMessage ) |
|
326 { |
|
327 DEBUG1( "CWlanBgScanAwsComms::SendAwsCommand( command %u )", aMessage.iCmd ); |
|
328 |
|
329 if( !iAws ) |
|
330 { |
|
331 DEBUG( "CWlanBgScanAwsComms::SendAwsCommand() - error: no AWS present!" ); |
|
332 ASSERT( 0 ); |
|
333 return; |
|
334 } |
|
335 |
|
336 DEBUG( "CWlanBgScanAwsComms::SendAwsCommand() - sending command" ); |
|
337 |
|
338 switch( aMessage.iCmd ) |
|
339 { |
|
340 case EStart: |
|
341 { |
|
342 iPendingCommand = EStart; |
|
343 iAws->Start( iStatus ); |
|
344 SetActive(); |
|
345 DEBUG( "CWlanBgScanAwsComms::SendAwsCommand() - EStart command sent" ); |
|
346 break; |
|
347 } |
|
348 case EStop: |
|
349 { |
|
350 iPendingCommand = EStop; |
|
351 iAws->Stop( iStatus ); |
|
352 SetActive(); |
|
353 DEBUG( "CWlanBgScanAwsComms::SendAwsCommand() - EStop command sent" ); |
|
354 break; |
|
355 } |
|
356 case ESetPowerSaveMode: |
|
357 { |
|
358 iPendingCommand = ESetPowerSaveMode; |
|
359 iAws->SetPowerSaveMode( static_cast<TAwsPsMode>( aMessage.iParameter ), iStatus ); |
|
360 SetActive(); |
|
361 DEBUG( "CWlanBgScanAwsComms::SendAwsCommand() - ESetPowerSaveMode command sent" ); |
|
362 break; |
|
363 } |
|
364 default: |
|
365 { |
|
366 iPendingCommand = EAwsCommandMax; |
|
367 DEBUG1( "CWlanBgScanAwsComms::SendAwsCommand() - unknown command %u", aMessage.iCmd ); |
|
368 ASSERT( 0 ); |
|
369 } |
|
370 } |
|
371 DEBUG( "CWlanBgScanAwsComms::SendAwsCommand() - done" ); |
|
372 } |
|
373 |
|
374 // --------------------------------------------------------------------------- |
|
375 // CWlanBgScanAwsComms::RunL |
|
376 // --------------------------------------------------------------------------- |
|
377 // |
|
378 void CWlanBgScanAwsComms::RunL() |
|
379 { |
|
380 DEBUG2( "CWlanBgScanAwsComms::RunL() - command: %u, completion status: %d", iPendingCommand, iStatus.Int() ); |
|
381 |
|
382 TAwsMessage cmd = { EAwsCommandMax, NULL }; |
|
383 |
|
384 // if there are more commands, send the next one |
|
385 if( iAwsMsgQueue.Count() ) |
|
386 { |
|
387 cmd = static_cast<TAwsMessage> ( iAwsMsgQueue[KFirstItemIndex] ); |
|
388 iAwsMsgQueue.Remove( KFirstItemIndex ); |
|
389 |
|
390 SendAwsCommand( cmd ); |
|
391 } |
|
392 } |
|
393 |
|
394 // --------------------------------------------------------------------------- |
|
395 // CWlanBgScanAwsComms::DoCancel |
|
396 // --------------------------------------------------------------------------- |
|
397 // |
|
398 void CWlanBgScanAwsComms::DoCancel() |
|
399 { |
|
400 DEBUG( "CWlanBgScanAwsComms::DoCancel()" ); |
|
401 } |
|
402 |
|
403 // --------------------------------------------------------------------------- |
|
404 // CWlanBgScanAwsComms::RunError |
|
405 // --------------------------------------------------------------------------- |
|
406 // |
|
407 TInt CWlanBgScanAwsComms::RunError( TInt aError ) |
|
408 { |
|
409 DEBUG1( "CWlanBgScanAwsComms::RunError( %d )", aError ); |
|
410 return aError; |
|
411 } |
|
412 |
|
413 // --------------------------------------------------------------------------- |
|
414 // CWlanBgScanAwsComms::DoSetInterval |
|
415 // --------------------------------------------------------------------------- |
|
416 // |
|
417 void CWlanBgScanAwsComms::DoSetInterval( TUint32 aNewInterval ) |
|
418 { |
|
419 DEBUG1( "CWlanBgScanAwsComms::DoSetInterval( aNewInterval: %u )", aNewInterval ); |
|
420 |
|
421 iBgScan.DoSetInterval( aNewInterval ); |
|
422 } |
|
423 |
|
424 // --------------------------------------------------------------------------- |
|
425 // CWlanBgScanAwsComms::IsAwsPresent |
|
426 // --------------------------------------------------------------------------- |
|
427 // |
|
428 TBool CWlanBgScanAwsComms::IsAwsPresent() |
|
429 { |
|
430 DEBUG1( "CWlanBgScanAwsComms::IsAwsPresent() - returning %d", iAwsOk ); |
|
431 |
|
432 return iAwsOk; |
|
433 } |