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;
77 nbytes = read(pty, &buf, sizeof(buf)-1);
84 /* remove trailing '\r' characters to be robust against some apps */
85 while (buf[0] == '\r' && nbytes > 0) {
86 for (tmp = 0; tmp < nbytes; tmp++)
87 buf[tmp] = buf[tmp+1];
98 for (tmp = 0; tmp < nbytes; tmp++)
106 /* check for filter configuration commands */
107 if (cmd == 'm' || cmd == 'M') {
108 buf[9] = 0; /* terminate filter string */
111 /* the filter is no SocketCAN filter :-( */
113 /* TODO: behave like a SJA1000 controller specific filter */
116 fi->can_id = strtoul(buf+1,NULL,16);
117 fi->can_id &= CAN_EFF_MASK;
119 fi->can_mask = strtoul(buf+1,NULL,16);
120 fi->can_mask &= CAN_EFF_MASK;
124 setsockopt(socket, SOL_CAN_RAW,
126 sizeof(struct can_filter));
132 /* check for timestamp on/off command */
134 *tstamp = buf[1] & 0x01;
139 /* check for 'O'pen command */
141 setsockopt(socket, SOL_CAN_RAW,
143 sizeof(struct can_filter));
149 /* check for 'C'lose command */
151 setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FILTER,
158 /* check for 'V'ersion command */
160 sprintf(replybuf, "V1013\r");
161 tmp = strlen(replybuf);
166 /* check for serial 'N'umber command */
168 sprintf(replybuf, "N4242\r");
169 tmp = strlen(replybuf);
174 /* check for read status 'F'lags */
176 sprintf(replybuf, "F00\r");
177 tmp = strlen(replybuf);
182 /* correctly answer unsupported commands */
195 if (cmd == 'P' || cmd == 'A') {
207 /* catch unknown commands */
208 if ((cmd != 't') && (cmd != 'T') &&
209 (cmd != 'r') && (cmd != 'R')) {
214 if (cmd & 0x20) /* tiny chars 'r' 't' => SFF */
215 ptr = 4; /* dlc position tiiid */
217 ptr = 9; /* dlc position Tiiiiiiiid */
219 *(unsigned long long *) (&frame.data) = 0ULL; /* clear data[] */
221 if ((cmd | 0x20) == 'r' && buf[ptr] != '0') {
224 * RTR frame without dlc information!
225 * This is against the SLCAN spec but sent
226 * by a commercial CAN tool ... so we are
227 * robust against this protocol violation.
230 frame.can_dlc = buf[ptr]; /* save following byte */
232 buf[ptr] = 0; /* terminate can_id string */
234 frame.can_id = strtoul(buf+1, NULL, 16);
235 frame.can_id |= CAN_RTR_FLAG;
237 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
238 frame.can_id |= CAN_EFF_FLAG;
240 buf[ptr] = frame.can_dlc; /* restore following byte */
242 ptr--; /* we have no dlc component in the violation case */
246 if (!(buf[ptr] >= '0' && buf[ptr] < '9'))
249 frame.can_dlc = buf[ptr] - '0'; /* get dlc from ASCII val */
251 buf[ptr] = 0; /* terminate can_id string */
253 frame.can_id = strtoul(buf+1, NULL, 16);
255 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
256 frame.can_id |= CAN_EFF_FLAG;
258 if ((cmd | 0x20) == 'r') /* RTR frame */
259 frame.can_id |= CAN_RTR_FLAG;
261 for (i = 0, ptr++; i < frame.can_dlc; i++) {
263 tmp = asc2nibble(buf[ptr++]);
266 frame.data[i] = (tmp << 4);
267 tmp = asc2nibble(buf[ptr++]);
270 frame.data[i] |= tmp;
272 /* point to last real data */
277 tmp = write(socket, &frame, sizeof(frame));
278 if (tmp != sizeof(frame)) {
279 perror("write socket");
291 tmp = write(pty, replybuf, tmp);
293 perror("write pty replybuf");
297 /* check if there is another command in this buffer */
298 if (nbytes > ptr+1) {
299 for (tmp = 0, ptr++; ptr+tmp < nbytes; tmp++)
300 buf[tmp] = buf[ptr+tmp];
308 /* read CAN frames from CAN interface and write it to the pty */
309 int can2pty(int pty, int socket, int *tstamp)
315 struct can_frame frame;
318 nbytes = read(socket, &frame, sizeof(frame));
319 if (nbytes != sizeof(frame)) {
320 perror("read socket");
324 /* convert to slcan ASCII frame */
325 if (frame.can_id & CAN_RTR_FLAG)
326 cmd = 'R'; /* becomes 'r' in SFF format */
328 cmd = 'T'; /* becomes 't' in SFF format */
330 if (frame.can_id & CAN_EFF_FLAG)
331 sprintf(buf, "%c%08X%d", cmd,
332 frame.can_id & CAN_EFF_MASK,
335 sprintf(buf, "%c%03X%d", cmd | 0x20,
336 frame.can_id & CAN_SFF_MASK,
341 for (i = 0; i < frame.can_dlc; i++)
342 sprintf(&buf[ptr + 2*i], "%02X",
348 if (ioctl(socket, SIOCGSTAMP, &tv) < 0)
349 perror("SIOCGSTAMP");
351 sprintf(&buf[ptr + 2*frame.can_dlc], "%04lX",
352 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
355 strcat(buf, "\r"); /* add terminating character */
356 nbytes = write(pty, buf, strlen(buf));
367 int main(int argc, char **argv)
370 int p; /* pty master file */
371 int s; /* can raw socket */
372 struct sockaddr_can addr;
373 struct termios topts;
378 struct can_filter fi;
380 /* check command line options */
382 fprintf(stderr, "\n");
383 fprintf(stderr, "%s creates a pty for applications using"
384 " the slcan ASCII protocol and\n", argv[0]);
385 fprintf(stderr, "converts the ASCII data to a CAN network"
386 " interface (and vice versa)\n\n");
387 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
388 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
389 " /dev/ttyc0 for the slcan application\n", argv[0]);
390 fprintf(stderr, "e.g. for pseudo-terminal '%s %s can0' creates"
391 " /dev/pts/N\n", argv[0], DEVICE_NAME_PTMX);
392 fprintf(stderr, "\n");
397 p = open(argv[1], O_RDWR);
403 if (tcgetattr(p, &topts)) {
408 /* disable local echo which would cause double frames */
409 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
410 ECHONL | ECHOPRT | ECHOKE | ICRNL);
411 tcsetattr(p, TCSANOW, &topts);
413 /* Support for the Unix 98 pseudo-terminal interface /dev/ptmx /dev/pts/N */
414 if (strcmp(argv[1], DEVICE_NAME_PTMX) == 0) {
416 char *name_pts = NULL; /* slave pseudo-terminal device name */
418 if (grantpt(p) < 0) {
423 if (unlockpt(p) < 0) {
428 name_pts = ptsname(p);
429 if (name_pts == NULL) {
433 printf("open: %s: slave pseudo-terminal is %s\n", argv[1], name_pts);
437 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
443 addr.can_family = AF_CAN;
445 strcpy(ifr.ifr_name, argv[2]);
446 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
447 perror("SIOCGIFINDEX");
450 addr.can_ifindex = ifr.ifr_ifindex;
452 /* disable reception of CAN frames until we are opened by 'O' */
453 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
455 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
460 /* open filter by default */
471 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
476 if (FD_ISSET(0, &rdfs)) {
481 if (FD_ISSET(p, &rdfs))
482 if (pty2can(p, s, &fi, &is_open, &tstamp)) {
487 if (FD_ISSET(s, &rdfs))
488 if (can2pty(p, s, &tstamp)) {