USB设备UDev和D-BUS

时间:2011-02-24 20:11:16

标签: python linux ubuntu dbus udev

我正在尝试获取Ubuntu 10.10中当前插入的USB设备的列表,并监视发生的更改,例如使用UDev和D-BUS插入或拔出的设备。我是使用D-BUS进行编程的新手。我看到一个例子:Linux : How to detect is usb keyboard is plugged and unplugged只有一个使用HAL,我知道HAL已被弃用。我找到了一些工作代码,对它进行了一些修改,只是它不适用于任何设备,只有存储设备,如usb棒,媒体播放器或cd-rom设备。我希望老鼠,键盘,usb相机充电器充满任何插入USB的东西我希望我的程序知道它。这基本上就是我所拥有的(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()

任何帮助都会被贬低。先感谢您, Calota Romeo

2 个答案:

答案 0 :(得分:10)

udisks D-Bus服务显然只报告磁盘。

直接监控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()

答案 1 :(得分:2)

这是我用来列出已插入闪存棒的内容。您可以根据需要修改脚本

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

这就是我用来看看是否有任何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()

这是UDisks的示例: python udisks - enumerating device information