You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

424 lines
9.8 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * slcand.c - userspace daemon for serial line CAN interface driver SLCAN
  3. *
  4. * Copyright (c) 2009 Robert Haddon <robert.haddon@verari.com>
  5. * Copyright (c) 2009 Verari Systems Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. * Send feedback to <linux-can@vger.kernel.org>
  22. *
  23. */
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <unistd.h>
  28. #include <sys/types.h>
  29. #include <sys/stat.h>
  30. #include <sys/socket.h>
  31. #include <fcntl.h>
  32. #include <syslog.h>
  33. #include <errno.h>
  34. #include <pwd.h>
  35. #include <signal.h>
  36. #include <sys/ioctl.h>
  37. #include <net/if.h>
  38. #include <termios.h>
  39. #include <linux/tty.h>
  40. #include <linux/sockios.h>
  41. #include <stdarg.h>
  42. /* Change this to whatever your daemon is called */
  43. #define DAEMON_NAME "slcand"
  44. /* Change this to the user under which to run */
  45. #define RUN_AS_USER "root"
  46. /* The length of ttypath buffer */
  47. #define TTYPATH_LENGTH 64
  48. /* UART flow control types */
  49. #define FLOW_NONE 0
  50. #define FLOW_HW 1
  51. #define FLOW_SW 2
  52. static void fake_syslog(int priority, const char *format, ...)
  53. {
  54. va_list ap;
  55. printf("[%d] ", priority);
  56. va_start(ap, format);
  57. vprintf(format, ap);
  58. va_end(ap);
  59. printf("\n");
  60. }
  61. typedef void (*syslog_t)(int priority, const char *format, ...);
  62. static syslog_t syslogger = syslog;
  63. void print_usage(char *prg)
  64. {
  65. fprintf(stderr, "\nUsage: %s [options] <tty> [canif-name]\n\n", prg);
  66. fprintf(stderr, "Options: -o (send open command 'O\\r')\n");
  67. fprintf(stderr, " -c (send close command 'C\\r')\n");
  68. fprintf(stderr, " -f (read status flags with 'F\\r' to reset error states)\n");
  69. fprintf(stderr, " -l (send listen only command 'L\\r', overrides -o)\n");
  70. fprintf(stderr, " -s <speed> (set CAN speed 0..8)\n");
  71. fprintf(stderr, " -S <speed> (set UART speed in baud)\n");
  72. fprintf(stderr, " -t <type> (set UART flow control type 'hw' or 'sw')\n");
  73. fprintf(stderr, " -b <btr> (set bit time register value)\n");
  74. fprintf(stderr, " -F (stay in foreground; no daemonize)\n");
  75. fprintf(stderr, " -h (show this help page)\n");
  76. fprintf(stderr, "\nExamples:\n");
  77. fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0\n");
  78. fprintf(stderr, "slcand -o -c -f -s6 ttyUSB0 can0\n");
  79. fprintf(stderr, "slcand -o -c -f -s6 /dev/ttyUSB0\n");
  80. fprintf(stderr, "\n");
  81. exit(EXIT_FAILURE);
  82. }
  83. static int slcand_running;
  84. static int exit_code;
  85. static char ttypath[TTYPATH_LENGTH];
  86. static void child_handler(int signum)
  87. {
  88. switch (signum) {
  89. case SIGUSR1:
  90. /* exit parent */
  91. exit(EXIT_SUCCESS);
  92. break;
  93. case SIGALRM:
  94. case SIGCHLD:
  95. syslogger(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
  96. exit_code = EXIT_FAILURE;
  97. slcand_running = 0;
  98. break;
  99. case SIGINT:
  100. case SIGTERM:
  101. syslogger(LOG_NOTICE, "received signal %i on %s", signum, ttypath);
  102. exit_code = EXIT_SUCCESS;
  103. slcand_running = 0;
  104. break;
  105. }
  106. }
  107. static int look_up_uart_speed(long int s)
  108. {
  109. switch (s) {
  110. case 9600:
  111. return B9600;
  112. case 19200:
  113. return B19200;
  114. case 38400:
  115. return B38400;
  116. case 57600:
  117. return B57600;
  118. case 115200:
  119. return B115200;
  120. case 230400:
  121. return B230400;
  122. case 460800:
  123. return B460800;
  124. case 500000:
  125. return B500000;
  126. case 576000:
  127. return B576000;
  128. case 921600:
  129. return B921600;
  130. case 1000000:
  131. return B1000000;
  132. case 1152000:
  133. return B1152000;
  134. case 1500000:
  135. return B1500000;
  136. case 2000000:
  137. return B2000000;
  138. #ifdef B2500000
  139. case 2500000:
  140. return B2500000;
  141. #endif
  142. #ifdef B3000000
  143. case 3000000:
  144. return B3000000;
  145. #endif
  146. #ifdef B3500000
  147. case 3500000:
  148. return B3500000;
  149. #endif
  150. #ifdef B3710000
  151. case 3710000
  152. return B3710000;
  153. #endif
  154. #ifdef B4000000
  155. case 4000000:
  156. return B4000000;
  157. #endif
  158. default:
  159. return -1;
  160. }
  161. }
  162. int main(int argc, char *argv[])
  163. {
  164. char *tty = NULL;
  165. char const *devprefix = "/dev/";
  166. char *name = NULL;
  167. char buf[IFNAMSIZ+1];
  168. struct termios tios;
  169. speed_t old_ispeed;
  170. speed_t old_ospeed;
  171. int opt;
  172. int send_open = 0;
  173. int send_close = 0;
  174. int send_listen = 0;
  175. int send_read_status_flags = 0;
  176. char *speed = NULL;
  177. char *uart_speed_str = NULL;
  178. long int uart_speed = 0;
  179. int flow_type = FLOW_NONE;
  180. char *btr = NULL;
  181. int run_as_daemon = 1;
  182. char *pch;
  183. int ldisc = N_SLCAN;
  184. int fd;
  185. ttypath[0] = '\0';
  186. while ((opt = getopt(argc, argv, "ocfls:S:t:b:?hF")) != -1) {
  187. switch (opt) {
  188. case 'o':
  189. send_open = 1;
  190. break;
  191. case 'c':
  192. send_close = 1;
  193. break;
  194. case 'f':
  195. send_read_status_flags = 1;
  196. break;
  197. case 'l':
  198. send_listen = 1;
  199. break;
  200. case 's':
  201. speed = optarg;
  202. if (strlen(speed) > 1)
  203. print_usage(argv[0]);
  204. break;
  205. case 'S':
  206. uart_speed_str = optarg;
  207. errno = 0;
  208. uart_speed = strtol(uart_speed_str, NULL, 10);
  209. if (errno)
  210. print_usage(argv[0]);
  211. if (look_up_uart_speed(uart_speed) == -1) {
  212. fprintf(stderr, "Unsupported UART speed (%lu)\n", uart_speed);
  213. exit(EXIT_FAILURE);
  214. }
  215. break;
  216. case 't':
  217. if (!strcmp(optarg, "hw")) {
  218. flow_type = FLOW_HW;
  219. } else if (!strcmp(optarg, "sw")) {
  220. flow_type = FLOW_SW;
  221. } else {
  222. fprintf(stderr, "Unsupported flow type (%s)\n", optarg);
  223. exit(EXIT_FAILURE);
  224. }
  225. break;
  226. case 'b':
  227. btr = optarg;
  228. if (strlen(btr) > 6)
  229. print_usage(argv[0]);
  230. break;
  231. case 'F':
  232. run_as_daemon = 0;
  233. break;
  234. case 'h':
  235. case '?':
  236. default:
  237. print_usage(argv[0]);
  238. break;
  239. }
  240. }
  241. if (!run_as_daemon)
  242. syslogger = fake_syslog;
  243. /* Initialize the logging interface */
  244. openlog(DAEMON_NAME, LOG_PID, LOG_LOCAL5);
  245. /* Parse serial device name and optional can interface name */
  246. tty = argv[optind];
  247. if (NULL == tty)
  248. print_usage(argv[0]);
  249. name = argv[optind + 1];
  250. /* Prepare the tty device name string */
  251. pch = strstr(tty, devprefix);
  252. if (pch != tty)
  253. snprintf(ttypath, TTYPATH_LENGTH, "%s%s", devprefix, tty);
  254. else
  255. snprintf(ttypath, TTYPATH_LENGTH, "%s", tty);
  256. syslogger(LOG_INFO, "starting on TTY device %s", ttypath);
  257. /* Daemonize */
  258. if (run_as_daemon) {
  259. if (daemon(0, 0)) {
  260. syslogger(LOG_ERR, "failed to daemonize");
  261. exit(EXIT_FAILURE);
  262. }
  263. }
  264. else {
  265. /* Trap signals that we expect to receive */
  266. signal(SIGINT, child_handler);
  267. signal(SIGTERM, child_handler);
  268. }
  269. /* */
  270. slcand_running = 1;
  271. /* Now we are a daemon -- do the work for which we were paid */
  272. fd = open(ttypath, O_RDWR | O_NONBLOCK | O_NOCTTY);
  273. if (fd < 0) {
  274. syslogger(LOG_NOTICE, "failed to open TTY device %s\n", ttypath);
  275. perror(ttypath);
  276. exit(EXIT_FAILURE);
  277. }
  278. /* Configure baud rate */
  279. memset(&tios, 0, sizeof(struct termios));
  280. if (tcgetattr(fd, &tios) < 0) {
  281. syslogger(LOG_NOTICE, "failed to get attributes for TTY device %s: %s\n", ttypath, strerror(errno));
  282. exit(EXIT_FAILURE);
  283. }
  284. /* Get old values for later restore */
  285. old_ispeed = cfgetispeed(&tios);
  286. old_ospeed = cfgetospeed(&tios);
  287. /* Reset UART settings */
  288. cfmakeraw(&tios);
  289. tios.c_iflag &= ~IXOFF;
  290. tios.c_cflag &= ~CRTSCTS;
  291. /* Baud Rate */
  292. cfsetispeed(&tios, look_up_uart_speed(uart_speed));
  293. cfsetospeed(&tios, look_up_uart_speed(uart_speed));
  294. /* Flow control */
  295. if (flow_type == FLOW_HW)
  296. tios.c_cflag |= CRTSCTS;
  297. else if (flow_type == FLOW_SW)
  298. tios.c_iflag |= (IXON | IXOFF);
  299. /* apply changes */
  300. if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
  301. syslogger(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
  302. if (speed) {
  303. sprintf(buf, "C\rS%s\r", speed);
  304. write(fd, buf, strlen(buf));
  305. }
  306. if (btr) {
  307. sprintf(buf, "C\rs%s\r", btr);
  308. write(fd, buf, strlen(buf));
  309. }
  310. if (send_read_status_flags) {
  311. sprintf(buf, "F\r");
  312. write(fd, buf, strlen(buf));
  313. }
  314. if (send_listen) {
  315. sprintf(buf, "L\r");
  316. write(fd, buf, strlen(buf));
  317. } else if (send_open) {
  318. sprintf(buf, "O\r");
  319. write(fd, buf, strlen(buf));
  320. }
  321. /* set slcan like discipline on given tty */
  322. if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
  323. perror("ioctl TIOCSETD");
  324. exit(EXIT_FAILURE);
  325. }
  326. /* retrieve the name of the created CAN netdevice */
  327. if (ioctl(fd, SIOCGIFNAME, buf) < 0) {
  328. perror("ioctl SIOCGIFNAME");
  329. exit(EXIT_FAILURE);
  330. }
  331. syslogger(LOG_NOTICE, "attached TTY %s to netdevice %s\n", ttypath, buf);
  332. /* try to rename the created netdevice */
  333. if (name) {
  334. struct ifreq ifr;
  335. int s = socket(PF_INET, SOCK_DGRAM, 0);
  336. if (s < 0)
  337. perror("socket for interface rename");
  338. else {
  339. strncpy(ifr.ifr_name, buf, IFNAMSIZ);
  340. strncpy(ifr.ifr_newname, name, IFNAMSIZ);
  341. if (ioctl(s, SIOCSIFNAME, &ifr) < 0) {
  342. syslogger(LOG_NOTICE, "netdevice %s rename to %s failed\n", buf, name);
  343. perror("ioctl SIOCSIFNAME rename");
  344. exit(EXIT_FAILURE);
  345. } else
  346. syslogger(LOG_NOTICE, "netdevice %s renamed to %s\n", buf, name);
  347. close(s);
  348. }
  349. }
  350. /* The Big Loop */
  351. while (slcand_running)
  352. sleep(1); /* wait 1 second */
  353. /* Reset line discipline */
  354. syslogger(LOG_INFO, "stopping on TTY device %s", ttypath);
  355. ldisc = N_TTY;
  356. if (ioctl(fd, TIOCSETD, &ldisc) < 0) {
  357. perror("ioctl TIOCSETD");
  358. exit(EXIT_FAILURE);
  359. }
  360. if (send_close) {
  361. sprintf(buf, "C\r");
  362. write(fd, buf, strlen(buf));
  363. }
  364. /* Reset old rates */
  365. cfsetispeed(&tios, old_ispeed);
  366. cfsetospeed(&tios, old_ospeed);
  367. /* apply changes */
  368. if (tcsetattr(fd, TCSADRAIN, &tios) < 0)
  369. syslogger(LOG_NOTICE, "Cannot set attributes for device \"%s\": %s!\n", ttypath, strerror(errno));
  370. /* Finish up */
  371. syslogger(LOG_NOTICE, "terminated on %s", ttypath);
  372. closelog();
  373. return exit_code;
  374. }