Dear Tony,
On Wed, Apr 24, 2013 at 10:49:16AM -0400, Tony Cheneau wrote:
> Add a "baudrate" parameter on the CLI that enables the user to choose
> from different baudrates. If no parameter is passed, the baudrate is set
> to 115200 (retaining the previous behavior).
>
> Signed-off-by: Tony Cheneau <[email protected]>
> ---
> src/serial.c | 103
> +++++++++++++++++++++++++++++++++++++++++++++++------------
> 1 file changed, 82 insertions(+), 21 deletions(-)
>
> diff --git a/src/serial.c b/src/serial.c
> index ab76aea..d42ea5b 100644
> --- a/src/serial.c
> +++ b/src/serial.c
> @@ -25,6 +25,7 @@
>
> #include <stdio.h>
> #include <string.h>
> +#include <stdlib.h>
>
> #include <sys/types.h>
> #include <sys/ioctl.h>
> @@ -32,33 +33,93 @@
> #include <termios.h>
> #include <unistd.h>
> #include <errno.h>
> +#include <getopt.h>
>
> #include "ieee802154.h"
>
> +#ifdef HAVE_GETOPT_LONG
> +static const struct option iz_long_opts[] = {
> + { "baudrate", optional_argument, NULL, 'b' },
> + { "help", no_argument, NULL, 'h' },
Maybe we should add "version" and 'v' here.
> + { NULL, 0, NULL, 0 },
> +};
> +#endif
> +
> +void serial_help(const char * pname) {
> + printf("Usage: %s [options] SERIAL_DEV\n", pname);
> + printf("Attach serial devices via UART to IEEE 802.15.4/ZigBee
> stack\n\n");
> + printf("Options:\n");
> + printf(" -b, --baudrate[=115200] set the baudrate\n");
> + printf(" -h, --help print help\n");
Same here.
> + printf(" SERIAL_DEV this specifies the serial
> device to attach.\n");
> + printf("Report bugs to " PACKAGE_BUGREPORT "\n\n");
> + printf(PACKAGE_NAME " homepage <" PACKAGE_URL ">\n");
> +}
> +
> +speed_t baudrate_to_speed(long baudrate) {
> + switch(baudrate) {
> + case 9600: return B9600;
> + case 19200: return B19200;
> + case 38400: return B38400;
> + case 57600: return B57600;
> + case 115200: return B115200;
> + case 230400: return B230400;
> + case 460800: return B460800;
> + case 921600: return B921600;
Intend here like linux kernel?
it's
switch(foo) {
case bar:
...
}
> + default:
> + printf("Unrecognized baudrate %ld\n", baudrate);
> + exit(EXIT_FAILURE);
> + }
> +}
> +
> int main(int argc, char **argv) {
> - int fd, ret, s;
> -
> - if (argc == 1 || !strcmp(argv[1], "--version")) {
> - printf( "izattach " VERSION "\n"
> - "Copyright (C) 2008, 2009 by Siemens AG\n"
> - "License GPLv2 GNU GPL version 2
> <http://gnu.org/licenses/gpl.html>.\n"
> - "This is free software: you are free to change and
> redistribute it.\n"
> - "There is NO WARRANTY, to the extent permitted by
> law.\n"
> - "\n"
> - "Written by Dmitry Eremin-Solenikov, Sergey Lapin and
> Maxim Osipov\n");
> - return 0;
> + int fd, ret, s, c;
> + long baudrate;
> + char * endptr;
> + speed_t speed = B115200;
> +
> + /* Parse options */
> + while (1) {
> +#ifdef HAVE_GETOPT_LONG
> + int opt_idx = -1;
> + c = getopt_long(argc, argv, "b:h", iz_long_opts, &opt_idx);
> +#else
> + c = getopt(argc, argv, "b:h");
> +#endif
> + if (c == -1)
> + break;
> +
> + switch(c) {
> + case 'h':
> + serial_help(argv[0]);
> + return 0;
> + case 'b':
> + baudrate = strtol(optarg, &endptr, 10);
> + if (* endptr == '\0')
> + speed = baudrate_to_speed(baudrate);
> + break;
> + case 'v':
> + printf( "izattach " VERSION "\n"
> + "Copyright (C) 2008, 2009 by Siemens AG\n"
> + "License GPLv2 GNU GPL version 2
> <http://gnu.org/licenses/gpl.html>.\n"
> + "This is free software: you are free to change
> and redistribute it.\n"
> + "There is NO WARRANTY, to the extent permitted
> by law.\n"
> + "\n"
> + "Written by Dmitry Eremin-Solenikov, Sergey
> Lapin and Maxim Osipov\n");
> + return 0;
> + default:
> + serial_help(argv[0]);
> + return 1;
> + }
> }
>
> - if (argc != 2 || (argc >= 1 && !strcmp(argv[1], "--help"))) {
> - printf("Usage: %s SERIAL_DEV\n", argv[0]);
> - printf("Attach serial devices via UART to IEEE 802.15.4/ZigBee
> stack\n\n");
> - printf(" SERIAL_DEV This specifies the serial device to
> attach.\n");
> - printf("Report bugs to " PACKAGE_BUGREPORT "\n\n");
> - printf(PACKAGE_NAME " homepage <" PACKAGE_URL ">\n");
> - return 1;
> + if (argc <= optind) {
> + printf("SERIAL_DEV argument is missing\n\n");
> + serial_help(argv[0]);
> + return 2;
Maybe we should only use return 1 for error and 0 for success. Maybe we should
use the
standard EXIT_FAILURE and EXIT_SUCCESS defines for that.
Regards
Alex
------------------------------------------------------------------------------
Try New Relic Now & We'll Send You this Cool Shirt
New Relic is the only SaaS-based application performance monitoring service
that delivers powerful full stack analytics. Optimize and monitor your
browser, app, & servers with just a few lines of code. Try New Relic
and get this awesome Nerd Life shirt! http://p.sf.net/sfu/newrelic_d2d_apr
_______________________________________________
Linux-zigbee-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-zigbee-devel