Thread: USB Plugin Detection in C/C++

  1. #1
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377

    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

  2. #2
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    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.

  3. #3
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    Thnx Kermit for ur reply but its sad to say i need some help on it

  4. #4
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,534
    I know you need some help - but what sort of help? You were pretty vague in your first post. What, specifically, is the trouble?

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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.

  6. #6
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  7. #7
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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

  8. #8
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  9. #9
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

  10. #10
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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)
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  11. #11
    {Jaxom,Imriel,Liam}'s Dad Kennedy's Avatar
    Join Date
    Aug 2006
    Location
    Alabama
    Posts
    1,065
    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.

  12. #12
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    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....

  13. #13
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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.
    Last edited by MK27; 10-08-2009 at 08:10 AM.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  14. #14
    Registered User
    Join Date
    Jan 2009
    Posts
    77
    I assume what that do haldaemon.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about usb
    By h3ro in forum Tech Board
    Replies: 6
    Last Post: 01-18-2009, 05:02 PM
  2. Write protecting USB drive
    By stevesmithx in forum Tech Board
    Replies: 8
    Last Post: 01-17-2009, 04:37 PM
  3. Plugin doesn't load on some systems
    By C+/- in forum C++ Programming
    Replies: 0
    Last Post: 06-16-2007, 08:46 AM
  4. Usb 2.0
    By ober in forum Tech Board
    Replies: 6
    Last Post: 09-06-2003, 10:14 AM
  5. Printers On USB
    By (TNT) in forum A Brief History of Cprogramming.com
    Replies: 1
    Last Post: 03-10-2002, 11:48 AM