sl@0
|
1 |
/*
|
sl@0
|
2 |
* Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies).
|
sl@0
|
3 |
* All rights reserved.
|
sl@0
|
4 |
* This component and the accompanying materials are made available
|
sl@0
|
5 |
* under the terms of the License "Eclipse Public License v1.0"
|
sl@0
|
6 |
* which accompanies this distribution, and is available
|
sl@0
|
7 |
* at the URL "http://www.eclipse.org/legal/epl-v10.html".
|
sl@0
|
8 |
*
|
sl@0
|
9 |
* Initial Contributors:
|
sl@0
|
10 |
* Nokia Corporation - initial contribution.
|
sl@0
|
11 |
*
|
sl@0
|
12 |
* Contributors:
|
sl@0
|
13 |
*
|
sl@0
|
14 |
* Description:
|
sl@0
|
15 |
*
|
sl@0
|
16 |
*/
|
sl@0
|
17 |
|
sl@0
|
18 |
|
sl@0
|
19 |
#include "sntpclientengine.h"
|
sl@0
|
20 |
#include "util.h"
|
sl@0
|
21 |
|
sl@0
|
22 |
// 40 second timeout on operations
|
sl@0
|
23 |
#define SNTP_ENGINE_TIMEOUT 40000000
|
sl@0
|
24 |
|
sl@0
|
25 |
// NTP port
|
sl@0
|
26 |
|
sl@0
|
27 |
#define SNTP_REMOTE_PORT 123
|
sl@0
|
28 |
|
sl@0
|
29 |
_LIT(KNTPEpochDate,"19000000:");
|
sl@0
|
30 |
|
sl@0
|
31 |
/* The simplest possible NTP request */
|
sl@0
|
32 |
|
sl@0
|
33 |
static const TUint8 sntpRequest[48] = {
|
sl@0
|
34 |
0x23, 0x00, 0x00, 0x00,
|
sl@0
|
35 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
36 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
37 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
38 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
39 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
40 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
41 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
42 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
43 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
44 |
0x00, 0x00, 0x00, 0x00,
|
sl@0
|
45 |
0x00, 0x00, 0x00, 0x00 };
|
sl@0
|
46 |
|
sl@0
|
47 |
/* The main engine of the SNTP client */
|
sl@0
|
48 |
|
sl@0
|
49 |
CSNTPClient* CSNTPClient::NewL(TCommandLineArgs& aArgs)
|
sl@0
|
50 |
{
|
sl@0
|
51 |
CSNTPClient* self = CSNTPClient::NewLC(aArgs);
|
sl@0
|
52 |
CleanupStack::Pop(self);
|
sl@0
|
53 |
return self;
|
sl@0
|
54 |
}
|
sl@0
|
55 |
|
sl@0
|
56 |
CSNTPClient* CSNTPClient::NewLC(TCommandLineArgs& aArgs)
|
sl@0
|
57 |
{
|
sl@0
|
58 |
CSNTPClient* self = new (ELeave) CSNTPClient(aArgs);
|
sl@0
|
59 |
CleanupStack::PushL(self);
|
sl@0
|
60 |
self->ConstructL();
|
sl@0
|
61 |
return self;
|
sl@0
|
62 |
}
|
sl@0
|
63 |
|
sl@0
|
64 |
TSNTPClientState CSNTPClient::State()
|
sl@0
|
65 |
{
|
sl@0
|
66 |
return iState;
|
sl@0
|
67 |
}
|
sl@0
|
68 |
|
sl@0
|
69 |
void CSNTPClient::Start()
|
sl@0
|
70 |
{
|
sl@0
|
71 |
|
sl@0
|
72 |
iState = EStateResolve;
|
sl@0
|
73 |
iResolver.GetByName(*(iArgs.iServers[iServerIndex]), iNameEntry, iStatus);
|
sl@0
|
74 |
SetActive();
|
sl@0
|
75 |
iTimer->After(SNTP_ENGINE_TIMEOUT);
|
sl@0
|
76 |
|
sl@0
|
77 |
}
|
sl@0
|
78 |
|
sl@0
|
79 |
CSNTPClient::~CSNTPClient()
|
sl@0
|
80 |
{
|
sl@0
|
81 |
Cancel();
|
sl@0
|
82 |
iResolver.Close();
|
sl@0
|
83 |
iSock.Close();
|
sl@0
|
84 |
iSockServ.Close();
|
sl@0
|
85 |
|
sl@0
|
86 |
delete iTimer;
|
sl@0
|
87 |
}
|
sl@0
|
88 |
|
sl@0
|
89 |
CSNTPClient::CSNTPClient(TCommandLineArgs& aArgs)
|
sl@0
|
90 |
: CActive(EPriorityStandard), iArgs(aArgs)
|
sl@0
|
91 |
{
|
sl@0
|
92 |
}
|
sl@0
|
93 |
|
sl@0
|
94 |
void CSNTPClient::ConstructL()
|
sl@0
|
95 |
{
|
sl@0
|
96 |
User::LeaveIfError(iSockServ.Connect());
|
sl@0
|
97 |
User::LeaveIfError(iSock.Open(iSockServ, KAfInet, KSockDatagram, KProtocolInetUdp));
|
sl@0
|
98 |
User::LeaveIfError(iResolver.Open(iSockServ, KAfInet, KProtocolInetUdp));
|
sl@0
|
99 |
|
sl@0
|
100 |
iTimer = CTimeOutTimer::NewL(EPriorityHigh, *this);
|
sl@0
|
101 |
CActiveScheduler::Add(this);
|
sl@0
|
102 |
}
|
sl@0
|
103 |
|
sl@0
|
104 |
void CSNTPClient::RunL()
|
sl@0
|
105 |
{
|
sl@0
|
106 |
|
sl@0
|
107 |
if (iStatus.Int() < 0)
|
sl@0
|
108 |
{
|
sl@0
|
109 |
User::Leave(iStatus.Int());
|
sl@0
|
110 |
}
|
sl@0
|
111 |
|
sl@0
|
112 |
switch (iState)
|
sl@0
|
113 |
{
|
sl@0
|
114 |
|
sl@0
|
115 |
case EStateResolve:
|
sl@0
|
116 |
iTimer->Cancel();
|
sl@0
|
117 |
iBuffer.Zero();
|
sl@0
|
118 |
iBuffer.Append(sntpRequest, 48);
|
sl@0
|
119 |
|
sl@0
|
120 |
// set the port on the address
|
sl@0
|
121 |
iNameEntry().iAddr.SetPort(SNTP_REMOTE_PORT);
|
sl@0
|
122 |
|
sl@0
|
123 |
iState = EStateWrite;
|
sl@0
|
124 |
iSock.SendTo(iBuffer, iNameEntry().iAddr, 0, iStatus);
|
sl@0
|
125 |
SetActive();
|
sl@0
|
126 |
iTimer->After(SNTP_ENGINE_TIMEOUT);
|
sl@0
|
127 |
break;
|
sl@0
|
128 |
|
sl@0
|
129 |
case EStateWrite:
|
sl@0
|
130 |
iTimer->Cancel();
|
sl@0
|
131 |
iState = EStateRead;
|
sl@0
|
132 |
iBuffer.Zero();
|
sl@0
|
133 |
iSock.RecvFrom(iBuffer, iNameEntry().iAddr, 0, iStatus);
|
sl@0
|
134 |
SetActive();
|
sl@0
|
135 |
iTimer->After(SNTP_ENGINE_TIMEOUT);
|
sl@0
|
136 |
break;
|
sl@0
|
137 |
|
sl@0
|
138 |
case EStateRead:
|
sl@0
|
139 |
{
|
sl@0
|
140 |
iTimer->Cancel();
|
sl@0
|
141 |
SetTimeL();
|
sl@0
|
142 |
iStatus = KRequestPending;
|
sl@0
|
143 |
iState = EStateComplete;
|
sl@0
|
144 |
TRequestStatus* status = &iStatus;
|
sl@0
|
145 |
SetActive();
|
sl@0
|
146 |
User::RequestComplete(status, KErrNone);
|
sl@0
|
147 |
break;
|
sl@0
|
148 |
}
|
sl@0
|
149 |
|
sl@0
|
150 |
case EStateComplete:
|
sl@0
|
151 |
CActiveScheduler::Stop();
|
sl@0
|
152 |
// done
|
sl@0
|
153 |
break;
|
sl@0
|
154 |
|
sl@0
|
155 |
default:
|
sl@0
|
156 |
// wuh oh. BC break!
|
sl@0
|
157 |
User::Leave(KErrArgument);
|
sl@0
|
158 |
break;
|
sl@0
|
159 |
|
sl@0
|
160 |
}
|
sl@0
|
161 |
|
sl@0
|
162 |
}
|
sl@0
|
163 |
|
sl@0
|
164 |
void CSNTPClient::DoCancel()
|
sl@0
|
165 |
{
|
sl@0
|
166 |
|
sl@0
|
167 |
iTimer->Cancel();
|
sl@0
|
168 |
|
sl@0
|
169 |
switch (iState)
|
sl@0
|
170 |
{
|
sl@0
|
171 |
case EStateResolve:
|
sl@0
|
172 |
iResolver.Cancel();
|
sl@0
|
173 |
break;
|
sl@0
|
174 |
case EStateWrite:
|
sl@0
|
175 |
iSock.CancelSend();
|
sl@0
|
176 |
break;
|
sl@0
|
177 |
case EStateRead:
|
sl@0
|
178 |
iSock.CancelRecv();
|
sl@0
|
179 |
break;
|
sl@0
|
180 |
}
|
sl@0
|
181 |
|
sl@0
|
182 |
}
|
sl@0
|
183 |
|
sl@0
|
184 |
|
sl@0
|
185 |
TInt CSNTPClient::RunError(TInt /* aError */)
|
sl@0
|
186 |
{
|
sl@0
|
187 |
// The current server didn't work, lets try the next if available.
|
sl@0
|
188 |
iTimer->Cancel();
|
sl@0
|
189 |
|
sl@0
|
190 |
if (++iServerIndex < iArgs.iServers.Count())
|
sl@0
|
191 |
{
|
sl@0
|
192 |
Start();
|
sl@0
|
193 |
}
|
sl@0
|
194 |
else
|
sl@0
|
195 |
{
|
sl@0
|
196 |
iState = EStateFailed;
|
sl@0
|
197 |
CActiveScheduler::Stop();
|
sl@0
|
198 |
}
|
sl@0
|
199 |
return KErrNone;
|
sl@0
|
200 |
}
|
sl@0
|
201 |
|
sl@0
|
202 |
void CSNTPClient::TimerExpired()
|
sl@0
|
203 |
{
|
sl@0
|
204 |
Cancel();
|
sl@0
|
205 |
|
sl@0
|
206 |
// The current server didn't work, lets try the next if available.
|
sl@0
|
207 |
|
sl@0
|
208 |
if (++iServerIndex < iArgs.iServers.Count())
|
sl@0
|
209 |
{
|
sl@0
|
210 |
Start();
|
sl@0
|
211 |
}
|
sl@0
|
212 |
else
|
sl@0
|
213 |
{
|
sl@0
|
214 |
iState = EStateAborted;
|
sl@0
|
215 |
CActiveScheduler::Stop();
|
sl@0
|
216 |
}
|
sl@0
|
217 |
}
|
sl@0
|
218 |
|
sl@0
|
219 |
void CSNTPClient::SetTimeL()
|
sl@0
|
220 |
{
|
sl@0
|
221 |
|
sl@0
|
222 |
TUint32 timestamp(0);
|
sl@0
|
223 |
|
sl@0
|
224 |
/* Use the seconds from the transmit time field
|
sl@0
|
225 |
|
sl@0
|
226 |
*/
|
sl@0
|
227 |
|
sl@0
|
228 |
for (TInt i = 40; i < 44; ++i)
|
sl@0
|
229 |
{
|
sl@0
|
230 |
timestamp = (timestamp << 8) + iBuffer[i];
|
sl@0
|
231 |
}
|
sl@0
|
232 |
|
sl@0
|
233 |
// Obtain the time, including the specified timezone offset
|
sl@0
|
234 |
|
sl@0
|
235 |
TTimeIntervalMinutes mins(timestamp / 60);
|
sl@0
|
236 |
TTimeIntervalSeconds secs(timestamp % 60);
|
sl@0
|
237 |
|
sl@0
|
238 |
TTime ntpTime;
|
sl@0
|
239 |
User::LeaveIfError(ntpTime.Set(KNTPEpochDate));
|
sl@0
|
240 |
ntpTime += mins;
|
sl@0
|
241 |
ntpTime += secs;
|
sl@0
|
242 |
|
sl@0
|
243 |
// Apply offset and (possibly) daylight savings time
|
sl@0
|
244 |
|
sl@0
|
245 |
TTimeIntervalHours hours;
|
sl@0
|
246 |
|
sl@0
|
247 |
if (iArgs.iApplyDaylightSavings && Util::DaylightSavingsAppliesL(ntpTime))
|
sl@0
|
248 |
{
|
sl@0
|
249 |
hours = iArgs.iOffset + 1;
|
sl@0
|
250 |
}
|
sl@0
|
251 |
else
|
sl@0
|
252 |
{
|
sl@0
|
253 |
hours = iArgs.iOffset;
|
sl@0
|
254 |
}
|
sl@0
|
255 |
|
sl@0
|
256 |
ntpTime += hours;
|
sl@0
|
257 |
|
sl@0
|
258 |
User::LeaveIfError(User::SetHomeTime(ntpTime));
|
sl@0
|
259 |
|
sl@0
|
260 |
}
|
sl@0
|
261 |
|
sl@0
|
262 |
|
sl@0
|
263 |
/* Timeout handler for read/write operations */
|
sl@0
|
264 |
|
sl@0
|
265 |
CTimeOutTimer::CTimeOutTimer(const TInt aPriority)
|
sl@0
|
266 |
: CTimer(aPriority)
|
sl@0
|
267 |
{
|
sl@0
|
268 |
}
|
sl@0
|
269 |
|
sl@0
|
270 |
CTimeOutTimer::~CTimeOutTimer()
|
sl@0
|
271 |
{
|
sl@0
|
272 |
Cancel();
|
sl@0
|
273 |
}
|
sl@0
|
274 |
|
sl@0
|
275 |
CTimeOutTimer* CTimeOutTimer::NewL(const TInt aPriority, MTimeOutNotify& aTimeOutNotify)
|
sl@0
|
276 |
{
|
sl@0
|
277 |
CTimeOutTimer *p = new (ELeave) CTimeOutTimer(aPriority);
|
sl@0
|
278 |
CleanupStack::PushL(p);
|
sl@0
|
279 |
p->ConstructL(aTimeOutNotify);
|
sl@0
|
280 |
CleanupStack::Pop();
|
sl@0
|
281 |
return p;
|
sl@0
|
282 |
}
|
sl@0
|
283 |
|
sl@0
|
284 |
void CTimeOutTimer::ConstructL(MTimeOutNotify &aTimeOutNotify)
|
sl@0
|
285 |
{
|
sl@0
|
286 |
iNotify=&aTimeOutNotify;
|
sl@0
|
287 |
CTimer::ConstructL();
|
sl@0
|
288 |
CActiveScheduler::Add(this);
|
sl@0
|
289 |
}
|
sl@0
|
290 |
|
sl@0
|
291 |
void CTimeOutTimer::RunL()
|
sl@0
|
292 |
// Timer request has completed, so notify the timer's owner
|
sl@0
|
293 |
{
|
sl@0
|
294 |
iNotify->TimerExpired();
|
sl@0
|
295 |
}
|