]> rtime.felk.cvut.cz Git - can-utils.git/blob - slcanpty.c
Some more implementation to make it work with a certain application.
[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 #define DEBUG
48
49 static int asc2nibble(char c)
50 {
51
52         if ((c >= '0') && (c <= '9'))
53                 return c - '0';
54
55         if ((c >= 'A') && (c <= 'F'))
56                 return c - 'A' + 10;
57
58         if ((c >= 'a') && (c <= 'f'))
59                 return c - 'a' + 10;
60
61         return 16; /* error */
62 }
63
64 int main(int argc, char **argv)
65 {
66         fd_set rdfs;
67         int p; /* pty master file */ 
68         int s; /* can raw socket */ 
69         int nbytes;
70         struct sockaddr_can addr;
71         struct termios topts;
72         struct ifreq ifr;
73         int running = 1;
74         int tstamp = 0;
75         int is_open = 0;
76         char txcmd, rxcmd;
77         char txbuf[SLC_MTU];
78         char rxbuf[200];
79         char replybuf[SLC_MTU];
80         int txp, rxp;
81         struct can_frame txf, rxf;
82         struct can_filter fi;
83         int tmp, i;
84
85         /* check command line options */
86         if (argc != 3) {
87                 fprintf(stderr, "\n");
88                 fprintf(stderr, "%s creates a pty for applications using"
89                         " the slcan ASCII protocol and\n", argv[0]);
90                 fprintf(stderr, "converts the ASCII data to a CAN network"
91                         " interface (and vice versa)\n\n");
92                 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
93                 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
94                         " /dev/ttyc0 for the slcan application\n", argv[0]);
95                 fprintf(stderr, "\n");
96                 return 1;
97         }
98
99         /* open pty */
100         p = open(argv[1], O_RDWR);
101         if (p < 0) {
102                 perror("open pty");
103                 return 1;
104         }
105
106         if (tcgetattr(p, &topts)) {
107                 perror("tcgetattr");
108                 return 1;
109         }
110
111         /* disable local echo which would cause double frames */
112         topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
113                            ECHONL | ECHOPRT | ECHOKE | ICRNL);
114         tcsetattr(p, TCSANOW, &topts);
115
116         /* open socket */
117         s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
118         if (s < 0) {
119                 perror("socket");
120                 return 1;
121         }
122
123         addr.can_family = AF_CAN;
124
125         strcpy(ifr.ifr_name, argv[2]);
126         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
127                 perror("SIOCGIFINDEX");
128                 return 1;
129         }
130         addr.can_ifindex = ifr.ifr_ifindex;
131
132         /* disable reception of CAN frames until we are opened by 'O' */
133         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
134
135         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
136                 perror("bind");
137                 return 1;
138         }
139
140         /* open filter by default */
141         fi.can_id   = 0;
142         fi.can_mask = 0;
143
144         while (running) {
145
146                 FD_ZERO(&rdfs);
147                 FD_SET(0, &rdfs);
148                 FD_SET(p, &rdfs);
149                 FD_SET(s, &rdfs);
150
151                 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
152                         perror("select");
153                         return 1;
154                 }
155
156                 if (FD_ISSET(0, &rdfs)) {
157                         running = 0;
158                         continue;
159                 }
160
161                 if (FD_ISSET(p, &rdfs)) {
162                         /* read rxdata from pty */
163                         nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
164                         if (nbytes < 0) {
165                                 perror("read pty");
166                                 return 1;
167                         }
168
169 rx_restart:
170                         /* remove trailing '\r' characters */
171                         while (rxbuf[0] == '\r' && nbytes > 0) {
172                                 for (tmp = 0; tmp < nbytes; tmp++)
173                                         rxbuf[tmp] = rxbuf[tmp+1];
174                                 nbytes--;
175                         }
176
177                         if (nbytes < 2)
178                                 continue;
179
180                         rxcmd = rxbuf[0];
181
182 #ifdef DEBUG
183                         for (tmp = 0; tmp < nbytes; tmp++)
184                                 if (rxbuf[tmp] == '\r')
185                                         putchar('@');
186                                 else
187                                         putchar(rxbuf[tmp]);
188                         printf("\n");
189 #endif
190
191                         /* check for filter configuration commands */
192                         if (rxcmd == 'm' || rxcmd == 'M') {
193                                 rxbuf[9] = 0; /* terminate filter string */
194                                 rxp = 9;
195 #if 0
196                                 /* the filter is no SocketCAN filter :-( */
197
198                                 /* TODO: behave like a SJA1000 filter */
199
200                                 if (rxcmd == 'm') {
201                                         fi.can_id = strtoul(rxbuf+1,NULL,16);
202                                         fi.can_id &= CAN_EFF_MASK;
203                                 } else {
204                                         fi.can_mask = strtoul(rxbuf+1,NULL,16);
205                                         fi.can_mask &= CAN_EFF_MASK;
206                                 }
207
208                                 /* set only when both values are defined */
209                                 if (is_open)
210                                         setsockopt(s, SOL_CAN_RAW,
211                                                    CAN_RAW_FILTER, &fi,
212                                                    sizeof(struct can_filter));
213 #endif
214                                 goto rx_out_ack;
215                         }
216
217
218                         /* check for timestamp on/off command */
219                         if (rxcmd == 'Z') {
220                                 tstamp = rxbuf[1] & 0x01;
221                                 rxp = 2;
222                                 goto rx_out_ack;
223                         }
224
225                         /* check for 'O'pen command */
226                         if (rxcmd == 'O') {
227                                 setsockopt(s, SOL_CAN_RAW,
228                                            CAN_RAW_FILTER, &fi,
229                                            sizeof(struct can_filter));
230                                 rxp = 1;
231                                 is_open = 1;
232                                 goto rx_out_ack;
233                         }
234
235                         /* check for 'C'lose command */
236                         if (rxcmd == 'C') {
237                                 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER,
238                                            NULL, 0);
239                                 rxp = 1;
240                                 is_open = 0;
241                                 goto rx_out_ack;
242                         }
243
244                         /* check for 'V'ersion command */
245                         if (rxcmd == 'V') {
246                                 sprintf(replybuf, "V1013\r");
247                                 tmp = strlen(replybuf);
248                                 rxp = 1;
249                                 goto rx_out;
250                         }
251
252                         /* check for serial 'N'umber command */
253                         if (rxcmd == 'N') {
254                                 sprintf(replybuf, "N4242\r");
255                                 tmp = strlen(replybuf);
256                                 rxp = 1;
257                                 goto rx_out;
258                         }
259
260                         /* check for read status 'F'lags */
261                         if (rxcmd == 'F') {
262                                 sprintf(replybuf, "F00\r");
263                                 tmp = strlen(replybuf);
264                                 rxp = 1;
265                                 goto rx_out;
266                         }
267
268                         /* correctly answer unsupported commands */
269                         if (rxcmd == 'U') {
270                                 rxp = 2;
271                                 goto rx_out_ack;
272                         }
273                         if (rxcmd == 'S') {
274                                 rxp = 2;
275                                 goto rx_out_ack;
276                         }
277                         if (rxcmd == 's') {
278                                 rxp = 5;
279                                 goto rx_out_ack;
280                         }
281                         if (rxcmd == 'P' || rxcmd == 'A') {
282                                 rxp = 1;
283                                 goto rx_out_nack;
284                         }
285                         if (rxcmd == 'X') {
286                                 rxp = 2;
287                                 if (rxbuf[1] & 0x01)
288                                         goto rx_out_ack;
289                                 else
290                                         goto rx_out_nack;
291                         }
292
293                         /* catch unknown commands */
294                         if ((rxcmd != 't') && (rxcmd != 'T') &&
295                             (rxcmd != 'r') && (rxcmd != 'R')) {
296                                 rxp = nbytes-1;
297                                 goto rx_out_nack;
298                         }
299
300                         if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
301                                 rxp = 4; /* dlc position tiiid */
302                         else
303                                 rxp = 9; /* dlc position Tiiiiiiiid */
304
305                         if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
306                                 goto rx_out_nack;
307
308                         rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
309
310                         rxbuf[rxp] = 0; /* terminate can_id string */
311
312                         rxf.can_id = strtoul(rxbuf+1, NULL, 16);
313
314                         if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
315                                 rxf.can_id |= CAN_EFF_FLAG;
316
317                         if ((rxcmd | 0x20) == 'r') /* RTR frame */
318                                 rxf.can_id |= CAN_RTR_FLAG;
319
320                         *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
321
322                         for (i = 0, rxp++; i < rxf.can_dlc; i++) {
323
324                                 tmp = asc2nibble(rxbuf[rxp++]);
325                                 if (tmp > 0x0F)
326                                         goto rx_out_nack;
327                                 rxf.data[i] = (tmp << 4);
328                                 tmp = asc2nibble(rxbuf[rxp++]);
329                                 if (tmp > 0x0F)
330                                         goto rx_out_nack;
331                                 rxf.data[i] |= tmp;
332                         }
333                         /* point to last real data */
334                         if (rxf.can_dlc)
335                                 rxp--;
336
337                         nbytes = write(s, &rxf, sizeof(rxf));
338                         if (nbytes != sizeof(rxf)) {
339                                 perror("write socket");
340                                 return 1;
341                         }
342
343 rx_out_ack:
344                         replybuf[0] = '\r';
345                         tmp = 1;
346                         goto rx_out;
347 rx_out_nack:
348                         replybuf[0] = '\a';
349                         tmp = 1;
350 rx_out:
351                         tmp = write(p, replybuf, tmp);
352                         if (tmp < 0) {
353                                 perror("write pty replybuf");
354                                 return 1;
355                         }
356
357                         /* check if there is another command in this buffer */
358                         if (nbytes > rxp+1) {
359                                 for (tmp = 0, rxp++; rxp+tmp < nbytes; tmp++)
360                                         rxbuf[tmp] = rxbuf[rxp+tmp];
361                                 nbytes = tmp;
362                                 goto rx_restart;
363                         }
364                 }
365
366                 if (FD_ISSET(s, &rdfs)) {
367                         /* read txframe from CAN interface */
368                         nbytes = read(s, &txf, sizeof(txf));
369                         if (nbytes != sizeof(txf)) {
370                                 perror("read socket");
371                                 return 1;
372                         }
373
374                         /* convert to slcan ASCII txf */
375                         if (txf.can_id & CAN_RTR_FLAG)
376                                 txcmd = 'R'; /* becomes 'r' in SFF format */
377                         else
378                                 txcmd = 'T'; /* becomes 't' in SFF format */
379
380                         if (txf.can_id & CAN_EFF_FLAG)
381                                 sprintf(txbuf, "%c%08X%d", txcmd,
382                                         txf.can_id & CAN_EFF_MASK,
383                                         txf.can_dlc);
384                         else
385                                 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
386                                         txf.can_id & CAN_SFF_MASK,
387                                         txf.can_dlc);
388
389                         txp = strlen(txbuf);
390
391                         for (i = 0; i < txf.can_dlc; i++)
392                                 sprintf(&txbuf[txp + 2*i], "%02X",
393                                         txf.data[i]);
394
395                         if (tstamp) {
396                                 struct timeval tv;
397
398                                 if (ioctl(s, SIOCGSTAMP, &tv) < 0)
399                                         perror("SIOCGSTAMP");
400
401                                 sprintf(&txbuf[txp + 2*txf.can_dlc], "%04lX",
402                                         (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
403                         }
404
405                         strcat(txbuf, "\r"); /* add terminating character */
406                         nbytes = write(p, txbuf, strlen(txbuf));
407                         if (nbytes < 0) {
408                                 perror("write pty");
409                                 return 1;
410                         }
411                         fflush(NULL);
412                 }
413         }
414
415         close(p);
416         close(s);
417
418         return 0;
419 }