omadrm/drmengine/drmclock/Src/GPSWatcher.cpp
branchRCL_3
changeset 18 8a03a285ab14
child 20 a20e54f39dd4
equal deleted inserted replaced
17:e16d72588c28 18:8a03a285ab14
       
     1 /*
       
     2 * Copyright (c) 2010 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:  Implementation of the GPS Watcher
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 #include <e32base.h>
       
    21 #include <e32svr.h>
       
    22 
       
    23 #include "GPSWatcher.h"
       
    24 #include "GPSTimeUpdater.h"
       
    25 #include "DRMClock.h"
       
    26 #include "drmlog.h"
       
    27 
       
    28 
       
    29 // ============================ MEMBER FUNCTIONS ===============================
       
    30 
       
    31 
       
    32 // -----------------------------------------------------------------------------
       
    33 // CGPSWatcher::CGPSWatcher
       
    34 // C++ default constructor can NOT contain any code, that
       
    35 // might leave.
       
    36 // -----------------------------------------------------------------------------
       
    37 //   
       
    38 CGPSWatcher::CGPSWatcher( CDRMClock* aClock ) :
       
    39 	CActive(EPriorityHigh),
       
    40 	iClock( aClock )
       
    41 	{
       
    42 	CActiveScheduler::Add(this);
       
    43 	}
       
    44 
       
    45 // -----------------------------------------------------------------------------
       
    46 // CGPSWatcher::~CGPSWatcher
       
    47 // C++ destructor
       
    48 // -----------------------------------------------------------------------------
       
    49 // 
       
    50 CGPSWatcher::~CGPSWatcher()
       
    51 	{
       
    52 	Cancel();
       
    53 
       
    54 	delete iTimeUpdater; 
       
    55 	iTimeUpdater = 0; 
       
    56 	
       
    57 	iPosServer.Close();
       
    58 	}
       
    59 
       
    60 // -----------------------------------------------------------------------------
       
    61 // CGPSWatcher::NewL
       
    62 // Two-phased constructor
       
    63 // -----------------------------------------------------------------------------
       
    64 //
       
    65 CGPSWatcher* CGPSWatcher::NewL( CDRMClock* aClock )
       
    66 	{
       
    67 	CGPSWatcher* self = new (ELeave) CGPSWatcher( aClock );
       
    68 	
       
    69 	CleanupStack::PushL(self);
       
    70 	self->ConstructL();
       
    71 	CleanupStack::Pop(self);
       
    72 	
       
    73 	return self;
       
    74 	}
       
    75 
       
    76 // -----------------------------------------------------------------------------
       
    77 // CGPSWatcher::ConstructL
       
    78 // Symbian 2nd phase constructor can leave.
       
    79 // -----------------------------------------------------------------------------
       
    80 //
       
    81 void CGPSWatcher::ConstructL()
       
    82 	{
       
    83 	User::LeaveIfError(iPosServer.Connect());
       
    84 	
       
    85 	// Immediately subscribe for module status events
       
    86 	iStatusEvent.SetRequestedEvents(TPositionModuleStatusEventBase::EEventDeviceStatus);
       
    87 	Subscribe();
       
    88 
       
    89 	// Check initial state
       
    90 	CheckModules();
       
    91 	}
       
    92 
       
    93 // -----------------------------------------------------------------------------
       
    94 // CGPSWatcher::Subscribe
       
    95 // Subscribe to position events
       
    96 // -----------------------------------------------------------------------------
       
    97 //
       
    98 void CGPSWatcher::Subscribe()
       
    99 	{
       
   100 	iPosServer.NotifyModuleStatusEvent(iStatusEvent, iStatus);
       
   101 	SetActive();
       
   102 	}
       
   103 
       
   104 // -----------------------------------------------------------------------------
       
   105 // CGPSWatcher::RunL
       
   106 // Inherited from CActive
       
   107 // -----------------------------------------------------------------------------
       
   108 //
       
   109 void CGPSWatcher::RunL()
       
   110 	{
       
   111 	// If we already updated the time:
       
   112 	if( iTimeUpdater && iTimeUpdater->TimeReceived() )
       
   113 	    {
       
   114 	    DRMLOG( _L("CGPSWatcher::RunL: time updater exists, time received, deleting"));    
       
   115 	    iClock->Notify( CDRMClock::ENotifyGPSTimeReceived );    
       
   116 	    DRMLOG( _L("CGPSWatcher::RunL: object deleted, returning"));
       
   117 	    return;    
       
   118 	    }
       
   119 	    
       
   120 	Subscribe();
       
   121 	TPositionModuleStatusEventBase::TModuleEvent occurredEvents = iStatusEvent.OccurredEvents();
       
   122 	DRMLOG2(_L("CGPSWatcher::RunL: occurredEvents = 0x%X"), occurredEvents);
       
   123 	
       
   124 	// If time updater is not running, check module statuses
       
   125 	if(!iTimeUpdater)
       
   126 		CheckModules();
       
   127 	}
       
   128 
       
   129 
       
   130 // -----------------------------------------------------------------------------
       
   131 // CGPSWatcher::DoCancel
       
   132 // Inherited from CActive
       
   133 // -----------------------------------------------------------------------------
       
   134 //
       
   135 void CGPSWatcher::DoCancel()
       
   136 	{
       
   137 	iPosServer.CancelRequest(EPositionServerNotifyModuleStatusEvent);
       
   138 	}
       
   139 
       
   140 // -----------------------------------------------------------------------------
       
   141 // CGPSWatcher::RunError
       
   142 // Inherited from CActive
       
   143 // -----------------------------------------------------------------------------
       
   144 //
       
   145 TInt CGPSWatcher::RunError( TInt /*aError*/ )
       
   146     {
       
   147     // ignore errors    
       
   148     return KErrNone;    
       
   149     }
       
   150 	
       
   151 // -----------------------------------------------------------------------------
       
   152 // CGPSWatcher::CheckModules
       
   153 // Check what modules are present and act accordingly
       
   154 // -----------------------------------------------------------------------------
       
   155 //
       
   156 void CGPSWatcher::CheckModules()
       
   157 	{
       
   158 	// Get number of modules
       
   159 	TUint numModules;
       
   160 	if(iPosServer.GetNumModules(numModules)!=KErrNone || !numModules)
       
   161 		return;
       
   162 	
       
   163 	// Collect all active and satellite capable modules
       
   164 	TFullName moduleName;
       
   165 	RPointerArray<TPositionModuleInfo> satelliteModules;
       
   166 	
       
   167 	TPositionModuleInfo *moduleInfo(0);
       
   168 	
       
   169 	for(TUint i=0; i<numModules; i++)
       
   170 		{
       
   171 		if(!moduleInfo)
       
   172 			{
       
   173 			moduleInfo = new TPositionModuleInfo;
       
   174 			if(!moduleInfo)
       
   175 			    {   
       
   176 				continue;
       
   177 			    }
       
   178 			}
       
   179 		
       
   180 		// Get module info from the server
       
   181 		if(iPosServer.GetModuleInfoByIndex(i, *moduleInfo) != KErrNone)
       
   182 		    {
       
   183 			continue;
       
   184             }
       
   185             
       
   186 		// Check if the module is internal and satellite capable		
       
   187 		if(! (moduleInfo->DeviceLocation() & TPositionModuleInfo::EDeviceInternal) ||
       
   188 		   ! (moduleInfo->Capabilities() & TPositionModuleInfo::ECapabilitySatellite) )
       
   189 			{
       
   190 			// Not internal or satellite capable
       
   191 			continue;
       
   192 			}
       
   193 		
       
   194 		// Get module status
       
   195 		TPositionModuleStatus moduleStatus;
       
   196 		if(iPosServer.GetModuleStatus(moduleStatus, moduleInfo->ModuleId()) != KErrNone)
       
   197 		    {
       
   198 			continue;
       
   199 		    }
       
   200 		// Check if the module is active
       
   201 		if(moduleStatus.DeviceStatus() != TPositionModuleStatus::EDeviceActive) 
       
   202 		    {
       
   203 			continue;
       
   204 		    }
       
   205 
       
   206 		moduleInfo->GetModuleName(moduleName);
       
   207 		
       
   208 		DRMLOG6( _L("Module %d: id=0x%08X, name = %S, cap=0x%08X, status=%d"), i, moduleInfo->ModuleId().iUid, &moduleName, moduleInfo->Capabilities(), moduleStatus.DeviceStatus());
       
   209 			
       
   210 		// Active internal satellite device, try to append in the array
       
   211 		if(satelliteModules.Append(moduleInfo) == KErrNone)
       
   212 		    {
       
   213 			moduleInfo = 0;
       
   214 		    }
       
   215 		}
       
   216 	
       
   217 	delete moduleInfo;
       
   218 	
       
   219 	if(satelliteModules.Count())
       
   220 		{
       
   221 		DRMLOG( _L("Active satellite module available") );    
       
   222 		// We have an active satellite module available
       
   223 		TPositionModuleId moduleId = satelliteModules[0]->ModuleId();
       
   224 		
       
   225 		// Just allocating/deleting the time updater will do the trick
       
   226 		iTimeUpdater = CGPSTimeUpdater::New(iPosServer, moduleId, iClock);
       
   227 		}
       
   228 	else
       
   229 		{
       
   230 		DRMLOG( _L("No active satellite modules available") );    
       
   231 		// Delete time updater since no active GPS anymore
       
   232 		delete iTimeUpdater; iTimeUpdater = 0;
       
   233 		}
       
   234 	
       
   235 	satelliteModules.ResetAndDestroy();
       
   236 	satelliteModules.Close();
       
   237 	}