Commit Graph

49 Commits (99c5c1479054ac9579940d2942fd10e9cf7ada04)

Author SHA1 Message Date
Oliver Hartkopp 8d7d7650d4
can-utils: fix sign-compare warnings (#513)
Fixing several build issues reported by Gary Bisson when he was building
can-utils with clang (AOSP14).

URL: https://github.com/linux-can/can-utils/pull/512
Reported-by: Gary Bisson (https://github.com/gibsson)

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-04-24 20:02:02 +02:00
Oliver Hartkopp 27030b3e54 lib: make sprint_canframe the buffer size aware snprintf_canframe
Make sure the library functions to convert CAN frames into ASCII
represenation do not exceed the given buffer size. This also applies
to the long CAN frame output library function.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-03-12 19:11:58 +01:00
Oliver Hartkopp 8e131401c1 lib: remove obsolete brackets
From review feedback.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-03-06 12:10:32 +01:00
Oliver Hartkopp 34a1cfad29 lib: make parse_canframe CAN XL aware
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-02-28 22:00:19 +01:00
Oliver Hartkopp 3644a54d5b candump/lib: make sprint_long_canframe CAN XL aware
The 'long' CAN frame representation does not really fit for CAN XL content.
Therefore just a cropped output is provided to be able to see the CAN XL
header information and up to 64 byte of data (without binary or swap
formating options). To get the full qualified CAN XL content use the log
file format.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-02-27 20:11:46 +01:00
Oliver Hartkopp 04312e5a10 candump/lib: make sprint_canframe CAN XL aware
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-02-27 18:23:29 +01:00
Oliver Hartkopp f491175d72 lib: remove obsolete fprint_(long_)canframe functions
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-02-27 11:17:49 +01:00
Oliver Hartkopp c6cc7151b7 candump/lib: convert to single output buffer
Build the CAN frame ASCII output inside a single (big) buffer as
preparation for CAN XL support.

This unifies different output modes (fprintf/sprintf/printf) at
the time of the CAN frame text generation.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-02-27 09:29:49 +01:00
Oliver Hartkopp 2522ec127b lib: integrate CAN FD indicator into dual-use struct canfd_frame
Replace maxdlen parameter and use CANFD_FDF flags instead.

Since the CANFD_FDF flag has been introduced in can.h the struct canfd_frame
can be used for CAN CC and CAN FD frames as a dual-use data structure.

Remove the extra maxdlen parameter in library calls and only use the
CANFD_FDF flag to differentiate the two CAN CC/FD frames.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2024-02-24 11:28:35 +01:00
Phillip Schichtel 0e0a033993 Include time.h in lib.c
this fixes the build on android
2024-02-18 16:44:07 +01:00
Oleksij Rempel 6004c64f06 lib: add timespec_diff_ms() and timespec_add_ms() helper functions
This functions will be used by isobusfs.

Signed-off-by: Oleksij Rempel <linux@rempel-privat.de>
2024-02-02 13:31:40 +01:00
Marc Kleine-Budde 7d59157d4d lib: snprintf_can_error_frame(): don't bail out if CAN_ERR_CNT is set
If CAN_ERR_CNT is set, the snprintf_can_error_frame() bails out, as it
cannot decode CAN_ERR_CNT.

Fixes: 21fb43532e ("lib: snprintf_can_error_frame: print counter errors if CAN_ERR_CNT is set")
2023-09-27 16:26:50 +02:00
Marc Kleine-Budde 0a43e84bcd lib: convert towards kernel coding style 2023-09-27 16:26:50 +02:00
Vincent Mailhol 21fb43532e lib: snprintf_can_error_frame: print counter errors if CAN_ERR_CNT is set
Since version 6.0.0 (specifically [1]), the kernel has a new flag:
CAN_ERR_CNT to notify whether or not the error counter is set. Use
this to decide whether on not the error-counter-tx-rx should be
printed.

This way, when a driver set data[6] and data[7] to zero, it is
possible to differentiate whether the error counter is not available
or if the drivers simply reported both counters to be zero.

For interoperability reasons, continue to check data[6] and data[7]
against zero so that the code still works with the old behavior if
run on a kernel version lower than 6.0.0.

[1] commit 3e5c291c7942 ("can: add CAN_ERR_CNT flag to notify
    availability of error counter")
Link: https://git.kernel.org/torvalds/linux/c/3e5c291c7942

Signed-off-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Acked-by: Oliver Hartkopp <socketcan@hartkopp.net>
Link: https://lore.kernel.org/all/20221114163848.3398-4-mailhol.vincent@wanadoo.fr
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2022-11-15 09:44:51 +01:00
Jeroen Hofstee 3bb412e072 lib: fix seperator in snprintf_error_data
When multiple error flags are set, snprintf_error_data created a comma
seperated textual presentation of it. Commit ef853f5553 ("added return value
check of snprintf to prevent possible buffer overflows detected by CodeQL")
removed the increment of count, so the delimiter is no longer added. Fix this
by incrementing count again.

Fixes: Commit ef853f5553 ("added return value check of snprintf to prevent possible buffer overflows detected by CodeQL")
Signed-off-by: Jeroen Hofstee <jhofstee@victronenergy.com>
2022-08-17 16:57:26 +02:00
weichslgartner da0326ffc6 fix for issue #369
adding cast preventing int/unsigned comparison (-Wsign-compare warning in lib.c)
2022-06-30 18:40:07 +02:00
weichslgartner ef853f5553 added return value check of snprintf to prevent possible buffer overflows detected by CodeQL 2022-06-20 19:22:11 +02:00
weichslgartner eb2b38790a fixes undefined behavior in parse_canframe (lib.c:187) by chaning tmp to
canid_t

Issue:
mkdir build && cd build
CC=clang cmake -DCMAKE_C_FLAGS="-fsanitize=address,undefined"
-DCMAKE_EXE_LINKER_FLAGS="-fsanitize=address,undefined" .. && cmake
--build .

 echo "(0.0) can1 ffffffff#00000000" | ./log2long
can-utils/lib.c:187:23: runtime error: left shift of 15 by 28 places
cannot be represented in type 'int'
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior
/can-utils/lib.c:187:23 in
(0.0)  can1  3FFFFFFF
2022-06-20 19:11:00 +02:00
Oliver Hartkopp 972054446a can: rename CAN FD related can_len2dlc and can_dlc2len helpers
The helper functions can_len2dlc and can_dlc2len are only relevant for
CAN FD data length code (DLC) conversion.

Update to latest in-kernel naming scheme:

can_dlc2len -> can_fd_dlc2len to get the payload length from the DLC
can_len2dlc -> can_fd_len2dlc to get the DLC from the payload length

Suggested-by: Vincent Mailhol <mailhol.vincent@wanadoo.fr>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2020-11-22 19:54:58 +01:00
Oliver Hartkopp b70d8c050f lib: add support for Classic CAN raw DLC values
ISO 11898-1 Chapter 8.4.2.3 defines a 4 bit data length code (DLC) table which
maps the DLC to the payload length of the CAN frame in bytes:

    DLC      ->  payload length
    0 .. 8   ->  0 .. 8
    9 .. 15  ->  8

To access the raw DLC values 9 .. 15 the len8_dlc element is introduced, which
is only valid when the payload length 'len' is 8 and the DLC is greater than 8.

The extension for len8_dlc has been implemented for the Classic CAN frame
representation in the log file format and for the standard output for
candump where the raw DLC value is printed in 'unusual' curly braces {}.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2020-11-22 18:59:53 +01:00
Rosen Penev b153fe3f35
clang-tidy: sort includes alphabetically
Found with llvm-include-order

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-10-12 23:28:58 -07:00
Rosen Penev 17a5fe6022
clang-tidy: do not use else after return
Found with readability-else-after-return

Signed-off-by: Rosen Penev <rosenp@gmail.com>
2020-10-12 23:28:22 -07:00
Gary Bisson 46895a41c5 Fix comparison type mismatch warnings
Signed-off-by: Gary Bisson <gary.bisson@boundarydevices.com>
2020-05-28 20:54:37 +02:00
Yegor Yefremov 7b7331976a Add SPDX identifiers
Signed-off-by: Yegor Yefremov <yegorslists@googlemail.com>
2019-06-19 12:24:24 +02:00
Heydar Elahi 1ae8714d0d Another macro usage
Used same macro instead of array
2017-08-09 11:56:05 +04:30
Heydar Elahi 23249c5cd8 Protocol violation location mistake in error message
I changed "id.28-to-id.28" to	"id.28-to-id.21"
2017-08-09 10:44:19 +04:30
Heydar Elahi 243bab70d5 use a macro
the hex_asc_upper_lo macro which is defined before do the same thing.
2017-08-09 02:36:47 +04:30
ajneu ef2ea8fa59 pointer to const in lib 2016-06-13 10:26:53 +02:00
Andri Yngvason 7492b1c873 lib: Update error messages according to new state change handling
This patch updates the error messages for the reworked state change handling
from Andri Yngvason you find in mainline Linux 3.19+

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2014-12-18 14:40:42 +01:00
Oliver Hartkopp 4a68366bac lib: optimize sprint_[long_]canframe()
Inspired by the SLCAN rework from Andre Naujoks this patch replaces the
extensive use of sprintf() with simple and efficient ASCII helpers.

valgrind/kcachegrind reported a cyle estimation of about 5.3 million cycles
before and about 1.5 million cycles after the rework (factor 3.55) for the
logging of the exact same CAN content.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2014-05-05 22:13:38 +02:00
Uwe Kleine-König 03c1bacfde can-utils: trivial: fix typos concerning "separation" and "useful"
Signed-off-by: Uwe Kleine-König <u.kleine-koenig@pengutronix.de>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2013-08-15 21:09:27 +02:00
Oliver Hartkopp eaf87a3bf6 lib: add support for non-zero dlc values in CAN 2.0B RTR frames
As Hakan Engblom pointed out a CAN 2.0B RTR frame can contain a non-zero DLC
value which was not addressed by parse_canframe() and sprint_canframe().

This patch adds support for non-zero DLC values in a way that legacy logfiles
are still usable as the DLC can optionally be added.

Reported-by: Hakan Engblom <H.Engblom@tele-radio.com>
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2013-02-15 23:25:13 +01:00
Oliver Hartkopp 836d3cc012 remove obsolete SVN ID tags
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2012-11-14 20:57:16 +01:00
Oliver Hartkopp fcbdf71f1a Add new format option when mixing EFF/SFF frame output
Added new view CANLIB_VIEW_INDENT_SFF flags to fix the sloppy output of
fprint_long_canframe() when mixing EFF & SFF CAN identifiers.

candump: Once an EFF frame is detected the indention is enabled.

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2012-11-14 19:59:18 +01:00
Oliver Hartkopp e7631bd7f9 canfd: upgrade tools to support CAN FD for Linux 3.6
This is a major upgrade of the basic tools to handle CAN FD frames.
The library to parse and print CAN frames and logfiles has been extended.

In detail:

 asc2log.c      |    5 +
 candump.c      |   24 ++++---
 cangen.c       |  172 +++++++++++++++++++++++++++++++++------------------
 canlogserver.c |   28 +++++---
 canplayer.c    |   25 ++++---
 cansend.c      |   55 ++++++++++++----
 lib.c          |  189 ++++++++++++++++++++++++++++++++++++++-------------------
 lib.h          |  109 ++++++++++++++++++++++++--------
 log2asc.c      |    8 +-
 log2long.c     |   26 ++++++-
 10 files changed, 440 insertions(+), 201 deletions(-)

asc2log.c / log2asc.c
- updates for new lib functions
- still can only handle CAN2.0 frames (no new info about ASC file layout)

log2long.c / canlogserver.c / canplayer.c
- updates for new lib functions to handle CAN FD

lib.h / lib.c
- reworked lib functions to handle CAN FD
- parse_canframe() now returns CAN_MTU and CANFD_MTU on success, 0 at failure
- added can_dlc2len() and can_len2dlc() helpers
- moved hexstring2candata to hexstring2data to support simple byte buffers
- in the long CAN frame representation use %03X/%08X instead of %3X/%8X
- introduced unified buffer size definitions for ASCII CAN frames
- updated documentation

cangen.c
- support CAN FD frames (added -f option to create CAN FD frames)
- added -m option ('mix') to create random extended / RTR / CAN FD frames
- fixed the 'fixed data' option which was zero'ing the payload by the time
- updated help text

candump.c
- support CAN FD frames (print, bridge, log)
- distinguish frame types by length info: [0] = CAN2.0 [00] = CAN FD frame

Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2012-11-14 19:47:21 +01:00
Marc Kleine-Budde 722a09116d treewide: replace berlios contact email by linux-can@vger.kernel.org
Signed-off-by: Marc Kleine-Budde <mkl@pengutronix.de>
2012-01-11 12:14:15 +01:00
Wolfgang Grandegger 71bcd74a7d can-utils/lib: fix build issues introduced with sprintf_can_error_frame()
Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
2011-02-14 16:10:39 +00:00
Wolfgang Grandegger 796475677a candump: add option -e to dump error messages in human readable format
If "candump" is called with the option "-e", the error messages are
dumped in human readable format:

  # candump -e any,0:0,#FFFFFFFF
  ...
  can0  20000088  [8] 00 00 80 19 00 00 00 00   ERRORFRAME
        protocol-violation{{error-on-tx}{acknowledge-slot}}
        bus-error
        error-counter{tx{128}rx{97}}
  ...
  can0  2000008C  [8] 00 08 80 19 00 00 00 00   ERRORFRAME
        controller-problem{tx-error-warning}
        protocol-violation{{error-on-tx}{acknowledge-slot}}
        bus-error
        error-counter{tx{128}rx{97}}

"candump" actually calles a library function snprintf_can_error_frame()
to do the formatting. It could be used for other purposes as well.

Signed-off-by: Wolfgang Grandegger <wg@grandegger.com>
Acked-by: Kurt Van Dijck <kurt.van.dijck@eia.be>
2011-02-14 15:49:27 +00:00
Oliver Hartkopp 450a058072 Fixed parse_canframe() with reordering the checks for the can_id
delimiter. Before this fix the function may have found delimiters in the 
string behind the terminating zero.
2008-11-28 21:50:24 +00:00
Oliver Hartkopp 1be68bcc64 Added support for swapped byte order representation. 2008-10-29 09:23:48 +00:00
Oliver Hartkopp 6410c22fd5 Added silent correction for can_dlc out of range. 2008-07-01 11:31:59 +00:00
Oliver Hartkopp ddbe7d9575 Introduced new functions:
unsigned char asc2nibble(char c);
int hexstring2candata(char *arg, struct can_frame *cf);
(see documentation in lib.h)
As prerequsite to fix the commandline interface of cangen.
2008-06-20 09:15:13 +00:00
Oliver Hartkopp adbe6f9bee Whitespace fixes. Indented the code following Linux styleguide to fix
the mess between tabs and spaces.
2008-06-03 08:46:57 +00:00
Oliver Hartkopp 8a5c0365e6 Added binary output option to candump.c and sprint_long_canframe() in lib.c . 2008-06-02 20:06:37 +00:00
Oliver Hartkopp d0b24ffc10 Fixed contradiction in Sourcecode discalimer.
See details at
http://marc.info/?l=linux-netdev&m=119074747913703&w=2
2007-09-27 12:29:32 +00:00
Oliver Hartkopp 44fb4089c3 Update outdated comment. 2007-09-21 07:21:24 +00:00
Oliver Hartkopp f95fc47ec8 - added error frame support in lib.c
- added sprint_* functions for CAN-frame output in lib.c / lib.h
- added comments / cosmetics

candump.c:
- removed support for the output in ASC representation (moved to log2asc.c)
- added option '-l' for logfile creation e.g. 'candump-2007-01-01_164123.log'
- added funtionality to terminate candump by pressing [ENTER] (not only ^C)
- added error frame support
- added color support even when reading from 'any'
- three different color levels (e.g. -c -c -c)
- making use if lib.c

cangen.c:
-  CAN frames generator for testing purposes (e.g. on vcanx)
(nice when you're on vacancy at the baltic sea and have no real CAN source :)

log2long.c:
- convert compact CAN frame representation into user readable representation

log2asc.c:
- convert compact CAN frame logfile to ASC logfile for 3rd party CAN tools

Next step: Create a tool to replay candump logfiles.
2007-01-01 18:29:52 +00:00
Oliver Hartkopp 50e8c467af Added include <sys/socket.h> to lib.c after discussion on commit-mailing-list.
Fixed comments.
Fixed Makefile (remove all *.o files / added empty line at end of file).
2006-10-31 10:41:21 +00:00
Oliver Hartkopp 99a3bba852 Added tiny tool cansend and a library to parse ASCII CAN frames (e.g. from
the command line) that are defined in one concatenated string.
This is a requirement for the comming command line tool 'bcmsend' that allows
to send more than one CAN frame at a time.
2006-10-30 13:14:36 +00:00