![]() |
| | #1 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| USB Plugin Detection in C/C++ I want to write a C/C++ program in which i can detect the USB device plugged in and un plugged in. With libusb or directly getting a notification from KUSBD[Daemon Thread] Whatever. Thnx Alot |
| RockyMarrone is offline | |
| | #2 | |
| ... Join Date: Jan 2003
Posts: 1,190
| Quote:
k thx bye. | |
| kermit is offline | |
| | #3 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Thnx Kermit for ur reply but its sad to say i need some help on it |
| RockyMarrone is offline | |
| | #5 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| Even frankly speaking i knw just the problem but i m not gettin from where to start..... What i now is that there is a daemon thread which always runs kusbd which prompts the application space of linux with hotplug.... can i in C/C++ catch that notification by the daemon thread that this USB device is plugged in/out. like registering any function pointer with the daemon thread or like that. |
| RockyMarrone is offline | |
| | #6 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Hardware is a "kernel space" realm; the things you refer to are "user space" applications that rely on the kernel 100% to inform them. So, you could write a simple kernel module and that will be a sure fire, custom method if you want notification of USB events. But then you have to learn some kernel programming. On the other hand, I have not looked at the API for libusb. "kudbd" or whatever is a distro specific thing, maybe you could use that. There are more universal high level methods you could use such as putting a hook into the kernel log, or into /proc, both of which record USB events. Just keep googling and find a method that works for what you want to do...but on a low level, using the kernel C API is probably the only way.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #7 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| I dont want to write a kernel module for that..... yah when kernel sends a notification to the user space that this device is plugged in i think in /var/log/messages i just want to hook this notification into C/C++ program |
| RockyMarrone is offline | |
| | #8 |
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Well, so write something that monitors /var/log/messages. You could make that generic, since it may have other uses, and then use a socket or signals for communication to your other program. Or you could right something specific that deals with /proc, you will have to investigate how the "usb subsystem" is referred to there. Another possibility, if the device has a corresponding device file in /dev and all you need to know is whether and when it is plugged in, would be to poll that file somehow.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS |
| MK27 is offline | |
| | #9 |
| Jaxom's & Imriel's Dad Join Date: Aug 2006 Location: Alabama
Posts: 801
| If you are truly writing a kernel space app, then you'll register to the USB subsystem and get notifications of the device types you request. If you are just looking to do something user-space, then just interject a script into the hotplug scripts. |
| Kennedy is offline | |
| | #10 | |
| Senior software engineer Join Date: Mar 2007 Location: Portland, OR
Posts: 5,381
| Quote:
(And if it comes down to monitoring messages in a log file I think I'll vomit)
__________________ "Congratulations on your purchase. To begin using your quantum computer, set the power switch to both off and on simultaneously." -- raftpeople@slashdot | |
| brewbuck is offline | |
| | #11 |
| Jaxom's & Imriel's Dad Join Date: Aug 2006 Location: Alabama
Posts: 801
| Nah, it doesn't. It really doesn't even need to be a script (though that is the "normal" way of doing things). Depending on what the hotplug system is (the old style was really freaky), one needs to simply modify the configuration of it (I think this is under /etc/hotplug/) to ensure to trap the notification of the device matching type X [ and vendor Y]. If the hotplug system is udevd, I know that the whole "real" configuration of this beast is under /dev/.udevd but I have yet to work with it (I know how to interface this sucker from kernel space, but not from user space). I recall that it will allow each device attached to run a specific script for user-space configuration, but I have never done this. I've only worked with hotplug (so far). [edit]In the past, the easiest thing that I did was to watch the device (in this case /dev/sda) for insertion by doing a call to dd attempting to read one byte from the device. If the device is not there, dd posts an error (echo $? == "1"). If, however, the device shows up, then I would kick out of my 10 second sleep loop and do something real. When I was done, I'd hit the top of my sleep loop again. Yes, this is a serious hack, but it was lots easier than attempting to keep hotplug happy.[/edit] Last edited by Kennedy; 10-07-2009 at 10:53 AM. |
| Kennedy is offline | |
| | #12 |
| Registered User Join Date: Oct 2009 Location: While(1)
Posts: 316
| From the discussion above i m tryin to get all the messages from udev and there is a utility udevmonitor which tells always when ever the device is plugged in or out.... |
| RockyMarrone is offline | |
| | #13 | ||
| subminimalist Join Date: Jul 2008 Location: NYC
Posts: 3,944
| Quote:
Oh come on it would work. It's also easy, and reliable.If you are actually going to put some days into this, you might as well try and tackle the kernel API and write a little module -- I'm sure it would not be more than 50 lines, and you could communicate with it in user space. Unfortunately the online documentation for this is pretty skimpy tho. Or maybe not...I just found this, which looks like a good, short intro: http://www.freesoftwaremagazine.com/.../drivers_linux Here's the API: http://www.gnugeneration.com/books/l...20/kernel-api/ And if you google around, there are now at least TWO free books online on kernel programming/linux device drivers. Quote:
Code: [root~] lsusb Bus 005 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub Bus 004 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 003 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 002 Device 005: ID 045e:00dd Microsoft Corp. Bus 002 Device 004: ID 045e:00b9 Microsoft Corp. Wireless Optical Mouse 3.0 Bus 002 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub Bus 001 Device 001: ID 1d6b:0001 Linux Foundation 1.1 root hub You could also try "apropos usb" which will give you a list of the manual pages for all commands that have "usb" in their short description.
__________________ Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS Last edited by MK27; 10-08-2009 at 08:10 AM. | ||
| MK27 is offline | |
| | #14 |
| Registered User Join Date: Jan 2009
Posts: 71
| I assume what that do haldaemon. |
| quantt is offline | |
![]() |
| Thread Tools | |
| Display Modes | |
|
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| question about usb | h3ro | Tech Board | 6 | 01-18-2009 05:02 PM |
| Write protecting USB drive | stevesmithx | Tech Board | 8 | 01-17-2009 04:37 PM |
| Plugin doesn't load on some systems | C+/- | C++ Programming | 0 | 06-16-2007 08:46 AM |
| Usb 2.0 | ober | Tech Board | 6 | 09-06-2003 10:14 AM |
| Printers On USB | (TNT) | A Brief History of Cprogramming.com | 1 | 03-10-2002 11:48 AM |