os/ossrv/genericopenlibs/posixrealtimeextensions/src/clock_funcs.cpp
author sl
Tue, 10 Jun 2014 14:32:02 +0200
changeset 1 260cb5ec6c19
permissions -rw-r--r--
Update contrib.
sl@0
     1
/*
sl@0
     2
* Copyright (c) 2005-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 "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:  This is a project specific source file for building the 
sl@0
    15
*                clock related functions as part of librt library.
sl@0
    16
*
sl@0
    17
*/
sl@0
    18
sl@0
    19
sl@0
    20
#include <time.h>
sl@0
    21
#include <errno.h>
sl@0
    22
#include <pthread.h>
sl@0
    23
#include <stdlib.h>
sl@0
    24
#include <e32std.h>
sl@0
    25
#include <sys/_timeval.h>
sl@0
    26
#include <sys/types.h>
sl@0
    27
sl@0
    28
#include "lposix.h"
sl@0
    29
sl@0
    30
#define UNIX_BASE	TTime(MAKE_TINT64(0x00dcddb3,0x0f2f8000))    // 00:00, Jan 1st 1970
sl@0
    31
sl@0
    32
#define BOUNDARY_CHECK(rqtp) ( (rqtp->tv_nsec != 0 && rqtp->tv_nsec < 1000) || \
sl@0
    33
							   rqtp->tv_nsec >= 1000000000L )\
sl@0
    34
sl@0
    35
