options.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /*
  2. * Copyright (C) 2000-2004 by ETH Zurich
  3. *
  4. * Redistribution and use in source and binary forms, with or without
  5. * modification, are permitted provided that the following conditions
  6. * are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice, this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright
  11. * notice, this list of conditions and the following disclaimer in the
  12. * documentation and/or other materials provided with the distribution.
  13. * 3. Neither the name of the copyright holders nor the names of
  14. * contributors may be used to endorse or promote products derived
  15. * from this software without specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY ETH ZURICH AND CONTRIBUTORS
  18. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  19. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  20. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL ETH ZURICH
  21. * OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  22. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  23. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  24. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  25. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  26. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  27. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  28. * SUCH DAMAGE.
  29. *
  30. * For additional information see http://www.ethernut.de/
  31. *
  32. */
  33. /*
  34. * unix_options.c - parsing of command line options
  35. *
  36. * 2002.11.13 Oliver Kasten <oliver.kasten@inf.ethz.ch>
  37. * 2003.06.09 Jan Beutel <j.beutel@ieee.org>
  38. * 2004.04.01 Matthias Ringwald <matthias.ringwald@inf.ethz.ch>
  39. *
  40. */
  41. /**
  42. \file arch/unix/os/options.c
  43. \brief Parsing of command line options. NOTE: Unix emulation only!
  44. */
  45. #include <compiler.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <unistd.h>
  50. #include <getopt.h>
  51. /* the command line options are stored here */
  52. emulation_options_t emulation_options;
  53. // default options
  54. static uart_options_t uart_0_opt = { "stdio", 0, 0, -1 };
  55. static uart_options_t uart_1_opt = { "stdio", 0, 0, -1 };
  56. static uart_options_t uart_2_opt = { "stdio", 0, 0, -1 };
  57. static void emulation_options_usage(void);
  58. static void emulation_options_print_all(void);
  59. static void emulation_options_print_uart(int);
  60. /* -------------------------------------------------------------------------
  61. * Parse command line parameters
  62. * syntax: -uN (M|hci:O) -h -v
  63. * N: uart nr (0/1)
  64. * M: unix device name
  65. * O: usb bluetooth number: 0..
  66. * ------------------------------------------------------------------------- */
  67. void emulation_options_parse(int argc, char *argv[])
  68. {
  69. int opt;
  70. // set default values
  71. emulation_options.verbose = 0;
  72. emulation_options.uart_options[0] = uart_0_opt;
  73. emulation_options.uart_options[1] = uart_1_opt;
  74. emulation_options.uart_options[2] = uart_2_opt;
  75. // parse args
  76. while ((opt = getopt(argc, argv, "u:hv::")) != -1) {
  77. switch (opt) {
  78. case 'u':
  79. {
  80. int devno = 0, bautrate = 0, flowctrl = 0;
  81. char *device = NULL;
  82. devno = atoi(optarg);
  83. // printf( "Parsing options for UART %i\n", devno );
  84. if ((devno < 0) || (devno > 2)) {
  85. printf("ERROR parsing arguments for UART device: " "device number out of range [0..2]\n");
  86. exit(1);
  87. }
  88. device = strdup(argv[optind++]);
  89. // printf( " device: %s\n", device );
  90. // terminal stdio?
  91. if (strcmp(device, "stdio") == 0) {
  92. // usb device ? hci:x
  93. } else if (strncmp(device, "hci", 3) == 0) {
  94. emulation_options.uart_options[devno].usbnum = atoi(device + 3);
  95. } else {
  96. // tcp/ip ? host:port
  97. // tty
  98. }
  99. emulation_options.uart_options[devno].device = device;
  100. emulation_options.uart_options[devno].bautrate = bautrate;
  101. emulation_options.uart_options[devno].flowcontrol = flowctrl;
  102. }
  103. break;
  104. case 'v':
  105. // verbose mode: -v [on|off]
  106. if (optarg) {
  107. if (strcmp(argv[optind], "off") == 0)
  108. emulation_options.verbose = 0;
  109. else if (strcmp(argv[optind], "on") == 0)
  110. emulation_options.verbose = 1;
  111. else {
  112. printf("ERROR parsing verboseness (-v [on|off])option.\n");
  113. exit(1);
  114. }
  115. } else {
  116. // -v without options turnes verbose mode on
  117. emulation_options.verbose = 1;
  118. }
  119. break;
  120. case 'h':
  121. emulation_options_usage();
  122. exit(0);
  123. break;
  124. default:
  125. printf("unrecognized option:\n");
  126. emulation_options_usage();
  127. exit(1);
  128. break;
  129. }
  130. }
  131. if (emulation_options.verbose)
  132. emulation_options_print_all();
  133. }
  134. void emulation_options_print_all()
  135. {
  136. int i = 0;
  137. if (!emulation_options.verbose)
  138. // not in verbose mode
  139. return;
  140. printf("OPTIONS SUMMARY:\n");
  141. printf(" verbose: %i\n", emulation_options.verbose);
  142. for (i = 0; i < 3; i++) {
  143. emulation_options_print_uart(i);
  144. }
  145. } // end _btn_options_print()
  146. void emulation_options_print_uart(int no)
  147. {
  148. uart_options_t uart_opt = emulation_options.uart_options[no];
  149. printf("Options for UART %i\n", no);
  150. printf(" device: %s\n", uart_opt.device);
  151. if (uart_opt.usbnum >= 0) {
  152. printf(" hci num : %i\n", (int) uart_opt.usbnum);
  153. } else {
  154. printf(" bautrate: %i\n", (int) uart_opt.bautrate);
  155. printf(" flowcontrol: %i\n", (int) uart_opt.flowcontrol);
  156. }
  157. } // end _btn_uart_options_print()
  158. static void emulation_options_usage()
  159. {
  160. printf("To properly run an emulated Nut/OS app, some hardware might need to be connected.\n"
  161. "With the following options you can specify how the uarts are mapped.\n");
  162. printf(" -h prints this help.\n");
  163. printf(" -v verbose mode. print the options parsed.\n");
  164. printf(" -u<N> DEVICE \n");
  165. printf(" map uart N to unix DEVICE \n");
  166. printf(" example: ./prog -u0 /dev/ttyS0 \n");
  167. printf(" -u<N> HOST:PORT \n");
  168. printf(" map uart N to TCP/IP socket with HOST at PORT \n");
  169. printf(" example: ./prog -u0 localhost:7007 \n");
  170. // printf(" -u<N> hci<M> \n");
  171. // printf(" map uart N to the USB BT dongle nr. M\n");
  172. // printf(" example: ./prog -u0 hci0\n");
  173. printf(" \n");
  174. printf(" \n");
  175. }