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