extern "C" {
sl@0
    36
sl@0
    37
//Returns the resolution (granularity) of a clock
sl@0
    38
//This value is placed in a (non-NULL) *res
sl@0
    39
EXPORT_C int clock_getres(clockid_t clock_id, struct timespec* res)
sl@0
    40
{
sl@0
    41
	int retval = -1;
sl@0
    42
	//We expect the user of the library to give us a valid pointer	
sl@0
    43
	if(res == NULL)
sl@0
    44
		{
sl@0
    45
		return 0; //no strict reactions please.
sl@0
    46
		}
sl@0
    47
			
sl@0
    48
	switch (clock_id) 
sl@0
    49
		{
sl@0
    50
		case CLOCK_REALTIME:
sl@0
    51
			//Since Symbian OS is not realtime,we simulate the same using
sl@0
    52
			//the available wall clock whose resolution is upto microseconds
sl@0
    53
			res->tv_sec = 0;
sl@0
    54
			res->tv_nsec = 1000;
sl@0
    55
			retval = 0;
sl@0
    56
			break;
sl@0
    57
		
sl@0
    58
		default:
sl@0
    59
			//For all other clocks that cannot be supported or invalid clockids,
sl@0
    60
			//we set errno to not-supported
sl@0
    61
			retval = -1;
sl@0
    62
			errno = EINVAL;
sl@0
    63
			break;
sl@0
    64
		}
sl@0
    65
		
sl@0
    66
	return retval;
sl@0
    67
}
sl@0
    68
sl@0
    69
//Allow the calling process to retrieve the value used by a clock which 
sl@0
    70
//is specified by clock_id
sl@0
    71
EXPORT_C int clock_gettime (clockid_t clock_id, struct timespec *tp)
sl@0
    72
{
sl@0
    73
	int retval = -1;
sl@0
    74
	TTime t;
sl@0
    75
	TTimeIntervalSeconds iSeconds;
sl@0
    76
	TInt err;
sl@0
    77
	//We expect the user of the library to give us a valid pointer	
sl@0
    78
	if(tp == NULL)
sl@0
    79
		{
sl@0
    80
		errno = EFAULT;
sl@0
    81
		return retval;
sl@0
    82
		}
sl@0
    83
sl@0
    84
	switch(clock_id)
sl@0
    85
		{
sl@0
    86
		case CLOCK_REALTIME:
sl@0
    87
			//Since Symbian OS is not realtime,we simulate the same using
sl@0
    88
			//the available wall clock.We use TTime::HomeTime() call to get
sl@0
    89
			//the wall clock time
sl@0
    90
			t.HomeTime();
sl@0
    91
			err = t.SecondsFrom(UNIX_BASE, iSeconds); //TODO check for the negative tests..
sl@0
    92
			if (!err)
sl@0
    93
				{
sl@0
    94
				t-=iSeconds;//extracting seconds info into iSeconds 			
sl@0
    95
				tp->tv_sec = iSeconds.Int();
sl@0
    96
				tp->tv_nsec = t.Int64();
sl@0
    97
				retval = 0;
sl@0
    98
				}
sl@0
    99
			break;
sl@0
   100
		
sl@0
   101
		default:
sl@0
   102
			//For all other clocks that cannot be supported or invalid clockids,
sl@0
   103
			//we set errno to invalid
sl@0
   104
			retval = -1;
sl@0
   105
			errno = EINVAL;
sl@0
   106
			break;
sl@0
   107
		}
sl@0
   108
	return retval;
sl@0
   109
}
sl@0
   110
sl@0
   111
sl@0
   112
//The clock_settime allow the calling process to set the value used by a 
sl@0
   113
//clock which is specified by  clock_id
sl@0
   114
EXPORT_C int clock_settime (clockid_t clock_id, const struct timespec *tp)
sl@0
   115
{
sl@0
   116
	int retval = -1;
sl@0
   117
	TTime t(MAKE_TINT64 (0x00dcddb3 ,0x0f2f8000)) ;  // 00:00, Jan 1st 1970
sl@0
   118
	TInt err;
sl@0
   119
	TInt64 microtime;
sl@0
   120
	
sl@0
   121
	if(tp == NULL)
sl@0
   122
		{
sl@0
   123
		errno = EFAULT;
sl@0
   124
		return retval;
sl@0
   125
		}
sl@0
   126
			
sl@0
   127
	//Check for boundary values of seconds and microseconds
sl@0
   128
	if (BOUNDARY_CHECK(tp))
sl@0
   129
		{
sl@0
   130
		errno = EINVAL;
sl@0
   131
		return retval;
sl@0
   132
		}
sl@0
   133
sl@0
   134
	switch(clock_id)
sl@0
   135
		{
sl@0
   136
		case CLOCK_REALTIME:
sl@0
   137
			//We support only the wall-clock,hence use the 
sl@0
   138
			//User::SetHomeTime call to set the time
sl@0
   139
			t+=(TTimeIntervalSeconds)tp->tv_sec;
sl@0
   140
			microtime = (tp->tv_nsec)/1000;
sl@0
   141
			t+=(TTimeIntervalMicroSeconds)microtime;        
sl@0
   142
			err = User::SetUTCTime(t);
sl@0
   143
			if(err)
sl@0
   144
				{
sl@0
   145
				MapError(err,errno);
sl@0
   146
				break;
sl@0
   147
				}
sl@0
   148
			else
sl@0
   149
				retval = 0;	
sl@0
   150
			break;
sl@0
   151
			
sl@0
   152
		default:
sl@0
   153
			//For all other clocks that cannot be supported or invalid clockids,
sl@0
   154
			//we set errno to invalid
sl@0
   155
			retval = -1;
sl@0
   156
			errno = EINVAL;
sl@0
   157
			break;
sl@0
   158
		}	
sl@0
   159
		
sl@0
   160
	return retval;
sl@0
   161
}
sl@0
   162
sl@0
   163
sl@0
   164
//Returns the clock ID of the CPU-time clock of the process specified by pid
sl@0
   165
EXPORT_C int clock_getcpuclockid (pid_t pid, clockid_t *clock_id)
sl@0
   166
{
sl@0
   167
	int retval = -1;
sl@0
   168
sl@0
   169
	if(clock_id == NULL)
sl@0
   170
		{
sl@0
   171
		errno = EFAULT;
sl@0
   172
		return retval;
sl@0
   173
		}
sl@0
   174
			
sl@0
   175
	/* We don't allow any process ID but our own.  */
sl@0
   176
	if (pid == 0)
sl@0
   177
		{
sl@0
   178
		//The only available clock is the realtime wall clock
sl@0
   179
		//Hence we set the clockid to that
sl@0
   180
		*clock_id = CLOCK_REALTIME;
sl@0
   181
		retval = 0;
sl@0
   182
		}
sl@0
   183
	else
sl@0
   184
		errno = ESRCH;
sl@0
   185
	
sl@0
   186
	return retval;
sl@0
   187
}
sl@0
   188
sl@0
   189
sl@0
   190
//clock_nanosleep will not be interrupted by the signal emulation. 
sl@0
   191
//hence EINTR is not valid here. 
sl@0
   192
sl@0
   193
EXPORT_C int clock_nanosleep(clockid_t clock_id, int flags,
sl@0
   194
       const struct timespec *rqtp, struct timespec */*rmtp*/)
sl@0
   195
{
sl@0
   196
	int retval = -1;
sl@0
   197
	
sl@0
   198
	if(rqtp == NULL)
sl@0
   199
		{
sl@0
   200
		errno = EFAULT;
sl@0
   201
		return retval;
sl@0
   202
		}
sl@0
   203
			
sl@0
   204
	//Check for boundary values of seconds and microseconds
sl@0
   205
	if (BOUNDARY_CHECK(rqtp))
sl@0
   206
		{
sl@0
   207
		errno = EINVAL;
sl@0
   208
		return retval;
sl@0
   209
		}
sl@0
   210
		
sl@0
   211
	switch(clock_id)
sl@0
   212
		{
sl@0
   213
		case CLOCK_REALTIME:
sl@0
   214
			{
sl@0
   215
			switch(flags)
sl@0
   216
				{
sl@0
   217
				case TIMER_ABSTIME:
sl@0
   218
					{
sl@0
   219
					TTime lSetTime(MAKE_TINT64 (0x00dcddb3 ,0x0f2f8000)) ;  // 00:00, Jan 1st 1970				
sl@0
   220
						
sl@0
   221
					lSetTime+=(TTimeIntervalSeconds)rqtp->tv_sec;
sl@0
   222
					lSetTime+=(TTimeIntervalMicroSeconds)(rqtp->tv_nsec/1000);
sl@0
   223
						
sl@0
   224
					User::At(lSetTime);
sl@0
   225
					}
sl@0
   226
					break;
sl@0
   227
				
sl@0
   228
				default:
sl@0
   229
					{
sl@0
   230
					unsigned long timeout;
sl@0
   231
					timeout = (1000000 * rqtp->tv_sec) + (rqtp->tv_nsec /1000);
sl@0
   232
					User::AfterHighRes(timeout);
sl@0
   233
					}
sl@0
   234
					break;
sl@0
   235
				}
sl@0
   236
			}
sl@0
   237
			retval = 0;
sl@0
   238
			break;
sl@0
   239
				
sl@0
   240
		default:
sl@0
   241
			//For all other clocks that cannot be supported or invalid clockids,
sl@0
   242
			//we set errno to invalid
sl@0
   243
			retval = -1;
sl@0
   244
			errno = EINVAL;
sl@0
   245
			break;
sl@0
   246
		}
sl@0
   247
		
sl@0
   248
	return retval;	
sl@0
   249
}
sl@0
   250
} // extern "C"
sl@0
   251
sl@0
   252
//EOF
sl@0
   253
sl@0
   254
sl@0
   255