omadrm/drmengine/drmclock/Src/DRMNitzObserver.cpp
changeset 23 493788a4a8a4
parent 0 95b198f216e5
equal deleted inserted replaced
5:79d62d1d7957 23:493788a4a8a4
    18 
    18 
    19 
    19 
    20 // INCLUDE FILES
    20 // INCLUDE FILES
    21 #include "DRMNitzObserver.h"
    21 #include "DRMNitzObserver.h"
    22 #include "DRMClock.h"
    22 #include "DRMClock.h"
    23 #include "DRMLog.h"
    23 #include "drmlog.h"
    24 
    24 
    25 // EXTERNAL DATA STRUCTURES
    25 // EXTERNAL DATA STRUCTURES
    26 
    26 
    27 // EXTERNAL FUNCTION PROTOTYPES  
    27 // EXTERNAL FUNCTION PROTOTYPES
    28 
    28 
    29 // CONSTANTS
    29 // CONSTANTS
    30 const TInt KTimeZoneValidBit(0x3F);     // Time zone difference
    30 const TInt KTimeZoneValidBit(0x3F);     // Time zone difference
    31 const TInt KTimeZoneSignBit(0x80);      // Sign of time zone difference
    31 const TInt KTimeZoneSignBit(0x80);      // Sign of time zone difference
    32 const TInt KNitzYearOffset(2000);       // Adjusting the year value received
    32 const TInt KNitzYearOffset(2000);       // Adjusting the year value received
    48 
    48 
    49 // ---------------------------------------------------------
    49 // ---------------------------------------------------------
    50 // CDRMNitzObserver::CDRMNitzObserver
    50 // CDRMNitzObserver::CDRMNitzObserver
    51 // Default Constructor
    51 // Default Constructor
    52 // ---------------------------------------------------------
    52 // ---------------------------------------------------------
    53 //  
    53 //
    54 CDRMNitzObserver::CDRMNitzObserver( RMobilePhone& aMobilePhone, CDRMClock* aDRMClock) :
    54 CDRMNitzObserver::CDRMNitzObserver( RMobilePhone& aMobilePhone, CDRMClock* aDRMClock) :
    55     CActive(EPriorityNormal) ,iDRMClock( aDRMClock ), iMobilePhone( &aMobilePhone ),
    55     CActive(EPriorityNormal) ,iDRMClock( aDRMClock ), iMobilePhone( &aMobilePhone ),
    56     iError( KErrNone )
    56     iError( KErrNone )
    57     {    
    57     {
    58     }
    58     }
    59 
    59 
    60 // ---------------------------------------------------------
    60 // ---------------------------------------------------------
    61 // CDRMNitzObserver::~CDRMNitzObserver
    61 // CDRMNitzObserver::~CDRMNitzObserver
    62 // Destructor
    62 // Destructor
    63 // ---------------------------------------------------------
    63 // ---------------------------------------------------------
    64 //        
    64 //
    65 CDRMNitzObserver::~CDRMNitzObserver()
    65 CDRMNitzObserver::~CDRMNitzObserver()
    66     {
    66     {
    67     Cancel();	
    67     Cancel();
    68     };      
    68     };
    69       
    69 
    70       
    70 
    71 // ---------------------------------------------------------
    71 // ---------------------------------------------------------
    72 // CDRMNitzObserver::ConstructL
    72 // CDRMNitzObserver::ConstructL
    73 // Two-phase Constructor
    73 // Two-phase Constructor
    74 // ---------------------------------------------------------
    74 // ---------------------------------------------------------
    75 //   
    75 //
    76 void CDRMNitzObserver::ConstructL()
    76 void CDRMNitzObserver::ConstructL()
    77     {
    77     {
    78     DRMLOG( _L("ConstructL "));
    78     DRMLOG( _L("ConstructL "));
    79     
    79 
    80     // Get the NITZ info on the creation, might not be available yet
    80     // Get the NITZ info on the creation, might not be available yet
    81     iError = iMobilePhone->GetNITZInfo(iNitzInfo);
    81     iError = iMobilePhone->GetNITZInfo(iNitzInfo);
    82     
    82 
    83     
    83 
    84     DRMLOG2( _L("ConstructL: Error = %d"), iError );
    84     DRMLOG2( _L("ConstructL: Error = %d"), iError );
    85     
    85 
    86     }
    86     }
    87  
    87 
    88 
    88 
    89 // ---------------------------------------------------------
    89 // ---------------------------------------------------------
    90 // CDRMNitzObserver::NewL
    90 // CDRMNitzObserver::NewL
    91 // Two-phase constructor
    91 // Two-phase constructor
    92 // ---------------------------------------------------------
    92 // ---------------------------------------------------------
    93 //  
    93 //
    94 CDRMNitzObserver* CDRMNitzObserver::NewL( RMobilePhone& aMobilePhone,
    94 CDRMNitzObserver* CDRMNitzObserver::NewL( RMobilePhone& aMobilePhone,
    95                                          CDRMClock* aDRMClock)
    95                                          CDRMClock* aDRMClock)
    96     {
    96     {
    97     CDRMNitzObserver* self = new(ELeave) CDRMNitzObserver(aMobilePhone,aDRMClock);
    97     CDRMNitzObserver* self = new(ELeave) CDRMNitzObserver(aMobilePhone,aDRMClock);
    98     CleanupStack::PushL(self);
    98     CleanupStack::PushL(self);
    99     self->ConstructL();
    99     self->ConstructL();
   100     CleanupStack::Pop();
   100     CleanupStack::Pop();
   101     return self;
   101     return self;
   102     }
   102     }
   103  
   103 
   104 // ---------------------------------------------------------
   104 // ---------------------------------------------------------
   105 // CDRMNitzObserver::Start
   105 // CDRMNitzObserver::Start
   106 // Adds the active object into the ActiveScheduler
   106 // Adds the active object into the ActiveScheduler
   107 // and sets the object active, makes sure RunL is called
   107 // and sets the object active, makes sure RunL is called
   108 // atleast once
   108 // atleast once
   109 // ---------------------------------------------------------
   109 // ---------------------------------------------------------
   110 //        
   110 //
   111 void CDRMNitzObserver::Start() 
   111 void CDRMNitzObserver::Start()
   112     {
   112     {
   113     TRequestStatus* status = 0;
   113     TRequestStatus* status = 0;
   114     
   114 
   115     DRMLOG2( _L("iError == %d "), iError );  
   115     DRMLOG2( _L("iError == %d "), iError );
   116  
   116 
   117     if( iError == KErrNotSupported ) 
   117     if( iError == KErrNotSupported )
   118         {
   118         {
   119         // NITZ never supported, kill the observer
   119         // NITZ never supported, kill the observer
   120         if( IsAdded() )
   120         if( IsAdded() )
   121             {
   121             {
   122             Deque();
   122             Deque();
   123             return;
   123             return;
   124             }
   124             }
   125         return;
   125         return;
   126         }     
   126         }
   127     
   127 
   128     if( !IsAdded() ) 
   128     if( !IsAdded() )
   129         {
   129         {
   130         CActiveScheduler::Add(this);        
   130         CActiveScheduler::Add(this);
   131         }
   131         }
   132     if ( !IsActive() ) 
   132     if ( !IsActive() )
   133         {
   133         {
   134         SetActive();        
   134         SetActive();
   135         }
   135         }
   136     status = &iStatus;
   136     status = &iStatus;
   137     User::RequestComplete(status,KErrNone);
   137     User::RequestComplete(status,KErrNone);
   138     }
   138     }
   139     
   139 
   140     
   140 
   141 // Ignore the error:    
   141 // Ignore the error:
   142 TInt CDRMNitzObserver::RunError( TInt /*aError*/ )
   142 TInt CDRMNitzObserver::RunError( TInt /*aError*/ )
   143     {
   143     {
   144     SetActive();
   144     SetActive();
   145     return KErrNone;
   145     return KErrNone;
   146     }
   146     }
   147     
   147 
   148     
   148 
   149 // ---------------------------------------------------------
   149 // ---------------------------------------------------------
   150 // CDRMNitzObserver::RunL
   150 // CDRMNitzObserver::RunL
   151 // Active object RunL
   151 // Active object RunL
   152 // ---------------------------------------------------------
   152 // ---------------------------------------------------------
   153 //  
   153 //
   154 void CDRMNitzObserver::RunL()
   154 void CDRMNitzObserver::RunL()
   155     {
   155     {
   156     TTime timeData;
   156     TTime timeData;
   157     TInt timeZone( 0 );
   157     TInt timeZone( 0 );
   158     
   158 
   159     // Check if the nitz has changed and is available, if so call update
   159     // Check if the nitz has changed and is available, if so call update
   160     // otherwise just wait
   160     // otherwise just wait
   161     
   161 
   162     DRMLOG( _L("ObserverRunL Called\n\r ") ); 
   162     DRMLOG( _L("ObserverRunL Called\n\r ") );
   163 
   163 
   164     iStatus = KRequestPending;
   164     iStatus = KRequestPending;
   165     
   165 
   166     iMobilePhone->NotifyNITZInfoChange(iStatus, iNitzInfo);
   166     iMobilePhone->NotifyNITZInfoChange(iStatus, iNitzInfo);
   167     
   167 
   168     DRMLOG( _L("NotifyNITZInfoChange:"));             
   168     DRMLOG( _L("NotifyNITZInfoChange:"));
   169     if( GetNitzTime( timeData, timeZone ) )
   169     if( GetNitzTime( timeData, timeZone ) )
   170         {
   170         {
   171         DRMLOG( _L("Resetting secure time from NITZ observer:"));          
   171         DRMLOG( _L("Resetting secure time from NITZ observer:"));
   172         TRAPD(error, iDRMClock->ResetSecureTimeL( timeData, timeZone ) );
   172         TRAPD(error, iDRMClock->ResetSecureTimeL( timeData, timeZone ) );
   173         if( error )
   173         if( error )
   174             {
   174             {
   175             DRMLOG( _L("Resetting secure time from NITZ observer returned an error."));   
   175             DRMLOG( _L("Resetting secure time from NITZ observer returned an error."));
   176             }
   176             }
   177         DRMLOG( _L("Resetting secure time from NITZ observer successful."));     
   177         DRMLOG( _L("Resetting secure time from NITZ observer successful."));
   178           
   178 
   179         if( error != KErrArgument )
   179         if( error != KErrArgument )
   180             {
   180             {
   181             if( error )
   181             if( error )
   182                 {
   182                 {
   183                 DRMLOG( _L("ObserverRunL Called error \n\r ") );                
   183                 DRMLOG( _L("ObserverRunL Called error \n\r ") );
   184                 }
   184                 }
   185             User::LeaveIfError( error );
   185             User::LeaveIfError( error );
   186             }
   186             }
   187         }
   187         }
   188 
   188 
   192 
   192 
   193 // ---------------------------------------------------------
   193 // ---------------------------------------------------------
   194 // CDRMNitzObserver::DoCancel
   194 // CDRMNitzObserver::DoCancel
   195 // Cancels the active object
   195 // Cancels the active object
   196 // ---------------------------------------------------------
   196 // ---------------------------------------------------------
   197 //        
   197 //
   198 void CDRMNitzObserver::DoCancel()
   198 void CDRMNitzObserver::DoCancel()
   199     {
   199     {
   200     // cancel the notify change
   200     // cancel the notify change
   201     iMobilePhone->CancelAsyncRequest(EMobilePhoneNotifyNITZInfoChange);
   201     iMobilePhone->CancelAsyncRequest(EMobilePhoneNotifyNITZInfoChange);
   202     };
   202     };
   225 // ---------------------------------------------------------
   225 // ---------------------------------------------------------
   226 //
   226 //
   227 TBool CDRMNitzObserver::GetNitzTime(TTime& aNitzTime, TInt& aTimeZone)
   227 TBool CDRMNitzObserver::GetNitzTime(TTime& aNitzTime, TInt& aTimeZone)
   228     {
   228     {
   229     TInt32 nitzCaps(iNitzInfo.iNitzFieldsUsed);
   229     TInt32 nitzCaps(iNitzInfo.iNitzFieldsUsed);
   230     
   230 
   231     if (nitzCaps & RMobilePhone::KCapsTimezoneAvailable)    
   231     if (nitzCaps & RMobilePhone::KCapsTimezoneAvailable)
   232         {
   232         {
   233         TInt timezone(iNitzInfo.iTimeZone & KTimeZoneValidBit);
   233         TInt timezone(iNitzInfo.iTimeZone & KTimeZoneValidBit);
   234 
   234 
   235         if (iNitzInfo.iTimeZone & KTimeZoneSignBit)
   235         if (iNitzInfo.iTimeZone & KTimeZoneSignBit)
   236             {
   236             {
   237             // Changes sign
   237             // Changes sign
   238             timezone = - timezone;
   238             timezone = - timezone;
   239             }
   239             }
   240         
   240 
   241         // TimeZone info
   241         // TimeZone info
   242         aTimeZone = timezone;
   242         aTimeZone = timezone;
   243         }
   243         }
   244     
   244 
   245     if (nitzCaps & RMobilePhone::KCapsTimeAvailable)  
   245     if (nitzCaps & RMobilePhone::KCapsTimeAvailable)
   246         {
   246         {
   247         TDateTime dateTime;
   247         TDateTime dateTime;
   248         TInt fourDigitYear(iNitzInfo.Year());
   248         TInt fourDigitYear(iNitzInfo.Year());
   249 
   249 
   250         //Check if our TSY returns 2 digits in stead of 4 digits for the year value
   250         //Check if our TSY returns 2 digits in stead of 4 digits for the year value
   251         if (fourDigitYear <= KMaximumTwoDigitValue)        // The maximum year value in UI applications is 2060 
   251         if (fourDigitYear <= KMaximumTwoDigitValue)        // The maximum year value in UI applications is 2060
   252 		    {
   252             {
   253            //The year received from TSY is 2 digits we make it 4 digits
   253            //The year received from TSY is 2 digits we make it 4 digits
   254             fourDigitYear = fourDigitYear + KNitzYearOffset;  // 2000   
   254             fourDigitYear = fourDigitYear + KNitzYearOffset;  // 2000
   255 		    }
   255             }
   256 
   256 
   257         dateTime.Set(fourDigitYear,
   257         dateTime.Set(fourDigitYear,
   258 		            TMonth(iNitzInfo.Month()),
   258                     TMonth(iNitzInfo.Month()),
   259 					iNitzInfo.Day(),
   259                     iNitzInfo.Day(),
   260 					iNitzInfo.Hour(),
   260                     iNitzInfo.Hour(),
   261 					iNitzInfo.Minute(),
   261                     iNitzInfo.Minute(),
   262 					iNitzInfo.Second(),
   262                     iNitzInfo.Second(),
   263 					iNitzInfo.MicroSecond());
   263                     iNitzInfo.MicroSecond());
   264 
   264 
   265         DRMLOG2( _L( "CDRMNitzObserver::GetNitzTime: DateTime: %d" ), fourDigitYear );
   265         DRMLOG2( _L( "CDRMNitzObserver::GetNitzTime: DateTime: %d" ), fourDigitYear );
   266         DRMLOG2( _L( ":%d" ), iNitzInfo.Month() );
   266         DRMLOG2( _L( ":%d" ), iNitzInfo.Month() );
   267         DRMLOG2( _L( ":%d" ), iNitzInfo.Day() );
   267         DRMLOG2( _L( ":%d" ), iNitzInfo.Day() );
   268         DRMLOG2( _L( ":%d" ), iNitzInfo.Hour() );
   268         DRMLOG2( _L( ":%d" ), iNitzInfo.Hour() );
   271 
   271 
   272         if (KErrNone != CheckDateTimeVal(dateTime))
   272         if (KErrNone != CheckDateTimeVal(dateTime))
   273             {
   273             {
   274             return EFalse;
   274             return EFalse;
   275             }
   275             }
   276         // Transfer the time into a TTime object, UTC    
   276         // Transfer the time into a TTime object, UTC
   277         aNitzTime = dateTime;
   277         aNitzTime = dateTime;
   278         return ETrue;
   278         return ETrue;
   279         }
   279         }
   280     
   280 
   281     return EFalse;
   281     return EFalse;
   282            
   282 
   283     };
   283     };