breakdeps/DRMEngine/DRMClock.cpp
changeset 88 ca165d35976d
child 90 62156f66dbad
equal deleted inserted replaced
87:e9fb2728ea8b 88:ca165d35976d
       
     1 /*
       
     2 * Copyright (c) 2003-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 "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 DRM Clock
       
    15 *
       
    16 */
       
    17 
       
    18 
       
    19 // INCLUDE FILES
       
    20 
       
    21 #include <mmtsy_names.h>
       
    22 
       
    23 #include "DRMClock.h"
       
    24 #include "drmlog.h"
       
    25 #include "DRMEventTimeChange.h"
       
    26 #include "wmdrmfileserverclient.h"
       
    27 
       
    28 #include <DRMNotifier.h>
       
    29 #include <s32strm.h>
       
    30 #include <s32file.h>
       
    31 #include <e32property.h>
       
    32 #include <e32keys.h>
       
    33 
       
    34 #ifdef RD_MULTIPLE_DRIVE
       
    35 #include <driveinfo.h>
       
    36 #endif
       
    37 
       
    38 #include "DRMNitzObserver.h"
       
    39 #include "GPSWatcher.h"
       
    40 
       
    41 // EXTERNAL DATA STRUCTURES
       
    42 
       
    43 // EXTERNAL FUNCTION PROTOTYPES  
       
    44 
       
    45 // CONSTANTS
       
    46 const TInt KMinuteInMicroseconds = 60000000;
       
    47 const TInt KTimeZoneIncrement = 15;
       
    48  
       
    49 // The time zones sanity check values, not sure if -13 hours exists
       
    50 // But atleast +13 does in: Nuku'Alofa
       
    51 const TInt KTimeZoneLow = -52; // -13 hours
       
    52 const TInt KTimeZoneHigh = 55; // +13 hours 45 minutes Some NZ owned island
       
    53 
       
    54 
       
    55 // MACROS
       
    56 
       
    57 // LOCAL CONSTANTS AND MACROS
       
    58 
       
    59 // MODULE DATA STRUCTURES
       
    60 
       
    61 // LOCAL FUNCTION PROTOTYPES
       
    62 
       
    63 // FORWARD DECLARATIONS
       
    64 
       
    65 // ============================= LOCAL FUNCTIONS ===============================
       
    66 
       
    67 
       
    68 // ============================ MEMBER FUNCTIONS ===============================
       
    69 
       
    70 
       
    71 // -----------------------------------------------------------------------------
       
    72 // CDRMRightsDB::CDRMRightsDB
       
    73 // C++ default constructor can NOT contain any code, that
       
    74 // might leave.
       
    75 // -----------------------------------------------------------------------------
       
    76 //    
       
    77 CDRMClock::CDRMClock()
       
    78     {      
       
    79     };
       
    80 
       
    81 // -----------------------------------------------------------------------------
       
    82 // CDRMRightsDB::ConstructL
       
    83 // Symbian 2nd phase constructor can leave.
       
    84 // -----------------------------------------------------------------------------
       
    85 //
       
    86 void CDRMClock::ConstructL()
       
    87     {
       
    88     DRMLOG( _L( "DRM Clock Starting: " ) );
       
    89     TInt error = KErrNone;
       
    90 
       
    91     // Create a notifier instance
       
    92     iNotifier = CDRMNotifier::NewL();
       
    93         
       
    94 #ifndef __WINS__
       
    95     ConnectToPhoneL();            
       
    96             
       
    97     iObserver = CDRMNitzObserver::NewL( iPhone, const_cast<CDRMClock*>(this));
       
    98 
       
    99     iObserver->Start();
       
   100     
       
   101     TRAP( error, iGpsWatcher = CGPSWatcher::NewL( const_cast<CDRMClock*>(this) ));
       
   102     DRMLOG2( _L("DRMClock: GPS watcher started: %d"), error );    
       
   103 #endif
       
   104 
       
   105     DRMLOG( _L( "DRM Clock started" ) );		    
       
   106     };
       
   107 
       
   108 // -----------------------------------------------------------------------------
       
   109 // CDRMClock::NewLC
       
   110 // Two-phased constructor
       
   111 // -----------------------------------------------------------------------------
       
   112 //
       
   113 CDRMClock* CDRMClock::NewLC()
       
   114     {
       
   115     DRMLOG( _L( "CDRMClock::NewLC" ) );
       
   116     
       
   117     CDRMClock* self = new(ELeave) CDRMClock;
       
   118     CleanupStack::PushL( self );
       
   119     self->ConstructL();
       
   120     
       
   121     DRMLOG( _L( "CDRMClock::NewLC ok" ) );
       
   122     
       
   123     return self;
       
   124     };
       
   125 
       
   126 // -----------------------------------------------------------------------------
       
   127 // CDRMClock::NewL
       
   128 // Two-phased constructor
       
   129 // -----------------------------------------------------------------------------
       
   130 //
       
   131 CDRMClock* CDRMClock::NewL()
       
   132     {
       
   133     DRMLOG( _L( "CDRMClock::NewL" ) );
       
   134     
       
   135     CDRMClock* self = NewLC();
       
   136     CleanupStack::Pop();
       
   137 
       
   138     DRMLOG( _L( "CDRMClock::NewL ok" ) );
       
   139     
       
   140     return self;
       
   141     };
       
   142   
       
   143 // ---------------------------------------------------------
       
   144 // CDRMClock::~CDRMClock
       
   145 // Destructor
       
   146 // ---------------------------------------------------------
       
   147 //
       
   148 CDRMClock::~CDRMClock()
       
   149     { 
       
   150     DRMLOG( _L( "CDRMClock::~CDRMClock" ) );
       
   151        
       
   152     if( iNotifier )
       
   153         {
       
   154         delete iNotifier;
       
   155         iNotifier = 0;
       
   156         }
       
   157         
       
   158 #ifndef __WINS__
       
   159     if(iObserver)            
       
   160         {
       
   161         iObserver->Cancel();
       
   162         delete iObserver;
       
   163         iObserver = 0;
       
   164         }  
       
   165         
       
   166     if( iGpsWatcher )
       
   167         {
       
   168         iGpsWatcher->Cancel();
       
   169         delete iGpsWatcher;
       
   170         iGpsWatcher = 0;
       
   171         }   
       
   172 #endif // __WINS__        
       
   173     };
       
   174 
       
   175 // -----------------------------------------------------------------------------
       
   176 // CDRMClock::GetSecureTime
       
   177 // returns time and security level
       
   178 // -----------------------------------------------------------------------------
       
   179 //
       
   180 void CDRMClock::GetSecureTime(TTime& aTime, TInt& aTimeZone, 
       
   181                               DRMClock::ESecurityLevel& aSecurityLevel)
       
   182     {
       
   183     DRMLOG( _L( "CDRMClock::GetSecureTime" ) );
       
   184     
       
   185     TTime currentUniversal;
       
   186     TTime currentHome;
       
   187     TInt error = KErrNone;
       
   188     
       
   189     // if there is an error it's not initialized
       
   190     error = aTime.UniversalTimeSecure();
       
   191     
       
   192     if( error == KErrNoSecureTime )
       
   193         {
       
   194         currentHome.HomeTime();               
       
   195         currentUniversal.UniversalTime();         
       
   196         
       
   197         aTimeZone = ( currentHome.Int64() - currentUniversal.Int64() ) / 
       
   198                    ( KMinuteInMicroseconds* KTimeZoneIncrement );
       
   199         
       
   200         
       
   201         aTime.UniversalTime();
       
   202 
       
   203         aSecurityLevel = DRMClock::KInsecure; 
       
   204        
       
   205         DRMLOG( _L( "CDRMClock::GetSecureTime: DRMClock is Insecure" ) );        
       
   206         }
       
   207     else 
       
   208         {
       
   209         currentHome.HomeTimeSecure();        
       
   210         currentUniversal.UniversalTimeSecure();
       
   211         
       
   212         aTimeZone = ( currentHome.Int64() - currentUniversal.Int64() ) / 
       
   213                    ( KMinuteInMicroseconds* KTimeZoneIncrement );
       
   214         
       
   215         aSecurityLevel = DRMClock::KSecure;     
       
   216         DRMLOG( _L( "CDRMClock::GetSecureTime: DRMClock is Secure" ) );  
       
   217         }    
       
   218 
       
   219     DRMLOG( _L( "CDRMClock::GetSecureTime ok" ) );
       
   220     };
       
   221 
       
   222 
       
   223 // -----------------------------------------------------------------------------
       
   224 // CDRMClock::ResetSecureTimeL
       
   225 // resets the secure time and recalculates the offsets
       
   226 // should not reset to 0
       
   227 // -----------------------------------------------------------------------------
       
   228 //
       
   229 // Do not reset the timezone, use whatever has been set or retrieved from the UI time
       
   230 // However check that the timezone is a valid one just in case
       
   231 void CDRMClock::ResetSecureTimeL( const TTime& aTime, const TInt& aTimeZone )
       
   232     {
       
   233     DRMLOG( _L( "CDRMClock::ResetSecureTimeL" ) );
       
   234    
       
   235     TRequestStatus status;  
       
   236     TInt error = KErrNone;  
       
   237     CDRMEventTimeChange* change = CDRMEventTimeChange::NewLC();
       
   238 	TTime previousTime;
       
   239 	TTime previousTimeLocal;
       
   240 	TTime newTime;
       
   241 	TInt timezone = 0;
       
   242 	TDateTime temppi; // Only for logging
       
   243 
       
   244     // check for param that the time is even reasonably valid:
       
   245     if( aTime.Int64() == 0 )
       
   246         {
       
   247         DRMLOG( _L("Trying to reset to zero time") );             
       
   248     	User::Leave( KErrArgument );
       
   249         }
       
   250     
       
   251     // Sanity check: Time zone has to be +/- certail hours
       
   252     // for this check -13h to +13.75h
       
   253     if( aTimeZone < KTimeZoneLow || aTimeZone > KTimeZoneHigh )
       
   254         {
       
   255         DRMLOG2( _L("TimeZone invalid, time may be as well, aborting change: %d"), aTimeZone  );
       
   256         User::Leave( KErrArgument );
       
   257         }
       
   258     
       
   259     
       
   260     // Get the current secure time with timezone
       
   261     // Ask the hometime first so that rounding of any divider goes correctly
       
   262     error = previousTimeLocal.HomeTimeSecure(); 
       
   263     
       
   264     // If there is an error, the secure hometime has not been set
       
   265     // Which means that the UI clock has the valid data
       
   266     if( error )
       
   267         {
       
   268         previousTimeLocal.HomeTime();
       
   269         previousTime.UniversalTime();
       
   270         timezone = ( previousTimeLocal.Int64() - previousTime.Int64() ) / 
       
   271                    ( KMinuteInMicroseconds* KTimeZoneIncrement );
       
   272 		change->SetOldSecurityLevel( DRMClock::KInsecure );
       
   273 		}
       
   274 	else
       
   275 		{
       
   276 		previousTime.UniversalTimeSecure();		                   
       
   277         
       
   278         timezone = ( previousTimeLocal.Int64() - previousTime.Int64() ) / 
       
   279                    ( KMinuteInMicroseconds* KTimeZoneIncrement );
       
   280 
       
   281 		change->SetOldSecurityLevel( DRMClock::KSecure );
       
   282 		change->SetNewSecurityLevel( DRMClock::KSecure );
       
   283 		}
       
   284     
       
   285     // Since it's not important to get the timezone we keep what is set or reset it:
       
   286     previousTimeLocal = aTime.Int64() + ( timezone * ( KMinuteInMicroseconds* KTimeZoneIncrement ) );
       
   287 
       
   288     // Do the actual updating:
       
   289     // Update using the wmdrm fileserver since it has TCB capability
       
   290     RWmDrmFileServerClient resetclient;
       
   291     User::LeaveIfError( resetclient.Connect() );
       
   292     CleanupClosePushL( resetclient );
       
   293     
       
   294     newTime = aTime;
       
   295     User::LeaveIfError( resetclient.UpdateSecureTime( previousTimeLocal, newTime ) );
       
   296        
       
   297     CleanupStack::PopAndDestroy();
       
   298 
       
   299 
       
   300     DRMLOG( _L( "CDRMClock::ResetSecureTimeL: AFTER RESET." ));	
       
   301 
       
   302     // DRM Notifier data:
       
   303     // send info about the change:
       
   304 
       
   305     change->SetNewSecurityLevel( DRMClock::KSecure );
       
   306     
       
   307     change->SetOldTime( previousTime );
       
   308     change->SetOldTimeZone( timezone );
       
   309 
       
   310     change->SetNewTime( aTime );
       
   311     change->SetNewTimeZone( timezone );
       
   312     
       
   313     // Notify clients
       
   314 
       
   315     iNotifier->SendEventL(*change,status);
       
   316     User::WaitForRequest(status);
       
   317     CleanupStack::PopAndDestroy();    
       
   318     
       
   319     DRMLOG( _L( "CDRMClock::ResetSecureTimeL ok" ) );
       
   320     };
       
   321 
       
   322 // ---------------------------------------------------------
       
   323 // CDRMClock::Notify
       
   324 // Notify DRM clock about an event
       
   325 // ---------------------------------------------------------
       
   326 //
       
   327 void CDRMClock::Notify( TInt aNotify )
       
   328     {
       
   329     switch( aNotify )
       
   330         {
       
   331         case ENotifyGPSTimeReceived:
       
   332             // GPS time received, listen again after the next boot, destroy GPS watcher:
       
   333             DRMLOG(_L("Notify: ENotifyGPSTimeReceived, Deleting GPS watcher"));
       
   334             delete iGpsWatcher;
       
   335             iGpsWatcher = NULL;
       
   336             DRMLOG(_L("Notify: GPS Watcher deleted"));
       
   337             break;    
       
   338         case ENotifyNone:
       
   339         default:
       
   340             break;  // Do nothing    
       
   341         }    
       
   342     }
       
   343 
       
   344 
       
   345 
       
   346 
       
   347 
       
   348 // ---------------------------------------------------------
       
   349 // CDRMClock::ConnectToPhoneL(const TDateTime& aDateTime)
       
   350 // Gets the nitz time from iNitzInfo
       
   351 // ---------------------------------------------------------
       
   352 //
       
   353 void CDRMClock::ConnectToPhoneL()
       
   354     {
       
   355     DRMLOG( _L( "CDRMClock::ConnectToPhoneL" ) );
       
   356     
       
   357     const TInt KTriesToConnectServer(10);
       
   358     const TInt KTimeBeforeRetryingServerConnection(100000);
       
   359     TInt thisTry(0);
       
   360     TInt err(KErrNone);
       
   361     TInt numPhone;
       
   362     TName tsyName;
       
   363     RTelServer::TPhoneInfo phoneInfo;
       
   364     RMobilePhone::TMobilePhoneSubscriberId imsi;
       
   365     TRequestStatus status;
       
   366     
       
   367 
       
   368     while ((err = iEtelServer.Connect()) != KErrNone &&
       
   369                             (thisTry++) <= KTriesToConnectServer)
       
   370         {
       
   371         User::After(KTimeBeforeRetryingServerConnection);
       
   372         }
       
   373     User::LeaveIfError(err);
       
   374 
       
   375     User::LeaveIfError(iEtelServer.LoadPhoneModule(KMmTsyModuleName));
       
   376     User::LeaveIfError(iEtelServer.EnumeratePhones(numPhone));
       
   377 
       
   378     for (TInt i(0); i < numPhone; i++)
       
   379         {
       
   380         User::LeaveIfError(iEtelServer.GetPhoneInfo(i, phoneInfo));
       
   381         User::LeaveIfError(iEtelServer.GetTsyName(i,tsyName));
       
   382 
       
   383         if (tsyName.CompareF(KMmTsyModuleName) == 0)
       
   384             {
       
   385             break;
       
   386             }
       
   387         }
       
   388 
       
   389     User::LeaveIfError(iPhone.Open(iEtelServer, phoneInfo.iName));   
       
   390     
       
   391     iPhone.GetSubscriberId( status, imsi );
       
   392     User::WaitForRequest( status );
       
   393     
       
   394     DRMLOG( imsi );    
       
   395     DRMLOG( _L( "CDRMClock::ConnectToPhoneL ok" ) );
       
   396     };
       
   397 
       
   398 
       
   399 // ========================== OTHER EXPORTED FUNCTIONS =========================
       
   400 
       
   401 
       
   402 //  End of File