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 <linux-can@vger.kernel.org>
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 /* CAN DLC to real data length conversion helpers */
63 static const unsigned char dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
64 8, 12, 16, 20, 24, 32, 48, 64};
66 /* get data length from can_dlc with sanitized can_dlc */
67 unsigned char can_dlc2len(unsigned char can_dlc)
69 return dlc2len[can_dlc & 0x0F];
72 static const unsigned char len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
73 9, 9, 9, 9, /* 9 - 12 */
74 10, 10, 10, 10, /* 13 - 16 */
75 11, 11, 11, 11, /* 17 - 20 */
76 12, 12, 12, 12, /* 21 - 24 */
77 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
78 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
79 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
80 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
81 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
83 /* map the sanitized data length to an appropriate data length code */
84 unsigned char can_len2dlc(unsigned char len)
92 unsigned char asc2nibble(char c) {
94 if ((c >= '0') && (c <= '9'))
97 if ((c >= 'A') && (c <= 'F'))
100 if ((c >= 'a') && (c <= 'f'))
103 return 16; /* error */
106 int hexstring2data(char *arg, unsigned char *data, int maxdlen) {
108 int len = strlen(arg);
112 if (!len || len%2 || len > maxdlen*2)
115 memset(data, 0, maxdlen);
117 for (i=0; i < len/2; i++) {
119 tmp = asc2nibble(*(arg+(2*i)));
123 data[i] = (tmp << 4);
125 tmp = asc2nibble(*(arg+(2*i)+1));
135 int parse_canframe(char *cs, struct canfd_frame *cf) {
136 /* documentation see lib.h */
138 int i, idx, dlen, len;
139 int maxdlen = CAN_MAX_DLEN;
144 //printf("'%s' len %d\n", cs, len);
146 memset(cf, 0, sizeof(*cf)); /* init CAN FD frame, e.g. LEN = 0 */
151 if (cs[3] == CANID_DELIM) { /* 3 digits */
155 if ((tmp = asc2nibble(cs[i])) > 0x0F)
157 cf->can_id |= (tmp << (2-i)*4);
160 } else if (cs[8] == CANID_DELIM) { /* 8 digits */
164 if ((tmp = asc2nibble(cs[i])) > 0x0F)
166 cf->can_id |= (tmp << (7-i)*4);
168 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe? */
169 cf->can_id |= CAN_EFF_FLAG; /* then it is an extended frame */
174 if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
175 cf->can_id |= CAN_RTR_FLAG;
179 if (cs[idx] == CANID_DELIM) { /* CAN FD frame escape char '##' */
181 maxdlen = CANFD_MAX_DLEN;
184 /* CAN FD frame <canid>##<flags><data>* */
185 if ((tmp = asc2nibble(cs[idx+1])) > 0x0F)
192 for (i=0, dlen=0; i < maxdlen; i++){
194 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
197 if(idx >= len) /* end of string => end of data */
200 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
202 cf->data[i] = (tmp << 4);
203 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
213 void fprint_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int sep, int maxdlen) {
214 /* documentation see lib.h */
216 char buf[CL_CFSZ]; /* max length */
218 sprint_canframe(buf, cf, sep, maxdlen);
219 fprintf(stream, "%s", buf);
221 fprintf(stream, "%s", eol);
224 void sprint_canframe(char *buf , struct canfd_frame *cf, int sep, int maxdlen) {
225 /* documentation see lib.h */
228 int len = (cf->len > maxdlen) ? maxdlen : cf->len;
230 if (cf->can_id & CAN_ERR_FLAG) {
231 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
233 } else if (cf->can_id & CAN_EFF_FLAG) {
234 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
237 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
241 /* standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
242 if (maxdlen == CAN_MAX_DLEN && cf->can_id & CAN_RTR_FLAG) {
243 sprintf(buf+offset, "R");
247 if (maxdlen == CANFD_MAX_DLEN) {
248 /* add CAN FD specific escape char and flags */
249 sprintf(buf+offset, "#%X", cf->flags & 0xF);
252 sprintf(buf+offset++, ".");
255 for (i = 0; i < len; i++) {
256 sprintf(buf+offset, "%02X", cf->data[i]);
258 if (sep && (i+1 < len))
259 sprintf(buf+offset++, ".");
263 void fprint_long_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int view, int maxdlen) {
264 /* documentation see lib.h */
266 char buf[CL_LONGCFSZ];
268 sprint_long_canframe(buf, cf, view, maxdlen);
269 fprintf(stream, "%s", buf);
270 if ((view & CANLIB_VIEW_ERROR) && (cf->can_id & CAN_ERR_FLAG)) {
271 snprintf_can_error_frame(buf, sizeof(buf), cf, "\n\t");
272 fprintf(stream, "\n\t%s", buf);
275 fprintf(stream, "%s", eol);
278 void sprint_long_canframe(char *buf , struct canfd_frame *cf, int view, int maxdlen) {
279 /* documentation see lib.h */
281 int i, j, dlen, offset;
282 int len = (cf->len > maxdlen)? maxdlen : cf->len;
284 if (cf->can_id & CAN_ERR_FLAG) {
285 sprintf(buf, "%08X ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
287 } else if (cf->can_id & CAN_EFF_FLAG) {
288 sprintf(buf, "%08X ", cf->can_id & CAN_EFF_MASK);
291 if (view & CANLIB_VIEW_INDENT_SFF) {
292 sprintf(buf, " %03X ", cf->can_id & CAN_SFF_MASK);
295 sprintf(buf, "%03X ", cf->can_id & CAN_SFF_MASK);
300 if (maxdlen == CAN_MAX_DLEN) {
301 sprintf(buf+offset, " [%d] ", len);
302 /* standard CAN frames may have RTR enabled */
303 if (cf->can_id & CAN_RTR_FLAG) {
304 sprintf(buf+offset+5, " remote request");
308 sprintf(buf+offset, "[%02d] ", len);
312 if (view & CANLIB_VIEW_BINARY) {
313 dlen = 9; /* _10101010 */
314 if (view & CANLIB_VIEW_SWAP) {
315 for (i = len - 1; i >= 0; i--) {
316 buf[offset++] = (i == len-1)?' ':SWAP_DELIMITER;
317 for (j = 7; j >= 0; j--)
318 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
321 for (i = 0; i < len; i++) {
323 for (j = 7; j >= 0; j--)
324 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
327 buf[offset] = 0; /* terminate string */
330 if (view & CANLIB_VIEW_SWAP) {
331 for (i = len - 1; i >= 0; i--) {
332 sprintf(buf+offset, "%c%02X",
333 (i == len-1)?' ':SWAP_DELIMITER,
338 for (i = 0; i < len; i++) {
339 sprintf(buf+offset, " %02X", cf->data[i]);
346 * The ASCII & ERRORFRAME output is put at a fixed len behind the data.
347 * For now we support ASCII output only for payload length up to 8 bytes.
348 * Does it make sense to write 64 ASCII byte behind 64 ASCII HEX data on the console?
350 if (len > CAN_MAX_DLEN)
353 if (cf->can_id & CAN_ERR_FLAG)
354 sprintf(buf+offset, "%*s", dlen*(8-len)+13, "ERRORFRAME");
355 else if (view & CANLIB_VIEW_ASCII) {
357 if (view & CANLIB_VIEW_SWAP) {
358 sprintf(buf+offset, "%*s", j, "`");
360 for (i = len - 1; i >= 0; i--)
361 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
362 buf[offset++] = cf->data[i];
366 sprintf(buf+offset, "`");
368 sprintf(buf+offset, "%*s", j, "'");
370 for (i = 0; i < len; i++)
371 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
372 buf[offset++] = cf->data[i];
376 sprintf(buf+offset, "'");
381 static const char *error_classes[] = {
384 "controller-problem",
385 "protocol-violation",
386 "transceiver-status",
387 "no-acknowledgement-on-tx",
390 "restarted-after-bus-off",
393 static const char *controller_problems[] = {
402 static const char *protocol_violation_types[] = {
404 "frame-format-error",
405 "bit-stuffing-error",
406 "tx-dominant-bit-error",
407 "tx-recessive-bit-error",
409 "back-to-error-active",
413 static const char *protocol_violation_locations[] = {
433 "tolerate-dominant-bits",
436 "passive-error-flag",
441 "acknowledge-delimiter",
449 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
452 static int snprintf_error_data(char *buf, size_t len, uint8_t err,
453 const char **arr, int arr_len)
455 int i, n = 0, count = 0;
457 if (!err || len <= 0)
460 for (i = 0; i < arr_len; i++) {
461 if (err & (1 << i)) {
463 n += snprintf(buf + n, len - n, ",");
464 n += snprintf(buf + n, len - n, "%s", arr[i]);
472 static int snprintf_error_lostarb(char *buf, size_t len, struct canfd_frame *cf)
476 return snprintf(buf, len, "{at bit %d}", cf->data[0]);
479 static int snprintf_error_ctrl(char *buf, size_t len, struct canfd_frame *cf)
486 n += snprintf(buf + n, len - n, "{");
487 n += snprintf_error_data(buf + n, len - n, cf->data[1],
489 ARRAY_SIZE(controller_problems));
490 n += snprintf(buf + n, len - n, "}");
495 static int snprintf_error_prot(char *buf, size_t len, struct canfd_frame *cf)
502 n += snprintf(buf + n, len - n, "{{");
503 n += snprintf_error_data(buf + n, len - n, cf->data[2],
504 protocol_violation_types,
505 ARRAY_SIZE(protocol_violation_types));
506 n += snprintf(buf + n, len - n, "}{");
507 if (cf->data[3] > 0 &&
508 cf->data[3] < ARRAY_SIZE(protocol_violation_locations))
509 n += snprintf(buf + n, len - n, "%s",
510 protocol_violation_locations[cf->data[3]]);
511 n += snprintf(buf + n, len - n, "}}");
516 void snprintf_can_error_frame(char *buf, size_t len, struct canfd_frame *cf,
520 int i, n = 0, classes = 0;
523 if (!(cf->can_id & CAN_ERR_FLAG))
526 class = cf->can_id & CAN_EFF_MASK;
527 if (class > (1 << ARRAY_SIZE(error_classes))) {
528 fprintf(stderr, "Error class %#x is invalid\n", class);
535 for (i = 0; i < ARRAY_SIZE(error_classes); i++) {
539 n += snprintf(buf + n, len - n, "%s", sep);
540 n += snprintf(buf + n, len - n, "%s", error_classes[i]);
541 if (mask == CAN_ERR_LOSTARB)
542 n += snprintf_error_lostarb(buf + n, len - n,
544 if (mask == CAN_ERR_CRTL)
545 n += snprintf_error_ctrl(buf + n, len - n, cf);
546 if (mask == CAN_ERR_PROT)
547 n += snprintf_error_prot(buf + n, len - n, cf);
552 if (cf->data[6] || cf->data[7]) {
553 n += snprintf(buf + n, len - n, "%s", sep);
554 n += snprintf(buf + n, len - n, "error-counter-tx-rx{{%d}{%d}}",
555 cf->data[6], cf->data[7]);