简体   繁体   English

USB设备UDev和D-BUS

[英]USB devices UDev and D-BUS

I am trying to get a list of currently plugged in USB devices in Ubuntu 10.10 and monitor changes that happen, like devices being plugged in or out using UDev and D-BUS. 我正在尝试获取Ubuntu 10.10中当前插入的USB设备的列表,并监视发生的变化,例如使用UDev和D-BUS插入或拔出的设备。 I'm fairly new to programming using D-BUS. 我是使用D-BUS进行编程的新手。 I saw one example: Linux : How to detect is usb keyboard is plugged and unplugged only that one uses HAL and I know that HAL is deprecated. 我看到一个例子: Linux:如何检测usb键盘是插入和拔出只有一个人使用HAL我知道HAL已被弃用。 I found some working code, modified it a bit, only it doesn't work for any device only storage devices such as usb sticks, media players or cd-rom devices. 我找到了一些工作代码,对它进行了一些修改,只是它不适用于任何设备,只有存储设备,如usb棒,媒体播放器或cd-rom设备。 I want the whole thing mice, keyboards, usb cameras chargers anything that is plugged in to the USB I want my program to know about it. 我希望老鼠,键盘,usb相机充电器充满任何插入USB的东西我希望我的程序知道它。 This is basically what I have ( http://moserei.de/2010/01/08/accessing-devicekit-with-dbus-and-python.html ): 这基本上就是我所拥有的( http://moserei.de/2010/01/08/accessing-devicekit-with-dbus-and-python.html ):

import dbus
import gobject
from dbus.mainloop.glib import DBusGMainLoop

def device_added_callback(device):
    print 'Device %s was added' % (device)

def device_changed_callback(device):
    print 'Device %s was changed' % (device)

#must be done before connecting to DBus
DBusGMainLoop(set_as_default=True)

bus = dbus.SystemBus()

proxy = bus.get_object("org.freedesktop.UDisks", 
                       "/org/freedesktop/UDisks")
iface = dbus.Interface(proxy, "org.freedesktop.UDisks.Device")

devices = iface.get_dbus_method('EnumerateDevices')()

print '%s' % (devices)

#addes two signal listeners
iface.connect_to_signal('DeviceAdded', device_added_callback)
iface.connect_to_signal('DeviceChanged', device_changed_callback)

#start the main loop
mainloop = gobject.MainLoop()
mainloop.run()

Any help would be apreciated. 任何帮助都会被贬低。 Thank you in advance, Calota Romeo 先谢谢你,Calota Romeo

The udisks D-Bus service, obviously, only reports disks. 显然, udisks D-Bus服务仅报告磁盘。

Just monitor udev directly (through libudev, through pyudev ). 只需直接监控udev(通过libudev,通过pyudev )。

import pyudev
context = pyudev.Context()
monitor = pyudev.Monitor.from_netlink(context)
observer = pyudev.pygtk.GUDevMonitorObserver(monitor)
observer.connect('device-added', device_added_callback)
observer.connect('device-changed', device_changed_callback)
monitor.enable_receiving()
mainloop = gobject.MainLoop()
mainloop.run()

This is what I use to list already plugged flash sticks. 这是我用来列出已插入的闪光棒。 You can modify script to your needs 您可以根据需要修改脚本

import dbus  
bus = dbus.SystemBus()

proxy = bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager")
iface = dbus.Interface(proxy, "org.freedesktop.Hal.Manager")

devices = iface.GetAllDevices ()

for device in devices:
  try:
      proxy1 = bus.get_object("org.freedesktop.Hal", device)
      iface1 = dbus.Interface(proxy1, "org.freedesktop.Hal.Device")
      props = iface1.GetAllProperties()

      removable = iface1.GetProperty("storage.removable")
      usb = iface1.GetProperty("storage.bus")
      if usb== "usb":
        print "\n".join(("%s: %s" % (k, props[k]) for k in props)) # shows available properties
  except:
    pass

And this is what I use to see if any usb plugged : 这就是我用来看看是否有任何USB插件:

#!/usr/bin/python
import dbus
import gobject

class DeviceAddedListener:
                def __init__(self):
                                self.bus = dbus.SystemBus()
                                self.hal_manager_obj = self.bus.get_object("org.freedesktop.Hal", "/org/freedesktop/Hal/Manager")
                                self.hal_manager = dbus.Interface(self.hal_manager_obj,"org.freedesktop.Hal.Manager")

                                self.hal_manager.connect_to_signal("DeviceAdded", self._filter) 

                def _filter(self, udi):
                                device_obj = self.bus.get_object ("org.freedesktop.Hal", udi)
                                device = dbus.Interface(device_obj, "org.freedesktop.Hal.Device")
                                self.do_something(device)

                def do_something(self, device):
                                try:

                                                usb = device.GetProperty("storage.bus")
                                                info = device.GetProperty("info.product")
                                                removable = device.GetProperty("storage.removable")
                                                print info
                                except:
                                                pass#blah blah


if __name__ == '__main__':
                from dbus.mainloop.glib import DBusGMainLoop
                DBusGMainLoop(set_as_default=True)
                loop = gobject.MainLoop()
                DeviceAddedListener()
                loop.run()

Here is the example of UDisks : python udisks - enumerating device information 以下是UDisks的示例: python udisks - 枚举设备信息

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM