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