Linux

Udev 規則可在 Linux 上辨識 2 個相同的網路攝像頭

  • August 2, 2019

我的 Raspberry Pi 中插入了 2 個相同的網路攝像頭。我想根據插入的 USB 埠辨別哪個相機是哪個相機。

這就是udev的目的,所以我寫瞭如下規則:

SUBSYSTEM=="usb", KERNEL=="1-1.3", ATTR{idVendor}=="05a3", ATTR{idProduct}=="9422", SYMLINK+="klastle/vlvid"
SUBSYSTEM=="usb", KERNEL=="1-1.5", ATTR{idVendor}=="05a3", ATTR{idProduct}=="9422", SYMLINK+="klastle/irvid"

重新啟動時會顯示這些設備:

root@raspberrypi:~# ls -rlt /dev/klastle/
total 0
lrwxrwxrwx 1 root root 18 Feb 18 01:24 irvid -> ../bus/usb/001/007
lrwxrwxrwx 1 root root 18 Feb 18 01:24 vlvid -> ../bus/usb/001/005

問題是這些不是 linux ( V4L2 ) 設備的影片,我的應用程序無法打開它們。

如何正確編寫規則以將實際的 /dev/video0 和 /dev/video1 符號連結到 /dev/klastle/irvid 和 /dev/klastle/vlvid?

謝謝。

更新:這是兩個設備的 udevadm 查詢輸出:

root@raspberrypi:~# udevadm info --query=all --name=/dev/video3
P: /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.5/1-1.5:1.0/video4linux/video3
N: video3
S: v4l/by-id/usb-Sonix_Technology_Co.__Ltd._USB_2.0_Camera_SN0001-video-index1
S: v4l/by-path/platform-3f980000.usb-usb-0:1.5:1.0-video-index1
E: DEVLINKS=/dev/v4l/by-path/platform-3f980000.usb-usb-0:1.5:1.0-video-index1 /dev/v4l/by-id/usb-Sonix_Technology_Co.__Ltd._USB_2.0_Camera_SN0001-video-index1
E: DEVNAME=/dev/video3
E: DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.5/1-1.5:1.0/video4linux/video3
E: ID_BUS=usb
E: ID_FOR_SEAT=video4linux-platform-3f980000_usb-usb-0_1_5_1_0
E: ID_MODEL=USB_2.0_Camera
E: ID_MODEL_ENC=USB\x202.0\x20Camera
E: ID_MODEL_ID=9422
E: ID_PATH=platform-3f980000.usb-usb-0:1.5:1.0
E: ID_PATH_TAG=platform-3f980000_usb-usb-0_1_5_1_0
E: ID_REVISION=0100
E: ID_SERIAL=Sonix_Technology_Co.__Ltd._USB_2.0_Camera_SN0001
E: ID_SERIAL_SHORT=SN0001
E: ID_TYPE=video
E: ID_USB_DRIVER=uvcvideo
E: ID_USB_INTERFACES=:0e0100:0e0200:010100:010200:
E: ID_USB_INTERFACE_NUM=00
E: ID_V4L_CAPABILITIES=:capture:
E: ID_V4L_PRODUCT=USB 2.0 Camera: H264 USB Camera
E: ID_V4L_VERSION=2
E: ID_VENDOR=Sonix_Technology_Co.__Ltd.
E: ID_VENDOR_ENC=Sonix\x20Technology\x20Co.\x2c\x20Ltd.
E: ID_VENDOR_ID=05a3
E: MAJOR=81
E: MINOR=3
E: SUBSYSTEM=video4linux
E: TAGS=:uaccess:seat:
E: USEC_INITIALIZED=3298533

root@raspberrypi:~# udevadm info --query=all --name=/dev/video0
P: /devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/video4linux/video0
N: video0
S: v4l/by-id/usb-Sonix_Technology_Co.__Ltd._USB_2.0_Camera_SN0001-video-index0
S: v4l/by-path/platform-3f980000.usb-usb-0:1.3:1.0-video-index0
E: DEVLINKS=/dev/v4l/by-path/platform-3f980000.usb-usb-0:1.3:1.0-video-index0 /dev/v4l/by-id/usb-Sonix_Technology_Co.__Ltd._USB_2.0_Camera_SN0001-video-index0
E: DEVNAME=/dev/video0
E: DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.3/1-1.3:1.0/video4linux/video0
E: ID_BUS=usb
E: ID_FOR_SEAT=video4linux-platform-3f980000_usb-usb-0_1_3_1_0
E: ID_MODEL=USB_2.0_Camera
E: ID_MODEL_ENC=USB\x202.0\x20Camera
E: ID_MODEL_ID=9422
E: ID_PATH=platform-3f980000.usb-usb-0:1.3:1.0
E: ID_PATH_TAG=platform-3f980000_usb-usb-0_1_3_1_0
E: ID_REVISION=0100
E: ID_SERIAL=Sonix_Technology_Co.__Ltd._USB_2.0_Camera_SN0001
E: ID_SERIAL_SHORT=SN0001
E: ID_TYPE=video
E: ID_USB_DRIVER=uvcvideo
E: ID_USB_INTERFACES=:0e0100:0e0200:010100:010200:
E: ID_USB_INTERFACE_NUM=00
E: ID_V4L_CAPABILITIES=:capture:
E: ID_V4L_PRODUCT=USB 2.0 Camera: H264 USB Camera
E: ID_V4L_VERSION=2
E: ID_VENDOR=Sonix_Technology_Co.__Ltd.
E: ID_VENDOR_ENC=Sonix\x20Technology\x20Co.\x2c\x20Ltd.
E: ID_VENDOR_ID=05a3
E: MAJOR=81
E: MINOR=0
E: SUBSYSTEM=video4linux
E: TAGS=:seat:uaccess:
E: USEC_INITIALIZED=3289947

您的規則在設備樹中的錯誤級別匹配:SUBSYSTEM=="usb"表示您想要一個符號連結到代表完整 USB 設備的東西,而不是 video4linux 層。所以你需要SUBSYSTEM=video4linux. 您還需要ATTRSand KERNELS(帶有額外的 S)而不是ATTRand KERNEL,因為您想要在樹中更靠前的*位置。*詳情請參閱man udev

獲取此資訊的方法是udevadm使用屬性等吐出到樹根的整個路徑,例如

udevadm info -a -p  $(udevadm info -q path -n /dev/video3)

(您已經為問題的編輯做了內部部分)。

所以規則應該變成類似

SUBSYSTEM=="video4linux", KERNELS=="1-1.3", ATTRS{idVendor}=="05a3", ATTRS{idProduct}=="9422", SYMLINK+="klastle/vlvid"

或類似的,可能取決於您在設備路徑中看到的內容。

引用自:https://unix.stackexchange.com/questions/424887