XCP_SIM
options.c
1 /*
2  * BlueParrot XCP
3  *
4  * (C) 2021 by Christoph Schueler <github.com/Christoph2,
5  * cpu12.gems@googlemail.com>
6  *
7  * All Rights Reserved
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License along
20  * with this program; if not, write to the Free Software Foundation, Inc.,
21  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  *
23  * s. FLOSS-EXCEPTION.txt
24  */
25 
26 #include <stdlib.h>
27 #include <stdio.h>
28 
30 #include "xcp.h"
33 #if defined(_WIN32)
34 
35 #else
36 #include <string.h>
37 #include <unistd.h>
38 
39 #if XCP_TRANSPORT_LAYER == XCP_ON_ETHERNET
40 static const char OPTION_STR[] = "htu46p:";
41 #elif XCP_TRANSPORT_LAYER == XCP_ON_CAN
42 static const char OPTION_STR[] = "hi:f";
43 #endif
44 
45 #endif
46 
47 #if defined(_WIN32)
48 void parse_options(int argc, char** argv, Xcp_OptionsType* options) {
49  int idx;
50  char* arg;
51 #if XCP_TRANSPORT_LAYER == XCP_ON_ETHERNET
52  options->ipv6 = XCP_FALSE;
53  options->tcp = XCP_TRUE;
54 
55  if (argc >= 2) {
56  for (idx = 1; idx < argc; ++idx) {
57  arg = argv[idx];
58  if ((arg[0] != '/') && (arg[0] != '-')) {
59  continue;
60  }
61  switch (arg[1]) {
62  case '4':
63  options->ipv6 = XCP_FALSE;
64  break;
65  case '6':
66  options->ipv6 = XCP_TRUE;
67  break;
68  case 'u':
69  options->tcp = XCP_FALSE;
70  break;
71  case 't':
72  options->tcp = XCP_TRUE;
73  break;
74  case 'h':
75  break;
76  default:
77  break;
78  }
79  }
80  }
81 #elif defined(TP_BLUETOOTH)
82 
83 #else /* defined(KVASER_CAN)*/
84 
85 #endif
86 }
87 #else
88 
89 void usage(void) {
90  printf("\nparameter summary: \n");
91 #if XCP_TRANSPORT_LAYER == XCP_ON_ETHERNET
92  printf("-h\t this message.\n");
93  printf("-t\t TCP\t\t default: TRUE\n");
94  printf("-u\t UDP\t\t default: FALSE\n");
95  printf("-4\t IPv4\t\t default: TRUE\n");
96  printf("-6\t IPv6\t\t default: FALSE\n");
97  printf("-p <port> port to listen default: 5555\n");
98 #elif XCP_TRANSPORT_LAYER == XCP_ON_CAN
99  printf("-h\tthis message.\n");
100  printf("-f\t\tuse CAN-FD\t\tdefault: FALSE\n");
101  printf("-i <if-name>\tinterface to use\tdefault: vcan0\n");
102 #endif
103  exit(0);
104 }
105 
106 void parse_options(int argc, char** argv, Xcp_OptionsType* options) {
107  int opt;
108  int res;
109 
110 #if XCP_TRANSPORT_LAYER == XCP_ON_ETHERNET
111  int p_assigned = 0;
112  int v_assigned = 0;
113 
114  options->tcp = XCP_TRUE;
115  options->ipv6 = XCP_FALSE;
116  options->port = XCP_ETH_DEFAULT_PORT;
117 #elif XCP_TRANSPORT_LAYER == XCP_ON_CAN
118  int if_assigned = 0;
119  options->fd = XCP_FALSE;
120 #endif
121 
122  while ((opt = getopt(argc, argv, OPTION_STR)) != -1) {
123  switch (opt) {
124  case 'h':
125  case '?':
126  usage();
127  break; /* never reached. */
128 #if XCP_TRANSPORT_LAYER == XCP_ON_ETHERNET
129  case 't':
130  if (p_assigned) {
131  printf("-t and -u options are mutual exclusive.\n");
132  exit(1);
133  }
134  p_assigned = XCP_TRUE;
135  options->tcp = XCP_TRUE;
136  break;
137  case 'u':
138  if (p_assigned) {
139  printf("-t and -u options are mutual exclusive.\n");
140  exit(1);
141  }
142  p_assigned = XCP_TRUE;
143  options->tcp = XCP_FALSE;
144  break;
145  case '4':
146  if (v_assigned) {
147  printf("-4 and -6 options are mutual exclusive.\n");
148  exit(1);
149  }
150  v_assigned = XCP_TRUE;
151  options->ipv6 = XCP_FALSE;
152  break;
153  case '6':
154  if (v_assigned) {
155  printf("-4 and -6 options are mutual exclusive.\n");
156  exit(1);
157  }
158  v_assigned = XCP_TRUE;
159  options->ipv6 = XCP_TRUE;
160  break;
161  case 'p':
162  options->port = atoi(optarg);
163 #elif XCP_TRANSPORT_LAYER == XCP_ON_CAN
164  case 'f':
165  options->fd = XCP_TRUE;
166  break;
167  case 'i':
168  if_assigned = 1;
169  strcpy(options->interface, optarg);
170  break;
171 #endif
172  }
173  }
174 
175 #if XCP_TRANSPORT_LAYER == XCP_ON_CAN
176  if (!if_assigned) {
177  strcpy(options->interface, XCP_SOCKET_CAN_DEFAULT_IF);
178  }
179 #endif
180 }
181 #endif