31
|
1 |
/*
|
|
2 |
* Copyright (c) 2005-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: This is a project specific include file for building the
|
|
15 |
* clock related functions as part of libc library.
|
|
16 |
*
|
|
17 |
*/
|
|
18 |
|
|
19 |
|
|
20 |
#include <time.h>
|
|
21 |
#include <errno.h>
|
|
22 |
#include <pthread.h>
|
|
23 |
#include <stdlib.h>
|
|
24 |
#include <e32std.h>
|
|
25 |
#include <sys/_timeval.h>
|
|
26 |
#include <sys/types.h>
|
|
27 |
#include "lposix.h"
|
|
28 |
|
|
29 |
#define UNIX_BASE TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000)) // 00:00, Jan 1st 1970
|
|
30 |
|
|
31 |
extern "C" {
|
|
32 |
|
|
33 |
//Returns the resolution (granularity) of a clock
|
|
34 |
//This value is placed in a (non-NULL) *res
|
|
35 |
EXPORT_C int clock_getres(clockid_t clock_id, struct timespec* res)
|
|
36 |
{
|
|
37 |
int retval = -1;
|
|
38 |
//We expect the user of the library to give us a valid pointer
|
|
39 |
if(res == NULL)
|
|
40 |
{
|
|
41 |
errno = EFAULT;
|
|
42 |
return retval;
|
|
43 |
}
|
|
44 |
|
|
45 |
switch (clock_id)
|
|
46 |
{
|
|
47 |
case CLOCK_REALTIME:
|
|
48 |
//Since Symbian OS is not realtime,we simulate the same using
|
|
49 |
//the available wall clock whose resolution is upto microseconds
|
|
50 |
res->tv_sec = 0;
|
|
51 |
res->tv_nsec = 1000000;
|
|
52 |
retval = 0;
|
|
53 |
break;
|
|
54 |
|
|
55 |
default:
|
|
56 |
//For all other clocks that cannot be supported or invalid clockids,
|
|
57 |
//we set errno to not-supported
|
|
58 |
retval = -1;
|
|
59 |
errno = EINVAL;
|
|
60 |
break;
|
|
61 |
}
|
|
62 |
return retval;
|
|
63 |
}
|
|
64 |
|
|
65 |
//Allow the calling process to retrieve the value used by a clock which
|
|
66 |
//is specified by clock_id
|
|
67 |
EXPORT_C int clock_gettime (clockid_t clock_id, struct timespec *tp)
|
|
68 |
{
|
|
69 |
int retval = -1;
|
|
70 |
TTime t;
|
|
71 |
TTimeIntervalSeconds iSeconds;
|
|
72 |
TInt err;
|
|
73 |
//We expect the user of the library to give us a valid pointer
|
|
74 |
if(tp == NULL)
|
|
75 |
{
|
|
76 |
errno = EFAULT;
|
|
77 |
return retval;
|
|
78 |
}
|
|
79 |
|
|
80 |
switch(clock_id)
|
|
81 |
{
|
|
82 |
case CLOCK_REALTIME:
|
|
83 |
//We use TTime::UniversalTime() call to get the Universal time
|
|
84 |
t.UniversalTime();
|
|
85 |
err = t.SecondsFrom(UNIX_BASE, iSeconds);
|
|
86 |
t-=iSeconds;//extracting seconds info into iSeconds
|
|
87 |
if (!err)
|
|
88 |
{
|
|
89 |
tp->tv_sec = iSeconds.Int();
|
|
90 |
tp->tv_nsec = t.Int64();
|
|
91 |
retval = 0;
|
|
92 |
}
|
|
93 |
break;
|
|
94 |
|
|
95 |
default:
|
|
96 |
//For all other clocks that cannot be supported or invalid clockids,
|
|
97 |
//we set errno to invalid
|
|
98 |
retval = -1;
|
|
99 |
errno = EINVAL;
|
|
100 |
break;
|
|
101 |
}
|
|
102 |
return retval;
|
|
103 |
}
|
|
104 |
|
|
105 |
|
|
106 |
//The clock_settime allow the calling process to set the value used by a
|
|
107 |
//clock which is specified by clock_id
|
|
108 |
EXPORT_C int clock_settime (clockid_t clock_id, const struct timespec *tp)
|
|
109 |
{
|
|
110 |
int retval = -1;
|
|
111 |
TTime t(MAKE_TINT64 (0x00dcddb3 ,0x0f2f8000)) ; // 00:00, Jan 1st 1970
|
|
112 |
TInt err;
|
|
113 |
TInt64 microtime;
|
|
114 |
//We must get a filled structure from the user of the library
|
|
115 |
if(tp == NULL)
|
|
116 |
{
|
|
117 |
errno = EFAULT;
|
|
118 |
return retval;
|
|
119 |
}
|
|
120 |
//Check for boundary values of seconds and microseconds
|
|
121 |
if (tp->tv_nsec < 0 || tp->tv_nsec >= 1000000000L)
|
|
122 |
{
|
|
123 |
errno = ERANGE;
|
|
124 |
return retval;
|
|
125 |
}
|
|
126 |
|
|
127 |
switch(clock_id)
|
|
128 |
{
|
|
129 |
case CLOCK_REALTIME:
|
|
130 |
//We support only the wall-clock,hence use the
|
|
131 |
//User::SetHomeTime call to set the time
|
|
132 |
t+=(TTimeIntervalSeconds)tp->tv_sec;
|
|
133 |
microtime = (tp->tv_nsec)/1000;
|
|
134 |
t+=(TTimeIntervalMicroSeconds)microtime;
|
|
135 |
err = User::SetUTCTime(t);
|
|
136 |
if(err)
|
|
137 |
{
|
|
138 |
MapError(err,errno);
|
|
139 |
break;
|
|
140 |
}
|
|
141 |
else
|
|
142 |
retval = 0;
|
|
143 |
break;
|
|
144 |
|
|
145 |
default:
|
|
146 |
//For all other clocks that cannot be supported or invalid clockids,
|
|
147 |
//we set errno to invalid
|
|
148 |
retval = -1;
|
|
149 |
errno = EINVAL;
|
|
150 |
break;
|
|
151 |
}
|
|
152 |
return retval;
|
|
153 |
}
|
|
154 |
|
|
155 |
|
|
156 |
//Returns the clock ID of the CPU-time clock of the process specified by pid
|
|
157 |
EXPORT_C int clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
|
|
158 |
{
|
|
159 |
int retval = -1;
|
|
160 |
/* We don't allow any process ID but our own. */
|
|
161 |
if (pid == 0)
|
|
162 |
{
|
|
163 |
//The only available clock is the realtime wall clock
|
|
164 |
//Hence we set the clockid to that
|
|
165 |
*clock_id = CLOCK_REALTIME;
|
|
166 |
retval = 0;
|
|
167 |
}
|
|
168 |
else
|
|
169 |
errno = ESRCH;
|
|
170 |
|
|
171 |
return retval;
|
|
172 |
}
|
|
173 |
|
|
174 |
//Makes small adjustments to the system time,
|
|
175 |
//advancing it by the time specified by the timeval delta
|
|
176 |
EXPORT_C int adjtime(const struct timeval *delta, struct timeval *olddelta)
|
|
177 |
{
|
|
178 |
//We can make only positive adjutments to the available clock
|
|
179 |
//The synchronization to the global time server is provided to us
|
|
180 |
//by the User::SetHomeTime api itself
|
|
181 |
int retval = -1;
|
|
182 |
TTime t;
|
|
183 |
TInt err;
|
|
184 |
TInt64 microtime;
|
|
185 |
long secs = 0;
|
|
186 |
suseconds_t usec = 0;
|
|
187 |
|
|
188 |
//We expect the user of the library to give us a valid pointer
|
|
189 |
if(delta == NULL)
|
|
190 |
{
|
|
191 |
errno = EFAULT;
|
|
192 |
return retval;
|
|
193 |
}
|
|
194 |
|
|
195 |
if ((delta->tv_usec) > 1000000)
|
|
196 |
{
|
|
197 |
errno = ERANGE;
|
|
198 |
return retval;
|
|
199 |
}
|
|
200 |
//Check for negative of seconds and make it positive
|
|
201 |
if((delta->tv_sec) < 0)
|
|
202 |
secs = -1*(delta->tv_sec);
|
|
203 |
else
|
|
204 |
secs = delta->tv_sec;
|
|
205 |
|
|
206 |
//Check for negative of microseconds and make it positive
|
|
207 |
if((delta->tv_usec) < 0)
|
|
208 |
usec = -1*(delta->tv_usec);
|
|
209 |
else
|
|
210 |
usec = delta->tv_usec;
|
|
211 |
|
|
212 |
t.HomeTime();
|
|
213 |
t+=(TTimeIntervalSeconds)secs;
|
|
214 |
microtime = usec;
|
|
215 |
t+=(TTimeIntervalMicroSeconds)microtime;
|
|
216 |
err = User::SetHomeTime(t);
|
|
217 |
if (!err)
|
|
218 |
{
|
|
219 |
retval = 0;
|
|
220 |
//olddelta is always zero in case of success
|
|
221 |
olddelta->tv_sec = 0;
|
|
222 |
olddelta->tv_usec = 0;
|
|
223 |
}
|
|
224 |
else
|
|
225 |
{
|
|
226 |
MapError(err,errno);
|
|
227 |
retval = -1;
|
|
228 |
}
|
|
229 |
return retval;
|
|
230 |
}
|
|
231 |
|
|
232 |
} // extern "C"
|