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];
183 for (tmp = 0; tmp < nbytes; tmp++)
184 if (rxbuf[tmp] == '\r')
191 /* check for filter configuration commands */
192 if (rxcmd == 'm' || rxcmd == 'M') {
193 rxbuf[9] = 0; /* terminate filter string */
196 /* the filter is no SocketCAN filter :-( */
198 /* TODO: behave like a SJA1000 filter */
201 fi.can_id = strtoul(rxbuf+1,NULL,16);
202 fi.can_id &= CAN_EFF_MASK;
204 fi.can_mask = strtoul(rxbuf+1,NULL,16);
205 fi.can_mask &= CAN_EFF_MASK;
208 /* set only when both values are defined */
210 setsockopt(s, SOL_CAN_RAW,
212 sizeof(struct can_filter));
218 /* check for timestamp on/off command */
220 tstamp = rxbuf[1] & 0x01;
225 /* check for 'O'pen command */
227 setsockopt(s, SOL_CAN_RAW,
229 sizeof(struct can_filter));
235 /* check for 'C'lose command */
237 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER,
244 /* check for 'V'ersion command */
246 sprintf(replybuf, "V1013\r");
247 tmp = strlen(replybuf);
252 /* check for serial 'N'umber command */
254 sprintf(replybuf, "N4242\r");
255 tmp = strlen(replybuf);
260 /* check for read status 'F'lags */
262 sprintf(replybuf, "F00\r");
263 tmp = strlen(replybuf);
268 /* correctly answer unsupported commands */
281 if (rxcmd == 'P' || rxcmd == 'A') {
293 /* catch unknown commands */
294 if ((rxcmd != 't') && (rxcmd != 'T') &&
295 (rxcmd != 'r') && (rxcmd != 'R')) {
300 if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
301 rxp = 4; /* dlc position tiiid */
303 rxp = 9; /* dlc position Tiiiiiiiid */
305 if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
308 rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
310 rxbuf[rxp] = 0; /* terminate can_id string */
312 rxf.can_id = strtoul(rxbuf+1, NULL, 16);
314 if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
315 rxf.can_id |= CAN_EFF_FLAG;
317 if ((rxcmd | 0x20) == 'r') /* RTR frame */
318 rxf.can_id |= CAN_RTR_FLAG;
320 *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
322 for (i = 0, rxp++; i < rxf.can_dlc; i++) {
324 tmp = asc2nibble(rxbuf[rxp++]);
327 rxf.data[i] = (tmp << 4);
328 tmp = asc2nibble(rxbuf[rxp++]);
333 /* point to last real data */
337 nbytes = write(s, &rxf, sizeof(rxf));
338 if (nbytes != sizeof(rxf)) {
339 perror("write socket");
351 tmp = write(p, replybuf, tmp);
353 perror("write pty replybuf");
357 /* check if there is another command in this buffer */
358 if (nbytes > rxp+1) {
359 for (tmp = 0, rxp++; rxp+tmp < nbytes; tmp++)
360 rxbuf[tmp] = rxbuf[rxp+tmp];
366 if (FD_ISSET(s, &rdfs)) {
367 /* read txframe from CAN interface */
368 nbytes = read(s, &txf, sizeof(txf));
369 if (nbytes != sizeof(txf)) {
370 perror("read socket");
374 /* convert to slcan ASCII txf */
375 if (txf.can_id & CAN_RTR_FLAG)
376 txcmd = 'R'; /* becomes 'r' in SFF format */
378 txcmd = 'T'; /* becomes 't' in SFF format */
380 if (txf.can_id & CAN_EFF_FLAG)
381 sprintf(txbuf, "%c%08X%d", txcmd,
382 txf.can_id & CAN_EFF_MASK,
385 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
386 txf.can_id & CAN_SFF_MASK,
391 for (i = 0; i < txf.can_dlc; i++)
392 sprintf(&txbuf[txp + 2*i], "%02X",
398 if (ioctl(s, SIOCGSTAMP, &tv) < 0)
399 perror("SIOCGSTAMP");
401 sprintf(&txbuf[txp + 2*txf.can_dlc], "%04lX",
402 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
405 strcat(txbuf, "\r"); /* add terminating character */
406 nbytes = write(p, txbuf, strlen(txbuf));