2 * PCAN-LIN, RS-232 to CAN/LIN converter control application
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
9 * Copyright: (c) 2012 Czech Technical University in Prague
10 * Authors: Rostislav Lisovy <lisovy@gmail.cz>
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
28 #include "linc_parse_xml.h"
29 #include "pcl_config.h"
30 #include "sllin_config.h"
31 #include "lin_config.h"
33 #include "linux/lin_bus.h"
35 struct linc_lin_state linc_lin_state;
37 void linc_explain(int argc, char *argv[])
39 // FIXME what is default behaviour
40 // Write a warning about not using a rs232--usb converter for sllin
41 fprintf(stderr, "Usage: %s [OPTIONS] <SERIAL_INTERFACE>\n", argv[0]);
42 fprintf(stderr, "\n");
43 fprintf(stderr, "'lin_config' is used for configuring sllin -- " \
44 "simple LIN device implemented\n" \
45 " as a TTY line discipline for arbitrary UART interface.\n" \
46 " This program is able to configure PCAN-LIN (RS232 configurable " \
47 "LIN node) as well.\n" \
48 " When invoked without any OPTIONS, it configures PCAN-LIN device\n" \
49 " with configuration obtained from '"PCL_DEFAULT_CONFIG"' " \
50 "file (if it exists).\n");
51 fprintf(stderr, "\n");
52 fprintf(stderr, "SERIAL_INTERFACE is in format CLASS:PATH\n");
53 fprintf(stderr, " CLASS defines the device class -- it is either " \
54 "'sllin' or 'pcanlin'\n");
55 fprintf(stderr, " PATH is path to the serial interface, e.g /dev/ttyS0\n");
56 fprintf(stderr, "\n");
57 fprintf(stderr, "General options:\n");
58 fprintf(stderr, " -c <FILE> Path to XML configuration file in PCLIN format\n");
59 fprintf(stderr, "\n");
60 fprintf(stderr, "PCAN-LIN specific options:\n");
61 fprintf(stderr, " -f Store the active configuration into internal " \
63 fprintf(stderr, " -r Execute only Reset of a device\n");
64 fprintf(stderr, "\n");
65 fprintf(stderr, "Sllin specific options:\n");
66 fprintf(stderr, " -a Attach sllin TTY line discipline to " \
67 "particular SERIAL_INTERFACE\n");
68 fprintf(stderr, " -d Detach sllin TTY line discipline from " \
69 "particular SERIAL_INTERFACE\n");
70 fprintf(stderr, "\n");
71 fprintf(stderr, "Examples:\n");
72 fprintf(stderr, " %s sllin:/dev/ttyS0 (Configure the device with the " \
73 "configuration from '"PCL_DEFAULT_CONFIG"')\n", argv[0]);
74 fprintf(stderr, " %s -r pcanlin:/dev/ttyS0 (Reset the device)\n", argv[0]);
77 int main(int argc, char *argv[])
83 char *filename = NULL;
85 while ((opt = getopt(argc, argv, "rfc:ad")) != -1) {
88 flags |= RESET_DEVICE_fl;
91 flags |= FLASH_CONF_fl;
97 flags |= SLLIN_ATTACH_fl;
100 flags |= SLLIN_DETACH_fl;
103 linc_explain(argc, argv);
107 linc_lin_state.flags = flags;
109 /* Expected argument after options */
110 if (optind >= argc) {
111 linc_explain(argc, argv);
115 ret = linc_parse_configuration(filename, &linc_lin_state);
117 printf("Configuration file %s parsed correctly\n", filename);
119 /* Parse device type and path */
120 c = argv[optind]; /* "devtype:devpath" */
121 while ((*c != ':') && (*c != '\0')) {
124 *c = '\0'; /* In case we found ":" split the string into two */
125 linc_lin_state.dev = strdup(c + 1); /* Second half of the string -- device name */
127 if (!strcmp("pcanlin", argv[optind])) {
128 ret = pcl_config(&linc_lin_state);
129 } else if (!strcmp("sllin", argv[optind])) {
130 ret = sllin_config(&linc_lin_state);
132 fprintf(stderr, "Device type is missing. Using default device -- sllin.\n");
133 ret = sllin_config(&linc_lin_state);
138 if (ret == LIN_EXIT_OK) /* Do not daemonize */
141 /* Run as daemon -- this is needed for attaching sllin TTY line discipline */
142 printf("Running in background ...\n");
149 // FIXME free() linc_lin_state?
150 /* Sleep to keep the line discipline active. */