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;
173 /* check for optional DLC value for CAN 2.0B frames */
174 if(cs[++idx] && (tmp = asc2nibble(cs[idx])) <= CAN_MAX_DLC)
180 if (cs[idx] == CANID_DELIM) { /* CAN FD frame escape char '##' */
182 maxdlen = CANFD_MAX_DLEN;
185 /* CAN FD frame <canid>##<flags><data>* */
186 if ((tmp = asc2nibble(cs[idx+1])) > 0x0F)
193 for (i=0, dlen=0; i < maxdlen; i++){
195 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) seperator */
198 if(idx >= len) /* end of string => end of data */
201 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
203 cf->data[i] = (tmp << 4);
204 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
214 void fprint_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int sep, int maxdlen) {
215 /* documentation see lib.h */
217 char buf[CL_CFSZ]; /* max length */
219 sprint_canframe(buf, cf, sep, maxdlen);
220 fprintf(stream, "%s", buf);
222 fprintf(stream, "%s", eol);
225 void sprint_canframe(char *buf , struct canfd_frame *cf, int sep, int maxdlen) {
226 /* documentation see lib.h */
229 int len = (cf->len > maxdlen) ? maxdlen : cf->len;
231 if (cf->can_id & CAN_ERR_FLAG) {
232 sprintf(buf, "%08X#", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
234 } else if (cf->can_id & CAN_EFF_FLAG) {
235 sprintf(buf, "%08X#", cf->can_id & CAN_EFF_MASK);
238 sprintf(buf, "%03X#", cf->can_id & CAN_SFF_MASK);
242 /* standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
243 if (maxdlen == CAN_MAX_DLEN && cf->can_id & CAN_RTR_FLAG) {
245 /* print a given CAN 2.0B DLC if it's not zero */
246 if (cf->len && cf->len <= CAN_MAX_DLC)
247 sprintf(buf+offset, "R%d", cf->len);
249 sprintf(buf+offset, "R");
254 if (maxdlen == CANFD_MAX_DLEN) {
255 /* add CAN FD specific escape char and flags */
256 sprintf(buf+offset, "#%X", cf->flags & 0xF);
259 sprintf(buf+offset++, ".");
262 for (i = 0; i < len; i++) {
263 sprintf(buf+offset, "%02X", cf->data[i]);
265 if (sep && (i+1 < len))
266 sprintf(buf+offset++, ".");
270 void fprint_long_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int view, int maxdlen) {
271 /* documentation see lib.h */
273 char buf[CL_LONGCFSZ];
275 sprint_long_canframe(buf, cf, view, maxdlen);
276 fprintf(stream, "%s", buf);
277 if ((view & CANLIB_VIEW_ERROR) && (cf->can_id & CAN_ERR_FLAG)) {
278 snprintf_can_error_frame(buf, sizeof(buf), cf, "\n\t");
279 fprintf(stream, "\n\t%s", buf);
282 fprintf(stream, "%s", eol);
285 void sprint_long_canframe(char *buf , struct canfd_frame *cf, int view, int maxdlen) {
286 /* documentation see lib.h */
288 int i, j, dlen, offset;
289 int len = (cf->len > maxdlen)? maxdlen : cf->len;
291 if (cf->can_id & CAN_ERR_FLAG) {
292 sprintf(buf, "%08X ", cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
294 } else if (cf->can_id & CAN_EFF_FLAG) {
295 sprintf(buf, "%08X ", cf->can_id & CAN_EFF_MASK);
298 if (view & CANLIB_VIEW_INDENT_SFF) {
299 sprintf(buf, " %03X ", cf->can_id & CAN_SFF_MASK);
302 sprintf(buf, "%03X ", cf->can_id & CAN_SFF_MASK);
307 if (maxdlen == CAN_MAX_DLEN) {
308 sprintf(buf+offset, " [%d] ", len);
309 /* standard CAN frames may have RTR enabled */
310 if (cf->can_id & CAN_RTR_FLAG) {
311 sprintf(buf+offset+5, " remote request");
315 sprintf(buf+offset, "[%02d] ", len);
319 if (view & CANLIB_VIEW_BINARY) {
320 dlen = 9; /* _10101010 */
321 if (view & CANLIB_VIEW_SWAP) {
322 for (i = len - 1; i >= 0; i--) {
323 buf[offset++] = (i == len-1)?' ':SWAP_DELIMITER;
324 for (j = 7; j >= 0; j--)
325 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
328 for (i = 0; i < len; i++) {
330 for (j = 7; j >= 0; j--)
331 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
334 buf[offset] = 0; /* terminate string */
337 if (view & CANLIB_VIEW_SWAP) {
338 for (i = len - 1; i >= 0; i--) {
339 sprintf(buf+offset, "%c%02X",
340 (i == len-1)?' ':SWAP_DELIMITER,
345 for (i = 0; i < len; i++) {
346 sprintf(buf+offset, " %02X", cf->data[i]);
353 * The ASCII & ERRORFRAME output is put at a fixed len behind the data.
354 * For now we support ASCII output only for payload length up to 8 bytes.
355 * Does it make sense to write 64 ASCII byte behind 64 ASCII HEX data on the console?
357 if (len > CAN_MAX_DLEN)
360 if (cf->can_id & CAN_ERR_FLAG)
361 sprintf(buf+offset, "%*s", dlen*(8-len)+13, "ERRORFRAME");
362 else if (view & CANLIB_VIEW_ASCII) {
364 if (view & CANLIB_VIEW_SWAP) {
365 sprintf(buf+offset, "%*s", j, "`");
367 for (i = len - 1; i >= 0; i--)
368 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
369 buf[offset++] = cf->data[i];
373 sprintf(buf+offset, "`");
375 sprintf(buf+offset, "%*s", j, "'");
377 for (i = 0; i < len; i++)
378 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
379 buf[offset++] = cf->data[i];
383 sprintf(buf+offset, "'");
388 static const char *error_classes[] = {
391 "controller-problem",
392 "protocol-violation",
393 "transceiver-status",
394 "no-acknowledgement-on-tx",
397 "restarted-after-bus-off",
400 static const char *controller_problems[] = {
409 static const char *protocol_violation_types[] = {
411 "frame-format-error",
412 "bit-stuffing-error",
413 "tx-dominant-bit-error",
414 "tx-recessive-bit-error",
416 "back-to-error-active",
420 static const char *protocol_violation_locations[] = {
440 "tolerate-dominant-bits",
443 "passive-error-flag",
448 "acknowledge-delimiter",
456 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
459 static int snprintf_error_data(char *buf, size_t len, uint8_t err,
460 const char **arr, int arr_len)
462 int i, n = 0, count = 0;
464 if (!err || len <= 0)
467 for (i = 0; i < arr_len; i++) {
468 if (err & (1 << i)) {
470 n += snprintf(buf + n, len - n, ",");
471 n += snprintf(buf + n, len - n, "%s", arr[i]);
479 static int snprintf_error_lostarb(char *buf, size_t len, struct canfd_frame *cf)
483 return snprintf(buf, len, "{at bit %d}", cf->data[0]);
486 static int snprintf_error_ctrl(char *buf, size_t len, struct canfd_frame *cf)
493 n += snprintf(buf + n, len - n, "{");
494 n += snprintf_error_data(buf + n, len - n, cf->data[1],
496 ARRAY_SIZE(controller_problems));
497 n += snprintf(buf + n, len - n, "}");
502 static int snprintf_error_prot(char *buf, size_t len, struct canfd_frame *cf)
509 n += snprintf(buf + n, len - n, "{{");
510 n += snprintf_error_data(buf + n, len - n, cf->data[2],
511 protocol_violation_types,
512 ARRAY_SIZE(protocol_violation_types));
513 n += snprintf(buf + n, len - n, "}{");
514 if (cf->data[3] > 0 &&
515 cf->data[3] < ARRAY_SIZE(protocol_violation_locations))
516 n += snprintf(buf + n, len - n, "%s",
517 protocol_violation_locations[cf->data[3]]);
518 n += snprintf(buf + n, len - n, "}}");
523 void snprintf_can_error_frame(char *buf, size_t len, struct canfd_frame *cf,
527 int i, n = 0, classes = 0;
530 if (!(cf->can_id & CAN_ERR_FLAG))
533 class = cf->can_id & CAN_EFF_MASK;
534 if (class > (1 << ARRAY_SIZE(error_classes))) {
535 fprintf(stderr, "Error class %#x is invalid\n", class);
542 for (i = 0; i < ARRAY_SIZE(error_classes); i++) {
546 n += snprintf(buf + n, len - n, "%s", sep);
547 n += snprintf(buf + n, len - n, "%s", error_classes[i]);
548 if (mask == CAN_ERR_LOSTARB)
549 n += snprintf_error_lostarb(buf + n, len - n,
551 if (mask == CAN_ERR_CRTL)
552 n += snprintf_error_ctrl(buf + n, len - n, cf);
553 if (mask == CAN_ERR_PROT)
554 n += snprintf_error_prot(buf + n, len - n, cf);
559 if (cf->data[6] || cf->data[7]) {
560 n += snprintf(buf + n, len - n, "%s", sep);
561 n += snprintf(buf + n, len - n, "error-counter-tx-rx{{%d}{%d}}",
562 cf->data[6], cf->data[7]);