Hi list.

I've made a small example using libusbx that could be useful to someone.
It's a simple USB-enumerator program that prints the VID/PID and names
of your local USB-hardware installed. Here is prints:

0: VID:8086 PID:3B3C -> Intel Corp.                     <not found>
1: VID:8086 PID:3B34 -> Intel Corp.                     <not found>
2: VID:041E PID:405F -> Creative Technology, Ltd        WebCam Vista (VF0330)
3: VID:04A9 PID:174A -> Canon, Inc.                     <not found>
4: VID:0846 PID:6A00 -> NetGear, Inc.                   WG111v2 54 Mbps 
Wireless [RealTek RTL8187L]

5: VID:0951 PID:1624 -> Kingston Technology             DataTraveler G2
6: VID:09DA PID:000A -> A4 Tech Co., Ltd                Optical Mouse Opto 510D
7: VID:0A12 PID:0001 -> Cambridge Silicon Radio, Ltd    Bluetooth Dongle (HCI 
mode)
8: VID:8087 PID:0020 -> Intel Corp.                     Integrated Rate 
Matching Hub
9: VID:8087 PID:0020 -> Intel Corp.                     Integrated Rate 
Matching Hub
10: VID:CACE PID:0001 -> CACE Technologies Inc.          <not found>

I made it with the help of a Python-script I took from the WireShark project.
And as you see the list at http://www.linux-usb.org/usb.ids is a bit incomplete.
examples/make-usb.py uses this URL for the vendors/product IDs.

Don't ask me for a autotool diff since I know very little about it. But the 
commands
I use to build it (MSVC + GNU-make) is:

usb_enum.exe: examples/usb_enum.c examples/usb_vendors.c libusb-1.0.lib
 $(CC) -c $(CFLAGS) examples/usb_enum.c examples/usb_vendors.c
 link $(LDFLAGS) -out:$@ usb_enum.obj usb_vendors.obj libusb-1.0.lib

examples/usb_vendors.c: examples/make-usb.py
       python $^ > $@

For the 'clean' target, add 'rm -f examples/usb_vendors.c'.

The example sources are attached.

--gv

#ifndef _USB_ENUM_H
#define _USB_ENUM_H

struct value_string {
      unsigned long val;
      const char   *string;
    };

extern const struct value_string usb_vendors_vals[];
extern const struct value_string usb_products_vals[];

#endif
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include "libusb.h"
#include "usb_enum.h"

#ifdef _MSC_VER
#define snprintf _snprintf
#endif

/*
* The generated arrays 'usb_vendors_vals[]' and 'usb_products_vals[]' are
* sorted on 'val'. So we break the loops below early when there can be
* no match.
*/

const char *find_vendor_name (unsigned short vendor)
{
 const struct value_string *list = usb_vendors_vals;

 for ( ; list->string && vendor >= list->val; list++) {
   if (list->val == vendor)
      return (list->string);
 }
 return (NULL);
}

const char *find_product_name (unsigned short vendor, unsigned short product)
{
 const struct value_string *list = usb_products_vals;
 unsigned long val = (vendor << 16) + product;

 for ( ; list->string && vendor >= (list->val >> 16); list++) {
   if (list->val == val)
      return (list->string);
 }
 return (NULL);
}

void list_devices (void)
{
 int i, cnt;
 libusb_context *ctx;
 libusb_device **list;

 libusb_init (&ctx);
 cnt = libusb_get_device_list (ctx, &list);

 for (i = 0; i < cnt; i++)
 {
   struct libusb_device_descriptor dd;
   const char *vendor, *product;

   libusb_get_device_descriptor (list[i], &dd);
   printf ("%2d: VID:%04X PID:%04X -> ", i, dd.idVendor, dd.idProduct);
   vendor  = find_vendor_name (dd.idVendor);
   product = find_product_name (dd.idVendor, dd.idProduct);
   printf ("%-30s  %s\n", vendor ? vendor : "<not found>", product ? product : "<not 
found>");
 }

 libusb_free_device_list (list, 1);
 libusb_exit (ctx);
}


int main (int argc, char **argv)
{
 list_devices();
 return (0);
}

Attachment: make-usb.py
Description: Binary data

------------------------------------------------------------------------------
Rapidly troubleshoot problems before they affect your business. Most IT 
organizations don't have a clear picture of how application performance 
affects their revenue. With AppDynamics, you get 100% visibility into your 
Java,.NET, & PHP application. Start your 15-day FREE TRIAL of AppDynamics Pro!
http://pubads.g.doubleclick.net/gampad/clk?id=84349831&iu=/4140/ostg.clktrk
_______________________________________________
libusbx-devel mailing list
libusbx-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/libusbx-devel

Reply via email to