#include "owlps-resultreader-udp.h" #include #include #include #include #define CSV_DELIMITER ";" /* * Receives a result from the socket of file descriptor 'sockfd', fills * an owl_result structure with the received information, 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(). */ owl_result* owl_receive_position(int sockfd) { ssize_t nread ; // recvfrom return value char csv[OWL_CSV_RESULT_STRLEN] ; // Read string nread = recvfrom(sockfd, &csv, OWL_CSV_RESULT_STRLEN, 0, NULL, NULL) ; if (nread <= 0) { perror("No request received from listener") ; return NULL ; } return owl_fill_result(csv) ; } /* * 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: * Mobile_MAC;Request_type;Request_timestamp;Algorithm;X;Y;Z;Error;Area * The Request_timestamp format is: * seconds.nanoseconds * The Area field can be empty. */ owl_result* owl_fill_result(char *csv) { char *csv_field = NULL ; owl_result *result = NULL ; int nb_algorithms = 0 ; result = malloc(sizeof(owl_result)) ; memset(result, 0, sizeof(*result)) ; /* Mobile MAC address */ csv_field = strsep(&csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the mobile's MAC address from the CSV" " string (empty string?)!\n") ; goto error ; } result->mobile_mac_addr = strndup(csv_field, OWL_ETHER_ADDR_STRLEN) ; /* Request type */ csv_field = strsep(&csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the request type from the CSV string!\n") ; goto error ; } result->request_type = atoi(csv_field) ; /* Timestamp */ // Seconds csv_field = strsep(&csv, ".") ; if (! csv_field) { fprintf(stderr, "Error reading the timestamp (seconds) from the CSV" " string!\n") ; goto error ; } result->mobile_timestamp.tv_sec = atol(csv_field) ; // Nanoseconds csv_field = strsep(&csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the timestamp (nanoseconds) from the CSV" " string!\n") ; goto error ; } result->mobile_timestamp.tv_nsec = atol(csv_field) ; /* Algorithm results */ do { owl_algorithm_result *current_algo = owl_fill_algorithm_result(&csv) ; if (current_algo == NULL) { fprintf(stderr, "Error reading the algorithm #%d!\n", nb_algorithms) ; break ; } // Insert the current algorithm at the begining of the list current_algo->next = result->results ; result->results = current_algo ; ++nb_algorithms ; } while (csv) ; return result ; // Success error: owl_free_result(result) ; return NULL ; } /* * Splits the 'csv' string, stores the fields in a new * owl_algorithm_result, and returns a pointer to it (or NULL * in case of error). * * Note that the new owl_algorithm_result is allocated with malloc() * and must be deleted using free(). * * 'csv' must follow this CSV format: * Algorithm;X;Y;Z;Error;Area * The Area field can be empty. */ owl_algorithm_result* owl_fill_algorithm_result(char **csv) { owl_algorithm_result *algo ; char *csv_field = NULL ; algo = malloc(sizeof(owl_algorithm_result)) ; memset(algo, 0, sizeof(*algo)) ; // Algorithm name csv_field = strsep(csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the algorithm name from the CSV string!\n") ; goto error ; } algo->algorithm = strndup(csv_field, OWL_ALGORITHM_STRLEN) ; // X coordinate csv_field = strsep(csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the X coordinate from the CSV string!\n") ; goto error ; } algo->x = atof(csv_field) ; // Y coordinate csv_field = strsep(csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the Y coordinate from the CSV string!\n") ; goto error ; } algo->y = atof(csv_field) ; // Z coordinate csv_field = strsep(csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the Z coordinate from the CSV string!\n") ; goto error ; } algo->z = atof(csv_field) ; // Distance error csv_field = strsep(csv, CSV_DELIMITER) ; if (! csv_field) { fprintf(stderr, "Error reading the distance error from the CSV string!\n") ; goto error ; } algo->error = atof(csv_field) ; // Area name (optional) csv_field = strsep(csv, CSV_DELIMITER) ; if (csv_field) algo->area = strndup(csv_field, OWL_AREA_STRLEN) ; return algo ; // Success error: owl_free_algorithm_result(algo) ; return NULL ; } /* * Converts an owl_result back to a CSV string. * 'dst' must be an allocated string of at least OWL_CSV_RESULT_STRLEN * characters. */ void owl_result_to_csv(char dst[OWL_CSV_RESULT_STRLEN], const owl_result *const src) { size_t dst_len ; char timestamp_str[OWL_TIMESTAMP_STRLEN] ; assert(src) ; owl_timestamp_to_string(timestamp_str, src->mobile_timestamp) ; snprintf(dst, OWL_CSV_RESULT_REQUEST_STRLEN, "%s;%"PRIu8";%s", src->mobile_mac_addr, src->request_type, timestamp_str) ; dst_len = strlen(dst) ; owl_algorithm_result *algo = src->results ; while (algo) { char algo_str[OWL_CSV_ALGORITHM_RESULT_STRLEN] ; owl_algorithm_result_to_csv(algo_str, algo) ; dst[dst_len++] = ';' ; strncpy(dst + dst_len, algo_str, OWL_CSV_ALGORITHM_RESULT_STRLEN) ; dst_len += strlen(algo_str) ; algo = algo->next ; } } /* * Converts an owl_algorithm_result back to a CSV string. * 'dst' must be an allocated string of at least * OWL_CSV_ALGORITHM_RESULT_STRLEN characters. */ void owl_algorithm_result_to_csv(char dst[OWL_CSV_ALGORITHM_RESULT_STRLEN], const owl_algorithm_result *const src) { assert(src) ; snprintf(dst, OWL_CSV_ALGORITHM_RESULT_STRLEN, "%s;%f;%f;%f;%f;%s", src->algorithm, src->x, src->y, src->z, src->error, src->area ? src->area : "") ; } /* * Prints an owl_result to the given stream. */ void owl_fprint_result(FILE *stream, const owl_result *const src) { char timestamp_str[OWL_TIMESTAMP_STRLEN] ; assert(src) ; owl_timestamp_to_string(timestamp_str, src->mobile_timestamp) ; fprintf(stream, "Mobile MAC: %s\n" "Request type: %"PRIu8"\n" "Mobile timestamp: %s\n" "Results:\n" , src->mobile_mac_addr, src->request_type, timestamp_str ) ; owl_algorithm_result *algo = src->results ; while (algo) { owl_fprint_algorithm_result(stream, algo) ; algo = algo->next ; } } /* * Prints an owl_algorithm_result to the given stream. */ void owl_fprint_algorithm_result(FILE *stream, const owl_algorithm_result *const src) { assert(src) ; fprintf(stream, "* Algorithm: %s\n" " X: %f\n" " Y: %f\n" " Z: %f\n" " Error: %f\n" " Area: %s\n" , src->algorithm, src->x, src->y, src->z, src->error, src->area ? src->area : "" ) ; } /* * 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. */ void owl_free_result(owl_result *result) { if (! result) return ; while (result->results) { owl_algorithm_result *algo = result->results ; result->results = algo->next ; owl_free_algorithm_result(algo) ; } free(result->mobile_mac_addr) ; free(result) ; } /* * Frees the memory allocated by a single owl_algorithm_result (*not* * 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. */ void owl_free_algorithm_result(owl_algorithm_result *algo) { if (! algo) return ; free(algo->algorithm) ; free(algo->area) ; free(algo) ; }