Compare commits

...

5 Commits

Author SHA1 Message Date
Ben Gardiner 8675d64fb5
Merge 63500f8dd0 into 7939678070 2025-12-13 10:11:52 -08:00
Oliver Hartkopp 7939678070 can-utils: unify indention
Signed-off-by: Oliver Hartkopp <socketcan@hartkopp.net>
2025-12-13 10:38:15 +01:00
Marc Kleine-Budde 88a0417a6b
Merge pull request #609 from dphipps-qnx/fix_cmsg_types
candump: Use SCM_* consts instead of SO_* for cmsg_type
2025-12-05 16:26:30 +01:00
Darcy Phipps 65e715d56d candump: Use SCM_* consts instead of SO_* for cmsg_type
There are explicit #defines for SCM_TIMESTAMP and SCM_TIMESTAMPING. They
currently happen to have the same value as SO_TIMESTAMP and
SO_TIMESTAMPING but the former should be used when checking cmsg_type in
case one day they do not.
2025-12-05 09:35:50 -05:00
Ben Gardiner 63500f8dd0 canplayer-bisect: introduce a tool to hunt for can packets by bisecting replays of candump logs 2016-04-25 20:14:28 -07:00
15 changed files with 895 additions and 686 deletions

View File

@ -72,6 +72,9 @@
#ifndef SO_TIMESTAMPING #ifndef SO_TIMESTAMPING
#define SO_TIMESTAMPING 37 #define SO_TIMESTAMPING 37
#endif #endif
#ifndef SCM_TIMESTAMPING
#define SCM_TIMESTAMPING SO_TIMESTAMPING
#endif
#define TIMESTAMPSZ 50 /* string 'absolute with date' requires max 49 bytes */ #define TIMESTAMPSZ 50 /* string 'absolute with date' requires max 49 bytes */
@ -807,13 +810,13 @@ int main(int argc, char **argv)
for (cmsg = CMSG_FIRSTHDR(&msg); for (cmsg = CMSG_FIRSTHDR(&msg);
cmsg && (cmsg->cmsg_level == SOL_SOCKET); cmsg && (cmsg->cmsg_level == SOL_SOCKET);
cmsg = CMSG_NXTHDR(&msg,cmsg)) { cmsg = CMSG_NXTHDR(&msg,cmsg)) {
if (cmsg->cmsg_type == SO_TIMESTAMP) { if (cmsg->cmsg_type == SCM_TIMESTAMP) {
struct timeval tv; struct timeval tv;
memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv)); memcpy(&tv, CMSG_DATA(cmsg), sizeof(tv));
ts.tv_sec = tv.tv_sec; ts.tv_sec = tv.tv_sec;
ts.tv_nsec = tv.tv_usec; ts.tv_nsec = tv.tv_usec;
ts.tv_nsec *= 1000; ts.tv_nsec *= 1000;
} else if (cmsg->cmsg_type == SO_TIMESTAMPING) { } else if (cmsg->cmsg_type == SCM_TIMESTAMPING) {
struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg); struct timespec *stamp = (struct timespec *)CMSG_DATA(cmsg);
/* /*

205
canplayer-bisect 100755
View File

@ -0,0 +1,205 @@
#!/bin/bash
if [ -z "${CANPLAYER}" ]; then
CANPLAYER="canplayer"
fi
die() {
echo "$*" > /dev/stderr
exit 1
}
usage() {
echo "canplayer-bisect <start|stop|clean|good|yes|bad|no|again|where|undo> <logfile> <canplayer options>"
}
is_ready() {
if [ ! -d .canplayer-bisect ]; then
usage
exit 1
fi
return 0
}
setup() {
is_ready
LOGFILE=$(cat .canplayer-bisect/logfile |head -n 1)
SAVED_LEN="$(cat .canplayer-bisect/len|tail -n 1)"
LEN="$(wc -l ${LOGFILE} | awk '{ print $1 }')"
if [ "$LEN" != "$SAVED_LEN" ]; then
die "logfile has changed size. restart"
fi
CANPLAYER_ARGS=$(cat .canplayer-bisect/args |head -n 1)
HEAD="$(cat .canplayer-bisect/head |tail -n 1)"
TAIL="$(cat .canplayer-bisect/tail |tail -n 1)"
}
back() {
HEAD="$(cat .canplayer-bisect/head |tail -n 2 |head -n1)"
TAIL="$(cat .canplayer-bisect/tail |tail -n 2 |head -n1)"
}
do_undo() {
sed -i '$ d' .canplayer-bisect/head
sed -i '$ d' .canplayer-bisect/tail
}
teardown() {
mkdir -p .canplayer-bisect
echo $LEN > .canplayer-bisect/len
echo $LOGFILE > .canplayer-bisect/logfile
echo $CANPLAYER_ARGS > .canplayer-bisect/args
echo $HEAD >> .canplayer-bisect/head
echo $TAIL >> .canplayer-bisect/tail
}
show() {
cat $LOGFILE | sed -n ${HEAD},${TAIL}p
}
play() {
#we *could* pipe directly to canplayer, but then the user can't add -l i to CANPLAYER_ARGS to hunt for packets using looped playback
the_show="$(mktemp)"
trap "rm -rf \"${the_show}\"" EXIT
show > "${the_show}"
"${CANPLAYER}" ${CANPLAYER_ARGS} -I "${the_show}"
}
do_show() {
setup
show
}
check_heads_n_tails() {
if [ $HEAD -eq $TAIL ]; then
do_stop
fi
}
do_good() {
setup
check_heads_n_tails
if [ $(( $HEAD + 1 )) -eq $TAIL ]; then
TAIL=$HEAD
else
TAIL=$(( ( $TAIL - $HEAD ) / 2 + $HEAD - 1 ))
fi
teardown
play
}
do_bad() {
setup
check_heads_n_tails
back
if [ $(( $HEAD + 1 )) -eq $TAIL ]; then
HEAD=$TAIL
else
HEAD=$(( ( $TAIL - $HEAD ) / 2 + $HEAD ))
fi
teardown
play
}
do_again() {
setup
play
}
do_start() {
do_clean
LEN="$(wc -l ${LOGFILE} | awk '{ print $1 }')"
HEAD=1
TAIL=$LEN
echo "assuming logfile contains the packets you seek... bisecting to first half"
teardown
play
}
do_where() {
setup
echo "between $HEAD and $TAIL (+$(( $TAIL - $HEAD ))) of $LOGFILE"
}
do_stop() {
setup
if [ "$COMMAND" == "no" ]; then
echo "failed to find what you were looking for"
exit 1
else
echo "the packets you seek are:"
do_where
exit 0
fi
}
do_clean() {
rm -rf .canplayer-bisect
}
if [ -z "$1" ]; then
usage
exit 1
fi
COMMAND=$1
if [ ! -d .canplayer-bisect ] && [ ! -z "$2" ] && [ ! -e "$2" ]; then
usage
exit 1
fi
LOGFILE="$2"
shift
shift
CANPLAYER_ARGS="$*"
case "$COMMAND" in
start)
do_start
;;
stop)
do_stop
;;
clean)
do_clean
;;
good|yes)
do_good
;;
bad|no)
do_bad
;;
again)
do_again
;;
where)
do_where
;;
undo)
do_undo
;;
show)
do_show
;;
esac

View File

@ -112,10 +112,10 @@ static struct {
int sig_alrm; int sig_alrm;
int sig_usr1; int sig_usr1;
int state; int state;
#define STATE_INITIAL 0 #define STATE_INITIAL 0
#define STATE_REQ_SENT 1 #define STATE_REQ_SENT 1
#define STATE_REQ_PENDING 2 /* wait 1250 msec for first claim */ #define STATE_REQ_PENDING 2 /* wait 1250 msec for first claim */
#define STATE_OPERATIONAL 3 #define STATE_OPERATIONAL 3
} s = { } s = {
.intf = default_intf, .intf = default_intf,
.ranges = default_range, .ranges = default_range,
@ -126,8 +126,8 @@ static struct {
struct { struct {
uint64_t name; uint64_t name;
int flags; int flags;
#define F_USE 0x01 #define F_USE 0x01
#define F_SEEN 0x02 #define F_SEEN 0x02
} addr[J1939_IDLE_ADDR /* =254 */]; } addr[J1939_IDLE_ADDR /* =254 */];
/* lookup by name */ /* lookup by name */

View File

@ -68,9 +68,9 @@ static struct {
int pkt_len; int pkt_len;
int priority; int priority;
int defined; int defined;
#define DEF_SRC 1 #define DEF_SRC 1
#define DEF_DST 2 #define DEF_DST 2
#define DEF_PRIO 4 #define DEF_PRIO 4
struct sockaddr_can src, dst; struct sockaddr_can src, dst;
} s = { } s = {
.priority = 6, .priority = 6,

7
lib.c
View File

@ -83,8 +83,8 @@ static inline void _put_id(char *buf, int end_offset, canid_t id)
/* CAN DLC to real data length conversion helpers */ /* CAN DLC to real data length conversion helpers */
static const unsigned char dlc2len[] = {0, 1, 2, 3, 4, 5, 6, 7, static const unsigned char dlc2len[] = {
8, 12, 16, 20, 24, 32, 48, 64}; 0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64};
/* get data length from raw data length code (DLC) */ /* get data length from raw data length code (DLC) */
unsigned char can_fd_dlc2len(unsigned char dlc) unsigned char can_fd_dlc2len(unsigned char dlc)
@ -92,7 +92,8 @@ unsigned char can_fd_dlc2len(unsigned char dlc)
return dlc2len[dlc & 0x0F]; return dlc2len[dlc & 0x0F];
} }
static const unsigned char len2dlc[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */ static const unsigned char len2dlc[] = {
0, 1, 2, 3, 4, 5, 6, 7, 8, /* 0 - 8 */
9, 9, 9, 9, /* 9 - 12 */ 9, 9, 9, 9, /* 9 - 12 */
10, 10, 10, 10, /* 13 - 16 */ 10, 10, 10, 10, /* 13 - 16 */
11, 11, 11, 11, /* 17 - 20 */ 11, 11, 11, 11, /* 17 - 20 */