LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-15-2011, 02:32 PM   #1
cjh21017
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Rep: Reputation: Disabled
Question want to use libusb in C, but confused ...


I'm running ubuntu, have done web search, but find conflicting/confusing information regarding the use of libusb -

first, I have done the following:

apt-get install libusb-dev
apt-get install libusb-1.0-0-dev

I have not been able to find a description of what the difference is between these, of why I need both, or what they really are - can someone illuminate?

second, looking at online examples, I see different function name prefixes some "usb_" some "libusb_", for example:

usb_get_device_list()
libusb_get_device_list()

is one deprecated? or are they for different libraries?

third, when I search my computer I find /usr/include/usb.h but no libusb.h. Again, I hve seen online examples with:

#include <libusb.h>

but is that really for real? Is it maybe deprecated?

fourth, when I build with gcc what library option to I use? -lusb? -llibusb?

Ultimately my goal is to use the libusb functions to extract the manufacturer, product and serial number strings.

Thanks in advance -
Carl
 
Old 06-16-2011, 08:01 AM   #2
knudfl
LQ 5k Club
 
Registered: Jan 2008
Location: Copenhagen, DK
Distribution: pclos2014, Slack14.1 DebWheezy, +50+ other Linux OS, for test only.
Posts: 13,522

Rep: Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503Reputation: 2503
Welcome to LQ.
As nobody else has answered : This is the little I know about libusb:

Package: libusb-1.0-0-dev (2:1.0.8-2) ... Which is "libusb 1"
http://packages.ubuntu.com/maverick/libusb-1.0-0-dev

$ dpkg -L libusb-1.0-0-dev → → /usr/include/libusb-1.0/libusb.h,
/usr/lib/pkgconfig/libusb-1.0.pc : When pkgconfig is used, then :
"#include <libusb.h>" is OK, else do "#include <libusb-1.0/libusb.h>"


Package: libusb-dev (2:0.1.12-15ubuntu2) ... And this one is "libusb 0"
http://packages.ubuntu.com/maverick/libusb-dev

$ dpkg -L libusb-dev → → /usr/include/usb.h
http://packages.ubuntu.com/maverick/...b-dev/filelist


Both versions can be installed at the same time.
Example application using libusb : libk8055
http://libk8055.sourceforge.net/
http://sourceforge.net/projects/libk...iles/libk8055/
..
 
Old 06-16-2011, 08:06 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942Reputation: 942
Quote:
Originally Posted by cjh21017 View Post
I'm running ubuntu, have done web search, but find conflicting/confusing information regarding the use of libusb
libusb 1.0 is an almost-total rewrite of the libusb, and has a different API to version 0.1.

libusb and libusb-dev are version 0.1 of libusb; libusb-1.0-0 and libusb-1.0-0-dev are version 1.0 of libusb.

Quote:
Originally Posted by cjh21017 View Post
second, looking at online examples, I see different function name prefixes some "usb_" some "libusb_"
Correct. These show the differences in the API between the two versions.

If you only compile your own programs against libusb, use libusb-1.0-0 and libusb-1.0-0-dev, i.e. the new API. It is documented here.
(The older 0.1 API is documented here.)

If you only compile your own program, use the new 1.0 interface. The older version is there for applications that use the older version 0.1 API.

Because the two versions have a different API, you can consider them different libraries, from a developer's point of view.

Quote:
Originally Posted by cjh21017 View Post
third, when I search my computer I find /usr/include/usb.h but no libusb.h
No, they're there. In Ubuntu:
  • Programs using the old 0.1 API use
    Code:
    #include <usb.h>
    and compile using
    Code:
    gcc -lusb ...
  • New programs are recommended to use the new 1.0 API,
    Code:
    #include <libusb-1.0/libusb.h>
    and compile using
    Code:
    gcc -lusb-1.0 ...

Hope this helps.
 
1 members found this post helpful.
Old 06-16-2011, 05:32 PM   #4
cjh21017
LQ Newbie
 
Registered: Jun 2011
Posts: 2

Original Poster
Rep: Reputation: Disabled
Talking thanks, works great

Just in case anyone else new to this wants to play, here's my little program to scan ports for manufacturer, product and serial number:

