C Board  

Go Back   C Board > Platform Specific Boards > Linux Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 10-07-2009, 07:24 AM   #1
Registered User
 
Join Date: Oct 2009
Location: While(1)
Posts: 316
USB Plugin Detection in C/C++

Hi All,
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   Reply With Quote
Old 10-07-2009, 07:32 AM   #2
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
Quote:
Originally Posted by RockyMarrone View Post
Hi All,
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
That's great. It's good to write programs. Especially ones that can detect USB devices being mounted.

k thx bye.
__________________
Got Ed?

sys-sizes
kermit is offline   Reply With Quote
Old 10-07-2009, 07:33 AM   #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   Reply With Quote
Old 10-07-2009, 07:35 AM   #4
...
 
kermit's Avatar
 
Join Date: Jan 2003
Posts: 1,190
I know you need some help - but what sort of help? You were pretty vague in your first post. What, specifically, is the trouble?
__________________
Got Ed?

sys-sizes
kermit is offline   Reply With Quote
Old 10-07-2009, 07:47 AM   #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   Reply With Quote
Old 10-07-2009, 08:20 AM   #6
subminimalist
 
MK27's Avatar
 
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   Reply With Quote
Old 10-07-2009, 08:30 AM   #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   Reply With Quote
Old 10-07-2009, 08:55 AM   #8
subminimalist
 
MK27's Avatar
 
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   Reply With Quote
Old 10-07-2009, 09:22 AM   #9
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
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   Reply With Quote
Old 10-07-2009, 10:39 AM   #10
Senior software engineer
 
brewbuck's Avatar
 
Join Date: Mar 2007
Location: Portland, OR
Posts: 5,381
Quote:
Originally Posted by Kennedy View Post
If you are just looking to do something user-space, then just interject a script into the hotplug scripts.
I think you're getting to the root of the issue here. How does a "hotplug script" work? It seems like the correct approach isn't to modify those scripts, but to figure out how they are triggered and base the solution on that same method.

(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   Reply With Quote
Old 10-07-2009, 10:49 AM   #11
Jaxom's & Imriel's Dad
 
Kennedy's Avatar
 
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   Reply With Quote
Old 10-08-2009, 05:01 AM   #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   Reply With Quote
Old 10-08-2009, 07:56 AM   #13
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Quote:
Originally Posted by brewbuck View Post
(And if it comes down to monitoring messages in a log file I think I'll vomit)
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:
Originally Posted by RockyMarrone View Post
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....
Of course, if you can find a little system command and use that, you're set. Also check out lsusb:
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
The microsoft stuff is a USB keyboard and mouse, which I stole from some dumbass windows user and is the only USB things connected to my system right now.

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   Reply With Quote
Old 10-14-2009, 03:53 AM   #14
Registered User
 
Join Date: Jan 2009
Posts: 71
I assume what that do haldaemon.
quantt is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

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


All times are GMT -6. The time now is 10:29 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22