linux usb disconnect,linux usb connect/disconnect event

If you want everything in your single process, you'll have to use libudev to either get events from udevd or directly from the kernel.

Seeing that it might be a problem to use libudev in your application (lack of documentation?), an alternative is to use the udevadm program, which can: report device events after being processed by udevd (udevadm monitor --udev --property),

report devive events directly from the kernel (udevadm monitor --kernel --property), and

dump udevd's database of current devices (but not the kernel's!) (udevadm info --query all --export-db)

udevadm is part of the udev package, but shouldn't need udevd if you only use it to report kernel events. You can use it by having your process spawn it and parse its standard output (but you'll have to launch it via stdbuf -o L ).

Either way, it'll probably be a lot of work. I've already implemented a lot of this in my NCD programming language, including monitoring of USB devices. You might want to take a look at NCD; it's useful for a lot of configuration tasks, and handles hotplugging well. For example, this NCD program will print USB device events to standard output: process main { sys.watch_usb() watcher; println(watcher.event_type, " ", watcher.devname, " ", watcher.vendor_id, ":", watcher.model_id); watcher->nextevent(); }

This will make NCD print something like that (with an initial added event for any USB device that was already plugged in): added /dev/bus/usb/002/045 0409:0059 added /dev/bus/usb/002/046 046d:c313 added /dev/bus/usb/002/047 046d:c03e added /dev/bus/usb/002/048 0557:2008 removed /dev/bus/usb/002/048 0557:2008

You can also use NCD just for this, and parse this standard output - which is much easier to work with than messing with udevadm directly.

Note that NCD itself uses udevadm, and it does require udevd to be running; but why is that a problem anyway? (with some work this dependency could be removed)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值