]> rtime.felk.cvut.cz Git - can-utils.git/blob - slcanpty.c
Added a proof of concept tool 'slcanpty' which
[can-utils.git] / slcanpty.c
1 /*
2  *  $Id$
3  */
4
5 /*
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)
8  *
9  * Copyright (c)2009 Oliver Hartkopp
10  *
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.
15  *
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.
20  *
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
24  *
25  * Send feedback to <socketcan-users@lists.berlios.de>
26  *
27  */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <fcntl.h>
34 #include <termios.h>
35
36 #include <net/if.h>
37 #include <sys/ioctl.h>
38 #include <sys/stat.h>
39 #include <sys/types.h>
40
41 #include <linux/can.h>
42 #include <linux/can/raw.h>
43
44 /* maximum rx buffer len: extended CAN frame with timestamp */
45 #define SLC_MTU (sizeof("T1111222281122334455667788EA5F\r")+1)
46
47 static int asc2nibble(char c)
48 {
49
50         if ((c >= '0') && (c <= '9'))
51                 return c - '0';
52
53         if ((c >= 'A') && (c <= 'F'))
54                 return c - 'A' + 10;
55
56         if ((c >= 'a') && (c <= 'f'))
57                 return c - 'a' + 10;
58
59         return 16; /* error */
60 }
61
62 int main(int argc, char **argv)
63 {
64         fd_set rdfs;
65         int p; /* pty master file */ 
66         int s; /* can raw socket */ 
67         int nbytes;
68         struct sockaddr_can addr;
69         struct termios topts;
70         struct ifreq ifr;
71         int running = 1;
72         char txcmd, rxcmd;
73         char txbuf[SLC_MTU];
74         char rxbuf[SLC_MTU];
75         int txp, rxp;
76         struct can_frame txf, rxf;
77         int tmp, i;
78
79         /* check command line options */
80         if (argc != 3) {
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");
90                 return 1;
91         }
92
93         /* open pty */
94         p = open(argv[1], O_RDWR);
95         if (p < 0) {
96                 perror("open pty");
97                 return 1;
98         }
99
100         if (tcgetattr(p, &topts)) {
101                 perror("tcgetattr");
102                 return 1;
103         }
104
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);
109
110         /* open socket */
111         s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
112         if (s < 0) {
113                 perror("socket");
114                 return 1;
115         }
116
117         addr.can_family = AF_CAN;
118
119         strcpy(ifr.ifr_name, argv[2]);
120         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
121                 perror("SIOCGIFINDEX");
122                 return 1;
123         }
124         addr.can_ifindex = ifr.ifr_ifindex;
125
126         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
127                 perror("bind");
128                 return 1;
129         }
130
131         while (running) {
132
133                 FD_ZERO(&rdfs);
134                 FD_SET(0, &rdfs);
135                 FD_SET(p, &rdfs);
136                 FD_SET(s, &rdfs);
137
138                 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
139                         perror("select");
140                         return 1;
141                 }
142
143                 if (FD_ISSET(0, &rdfs)) {
144                         running = 0;
145                         continue;
146                 }
147
148                 if (FD_ISSET(p, &rdfs)) {
149                         /* read rxdata from pty */
150                         nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
151                         if (nbytes < 0) {
152                                 perror("read pty");
153                                 return 1;
154                         }
155
156                         /* convert to struct can_frame rxf */
157                         rxcmd = rxbuf[0];
158
159                         if ((rxcmd != 't') && (rxcmd != 'T') &&
160                             (rxcmd != 'r') && (rxcmd != 'R'))
161                                 continue;
162
163                         if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
164                                 rxp = 4; /* dlc position tiiid */
165                         else
166                                 rxp = 9; /* dlc position Tiiiiiiiid */
167
168                         if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
169                                 continue;
170
171                         rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
172
173                         if (rxf.can_dlc > 8)
174                                 continue;
175
176                         rxbuf[rxp] = 0; /* terminate can_id string */
177
178                         rxf.can_id = strtoul(rxbuf+1, NULL, 16);
179
180                         if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
181                                 rxf.can_id |= CAN_EFF_FLAG;
182
183                         if ((rxcmd | 0x20) == 'r') /* RTR frame */
184                                 rxf.can_id |= CAN_RTR_FLAG;
185
186                         *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
187
188                         for (i = 0, rxp++; i < rxf.can_dlc; i++) {
189
190                                 tmp = asc2nibble(rxbuf[rxp++]);
191                                 if (tmp > 0x0F)
192                                         continue;
193                                 rxf.data[i] = (tmp << 4);
194                                 tmp = asc2nibble(rxbuf[rxp++]);
195                                 if (tmp > 0x0F)
196                                         continue;
197                                 rxf.data[i] |= tmp;
198                         }
199
200                         nbytes = write(s, &rxf, sizeof(rxf));
201                         if (nbytes != sizeof(rxf)) {
202                                 perror("write socket");
203                                 return 1;
204                         }
205                 }
206
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");
212                                 return 1;
213                         }
214
215                         /* convert to slcan ASCII txf */
216                         if (txf.can_id & CAN_RTR_FLAG)
217                                 txcmd = 'R'; /* becomes 'r' in SFF format */
218                         else
219                                 txcmd = 'T'; /* becomes 't' in SFF format */
220
221                         if (txf.can_id & CAN_EFF_FLAG)
222                                 sprintf(txbuf, "%c%08X%d", txcmd,
223                                         txf.can_id & CAN_EFF_MASK,
224                                         txf.can_dlc);
225                         else
226                                 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
227                                         txf.can_id & CAN_SFF_MASK,
228                                         txf.can_dlc);
229
230                         txp = strlen(txbuf);
231
232                         for (i = 0; i < txf.can_dlc; i++)
233                                 sprintf(&txbuf[txp + 2*i], "%02X",
234                                         txf.data[i]);
235
236                         strcat(txbuf, "\r"); /* add terminating character */
237                         nbytes = write(p, txbuf, strlen(txbuf));
238                         if (nbytes < 0) {
239                                 perror("write pty");
240                                 return 1;
241                         }
242                         fflush(NULL);
243                 }
244         }
245
246         close(p);
247         close(s);
248
249         return 0;
250 }