[lib] Use clock_gettime() instead of gettimeofday()

Abandon the old struct timeval and gettimeofday(). We now use the new
struct timespec and clock_gettime().
struct timespec has a nanosecond resolution, but we lower the resolution
to keep only milliseconds. The code is inspired from the Timestamp class
of the owlps-positioning.
This commit is contained in:
Matteo Cypriani 2011-03-10 15:51:42 +01:00
parent 4c1da67294
commit f8e7d9adef
7 changed files with 51 additions and 27 deletions

3
TODO
View File

@ -1,4 +1 @@
Abandonner gettimeofday() et struct timeval au profit de clock_gettime()
et struct timespec. gettimeofday() est obsolète depuis POSIX:2008.
Abandonner int, long & Cie pour les échanges de données, passer à stdint.h

View File

@ -25,7 +25,7 @@ DEPFLAGS = -MMD
XCFLAGS = $(CFLAGS) $(DEPFLAGS) $(WARN) $(HEADERS)
PICFLAG = -fPIC
LIBS = -L../../libowlps -lowlps -L../libowlps-client -lowlps-client
STATIC_LIBS = -liw -lm
STATIC_LIBS = -liw -lm -lrt
## Cibles de compilation standard ##

View File

@ -629,7 +629,8 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
return ;
memcpy(couple.ap_mac_addr_bytes, my_mac, 6) ; // Copy AP MAC
couple.start_time = header->ts ; // Capture time is in the pcap header
// Capture time is in the pcap header:
couple.start_time = timeval_to_timestamp(header->ts) ;
// Transmission time on the mobile is unknown (unless the packet is
// an explicit request):
bzero(&couple.request_time, sizeof(TIMESTAMP)) ;

View File

@ -38,7 +38,7 @@ CFLAGS=-O2 -Wall -Wextra -Wstrict-prototypes -O -I.
DEPFLAGS=-MMD
XCFLAGS=$(CFLAGS) $(DEPFLAGS) $(WARN) $(HEADERS)
PICFLAG=-fPIC
LIBS=-liw
LIBS = -liw -lrt
#STRIPFLAGS= -Wl,-s
#LDFLAGS=

View File

@ -38,7 +38,7 @@ CFLAGS=-O2 -Wall -Wextra -Wstrict-prototypes -O -I.
DEPFLAGS=-MMD
XCFLAGS=$(CFLAGS) $(DEPFLAGS) $(WARN) $(HEADERS)
PICFLAG=-fPIC
LIBS=-liw
LIBS = -liw -lrt
#STRIPFLAGS= -Wl,-s
#LDFLAGS=
LATHEROS=-L$(TOOLCHAIN)/mips/usr/lib/

View File

@ -91,12 +91,50 @@ char frequency_to_channel(unsigned short channel)
/*
* Set the TIMESTAMP 'now' at the current time.
* Set the TIMESTAMP 'now' at the current time (millisecond-precision).
*/
void timestamp_now(TIMESTAMP *now)
{
if (gettimeofday(now, NULL))
perror("Cannot get the current time") ;
if (timestamp_now_ns(now))
timestamp_round_to_ms(now) ;
}
/*
* Set the TIMESTAMP 'now' at the current time (nanosecond-precision).
*/
BOOL timestamp_now_ns(TIMESTAMP *now)
{
if (clock_gettime(CLOCK_REALTIME, now))
{
perror("Cannot get the current time") ;
return FALSE ;
}
return TRUE ;
}
/*
* Lower the precision of 'now' to milliseconds.
*/
void timestamp_round_to_ms(TIMESTAMP *now)
{
now->tv_nsec = now->tv_nsec / 1000000 * 1000000 ;
}
/*
* Returns a TIMESTAMP from a struct timeval.
*/
TIMESTAMP timeval_to_timestamp(const struct timeval d)
{
TIMESTAMP res ;
res.tv_sec = d.tv_sec ;
res.tv_nsec = d.tv_usec * 1000 ;
return res ;
}
@ -106,25 +144,11 @@ void timestamp_now(TIMESTAMP *now)
*/
unsigned long long timestamp_to_ms(TIMESTAMP d)
{
return d.tv_sec * 1000 + d.tv_usec / 1000 ;
return d.tv_sec * 1000 + d.tv_nsec / 1000000 ;
}
/*
* Converts a date value in milliseconds into a TIMESTAMP.
*
TIMESTAMP ms_to_timestamp(unsigned long long tms)
{
TIMESTAMP d ;
d.tv_sec = tms / 1000 ;
d.tv_usec = (tms - d.tv_sec * 1000) * 1000 ;
return d ;
}
*/
/*
* Returns the time (in milliseconds) between two dates.
*/

View File

@ -13,7 +13,6 @@
#include <pthread.h>
#include <sys/time.h>
#include <time.h>
#include <sys/types.h>
@ -61,7 +60,7 @@ typedef enum {NORTH = 1, EAST, SOUTH, WEST} DIRECTION ;
/* Timestamp type */
typedef struct timeval TIMESTAMP ;
typedef struct timespec TIMESTAMP ;
/* Message sent by the listener to the aggregator */
@ -224,6 +223,9 @@ char frequency_to_channel(unsigned short channel) ;
// Time
void timestamp_now(TIMESTAMP *now) ;
BOOL timestamp_now_ns(TIMESTAMP *now) ;
void timestamp_round_to_ms(TIMESTAMP *now) ;
TIMESTAMP timeval_to_timestamp(const struct timeval d) ;
unsigned long long timestamp_to_ms(TIMESTAMP date) ;
unsigned long time_elapsed(TIMESTAMP sup, TIMESTAMP inf) ;