Code:
//==========================================
// experimental files
// COMPILE WITH:
// gcc usb_exp.c -lusb-1.0
//==========================================

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <libusb-1.0/libusb.h>

//=========================================================================
// This program tests out the libusb functions.
//=========================================================================

int main (int argc, char *argv)
{
   libusb_device                    **devList = NULL;
   libusb_device                    *devPtr = NULL;
   libusb_device_handle             *devHandle = NULL;
   struct libusb_device_descriptor  devDesc;

   unsigned char              strDesc[256];
   ssize_t                    numUsbDevs = 0;      // pre-initialized scalars
   ssize_t                    idx = 0;
   int                        retVal = 0;

   //========================================================================
   // test out the libusb functions
   //========================================================================

   printf ("*************************\n USB Experiment Program:\n*************************\n");
   retVal = libusb_init (NULL);

   //========================================================================
   // Get the list of USB devices visible to the system.
   //========================================================================

   numUsbDevs = libusb_get_device_list (NULL, &devList);

   //========================================================================
   // Loop through the list, looking for the device with the required values
   // for Manufacturer ("RockwellCollins") and Product ("TACDIS").
   //========================================================================

   while (idx < numUsbDevs)
   {
      printf ("\n[%d]\n", idx+1);

      //=====================================================================
      // Get next device pointer out of the list, use it to open the device.
      //=====================================================================

      devPtr = devList[idx];

      retVal = libusb_open (devPtr, &devHandle);
      if (retVal != LIBUSB_SUCCESS)
         break;

      //=====================================================================
      // Get the device descriptor for this device.
      //=====================================================================

      retVal = libusb_get_device_descriptor (devPtr, &devDesc);
      if (retVal != LIBUSB_SUCCESS)
         break;

      //=====================================================================
      // Get the string associated with iManufacturer index.
      //=====================================================================

      printf ("   iManufacturer = %d\n", devDesc.iManufacturer);
      if (devDesc.iManufacturer > 0)
      {
         retVal = libusb_get_string_descriptor_ascii
                  (devHandle, devDesc.iManufacturer, strDesc, 256);
         if (retVal < 0)
            break;

         printf ("   string = %s\n",  strDesc);
      }

      //========================================================================
      // Get string associated with iProduct index.
      //========================================================================

      printf ("   iProduct = %d\n", devDesc.iProduct);
      if (devDesc.iProduct > 0)
      {
         retVal = libusb_get_string_descriptor_ascii
                  (devHandle, devDesc.iProduct, strDesc, 256);
         if (retVal < 0)
            break;

         printf ("   string = %s\n", strDesc);
      }

      //==================================================================
      // Get string associated with iSerialNumber index.
      //==================================================================

      printf ("   iSerialNumber = %d\n", devDesc.iSerialNumber);
      if (devDesc.iSerialNumber > 0)
      {
         retVal = libusb_get_string_descriptor_ascii
                  (devHandle, devDesc.iSerialNumber, strDesc, 256);
         if (retVal < 0)
            break;

         printf ("   string = %s\n", strDesc);
      }

      //========================================================================
      // Close and try next one.
      //========================================================================

      libusb_close (devHandle);
      devHandle = NULL;
      idx++;
   }  // end of while loop

   if (devHandle != NULL)
   {
      //========================================================================
      // Close device if left open due to break out of loop on error.
      //========================================================================

      libusb_close (devHandle);
   }   

   libusb_exit (NULL);

   printf ("\n*************************\n        Done\n*************************\n");
   return 0;
}

//==========================================
// EOF
//==========================================
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
learning libusb knobby67 Programming 2 10-03-2010 03:30 AM
[SOLVED] ubuntu 9.10 - #include <libusb/libusb.h> stonehinge03 Linux - Newbie 5 04-20-2010 09:02 AM
driver help with libusb halfwit Linux - Hardware 19 01-23-2008 12:59 AM
How to Install Libusb jsilvester Linux - Newbie 3 01-10-2008 06:34 AM
libUSB install Dutch3 Linux - Newbie 1 04-30-2002 07:15 AM


All times are GMT -5. The time now is 12:17 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration