]> rtime.felk.cvut.cz Git - linux-lin.git/blob - lin_config/src/lin_config.c
lin_config: Basic sllin frame cache 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 #include "linux/lin_bus.h"
34
35 struct linc_lin_state linc_lin_state;
36
37
38 void linc_explain(int argc, char *argv[])
39 {
40 // FIXME what is default behaviour
41 // Write a warning about not using a rs232--usb converter for sllin
42         fprintf(stderr, "Usage: %s [OPTIONS] <SERIAL_INTERFACE>\n", argv[0]);
43         fprintf(stderr, "\n");
44         fprintf(stderr, "'lin_config' is used for configuring sllin -- " \
45                 "simple LIN device implemented\n" \
46                 "  as a TTY line discipline for arbitrary UART interface.\n" \
47                 "  This program is able to configure PCAN-LIN (RS232 configurable " \
48                 "LIN node) as well.\n" \
49                 "  When invoked without any OPTIONS, it configures PCAN-LIN device\n" \
50                 "  with configuration obtained from '"PCL_DEFAULT_CONFIG"' " \
51                 "file (if it exists).\n");
52         fprintf(stderr, "\n");
53         fprintf(stderr, "SERIAL_INTERFACE is in format CLASS:PATH\n");
54         fprintf(stderr, "  CLASS defines the device class -- it is either " \
55                 "'sllin' or 'pcanlin'\n");
56         fprintf(stderr, "  PATH is path to the serial interface, e.g /dev/ttyS0\n");
57         fprintf(stderr, "\n");
58         fprintf(stderr, "General options:\n");
59         fprintf(stderr, " -c <FILE>   Path to XML configuration file in PCLIN format\n");
60         fprintf(stderr, " -r          Execute only Reset of a device\n");
61         fprintf(stderr, "\n");
62         fprintf(stderr, "PCAN-LIN specific options:\n");
63         fprintf(stderr, " -f          Store the active configuration into internal " \
64                 "flash memory\n");
65         fprintf(stderr, "\n");
66         fprintf(stderr, "Sllin specific options:\n");
67         fprintf(stderr, " -a          Attach sllin TTY line discipline to " \
68                 "particular SERIAL_INTERFACE\n");
69         fprintf(stderr, " -d          Detach sllin TTY line discipline from " \
70                 "particular SERIAL_INTERFACE\n");
71         fprintf(stderr, "\n");
72         fprintf(stderr, "Examples:\n");
73         fprintf(stderr, " %s sllin:/dev/ttyS0        (Configure the device with the " \
74                 "configuration from '"PCL_DEFAULT_CONFIG"')\n", argv[0]);
75         fprintf(stderr, " %s -r pcanlin:/dev/ttyS0   (Reset the device)\n", argv[0]);
76 }
77
78 int main(int argc, char *argv[])
79 {
80         int ret;
81         int opt;
82         int flags = 0;
83         char *filename = NULL;
84
85         while ((opt = getopt(argc, argv, "rfc:ad")) != -1) {
86                 switch (opt) {
87                 case 'r':
88                         flags |= RESET_DEVICE_fl;
89                         break;
90                 case 'f':
91                         flags |= FLASH_CONF_fl;
92                         break;
93                 case 'c':
94                         filename = optarg;
95                         break;
96                 case 'a':
97                         flags |= SLLIN_ATTACH_fl;
98                         break;
99                 case 'd':
100                         flags |= SLLIN_DETACH_fl;
101                         break;
102                 default:
103                         linc_explain(argc, argv);
104                         return EXIT_FAILURE;
105                 }
106         }
107
108         /* Expected argument after options */
109         if (optind >= argc) {
110                 linc_explain(argc, argv);
111                 exit(EXIT_FAILURE);
112         }
113
114         linc_lin_state.dev = strdup(argv[optind]);
115
116         ret = linc_parse_configuration(filename, &linc_lin_state);
117         if (!ret)
118                 printf("Configuration file %s parsed correctly\n", filename);
119
120         linc_lin_state.flags = flags;
121         //ret = pcl_config(&linc_lin_state);
122         ret = sllin_config(&linc_lin_state);
123
124         if (ret < 0)
125                 return EXIT_FAILURE;
126         if (ret == LIN_EXIT_OK) {
127
128                 return EXIT_SUCCESS;
129         }
130
131         printf("Running in background ...\n");
132         ret = daemon(0, 0);
133         if (ret < 0) {
134                 perror("daemon()");
135                 return EXIT_FAILURE;
136         }
137
138         // FIXME free() linc_lin_state?
139         /* Sleep to keep the line discipline active. */
140         pause();
141
142         return EXIT_SUCCESS;
143 }