]> rtime.felk.cvut.cz Git - sojka/can-utils.git/blob - lib.c
cangen: Added '-n <count>' commandline option analogue to candump tool.
[sojka/can-utils.git] / lib.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * lib.c - library for command line tools
7  *
8  * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
9  * All rights reserved.
10  *
11  * Redistribution and use in source and binary forms, with or without
12  * modification, are permitted provided that the following conditions
13  * are met:
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer.
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in the
18  *    documentation and/or other materials provided with the distribution.
19  * 3. Neither the name of Volkswagen nor the names of its contributors
20  *    may be used to endorse or promote products derived from this software
21  *    without specific prior written permission.
22  *
23  * Alternatively, provided that this notice is retained in full, this
24  * software may be distributed under the terms of the GNU General
25  * Public License ("GPL") version 2, in which case the provisions of the
26  * GPL apply INSTEAD OF those given above.
27  *
28  * The provided data structures and external interfaces from this code
29  * are not restricted to be used by modules with a GPL compatible license.
30  *
31  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
32  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
33  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
34  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
35  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
36  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
37  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
38  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
39  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
40  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
42  * DAMAGE.
43  *
44  * Send feedback to <socketcan-users@lists.berlios.de>
45  *
46  */
47
48 #include <stdio.h>
49 #include <string.h>
50
51 #include <sys/socket.h> /* for sa_family_t */
52 #include <linux/can.h>
53
54 #include "lib.h"
55
56 #define CANID_DELIM '#'
57 #define DATA_SEPERATOR '.'
58
59 #define MAX_CANFRAME      "12345678#01.23.45.67.89.AB.CD.EF"
60 #define MAX_LONG_CANFRAME "12345678  [8] 10101010 10101010 10101010 10101010 10101010 10101010 10101010 10101010   '........'"
61
62 unsigned char asc2nibble(char c) {
63
64         if ((c >= '0') && (c <= '9'))
65                 return c - '0';
66
67         if ((c >= 'A') && (c <= 'F'))
68                 return c - 'A' + 10;
69
70         if ((c >= 'a') && (c <= 'f'))
71                 return c - 'a' + 10;
72
73         return 16; /* error */
74 }
75
76 int hexstring2candata(char *arg, struct can_frame *cf) {
77
78         int len = strlen(arg);
79         int i;
80         unsigned char tmp;
81
82         if (!len || len%2 || len > 16)
83                 return 1;
84
85         for (i=0; i < len/2; i++) {
86
87                 tmp = asc2nibble(*(arg+(2*i)));
88                 if (tmp > 0x0F)
89                         return 1;
90
91                 cf->data[i] = (tmp << 4);
92
93                 tmp = asc2nibble(*(arg+(2*i)+1));
94                 if (tmp > 0x0F)
95                         return 1;
96
97                 cf->data[i] |= tmp;
98         }
99
100         return 0;
101 }
102
103 int parse_canframe(char *cs, struct can_frame *cf) {
104         /* documentation see lib.h */
105
106         int i, idx, dlc, len;
107         unsigned char tmp;
108
109         len = strlen(cs);
110         //printf("'%s' len %d\n", cs, len);
111
112         memset(cf, 0, sizeof(*cf)); /* init CAN frame, e.g. DLC = 0 */
113
114         if (len < 4)
115                 return 1;
116
117         if (cs[3] == CANID_DELIM) { /* 3 digits */
118
119                 idx = 4;
120                 for (i=0; i<3; i++){
121                         if ((tmp = asc2nibble(cs[i])) > 0x0F)
122                                 return 1;
123                         cf->can_id |= (tmp << (2-i)*4);
124                 }
125
126         } else if (cs[8] == CANID_DELIM) { /* 8 digits */
127
128                 idx = 9;
129                 for (i=0; i<8; i++){
130                         if ((tmp = asc2nibble(cs[i])) > 0x0F)
131                                 return 1;
132                         cf->can_id |= (tmp << (7-i)*4);
133                 }
134                 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe?  */
135                         cf->can_id |= CAN_EFF_FLAG;   /* then it is an extended frame */
136
137         } else
138                 return 1;
139
140         if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
141                 cf->can_id |= CAN_RTR_FLAG;
142                 return 0;
143         }
144
145         for (i=0, dlc=0; i<8; i++){
146
147                 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
148                         idx++;
149
150                 if(idx >= len) /* end of string => end of data */
151                         break;
152
153                 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
154                         return 1;
155                 cf->data[i] = (tmp << 4);
156                 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
157                         return 1;
158                 cf->data[i] |= tmp;
159                 dlc++;
160         }
161
162         cf->can_dlc = dlc;
163
164         return 0;
165 }
166
167 void fprint_canframe(FILE *stream , struct can_frame *cf, char *eol, int sep) {
168         /* documentation see lib.h */
169
170         char buf[sizeof(MAX_CANFRAME)+1]; /* max length */
171
172         sprint_canframe(buf, cf, sep);
173         fprintf(stream, "%s", buf);
174         if (eol)
175                 fprintf(stream, "%s", eol);
176 }
177
178 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
179         /* documentation see lib.h */
180
181         int i,offset;
182         int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
183
184         if (cf->can_id & CAN_ERR_FLAG) {
185                 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
186                 offset = 9;
187         } else if (cf->can_id & CAN_EFF_FLAG) {
188                 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
189                 offset = 9;
190         } else {
191                 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
192                 offset = 4;
193         }
194
195         if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
196                 sprintf(buf+offset, "R");
197         else
198                 for (i = 0; i < dlc; i++) {
199                         sprintf(buf+offset, "%02X", cf->data[i]);
200                         offset += 2;
201                         if (sep && (i+1 < dlc))
202                                 sprintf(buf+offset++, ".");
203                 }
204
205
206 }
207
208 void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int view) {
209         /* documentation see lib.h */
210
211         char buf[sizeof(MAX_LONG_CANFRAME)+1]; /* max length */
212
213         sprint_long_canframe(buf, cf, view);
214         fprintf(stream, "%s", buf);
215         if (eol)
216                 fprintf(stream, "%s", eol);
217 }
218
219 void sprint_long_canframe(char *buf , struct can_frame *cf, int view) {
220         /* documentation see lib.h */
221
222         int i, j, dlen, offset;
223         int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
224
225         if (cf->can_id & CAN_ERR_FLAG) {
226                 sprintf(buf, "%8X  ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
227                 offset = 10;
228         } else if (cf->can_id & CAN_EFF_FLAG) {
229                 sprintf(buf, "%8X  ", cf->can_id & CAN_EFF_MASK);
230                 offset = 10;
231         } else {
232                 sprintf(buf, "%3X  ", cf->can_id & CAN_SFF_MASK);
233                 offset = 5;
234         }
235
236         sprintf(buf+offset, "[%d]", dlc);
237         offset += 3;
238
239         if (cf->can_id & CAN_RTR_FLAG) { /* there are no ERR frames with RTR */
240                 sprintf(buf+offset, " remote request");
241                 return;
242         }
243
244         if (view & CANLIB_VIEW_BINARY) {
245                 dlen = 9; /* _10101010 */
246                 if (view & CANLIB_VIEW_SWAP) {
247                         for (i = dlc - 1; i >= 0; i--) {
248                                 buf[offset++] = (i == dlc-1)?' ':SWAP_DELIMITER;
249                                 for (j = 7; j >= 0; j--)
250                                         buf[offset++] = (1<<j & cf->data[i])?'1':'0';
251                         }
252                 } else {
253                         for (i = 0; i < dlc; i++) {
254                                 buf[offset++] = ' ';
255                                 for (j = 7; j >= 0; j--)
256                                         buf[offset++] = (1<<j & cf->data[i])?'1':'0';
257                         }
258                 }
259                 buf[offset] = 0; /* terminate string */
260         } else {
261                 dlen = 3; /* _AA */
262                 if (view & CANLIB_VIEW_SWAP) {
263                         for (i = dlc - 1; i >= 0; i--) {
264                                 sprintf(buf+offset, "%c%02X",
265                                         (i == dlc-1)?' ':SWAP_DELIMITER,
266                                         cf->data[i]);
267                                 offset += dlen;
268                         }
269                 } else {
270                         for (i = 0; i < dlc; i++) {
271                                 sprintf(buf+offset, " %02X", cf->data[i]);
272                                 offset += dlen;
273                         }
274                 }
275         }
276
277         if (cf->can_id & CAN_ERR_FLAG)
278                 sprintf(buf+offset, "%*s", dlen*(8-dlc)+13, "ERRORFRAME");
279         else if (view & CANLIB_VIEW_ASCII) {
280                 j = dlen*(8-dlc)+4;
281                 if (view & CANLIB_VIEW_SWAP) {
282                         sprintf(buf+offset, "%*s", j, "`");
283                         offset += j;
284                         for (i = dlc - 1; i >= 0; i--)
285                                 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
286                                         buf[offset++] = cf->data[i];
287                                 else
288                                         buf[offset++] = '.';
289
290                         sprintf(buf+offset, "`");
291                 } else {
292                         sprintf(buf+offset, "%*s", j, "'");
293                         offset += j;
294                         for (i = 0; i < dlc; i++)
295                                 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
296                                         buf[offset++] = cf->data[i];
297                                 else
298                                         buf[offset++] = '.';
299
300                         sprintf(buf+offset, "'");
301                 }
302         } 
303 }
304