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;
80 /* check command line options */
82 fprintf(stderr, "\n");
83 fprintf(stderr, "%s creates a pty for applications using"
84 " the slcan ASCII protocol and\n", argv[0]);
85 fprintf(stderr, "converts the ASCII data to a CAN network"
86 " interface (and vice versa)\n\n");
87 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
88 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
89 " /dev/ttyc0 for the slcan application\n", argv[0]);
90 fprintf(stderr, "\n");
95 p = open(argv[1], O_RDWR);
101 if (tcgetattr(p, &topts)) {
106 /* disable local echo which would cause double frames */
107 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
108 ECHONL | ECHOPRT | ECHOKE | ICRNL);
109 tcsetattr(p, TCSANOW, &topts);
112 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
118 addr.can_family = AF_CAN;
120 strcpy(ifr.ifr_name, argv[2]);
121 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
122 perror("SIOCGIFINDEX");
125 addr.can_ifindex = ifr.ifr_ifindex;
127 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
132 /* no filter content by default */
133 fi.can_id = CAN_ERR_FLAG;
134 fi.can_mask = CAN_ERR_FLAG;
143 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
148 if (FD_ISSET(0, &rdfs)) {
153 if (FD_ISSET(p, &rdfs)) {
154 /* read rxdata from pty */
155 nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
161 /* convert to struct can_frame rxf */
164 /* check for filter configuration commands */
165 if (rxcmd == 'm' || rxcmd == 'M') {
166 rxbuf[9] = 0; /* terminate filter string */
169 fi.can_id = strtoul(rxbuf+1,NULL,16);
170 fi.can_id &= CAN_EFF_MASK;
172 fi.can_mask = strtoul(rxbuf+1,NULL,16);
173 fi.can_mask &= CAN_EFF_MASK;
176 /* set only when both values are defined */
177 if (fi.can_id != CAN_ERR_FLAG &&
178 fi.can_mask != CAN_ERR_FLAG)
179 setsockopt(s, SOL_CAN_RAW,
181 sizeof(struct can_filter));
184 if ((rxcmd != 't') && (rxcmd != 'T') &&
185 (rxcmd != 'r') && (rxcmd != 'R'))
188 if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
189 rxp = 4; /* dlc position tiiid */
191 rxp = 9; /* dlc position Tiiiiiiiid */
193 if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
196 rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
198 rxbuf[rxp] = 0; /* terminate can_id string */
200 rxf.can_id = strtoul(rxbuf+1, NULL, 16);
202 if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
203 rxf.can_id |= CAN_EFF_FLAG;
205 if ((rxcmd | 0x20) == 'r') /* RTR frame */
206 rxf.can_id |= CAN_RTR_FLAG;
208 *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
210 for (i = 0, rxp++; i < rxf.can_dlc; i++) {
212 tmp = asc2nibble(rxbuf[rxp++]);
215 rxf.data[i] = (tmp << 4);
216 tmp = asc2nibble(rxbuf[rxp++]);
222 nbytes = write(s, &rxf, sizeof(rxf));
223 if (nbytes != sizeof(rxf)) {
224 perror("write socket");
229 if (FD_ISSET(s, &rdfs)) {
230 /* read txframe from CAN interface */
231 nbytes = read(s, &txf, sizeof(txf));
232 if (nbytes != sizeof(txf)) {
233 perror("read socket");
237 /* convert to slcan ASCII txf */
238 if (txf.can_id & CAN_RTR_FLAG)
239 txcmd = 'R'; /* becomes 'r' in SFF format */
241 txcmd = 'T'; /* becomes 't' in SFF format */
243 if (txf.can_id & CAN_EFF_FLAG)
244 sprintf(txbuf, "%c%08X%d", txcmd,
245 txf.can_id & CAN_EFF_MASK,
248 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
249 txf.can_id & CAN_SFF_MASK,
254 for (i = 0; i < txf.can_dlc; i++)
255 sprintf(&txbuf[txp + 2*i], "%02X",
258 strcat(txbuf, "\r"); /* add terminating character */
259 nbytes = write(p, txbuf, strlen(txbuf));