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