slcanpty.c 11 KB

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