2 * lib.c - library for command line tools
4 * Copyright (c) 2002-2007 Volkswagen Group Electronic Research
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of Volkswagen nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
19 * Alternatively, provided that this notice is retained in full, this
20 * software may be distributed under the terms of the GNU General
21 * Public License ("GPL") version 2, in which case the provisions of the
22 * GPL apply INSTEAD OF those given above.
24 * The provided data structures and external interfaces from this code
25 * are not restricted to be used by modules with a GPL compatible license.
27 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
28 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
29 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
30 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
31 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
34 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
35 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
40 * Send feedback to <linux-can@vger.kernel.org>
48 #include <sys/socket.h> /* for sa_family_t */
49 #include <linux/can.h>
50 #include <linux/can/error.h>
54 #define CANID_DELIM '#'
55 #define DATA_SEPERATOR '.'
57 /* CAN DLC to real data length conversion helpers */
59 static const unsigned char dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
60 8, 12, 16, 20, 24, 32, 48, 64};
62 /* get data length from can_dlc with sanitized can_dlc */
63 unsigned char can_dlc2len(unsigned char can_dlc)
65 return dlc2len[can_dlc & 0x0F];
68 static const unsigned char len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
69 9, 9, 9, 9, /* 9 - 12 */
70 10, 10, 10, 10, /* 13 - 16 */
71 11, 11, 11, 11, /* 17 - 20 */
72 12, 12, 12, 12, /* 21 - 24 */
73 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
74 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
75 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
76 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
77 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
79 /* map the sanitized data length to an appropriate data length code */
80 unsigned char can_len2dlc(unsigned char len)
88 unsigned char asc2nibble(char c) {
90 if ((c >= '0') && (c <= '9'))
93 if ((c >= 'A') && (c <= 'F'))
96 if ((c >= 'a') && (c <= 'f'))
99 return 16; /* error */
102 int hexstring2data(char *arg, unsigned char *data, int maxdlen) {
104 int len = strlen(arg);
108 if (!len || len%2 || len > maxdlen*2)
111 memset(data, 0, maxdlen);
113 for (i=0; i < len/2; i++) {
115 tmp = asc2nibble(*(arg+(2*i)));
119 data[i] = (tmp << 4);
121 tmp = asc2nibble(*(arg+(2*i)+1));
131 int parse_canframe(char *cs, struct canfd_frame *cf) {
132 /* documentation see lib.h */
134 int i, idx, dlen, len;
135 int maxdlen = CAN_MAX_DLEN;
140 //printf("'%s' len %d\n", cs, len);
142 memset(cf, 0, sizeof(*cf)); /* init CAN FD frame, e.g. LEN = 0 */
147 if (cs[3] == CANID_DELIM) { /* 3 digits */
151 if ((tmp = asc2nibble(cs[i])) > 0x0F)
153 cf->can_id |= (tmp << (2-i)*4);
156 } else if (cs[8] == CANID_DELIM) { /* 8 digits */
160 if ((tmp = asc2nibble(cs[i])) > 0x0F)
162 cf->can_id |= (tmp << (7-i)*4);
164 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe? */
165 cf->can_id |= CAN_EFF_FLAG; /* then it is an extended frame */
170 if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
171 cf->can_id |= CAN_RTR_FLAG;
175 if (cs[idx] == CANID_DELIM) { /* CAN FD frame escape char '##' */
177 maxdlen = CANFD_MAX_DLEN;
180 /* CAN FD frame <canid>##<flags><data>* */
181 if ((tmp = asc2nibble(cs[idx+1])) > 0x0F)
188 for (i=0, dlen=0; i < maxdlen; i++){
190 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
193 if(idx >= len) /* end of string => end of data */
196 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
198 cf->data[i] = (tmp << 4);
199 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
209 void fprint_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int sep, int maxdlen) {
210 /* documentation see lib.h */
212 char buf[CL_CFSZ]; /* max length */
214 sprint_canframe(buf, cf, sep, maxdlen);
215 fprintf(stream, "%s", buf);
217 fprintf(stream, "%s", eol);
220 void sprint_canframe(char *buf , struct canfd_frame *cf, int sep, int maxdlen) {
221 /* documentation see lib.h */
224 int len = (cf->len > maxdlen) ? maxdlen : cf->len;
226 if (cf->can_id & CAN_ERR_FLAG) {
227 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
229 } else if (cf->can_id & CAN_EFF_FLAG) {
230 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
233 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
237 /* standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
238 if (maxdlen == CAN_MAX_DLEN && cf->can_id & CAN_RTR_FLAG) {
239 sprintf(buf+offset, "R");
243 if (maxdlen == CANFD_MAX_DLEN) {
244 /* add CAN FD specific escape char and flags */
245 sprintf(buf+offset, "#%X", cf->flags & 0xF);
248 sprintf(buf+offset++, ".");
251 for (i = 0; i < len; i++) {
252 sprintf(buf+offset, "%02X", cf->data[i]);
254 if (sep && (i+1 < len))
255 sprintf(buf+offset++, ".");
259 void fprint_long_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int view, int maxdlen) {
260 /* documentation see lib.h */
262 char buf[CL_LONGCFSZ];
264 sprint_long_canframe(buf, cf, view, maxdlen);
265 fprintf(stream, "%s", buf);
266 if ((view & CANLIB_VIEW_ERROR) && (cf->can_id & CAN_ERR_FLAG)) {
267 snprintf_can_error_frame(buf, sizeof(buf), cf, "\n\t");
268 fprintf(stream, "\n\t%s", buf);
271 fprintf(stream, "%s", eol);
274 void sprint_long_canframe(char *buf , struct canfd_frame *cf, int view, int maxdlen) {
275 /* documentation see lib.h */
277 int i, j, dlen, offset;
278 int len = (cf->len > maxdlen)? maxdlen : cf->len;
280 if (cf->can_id & CAN_ERR_FLAG) {
281 sprintf(buf, "%08X ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
283 } else if (cf->can_id & CAN_EFF_FLAG) {
284 sprintf(buf, "%08X ", cf->can_id & CAN_EFF_MASK);
287 if (view & CANLIB_VIEW_INDENT_SFF) {
288 sprintf(buf, " %03X ", cf->can_id & CAN_SFF_MASK);
291 sprintf(buf, "%03X ", cf->can_id & CAN_SFF_MASK);
296 if (maxdlen == CAN_MAX_DLEN) {
297 sprintf(buf+offset, " [%d] ", len);
298 /* standard CAN frames may have RTR enabled */
299 if (cf->can_id & CAN_RTR_FLAG) {
300 sprintf(buf+offset+5, " remote request");
304 sprintf(buf+offset, "[%02d] ", len);
308 if (view & CANLIB_VIEW_BINARY) {
309 dlen = 9; /* _10101010 */
310 if (view & CANLIB_VIEW_SWAP) {
311 for (i = len - 1; i >= 0; i--) {
312 buf[offset++] = (i == len-1)?' ':SWAP_DELIMITER;
313 for (j = 7; j >= 0; j--)
314 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
317 for (i = 0; i < len; i++) {
319 for (j = 7; j >= 0; j--)
320 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
323 buf[offset] = 0; /* terminate string */
326 if (view & CANLIB_VIEW_SWAP) {
327 for (i = len - 1; i >= 0; i--) {
328 sprintf(buf+offset, "%c%02X",
329 (i == len-1)?' ':SWAP_DELIMITER,
334 for (i = 0; i < len; i++) {
335 sprintf(buf+offset, " %02X", cf->data[i]);
342 * The ASCII & ERRORFRAME output is put at a fixed len behind the data.
343 * For now we support ASCII output only for payload length up to 8 bytes.
344 * Does it make sense to write 64 ASCII byte behind 64 ASCII HEX data on the console?
346 if (len > CAN_MAX_DLEN)
349 if (cf->can_id & CAN_ERR_FLAG)
350 sprintf(buf+offset, "%*s", dlen*(8-len)+13, "ERRORFRAME");
351 else if (view & CANLIB_VIEW_ASCII) {
353 if (view & CANLIB_VIEW_SWAP) {
354 sprintf(buf+offset, "%*s", j, "`");
356 for (i = len - 1; i >= 0; i--)
357 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
358 buf[offset++] = cf->data[i];
362 sprintf(buf+offset, "`");
364 sprintf(buf+offset, "%*s", j, "'");
366 for (i = 0; i < len; i++)
367 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
368 buf[offset++] = cf->data[i];
372 sprintf(buf+offset, "'");
377 static const char *error_classes[] = {
380 "controller-problem",
381 "protocol-violation",
382 "transceiver-status",
383 "no-acknowledgement-on-tx",
386 "restarted-after-bus-off",
389 static const char *controller_problems[] = {
398 static const char *protocol_violation_types[] = {
400 "frame-format-error",
401 "bit-stuffing-error",
402 "tx-dominant-bit-error",
403 "tx-recessive-bit-error",
405 "back-to-error-active",
409 static const char *protocol_violation_locations[] = {
429 "tolerate-dominant-bits",
432 "passive-error-flag",
437 "acknowledge-delimiter",
445 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
448 static int snprintf_error_data(char *buf, size_t len, uint8_t err,
449 const char **arr, int arr_len)
451 int i, n = 0, count = 0;
453 if (!err || len <= 0)
456 for (i = 0; i < arr_len; i++) {
457 if (err & (1 << i)) {
459 n += snprintf(buf + n, len - n, ",");
460 n += snprintf(buf + n, len - n, "%s", arr[i]);
468 static int snprintf_error_lostarb(char *buf, size_t len, struct canfd_frame *cf)
472 return snprintf(buf, len, "{at bit %d}", cf->data[0]);
475 static int snprintf_error_ctrl(char *buf, size_t len, struct canfd_frame *cf)
482 n += snprintf(buf + n, len - n, "{");
483 n += snprintf_error_data(buf + n, len - n, cf->data[1],
485 ARRAY_SIZE(controller_problems));
486 n += snprintf(buf + n, len - n, "}");
491 static int snprintf_error_prot(char *buf, size_t len, struct canfd_frame *cf)
498 n += snprintf(buf + n, len - n, "{{");
499 n += snprintf_error_data(buf + n, len - n, cf->data[2],
500 protocol_violation_types,
501 ARRAY_SIZE(protocol_violation_types));
502 n += snprintf(buf + n, len - n, "}{");
503 if (cf->data[3] > 0 &&
504 cf->data[3] < ARRAY_SIZE(protocol_violation_locations))
505 n += snprintf(buf + n, len - n, "%s",
506 protocol_violation_locations[cf->data[3]]);
507 n += snprintf(buf + n, len - n, "}}");
512 void snprintf_can_error_frame(char *buf, size_t len, struct canfd_frame *cf,
516 int i, n = 0, classes = 0;
519 if (!(cf->can_id & CAN_ERR_FLAG))
522 class = cf->can_id & CAN_EFF_MASK;
523 if (class > (1 << ARRAY_SIZE(error_classes))) {
524 fprintf(stderr, "Error class %#x is invalid\n", class);
531 for (i = 0; i < ARRAY_SIZE(error_classes); i++) {
535 n += snprintf(buf + n, len - n, "%s", sep);
536 n += snprintf(buf + n, len - n, "%s", error_classes[i]);
537 if (mask == CAN_ERR_LOSTARB)
538 n += snprintf_error_lostarb(buf + n, len - n,
540 if (mask == CAN_ERR_CRTL)
541 n += snprintf_error_ctrl(buf + n, len - n, cf);
542 if (mask == CAN_ERR_PROT)
543 n += snprintf_error_prot(buf + n, len - n, cf);
548 if (cf->data[6] || cf->data[7]) {
549 n += snprintf(buf + n, len - n, "%s", sep);
550 n += snprintf(buf + n, len - n, "error-counter-tx-rx{{%d}{%d}}",
551 cf->data[6], cf->data[7]);