[lib-result] owl_receive_position(): return a pointer
Return a pointer instead of using a result argument.
This commit is contained in:
parent
8da063b597
commit
a7ec779231
|
@ -11,16 +11,14 @@
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Receives a result from the socket of file descriptor 'sockfd' and
|
* Receives a result from the socket of file descriptor 'sockfd', fills
|
||||||
* fills an owl_result structure 'result'.
|
* an owl_result structure with the received information, and returns a
|
||||||
|
* pointer to it (or NULL in case of error).
|
||||||
*
|
*
|
||||||
* Note that *result will be set by this function, and **result will
|
* Note that the new owl_result is allocated with malloc() and must be
|
||||||
* point on a valid owl_result structure if everything goes well.
|
* deleted using free().
|
||||||
* You will have to free *result yourself with owl_free_result().
|
|
||||||
*
|
|
||||||
* In case of error, *result is set to NULL.
|
|
||||||
*/
|
*/
|
||||||
void owl_receive_position(int sockfd, owl_result **result)
|
owl_result* owl_receive_position(int sockfd)
|
||||||
{
|
{
|
||||||
ssize_t nread ; // recvfrom return value
|
ssize_t nread ; // recvfrom return value
|
||||||
char csv[OWL_CSV_RESULT_STRLEN] ; // Read string
|
char csv[OWL_CSV_RESULT_STRLEN] ; // Read string
|
||||||
|
@ -31,30 +29,35 @@ void owl_receive_position(int sockfd, owl_result **result)
|
||||||
if (nread <= 0)
|
if (nread <= 0)
|
||||||
{
|
{
|
||||||
perror("No request received from listener") ;
|
perror("No request received from listener") ;
|
||||||
*result = NULL ;
|
return NULL ;
|
||||||
return ;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
owl_fill_result(result, csv) ;
|
return owl_fill_result(csv) ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* Splits the 'csv' string received from OwlPS Positioning and stores
|
/*
|
||||||
* the fields in 'result' (which is allocated).
|
* Splits the 'csv' string received from OwlPS Positioning and stores
|
||||||
|
* the fields in a new owl_result, and returns a pointer to it (or NULL
|
||||||
|
* in case of error).
|
||||||
|
*
|
||||||
|
* Note that the new owl_result is allocated with malloc() and must be
|
||||||
|
* deleted using free().
|
||||||
*
|
*
|
||||||
* Handled CSV format:
|
* Handled CSV format:
|
||||||
* Mobile_MAC;Request_type;Request_timestamp;Algorithm;X;Y;Z;Error;Area
|
* Mobile_MAC;Request_type;Request_timestamp;Algorithm;X;Y;Z;Error;Area
|
||||||
* The Request_timestamp format is:
|
* The Request_timestamp format is:
|
||||||
* seconds.nanoseconds
|
* seconds.nanoseconds
|
||||||
*/
|
*/
|
||||||
void owl_fill_result(owl_result **result, char *csv)
|
owl_result* owl_fill_result(char *csv)
|
||||||
{
|
{
|
||||||
char *csv_field = NULL ;
|
char *csv_field = NULL ;
|
||||||
|
owl_result *result = NULL ;
|
||||||
int nb_algorithms = 0 ;
|
int nb_algorithms = 0 ;
|
||||||
owl_algorithm_result *current_algo = NULL ;
|
owl_algorithm_result *current_algo = NULL ;
|
||||||
|
|
||||||
*result = malloc(sizeof(owl_result)) ;
|
result = malloc(sizeof(owl_result)) ;
|
||||||
(*result)->results = NULL ;
|
memset(result, 0, sizeof(*result)) ;
|
||||||
|
|
||||||
/* Mobile MAC address */
|
/* Mobile MAC address */
|
||||||
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
||||||
|
@ -65,7 +68,7 @@ void owl_fill_result(owl_result **result, char *csv)
|
||||||
" string (empty string?)!\n") ;
|
" string (empty string?)!\n") ;
|
||||||
goto error ;
|
goto error ;
|
||||||
}
|
}
|
||||||
(*result)->mobile_mac_addr =
|
result->mobile_mac_addr =
|
||||||
strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ;
|
strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ;
|
||||||
|
|
||||||
/* Request type */
|
/* Request type */
|
||||||
|
@ -76,7 +79,7 @@ void owl_fill_result(owl_result **result, char *csv)
|
||||||
"Error reading the request type from the CSV string!\n") ;
|
"Error reading the request type from the CSV string!\n") ;
|
||||||
goto error ;
|
goto error ;
|
||||||
}
|
}
|
||||||
(*result)->request_type = atoi(csv_field) ;
|
result->request_type = atoi(csv_field) ;
|
||||||
|
|
||||||
/* Timestamp */
|
/* Timestamp */
|
||||||
// Seconds
|
// Seconds
|
||||||
|
@ -88,7 +91,7 @@ void owl_fill_result(owl_result **result, char *csv)
|
||||||
" string!\n") ;
|
" string!\n") ;
|
||||||
goto error ;
|
goto error ;
|
||||||
}
|
}
|
||||||
(*result)->mobile_timestamp.tv_sec = atol(csv_field) ;
|
result->mobile_timestamp.tv_sec = atol(csv_field) ;
|
||||||
// Nanoseconds
|
// Nanoseconds
|
||||||
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
||||||
if (! csv_field)
|
if (! csv_field)
|
||||||
|
@ -98,7 +101,7 @@ void owl_fill_result(owl_result **result, char *csv)
|
||||||
" string!\n") ;
|
" string!\n") ;
|
||||||
goto error ;
|
goto error ;
|
||||||
}
|
}
|
||||||
(*result)->mobile_timestamp.tv_nsec = atol(csv_field) ;
|
result->mobile_timestamp.tv_nsec = atol(csv_field) ;
|
||||||
|
|
||||||
/* Algorithm results */
|
/* Algorithm results */
|
||||||
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
csv_field = strsep(&csv, CSV_DELIMITER) ;
|
||||||
|
@ -106,6 +109,7 @@ void owl_fill_result(owl_result **result, char *csv)
|
||||||
{
|
{
|
||||||
++nb_algorithms ;
|
++nb_algorithms ;
|
||||||
current_algo = malloc(sizeof(owl_algorithm_result)) ;
|
current_algo = malloc(sizeof(owl_algorithm_result)) ;
|
||||||
|
memset(current_algo, 0, sizeof(*current_algo)) ;
|
||||||
|
|
||||||
// Algorithm name
|
// Algorithm name
|
||||||
if (! csv_field)
|
if (! csv_field)
|
||||||
|
@ -168,17 +172,17 @@ void owl_fill_result(owl_result **result, char *csv)
|
||||||
current_algo->area = strndup(csv_field, OWL_AREA_STRLEN) ;
|
current_algo->area = strndup(csv_field, OWL_AREA_STRLEN) ;
|
||||||
|
|
||||||
// Insert the current algorithm at the begining of the list
|
// Insert the current algorithm at the begining of the list
|
||||||
current_algo->next = (*result)->results ;
|
current_algo->next = result->results ;
|
||||||
(*result)->results = current_algo ;
|
result->results = current_algo ;
|
||||||
}
|
}
|
||||||
while ((csv_field = strsep(&csv, CSV_DELIMITER))) ;
|
while ((csv_field = strsep(&csv, CSV_DELIMITER))) ;
|
||||||
|
|
||||||
return ; // Success
|
return result ; // Success
|
||||||
|
|
||||||
error:
|
error:
|
||||||
owl_free_result(*result) ;
|
owl_free_result(result) ;
|
||||||
*result = NULL ;
|
owl_free_algorithm_result(current_algo) ;
|
||||||
free(current_algo) ;
|
return NULL ;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@ -296,7 +300,9 @@ void owl_fprint_algorithm_result(FILE *stream,
|
||||||
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Frees the memory allocated by an owl_result.
|
* Frees the memory allocated by an owl_result. The 'results' and
|
||||||
|
* 'mobile_mac_addr' fields *must* be defined, either to NULL or to a
|
||||||
|
* valid memory block allocated with malloc().
|
||||||
* Note that the pointer will not set to NULL.
|
* Note that the pointer will not set to NULL.
|
||||||
*/
|
*/
|
||||||
void owl_free_result(owl_result *result)
|
void owl_free_result(owl_result *result)
|
||||||
|
@ -316,7 +322,8 @@ void owl_free_result(owl_result *result)
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Frees the memory allocated by a single owl_algorithm_result (*not*
|
* Frees the memory allocated by a single owl_algorithm_result (*not*
|
||||||
* recursively).
|
* recursively). The 'algorithm' and 'area' fields *must* be defined,
|
||||||
|
* either to NULL or to a valid memory block allocated with malloc().
|
||||||
* Note that the pointer will not set to NULL.
|
* Note that the pointer will not set to NULL.
|
||||||
*/
|
*/
|
||||||
void owl_free_algorithm_result(owl_algorithm_result *algo)
|
void owl_free_algorithm_result(owl_algorithm_result *algo)
|
||||||
|
|
|
@ -20,8 +20,7 @@ int main(void)
|
||||||
|
|
||||||
while (1)
|
while (1)
|
||||||
{
|
{
|
||||||
owl_receive_position(sockfd, &result) ;
|
if (! (result = owl_receive_position(sockfd)))
|
||||||
if (result == NULL)
|
|
||||||
return 1 ;
|
return 1 ;
|
||||||
owl_print_result(result) ;
|
owl_print_result(result) ;
|
||||||
owl_free_result(result) ;
|
owl_free_result(result) ;
|
||||||
|
|
|
@ -54,8 +54,8 @@ typedef struct _owl_result
|
||||||
} owl_result ;
|
} owl_result ;
|
||||||
|
|
||||||
|
|
||||||
void owl_receive_position(int sockfd, owl_result **result) ;
|
owl_result* owl_receive_position(int sockfd) ;
|
||||||
void owl_fill_result(owl_result **result, char *csv) ;
|
owl_result* owl_fill_result(char *csv) ;
|
||||||
|
|
||||||
void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],
|
void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN],
|
||||||
const owl_result *const src) ;
|
const owl_result *const src) ;
|
||||||
|
|
Loading…
Reference in New Issue