USB 设备 UDev 和 D-BUS [英] USB devices UDev and D-BUS

查看:23
本文介绍了USB 设备 UDev 和 D-BUS的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试获取 Ubuntu 10.10 中当前插入的 USB 设备的列表并监控发生的更改,例如使用 UDev 和 D-BUS 插入或拔出的设备.我对使用 D-BUS 编程还很陌生.我看到了一个例子:Linux:如何检测是否插入和拔出 USB 键盘 只有一个使用 HAL,我知道 HAL 已被弃用.我找到了一些可以工作的代码,对其进行了一些修改,只是它不适用于任何仅设备的存储设备,例如 U 盘、媒体播放器或 cd-rom 设备.我想要鼠标、键盘、USB 相机充电器以及任何插入 USB 的东西,我希望我的程序知道它.这基本上就是我所拥有的(http://moserei.de/2010/01/08/accessing-devicekit-with-dbus-and-python.html ):

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. I'm fairly new to programming using 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. 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. 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. This is basically what I have ( 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

推荐答案

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

The udisks D-Bus service, obviously, only reports disks.

直接监控udev(通过libudev,通过pyudev).

Just monitor udev directly (through libudev, through 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()

这篇关于USB 设备 UDev 和 D-BUS的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