]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - slcanpty.c
Add can_id/can_mask filter handling which is defined in the slcan protocol (m/M).
[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         char txcmd, rxcmd;
73         char txbuf[SLC_MTU];
74         char rxbuf[SLC_MTU];
75         int txp, rxp;
76         struct can_frame txf, rxf;
77         struct can_filter fi;
78         int tmp, i;
79
80         /* check command line options */
81         if (argc != 3) {
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");
91                 return 1;
92         }
93
94         /* open pty */
95         p = open(argv[1], O_RDWR);
96         if (p < 0) {
97                 perror("open pty");
98                 return 1;
99         }
100
101         if (tcgetattr(p, &topts)) {
102                 perror("tcgetattr");
103                 return 1;
104         }
105
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);
110
111         /* open socket */
112         s = socket(PF_CAN, SOCK_RAW, CAN_RAW);
113         if (s < 0) {
114                 perror("socket");
115                 return 1;
116         }
117
118         addr.can_family = AF_CAN;
119
120         strcpy(ifr.ifr_name, argv[2]);
121         if (ioctl(s, SIOCGIFINDEX, &ifr) < 0) {
122                 perror("SIOCGIFINDEX");
123                 return 1;
124         }
125         addr.can_ifindex = ifr.ifr_ifindex;
126
127         if (bind(s, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
128                 perror("bind");
129                 return 1;
130         }
131
132         /* no filter content by default */
133         fi.can_id   = CAN_ERR_FLAG;
134         fi.can_mask = CAN_ERR_FLAG;
135
136         while (running) {
137
138                 FD_ZERO(&rdfs);
139                 FD_SET(0, &rdfs);
140                 FD_SET(p, &rdfs);
141                 FD_SET(s, &rdfs);
142
143                 if (select(s+1, &rdfs, NULL, NULL, NULL) < 0) {
144                         perror("select");
145                         return 1;
146                 }
147
148                 if (FD_ISSET(0, &rdfs)) {
149                         running = 0;
150                         continue;
151                 }
152
153                 if (FD_ISSET(p, &rdfs)) {
154                         /* read rxdata from pty */
155                         nbytes = read(p, &rxbuf, sizeof(rxbuf)-1);
156                         if (nbytes < 0) {
157                                 perror("read pty");
158                                 return 1;
159                         }
160
161                         /* convert to struct can_frame rxf */
162                         rxcmd = rxbuf[0];
163
164                         /* check for filter configuration commands */
165                         if (rxcmd == 'm' || rxcmd == 'M') {
166                                 rxbuf[9] = 0; /* terminate filter string */
167
168                                 if (rxcmd == 'm') {
169                                         fi.can_id = strtoul(rxbuf+1,NULL,16);
170                                         fi.can_id &= CAN_EFF_MASK;
171                                 } else {
172                                         fi.can_mask = strtoul(rxbuf+1,NULL,16);
173                                         fi.can_mask &= CAN_EFF_MASK;
174                                 }
175
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,
180                                                    CAN_RAW_FILTER, &fi,
181                                                    sizeof(struct can_filter));
182                         }
183
184                         if ((rxcmd != 't') && (rxcmd != 'T') &&
185                             (rxcmd != 'r') && (rxcmd != 'R'))
186                                 continue;
187
188                         if (rxcmd & 0x20) /* tiny chars 'r' 't' => SFF */
189                                 rxp = 4; /* dlc position tiiid */
190                         else
191                                 rxp = 9; /* dlc position Tiiiiiiiid */
192
193                         if (!((rxbuf[rxp] >= '0') && (rxbuf[rxp] < '9')))
194                                 continue;
195
196                         rxf.can_dlc = rxbuf[rxp] & 0x0F; /* get can_dlc */
197
198                         rxbuf[rxp] = 0; /* terminate can_id string */
199
200                         rxf.can_id = strtoul(rxbuf+1, NULL, 16);
201
202                         if (!(rxcmd & 0x20)) /* NO tiny chars => EFF */
203                                 rxf.can_id |= CAN_EFF_FLAG;
204
205                         if ((rxcmd | 0x20) == 'r') /* RTR frame */
206                                 rxf.can_id |= CAN_RTR_FLAG;
207
208                         *(unsigned long long *) (&rxf.data) = 0ULL; /* clear */
209
210                         for (i = 0, rxp++; i < rxf.can_dlc; i++) {
211
212                                 tmp = asc2nibble(rxbuf[rxp++]);
213                                 if (tmp > 0x0F)
214                                         continue;
215                                 rxf.data[i] = (tmp << 4);
216                                 tmp = asc2nibble(rxbuf[rxp++]);
217                                 if (tmp > 0x0F)
218                                         continue;
219                                 rxf.data[i] |= tmp;
220                         }
221
222                         nbytes = write(s, &rxf, sizeof(rxf));
223                         if (nbytes != sizeof(rxf)) {
224                                 perror("write socket");
225                                 return 1;
226                         }
227                 }
228
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");
234                                 return 1;
235                         }
236
237                         /* convert to slcan ASCII txf */
238                         if (txf.can_id & CAN_RTR_FLAG)
239                                 txcmd = 'R'; /* becomes 'r' in SFF format */
240                         else
241                                 txcmd = 'T'; /* becomes 't' in SFF format */
242
243                         if (txf.can_id & CAN_EFF_FLAG)
244                                 sprintf(txbuf, "%c%08X%d", txcmd,
245                                         txf.can_id & CAN_EFF_MASK,
246                                         txf.can_dlc);
247                         else
248                                 sprintf(txbuf, "%c%03X%d", txcmd | 0x20,
249                                         txf.can_id & CAN_SFF_MASK,
250                                         txf.can_dlc);
251
252                         txp = strlen(txbuf);
253
254                         for (i = 0; i < txf.can_dlc; i++)
255                                 sprintf(&txbuf[txp + 2*i], "%02X",
256                                         txf.data[i]);
257
258                         strcat(txbuf, "\r"); /* add terminating character */
259                         nbytes = write(p, txbuf, strlen(txbuf));
260                         if (nbytes < 0) {
261                                 perror("write pty");
262                                 return 1;
263                         }
264                         fflush(NULL);
265                 }
266         }
267
268         close(p);
269         close(s);
270
271         return 0;
272 }