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)
5 * Copyright (c)2009 Oliver Hartkopp
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.
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.
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
21 * Send feedback to <linux-can@vger.kernel.org>
25 /* To get ptsname grantpt and unlockpt definitions from stdlib.h */
36 #include <sys/socket.h>
37 #include <sys/ioctl.h>
39 #include <sys/types.h>
41 #include <linux/can.h>
42 #include <linux/can/raw.h>
44 /* maximum rx buffer len: extended CAN frame with timestamp */
45 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
46 #define DEVICE_NAME_PTMX "/dev/ptmx"
50 static int asc2nibble(char c)
53 if ((c >= '0') && (c <= '9'))
56 if ((c >= 'A') && (c <= 'F'))
59 if ((c >= 'a') && (c <= 'f'))
62 return 16; /* error */
65 /* read data from pty, send CAN frames to CAN socket and answer commands */
66 int pty2can(int pty, int socket, struct can_filter *fi,
67 int *is_open, int *tstamp)
72 char replybuf[10]; /* for answers to received commands */
74 struct can_frame frame;
76 static int rxoffset = 0; /* points to the end of an received incomplete SLCAN message */
78 nbytes = read(pty, &buf[rxoffset], sizeof(buf)-rxoffset-1);
80 /* nbytes == 0 : no error but pty decriptor has been closed */
87 /* reset incomplete message offset */
92 /* remove trailing '\r' characters to be robust against some apps */
93 while (buf[0] == '\r' && nbytes > 0) {
94 for (tmp = 0; tmp < nbytes; tmp++)
95 buf[tmp] = buf[tmp+1];
102 /* check if we can detect a complete SLCAN message including '\r' */
103 for (tmp = 0; tmp < nbytes; tmp++) {
104 if (buf[tmp] == '\r')
108 /* no '\r' found in the message buffer? */
110 /* save incomplete message */
113 /* leave here and read from pty again */
121 for (tmp = 0; tmp < nbytes; tmp++)
122 if (buf[tmp] == '\r')
129 /* check for filter configuration commands */
130 if (cmd == 'm' || cmd == 'M') {
131 buf[9] = 0; /* terminate filter string */
134 /* the filter is no SocketCAN filter :-( */
136 /* TODO: behave like a SJA1000 controller specific filter */
139 fi->can_id = strtoul(buf+1,NULL,16);
140 fi->can_id &= CAN_EFF_MASK;
142 fi->can_mask = strtoul(buf+1,NULL,16);
143 fi->can_mask &= CAN_EFF_MASK;
147 setsockopt(socket, SOL_CAN_RAW,
149 sizeof(struct can_filter));
155 /* check for timestamp on/off command */
157 *tstamp = buf[1] & 0x01;
162 /* check for 'O'pen command */
164 setsockopt(socket, SOL_CAN_RAW,
166 sizeof(struct can_filter));
172 /* check for 'C'lose command */
174 setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FILTER,
181 /* check for 'V'ersion command */
183 sprintf(replybuf, "V1013\r");
184 tmp = strlen(replybuf);
189 /* check for serial 'N'umber command */
191 sprintf(replybuf, "N4242\r");
192 tmp = strlen(replybuf);
197 /* check for read status 'F'lags */
199 sprintf(replybuf, "F00\r");
200 tmp = strlen(replybuf);
205 /* correctly answer unsupported commands */
218 if (cmd == 'P' || cmd == 'A') {
230 /* catch unknown commands */
231 if ((cmd != 't') && (cmd != 'T') &&
232 (cmd != 'r') && (cmd != 'R')) {
237 if (cmd & 0x20) /* tiny chars 'r' 't' => SFF */
238 ptr = 4; /* dlc position tiiid */
240 ptr = 9; /* dlc position Tiiiiiiiid */
242 *(unsigned long long *) (&frame.data) = 0ULL; /* clear data[] */
244 if ((cmd | 0x20) == 'r' && buf[ptr] != '0') {
247 * RTR frame without dlc information!
248 * This is against the SLCAN spec but sent
249 * by a commercial CAN tool ... so we are
250 * robust against this protocol violation.
253 frame.can_dlc = buf[ptr]; /* save following byte */
255 buf[ptr] = 0; /* terminate can_id string */
257 frame.can_id = strtoul(buf+1, NULL, 16);
258 frame.can_id |= CAN_RTR_FLAG;
260 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
261 frame.can_id |= CAN_EFF_FLAG;
263 buf[ptr] = frame.can_dlc; /* restore following byte */
265 ptr--; /* we have no dlc component in the violation case */
269 if (!(buf[ptr] >= '0' && buf[ptr] < '9'))
272 frame.can_dlc = buf[ptr] - '0'; /* get dlc from ASCII val */
274 buf[ptr] = 0; /* terminate can_id string */
276 frame.can_id = strtoul(buf+1, NULL, 16);
278 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
279 frame.can_id |= CAN_EFF_FLAG;
281 if ((cmd | 0x20) == 'r') /* RTR frame */
282 frame.can_id |= CAN_RTR_FLAG;
284 for (i = 0, ptr++; i < frame.can_dlc; i++) {
286 tmp = asc2nibble(buf[ptr++]);
289 frame.data[i] = (tmp << 4);
290 tmp = asc2nibble(buf[ptr++]);
293 frame.data[i] |= tmp;
295 /* point to last real data */
300 tmp = write(socket, &frame, sizeof(frame));
301 if (tmp != sizeof(frame)) {
302 perror("write socket");
314 tmp = write(pty, replybuf, tmp);
316 perror("write pty replybuf");
320 /* check if there is another command in this buffer */
321 if (nbytes > ptr+1) {
322 for (tmp = 0, ptr++; ptr+tmp < nbytes; tmp++)
323 buf[tmp] = buf[ptr+tmp];
331 /* read CAN frames from CAN interface and write it to the pty */
332 int can2pty(int pty, int socket, int *tstamp)
338 struct can_frame frame;
341 nbytes = read(socket, &frame, sizeof(frame));
342 if (nbytes != sizeof(frame)) {
343 perror("read socket");
347 /* convert to slcan ASCII frame */
348 if (frame.can_id & CAN_RTR_FLAG)
349 cmd = 'R'; /* becomes 'r' in SFF format */
351 cmd = 'T'; /* becomes 't' in SFF format */
353 if (frame.can_id & CAN_EFF_FLAG)
354 sprintf(buf, "%c%08X%d", cmd,
355 frame.can_id & CAN_EFF_MASK,
358 sprintf(buf, "%c%03X%d", cmd | 0x20,
359 frame.can_id & CAN_SFF_MASK,
364 for (i = 0; i < frame.can_dlc; i++)
365 sprintf(&buf[ptr + 2*i], "%02X",
371 if (ioctl(socket, SIOCGSTAMP, &tv) < 0)
372 perror("SIOCGSTAMP");
374 sprintf(&buf[ptr + 2*frame.can_dlc], "%04lX",
375 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
378 strcat(buf, "\r"); /* add terminating character */
379 nbytes = write(pty, buf, strlen(buf));
389 int check_select_stdin(void)
392 struct timeval timeout;
400 ret = select(1, &rdfs, NULL, NULL, &timeout);
403 return 0; /* not selectable */
405 if (ret > 0 && getchar() == EOF)
406 return 0; /* EOF, eg. /dev/null */
411 int main(int argc, char **argv)
414 int p; /* pty master file */
415 int s; /* can raw socket */
416 struct sockaddr_can addr;
417 struct termios topts;
419 int select_stdin = 0;
423 struct can_filter fi;
425 /* check command line options */
427 fprintf(stderr, "\n");
428 fprintf(stderr, "%s creates a pty for applications using"
429 " the slcan ASCII protocol and\n", argv[0]);
430 fprintf(stderr, "converts the ASCII data to a CAN network"
431 " interface (and vice versa)\n\n");
432 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
433 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
434 " /dev/ttyc0 for the slcan application\n", argv[0]);
435 fprintf(stderr, "e.g. for pseudo-terminal '%s %s can0' creates"
436 " /dev/pts/N\n", argv[0], DEVICE_NAME_PTMX);
437 fprintf(stderr, "\n");
441 select_stdin = check_select_stdin();
444 p = open(argv[1], O_RDWR);
450 if (tcgetattr(p, &topts)) {
455 /* disable local echo which would cause double frames */
456 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
457 ECHONL | ECHOPRT | ECHOKE);
458 topts.c_iflag &= ~(ICRNL);
459 topts.c_iflag |= INLCR;
460 tcsetattr(p, TCSANOW, &topts);
462 /* Support for the Unix 98 pseudo-terminal interface /dev/ptmx /dev/pts/N */
463 if (strcmp(argv[1], DEVICE_NAME_PTMX) == 0) {
465 char *name_pts = NULL; /* slave pseudo-terminal device name */
467 if (grantpt(p) < 0) {
472 if (unlockpt(p) < 0) {
477 name_pts = ptsname(p);
478 if (name_pts == NULL) {
482 printf("open: %s: slave pseudo-terminal is %s\n", argv[1], name_pts);
486 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
492 addr.can_family = AF_CAN;
494 strcpy(ifr.ifr_name, argv[2]);
495 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
496 perror("SIOCGIFINDEX");
499 addr.can_ifindex = ifr.ifr_ifindex;
501 /* disable reception of CAN frames until we are opened by 'O' */
502 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
504 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
509 /* open filter by default */
523 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
528 if (FD_ISSET(0, &rdfs)) {
533 if (FD_ISSET(p, &rdfs))
534 if (pty2can(p, s, &fi, &is_open, &tstamp)) {
539 if (FD_ISSET(s, &rdfs))
540 if (can2pty(p, s, &tstamp)) {