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;
77 struct can_frame txf, rxf;
81 /* check command line options */
83 fprintf(stderr, "\n");
84 fprintf(stderr, "%s creates a pty for applications using"
85 " the slcan ASCII protocol and\n", argv[0]);
86 fprintf(stderr, "converts the ASCII data to a CAN network"
87 " interface (and vice versa)\n\n");
88 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
89 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
90 " /dev/ttyc0 for the slcan application\n", argv[0]);
91 fprintf(stderr, "\n");
96 p = open(argv[1], O_RDWR);
102 if (tcgetattr(p, &topts)) {
107 /* disable local echo which would cause double frames */
108 topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
109 ECHONL | ECHOPRT | ECHOKE | ICRNL);
110 tcsetattr(p, TCSANOW, &topts);
113 s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
119 addr.can_family = AF_CAN;
121 strcpy(ifr.ifr_name, argv[2]);
122 if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
123 perror("SIOCGIFINDEX");
126 addr.can_ifindex = ifr.ifr_ifindex;
128 if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
133 /* no filter content by default */
134 fi.can_id = CAN_ERR_FLAG;
135 fi.can_mask = CAN_ERR_FLAG;
144 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
149 if (FD_ISSET(0, &rdfs)) {
154 if (FD_ISSET(p, &rdfs)) {
155 /* read rxdata from pty */
156 nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
162 /* convert to struct can_frame rxf */
165 /* check for filter configuration commands */
166 if (rxcmd == 'm' || rxcmd == 'M') {
167 rxbuf[9] = 0; /* terminate filter string */
170 fi.can_id = strtoul(rxbuf+1,NULL,16);
171 fi.can_id &= CAN_EFF_MASK;
173 fi.can_mask = strtoul(rxbuf+1,NULL,16);
174 fi.can_mask &= CAN_EFF_MASK;
177 /* set only when both values are defined */
178 if (fi.can_id != CAN_ERR_FLAG &&
179 fi.can_mask != CAN_ERR_FLAG)
180 setsockopt(s, SOL_CAN_RAW,
182 sizeof(struct can_filter));
186 /* check for timestamp on/off command */
188 tstamp = rxbuf[1] & 0x01;
192 if ((rxcmd != 't') && (rxcmd != 'T') &&
193 (rxcmd != 'r') && (rxcmd != 'R'))
196 if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
197 rxp = 4; /* dlc position tiiid */
199 rxp = 9; /* dlc position Tiiiiiiiid */
201 if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
204 rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
206 rxbuf[rxp] = 0; /* terminate can_id string */
208 rxf.can_id = strtoul(rxbuf+1, NULL, 16);
210 if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
211 rxf.can_id |= CAN_EFF_FLAG;
213 if ((rxcmd | 0x20) == 'r') /* RTR frame */
214 rxf.can_id |= CAN_RTR_FLAG;
216 *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
218 for (i = 0, rxp++; i < rxf.can_dlc; i++) {
220 tmp = asc2nibble(rxbuf[rxp++]);
223 rxf.data[i] = (tmp << 4);
224 tmp = asc2nibble(rxbuf[rxp++]);
230 nbytes = write(s, &rxf, sizeof(rxf));
231 if (nbytes != sizeof(rxf)) {
232 perror("write socket");
237 if (FD_ISSET(s, &rdfs)) {
238 /* read txframe from CAN interface */
239 nbytes = read(s, &txf, sizeof(txf));
240 if (nbytes != sizeof(txf)) {
241 perror("read socket");
245 /* convert to slcan ASCII txf */
246 if (txf.can_id & CAN_RTR_FLAG)
247 txcmd = 'R'; /* becomes 'r' in SFF format */
249 txcmd = 'T'; /* becomes 't' in SFF format */
251 if (txf.can_id & CAN_EFF_FLAG)
252 sprintf(txbuf, "%c%08X%d", txcmd,
253 txf.can_id & CAN_EFF_MASK,
256 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
257 txf.can_id & CAN_SFF_MASK,
262 for (i = 0; i < txf.can_dlc; i++)
263 sprintf(&txbuf[txp + 2*i], "%02X",
269 if (ioctl(s, SIOCGSTAMP, &tv) < 0)
270 perror("SIOCGSTAMP");
272 sprintf(&txbuf[txp + 2*txf.can_dlc], "%04lX",
273 (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
276 strcat(txbuf, "\r"); /* add terminating character */
277 nbytes = write(p, txbuf, strlen(txbuf));