6 * slcanpty.c - creates a pty for applications using the slcan ASCII protocol
7 * and converts the ASCII data to a CAN network interface (and vice versa)
9 * Copyright (c)2009 Oliver Hartkopp
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, write to the Free Software
23 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25 * Send feedback to <socketcan-users@lists.berlios.de>
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)
49 static int asc2nibble(char c)
52 if ((c >= '0') && (c <= '9'))
55 if ((c >= 'A') && (c <= 'F'))
58 if ((c >= 'a') && (c <= 'f'))
61 return 16; /* error */
64 /* read data from pty, send CAN frames to CAN socket and answer commands */
65 int pty2can(int pty, int socket, struct can_filter *fi,
66 int *is_open, int *tstamp)
71 char replybuf[10]; /* for answers to received commands */
73 struct can_frame frame;
76 nbytes = read(pty, &buf, sizeof(buf)-1);
83 /* remove trailing '\r' characters to be robust against some apps */
84 while (buf[0] == '\r' && nbytes > 0) {
85 for (tmp = 0; tmp < nbytes; tmp++)
86 buf[tmp] = buf[tmp+1];
97 for (tmp = 0; tmp < nbytes; tmp++)
105 /* check for filter configuration commands */
106 if (cmd == 'm' || cmd == 'M') {
107 buf[9] = 0; /* terminate filter string */
110 /* the filter is no SocketCAN filter :-( */
112 /* TODO: behave like a SJA1000 controller specific filter */
115 fi->can_id = strtoul(buf+1,NULL,16);
116 fi->can_id &= CAN_EFF_MASK;
118 fi->can_mask = strtoul(buf+1,NULL,16);
119 fi->can_mask &= CAN_EFF_MASK;
123 setsockopt(socket, SOL_CAN_RAW,
125 sizeof(struct can_filter));
131 /* check for timestamp on/off command */
133 *tstamp = buf[1] & 0x01;
138 /* check for 'O'pen command */
140 setsockopt(socket, SOL_CAN_RAW,
142 sizeof(struct can_filter));
148 /* check for 'C'lose command */
150 setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FILTER,
157 /* check for 'V'ersion command */
159 sprintf(replybuf, "V1013\r");
160 tmp = strlen(replybuf);
165 /* check for serial 'N'umber command */
167 sprintf(replybuf, "N4242\r");
168 tmp = strlen(replybuf);
173 /* check for read status 'F'lags */
175 sprintf(replybuf, "F00\r");
176 tmp = strlen(replybuf);
181 /* correctly answer unsupported commands */
194 if (cmd == 'P' || cmd == 'A') {
206 /* catch unknown commands */
207 if ((cmd != 't') && (cmd != 'T') &&
208 (cmd != 'r') && (cmd != 'R')) {
213 if (cmd & 0x20) /* tiny chars 'r' 't' => SFF */
214 ptr = 4; /* dlc position tiiid */
216 ptr = 9; /* dlc position Tiiiiiiiid */
218 *(unsigned long long *) (&frame.data) = 0ULL; /* clear data[] */
220 if ((cmd | 0x20) == 'r' && buf[ptr] != '0') {
223 * RTR frame without dlc information!
224 * This is against the SLCAN spec but sent
225 * by a commercial CAN tool ... so we are
226 * robust against this protocol violation.
229 frame.can_dlc = buf[ptr]; /* save following byte */
231 buf[ptr] = 0; /* terminate can_id string */
233 frame.can_id = strtoul(buf+1, NULL, 16);
234 frame.can_id |= CAN_RTR_FLAG;
236 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
237 frame.can_id |= CAN_EFF_FLAG;
239 buf[ptr] = frame.can_dlc; /* restore following byte */
241 ptr--; /* we have no dlc component in the violation case */
245 if (!(buf[ptr] >= '0' && buf[ptr] < '9'))
248 frame.can_dlc = buf[ptr] - '0'; /* get dlc from ASCII val */
250 buf[ptr] = 0; /* terminate can_id string */
252 frame.can_id = strtoul(buf+1, NULL, 16);
254 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
255 frame.can_id |= CAN_EFF_FLAG;
257 if ((cmd | 0x20) == 'r') /* RTR frame */
258 frame.can_id |= CAN_RTR_FLAG;
260 for (i = 0, ptr++; i < frame.can_dlc; i++) {
262 tmp = asc2nibble(buf[ptr++]);
265 frame.data[i] = (tmp << 4);
266 tmp = asc2nibble(buf[ptr++]);
269 frame.data[i] |= tmp;
271 /* point to last real data */
276 nbytes = write(socket, &frame, sizeof(frame));
277 if (nbytes != sizeof(frame)) {
278 perror("write socket");
290 tmp = write(pty, replybuf, tmp);
292 perror("write pty replybuf");
296 /* check if there is another command in this buffer */
297 if (nbytes > ptr+1) {
298 for (tmp = 0, ptr++; ptr+tmp < nbytes; tmp++)
299 buf[tmp] = buf[ptr+tmp];
307 /* read CAN frames from CAN interface and write it to the pty */
308 int can2pty(int pty, int socket, int *tstamp)
314 struct can_frame frame;
317 nbytes = read(socket, &frame, sizeof(frame));
318 if (nbytes != sizeof(frame)) {
319 perror("read socket");
323 /* convert to slcan ASCII frame */
324 if (frame.can_id & CAN_RTR_FLAG)
325 cmd = 'R'; /* becomes 'r' in SFF format */
327 cmd = 'T'; /* becomes 't' in SFF format */
329 if (frame.can_id & CAN_EFF_FLAG)
330 sprintf(buf, "%c%08X%d", cmd,
331 frame.can_id & CAN_EFF_MASK,
334 sprintf(buf, "%c%03X%d", cmd | 0x20,
335 frame.can_id & CAN_SFF_MASK,
340 for (i = 0; i < frame.can_dlc; i++)
341 sprintf(&buf[ptr + 2*i], "%02X",
347 if (ioctl(socket, SIOCGSTAMP, &tv) < 0)
348 perror("SIOCGSTAMP");
350 sprintf(&buf[ptr + 2*frame.can_dlc], "%04lX",
351 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
354 strcat(buf, "\r"); /* add terminating character */
355 nbytes = write(pty, buf, strlen(buf));
366 int main(int argc, char **argv)
369 int p; /* pty master file */
370 int s; /* can raw socket */
371 struct sockaddr_can addr;
372 struct termios topts;
377 struct can_filter fi;
379 /* check command line options */
381 fprintf(stderr, "\n");
382 fprintf(stderr, "%s creates a pty for applications using"
383 " the slcan ASCII protocol and\n", argv[0]);
384 fprintf(stderr, "converts the ASCII data to a CAN network"
385 " interface (and vice versa)\n\n");
386 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
387 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
388 " /dev/ttyc0 for the slcan application\n", argv[0]);
389 fprintf(stderr, "\n");
394 p = open(argv[1], O_RDWR);
400 if (tcgetattr(p, &topts)) {
405 /* disable local echo which would cause double frames */
406 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
407 ECHONL | ECHOPRT | ECHOKE | ICRNL);
408 tcsetattr(p, TCSANOW, &topts);
411 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
417 addr.can_family = AF_CAN;
419 strcpy(ifr.ifr_name, argv[2]);
420 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
421 perror("SIOCGIFINDEX");
424 addr.can_ifindex = ifr.ifr_ifindex;
426 /* disable reception of CAN frames until we are opened by 'O' */
427 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
429 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
434 /* open filter by default */
445 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
450 if (FD_ISSET(0, &rdfs)) {
455 if (FD_ISSET(p, &rdfs))
456 if (pty2can(p, s, &fi, &is_open, &tstamp)) {
461 if (FD_ISSET(s, &rdfs))
462 if (can2pty(p, s, &tstamp)) {