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 int main(int argc, char **argv)
67 int p; /* pty master file */
68 int s; /* can raw socket */
70 struct sockaddr_can addr;
79 char replybuf[SLC_MTU];
81 struct can_frame txf, rxf;
85 /* check command line options */
87 fprintf(stderr, "\n");
88 fprintf(stderr, "%s creates a pty for applications using"
89 " the slcan ASCII protocol and\n", argv[0]);
90 fprintf(stderr, "converts the ASCII data to a CAN network"
91 " interface (and vice versa)\n\n");
92 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
93 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
94 " /dev/ttyc0 for the slcan application\n", argv[0]);
95 fprintf(stderr, "\n");
100 p = open(argv[1], O_RDWR);
106 if (tcgetattr(p, &topts)) {
111 /* disable local echo which would cause double frames */
112 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
113 ECHONL | ECHOPRT | ECHOKE | ICRNL);
114 tcsetattr(p, TCSANOW, &topts);
117 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
123 addr.can_family = AF_CAN;
125 strcpy(ifr.ifr_name, argv[2]);
126 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
127 perror("SIOCGIFINDEX");
130 addr.can_ifindex = ifr.ifr_ifindex;
132 /* disable reception of CAN frames until we are opened by 'O' */
133 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
135 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
140 /* open filter by default */
151 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
156 if (FD_ISSET(0, &rdfs)) {
161 if (FD_ISSET(p, &rdfs)) {
162 /* read rxdata from pty */
163 nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
170 /* remove trailing '\r' characters */
171 while (rxbuf[0] == '\r' && nbytes > 0) {
172 for (tmp = 0; tmp < nbytes; tmp++)
173 rxbuf[tmp] = rxbuf[tmp+1];
184 for (tmp = 0; tmp < nbytes; tmp++)
185 if (rxbuf[tmp] == '\r')
192 /* check for filter configuration commands */
193 if (rxcmd == 'm' || rxcmd == 'M') {
194 rxbuf[9] = 0; /* terminate filter string */
197 /* the filter is no SocketCAN filter :-( */
199 /* TODO: behave like a SJA1000 filter */
202 fi.can_id = strtoul(rxbuf+1,NULL,16);
203 fi.can_id &= CAN_EFF_MASK;
205 fi.can_mask = strtoul(rxbuf+1,NULL,16);
206 fi.can_mask &= CAN_EFF_MASK;
209 /* set only when both values are defined */
211 setsockopt(s, SOL_CAN_RAW,
213 sizeof(struct can_filter));
219 /* check for timestamp on/off command */
221 tstamp = rxbuf[1] & 0x01;
226 /* check for 'O'pen command */
228 setsockopt(s, SOL_CAN_RAW,
230 sizeof(struct can_filter));
236 /* check for 'C'lose command */
238 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER,
245 /* check for 'V'ersion command */
247 sprintf(replybuf, "V1013\r");
248 tmp = strlen(replybuf);
253 /* check for serial 'N'umber command */
255 sprintf(replybuf, "N4242\r");
256 tmp = strlen(replybuf);
261 /* check for read status 'F'lags */
263 sprintf(replybuf, "F00\r");
264 tmp = strlen(replybuf);
269 /* correctly answer unsupported commands */
282 if (rxcmd == 'P' || rxcmd == 'A') {
294 /* catch unknown commands */
295 if ((rxcmd != 't') && (rxcmd != 'T') &&
296 (rxcmd != 'r') && (rxcmd != 'R')) {
301 if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
302 rxp = 4; /* dlc position tiiid */
304 rxp = 9; /* dlc position Tiiiiiiiid */
306 *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
308 if ((rxcmd | 0x20) == 'r' && rxbuf[rxp] != '0') {
309 /* RTR frame without dlc information */
311 rxf.can_dlc = rxbuf[rxp]; /* save */
313 rxbuf[rxp] = 0; /* terminate can_id string */
315 rxf.can_id = strtoul(rxbuf+1, NULL, 16);
316 rxf.can_id |= CAN_RTR_FLAG;
318 if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
319 rxf.can_id |= CAN_EFF_FLAG;
321 rxbuf[rxp] = rxf.can_dlc; /* restore */
323 rxp--; /* we have no dlc component here */
327 if (!(rxbuf[rxp] >= '0' && rxbuf[rxp] < '9'))
330 rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get dlc */
332 rxbuf[rxp] = 0; /* terminate can_id string */
334 rxf.can_id = strtoul(rxbuf+1, NULL, 16);
336 if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
337 rxf.can_id |= CAN_EFF_FLAG;
339 if ((rxcmd | 0x20) == 'r') /* RTR frame */
340 rxf.can_id |= CAN_RTR_FLAG;
342 for (i = 0, rxp++; i < rxf.can_dlc; i++) {
344 tmp = asc2nibble(rxbuf[rxp++]);
347 rxf.data[i] = (tmp << 4);
348 tmp = asc2nibble(rxbuf[rxp++]);
353 /* point to last real data */
358 nbytes = write(s, &rxf, sizeof(rxf));
359 if (nbytes != sizeof(rxf)) {
360 perror("write socket");
372 tmp = write(p, replybuf, tmp);
374 perror("write pty replybuf");
378 /* check if there is another command in this buffer */
379 if (nbytes > rxp+1) {
380 for (tmp = 0, rxp++; rxp+tmp < nbytes; tmp++)
381 rxbuf[tmp] = rxbuf[rxp+tmp];
387 if (FD_ISSET(s, &rdfs)) {
388 /* read txframe from CAN interface */
389 nbytes = read(s, &txf, sizeof(txf));
390 if (nbytes != sizeof(txf)) {
391 perror("read socket");
395 /* convert to slcan ASCII txf */
396 if (txf.can_id & CAN_RTR_FLAG)
397 txcmd = 'R'; /* becomes 'r' in SFF format */
399 txcmd = 'T'; /* becomes 't' in SFF format */
401 if (txf.can_id & CAN_EFF_FLAG)
402 sprintf(txbuf, "%c%08X%d", txcmd,
403 txf.can_id & CAN_EFF_MASK,
406 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
407 txf.can_id & CAN_SFF_MASK,
412 for (i = 0; i < txf.can_dlc; i++)
413 sprintf(&txbuf[txp + 2*i], "%02X",
419 if (ioctl(s, SIOCGSTAMP, &tv) < 0)
420 perror("SIOCGSTAMP");
422 sprintf(&txbuf[txp + 2*txf.can_dlc], "%04lX",
423 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
426 strcat(txbuf, "\r"); /* add terminating character */
427 nbytes = write(p, txbuf, strlen(txbuf));