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