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 sprintf(buf, "%03X ", cf->can_id & CAN_SFF_MASK);
295 if (maxdlen == CAN_MAX_DLEN) {
296 sprintf(buf+offset, " [%d] ", len);
297 /* standard CAN frames may have RTR enabled */
298 if (cf->can_id & CAN_RTR_FLAG) {
299 sprintf(buf+offset+5, " remote request");
303 sprintf(buf+offset, "[%02d] ", len);
307 if (view & CANLIB_VIEW_BINARY) {
308 dlen = 9; /* _10101010 */
309 if (view & CANLIB_VIEW_SWAP) {
310 for (i = len - 1; i >= 0; i--) {
311 buf[offset++] = (i == len-1)?' ':SWAP_DELIMITER;
312 for (j = 7; j >= 0; j--)
313 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
316 for (i = 0; i < len; i++) {
318 for (j = 7; j >= 0; j--)
319 buf[offset++] = (1<<j & cf->data[i])?'1':'0';
322 buf[offset] = 0; /* terminate string */
325 if (view & CANLIB_VIEW_SWAP) {
326 for (i = len - 1; i >= 0; i--) {
327 sprintf(buf+offset, "%c%02X",
328 (i == len-1)?' ':SWAP_DELIMITER,
333 for (i = 0; i < len; i++) {
334 sprintf(buf+offset, " %02X", cf->data[i]);
341 * The ASCII & ERRORFRAME output is put at a fixed len behind the data.
342 * For now we support ASCII output only for payload length up to 8 bytes.
343 * Does it make sense to write 64 ASCII byte behind 64 ASCII HEX data on the console?
345 if (len > CAN_MAX_DLEN)
348 if (cf->can_id & CAN_ERR_FLAG)
349 sprintf(buf+offset, "%*s", dlen*(8-len)+13, "ERRORFRAME");
350 else if (view & CANLIB_VIEW_ASCII) {
352 if (view & CANLIB_VIEW_SWAP) {
353 sprintf(buf+offset, "%*s", j, "`");
355 for (i = len - 1; i >= 0; i--)
356 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
357 buf[offset++] = cf->data[i];
361 sprintf(buf+offset, "`");
363 sprintf(buf+offset, "%*s", j, "'");
365 for (i = 0; i < len; i++)
366 if ((cf->data[i] > 0x1F) && (cf->data[i] < 0x7F))
367 buf[offset++] = cf->data[i];
371 sprintf(buf+offset, "'");
376 static const char *error_classes[] = {
379 "controller-problem",
380 "protocol-violation",
381 "transceiver-status",
382 "no-acknowledgement-on-tx",
385 "restarted-after-bus-off",
388 static const char *controller_problems[] = {
397 static const char *protocol_violation_types[] = {
399 "frame-format-error",
400 "bit-stuffing-error",
401 "tx-dominant-bit-error",
402 "tx-recessive-bit-error",
404 "back-to-error-active",
408 static const char *protocol_violation_locations[] = {
428 "tolerate-dominant-bits",
431 "passive-error-flag",
436 "acknowledge-delimiter",
444 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof((arr)[0]))
447 static int snprintf_error_data(char *buf, size_t len, uint8_t err,
448 const char **arr, int arr_len)
450 int i, n = 0, count = 0;
452 if (!err || len <= 0)
455 for (i = 0; i < arr_len; i++) {
456 if (err & (1 << i)) {
458 n += snprintf(buf + n, len - n, ",");
459 n += snprintf(buf + n, len - n, "%s", arr[i]);
467 static int snprintf_error_lostarb(char *buf, size_t len, struct canfd_frame *cf)
471 return snprintf(buf, len, "{at bit %d}", cf->data[0]);
474 static int snprintf_error_ctrl(char *buf, size_t len, struct canfd_frame *cf)
481 n += snprintf(buf + n, len - n, "{");
482 n += snprintf_error_data(buf + n, len - n, cf->data[1],
484 ARRAY_SIZE(controller_problems));
485 n += snprintf(buf + n, len - n, "}");
490 static int snprintf_error_prot(char *buf, size_t len, struct canfd_frame *cf)
497 n += snprintf(buf + n, len - n, "{{");
498 n += snprintf_error_data(buf + n, len - n, cf->data[2],
499 protocol_violation_types,
500 ARRAY_SIZE(protocol_violation_types));
501 n += snprintf(buf + n, len - n, "}{");
502 if (cf->data[3] > 0 &&
503 cf->data[3] < ARRAY_SIZE(protocol_violation_locations))
504 n += snprintf(buf + n, len - n, "%s",
505 protocol_violation_locations[cf->data[3]]);
506 n += snprintf(buf + n, len - n, "}}");
511 void snprintf_can_error_frame(char *buf, size_t len, struct canfd_frame *cf,
515 int i, n = 0, classes = 0;
518 if (!(cf->can_id & CAN_ERR_FLAG))
521 class = cf->can_id & CAN_EFF_MASK;
522 if (class > (1 << ARRAY_SIZE(error_classes))) {
523 fprintf(stderr, "Error class %#x is invalid\n", class);
530 for (i = 0; i < ARRAY_SIZE(error_classes); i++) {
534 n += snprintf(buf + n, len - n, "%s", sep);
535 n += snprintf(buf + n, len - n, "%s", error_classes[i]);
536 if (mask == CAN_ERR_LOSTARB)
537 n += snprintf_error_lostarb(buf + n, len - n,
539 if (mask == CAN_ERR_CRTL)
540 n += snprintf_error_ctrl(buf + n, len - n, cf);
541 if (mask == CAN_ERR_PROT)
542 n += snprintf_error_prot(buf + n, len - n, cf);
547 if (cf->data[6] || cf->data[7]) {
548 n += snprintf(buf + n, len - n, "%s", sep);
549 n += snprintf(buf + n, len - n, "error-counter-tx-rx{{%d}{%d}}",
550 cf->data[6], cf->data[7]);