[lib] Avoid struct arguments & return values

Modify the *time*() functions to avoid passing structures in arguments
(using const pointers instead) or returning structures (using result
arguments instead).
This commit is contained in:
Matteo Cypriani 2012-01-07 00:43:21 +01:00
parent 93f316d2c6
commit cf73bf6309
7 changed files with 96 additions and 81 deletions

View File

@ -234,7 +234,7 @@ void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],
assert(src) ; assert(src) ;
owl_timestamp_to_string(timestamp_str, src->mobile_timestamp) ; owl_timestamp_to_string(&src->mobile_timestamp, timestamp_str) ;
snprintf(dst, OWL_CSV_RESULT_REQUEST_STRLEN, snprintf(dst, OWL_CSV_RESULT_REQUEST_STRLEN,
"%s;%"PRIu8";%s;%u", "%s;%"PRIu8";%s;%u",
src->mobile_mac_addr, src->mobile_mac_addr,
@ -353,7 +353,7 @@ void owl_fprint_result(FILE *stream, const owl_result *const src)
assert(src) ; assert(src) ;
owl_timestamp_to_string(timestamp_str, src->mobile_timestamp) ; owl_timestamp_to_string(&src->mobile_timestamp, timestamp_str) ;
fprintf(stream, fprintf(stream,
"Mobile MAC: %s\n" "Mobile MAC: %s\n"
"Request type: %"PRIu8"\n" "Request type: %"PRIu8"\n"

View File

@ -180,55 +180,62 @@ int owl_timestamp_now(owl_timestamp *const now)
return ret ; return ret ;
} }
*now = owl_timespec_to_timestamp(now_ts) ; owl_timespec_to_timestamp(&now_ts, now) ;
return 0 ; return 0 ;
} }
/* /*
* Returns a owl_timestamp from a struct timespec. * Converts the struct timespec 'src' into the owl_timestamp 'dst'.
*/ */
owl_timestamp owl_timespec_to_timestamp(const struct timespec d) void owl_timespec_to_timestamp(const struct timespec *const src,
owl_timestamp *const dst)
{ {
owl_timestamp res ; assert(src) ;
res.tv_sec = d.tv_sec ; assert(dst) ;
res.tv_nsec = d.tv_nsec ; dst->tv_sec = src->tv_sec ;
return res ; dst->tv_nsec = src->tv_nsec ;
} }
/* /*
* Returns a owl_timestamp from a struct timeval. * Converts the struct timeval 'src' into the owl_timestamp 'dst'.
*/ */
owl_timestamp owl_timeval_to_timestamp(const struct timeval d) void owl_timeval_to_timestamp(const struct timeval *const src,
owl_timestamp *const dst)
{ {
owl_timestamp res ; assert(src) ;
res.tv_sec = d.tv_sec ; assert(dst) ;
res.tv_nsec = d.tv_usec * 1000u ; dst->tv_sec = src->tv_sec ;
return res ; dst->tv_nsec = src->tv_usec * 1000u ;
} }
owl_bool owl_timestamp_equals(const owl_timestamp d1, owl_bool owl_timestamp_equals(const owl_timestamp *const d1,
const owl_timestamp d2) const owl_timestamp *const d2)
{ {
return d1.tv_sec == d2.tv_sec && d1.tv_nsec == d2.tv_nsec ; assert(d1) ;
assert(d2) ;
return d1->tv_sec == d2->tv_sec && d1->tv_nsec == d2->tv_nsec ;
} }
owl_bool owl_timestamp_is_null(const owl_timestamp d) owl_bool owl_timestamp_is_null(const owl_timestamp *const d)
{ {
return d.tv_sec == 0 && d.tv_nsec == 0 ; assert(d) ;
return d->tv_sec == 0 && d->tv_nsec == 0 ;
} }
/* /*
* Converts a owl_timestamp date value into milliseconds. * Converts a owl_timestamp date value into milliseconds.
*/ */
uint64_t owl_timestamp_to_ms(const owl_timestamp d) uint64_t owl_timestamp_to_ms(const owl_timestamp *const d)
{ {
return (uint64_t) d.tv_sec * 1000u + (uint64_t) d.tv_nsec / 1000000lu ; assert(d) ;
return
(uint64_t) d->tv_sec * 1000u + (uint64_t) d->tv_nsec / 1000000lu ;
} }
@ -237,22 +244,25 @@ uint64_t owl_timestamp_to_ms(const owl_timestamp d)
* 'dst' must be an allocated array of at least OWL_TIMESTAMP_STRLEN * 'dst' must be an allocated array of at least OWL_TIMESTAMP_STRLEN
* characters. * characters.
*/ */
void owl_timestamp_to_string(char *const dst, const owl_timestamp src) void owl_timestamp_to_string(const owl_timestamp *const src,
char *const dst)
{ {
assert(src) ;
assert(dst) ;
snprintf(dst, OWL_TIMESTAMP_STRLEN, "%"PRIu32".%"PRIu32, snprintf(dst, OWL_TIMESTAMP_STRLEN, "%"PRIu32".%"PRIu32,
src.tv_sec, src.tv_nsec) ; src->tv_sec, src->tv_nsec) ;
} }
/* /*
* Returns the time (in milliseconds) between two dates. * Returns the time (in milliseconds) between two dates.
*/ */
uint_fast32_t owl_time_elapsed_ms(const owl_timestamp d1, uint_fast32_t owl_time_elapsed_ms(const owl_timestamp *const d1,
const owl_timestamp d2) const owl_timestamp *const d2)
{ {
owl_timestamp elapsed ; owl_timestamp elapsed ;
owl_time_elapsed(&d1, &d2, &elapsed) ; owl_time_elapsed(d1, d2, &elapsed) ;
return owl_timestamp_to_ms(elapsed) ; return owl_timestamp_to_ms(&elapsed) ;
} }
@ -317,24 +327,22 @@ void owl_time_elapsed(const owl_timestamp *const d1,
/* /*
* Converts a owl_timestamp from host endianess to network endianess. * Converts a owl_timestamp from host endianess to network endianess.
*/ */
owl_timestamp owl_hton_timestamp(const owl_timestamp d) void owl_hton_timestamp(owl_timestamp *const d)
{ {
owl_timestamp date ; assert(d) ;
date.tv_sec = htonl(d.tv_sec) ; d->tv_sec = htonl(d->tv_sec) ;
date.tv_nsec = htonl(d.tv_nsec) ; d->tv_nsec = htonl(d->tv_nsec) ;
return date ;
} }
/* /*
* Converts a owl_timestamp from network endianess to host endianess. * Converts a owl_timestamp from network endianess to host endianess.
*/ */
owl_timestamp owl_ntoh_timestamp(const owl_timestamp d) void owl_ntoh_timestamp(owl_timestamp *const d)
{ {
owl_timestamp date ; assert(d) ;
date.tv_sec = ntohl(d.tv_sec) ; d->tv_sec = ntohl(d->tv_sec) ;
date.tv_nsec = ntohl(d.tv_nsec) ; d->tv_nsec = ntohl(d->tv_nsec) ;
return date ;
} }

View File

@ -212,22 +212,25 @@ uint_fast8_t owl_frequency_to_channel(const uint_fast16_t channel) ;
// Time // Time
int owl_msleep(uint32_t time_ms) ; int owl_msleep(uint32_t time_ms) ;
int owl_timestamp_now(owl_timestamp *const now) ; int owl_timestamp_now(owl_timestamp *const now) ;
owl_timestamp owl_timespec_to_timestamp(const struct timespec d) ; void owl_timespec_to_timestamp(const struct timespec *const src,
owl_timestamp owl_timeval_to_timestamp(const struct timeval d) ; owl_timestamp *const dst) ;
owl_bool owl_timestamp_equals(const owl_timestamp d1, void owl_timeval_to_timestamp(const struct timeval *const src,
const owl_timestamp d2) ; owl_timestamp *const dst) ;
owl_bool owl_timestamp_is_null(const owl_timestamp d) ; owl_bool owl_timestamp_equals(const owl_timestamp *const d1,
uint64_t owl_timestamp_to_ms(const owl_timestamp d) ; const owl_timestamp *const d2) ;
void owl_timestamp_to_string(char *const dst, const owl_timestamp src) ; owl_bool owl_timestamp_is_null(const owl_timestamp *const d) ;
uint_fast32_t owl_time_elapsed_ms(const owl_timestamp d1, uint64_t owl_timestamp_to_ms(const owl_timestamp *const d) ;
const owl_timestamp d2) ; void owl_timestamp_to_string(const owl_timestamp *const src,
char *const dst) ;
uint_fast32_t owl_time_elapsed_ms(const owl_timestamp *const d1,
const owl_timestamp *const d2) ;
void owl_time_elapsed(const owl_timestamp *const d1, void owl_time_elapsed(const owl_timestamp *const d1,
const owl_timestamp *const d2, const owl_timestamp *const d2,
owl_timestamp *const elapsed) ; owl_timestamp *const elapsed) ;
// Endianess // Endianess
owl_timestamp owl_hton_timestamp(const owl_timestamp d) ; void owl_hton_timestamp(owl_timestamp *const d) ;
owl_timestamp owl_ntoh_timestamp(const owl_timestamp d) ; void owl_ntoh_timestamp(owl_timestamp *const d) ;
float owl_swap_float(const float f) ; float owl_swap_float(const float f) ;
#if __BYTE_ORDER == __BIG_ENDIAN #if __BYTE_ORDER == __BIG_ENDIAN
# define owl_htonf(f) (f) # define owl_htonf(f) (f)

View File

@ -468,8 +468,8 @@ int read_loop(int sockfd)
} }
// Endianess conversions: // Endianess conversions:
request.request_time = owl_ntoh_timestamp(request.request_time) ; owl_ntoh_timestamp(&request.request_time) ;
request.capture_time = owl_ntoh_timestamp(request.capture_time) ; owl_ntoh_timestamp(&request.capture_time) ;
request.x_position = owl_ntohf(request.x_position) ; request.x_position = owl_ntohf(request.x_position) ;
request.y_position = owl_ntohf(request.y_position) ; request.y_position = owl_ntohf(request.y_position) ;
request.z_position = owl_ntohf(request.z_position) ; request.z_position = owl_ntohf(request.z_position) ;
@ -483,9 +483,10 @@ int read_loop(int sockfd)
mobile_mac_addr_str[OWL_ETHER_ADDR_STRLEN], mobile_mac_addr_str[OWL_ETHER_ADDR_STRLEN],
mobile_ip_str[INET_ADDRSTRLEN] ; mobile_ip_str[INET_ADDRSTRLEN] ;
owl_timestamp_to_string(request_time_str, owl_timestamp_to_string(&request.request_time,
request.request_time) ; request_time_str) ;
owl_timestamp_to_string(capture_time_str, request.capture_time) ; owl_timestamp_to_string(&request.capture_time,
capture_time_str) ;
owl_mac_bytes_to_string_r(request.ap_mac_addr_bytes, owl_mac_bytes_to_string_r(request.ap_mac_addr_bytes,
ap_mac_addr_str) ; ap_mac_addr_str) ;
owl_mac_bytes_to_string_r(request.mobile_mac_addr_bytes, owl_mac_bytes_to_string_r(request.mobile_mac_addr_bytes,
@ -594,8 +595,8 @@ void* monitor_requests(void *NULL_value)
while (request_ptr != NULL) // Parsing list while (request_ptr != NULL) // Parsing list
{ {
sub = owl_time_elapsed_ms(request_ptr->start_time, sub = owl_time_elapsed_ms(&request_ptr->start_time,
current_time) ; &current_time) ;
// If the request was not treated already // If the request was not treated already
if (request_ptr->info != NULL) if (request_ptr->info != NULL)
@ -618,8 +619,8 @@ void* monitor_requests(void *NULL_value)
fprintf(fd, "%"PRIu8";", request_ptr->type) ; fprintf(fd, "%"PRIu8";", request_ptr->type) ;
// Print request mobile timestamp to the output file // Print request mobile timestamp to the output file
owl_timestamp_to_string(request_time_str, owl_timestamp_to_string(&request_ptr->request_time,
request_ptr->request_time) ; request_time_str) ;
fprintf(fd, "%s;", request_time_str) ; fprintf(fd, "%s;", request_time_str) ;
// Print request info to the output file // Print request info to the output file
@ -634,8 +635,8 @@ void* monitor_requests(void *NULL_value)
memcpy(request.mobile_mac_addr_bytes, memcpy(request.mobile_mac_addr_bytes,
request_ptr->mobile_mac_addr_bytes, request_ptr->mobile_mac_addr_bytes,
ETHER_ADDR_LEN) ; ETHER_ADDR_LEN) ;
request.request_time = request.request_time = request_ptr->request_time ;
owl_hton_timestamp(request_ptr->request_time) ; owl_hton_timestamp(&request.request_time) ;
request.x_position = request.x_position =
owl_htonf(request_ptr->x_position) ; owl_htonf(request_ptr->x_position) ;
request.y_position = request.y_position =
@ -790,8 +791,8 @@ void got_request(owl_captured_request request)
{ // Research criterion: MAC and transmission time { // Research criterion: MAC and transmission time
if (owl_mac_equals(request.mobile_mac_addr_bytes, if (owl_mac_equals(request.mobile_mac_addr_bytes,
tmp_request->mobile_mac_addr_bytes) tmp_request->mobile_mac_addr_bytes)
&& owl_timestamp_equals(request.request_time, && owl_timestamp_equals(&request.request_time,
tmp_request->request_time)) &tmp_request->request_time))
break ; // If the request exists, we stop on it break ; // If the request exists, we stop on it
tmp_request = tmp_request->next ; tmp_request = tmp_request->next ;
} }
@ -804,9 +805,9 @@ void got_request(owl_captured_request request)
// times on the APs less than 10 ms // times on the APs less than 10 ms
// TODO : define an option for the maximal difference time. // TODO : define an option for the maximal difference time.
if (owl_mac_equals(request.mobile_mac_addr_bytes, if (owl_mac_equals(request.mobile_mac_addr_bytes,
tmp_request->mobile_mac_addr_bytes) tmp_request->mobile_mac_addr_bytes) &&
&& owl_time_elapsed_ms(request.capture_time, owl_time_elapsed_ms(&request.capture_time,
tmp_request->request_time) <= 10) &tmp_request->request_time) <= 10)
break ; // If the request exists, we stop on it break ; // If the request exists, we stop on it
tmp_request = tmp_request->next ; tmp_request = tmp_request->next ;
} }
@ -1102,7 +1103,7 @@ void delete_old_aps()
owl_timestamp_now(&now) ; owl_timestamp_now(&now) ;
while (token_aps != NULL) while (token_aps != NULL)
if (owl_time_elapsed_ms(token_aps->last_seen, now) > if (owl_time_elapsed_ms(&token_aps->last_seen, &now) >
(uint_fast32_t) cfg_getint(cfg, "ap_keep_timeout") * 1000) (uint_fast32_t) cfg_getint(cfg, "ap_keep_timeout") * 1000)
delete_ap(token_aps) ; delete_ap(token_aps) ;
else else
@ -1248,10 +1249,10 @@ void print_request_list()
owl_mac_bytes_to_string_r(request_ptr->mobile_mac_addr_bytes, owl_mac_bytes_to_string_r(request_ptr->mobile_mac_addr_bytes,
mobile_mac_str) ; mobile_mac_str) ;
owl_timestamp_to_string(request_time_str, owl_timestamp_to_string(&request_ptr->request_time,
request_ptr->request_time) ; request_time_str) ;
owl_timestamp_to_string(start_time_str, owl_timestamp_to_string(&request_ptr->start_time,
request_ptr->start_time) ; start_time_str) ;
fprintf(stderr, fprintf(stderr,
"Type: %"PRIu8"\n" "Type: %"PRIu8"\n"
"Mobile MAC: %s\n" "Mobile MAC: %s\n"

View File

@ -400,8 +400,8 @@ void make_packet()
// Get the current time and copy it as a string before to switch it to // Get the current time and copy it as a string before to switch it to
// network endianess: // network endianess:
owl_timestamp_now(&request_time) ; owl_timestamp_now(&request_time) ;
owl_timestamp_to_string(request_time_str, request_time) ; owl_timestamp_to_string(&request_time, request_time_str) ;
request_time = owl_hton_timestamp(request_time) ; owl_hton_timestamp(&request_time) ;
if (is_calibration_request) // Calibration packet if (is_calibration_request) // Calibration packet
{ {

View File

@ -926,8 +926,8 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
// Copy AP MAC : // Copy AP MAC :
memcpy(request.ap_mac_addr_bytes, my_mac_bytes, ETHER_ADDR_LEN) ; memcpy(request.ap_mac_addr_bytes, my_mac_bytes, ETHER_ADDR_LEN) ;
// Capture time is in the pcap header (host-endian): // Capture time is in the pcap header (host-endian):
request.capture_time = owl_timeval_to_timestamp(&header->ts, &request.capture_time) ;
owl_hton_timestamp(owl_timeval_to_timestamp(header->ts)) ; owl_hton_timestamp(&request.capture_time) ;
/* Active mode */ /* Active mode */
if (is_explicit_packet if (is_explicit_packet
@ -1018,6 +1018,7 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
/* Display the packet details */ /* Display the packet details */
if (VERBOSE_DISPLAY_CAPTURED) if (VERBOSE_DISPLAY_CAPTURED)
{ {
owl_timestamp tmp_time ;
char char
request_time_str[OWL_TIMESTAMP_STRLEN], request_time_str[OWL_TIMESTAMP_STRLEN],
capture_time_str[OWL_TIMESTAMP_STRLEN], capture_time_str[OWL_TIMESTAMP_STRLEN],
@ -1025,10 +1026,12 @@ void read_packet(u_char *args, const struct pcap_pkthdr *header,
mobile_mac_addr_str[OWL_ETHER_ADDR_STRLEN], mobile_mac_addr_str[OWL_ETHER_ADDR_STRLEN],
mobile_ip_str[INET_ADDRSTRLEN] ; mobile_ip_str[INET_ADDRSTRLEN] ;
owl_timestamp_to_string(request_time_str, tmp_time = request.request_time ;
owl_ntoh_timestamp(request.request_time)) ; owl_ntoh_timestamp(&tmp_time) ;
owl_timestamp_to_string(capture_time_str, owl_timestamp_to_string(&tmp_time, request_time_str) ;
owl_ntoh_timestamp(request.capture_time)) ; tmp_time = request.capture_time ;
owl_ntoh_timestamp(&tmp_time) ;
owl_timestamp_to_string(&tmp_time, capture_time_str) ;
owl_mac_bytes_to_string_r(request.ap_mac_addr_bytes, owl_mac_bytes_to_string_r(request.ap_mac_addr_bytes,
ap_mac_addr_str) ; ap_mac_addr_str) ;
owl_mac_bytes_to_string_r(request.mobile_mac_addr_bytes, owl_mac_bytes_to_string_r(request.mobile_mac_addr_bytes,
@ -1388,11 +1391,11 @@ uint_fast16_t make_packet(uint8_t **packet)
if (VERBOSE_CHATTERBOX) if (VERBOSE_CHATTERBOX)
{ {
char request_time_str[OWL_TIMESTAMP_STRLEN] ; char request_time_str[OWL_TIMESTAMP_STRLEN] ;
owl_timestamp_to_string(request_time_str, request_time) ; owl_timestamp_to_string(&request_time, request_time_str) ;
printf("Autocalibration time: %s\n", request_time_str) ; printf("Autocalibration time: %s\n", request_time_str) ;
} }
request_time = owl_hton_timestamp(request_time) ; owl_hton_timestamp(&request_time) ;
offset = 0 ; offset = 0 ;
size = size =

View File

@ -97,7 +97,7 @@ bool InputUDPSocket::fill_current_request()
} }
// Endianess conversions // Endianess conversions
request.request_time = owl_ntoh_timestamp(request.request_time) ; owl_ntoh_timestamp(&request.request_time) ;
request.nb_info = ntohs(request.nb_info) ; request.nb_info = ntohs(request.nb_info) ;
request.x_position = owl_ntohf(request.x_position) ; request.x_position = owl_ntohf(request.x_position) ;
request.y_position = owl_ntohf(request.y_position) ; request.y_position = owl_ntohf(request.y_position) ;