options.c

Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2000-2004 by ETH Zurich
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions
00006  * are met:
00007  *
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the copyright holders nor the names of
00014  *    contributors may be used to endorse or promote products derived
00015  *    from this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
00018  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00019  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00020  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
00021  *  OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00022  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00023  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
00024  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
00025  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00026  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
00027  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00028  * SUCH DAMAGE.
00029  *
00030  * For additional information see http://www.ethernut.de/
00031  *
00032  */
00033 
00034 /*
00035  * unix_options.c - parsing of command line options
00036  *
00037  * 2002.11.13 Oliver Kasten <oliver.kasten@inf.ethz.ch>
00038  * 2003.06.09 Jan Beutel <j.beutel@ieee.org> 
00039  * 2004.04.01 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
00040  *
00041  */
00042 
00051 #include <compiler.h>
00052 
00053 #include <stdio.h>
00054 #include <stdlib.h>
00055 #include <string.h>
00056 #include <unistd.h>
00057 #include <getopt.h>
00058 
00059 /* the command line options are stored here */
00060 emulation_options_t emulation_options;
00061 
00062 // default options
00063 static uart_options_t uart_0_opt = { "stdio", 0, 0, -1 };
00064 static uart_options_t uart_1_opt = { "stdio", 0, 0, -1 };
00065 static uart_options_t uart_2_opt = { "stdio", 0, 0, -1 };
00066 
00067 static void emulation_options_usage(void);
00068 static void emulation_options_print_all(void);
00069 static void emulation_options_print_uart(int);
00070 
00071 /* -------------------------------------------------------------------------
00072  * Parse command line parameters
00073  * syntax: -uN (M|hci:O) -h -v
00074  *  N: uart nr (0/1)
00075  *  M: unix device name
00076  *  O: usb bluetooth number: 0..
00077  * ------------------------------------------------------------------------- */
00078 
00079 void emulation_options_parse(int argc, char *argv[])
00080 {
00081 
00082     int opt;
00083 
00084     // set default values
00085     emulation_options.verbose = 0;
00086     emulation_options.uart_options[0] = uart_0_opt;
00087     emulation_options.uart_options[1] = uart_1_opt;
00088     emulation_options.uart_options[2] = uart_2_opt;
00089 
00090     // parse args 
00091     while ((opt = getopt(argc, argv, "u:hv::")) != -1) {
00092 
00093         switch (opt) {
00094 
00095         case 'u':
00096             {
00097 
00098                 int devno = 0, bautrate = 0, flowctrl = 0;
00099                 char *device = NULL;
00100 
00101                 devno = atoi(optarg);
00102                 // printf( "Parsing options for UART %i\n", devno );
00103 
00104                 if ((devno < 0) || (devno > 2)) {
00105                     printf("ERROR parsing arguments for UART device: " "device number out of range [0..2]\n");
00106                     exit(1);
00107                 }
00108 
00109                 device = strdup(argv[optind++]);
00110                 // printf( "  device: %s\n", device );
00111 
00112                 // terminal stdio?
00113                 if (strcmp(device, "stdio") == 0) {
00114 
00115                 // usb device ? hci:x
00116                 } else if (strncmp(device, "hci", 3) == 0) {
00117                     emulation_options.uart_options[devno].usbnum = atoi(device + 3);
00118                 } else {
00119                     // tcp/ip ? host:port
00120                     // tty
00121 
00122                 }
00123                 emulation_options.uart_options[devno].device = device;
00124                 emulation_options.uart_options[devno].bautrate = bautrate;
00125                 emulation_options.uart_options[devno].flowcontrol = flowctrl;
00126 
00127             }
00128             break;
00129 
00130         case 'v':
00131             // verbose mode: -v [on|off]
00132 
00133             if (optarg) {
00134                 if (strcmp(argv[optind], "off") == 0)
00135                     emulation_options.verbose = 0;
00136                 else if (strcmp(argv[optind], "on") == 0)
00137                     emulation_options.verbose = 1;
00138                 else {
00139                     printf("ERROR parsing verboseness (-v [on|off])option.\n");
00140                     exit(1);
00141                 }
00142             } else {
00143                 // -v without options turnes verbose mode on
00144                 emulation_options.verbose = 1;
00145             }
00146             break;
00147 
00148         case 'h':
00149             emulation_options_usage();
00150             exit(0);
00151             break;
00152 
00153         default:
00154             printf("unrecognized option:\n");
00155             emulation_options_usage();
00156             exit(1);
00157 
00158             break;
00159         }
00160     }
00161 
00162     if (emulation_options.verbose)
00163         emulation_options_print_all();
00164 }
00165 
00166 void emulation_options_print_all()
00167 {
00168 
00169     int i = 0;
00170 
00171     if (!emulation_options.verbose)
00172         // not in verbose mode
00173         return;
00174 
00175     printf("OPTIONS SUMMARY:\n");
00176 
00177     printf("  verbose: %i\n", emulation_options.verbose);
00178 
00179     for (i = 0; i < 3; i++) {
00180         emulation_options_print_uart(i);
00181     }
00182 
00183 }                               // end _btn_options_print()
00184 
00185 void emulation_options_print_uart(int no)
00186 {
00187 
00188     uart_options_t uart_opt = emulation_options.uart_options[no];
00189 
00190     printf("Options for UART %i\n", no);
00191 
00192     printf("  device: %s\n", uart_opt.device);
00193     if (uart_opt.usbnum >= 0) {
00194         printf("  hci num : %i\n", (int) uart_opt.usbnum);
00195     } else {
00196         printf("  bautrate: %i\n", (int) uart_opt.bautrate);
00197         printf("  flowcontrol: %i\n", (int) uart_opt.flowcontrol);
00198     }
00199 }                               // end _btn_uart_options_print()
00200 
00201 static void emulation_options_usage()
00202 {
00203     printf("To properly run an emulated Nut/OS app, some hardware might need to be connected.\n"
00204            "With the following options you can specify how the uarts are mapped.\n");
00205     printf("  -h  prints this help.\n");
00206     printf("  -v  verbose mode. print the options parsed.\n");
00207     printf("  -u<N> DEVICE \n");
00208     printf("     map uart N to unix DEVICE \n");
00209     printf("     example: ./prog -u0 /dev/ttyS0 \n");
00210     printf("  -u<N> HOST:PORT \n");
00211     printf("     map uart N to TCP/IP socket with HOST at PORT \n");
00212     printf("     example: ./prog -u0 localhost:7007 \n");
00213 //    printf("  -u<N> hci<M> \n");
00214 //    printf("     map uart N to the USB BT dongle nr. M\n");
00215 //    printf("     example: ./prog -u0 hci0\n");
00216 
00217     printf("  \n");
00218     printf("  \n");
00219 
00220 }

© 2000-2007 by egnite Software GmbH - visit http://www.ethernut.de/