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 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]);
68 int main(int argc, char *argv[])
74 char *filename = NULL;
76 while ((opt = getopt(argc, argv, "rfc:ad")) != -1) {
79 flags |= RESET_DEVICE_fl;
82 flags |= FLASH_CONF_fl;
88 flags |= SLLIN_ATTACH_fl;
91 flags |= SLLIN_DETACH_fl;
94 linc_explain(argc, argv);
98 linc_lin_state.flags = flags;
100 /* Expected argument after options */
101 if (optind >= argc) {
102 linc_explain(argc, argv);
106 ret = linc_parse_configuration(filename, &linc_lin_state);
108 printf("Configuration file %s parsed correctly\n", filename);
110 /* Parse device type and path */
111 c = argv[optind]; /* "devtype:devpath" */
112 while ((*c != ':') && (*c != '\0')) {
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 */
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);
129 if (ret == LIN_EXIT_OK) /* Do not daemonize */
132 /* Run as daemon -- this is needed for attaching sllin TTY line discipline */
133 printf("Running in background ...\n");
140 // FIXME free() linc_lin_state?
141 /* Sleep to keep the line discipline active. */