options.c
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
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
00060 emulation_options_t emulation_options;
00061
00062
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
00073
00074
00075
00076
00077
00078
00079 void emulation_options_parse(int argc, char *argv[])
00080 {
00081
00082 int opt;
00083
00084
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
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
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
00111
00112
00113 if (strcmp(device, "stdio") == 0) {
00114
00115
00116 } else if (strncmp(device, "hci", 3) == 0) {
00117 emulation_options.uart_options[devno].usbnum = atoi(device + 3);
00118 } else {
00119
00120
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
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
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
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 }
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 }
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
00214
00215
00216
00217 printf(" \n");
00218 printf(" \n");
00219
00220 }