]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - slcanpty.c
Implemented ACK/NACK replies for ASCII commands.
[sojka/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         int tstamp = 0;
73         int is_open = 0;
74         char txcmd, rxcmd;
75         char txbuf[SLC_MTU];
76         char rxbuf[SLC_MTU];
77         int txp, rxp;
78         struct can_frame txf, rxf;
79         struct can_filter fi;
80         int tmp, i;
81
82         /* check command line options */
83         if (argc != 3) {
84                 fprintf(stderr, "\n");
85                 fprintf(stderr, "%s creates a pty for applications using"
86                         " the slcan ASCII protocol and\n", argv[0]);
87                 fprintf(stderr, "converts the ASCII data to a CAN network"
88                         " interface (and vice versa)\n\n");
89                 fprintf(stderr, "Usage: %s <pty> <can interface>\n", argv[0]);
90                 fprintf(stderr, "e.g. '%s /dev/ptyc0 can0' creates"
91                         " /dev/ttyc0 for the slcan application\n", argv[0]);
92                 fprintf(stderr, "\n");
93                 return 1;
94         }
95
96         /* open pty */
97         p = open(argv[1], O_RDWR);
98         if (p < 0) {
99                 perror("open pty");
100                 return 1;
101         }
102
103         if (tcgetattr(p, &topts)) {
104                 perror("tcgetattr");
105                 return 1;
106         }
107
108         /* disable local echo which would cause double frames */
109         topts.c_lflag &= ~(ICANON | ECHO | ECHOE | ECHOK |
110                            ECHONL | ECHOPRT | ECHOKE | ICRNL);
111         tcsetattr(p, TCSANOW, &topts);
112
113         /* open socket */
114         s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
115         if (s < 0) {
116                 perror("socket");
117                 return 1;
118         }
119
120         addr.can_family = AF_CAN;
121
122         strcpy(ifr.ifr_name, argv[2]);
123         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
124                 perror("SIOCGIFINDEX");
125                 return 1;
126         }
127         addr.can_ifindex = ifr.ifr_ifindex;
128
129         /* disable reception of CAN frames until we are opened by 'O' */
130         setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER, NULL, 0);
131
132         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
133                 perror("bind");
134                 return 1;
135         }
136
137         /* open filter by default */
138         fi.can_id   = 0;
139         fi.can_mask = 0;
140
141         while (running) {
142
143                 FD_ZERO(&rdfs);
144                 FD_SET(0, &rdfs);
145                 FD_SET(p, &rdfs);
146                 FD_SET(s, &rdfs);
147
148                 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
149                         perror("select");
150                         return 1;
151                 }
152
153                 if (FD_ISSET(0, &rdfs)) {
154                         running = 0;
155                         continue;
156                 }
157
158                 if (FD_ISSET(p, &rdfs)) {
159                         /* read rxdata from pty */
160                         nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
161                         if (nbytes < 0) {
162                                 perror("read pty");
163                                 return 1;
164                         }
165
166                         /* convert to struct can_frame rxf */
167                         rxcmd = rxbuf[0];
168
169                         /* check for filter configuration commands */
170                         if (rxcmd == 'm' || rxcmd == 'M') {
171                                 rxbuf[9] = 0; /* terminate filter string */
172
173                                 if (rxcmd == 'm') {
174                                         fi.can_id = strtoul(rxbuf+1,NULL,16);
175                                         fi.can_id &= CAN_EFF_MASK;
176                                 } else {
177                                         fi.can_mask = strtoul(rxbuf+1,NULL,16);
178                                         fi.can_mask &= CAN_EFF_MASK;
179                                 }
180
181                                 /* set only when both values are defined */
182                                 if (is_open)
183                                         setsockopt(s, SOL_CAN_RAW,
184                                                    CAN_RAW_FILTER, &fi,
185                                                    sizeof(struct can_filter));
186                                 goto rx_out_ack;
187                         }
188
189                         /* check for timestamp on/off command */
190                         if (rxcmd == 'Z') {
191                                 tstamp = rxbuf[1] & 0x01;
192                                 goto rx_out_ack;
193                         }
194
195                         /* check for 'O'pen command */
196                         if (rxcmd == 'O') {
197                                 setsockopt(s, SOL_CAN_RAW,
198                                            CAN_RAW_FILTER, &fi,
199                                            sizeof(struct can_filter));
200                                 is_open = 1;
201                                 goto rx_out_ack;
202                         }
203
204                         /* check for 'C'lose command */
205                         if (rxcmd == 'C') {
206                                 setsockopt(s, SOL_CAN_RAW, CAN_RAW_FILTER,
207                                            NULL, 0);
208                                 is_open = 0;
209                                 goto rx_out_ack;
210                         }
211
212                         if ((rxcmd != 't') && (rxcmd != 'T') &&
213                             (rxcmd != 'r') && (rxcmd != 'R'))
214                                 goto rx_out_ack;
215
216                         if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
217                                 rxp = 4; /* dlc position tiiid */
218                         else
219                                 rxp = 9; /* dlc position Tiiiiiiiid */
220
221                         if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
222                                 goto rx_out_nack;
223
224                         rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
225
226                         rxbuf[rxp] = 0; /* terminate can_id string */
227
228                         rxf.can_id = strtoul(rxbuf+1, NULL, 16);
229
230                         if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
231                                 rxf.can_id |= CAN_EFF_FLAG;
232
233                         if ((rxcmd | 0x20) == 'r') /* RTR frame */
234                                 rxf.can_id |= CAN_RTR_FLAG;
235
236                         *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
237
238                         for (i = 0, rxp++; i < rxf.can_dlc; i++) {
239
240                                 tmp = asc2nibble(rxbuf[rxp++]);
241                                 if (tmp > 0x0F)
242                                         goto rx_out_nack;
243                                 rxf.data[i] = (tmp << 4);
244                                 tmp = asc2nibble(rxbuf[rxp++]);
245                                 if (tmp > 0x0F)
246                                         goto rx_out_nack;
247                                 rxf.data[i] |= tmp;
248                         }
249
250                         nbytes = write(s, &rxf, sizeof(rxf));
251                         if (nbytes != sizeof(rxf)) {
252                                 perror("write socket");
253                                 return 1;
254                         }
255
256 rx_out_ack:
257                         rxcmd = '\r';
258                         goto rx_out;
259 rx_out_nack:
260                         rxcmd = '\a';
261 rx_out:
262                         nbytes = write(p, &rxcmd, 1);
263                         if (nbytes < 0) {
264                                 perror("write pty ack/nack");
265                                 return 1;
266                         }
267                 }
268
269                 if (FD_ISSET(s, &rdfs)) {
270                         /* read txframe from CAN interface */
271                         nbytes = read(s, &txf, sizeof(txf));
272                         if (nbytes != sizeof(txf)) {
273                                 perror("read socket");
274                                 return 1;
275                         }
276
277                         /* convert to slcan ASCII txf */
278                         if (txf.can_id & CAN_RTR_FLAG)
279                                 txcmd = 'R'; /* becomes 'r' in SFF format */
280                         else
281                                 txcmd = 'T'; /* becomes 't' in SFF format */
282
283                         if (txf.can_id & CAN_EFF_FLAG)
284                                 sprintf(txbuf, "%c%08X%d", txcmd,
285                                         txf.can_id & CAN_EFF_MASK,
286                                         txf.can_dlc);
287                         else
288                                 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
289                                         txf.can_id & CAN_SFF_MASK,
290                                         txf.can_dlc);
291
292                         txp = strlen(txbuf);
293
294                         for (i = 0; i < txf.can_dlc; i++)
295                                 sprintf(&txbuf[txp + 2*i], "%02X",
296                                         txf.data[i]);
297
298                         if (tstamp) {
299                                 struct timeval tv;
300
301                                 if (ioctl(s, SIOCGSTAMP, &tv) < 0)
302                                         perror("SIOCGSTAMP");
303
304                                 sprintf(&txbuf[txp + 2*txf.can_dlc], "%04lX",
305                                         (tv.tv_sec%60)*1000 + tv.tv_usec/1000);
306                         }
307
308                         strcat(txbuf, "\r"); /* add terminating character */
309                         nbytes = write(p, txbuf, strlen(txbuf));
310                         if (nbytes < 0) {
311                                 perror("write pty");
312                                 return 1;
313                         }
314                         fflush(NULL);
315                 }
316         }
317
318         close(p);
319         close(s);
320
321         return 0;
322 }