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 const char hex_asc_upper[] = "0123456789ABCDEF";
59 #define hex_asc_upper_lo(x) hex_asc_upper[((x) & 0x0F)]
60 #define hex_asc_upper_hi(x) hex_asc_upper[((x) & 0xF0) >> 4]
62 static inline void put_hex_byte(char *buf, __u8 byte)
64 buf[0] = hex_asc_upper_hi(byte);
65 buf[1] = hex_asc_upper_lo(byte);
68 static inline void _put_id(char *buf, int end_offset, canid_t id)
70 /* build 3 (SFF) or 8 (EFF) digit CAN identifier */
71 while (end_offset >= 0) {
72 buf[end_offset--] = hex_asc_upper[id & 0xF];
77 #define put_sff_id(buf, id) _put_id(buf, 2, id)
78 #define put_eff_id(buf, id) _put_id(buf, 7, id)
80 /* CAN DLC to real data length conversion helpers */
82 static const unsigned char dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7,
83 8, 12, 16, 20, 24, 32, 48, 64};
85 /* get data length from can_dlc with sanitized can_dlc */
86 unsigned char can_dlc2len(unsigned char can_dlc)
88 return dlc2len[can_dlc & 0x0F];
91 static const unsigned char len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
92 9, 9, 9, 9, /* 9 - 12 */
93 10, 10, 10, 10, /* 13 - 16 */
94 11, 11, 11, 11, /* 17 - 20 */
95 12, 12, 12, 12, /* 21 - 24 */
96 13, 13, 13, 13, 13, 13, 13, 13, /* 25 - 32 */
97 14, 14, 14, 14, 14, 14, 14, 14, /* 33 - 40 */
98 14, 14, 14, 14, 14, 14, 14, 14, /* 41 - 48 */
99 15, 15, 15, 15, 15, 15, 15, 15, /* 49 - 56 */
100 15, 15, 15, 15, 15, 15, 15, 15}; /* 57 - 64 */
102 /* map the sanitized data length to an appropriate data length code */
103 unsigned char can_len2dlc(unsigned char len)
111 unsigned char asc2nibble(char c) {
113 if ((c >= '0') && (c <= '9'))
116 if ((c >= 'A') && (c <= 'F'))
119 if ((c >= 'a') && (c <= 'f'))
122 return 16; /* error */
125 int hexstring2data(char *arg, unsigned char *data, int maxdlen) {
127 int len = strlen(arg);
131 if (!len || len%2 || len > maxdlen*2)
134 memset(data, 0, maxdlen);
136 for (i=0; i < len/2; i++) {
138 tmp = asc2nibble(*(arg+(2*i)));
142 data[i] = (tmp << 4);
144 tmp = asc2nibble(*(arg+(2*i)+1));
154 int parse_canframe(char *cs, struct canfd_frame *cf) {
155 /* documentation see lib.h */
157 int i, idx, dlen, len;
158 int maxdlen = CAN_MAX_DLEN;
163 //printf("'%s' len %d\n", cs, len);
165 memset(cf, 0, sizeof(*cf)); /* init CAN FD frame, e.g. LEN = 0 */
170 if (cs[3] == CANID_DELIM) { /* 3 digits */
174 if ((tmp = asc2nibble(cs[i])) > 0x0F)
176 cf->can_id |= (tmp << (2-i)*4);
179 } else if (cs[8] == CANID_DELIM) { /* 8 digits */
183 if ((tmp = asc2nibble(cs[i])) > 0x0F)
185 cf->can_id |= (tmp << (7-i)*4);
187 if (!(cf->can_id & CAN_ERR_FLAG)) /* 8 digits but no errorframe? */
188 cf->can_id |= CAN_EFF_FLAG; /* then it is an extended frame */
193 if((cs[idx] == 'R') || (cs[idx] == 'r')){ /* RTR frame */
194 cf->can_id |= CAN_RTR_FLAG;
196 /* check for optional DLC value for CAN 2.0B frames */
197 if(cs[++idx] && (tmp = asc2nibble(cs[idx])) <= CAN_MAX_DLC)
203 if (cs[idx] == CANID_DELIM) { /* CAN FD frame escape char '##' */
205 maxdlen = CANFD_MAX_DLEN;
208 /* CAN FD frame <canid>##<flags><data>* */
209 if ((tmp = asc2nibble(cs[idx+1])) > 0x0F)
216 for (i=0, dlen=0; i < maxdlen; i++){
218 if(cs[idx] == DATA_SEPERATOR) /* skip (optional) separator */
221 if(idx >= len) /* end of string => end of data */
224 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
226 cf->data[i] = (tmp << 4);
227 if ((tmp = asc2nibble(cs[idx++])) > 0x0F)
237 void fprint_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int sep, int maxdlen) {
238 /* documentation see lib.h */
240 char buf[CL_CFSZ]; /* max length */
242 sprint_canframe(buf, cf, sep, maxdlen);
243 fprintf(stream, "%s", buf);
245 fprintf(stream, "%s", eol);
248 void sprint_canframe(char *buf , struct canfd_frame *cf, int sep, int maxdlen) {
249 /* documentation see lib.h */
252 int len = (cf->len > maxdlen) ? maxdlen : cf->len;
254 if (cf->can_id & CAN_ERR_FLAG) {
255 put_eff_id(buf, cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
258 } else if (cf->can_id & CAN_EFF_FLAG) {
259 put_eff_id(buf, cf->can_id & CAN_EFF_MASK);
263 put_sff_id(buf, cf->can_id & CAN_SFF_MASK);
268 /* standard CAN frames may have RTR enabled. There are no ERR frames with RTR */
269 if (maxdlen == CAN_MAX_DLEN && cf->can_id & CAN_RTR_FLAG) {
271 /* print a given CAN 2.0B DLC if it's not zero */
272 if (cf->len && cf->len <= CAN_MAX_DLC)
273 buf[offset++] = hex_asc_upper[cf->len & 0xF];
279 if (maxdlen == CANFD_MAX_DLEN) {
280 /* add CAN FD specific escape char and flags */
282 buf[offset++] = hex_asc_upper[cf->flags & 0xF];
287 for (i = 0; i < len; i++) {
288 put_hex_byte(buf + offset, cf->data[i]);
290 if (sep && (i+1 < len))
297 void fprint_long_canframe(FILE *stream , struct canfd_frame *cf, char *eol, int view, int maxdlen) {
298 /* documentation see lib.h */
300 char buf[CL_LONGCFSZ];
302 sprint_long_canframe(buf, cf, view, maxdlen);
303 fprintf(stream, "%s", buf);
304 if ((view & CANLIB_VIEW_ERROR) && (cf->can_id & CAN_ERR_FLAG)) {
305 snprintf_can_error_frame(buf, sizeof(buf), cf, "\n\t");
306 fprintf(stream, "\n\t%s", buf);
309 fprintf(stream, "%s", eol);
312 void sprint_long_canframe(char *buf , struct canfd_frame *cf, int view, int maxdlen) {
313 /* documentation see lib.h */
315 int i, j, dlen, offset;
316 int len = (cf->len > maxdlen)? maxdlen : cf->len;
318 /* initialize space for CAN-ID and length information */
319 memset(buf, ' ', 15);
321 if (cf->can_id & CAN_ERR_FLAG) {
322 put_eff_id(buf, cf->can_id & (CAN_ERR_MASK|CAN_ERR_FLAG));
324 } else if (cf->can_id & CAN_EFF_FLAG) {
325 put_eff_id(buf, cf->can_id & CAN_EFF_MASK);
328 if (view & CANLIB_VIEW_INDENT_SFF) {
329 put_sff_id(buf + 5, cf->can_id & CAN_SFF_MASK);
332 put_sff_id(buf, cf->can_id & CAN_SFF_MASK);
337 /* The len value is sanitized by maxdlen (see above) */
338 if (maxdlen == CAN_MAX_DLEN) {
339 buf[offset + 1] = '[';
340 buf[offset + 2] = len + '0';
341 buf[offset + 3] = ']';
343 /* standard CAN frames may have RTR enabled */
344 if (cf->can_id & CAN_RTR_FLAG) {
345 sprintf(buf+offset+5, " remote request");
350 buf[offset + 1] = (len/10) + '0';
351 buf[offset + 2] = (len%10) + '0';
352 buf[offset + 3] = ']';
356 if (view & CANLIB_VIEW_BINARY) {
357 dlen = 9; /* _10101010 */
358 if (view & CANLIB_VIEW_SWAP) {
359 for (i = len - 1; i >= 0; i--) {
360 buf[offset++] = (i == len-1)?' ':SWAP_DELIMITER;
361 for (j = 7; j >= 0; j--)
362 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
365 for (i = 0; i < len; i++) {
367 for (j = 7; j >= 0; j--)
368 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
373 if (view & CANLIB_VIEW_SWAP) {
374 for (i = len - 1; i >= 0; i--) {
378 buf[offset++] = SWAP_DELIMITER;
380 put_hex_byte(buf + offset, cf->data[i]);
384 for (i = 0; i < len; i++) {
386 put_hex_byte(buf + offset, cf->data[i]);
392 buf[offset] = 0; /* terminate string */
395 * The ASCII & ERRORFRAME output is put at a fixed len behind the data.
396 * For now we support ASCII output only for payload length up to 8 bytes.
397 * Does it make sense to write 64 ASCII byte behind 64 ASCII HEX data on the console?
399 if (len > CAN_MAX_DLEN)
402 if (cf->can_id & CAN_ERR_FLAG)
403 sprintf(buf+offset, "%*s", dlen*(8-len)+13, "ERRORFRAME");
404 else if (view & CANLIB_VIEW_ASCII) {
406 if (view & CANLIB_VIEW_SWAP) {
407 sprintf(buf+offset, "%*s", j, "`");
409 for (i = len - 1; i >= 0; i--)
410 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
411 buf[offset++] = cf->data[i];
415 sprintf(buf+offset, "`");
417 sprintf(buf+offset, "%*s", j, "'");
419 for (i = 0; i < len; i++)
420 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
421 buf[offset++] = cf->data[i];
425 sprintf(buf+offset, "'");
430 static const char *error_classes[] = {
433 "controller-problem",
434 "protocol-violation",
435 "transceiver-status",
436 "no-acknowledgement-on-tx",
439 "restarted-after-bus-off",
442 static const char *controller_problems[] = {
451 static const char *protocol_violation_types[] = {
453 "frame-format-error",
454 "bit-stuffing-error",
455 "tx-dominant-bit-error",
456 "tx-recessive-bit-error",
458 "back-to-error-active",
462 static const char *protocol_violation_locations[] = {
482 "tolerate-dominant-bits",
485 "passive-error-flag",
490 "acknowledge-delimiter",
498 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
501 static int snprintf_error_data(char *buf, size_t len, uint8_t err,
502 const char **arr, int arr_len)
504 int i, n = 0, count = 0;
506 if (!err || len <= 0)
509 for (i = 0; i < arr_len; i++) {
510 if (err & (1 << i)) {
512 n += snprintf(buf + n, len - n, ",");
513 n += snprintf(buf + n, len - n, "%s", arr[i]);
521 static int snprintf_error_lostarb(char *buf, size_t len, struct canfd_frame *cf)
525 return snprintf(buf, len, "{at bit %d}", cf->data[0]);
528 static int snprintf_error_ctrl(char *buf, size_t len, struct canfd_frame *cf)
535 n += snprintf(buf + n, len - n, "{");
536 n += snprintf_error_data(buf + n, len - n, cf->data[1],
538 ARRAY_SIZE(controller_problems));
539 n += snprintf(buf + n, len - n, "}");
544 static int snprintf_error_prot(char *buf, size_t len, struct canfd_frame *cf)
551 n += snprintf(buf + n, len - n, "{{");
552 n += snprintf_error_data(buf + n, len - n, cf->data[2],
553 protocol_violation_types,
554 ARRAY_SIZE(protocol_violation_types));
555 n += snprintf(buf + n, len - n, "}{");
556 if (cf->data[3] > 0 &&
557 cf->data[3] < ARRAY_SIZE(protocol_violation_locations))
558 n += snprintf(buf + n, len - n, "%s",
559 protocol_violation_locations[cf->data[3]]);
560 n += snprintf(buf + n, len - n, "}}");
565 void snprintf_can_error_frame(char *buf, size_t len, struct canfd_frame *cf,
569 int i, n = 0, classes = 0;
572 if (!(cf->can_id & CAN_ERR_FLAG))
575 class = cf->can_id & CAN_EFF_MASK;
576 if (class > (1 << ARRAY_SIZE(error_classes))) {
577 fprintf(stderr, "Error class %#x is invalid\n", class);
584 for (i = 0; i < ARRAY_SIZE(error_classes); i++) {
588 n += snprintf(buf + n, len - n, "%s", sep);
589 n += snprintf(buf + n, len - n, "%s", error_classes[i]);
590 if (mask == CAN_ERR_LOSTARB)
591 n += snprintf_error_lostarb(buf + n, len - n,
593 if (mask == CAN_ERR_CRTL)
594 n += snprintf_error_ctrl(buf + n, len - n, cf);
595 if (mask == CAN_ERR_PROT)
596 n += snprintf_error_prot(buf + n, len - n, cf);
601 if (cf->data[6] || cf->data[7]) {
602 n += snprintf(buf + n, len - n, "%s", sep);
603 n += snprintf(buf + n, len - n, "error-counter-tx-rx{{%d}{%d}}",
604 cf->data[6], cf->data[7]);