User Tools

Site Tools


slcan-linux

slcan unter Linux

Mithilfe von udev soll die Funktion realisiert werden, dass sobald der UsbTuCan angesteckt wird, dieser auch als socketcan Device attached wird und beim abstecken automatisch wieder entfernt wird.

udev Regeln

maxi@duplo:~/code/can-utils$ cat /etc/udev/rules.d/90-usb2can.rules 
ACTION=="add", SUBSYSTEM=="tty", ATTRS{idVendor}=="4c54", ATTRS{idProduct}=="5543", RUN+="/usr/local/bin/slcan-udev"
ACTION=="remove", SUBSYSTEM=="tty", ENV{ID_VENDOR_ID}=="4c54", ENV{ID_MODEL_ID}=="5543", RUN+="/usr/local/bin/slcan-udev"

Shellscript für udev

maxi@duplo:~/code/can-utils$ cat /usr/local/bin/slcan-udev
#!/bin/bash
# script to automatically configure slcan through udev
# (c) Maximilian Pachl, m@ximilian.info
# 2015-10-10


# -----------------------------------------------------
# configuration
# -----------------------------------------------------

LOG_FILE="/tmp/slcan.log"
PID_PREFIX="/var/run/slcan"


# -----------------------------------------------------
# helper functions
# -----------------------------------------------------

log()
{
	echo "[$(date)] $1" >> $LOG_FILE;
}

get_tty()
{
	echo $DEVNAME | awk -F "/" '{print $NF}'
}


# -----------------------------------------------------
# udev action handler
# -----------------------------------------------------

add()
{
	TTY=$(get_tty)
	log "new slcan compatible device $TTY found"

	# attch tty to a slcan device
	INTERFACE=$(slcan_attach -s8 -o $DEVNAME | awk 'NF>1{print $NF}')

	# start the slcan userspace daemon
	slcand $TTY $INTERFACE
	echo $(pgrep -f "slcand $TTY $INTERFACE") > $PID_PREFIX-$TTY

	# bring interface up
	ifconfig $INTERFACE up
	log "succesfully configured $TTY as $INTERFACE"
}

remove()
{
	TTY=$(get_tty)
	log "slcan device $(get_tty) removed"
	PID=$(cat "$PID_PREFIX-$TTY")
	kill $PID
	rm $PID_PREFIX-$TTY
	log "successfully removed $TTY, stopped $PID"
}


# -----------------------------------------------------
# main
# -----------------------------------------------------

# check if all environment variables are supplied
if [ -z "$DEVNAME" ]; then
	log "no DEVNAME name supplied"
        exit 1
fi

if [ -z "$ACTION" ]; then
	log "no ACTION supplied"
        exit 1
fi


# process the action issued by udev
case "$ACTION" in
	add)	add;;
	remove)	remove;;
esac
slcan-linux.txt · Last modified: by maxi2