2017-02-16 147 views
1

我試圖在我的鍵盤插入我的Raspberry Pi時運行一些代碼(最好在腳本中)。我目前已經安裝了Rasbian Jessie(可能是最新版本?)。我通過研究發現,當usb設備插入Linux機器時,我應該使用「udev」觸發事件。Raspbian Jessie:當我插入我的USB設備時,udev不會觸發

只是爲了測試這個,我希望它能夠在插入我的鍵盤時打開一個瀏覽器(它試圖運行的命令在我的監視器插入到終端時在樹莓派上運行)。我創建了以下文件/etc/udev/rules.d/keyboard.rules

ACTION=="add",ATTRS{idVendor}=="413c",RUN+="epiphany --display=:0 http://www.google.com"

我從lsusb命令得到了idVendor:Bus 001 Device 027: ID 413c:2105 Dell Computer Corp. Model L100 Keyboard

但當我拔下插頭&早在我的鍵盤沒有任何反應。

還有什麼我已經試過:

  • 重啓樹莓派
  • 運行命令sudo udevadm control --reload-rules
  • 運行命令sudo udevadm control --reload
  • 具有keyboard.rules嘗試和運行腳本RUN+='/home/pi/test.sh'(它已被設置爲可運行)。
  • 檢查不同的「桌面」(點擊Crl + Alt + F1 - F12)(儘管只有F1(控制檯)和F7(GUI桌面)處於活動狀態)。
  • 運行udevadm info -a -p $(udevadm info -q path -n /dev/input/event0)(這給了我所有的ATTRS爲鍵盤)
  • 驗證是否udev的是看到「添加」驗證idVendor甚至通過運行udevadm monitor & udevadm monitor --property進來,因爲我在鍵盤
  • 我堵
  • udevadm test $(udevadm info -q path -n /dev/input/event0)進行測試。這顯示以下信息:
 
    calling: test 
    version 215 
    This program is for debugging only, it does not run any program 
    specified by a RUN key. It may show incorrect results, because 
    some values may be different, or not available at a simulation run. 

    load module index 
    Network interface NamePolicy= disabled on kernel commandline, ignoring. 
    timestamp of '/etc/systemd/network' changed 
    timestamp of '/lib/systemd/network' changed 
    Parsed configuration file /lib/systemd/network/99-default.link 
    Created link configuration context. 
    timestamp of '/etc/udev/rules.d' changed 
    timestamp of '/lib/udev/rules.d' changed 
    read rules file: /lib/udev/rules.d/10-local-rpi.rules 
    read rules file: /etc/udev/rules.d/40-scratch.rules 
    read rules file: /lib/udev/rules.d/42-usb-hid-pm.rules 
    read rules file: /lib/udev/rules.d/50-bluetooth-hci-auto-poweron.rules 
    read rules file: /lib/udev/rules.d/50-firmware.rules 
    read rules file: /lib/udev/rules.d/50-udev-default.rules 
    read rules file: /lib/udev/rules.d/55-dm.rules 
    read rules file: /lib/udev/rules.d/60-cdrom_id.rules 
    read rules file: /lib/udev/rules.d/60-crda.rules 
    read rules file: /lib/udev/rules.d/60-drm.rules 
    read rules file: /lib/udev/rules.d/60-fuse.rules 
    read rules file: /lib/udev/rules.d/60-gnupg.rules 
    read rules file: /lib/udev/rules.d/60-keyboard.rules 
    read rules file: /lib/udev/rules.d/60-libgphoto2-6.rules 
    read rules file: /lib/udev/rules.d/60-libpisock9.rules 
    read rules file: /lib/udev/rules.d/60-persistent-alsa.rules 
    read rules file: /lib/udev/rules.d/60-persistent-input.rules 
    read rules file: /lib/udev/rules.d/60-persistent-serial.rules 
    read rules file: /lib/udev/rules.d/60-persistent-storage-dm.rules 
    read rules file: /lib/udev/rules.d/60-persistent-storage-tape.rules 
    read rules file: /lib/udev/rules.d/60-persistent-storage.rules 
    read rules file: /lib/udev/rules.d/60-persistent-v4l.rules 
    read rules file: /lib/udev/rules.d/60-triggerhappy.rules 
    read rules file: /lib/udev/rules.d/61-accelerometer.rules 
    read rules file: /lib/udev/rules.d/64-btrfs.rules 
    read rules file: /lib/udev/rules.d/64-xorg-xkb.rules 
    read rules file: /lib/udev/rules.d/69-libmtp.rules 
    read rules file: /lib/udev/rules.d/70-power-switch.rules 
    read rules file: /lib/udev/rules.d/70-uaccess.rules 
    read rules file: /lib/udev/rules.d/71-seat.rules 
    read rules file: /lib/udev/rules.d/73-idrac.rules 
    read rules file: /lib/udev/rules.d/73-seat-late.rules 
    read rules file: /lib/udev/rules.d/75-net-description.rules 
    read rules file: /lib/udev/rules.d/75-persistent-net-generator.rules 
    read rules file: /lib/udev/rules.d/75-probe_mtd.rules 
    read rules file: /lib/udev/rules.d/75-tty-description.rules 
    read rules file: /lib/udev/rules.d/78-sound-card.rules 
    read rules file: /lib/udev/rules.d/80-drivers.rules 
    read rules file: /lib/udev/rules.d/80-net-setup-link.rules 
    read rules file: /lib/udev/rules.d/80-networking.rules 
    read rules file: /lib/udev/rules.d/80-udisks.rules 
    read rules file: /lib/udev/rules.d/80-udisks2.rules 
    read rules file: /lib/udev/rules.d/85-hdparm.rules 
    read rules file: /lib/udev/rules.d/85-hwclock.rules 
    read rules file: /lib/udev/rules.d/85-regulatory.rules 
    read rules file: /lib/udev/rules.d/90-alsa-restore.rules 
    read rules file: /lib/udev/rules.d/95-udev-late.rules 
    read rules file: /lib/udev/rules.d/95-wedo.rules 
    read rules file: /lib/udev/rules.d/97-hid2hci.rules 
    read rules file: /etc/udev/rules.d/99-com.rules 
    read rules file: /lib/udev/rules.d/99-systemd.rules 
    read rules file: /etc/udev/rules.d/keyboard.rules 
    read rules file: /etc/udev/rules.d/test.rules 
    rules contain 393216 bytes tokens (32768 * 12 bytes), 23387 bytes strings 
    23799 strings (190642 bytes), 21572 de-duplicated (169483 bytes), 2228 trie nodes used 
    IMPORT builtin 'input_id' /lib/udev/rules.d/50-udev-default.rules:10 
    capabilities/ev raw kernel attribute: 120013 
    capabilities/abs raw kernel attribute: 0 
    capabilities/rel raw kernel attribute: 0 
    capabilities/key raw kernel attribute: 10000 7 ff9f207a c14057ff febeffdf ffefffff ffffffff fffffffe 
    test_key: checking bit block 0 for any keys; found=1 
    test_key: checking bit block 32 for any keys; found=1 
    test_key: checking bit block 64 for any keys; found=1 
    test_key: checking bit block 96 for any keys; found=1 
    test_key: checking bit block 128 for any keys; found=1 
    test_key: checking bit block 160 for any keys; found=1 
    test_key: checking bit block 192 for any keys; found=1 
    test_key: checking bit block 224 for any keys; found=1 
    GROUP 101 /lib/udev/rules.d/50-udev-default.rules:29 
    IMPORT builtin 'hwdb' /lib/udev/rules.d/60-keyboard.rules:12 
    IMPORT builtin 'hwdb' returned non-zero 
    IMPORT builtin 'hwdb' /lib/udev/rules.d/60-keyboard.rules:20 
    IMPORT builtin 'hwdb' returned non-zero 
    IMPORT builtin 'usb_id' /lib/udev/rules.d/60-persistent-input.rules:7 
    /sys/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0: if_class 3 protocol 0 
    LINK 'input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd' /lib/udev/rules.d/60-persistent-input.rules:24 
    IMPORT builtin 'path_id' /lib/udev/rules.d/60-persistent-input.rules:31 
    LINK 'input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd' /lib/udev/rules.d/60-persistent-input.rules:33 
    RUN '/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev' /lib/udev/rules.d/60-triggerhappy.rules:4 
    GROUP 101 /etc/udev/rules.d/99-com.rules:1 
    MODE 0660 /etc/udev/rules.d/99-com.rules:1 
    RUN 'epiphany --display=:0 http://www.google.com' /etc/udev/rules.d/keyboard.rules:1 
    handling device node '/dev/input/event0', devnum=c13:64, mode=0660, uid=0, gid=101 
    preserve permissions /dev/input/event0, 020660, uid=0, gid=101 
    preserve already existing symlink '/dev/char/13:64' to '../input/event0' 
    found 'c13:64' claiming '/run/udev/links/\x2finput\x2fby-id\x2fusb-Dell_Dell_USB_Keyboard-event-kbd' 
    creating link '/dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd' to '/dev/input/event0' 
    preserve already existing symlink '/dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd' to '../event0' 
    found 'c13:64' claiming '/run/udev/links/\x2finput\x2fby-path\x2fplatform-3f980000.usb-usb-0:1.2:1.0-event-kbd' 
    creating link '/dev/input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd' to '/dev/input/event0' 
    preserve already existing symlink '/dev/input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd' to '../event0' 
    unable to create temporary db file '/run/udev/data/c13:64.tmp': Permission denied 
    .INPUT_CLASS=kbd 
    ACTION=add 
    BACKSPACE=guess 
    DEVLINKS=/dev/input/by-id/usb-Dell_Dell_USB_Keyboard-event-kbd /dev/input/by-path/platform-3f980000.usb-usb-0:1.2:1.0-event-kbd 
    DEVNAME=/dev/input/event0 
    DEVPATH=/devices/platform/soc/3f980000.usb/usb1/1-1/1-1.2/1-1.2:1.0/0003:413C:2105.0014/input/input23/event0 
    ID_BUS=usb 
    ID_INPUT=1 
    ID_INPUT_KEY=1 
    ID_INPUT_KEYBOARD=1 
    ID_MODEL=Dell_USB_Keyboard 
    ID_MODEL_ENC=Dell\x20USB\x20Keyboard 
    ID_MODEL_ID=2105 
    ID_PATH=platform-3f980000.usb-usb-0:1.2:1.0 
    ID_PATH_TAG=platform-3f980000_usb-usb-0_1_2_1_0 
    ID_REVISION=0352 
    ID_SERIAL=Dell_Dell_USB_Keyboard 
    ID_TYPE=hid 
    ID_USB_DRIVER=usbhid 
    ID_USB_INTERFACES=:030101: 
    ID_USB_INTERFACE_NUM=00 
    ID_VENDOR=Dell 
    ID_VENDOR_ENC=Dell 
    ID_VENDOR_ID=413c 
    MAJOR=13 
    MINOR=64 
    SUBSYSTEM=input 
    USEC_INITIALIZED=78790607 
    XKBLAYOUT=us 
    XKBMODEL=pc105 
    XKBOPTIONS=terminate:ctrl_alt_bksp 
    XKBVARIANT= 
    run: '/usr/sbin/th-cmd --socket /var/run/thd.socket --passfd --udev' 
    run: 'epiphany --display=:0 http://www.google.com' 
    unload module index 
    Unloaded link configuration context. 

