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>
33 #include <sys/socket.h>
34 #include <sys/ioctl.h>
36 #include <sys/types.h>
38 #include <linux/can.h>
39 #include <linux/can/raw.h>
41 /* maximum rx buffer len: extended CAN frame with timestamp */
42 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
46 static int asc2nibble(char c)
49 if ((c >= '0') && (c <= '9'))
52 if ((c >= 'A') && (c <= 'F'))
55 if ((c >= 'a') && (c <= 'f'))
58 return 16; /* error */
61 /* read data from pty, send CAN frames to CAN socket and answer commands */
62 int pty2can(int pty, int socket, struct can_filter *fi,
63 int *is_open, int *tstamp)
68 char replybuf[10]; /* for answers to received commands */
70 struct can_frame frame;
73 nbytes = read(pty, &buf, sizeof(buf)-1);
80 /* remove trailing '\r' characters to be robust against some apps */
81 while (buf[0] == '\r' && nbytes > 0) {
82 for (tmp = 0; tmp < nbytes; tmp++)
83 buf[tmp] = buf[tmp+1];
94 for (tmp = 0; tmp < nbytes; tmp++)
102 /* check for filter configuration commands */
103 if (cmd == 'm' || cmd == 'M') {
104 buf[9] = 0; /* terminate filter string */
107 /* the filter is no SocketCAN filter :-( */
109 /* TODO: behave like a SJA1000 controller specific filter */
112 fi->can_id = strtoul(buf+1,NULL,16);
113 fi->can_id &= CAN_EFF_MASK;
115 fi->can_mask = strtoul(buf+1,NULL,16);
116 fi->can_mask &= CAN_EFF_MASK;
120 setsockopt(socket, SOL_CAN_RAW,
122 sizeof(struct can_filter));
128 /* check for timestamp on/off command */
130 *tstamp = buf[1] & 0x01;
135 /* check for 'O'pen command */
137 setsockopt(socket, SOL_CAN_RAW,
139 sizeof(struct can_filter));
145 /* check for 'C'lose command */
147 setsockopt(socket, SOL_CAN_RAW, CAN_RAW_FILTER,
154 /* check for 'V'ersion command */
156 sprintf(replybuf, "V1013\r");
157 tmp = strlen(replybuf);
162 /* check for serial 'N'umber command */
164 sprintf(replybuf, "N4242\r");
165 tmp = strlen(replybuf);
170 /* check for read status 'F'lags */
172 sprintf(replybuf, "F00\r");
173 tmp = strlen(replybuf);
178 /* correctly answer unsupported commands */
191 if (cmd == 'P' || cmd == 'A') {
203 /* catch unknown commands */
204 if ((cmd != 't') && (cmd != 'T') &&
205 (cmd != 'r') && (cmd != 'R')) {
210 if (cmd & 0x20) /* tiny chars 'r' 't' => SFF */
211 ptr = 4; /* dlc position tiiid */
213 ptr = 9; /* dlc position Tiiiiiiiid */
215 *(unsigned long long *) (&frame.data) = 0ULL; /* clear data[] */
217 if ((cmd | 0x20) == 'r' && buf[ptr] != '0') {
220 * RTR frame without dlc information!
221 * This is against the SLCAN spec but sent
222 * by a commercial CAN tool ... so we are
223 * robust against this protocol violation.
226 frame.can_dlc = buf[ptr]; /* save following byte */
228 buf[ptr] = 0; /* terminate can_id string */
230 frame.can_id = strtoul(buf+1, NULL, 16);
231 frame.can_id |= CAN_RTR_FLAG;
233 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
234 frame.can_id |= CAN_EFF_FLAG;
236 buf[ptr] = frame.can_dlc; /* restore following byte */
238 ptr--; /* we have no dlc component in the violation case */
242 if (!(buf[ptr] >= '0' && buf[ptr] < '9'))
245 frame.can_dlc = buf[ptr] - '0'; /* get dlc from ASCII val */
247 buf[ptr] = 0; /* terminate can_id string */
249 frame.can_id = strtoul(buf+1, NULL, 16);
251 if (!(cmd & 0x20)) /* NO tiny chars => EFF */
252 frame.can_id |= CAN_EFF_FLAG;
254 if ((cmd | 0x20) == 'r') /* RTR frame */
255 frame.can_id |= CAN_RTR_FLAG;
257 for (i = 0, ptr++; i < frame.can_dlc; i++) {
259 tmp = asc2nibble(buf[ptr++]);
262 frame.data[i] = (tmp << 4);
263 tmp = asc2nibble(buf[ptr++]);
266 frame.data[i] |= tmp;
268 /* point to last real data */
273 tmp = write(socket, &frame, sizeof(frame));
274 if (tmp != sizeof(frame)) {
275 perror("write socket");
287 tmp = write(pty, replybuf, tmp);
289 perror("write pty replybuf");
293 /* check if there is another command in this buffer */
294 if (nbytes > ptr+1) {
295 for (tmp = 0, ptr++; ptr+tmp < nbytes; tmp++)
296 buf[tmp] = buf[ptr+tmp];
304 /* read CAN frames from CAN interface and write it to the pty */
305 int can2pty(int pty, int socket, int *tstamp)
311 struct can_frame frame;
314 nbytes = read(socket, &frame, sizeof(frame));
315 if (nbytes != sizeof(frame)) {
316 perror("read socket");
320 /* convert to slcan ASCII frame */
321 if (frame.can_id & CAN_RTR_FLAG)
322 cmd = 'R'; /* becomes 'r' in SFF format */
324 cmd = 'T'; /* becomes 't' in SFF format */
326 if (frame.can_id & CAN_EFF_FLAG)
327 sprintf(buf, "%c%08X%d", cmd,
328 frame.can_id & CAN_EFF_MASK,
331 sprintf(buf, "%c%03X%d", cmd | 0x20,
332 frame.can_id & CAN_SFF_MASK,
337 for (i = 0; i < frame.can_dlc; i++)
338 sprintf(&buf[ptr + 2*i], "%02X",
344 if (ioctl(socket, SIOCGSTAMP, &tv) < 0)
345 perror("SIOCGSTAMP");
347 sprintf(&buf[ptr + 2*frame.can_dlc], "%04lX",
348 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
351 strcat(buf, "\r"); /* add terminating character */
352 nbytes = write(pty, buf, strlen(buf));
363 int main(int argc, char **argv)
366 int p; /* pty master file */
367 int s; /* can raw socket */
368 struct sockaddr_can addr;
369 struct termios topts;
374 struct can_filter fi;
376 /* check command line options */
378 fprintf(stderr, "\n");
379 fprintf(stderr, "%s creates a pty for applications using"
380 " the slcan ASCII protocol and\n", argv[0]);
381 fprintf(stderr, "converts the ASCII data to a CAN network"
382 " interface (and vice versa)\n\n");
383 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
384 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
385 " /dev/ttyc0 for the slcan application\n", argv[0]);
386 fprintf(stderr, "\n");
391 p = open(argv[1], O_RDWR);
397 if (tcgetattr(p, &topts)) {
402 /* disable local echo which would cause double frames */
403 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
404 ECHONL | ECHOPRT | ECHOKE | ICRNL);
405 tcsetattr(p, TCSANOW, &topts);
408 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
414 addr.can_family = AF_CAN;
416 strcpy(ifr.ifr_name, argv[2]);
417 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
418 perror("SIOCGIFINDEX");
421 addr.can_ifindex = ifr.ifr_ifindex;
423 /* disable reception of CAN frames until we are opened by 'O' */
424 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
426 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
431 /* open filter by default */
442 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
447 if (FD_ISSET(0, &rdfs)) {
452 if (FD_ISSET(p, &rdfs))
453 if (pty2can(p, s, &fi, &is_open, &tstamp)) {
458 if (FD_ISSET(s, &rdfs))
459 if (can2pty(p, s, &tstamp)) {