From b859734d234d07351caf3715c24cdff98c17cafe Mon Sep 17 00:00:00 2001 From: Matteo Cypriani Date: Fri, 7 Jun 2013 16:43:11 -0400 Subject: [PATCH] [Client] Add verbose and quiet options (-v / -q) --- TODO.t2t | 2 - doc/owlps-client.t2t | 17 +++++--- owlps-client/owlps-client.c | 87 +++++++++++++++++++------------------ 3 files changed, 56 insertions(+), 50 deletions(-) diff --git a/TODO.t2t b/TODO.t2t index b4f4205..5397b09 100644 --- a/TODO.t2t +++ b/TODO.t2t @@ -170,8 +170,6 @@ Work to do in OwlPS = Client = -- Add verbose & quiet options. - - Log sent requests? - Allow to use a string for the direction? diff --git a/doc/owlps-client.t2t b/doc/owlps-client.t2t index ef0f99a..e8ce622 100644 --- a/doc/owlps-client.t2t +++ b/doc/owlps-client.t2t @@ -20,13 +20,13 @@ OwlPS {{OWLPS_VERSION}} = Synopsis = -**owlps-client** **-i** dest_ip [ **-p** //dest_port// ] [ **-I** //iface// ] [ **-t** //delay// ] - [ **-n** //nb_packets// ] [ **-s** //packet_size// ] [ **-F** [ //delay// ] - [ **-N** //nb_requests// ] [ **-D** ] ] [ **-l** [ //port// ] ] +**owlps-client** [ **-v** | **-q** ] **-i** dest_ip [ **-p** //dest_port// ] [ **-I** //iface// ] + [ **-t** //delay// ] [ **-n** //nb_packets// ] [ **-s** //packet_size// ] + [ **-F** [ //delay// ] [ **-N** //nb_requests// ] [ **-D** ] ] [ **-l** [ //port// ] ] -**owlps-client** **-i** dest_ip [ **-p** //dest_port// ] [ **-I** //iface// ] [ **-t** //delay// ] - [ **-n** //nb_packets// ] [ **-s** //packet_size// ] [ **-F** [ //delay// ] - [ **-N** //nb_requests// ] [ **-D** ] ] //direction x y z// +**owlps-client** [ **-v** | **-q** ] **-i** dest_ip [ **-p** //dest_port// ] [ **-I** //iface// ] + [ **-t** //delay// ] [ **-n** //nb_packets// ] [ **-s** //packet_size// ] + [ **-F** [ //delay// ] [ **-N** //nb_requests// ] [ **-D** ] ] //direction x y z// @@ -96,6 +96,11 @@ Where: Print help message. : **-V** Print version information. +: **-v** + Turn on verbose mode (default). +: **-q** + Do not print informational messages and some (less important) + warnings. : **-i** //dest_ip// Destination IP address of the localisation request. : **-p** //dest_port// diff --git a/owlps-client/owlps-client.c b/owlps-client/owlps-client.c index 8f4a22d..3d0c66a 100644 --- a/owlps-client/owlps-client.c +++ b/owlps-client/owlps-client.c @@ -50,7 +50,7 @@ #define MAX_PKT_SIZE 1450u /* Program arguments (getopt string) */ -#define OPTIONS "DF::hi:I:l::n:N:p:s:t:V" +#define OPTIONS "DF::hi:I:l::n:N:p:qs:t:vV" /* Function headers */ @@ -59,9 +59,7 @@ void parse_main_options(int argc, char **argv) ; void check_destination_ip(void) ; void parse_calibration_data(int argc, char **argv) ; void check_configuration(void) ; -#ifdef OWLPS_DEBUG void print_configuration(void) ; -#endif // OWLPS_DEBUG void create_socket(void) ; void send_request(void) ; void make_packet(void) ; @@ -78,6 +76,7 @@ void print_version(void) ; /* Options */ struct { bool daemon ; + bool verbose ; char dest_ip[INET_ADDRSTRLEN] ; // Destination IP of the packets uint_fast16_t dest_port ; char iface[IFNAMSIZ + 1] ; // Source network interface @@ -94,6 +93,7 @@ struct { float z ; } options = { false, // daemon + true, // verbose "", // dest_ip OWL_DEFAULT_REQUEST_PORT, // dest_port "", // iface @@ -131,7 +131,8 @@ int main(int argc, char *argv[]) if (options.daemon) { - fprintf(stderr, "Detaching to background...\n") ; + if (options.verbose) + fprintf(stderr, "Detaching to background...\n") ; if (daemon(0, 0)) perror("Cannot daemonize") ; } @@ -179,9 +180,8 @@ void parse_command_line(int argc, char **argv) parse_calibration_data(argc, argv) ; check_configuration() ; -#ifdef OWLPS_DEBUG - print_configuration() ; -#endif // OWLPS_DEBUG + if (options.verbose) + print_configuration() ; } @@ -265,12 +265,18 @@ void parse_main_options(int argc, char **argv) case 'p' : options.dest_port = strtoul(optarg, NULL, 0) ; break ; + case 'q' : + options.verbose = false ; + break ; case 's' : options.pkt_size = strtoul(optarg, NULL, 0) ; break ; case 't' : options.delay = strtol(optarg, NULL, 0) ; break ; + case 'v' : + options.verbose = true ; + break ; case 'V' : print_version() ; exit(0) ; @@ -325,10 +331,9 @@ void check_configuration() // Delay not specified (or bad delay): if (options.delay < 0) { -#ifdef OWLPS_DEBUG - fprintf(stderr, - "Warning! delay: failing back to default value.\n") ; -#endif // OWLPS_DEBUG + if (options.verbose) + fprintf(stderr, + "Warning! delay: failing back to default value.\n") ; if (is_calibration_request) options.delay = DEFAULT_DELAY_CALIB ; else @@ -338,10 +343,9 @@ void check_configuration() // Number of packet not specified (or bad number) if (options.nb_pkt < 1) { -#ifdef OWLPS_DEBUG - fprintf(stderr, - "Warning! nb_pkt: failing back to default value.\n") ; -#endif // OWLPS_DEBUG + if (options.verbose) + fprintf(stderr, + "Warning! nb_pkt: failing back to default value.\n") ; if (is_calibration_request) options.nb_pkt = DEFAULT_NBPKT_CALIB ; else @@ -351,11 +355,10 @@ void check_configuration() // Packet size too big if (options.pkt_size > MAX_PKT_SIZE) { -#ifdef OWLPS_DEBUG - fprintf(stderr, - "Warning! pkt_size cannot be greater than %d bytes:" - " failing back to %d.\n", MAX_PKT_SIZE, MAX_PKT_SIZE) ; -#endif // OWLPS_DEBUG + if (options.verbose) + fprintf(stderr, + "Warning! pkt_size cannot be greater than %d bytes:" + " failing back to %d.\n", MAX_PKT_SIZE, MAX_PKT_SIZE) ; options.pkt_size = MAX_PKT_SIZE ; } @@ -372,28 +375,22 @@ void check_configuration() // Check port numbers if (options.dest_port < 1 || options.dest_port > 65535) { -#ifdef OWLPS_DEBUG fprintf(stderr, "Warning! Bad dest_port:" " failing back to default value.\n") ; options.dest_port = OWL_DEFAULT_REQUEST_PORT ; -#endif // OWLPS_DEBUG } if (options.listening_port > 65535) { -#ifdef OWLPS_DEBUG fprintf(stderr, "Warning! listening_port too high: ignored.\n") ; options.listening_port = 0 ; -#endif // OWLPS_DEBUG } // We want to send a calibration request AND to be located, which is // not allowed: if (is_calibration_request && options.listening_port > 0) { -#ifdef OWLPS_DEBUG fprintf(stderr, "Warning! You cannot wait for a server answer when" " you calibrate. Option -l ignored.\n") ; -#endif // OWLPS_DEBUG options.listening_port = 0 ; } @@ -402,10 +399,8 @@ void check_configuration() // We want to flood AND to be located, which is not allowed: if (options.listening_port > 0) { -#ifdef OWLPS_DEBUG fprintf(stderr, "Warning! You cannot wait for a server answer" " when you flood. Option -l ignored.\n") ; -#endif // OWLPS_DEBUG options.listening_port = 0 ; } } @@ -413,19 +408,15 @@ void check_configuration() { if (options.nb_requests) { -#ifdef OWLPS_DEBUG fprintf(stderr, "Warning! The -N option can be used only along" " with -F. Option -N ignored.\n") ; -#endif // OWLPS_DEBUG options.nb_requests = 0 ; } if (options.daemon) { -#ifdef OWLPS_DEBUG fprintf(stderr, "Warning! It is useless to detach from" " the foreground if the flood mode is not activated" " Option -D ignored.\n") ; -#endif // OWLPS_DEBUG options.daemon = false ; } } @@ -433,7 +424,6 @@ void check_configuration() -#ifdef OWLPS_DEBUG void print_configuration() { fprintf(stderr, "Options:\n" @@ -464,7 +454,6 @@ void print_configuration() options.z ) ; } -#endif // OWLPS_DEBUG @@ -499,7 +488,8 @@ void make_packet() if (is_calibration_request) // Calibration packet { - printf("Preparing calibration request packet...\n") ; + if (options.verbose) + printf("Preparing calibration request packet...\n") ; packet_size = sizeof(uint8_t) * 2 + sizeof(owl_timestamp) + sizeof(float) * 3 + @@ -518,7 +508,8 @@ void make_packet() else // Standard packet { - printf("Preparing request packet...\n") ; + if (options.verbose) + printf("Preparing request packet...\n") ; packet_size = sizeof(uint8_t) + sizeof(owl_timestamp) + sizeof(uint16_t) * 2 ; @@ -548,7 +539,8 @@ void add_padding() if (options.pkt_size > packet_size) packet_size = options.pkt_size ; - printf("Packet size: %"PRIuFAST16"\n", packet_size) ; + if (options.verbose) + printf("Packet size: %"PRIuFAST16"\n", packet_size) ; } @@ -586,7 +578,8 @@ uint_fast16_t initialise_common_fields(uint_fast8_t packet_type) memcpy(&packet[offset], &request_time, sizeof(request_time)) ; offset += sizeof(request_time) ; - printf("Packet timestamp: %s\n", request_time_str) ; + if (options.verbose) + printf("Packet timestamp: %s\n", request_time_str) ; return offset ; } @@ -603,10 +596,9 @@ uint_fast16_t initialise_calibration_fields(uint_fast16_t offset) // Direction: packet[offset++] = options.direction ; -#ifdef OWLPS_DEBUG - printf("Direction = %d, X = %f, Y = %f, Z = %f\n", - packet[offset - 1], options.x, options.y, options.z) ; -#endif // OWLPS_DEBUG + if (options.verbose) + printf("Direction = %d, X = %f, Y = %f, Z = %f\n", + packet[offset - 1], options.x, options.y, options.z) ; // Convert the coordinates to the network endianness: x = owl_htonf(options.x) ; @@ -657,11 +649,18 @@ int receive_position() +/* + * Prints the usage message on the standard output. + * + * /!\ Don't forget to update the documentation when modifying something + * here! + */ void print_usage() { printf("Usage:\n" "Localisation request:\n" "\t%s" + " [-v | -q]" " -i dest_ip" " [-p dest_port]" " [-I iface]" @@ -674,6 +673,7 @@ void print_usage() " [-l [port]]\n" "Calibration request:\n" "\t%s" + " [-v | -q]" " -i dest_ip" " [-p dest_port]" " [-I iface]" @@ -688,6 +688,9 @@ void print_usage() "Options:\n" "\t-h\t\tPrint this help.\n" "\t-V\t\tPrint version information.\n" + "\t-v\t\tTurn on verbose mode (default).\n" + "\t-q\t\tDo not print informational messages and some (less\n" + "\t\t\timportant) warnings.\n" "\t-i dest_ip\tDestination IP address of the localisation" " request.\n" "\t-p dest_port\tDestination port of the localisation request"