你們若向下滾動至底部看到run: 'epiphany --display=:0 http://www.google.com'。這使我相信它應該工作。

但當我拔下插頭&我的鍵盤迴沒有任何反應....

我一直沒能得到udevadm trigger做任何事情(嘗試多路徑等方面的投入,但它只是沒有返回) 。

是否有某種日誌,我可以檢查它是否試圖運行我的命令/腳本?有沒有另一種方法來監視這個腳本?有沒有另一種方法來測試這個腳本,所以我實際上可以看到它在沒有真正插入USB設備的情況下運行?

+0

我創建具有以下內容的腳本controller_retropi.sh: –

+0

我創建了一個腳本controller_retropi.sh,它具有以下內容: '#!/ bin/sh epiphany --display =:0 http://www.google.com ' (把它縮小到777以便它可以運行) 我已經更新了我的'keyboard.rules'到現在: 'ACTION ==「add」,ATTRS {idVendor} ==「413c」,RUN + =「/ bin/bash /home/pi/scripts/controller_retropi.sh&」 我重新運行了'sudo udevadm control --reload-rules'來刷新規則並雙擊檢查它們是否加載了'udevadmin test $(udevadm info -q path -n/dev/input/event0)'。 仍然沒有運氣。 –

+0

我也想出瞭如何使用'sudo udevadm trigger -v -c add -a idVendor = 413c'來觸發。我看到設備彈出,當我監視它時看到它。但運行命令仍然沒有被觸發..... –

