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