]> rtime.felk.cvut.cz Git - linux-lin.git/blob - lin_config/src/lin_config.c
linconf: Basic sllin configuration.
[linux-lin.git] / lin_config / src / lin_config.c
1 /*
2  * PCAN-LIN, RS-232 to CAN/LIN converter control application
3  *
4  *   This program is free software; you can distribute it and/or
5  *   modify it under the terms of the GNU General Public License
6  *   as published by the Free Software Foundation; version 2 of
7  *   the License.
8  *
9  * Copyright:  (c) 2012 Czech Technical University in Prague
10  * Authors:    Rostislav Lisovy <lisovy@gmail.cz>
11  */
12
13 /*
14   Used prefixes explanation:
15     pcl_ -- PCAN-LIN (hw) related functions
16     sll_ -- sllin (tty lin implementation) related functions
17     linc_ -- LIN config general functions
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <string.h>
25 #include <termios.h>
26 #include <stdint.h>
27 #include <assert.h>
28 #include "linc_parse_xml.h"
29 #include "pcl_config.h"
30 #include "sllin_config.h"
31 #include "lin_config.h"
32
33
34 void linc_explain(int argc, char *argv[])
35 {
36 // FIXME what is default behaviour
37 // Write a warning about not using a rs232--usb converter for sllin
38         fprintf(stderr, "Usage: %s [OPTIONS] <SERIAL_INTERFACE>\n", argv[0]);
39         fprintf(stderr, "\n");
40         fprintf(stderr, "'lin_config' is used for configuring sllin -- " \
41                 "simple LIN device implemented\n" \
42                 "  as a TTY line discipline for arbitrary UART interface.\n" \
43                 "  This program is able to configure PCAN-LIN (RS232 configurable " \
44                 "LIN node) as well.\n" \
45                 "  When invoked without any OPTIONS, it configures PCAN-LIN device\n" \
46                 "  with configuration obtained from '"PCL_DEFAULT_CONFIG"' " \
47                 "file (if it exists).\n");
48         fprintf(stderr, "\n");
49         fprintf(stderr, "SERIAL_INTERFACE is in format CLASS:PATH\n");
50         fprintf(stderr, "  CLASS defines the device class -- it is either " \
51                 "'sllin' or 'pcanlin'\n");
52         fprintf(stderr, "  PATH is path to the serial interface, e.g /dev/ttyS0\n");
53         fprintf(stderr, "\n");
54         fprintf(stderr, "General options:\n");
55         fprintf(stderr, " -c <FILE>   Path to XML configuration file in PCLIN format\n");
56         fprintf(stderr, " -r          Execute only Reset of a device\n");
57         fprintf(stderr, "\n");
58         fprintf(stderr, "PCAN-LIN specific options:\n");
59         fprintf(stderr, " -f          Store the active configuration into internal " \
60                 "flash memory\n");
61         fprintf(stderr, "\n");
62         fprintf(stderr, "Sllin specific options:\n");
63         fprintf(stderr, " -a          Attach sllin TTY line discipline to " \
64                 "particular SERIAL_INTERFACE\n");
65         fprintf(stderr, " -d          Detach sllin TTY line discipline from " \
66                 "particular SERIAL_INTERFACE\n");
67         fprintf(stderr, "\n");
68         fprintf(stderr, "Examples:\n");
69         fprintf(stderr, " %s sllin:/dev/ttyS0        (Configure the device with the " \
70                 "configuration from '"PCL_DEFAULT_CONFIG"')\n", argv[0]);
71         fprintf(stderr, " %s -r pcanlin:/dev/ttyS0   (Reset the device)\n", argv[0]);
72 }
73
74 int main(int argc, char *argv[])
75 {
76         int ret;
77         int opt;
78         int flags = 0;
79         char *filename = NULL;
80
81         while ((opt = getopt(argc, argv, "rfc:ad")) != -1) {
82                 switch (opt) {
83                 case 'r':
84                         flags |= RESET_DEVICE_fl;
85                         break;
86                 case 'f':
87                         flags |= FLASH_CONF_fl;
88                         break;
89                 case 'c':
90                         filename = optarg;
91                         break;
92                 case 'a':
93                         flags |= SLLIN_ATTACH_fl;
94                         break;
95                 case 'd':
96                         flags |= SLLIN_DETACH_fl;
97                         break;
98                 default:
99                         linc_explain(argc, argv);
100                         return EXIT_FAILURE;
101                 }
102         }
103
104         /* Expected argument after options */
105         if (optind >= argc) {
106                 linc_explain(argc, argv);
107                 exit(EXIT_FAILURE);
108         }
109
110         linc_lin_state.dev = strdup(argv[optind]);
111
112         ret = linc_parse_configuration(filename, &linc_lin_state);
113         if (!ret)
114                 printf("Configuration file %s parsed correctly\n", filename);
115
116         linc_lin_state.flags = flags;
117         //ret = pcl_config(&linc_lin_state);
118         ret = sllin_config(&linc_lin_state);
119
120 //      printf("Press any key to detach %s ...\n", linc_lin_state.dev);
121 //      getchar();
122
123
124         if (ret < 0)
125                 return EXIT_FAILURE;
126
127         return EXIT_SUCCESS;
128 }