]> rtime.felk.cvut.cz Git - socketcan-devel.git/blob - can-utils/lib.c
- added error frame support in lib.c
[socketcan-devel.git] / can-utils / lib.c
1 /*
2  *  $Id$
3  */
4
5 /*
6  * lib.c - library for command line tools
7  *
8  * Copyright (c) 2002-2005 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, the following disclaimer and
16  *    the referenced file 'COPYING'.
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in the
19  *    documentation and/or other materials provided with the distribution.
20  * 3. Neither the name of Volkswagen nor the names of its contributors
21  *    may be used to endorse or promote products derived from this software
22  *    without specific prior written permission.
23  *
24  * Alternatively, provided that this notice is retained in full, this
25  * software may be distributed under the terms of the GNU General
26  * Public License ("GPL") version 2 as distributed in the 'COPYING'
27  * file from the main directory of the linux kernel source.
28  *
29  * The provided data structures and external interfaces from this code
30  * are not restricted to be used by modules with a GPL compatible license.
31  *
32  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
33  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
34  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
35  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
36  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
37  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
39  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
40  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
41  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
42  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
43  * DAMAGE.
44  *
45  * Send feedback to <socketcan-users@lists.berlios.de>
46  *
47  */
48
49 #include <stdio.h>
50 #include <string.h>
51
52 #include <sys/socket.h> /* for sa_family_t */
53 #include <linux/can.h>
54
55 #include "lib.h"
56
57 #define CANID_DELIM '#'
58 #define DATA_SEPERATOR '.'
59
60 #define MAX_CANFRAME      "12345678#01.23.45.67.89.AB.CD.EF"
61 #define MAX_LONG_CANFRAME "12345678  [8] 01 23 45 67 89 AB CD EF   '........'"
62
63 static int asc2nibble(char c) {
64
65     if ((c >= '0') && (c <= '9'))
66         return c - '0';
67
68     if ((c >= 'A') && (c <= 'F'))
69         return c - 'A' + 10;
70
71     if ((c >= 'a') && (c <= 'f'))
72         return c - 'a' + 10;
73
74     return 16; /* error */
75 }
76
77 int parse_canframe(char *cs, struct can_frame *cf) {
78     /* documentation see lib.h */
79
80     int i, idx, dlc, len, tmp;
81
82     len = strlen(cs);
83     //printf("'%s' len %d\n", cs, len);
84
85     memset(cf, 0, sizeof(*cf)); /* init CAN frame, e.g. DLC = 0 */
86
87     if (len < 4)
88         return 1;
89
90     if (!((cs[3] == CANID_DELIM) || (cs[8] == CANID_DELIM)))
91         return 1;
92
93     if (cs[8] == CANID_DELIM) { /* 8 digits */
94
95         idx = 9;
96         for (i=0; i<8; i++){
97             if ((tmp = asc2nibble(cs[i])) > 0x0F)
98                 return 1;
99             cf->can_id |= (tmp << (7-i)*4);
100         }
101         if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe?  */
102             cf->can_id |= CAN_EFF_FLAG;   /* then it is an extended frame */
103
104     } else { /* 3 digits */
105
106         idx = 4;
107         for (i=0; i<3; i++){
108             if ((tmp = asc2nibble(cs[i])) > 0x0F)
109                 return 1;
110             cf->can_id |= (tmp << (2-i)*4);
111         }
112     }
113
114     if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
115         cf->can_id |= CAN_RTR_FLAG;
116         return 0;
117     }
118
119     for (i=0, dlc=0; i<8; i++){
120
121         if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
122             idx++;
123
124         if(idx >= len) /* end of string => end of data */
125             break;
126
127         if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
128             return 1;
129         cf->data[i] = (tmp << 4);
130         if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
131             return 1;
132         cf->data[i] |= tmp;
133         dlc++;
134     }
135
136     cf->can_dlc = dlc;
137
138     return 0;
139 }
140
141 void fprint_canframe(FILE *stream , struct can_frame *cf, char *eol, int sep) {
142     /* documentation see lib.h */
143
144     char buf[sizeof(MAX_CANFRAME)+1]; /* max length */
145
146     sprint_canframe(buf, cf, sep);
147     fprintf(stream, "%s", buf);
148     if (eol)
149         fprintf(stream, "%s", eol);
150 }
151
152 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
153     /* documentation see lib.h */
154
155     int i,offset;
156
157     if (cf->can_id & CAN_ERR_FLAG) {
158         sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
159         offset = 9;
160     } else if (cf->can_id & CAN_EFF_FLAG) {
161         sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
162         offset = 9;
163     } else {
164         sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
165         offset = 4;
166     }
167
168     if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
169         sprintf(buf+offset, "R");
170     else
171         for (i = 0; i < cf->can_dlc; i++) {
172             sprintf(buf+offset, "%02X", cf->data[i]);
173             offset += 2;
174             if (sep && (i+1 < cf->can_dlc))
175                 sprintf(buf+offset++, ".");
176         }
177
178
179 }
180
181 void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int ascii) {
182     /* documentation see lib.h */
183
184     char buf[sizeof(MAX_LONG_CANFRAME)+1]; /* max length */
185
186     sprint_long_canframe(buf, cf, ascii);
187     fprintf(stream, "%s", buf);
188     if (eol)
189         fprintf(stream, "%s", eol);
190 }
191
192 void sprint_long_canframe(char *buf , struct can_frame *cf, int ascii) {
193     /* documentation see lib.h */
194
195     int i, offset;
196
197     if (cf->can_id & CAN_ERR_FLAG) {
198         sprintf(buf, "%8X  ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
199         offset = 10;
200     } else if (cf->can_id & CAN_EFF_FLAG) {
201         sprintf(buf, "%8X  ", cf->can_id & CAN_EFF_MASK);
202         offset = 10;
203     } else {
204         sprintf(buf, "%3X  ", cf->can_id & CAN_SFF_MASK);
205         offset = 5;
206     }
207
208     sprintf(buf+offset, "[%d]", cf->can_dlc);
209     offset += 3;
210
211     if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
212         sprintf(buf+offset, " remote request");
213     else {
214         for (i = 0; i < cf->can_dlc; i++) {
215             sprintf(buf+offset, " %02X", cf->data[i]);
216             offset += 3;
217         }
218         if (cf->can_id & CAN_ERR_FLAG)
219             sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+13, "ERRORFRAME");
220         else if (ascii) {
221             sprintf(buf+offset, "%*s", 3*(8-cf->can_dlc)+4, "'");
222             offset += 3*(8-cf->can_dlc)+4;
223
224             for (i = 0; i < cf->can_dlc; i++)
225                 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
226                     buf[offset++] = cf->data[i];
227                 else
228                     buf[offset++] = '.';
229             sprintf(buf+offset, "'");
230         } 
231     }
232 }
233