OpenWRT 加载设备自动执行脚本使用的是 hotplug,官方文档在
https://openwrt.org/docs/guide-user/base-system/hotplug
一般 Linux 可能要使用 udev。
此方案的设计目标是,使支持 OpenWRT 的随身路由,通过 USB/IP,改造成车载CAN采集终端,并可以支持远程实时采集、刷写。
简单的脚本如下。(此处只使用了 ETAS 的产品的 USB VID,其他诸如 Vector,IntrepidCS,Peak 等的,修改 if 的条件即可。)
/etc/hotplug/usb/99-usbcan
#!/bin/sh
# ETAS 58x
# 108c/168/*) # es582.1
# 108c/169/*) # es584.1
# 108c/15a/*) # es583.1
# 108c/159/*) # es581.4
# 108c/15b/*) # es581.4
if [ "${ACTION}" = "bind" ]; then
if [[ "${PRODUCT}" =~ "108c/.*" ]]; then
usbip bind -b ${DEVICENAME}
fi
fi
if [ "${ACTION}" = "remove" ]; then
if [[ "${PRODUCT}" =~ "108c/.*" ]]; then
usbip unbind -b ${DEVICENAME}
fi
fi
附上记录的日志,内容格式
"$PRODUCT detected, Action=${ACTION}, DEVICENAME=${DEVICENAME}, DEVICE_TTY=${DEVICE_TTY}, DEVPATH=${DEVPATH}"
插拔 ES582.1 的日志
108c/168/100 detected, Action=add, DEVICENAME=3-1, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1
108c/168/100 detected, Action=add, DEVICENAME=3-1:1.0, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1/3-1:1.0
108c/168/100 detected, Action=add, DEVICENAME=3-1:1.1, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1/3-1:1.1
108c/168/100 detected, Action=bind, DEVICENAME=3-1, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1
108c/168/100 detected, Action=remove, DEVICENAME=3-1:1.0, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1/3-1:1.0
108c/168/100 detected, Action=remove, DEVICENAME=3-1:1.1, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1/3-1:1.1
108c/168/100 detected, Action=unbind, DEVICENAME=3-1, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1
108c/168/100 detected, Action=remove, DEVICENAME=3-1, DEVICE_TTY=, DEVPATH=/devices/platform/usbdrd3_0/fc000000.usb/xhci-hcd.6.auto/usb3/3-1
其他已知的 USB VIDs:
Vector: 0x1248
Peak System: 0x0C72
Kvaser: 0x0BFD
也可以从Linux 内核源码里找 https://github.com/torvalds/linux/tree/master/drivers/net/can/usb