回答

1

Are there some sort of logs that I can check to see if it's trying to run my command/script? Is there another way to monitor this script?

找到this線後,我發現,你可以看到在/var/log/syslog文件中的系統日誌文件中的udev日誌。這將僅顯示默認情況下的錯誤,但如果您將日誌級別上升到「調試」,您將能夠看到所有內容。我首先運行udevadm control --log-priority=debug並更新了/etc/udev/udev.conf文件以「調試」。

我發現那是什麼,當我在我的鍵盤插上系統會拋出以下錯誤:

failed to execute '/lib/udev/epiphany --display=:0 http://www.google.com': No such file or directory

啊哈,它似乎是試圖從/ lib/udev文件夾運行epiphany。因此,我再次更新了代碼以運行正確的文件路徑以顯示epiphany:/usr/bin/epiphany --display=:0 http://www.google.com。但我則得到了以下錯誤:

Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com'(err) 'No protocol specified' 
Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com'(err) 'Unable to init server: Could not connect: Connection refused' 
Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com'(out) 'Failed to parse arguments: Cannot open display: :0' 
Feb 19 13:44:10 raspberrypi systemd-udevd[6473]: '/usr/bin/epiphany --display=:0 http://www.google.com' [6479] exit with return code 1 

此代碼工作正常,如果我直接粘貼到一個極端,從而很奇怪,它找不到顯示...最後我想通了,udev的運行一切作爲「根」。顯然「根」不能訪問顯示器。當我通過終端運行命令時,它使用可以訪問顯示器的用戶「pi」。所以最後編輯的命令爲:/sbin/runuser -l pi -c '/usr/bin/epiphany --display=:0 http://www.google.com'。和它工作!

目前的小打嗝是它打開2瀏覽器,但我認爲這是因爲我只是檢查「idVendor」和USB設備有不少孩子分享「idVendor」。因此,當單個鍵盤插入時,會看到多個USB設備並多次運行udev規則。

Is there another way to test this script so I can actually see it run without actually plugging in a usb device?

我在上面提到的意見,但我發現,你可以運行下面的命令,而不必非插&插回鍵盤觸發udev的命令:sudo udevadm trigger -v -c add -a idVendor=413c

相關問題