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.

550 lines
11 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * slcanpty.c - creates a pty for applications using the slcan ASCII protocol
  3. * and converts the ASCII data to a CAN network interface (and vice versa)
  4. *
  5. * Copyright (c)2009 Oliver Hartkopp
  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. /* To get ptsname grantpt and unlockpt definitions from stdlib.h */
  25. #define _GNU_SOURCE
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #include <unistd.h>
  30. #include <fcntl.h>
  31. #include <termios.h>
  32. #include <net/if.h>
  33. #include <sys/socket.h>
  34. #include <sys/ioctl.h>
  35. #include <sys/stat.h>
  36. #include <sys/types.h>
  37. #include <linux/can.h>
  38. #include <linux/can/raw.h>
  39. /* maximum rx buffer len: extended CAN frame with timestamp */
  40. #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
  41. #define DEVICE_NAME_PTMX "/dev/ptmx"
  42. #define DEBUG
  43. static int asc2nibble(char c)
  44. {
  45. if ((c >= '0') && (c <= '9'))
  46. return c - '0';
  47. if ((c >= 'A') && (c <= 'F'))
  48. return c - 'A' + 10;
  49. if ((c >= 'a') && (c <= 'f'))
  50. return c - 'a' + 10;
  51. return 16; /* error */
  52. }
  53. /* read data from pty, send CAN frames to CAN socket and answer commands */
  54. int pty2can(int pty, int socket, struct can_filter *fi,
  55. int *is_open, int *tstamp)
  56. {
  57. int nbytes;
  58. char cmd;
  59. static char buf[200];
  60. char replybuf[10]; /* for answers to received commands */
  61. int ptr;
  62. struct can_frame frame;
  63. int tmp, i;
  64. static int rxoffset = 0; /* points to the end of an received incomplete SLCAN message */
  65. nbytes = read(pty, &buf[rxoffset], sizeof(buf)-rxoffset-1);
  66. if (nbytes <= 0) {
  67. /* nbytes == 0 : no error but pty decriptor has been closed */
  68. if (nbytes < 0)
  69. perror("read pty");
  70. return 1;
  71. }
  72. /* reset incomplete message offset */
  73. nbytes += rxoffset;
  74. rxoffset = 0;
  75. rx_restart:
  76. /* remove trailing '\r' characters to be robust against some apps */
  77. while (buf[0] == '\r' && nbytes > 0) {
  78. for (tmp = 0; tmp < nbytes; tmp++)
  79. buf[tmp] = buf[tmp+1];
  80. nbytes--;
  81. }
  82. if (!nbytes)
  83. return 0;
  84. /* check if we can detect a complete SLCAN message including '\r' */
  85. for (tmp = 0; tmp < nbytes; tmp++) {
  86. if (buf[tmp] == '\r')
  87. break;
  88. }
  89. /* no '\r' found in the message buffer? */
  90. if (tmp == nbytes) {
  91. /* save incomplete message */
  92. rxoffset = nbytes;
  93. /* leave here and read from pty again */
  94. return 0;
  95. }
  96. cmd = buf[0];
  97. buf[nbytes] = 0;
  98. #ifdef DEBUG
  99. for (tmp = 0; tmp < nbytes; tmp++)
  100. if (buf[tmp] == '\r')
  101. putchar('@');
  102. else
  103. putchar(buf[tmp]);
  104. printf("\n");
  105. #endif
  106. /* check for filter configuration commands */
  107. if (cmd == 'm' || cmd == 'M') {
  108. buf[9] = 0; /* terminate filter string */
  109. ptr = 9;
  110. #if 0
  111. /* the filter is no SocketCAN filter :-( */
  112. /* TODO: behave like a SJA1000 controller specific filter */
  113. if (cmd == 'm') {
  114. fi->can_id = strtoul(buf+1,NULL,16);
  115. fi->can_id &= CAN_EFF_MASK;
  116. } else {
  117. fi->can_mask = strtoul(buf+1,NULL,16);
  118. fi->can_mask &= CAN_EFF_MASK;
  119. }
  120. if (*is_open)
  121. setsockopt(socket, SOL_CAN_RAW,
  122. CAN_RAW_FILTER, fi,
  123. sizeof(struct can_filter));
  124. #endif
  125. goto rx_out_ack;
  126. }
  127. /* check for timestamp on/off command */
  128. if (cmd == 'Z') {
  129. *tstamp = buf[1] & 0x01;
  130. ptr = 2;
  131. goto rx_out_ack;
  132. }
  133. /* check for 'O'pen command */
  134. if (cmd == 'O') {
  135. setsockopt(socket, SOL_CAN_RAW,
  136. CAN_RAW_FILTER, fi,
  137. sizeof(struct can_filter));
  138. ptr = 1;
  139. *is_open = 1;
  140. goto rx_out_ack;
  141. }
  142. /* check for 'C'lose command */
  143. if (cmd == 'C') {
  144. setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FILTER,
  145. NULL, 0);
  146. ptr = 1;
  147. *is_open = 0;
  148. goto rx_out_ack;
  149. }
  150. /* check for 'V'ersion command */
  151. if (cmd == 'V') {
  152. sprintf(replybuf, "V1013\r");
  153. tmp = strlen(replybuf);
  154. ptr = 1;
  155. goto rx_out;
  156. }
  157. /* check for 'v'ersion command */
  158. if (cmd == 'v') {
  159. sprintf(replybuf, "v1014\r");
  160. tmp = strlen(replybuf);
  161. ptr = 1;
  162. goto rx_out;
  163. }
  164. /* check for serial 'N'umber command */
  165. if (cmd == 'N') {
  166. sprintf(replybuf, "N4242\r");
  167. tmp = strlen(replybuf);
  168. ptr = 1;
  169. goto rx_out;
  170. }
  171. /* check for read status 'F'lags */
  172. if (cmd == 'F') {
  173. sprintf(replybuf, "F00\r");
  174. tmp = strlen(replybuf);
  175. ptr = 1;
  176. goto rx_out;
  177. }
  178. /* correctly answer unsupported commands */
  179. if (cmd == 'U') {
  180. ptr = 2;
  181. goto rx_out_ack;
  182. }
  183. if (cmd == 'S') {
  184. ptr = 2;
  185. goto rx_out_ack;
  186. }
  187. if (cmd == 's') {
  188. ptr = 5;
  189. goto rx_out_ack;
  190. }
  191. if (cmd == 'P' || cmd == 'A') {
  192. ptr = 1;
  193. goto rx_out_nack;
  194. }
  195. if (cmd == 'X') {
  196. ptr = 2;
  197. if (buf[1] & 0x01)
  198. goto rx_out_ack;
  199. else
  200. goto rx_out_nack;
  201. }
  202. /* catch unknown commands */
  203. if ((cmd != 't') && (cmd != 'T') &&
  204. (cmd != 'r') && (cmd != 'R')) {
  205. ptr = nbytes-1;
  206. goto rx_out_nack;
  207. }
  208. if (cmd & 0x20) /* tiny chars 'r' 't' => SFF */
  209. ptr = 4; /* dlc position tiiid */
  210. else
  211. ptr = 9; /* dlc position Tiiiiiiiid */
  212. memset(&frame.data, 0, 8); /* clear data[] */
  213. if ((cmd | 0x20) == 'r' && buf[ptr] != '0') {
  214. /*
  215. * RTR frame without dlc information!
  216. * This is against the SLCAN spec but sent
  217. * by a commercial CAN tool ... so we are
  218. * robust against this protocol violation.
  219. */
  220. frame.can_dlc = buf[ptr]; /* save following byte */
  221. buf[ptr] = 0; /* terminate can_id string */
  222. frame.can_id = strtoul(buf+1, NULL, 16);
  223. frame.can_id |= CAN_RTR_FLAG;
  224. if (!(cmd & 0x20)) /* NO tiny chars => EFF */
  225. frame.can_id |= CAN_EFF_FLAG;
  226. buf[ptr] = frame.can_dlc; /* restore following byte */
  227. frame.can_dlc = 0;
  228. ptr--; /* we have no dlc component in the violation case */
  229. } else {
  230. if (!(buf[ptr] >= '0' && buf[ptr] < '9'))
  231. goto rx_out_nack;
  232. frame.can_dlc = buf[ptr] - '0'; /* get dlc from ASCII val */
  233. buf[ptr] = 0; /* terminate can_id string */
  234. frame.can_id = strtoul(buf+1, NULL, 16);
  235. if (!(cmd & 0x20)) /* NO tiny chars => EFF */
  236. frame.can_id |= CAN_EFF_FLAG;
  237. if ((cmd | 0x20) == 'r') /* RTR frame */
  238. frame.can_id |= CAN_RTR_FLAG;
  239. for (i = 0, ptr++; i < frame.can_dlc; i++) {
  240. tmp = asc2nibble(buf[ptr++]);
  241. if (tmp > 0x0F)
  242. goto rx_out_nack;
  243. frame.data[i] = (tmp << 4);
  244. tmp = asc2nibble(buf[ptr++]);
  245. if (tmp > 0x0F)
  246. goto rx_out_nack;
  247. frame.data[i] |= tmp;
  248. }
  249. /* point to last real data */
  250. if (frame.can_dlc)
  251. ptr--;
  252. }
  253. tmp = write(socket, &frame, sizeof(frame));
  254. if (tmp != sizeof(frame)) {
  255. perror("write socket");
  256. return 1;
  257. }
  258. rx_out_ack:
  259. replybuf[0] = '\r';
  260. tmp = 1;
  261. goto rx_out;
  262. rx_out_nack:
  263. replybuf[0] = '\a';
  264. tmp = 1;
  265. rx_out:
  266. tmp = write(pty, replybuf, tmp);
  267. if (tmp < 0) {
  268. perror("write pty replybuf");
  269. return 1;
  270. }
  271. /* check if there is another command in this buffer */
  272. if (nbytes > ptr+1) {
  273. for (tmp = 0, ptr++; ptr+tmp < nbytes; tmp++)
  274. buf[tmp] = buf[ptr+tmp];
  275. nbytes = tmp;
  276. goto rx_restart;
  277. }
  278. return 0;
  279. }
  280. /* read CAN frames from CAN interface and write it to the pty */
  281. int can2pty(int pty, int socket, int *tstamp)
  282. {
  283. int nbytes;
  284. char cmd;
  285. char buf[SLC_MTU];
  286. int ptr;
  287. struct can_frame frame;
  288. int i;
  289. nbytes = read(socket, &frame, sizeof(frame));
  290. if (nbytes != sizeof(frame)) {
  291. perror("read socket");
  292. return 1;
  293. }
  294. /* convert to slcan ASCII frame */
  295. if (frame.can_id & CAN_RTR_FLAG)
  296. cmd = 'R'; /* becomes 'r' in SFF format */
  297. else
  298. cmd = 'T'; /* becomes 't' in SFF format */
  299. if (frame.can_id & CAN_EFF_FLAG)
  300. sprintf(buf, "%c%08X%d", cmd,
  301. frame.can_id & CAN_EFF_MASK,
  302. frame.can_dlc);
  303. else
  304. sprintf(buf, "%c%03X%d", cmd | 0x20,
  305. frame.can_id & CAN_SFF_MASK,
  306. frame.can_dlc);
  307. ptr = strlen(buf);
  308. for (i = 0; i < frame.can_dlc; i++)
  309. sprintf(&buf[ptr + 2*i], "%02X",
  310. frame.data[i]);
  311. if (*tstamp) {
  312. struct timeval tv;
  313. if (ioctl(socket, SIOCGSTAMP, &tv) < 0)
  314. perror("SIOCGSTAMP");
  315. sprintf(&buf[ptr + 2*frame.can_dlc], "%04lX",
  316. (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
  317. }
  318. strcat(buf, "\r"); /* add terminating character */
  319. nbytes = write(pty, buf, strlen(buf));
  320. if (nbytes < 0) {
  321. perror("write pty");
  322. return 1;
  323. }
  324. fflush(NULL);
  325. return 0;
  326. }
  327. int check_select_stdin(void)
  328. {
  329. fd_set rdfs;
  330. struct timeval timeout;
  331. int ret;
  332. FD_ZERO(&rdfs);
  333. FD_SET(0, &rdfs);
  334. timeout.tv_sec = 0;
  335. timeout.tv_usec = 0;
  336. ret = select(1, &rdfs, NULL, NULL, &timeout);
  337. if (ret < 0)
  338. return 0; /* not selectable */
  339. if (ret > 0 && getchar() == EOF)
  340. return 0; /* EOF, eg. /dev/null */
  341. return 1;
  342. }
  343. int main(int argc, char **argv)
  344. {
  345. fd_set rdfs;
  346. int p; /* pty master file */
  347. int s; /* can raw socket */
  348. struct sockaddr_can addr;
  349. struct termios topts;
  350. int select_stdin = 0;
  351. int running = 1;
  352. int tstamp = 0;
  353. int is_open = 0;
  354. struct can_filter fi;
  355. /* check command line options */
  356. if (argc != 3) {
  357. fprintf(stderr, "\n");
  358. fprintf(stderr, "%s creates a pty for applications using"
  359. " the slcan ASCII protocol and\n", argv[0]);
  360. fprintf(stderr, "converts the ASCII data to a CAN network"
  361. " interface (and vice versa)\n\n");
  362. fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
  363. fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
  364. " /dev/ttyc0 for the slcan application\n", argv[0]);
  365. fprintf(stderr, "e.g. for pseudo-terminal '%s %s can0' creates"
  366. " /dev/pts/N\n", argv[0], DEVICE_NAME_PTMX);
  367. fprintf(stderr, "\n");
  368. return 1;
  369. }
  370. select_stdin = check_select_stdin();
  371. /* open pty */
  372. p = open(argv[1], O_RDWR);
  373. if (p < 0) {
  374. perror("open pty");
  375. return 1;
  376. }
  377. if (tcgetattr(p, &topts)) {
  378. perror("tcgetattr");
  379. return 1;
  380. }
  381. /* disable local echo which would cause double frames */
  382. topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
  383. ECHONL | ECHOPRT | ECHOKE);
  384. topts.c_iflag &= ~(ICRNL);
  385. topts.c_iflag |= INLCR;
  386. tcsetattr(p, TCSANOW, &topts);
  387. /* Support for the Unix 98 pseudo-terminal interface /dev/ptmx /dev/pts/N */
  388. if (strcmp(argv[1], DEVICE_NAME_PTMX) == 0) {
  389. char *name_pts = NULL; /* slave pseudo-terminal device name */
  390. if (grantpt(p) < 0) {
  391. perror("grantpt");
  392. return 1;
  393. }
  394. if (unlockpt(p) < 0) {
  395. perror("unlockpt");
  396. return 1;
  397. }
  398. name_pts = ptsname(p);
  399. if (name_pts == NULL) {
  400. perror("ptsname");
  401. return 1;
  402. }
  403. printf("open: %s: slave pseudo-terminal is %s\n", argv[1], name_pts);
  404. }
  405. /* open socket */
  406. s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
  407. if (s < 0) {
  408. perror("socket");
  409. return 1;
  410. }
  411. addr.can_family = AF_CAN;
  412. addr.can_ifindex = if_nametoindex(argv[2]);
  413. /* disable reception of CAN frames until we are opened by 'O' */
  414. setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
  415. if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
  416. perror("bind");
  417. return 1;
  418. }
  419. /* open filter by default */
  420. fi.can_id = 0;
  421. fi.can_mask = 0;
  422. while (running) {
  423. FD_ZERO(&rdfs);
  424. if (select_stdin)
  425. FD_SET(0, &rdfs);
  426. FD_SET(p, &rdfs);
  427. FD_SET(s, &rdfs);
  428. if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
  429. perror("select");
  430. return 1;
  431. }
  432. if (FD_ISSET(0, &rdfs)) {
  433. running = 0;
  434. continue;
  435. }
  436. if (FD_ISSET(p, &rdfs))
  437. if (pty2can(p, s, &fi, &is_open, &tstamp)) {
  438. running = 0;
  439. continue;
  440. }
  441. if (FD_ISSET(s, &rdfs))
  442. if (can2pty(p, s, &tstamp)) {
  443. running = 0;
  444. continue;
  445. }
  446. }
  447. close(p);
  448. close(s);
  449. return 0;
  450. }