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;
76 struct can_frame txf, rxf;
79 /* check command line options */
81 fprintf(stderr, "\n");
82 fprintf(stderr, "%s creates a pty for applications using"
83 " the slcan ASCII protocol and\n", argv[0]);
84 fprintf(stderr, "converts the ASCII data to a CAN network"
85 " interface (and vice versa)\n\n");
86 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
87 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
88 " /dev/ttyc0 for the slcan application\n", argv[0]);
89 fprintf(stderr, "\n");
94 p = open(argv[1], O_RDWR);
100 if (tcgetattr(p, &topts)) {
105 /* disable local echo which would cause double frames */
106 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
107 ECHONL | ECHOPRT | ECHOKE | ICRNL);
108 tcsetattr(p, TCSANOW, &topts);
111 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
117 addr.can_family = AF_CAN;
119 strcpy(ifr.ifr_name, argv[2]);
120 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
121 perror("SIOCGIFINDEX");
124 addr.can_ifindex = ifr.ifr_ifindex;
126 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
138 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
143 if (FD_ISSET(0, &rdfs)) {
148 if (FD_ISSET(p, &rdfs)) {
149 /* read rxdata from pty */
150 nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
156 /* convert to struct can_frame rxf */
159 if ((rxcmd != 't') && (rxcmd != 'T') &&
160 (rxcmd != 'r') && (rxcmd != 'R'))
163 if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
164 rxp = 4; /* dlc position tiiid */
166 rxp = 9; /* dlc position Tiiiiiiiid */
168 if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
171 rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
176 rxbuf[rxp] = 0; /* terminate can_id string */
178 rxf.can_id = strtoul(rxbuf+1, NULL, 16);
180 if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
181 rxf.can_id |= CAN_EFF_FLAG;
183 if ((rxcmd | 0x20) == 'r') /* RTR frame */
184 rxf.can_id |= CAN_RTR_FLAG;
186 *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
188 for (i = 0, rxp++; i < rxf.can_dlc; i++) {
190 tmp = asc2nibble(rxbuf[rxp++]);
193 rxf.data[i] = (tmp << 4);
194 tmp = asc2nibble(rxbuf[rxp++]);
200 nbytes = write(s, &rxf, sizeof(rxf));
201 if (nbytes != sizeof(rxf)) {
202 perror("write socket");
207 if (FD_ISSET(s, &rdfs)) {
208 /* read txframe from CAN interface */
209 nbytes = read(s, &txf, sizeof(txf));
210 if (nbytes != sizeof(txf)) {
211 perror("read socket");
215 /* convert to slcan ASCII txf */
216 if (txf.can_id & CAN_RTR_FLAG)
217 txcmd = 'R'; /* becomes 'r' in SFF format */
219 txcmd = 'T'; /* becomes 't' in SFF format */
221 if (txf.can_id & CAN_EFF_FLAG)
222 sprintf(txbuf, "%c%08X%d", txcmd,
223 txf.can_id & CAN_EFF_MASK,
226 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
227 txf.can_id & CAN_SFF_MASK,
232 for (i = 0; i < txf.can_dlc; i++)
233 sprintf(&txbuf[txp + 2*i], "%02X",
236 strcat(txbuf, "\r"); /* add terminating character */
237 nbytes = write(p, txbuf, strlen(txbuf));