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)
47 static int asc2nibble(char c)
50 if ((c >= '0') && (c <= '9'))
53 if ((c >= 'A') && (c <= 'F'))
56 if ((c >= 'a') && (c <= 'f'))
59 return 16; /* error */
62 int main(int argc, char **argv)
65 int p; /* pty master file */
66 int s; /* can raw socket */
68 struct sockaddr_can addr;
78 struct can_frame txf, rxf;
82 /* check command line options */
84 fprintf(stderr, "\n");
85 fprintf(stderr, "%s creates a pty for applications using"
86 " the slcan ASCII protocol and\n", argv[0]);
87 fprintf(stderr, "converts the ASCII data to a CAN network"
88 " interface (and vice versa)\n\n");
89 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
90 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
91 " /dev/ttyc0 for the slcan application\n", argv[0]);
92 fprintf(stderr, "\n");
97 p = open(argv[1], O_RDWR);
103 if (tcgetattr(p, &topts)) {
108 /* disable local echo which would cause double frames */
109 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
110 ECHONL | ECHOPRT | ECHOKE | ICRNL);
111 tcsetattr(p, TCSANOW, &topts);
114 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
120 addr.can_family = AF_CAN;
122 strcpy(ifr.ifr_name, argv[2]);
123 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
124 perror("SIOCGIFINDEX");
127 addr.can_ifindex = ifr.ifr_ifindex;
129 /* disable reception of CAN frames until we are opened by 'O' */
130 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
132 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
137 /* open filter by default */
148 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
153 if (FD_ISSET(0, &rdfs)) {
158 if (FD_ISSET(p, &rdfs)) {
159 /* read rxdata from pty */
160 nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
166 /* convert to struct can_frame rxf */
169 /* check for filter configuration commands */
170 if (rxcmd == 'm' || rxcmd == 'M') {
171 rxbuf[9] = 0; /* terminate filter string */
174 fi.can_id = strtoul(rxbuf+1,NULL,16);
175 fi.can_id &= CAN_EFF_MASK;
177 fi.can_mask = strtoul(rxbuf+1,NULL,16);
178 fi.can_mask &= CAN_EFF_MASK;
181 /* set only when both values are defined */
183 setsockopt(s, SOL_CAN_RAW,
185 sizeof(struct can_filter));
189 /* check for timestamp on/off command */
191 tstamp = rxbuf[1] & 0x01;
195 /* check for 'O'pen command */
197 setsockopt(s, SOL_CAN_RAW,
199 sizeof(struct can_filter));
204 /* check for 'C'lose command */
206 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER,
212 if ((rxcmd != 't') && (rxcmd != 'T') &&
213 (rxcmd != 'r') && (rxcmd != 'R'))
216 if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
217 rxp = 4; /* dlc position tiiid */
219 rxp = 9; /* dlc position Tiiiiiiiid */
221 if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
224 rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
226 rxbuf[rxp] = 0; /* terminate can_id string */
228 rxf.can_id = strtoul(rxbuf+1, NULL, 16);
230 if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
231 rxf.can_id |= CAN_EFF_FLAG;
233 if ((rxcmd | 0x20) == 'r') /* RTR frame */
234 rxf.can_id |= CAN_RTR_FLAG;
236 *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
238 for (i = 0, rxp++; i < rxf.can_dlc; i++) {
240 tmp = asc2nibble(rxbuf[rxp++]);
243 rxf.data[i] = (tmp << 4);
244 tmp = asc2nibble(rxbuf[rxp++]);
250 nbytes = write(s, &rxf, sizeof(rxf));
251 if (nbytes != sizeof(rxf)) {
252 perror("write socket");
262 nbytes = write(p, &rxcmd, 1);
264 perror("write pty ack/nack");
269 if (FD_ISSET(s, &rdfs)) {
270 /* read txframe from CAN interface */
271 nbytes = read(s, &txf, sizeof(txf));
272 if (nbytes != sizeof(txf)) {
273 perror("read socket");
277 /* convert to slcan ASCII txf */
278 if (txf.can_id & CAN_RTR_FLAG)
279 txcmd = 'R'; /* becomes 'r' in SFF format */
281 txcmd = 'T'; /* becomes 't' in SFF format */
283 if (txf.can_id & CAN_EFF_FLAG)
284 sprintf(txbuf, "%c%08X%d", txcmd,
285 txf.can_id & CAN_EFF_MASK,
288 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
289 txf.can_id & CAN_SFF_MASK,
294 for (i = 0; i < txf.can_dlc; i++)
295 sprintf(&txbuf[txp + 2*i], "%02X",
301 if (ioctl(s, SIOCGSTAMP, &tv) < 0)
302 perror("SIOCGSTAMP");
304 sprintf(&txbuf[txp + 2*txf.can_dlc], "%04lX",
305 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
308 strcat(txbuf, "\r"); /* add terminating character */
309 nbytes = write(p, txbuf, strlen(txbuf));