6 * lib.c - library for command line tools
8 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
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.
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.
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.
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
44 * Send feedback to <socketcan-users@lists.berlios.de>
52 #include <sys/socket.h> /* for sa_family_t */
53 #include <linux/can.h>
54 #include <linux/can/error.h>
58 #define CANID_DELIM '#'
59 #define DATA_SEPERATOR '.'
61 #define MAX_CANFRAME "12345678#01.23.45.67.89.AB.CD.EF"
62 #define MAX_LONG_CANFRAME_SIZE 256
64 unsigned char asc2nibble(char c) {
66 if ((c >= '0') && (c <= '9'))
69 if ((c >= 'A') && (c <= 'F'))
72 if ((c >= 'a') && (c <= 'f'))
75 return 16; /* error */
78 int hexstring2candata(char *arg, struct can_frame *cf) {
80 int len = strlen(arg);
84 if (!len || len%2 || len > 16)
87 for (i=0; i < len/2; i++) {
89 tmp = asc2nibble(*(arg+(2*i)));
93 cf->data[i] = (tmp << 4);
95 tmp = asc2nibble(*(arg+(2*i)+1));
105 int parse_canframe(char *cs, struct can_frame *cf) {
106 /* documentation see lib.h */
108 int i, idx, dlc, len;
112 //printf("'%s' len %d\n", cs, len);
114 memset(cf, 0, sizeof(*cf)); /* init CAN frame, e.g. DLC = 0 */
119 if (cs[3] == CANID_DELIM) { /* 3 digits */
123 if ((tmp = asc2nibble(cs[i])) > 0x0F)
125 cf->can_id |= (tmp << (2-i)*4);
128 } else if (cs[8] == CANID_DELIM) { /* 8 digits */
132 if ((tmp = asc2nibble(cs[i])) > 0x0F)
134 cf->can_id |= (tmp << (7-i)*4);
136 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe? */
137 cf->can_id |= CAN_EFF_FLAG; /* then it is an extended frame */
142 if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
143 cf->can_id |= CAN_RTR_FLAG;
147 for (i=0, dlc=0; i<8; i++){
149 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
152 if(idx >= len) /* end of string => end of data */
155 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
157 cf->data[i] = (tmp << 4);
158 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
169 void fprint_canframe(FILE *stream , struct can_frame *cf, char *eol, int sep) {
170 /* documentation see lib.h */
172 char buf[sizeof(MAX_CANFRAME)+1]; /* max length */
174 sprint_canframe(buf, cf, sep);
175 fprintf(stream, "%s", buf);
177 fprintf(stream, "%s", eol);
180 void sprint_canframe(char *buf , struct can_frame *cf, int sep) {
181 /* documentation see lib.h */
184 int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
186 if (cf->can_id & CAN_ERR_FLAG) {
187 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
189 } else if (cf->can_id & CAN_EFF_FLAG) {
190 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
193 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
197 if (cf->can_id & CAN_RTR_FLAG) /* there are no ERR frames with RTR */
198 sprintf(buf+offset, "R");
200 for (i = 0; i < dlc; i++) {
201 sprintf(buf+offset, "%02X", cf->data[i]);
203 if (sep && (i+1 < dlc))
204 sprintf(buf+offset++, ".");
210 void fprint_long_canframe(FILE *stream , struct can_frame *cf, char *eol, int view) {
211 /* documentation see lib.h */
213 char buf[MAX_LONG_CANFRAME_SIZE];
215 sprint_long_canframe(buf, cf, view);
216 fprintf(stream, "%s", buf);
217 if ((view & CANLIB_VIEW_ERROR) && (cf->can_id & CAN_ERR_FLAG)) {
218 snprintf_can_error_frame(buf, sizeof(buf), cf, "\n\t");
219 fprintf(stream, "\n\t%s", buf);
222 fprintf(stream, "%s", eol);
225 void sprint_long_canframe(char *buf , struct can_frame *cf, int view) {
226 /* documentation see lib.h */
228 int i, j, dlen, offset;
229 int dlc = (cf->can_dlc > 8)? 8 : cf->can_dlc;
231 if (cf->can_id & CAN_ERR_FLAG) {
232 sprintf(buf, "%8X ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
234 } else if (cf->can_id & CAN_EFF_FLAG) {
235 sprintf(buf, "%8X ", cf->can_id & CAN_EFF_MASK);
238 sprintf(buf, "%3X ", cf->can_id & CAN_SFF_MASK);
242 sprintf(buf+offset, "[%d]", dlc);
245 if (cf->can_id & CAN_RTR_FLAG) { /* there are no ERR frames with RTR */
246 sprintf(buf+offset, " remote request");
250 if (view & CANLIB_VIEW_BINARY) {
251 dlen = 9; /* _10101010 */
252 if (view & CANLIB_VIEW_SWAP) {
253 for (i = dlc - 1; i >= 0; i--) {
254 buf[offset++] = (i == dlc-1)?' ':SWAP_DELIMITER;
255 for (j = 7; j >= 0; j--)
256 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
259 for (i = 0; i < dlc; i++) {
261 for (j = 7; j >= 0; j--)
262 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
265 buf[offset] = 0; /* terminate string */
268 if (view & CANLIB_VIEW_SWAP) {
269 for (i = dlc - 1; i >= 0; i--) {
270 sprintf(buf+offset, "%c%02X",
271 (i == dlc-1)?' ':SWAP_DELIMITER,
276 for (i = 0; i < dlc; i++) {
277 sprintf(buf+offset, " %02X", cf->data[i]);
283 if (cf->can_id & CAN_ERR_FLAG)
284 sprintf(buf+offset, "%*s", dlen*(8-dlc)+13, "ERRORFRAME");
285 else if (view & CANLIB_VIEW_ASCII) {
287 if (view & CANLIB_VIEW_SWAP) {
288 sprintf(buf+offset, "%*s", j, "`");
290 for (i = dlc - 1; i >= 0; i--)
291 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
292 buf[offset++] = cf->data[i];
296 sprintf(buf+offset, "`");
298 sprintf(buf+offset, "%*s", j, "'");
300 for (i = 0; i < dlc; i++)
301 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
302 buf[offset++] = cf->data[i];
306 sprintf(buf+offset, "'");
311 static const char *error_classes[] = {
314 "controller-problem",
315 "protocol-violation",
316 "transceiver-status",
317 "no-acknowledgement-on-tx",
320 "restarted-after-bus-off",
323 static const char *controller_problems[] = {
332 static const char *protocol_violation_types[] = {
334 "frame-format-error",
335 "bit-stuffing-error",
336 "tx-dominant-bit-error",
337 "tx-recessive-bit-error",
339 "back-to-error-active",
343 static const char *protocol_violation_locations[] = {
363 "tolerate-dominant-bits",
366 "passive-error-flag",
371 "acknowledge-delimiter",
379 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
382 static int snprintf_error_data(char *buf, size_t len, uint8_t err,
383 const char **arr, int arr_len)
385 int i, n = 0, count = 0;
387 if (!err || len <= 0)
390 for (i = 0; i < arr_len; i++) {
391 if (err & (1 << i)) {
393 n += snprintf(buf + n, len - n, ",");
394 n += snprintf(buf + n, len - n, "%s", arr[i]);
402 static int snprintf_error_lostarb(char *buf, size_t len, struct can_frame *cf)
406 return snprintf(buf, len, "{at bit %d}", cf->data[0]);
409 static int snprintf_error_ctrl(char *buf, size_t len, struct can_frame *cf)
416 n += snprintf(buf + n, len - n, "{");
417 n += snprintf_error_data(buf + n, len - n, cf->data[1],
419 ARRAY_SIZE(controller_problems));
420 n += snprintf(buf + n, len - n, "}");
425 static int snprintf_error_prot(char *buf, size_t len, struct can_frame *cf)
432 n += snprintf(buf + n, len - n, "{{");
433 n += snprintf_error_data(buf + n, len - n, cf->data[2],
434 protocol_violation_types,
435 ARRAY_SIZE(protocol_violation_types));
436 n += snprintf(buf + n, len - n, "}{");
437 if (cf->data[3] > 0 &&
438 cf->data[3] < ARRAY_SIZE(protocol_violation_locations))
439 n += snprintf(buf + n, len - n, "%s",
440 protocol_violation_locations[cf->data[3]]);
441 n += snprintf(buf + n, len - n, "}}");
446 void snprintf_can_error_frame(char *buf, size_t len, struct can_frame *cf,
450 int i, n = 0, classes = 0;
453 if (!(cf->can_id & CAN_ERR_FLAG))
456 class = cf->can_id & CAN_EFF_MASK;
457 if (class > (1 << ARRAY_SIZE(error_classes))) {
458 fprintf(stderr, "Error class %#x is invalid\n", class);
465 for (i = 0; i < ARRAY_SIZE(error_classes); i++) {
469 n += snprintf(buf + n, len - n, "%s", sep);
470 n += snprintf(buf + n, len - n, "%s", error_classes[i]);
471 if (mask == CAN_ERR_LOSTARB)
472 n += snprintf_error_lostarb(buf + n, len - n,
474 if (mask == CAN_ERR_CRTL)
475 n += snprintf_error_ctrl(buf + n, len - n, cf);
476 if (mask == CAN_ERR_PROT)
477 n += snprintf_error_prot(buf + n, len - n, cf);
482 if (cf->data[6] || cf->data[7]) {
483 n += snprintf(buf + n, len - n, "%s", sep);
484 n += snprintf(buf + n, len - n, "error-counter-tx-rx{{%d}{%d}}",
485 cf->data[6], cf->data[7]);